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