xref: /netbsd-src/games/morse/morse.c (revision 70f7362772ba52b749c976fb5e86e39a8b2c9afc)
1 /*	$NetBSD: morse.c,v 1.20 2023/06/01 04:08:36 mrg 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\
35  The Regents of the University of California.  All rights reserved.");
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.20 2023/06/01 04:08:36 mrg 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 static 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 	{ '_', "..--.-" },
111 	{ '@', ".--.-." },
112 	{ '\0', NULL }
113 };
114 
115 int	main(int, char *[]);
116 static void morse(int);
117 static void decode(const char *);
118 static void show(const char *);
119 
120 static int sflag;
121 static int dflag;
122 
123 int
124 main(int argc, 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(const char *s)
205 {
206 	int i;
207 
208 	for (i = 0; i < 10; i++)
209 		if (strcmp(digit[i], s) == 0) {
210 			putchar('0' + i);
211 			return;
212 		}
213 
214 	for (i = 0; i < 26; i++)
215 		if (strcmp(alph[i], s) == 0) {
216 			putchar('A' + i);
217 			return;
218 		}
219 	i = 0;
220 	while (other[i].c) {
221 		if (strcmp(other[i].morse, s) == 0) {
222 			putchar(other[i].c);
223 			return;
224 		}
225 		i++;
226 	}
227 	if (strcmp("...-.-", s) == 0)
228 		return;
229 	putchar('x');	/* line noise */
230 }
231 
232 void
233 morse(int c)
234 {
235 	int i;
236 
237 	if (isalpha(c))
238 		show(alph[c - (isupper(c) ? 'A' : 'a')]);
239 	else if (isdigit(c))
240 		show(digit[c - '0']);
241 	else if (isspace(c))
242 		show("");  /* could show BT for a pause */
243 	else {
244 		i = 0;
245 		while (other[i].c) {
246 			if (other[i].c == c) {
247 				show(other[i].morse);
248 				break;
249 			}
250 			i++;
251 		}
252 	}
253 }
254 
255 void
256 show(const char *s)
257 {
258 	if (sflag)
259 		printf(" %s", s);
260 	else for (; *s; ++s)
261 		printf(" %s", *s == '.' ? "dit" : "daw");
262 	printf("\n");
263 }
264