]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - morse/morse.c
games: remove trailing whitespace in *.c and *.h
[bsdgames-darwin.git] / morse / morse.c
1 /* $NetBSD: morse.c,v 1.19 2021/05/02 12:50:45 rillig 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.19 2021/05/02 12:50:45 rillig 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 { '\0', NULL }
112 };
113
114 int main(int, char *[]);
115 static void morse(int);
116 static void decode(const char *);
117 static void show(const char *);
118
119 static int sflag;
120 static int dflag;
121
122 int
123 main(int argc, char **argv)
124 {
125 int ch;
126 char *p;
127
128 /* Revoke setgid privileges */
129 setgid(getgid());
130
131 while ((ch = getopt(argc, argv, "ds")) != -1)
132 switch((char)ch) {
133 case 'd':
134 dflag = 1;
135 break;
136 case 's':
137 sflag = 1;
138 break;
139 case '?':
140 default:
141 fprintf(stderr, "usage: morse [-ds] [string ...]\n");
142 exit(1);
143 }
144 argc -= optind;
145 argv += optind;
146
147 if (dflag) {
148 if (*argv) {
149 do {
150 decode(*argv);
151 } while (*++argv);
152 } else {
153 char foo[10]; /* All morse chars shorter than this */
154 int is_blank, i;
155
156 i = 0;
157 is_blank = 0;
158 while ((ch = getchar()) != EOF) {
159 if (ch == '-' || ch == '.') {
160 foo[i++] = ch;
161 if (i == 10) {
162 /* overrun means gibberish--print 'x' and
163 * advance */
164 i = 0;
165 putchar('x');
166 while ((ch = getchar()) != EOF &&
167 (ch == '.' || ch == '-'))
168 ;
169 is_blank = 1;
170 }
171 } else if (i) {
172 foo[i] = '\0';
173 decode(foo);
174 i = 0;
175 is_blank = 0;
176 } else if (isspace(ch)) {
177 if (is_blank) {
178 /* print whitespace for each double blank */
179 putchar(' ');
180 is_blank = 0;
181 } else
182 is_blank = 1;
183 }
184 }
185 }
186 putchar('\n');
187 } else {
188 if (*argv)
189 do {
190 for (p = *argv; *p; ++p)
191 morse((int)*p);
192 show("");
193 } while (*++argv);
194 else while ((ch = getchar()) != EOF)
195 morse(ch);
196 show("...-.-"); /* SK */
197 }
198
199 return 0;
200 }
201
202 void
203 decode(const char *s)
204 {
205 int i;
206
207 for (i = 0; i < 10; i++)
208 if (strcmp(digit[i], s) == 0) {
209 putchar('0' + i);
210 return;
211 }
212
213 for (i = 0; i < 26; i++)
214 if (strcmp(alph[i], s) == 0) {
215 putchar('A' + i);
216 return;
217 }
218 i = 0;
219 while (other[i].c) {
220 if (strcmp(other[i].morse, s) == 0) {
221 putchar(other[i].c);
222 return;
223 }
224 i++;
225 }
226 if (strcmp("...-.-", s) == 0)
227 return;
228 putchar('x'); /* line noise */
229 }
230
231 void
232 morse(int c)
233 {
234 int i;
235
236 if (isalpha(c))
237 show(alph[c - (isupper(c) ? 'A' : 'a')]);
238 else if (isdigit(c))
239 show(digit[c - '0']);
240 else if (isspace(c))
241 show(""); /* could show BT for a pause */
242 else {
243 i = 0;
244 while (other[i].c) {
245 if (other[i].c == c) {
246 show(other[i].morse);
247 break;
248 }
249 i++;
250 }
251 }
252 }
253
254 void
255 show(const char *s)
256 {
257 if (sflag)
258 printf(" %s", s);
259 else for (; *s; ++s)
260 printf(" %s", *s == '.' ? "dit" : "daw");
261 printf("\n");
262 }