Sync usage with man page.
[netbsd-mini2440.git] / games / morse / morse.c
blobf80dd29f110c6ba0dc2cdea2c835efe0c7889cdf
1 /* $NetBSD: morse.c,v 1.15 2008/07/20 01:03:21 lukem Exp $ */
3 /*
4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved.
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.
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.
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 */
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.15 2008/07/20 01:03:21 lukem Exp $");
43 #endif
44 #endif /* not lint */
46 #include <ctype.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
52 static const char
53 *const digit[] = {
54 "-----",
55 ".----",
56 "..---",
57 "...--",
58 "....-",
59 ".....",
60 "-....",
61 "--...",
62 "---..",
63 "----.",
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 "--..",
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 }
113 int main(int, char *[]);
114 static void morse(int);
115 static void decode(const char *);
116 static void show(const char *);
118 static int sflag;
119 static int dflag;
122 main(argc, argv)
123 int argc;
124 char **argv;
126 int ch;
127 char *p;
129 /* Revoke setgid privileges */
130 setgid(getgid());
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);
145 argc -= optind;
146 argv += optind;
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;
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 == '-'))
170 is_blank = 1;
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;
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 */
200 return 0;
203 void
204 decode(s)
205 const char *s;
207 int i;
209 for (i = 0; i < 10; i++)
210 if (strcmp(digit[i], s) == 0) {
211 putchar('0' + i);
212 return;
215 for (i = 0; i < 26; i++)
216 if (strcmp(alph[i], s) == 0) {
217 putchar('A' + i);
218 return;
220 i = 0;
221 while (other[i].c) {
222 if (strcmp(other[i].morse, s) == 0) {
223 putchar(other[i].c);
224 return;
226 i++;
228 if (strcmp("...-.-", s) == 0)
229 return;
230 putchar('x'); /* line noise */
233 void
234 morse(c)
235 int c;
237 int i;
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;
252 i++;
257 void
258 show(s)
259 const char *s;
261 if (sflag)
262 printf(" %s", s);
263 else for (; *s; ++s)
264 printf(" %s", *s == '.' ? "dit" : "daw");
265 printf("\n");