Home | History | Annotate | Line # | Download | only in morse
morse.c revision 1.11
      1 /*	$NetBSD: morse.c,v 1.11 2003/08/07 09:37:30 agc Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1988, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __COPYRIGHT("@(#) Copyright (c) 1988, 1993\n\
     35 	The Regents of the University of California.  All rights reserved.\n");
     36 #endif /* not lint */
     37 
     38 #ifndef lint
     39 #if 0
     40 static char sccsid[] = "@(#)morse.c	8.1 (Berkeley) 5/31/93";
     41 #else
     42 __RCSID("$NetBSD: morse.c,v 1.11 2003/08/07 09:37:30 agc Exp $");
     43 #endif
     44 #endif /* not lint */
     45 
     46 #include <ctype.h>
     47 #include <stdio.h>
     48 #include <stdlib.h>
     49 #include <string.h>
     50 #include <unistd.h>
     51 
     52 #define MORSE_COLON	"--..--"
     53 #define MORSE_PERIOD	".-.-.-"
     54 
     55 
     56 static const char
     57 	*const digit[] = {
     58 	"-----",
     59 	".----",
     60 	"..---",
     61 	"...--",
     62 	"....-",
     63 	".....",
     64 	"-....",
     65 	"--...",
     66 	"---..",
     67 	"----.",
     68 },
     69 	*const alph[] = {
     70 	".-",
     71 	"-...",
     72 	"-.-.",
     73 	"-..",
     74 	".",
     75 	"..-.",
     76 	"--.",
     77 	"....",
     78 	"..",
     79 	".---",
     80 	"-.-",
     81 	".-..",
     82 	"--",
     83 	"-.",
     84 	"---",
     85 	".--.",
     86 	"--.-",
     87 	".-.",
     88 	"...",
     89 	"-",
     90 	"..-",
     91 	"...-",
     92 	".--",
     93 	"-..-",
     94 	"-.--",
     95 	"--..",
     96 };
     97 
     98 int	main __P((int, char *[]));
     99 void	morse __P((int));
    100 void	decode __P((const char *));
    101 void	show __P((const char *));
    102 
    103 static int sflag;
    104 static int dflag;
    105 
    106 int
    107 main(argc, argv)
    108 	int argc;
    109 	char **argv;
    110 {
    111 	int ch;
    112 	char *s, *p;
    113 
    114 	/* Revoke setgid privileges */
    115 	setgid(getgid());
    116 
    117 	while ((ch = getopt(argc, argv, "ds")) != -1)
    118 		switch((char)ch) {
    119 		case 'd':
    120 			dflag = 1;
    121 			break;
    122 		case 's':
    123 			sflag = 1;
    124 			break;
    125 		case '?':
    126 		default:
    127 			fprintf(stderr, "usage: morse [-ds] [string ...]\n");
    128 			exit(1);
    129 		}
    130 	argc -= optind;
    131 	argv += optind;
    132 
    133 	if (dflag) {
    134 		if (*argv) {
    135 			do {
    136 				s=strchr(*argv, ',');
    137 
    138 				if (s)
    139 					*s='\0';
    140 
    141 				decode(*argv);
    142 			} while (*++argv);
    143 		}else{
    144 			char buf[1024];
    145 
    146 			while (fgets(buf, 1024, stdin)) {
    147 				s=buf;
    148 
    149 				while (*s && isspace(*s))
    150 					s++;
    151 
    152 				if (*s) {
    153 					p=strtok(s, " \n\t");
    154 
    155 					while (p) {
    156 						s=strchr(p, ',');
    157 
    158 						if (s)
    159 							*s='\0';
    160 
    161 						decode(p);
    162 						p=strtok(NULL, " \n\t");
    163 					}
    164 				}
    165 			}
    166 		}
    167 		putchar('\n');
    168 	}else{
    169 		if (*argv)
    170 			do {
    171 				for (p = *argv; *p; ++p)
    172 					morse((int)*p);
    173 			} while (*++argv);
    174 		else while ((ch = getchar()) != EOF)
    175 			morse(ch);
    176 	}
    177 
    178 	return 0;
    179 }
    180 
    181 void
    182 decode(s)
    183 	const char *s;
    184 {
    185 	if (strcmp(s, MORSE_COLON) == 0){
    186 		putchar(',');
    187 	} else if (strcmp(s, MORSE_PERIOD) == 0){
    188 		putchar('.');
    189 	} else {
    190 		int found;
    191 		const char *const *a;
    192 		int size;
    193 		int i;
    194 
    195 		found=0;
    196 		a=digit;
    197 		size=sizeof(digit)/sizeof(digit[0]);
    198 		for (i=0; i<size; i++) {
    199 			if (strcmp(a[i], s) == 0) {
    200 				found = 1;
    201 				break;
    202 			}
    203 		}
    204 
    205 		if (found) {
    206 			putchar('0'+i);
    207 			return;
    208 		}
    209 
    210 		found=0;
    211 		a=alph;
    212 		size=sizeof(alph)/sizeof(alph[0]);
    213 		for (i=0; i<size; i++) {
    214 			if (strcmp(a[i], s) == 0) {
    215 				found = 1;
    216 				break;
    217 			}
    218 		}
    219 
    220 		if (found)
    221 			putchar('a'+i);
    222 		else
    223 			putchar(' ');
    224 	}
    225 }
    226 
    227 void
    228 morse(c)
    229 	int c;
    230 {
    231 	if (isalpha(c))
    232 		show(alph[c - (isupper(c) ? 'A' : 'a')]);
    233 	else if (isdigit(c))
    234 		show(digit[c - '0']);
    235 	else if (c == ',')
    236 		show(MORSE_COLON);
    237 	else if (c == '.')
    238 		show(MORSE_PERIOD);
    239 	else if (isspace(c))
    240 		show(" ...\n");
    241 }
    242 
    243 void
    244 show(s)
    245 	const char *s;
    246 {
    247 	if (sflag)
    248 		printf(" %s", s);
    249 	else for (; *s; ++s)
    250 		printf(" %s", *s == '.' ? "dit" : "daw");
    251 	printf(",\n");
    252 }
    253