Home | History | Annotate | Line # | Download | only in morse
morse.c revision 1.14
      1 /*	$NetBSD: morse.c,v 1.14 2008/01/28 06:18:23 dholland 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.14 2008/01/28 06:18:23 dholland 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 static const char
     53 	*const digit[] = {
     54 	"-----",
     55 	".----",
     56 	"..---",
     57 	"...--",
     58 	"....-",
     59 	".....",
     60 	"-....",
     61 	"--...",
     62 	"---..",
     63 	"----.",
     64 },
     65 	*const alph[] = {
     66 	".-",
     67 	"-...",
     68 	"-.-.",
     69 	"-..",
     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 const struct punc {
     95 	char c;
     96 	const char *morse;
     97 } other[] = {
     98 	{ '.', ".-.-.-" },
     99 	{ ',', "--..--" },
    100 	{ ':', "---..." },
    101 	{ '?', "..--.." },
    102 	{ '\'', ".----." },
    103 	{ '-', "-....-" },
    104 	{ '/', "-..-." },
    105 	{ '(', "-.--." },
    106 	{ ')', "-.--.-" },
    107 	{ '"', ".-..-." },
    108 	{ '=', "-...-" },
    109 	{ '+', ".-.-." },
    110 	{ '\0', NULL }
    111 };
    112 
    113 int	main(int, char *[]);
    114 void	morse(int);
    115 void	decode(const char *);
    116 void	show(const char *);
    117 
    118 static int sflag;
    119 static int dflag;
    120 
    121 int
    122 main(argc, argv)
    123 	int argc;
    124 	char **argv;
    125 {
    126 	int ch;
    127 	char *p;
    128 
    129 	/* Revoke setgid privileges */
    130 	setgid(getgid());
    131 
    132 	while ((ch = getopt(argc, argv, "ds")) != -1)
    133 		switch((char)ch) {
    134 		case 'd':
    135 			dflag = 1;
    136 			break;
    137 		case 's':
    138 			sflag = 1;
    139 			break;
    140 		case '?':
    141 		default:
    142 			fprintf(stderr, "usage: morse [-ds] [string ...]\n");
    143 			exit(1);
    144 		}
    145 	argc -= optind;
    146 	argv += optind;
    147 
    148 	if (dflag) {
    149 		if (*argv) {
    150 			do {
    151 				decode(*argv);
    152 			} while (*++argv);
    153 		} else {
    154 			char foo[10];	/* All morse chars shorter than this */
    155 			int is_blank, i;
    156 
    157 			i = 0;
    158 			is_blank = 0;
    159 			while ((ch = getchar()) != EOF) {
    160 				if (ch == '-' || ch == '.') {
    161 					foo[i++] = ch;
    162 					if (i == 10) {
    163 						/* overrun means gibberish--print 'x' and
    164 						 * advance */
    165 						i = 0;
    166 						putchar('x');
    167 						while ((ch = getchar()) != EOF &&
    168 						    (ch == '.' || ch == '-'))
    169 							;
    170 						is_blank = 1;
    171 					}
    172 				} else if (i) {
    173 					foo[i] = '\0';
    174 					decode(foo);
    175 					i = 0;
    176 					is_blank = 0;
    177 				} else if (isspace(ch)) {
    178 					if (is_blank) {
    179 						/* print whitespace for each double blank */
    180 						putchar(' ');
    181 						is_blank = 0;
    182 					} else
    183 						is_blank = 1;
    184 				}
    185 			}
    186 		}
    187 		putchar('\n');
    188 	} else {
    189 		if (*argv)
    190 			do {
    191 				for (p = *argv; *p; ++p)
    192 					morse((int)*p);
    193 				show("");
    194 			} while (*++argv);
    195 		else while ((ch = getchar()) != EOF)
    196 			morse(ch);
    197 		show("...-.-");	/* SK */
    198 	}
    199 
    200 	return 0;
    201 }
    202 
    203 void
    204 decode(s)
    205 	const char *s;
    206 {
    207 	int i;
    208 
    209 	for (i = 0; i < 10; i++)
    210 		if (strcmp(digit[i], s) == 0) {
    211 			putchar('0' + i);
    212 			return;
    213 		}
    214 
    215 	for (i = 0; i < 26; i++)
    216 		if (strcmp(alph[i], s) == 0) {
    217 			putchar('A' + i);
    218 			return;
    219 		}
    220 	i = 0;
    221 	while (other[i].c) {
    222 		if (strcmp(other[i].morse, s) == 0) {
    223 			putchar(other[i].c);
    224 			return;
    225 		}
    226 		i++;
    227 	}
    228 	if (strcmp("...-.-", s) == 0)
    229 		return;
    230 	putchar('x');	/* line noise */
    231 }
    232 
    233 void
    234 morse(c)
    235 	int c;
    236 {
    237 	int i;
    238 
    239 	if (isalpha(c))
    240 		show(alph[c - (isupper(c) ? 'A' : 'a')]);
    241 	else if (isdigit(c))
    242 		show(digit[c - '0']);
    243 	else if (isspace(c))
    244 		show("");  /* could show BT for a pause */
    245 	else {
    246 		i = 0;
    247 		while (other[i].c) {
    248 			if (other[i].c == c) {
    249 				show(other[i].morse);
    250 				break;
    251 			}
    252 			i++;
    253 		}
    254 	}
    255 }
    256 
    257 void
    258 show(s)
    259 	const char *s;
    260 {
    261 	if (sflag)
    262 		printf(" %s", s);
    263 	else for (; *s; ++s)
    264 		printf(" %s", *s == '.' ? "dit" : "daw");
    265 	printf("\n");
    266 }
    267