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