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