morse.c revision 1.8 1 1.8 jsm /* $NetBSD: morse.c,v 1.8 1999/09/12 09:02:22 jsm 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.8 jsm __RCSID("$NetBSD: morse.c,v 1.8 1999/09/12 09:02:22 jsm 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.6 hubertf #include <string.h>
53 1.4 lukem #include <unistd.h>
54 1.1 cgd
55 1.6 hubertf #define MORSE_COLON "--..--"
56 1.6 hubertf #define MORSE_PERIOD ".-.-.-"
57 1.6 hubertf
58 1.6 hubertf
59 1.7 jsm static const char
60 1.7 jsm *const digit[] = {
61 1.1 cgd "-----",
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.7 jsm *const alph[] = {
73 1.1 cgd ".-",
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.4 lukem int main __P((int, char *[]));
102 1.4 lukem void morse __P((int));
103 1.6 hubertf void decode __P((const char *));
104 1.7 jsm void show __P((const char *));
105 1.4 lukem
106 1.1 cgd static int sflag;
107 1.6 hubertf static int dflag;
108 1.1 cgd
109 1.4 lukem int
110 1.1 cgd main(argc, argv)
111 1.1 cgd int argc;
112 1.1 cgd char **argv;
113 1.1 cgd {
114 1.4 lukem int ch;
115 1.6 hubertf char *s, *p;
116 1.8 jsm
117 1.8 jsm /* Revoke setgid privileges */
118 1.8 jsm setregid(getgid(), getgid());
119 1.1 cgd
120 1.6 hubertf while ((ch = getopt(argc, argv, "ds")) != -1)
121 1.1 cgd switch((char)ch) {
122 1.6 hubertf case 'd':
123 1.6 hubertf dflag = 1;
124 1.6 hubertf break;
125 1.1 cgd case 's':
126 1.1 cgd sflag = 1;
127 1.1 cgd break;
128 1.1 cgd case '?':
129 1.1 cgd default:
130 1.6 hubertf fprintf(stderr, "usage: morse [-ds] [string ...]\n");
131 1.1 cgd exit(1);
132 1.1 cgd }
133 1.1 cgd argc -= optind;
134 1.1 cgd argv += optind;
135 1.1 cgd
136 1.6 hubertf if (dflag) {
137 1.6 hubertf if (*argv) {
138 1.6 hubertf do {
139 1.6 hubertf s=strchr(*argv, ',');
140 1.6 hubertf
141 1.6 hubertf if (s)
142 1.6 hubertf *s='\0';
143 1.6 hubertf
144 1.6 hubertf decode(*argv);
145 1.6 hubertf } while (*++argv);
146 1.6 hubertf }else{
147 1.6 hubertf char buf[1024];
148 1.6 hubertf
149 1.6 hubertf while (fgets(buf, 1024, stdin)) {
150 1.6 hubertf s=buf;
151 1.6 hubertf
152 1.6 hubertf while (*s && isspace(*s))
153 1.6 hubertf s++;
154 1.6 hubertf
155 1.6 hubertf if (*s) {
156 1.6 hubertf p=strtok(s, " \n\t");
157 1.6 hubertf
158 1.6 hubertf while (p) {
159 1.6 hubertf s=strchr(p, ',');
160 1.6 hubertf
161 1.6 hubertf if (s)
162 1.6 hubertf *s='\0';
163 1.6 hubertf
164 1.6 hubertf decode(p);
165 1.6 hubertf p=strtok(NULL, " \n\t");
166 1.6 hubertf }
167 1.6 hubertf }
168 1.6 hubertf }
169 1.6 hubertf }
170 1.6 hubertf putchar('\n');
171 1.6 hubertf }else{
172 1.6 hubertf if (*argv)
173 1.6 hubertf do {
174 1.6 hubertf for (p = *argv; *p; ++p)
175 1.6 hubertf morse((int)*p);
176 1.6 hubertf } while (*++argv);
177 1.6 hubertf else while ((ch = getchar()) != EOF)
178 1.6 hubertf morse(ch);
179 1.6 hubertf }
180 1.6 hubertf
181 1.6 hubertf return 0;
182 1.6 hubertf }
183 1.6 hubertf
184 1.6 hubertf void
185 1.6 hubertf decode(s)
186 1.6 hubertf const char *s;
187 1.6 hubertf {
188 1.6 hubertf if (strcmp(s, MORSE_COLON) == 0){
189 1.6 hubertf putchar(',');
190 1.6 hubertf } else if (strcmp(s, MORSE_PERIOD) == 0){
191 1.6 hubertf putchar('.');
192 1.6 hubertf } else {
193 1.6 hubertf int found;
194 1.7 jsm const char *const *a;
195 1.6 hubertf int size;
196 1.6 hubertf int i;
197 1.6 hubertf
198 1.6 hubertf found=0;
199 1.6 hubertf a=digit;
200 1.6 hubertf size=sizeof(digit)/sizeof(digit[0]);
201 1.6 hubertf for (i=0; i<size; i++) {
202 1.6 hubertf if (strcmp(a[i], s) == 0) {
203 1.6 hubertf found = 1;
204 1.6 hubertf break;
205 1.6 hubertf }
206 1.6 hubertf }
207 1.6 hubertf
208 1.6 hubertf if (found) {
209 1.6 hubertf putchar('0'+i);
210 1.6 hubertf return;
211 1.6 hubertf }
212 1.6 hubertf
213 1.6 hubertf found=0;
214 1.6 hubertf a=alph;
215 1.6 hubertf size=sizeof(alph)/sizeof(alph[0]);
216 1.6 hubertf for (i=0; i<size; i++) {
217 1.6 hubertf if (strcmp(a[i], s) == 0) {
218 1.6 hubertf found = 1;
219 1.6 hubertf break;
220 1.6 hubertf }
221 1.6 hubertf }
222 1.6 hubertf
223 1.6 hubertf if (found)
224 1.6 hubertf putchar('a'+i);
225 1.6 hubertf else
226 1.6 hubertf putchar(' ');
227 1.6 hubertf }
228 1.1 cgd }
229 1.1 cgd
230 1.4 lukem void
231 1.1 cgd morse(c)
232 1.4 lukem int c;
233 1.1 cgd {
234 1.1 cgd if (isalpha(c))
235 1.1 cgd show(alph[c - (isupper(c) ? 'A' : 'a')]);
236 1.1 cgd else if (isdigit(c))
237 1.1 cgd show(digit[c - '0']);
238 1.1 cgd else if (c == ',')
239 1.6 hubertf show(MORSE_COLON);
240 1.1 cgd else if (c == '.')
241 1.6 hubertf show(MORSE_PERIOD);
242 1.1 cgd else if (isspace(c))
243 1.1 cgd show(" ...\n");
244 1.1 cgd }
245 1.1 cgd
246 1.4 lukem void
247 1.1 cgd show(s)
248 1.7 jsm const char *s;
249 1.1 cgd {
250 1.1 cgd if (sflag)
251 1.1 cgd printf(" %s", s);
252 1.1 cgd else for (; *s; ++s)
253 1.1 cgd printf(" %s", *s == '.' ? "dit" : "daw");
254 1.1 cgd printf(",\n");
255 1.1 cgd }
256