]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - morse/morse.c
This patch fixes adventure(6) to use `extern' on declarations of
[bsdgames-darwin.git] / morse / morse.c
1 /* $NetBSD: morse.c,v 1.6 1998/11/18 14:22:32 hubertf 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1988, 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)morse.c 8.1 (Berkeley) 5/31/93";
45 #else
46 __RCSID("$NetBSD: morse.c,v 1.6 1998/11/18 14:22:32 hubertf Exp $");
47 #endif
48 #endif /* not lint */
49
50 #include <ctype.h>
51 #include <stdio.h>
52 #include <string.h>
53 #include <unistd.h>
54
55 #define MORSE_COLON "--..--"
56 #define MORSE_PERIOD ".-.-.-"
57
58
59 static char
60 *digit[] = {
61 "-----",
62 ".----",
63 "..---",
64 "...--",
65 "....-",
66 ".....",
67 "-....",
68 "--...",
69 "---..",
70 "----.",
71 },
72 *alph[] = {
73 ".-",
74 "-...",
75 "-.-.",
76 "-..",
77 ".",
78 "..-.",
79 "--.",
80 "....",
81 "..",
82 ".---",
83 "-.-",
84 ".-..",
85 "--",
86 "-.",
87 "---",
88 ".--.",
89 "--.-",
90 ".-.",
91 "...",
92 "-",
93 "..-",
94 "...-",
95 ".--",
96 "-..-",
97 "-.--",
98 "--..",
99 };
100
101 int main __P((int, char *[]));
102 void morse __P((int));
103 void decode __P((const char *));
104 void show __P((char *));
105
106 static int sflag;
107 static int dflag;
108
109 int
110 main(argc, argv)
111 int argc;
112 char **argv;
113 {
114 int ch;
115 char *s, *p;
116
117 while ((ch = getopt(argc, argv, "ds")) != -1)
118 switch((char)ch) {
119 case 'd':
120 dflag = 1;
121 break;
122 case 's':
123 sflag = 1;
124 break;
125 case '?':
126 default:
127 fprintf(stderr, "usage: morse [-ds] [string ...]\n");
128 exit(1);
129 }
130 argc -= optind;
131 argv += optind;
132
133 if (dflag) {
134 if (*argv) {
135 do {
136 s=strchr(*argv, ',');
137
138 if (s)
139 *s='\0';
140
141 decode(*argv);
142 } while (*++argv);
143 }else{
144 char buf[1024];
145
146 while (fgets(buf, 1024, stdin)) {
147 s=buf;
148
149 while (*s && isspace(*s))
150 s++;
151
152 if (*s) {
153 p=strtok(s, " \n\t");
154
155 while (p) {
156 s=strchr(p, ',');
157
158 if (s)
159 *s='\0';
160
161 decode(p);
162 p=strtok(NULL, " \n\t");
163 }
164 }
165 }
166 }
167 putchar('\n');
168 }else{
169 if (*argv)
170 do {
171 for (p = *argv; *p; ++p)
172 morse((int)*p);
173 } while (*++argv);
174 else while ((ch = getchar()) != EOF)
175 morse(ch);
176 }
177
178 return 0;
179 }
180
181 void
182 decode(s)
183 const char *s;
184 {
185 if (strcmp(s, MORSE_COLON) == 0){
186 putchar(',');
187 } else if (strcmp(s, MORSE_PERIOD) == 0){
188 putchar('.');
189 } else {
190 int found;
191 char **a;
192 int size;
193 int i;
194
195 found=0;
196 a=digit;
197 size=sizeof(digit)/sizeof(digit[0]);
198 for (i=0; i<size; i++) {
199 if (strcmp(a[i], s) == 0) {
200 found = 1;
201 break;
202 }
203 }
204
205 if (found) {
206 putchar('0'+i);
207 return;
208 }
209
210 found=0;
211 a=alph;
212 size=sizeof(alph)/sizeof(alph[0]);
213 for (i=0; i<size; i++) {
214 if (strcmp(a[i], s) == 0) {
215 found = 1;
216 break;
217 }
218 }
219
220 if (found)
221 putchar('a'+i);
222 else
223 putchar(' ');
224 }
225 }
226
227 void
228 morse(c)
229 int c;
230 {
231 if (isalpha(c))
232 show(alph[c - (isupper(c) ? 'A' : 'a')]);
233 else if (isdigit(c))
234 show(digit[c - '0']);
235 else if (c == ',')
236 show(MORSE_COLON);
237 else if (c == '.')
238 show(MORSE_PERIOD);
239 else if (isspace(c))
240 show(" ...\n");
241 }
242
243 void
244 show(s)
245 char *s;
246 {
247 if (sflag)
248 printf(" %s", s);
249 else for (; *s; ++s)
250 printf(" %s", *s == '.' ? "dit" : "daw");
251 printf(",\n");
252 }