morse.c revision 1.17 1 1.17 dholland /* $NetBSD: morse.c,v 1.17 2012/06/19 05:46:08 dholland 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.11 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.4 lukem #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.15 lukem __COPYRIGHT("@(#) Copyright (c) 1988, 1993\
35 1.15 lukem The Regents of the University of California. All rights reserved.");
36 1.1 cgd #endif /* not lint */
37 1.1 cgd
38 1.1 cgd #ifndef lint
39 1.3 cgd #if 0
40 1.3 cgd static char sccsid[] = "@(#)morse.c 8.1 (Berkeley) 5/31/93";
41 1.3 cgd #else
42 1.17 dholland __RCSID("$NetBSD: morse.c,v 1.17 2012/06/19 05:46:08 dholland Exp $");
43 1.3 cgd #endif
44 1.1 cgd #endif /* not lint */
45 1.1 cgd
46 1.4 lukem #include <ctype.h>
47 1.1 cgd #include <stdio.h>
48 1.10 matt #include <stdlib.h>
49 1.6 hubertf #include <string.h>
50 1.4 lukem #include <unistd.h>
51 1.1 cgd
52 1.7 jsm static const char
53 1.7 jsm *const digit[] = {
54 1.1 cgd "-----",
55 1.1 cgd ".----",
56 1.1 cgd "..---",
57 1.1 cgd "...--",
58 1.1 cgd "....-",
59 1.1 cgd ".....",
60 1.1 cgd "-....",
61 1.1 cgd "--...",
62 1.1 cgd "---..",
63 1.1 cgd "----.",
64 1.1 cgd },
65 1.7 jsm *const alph[] = {
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.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.16 dholland static const struct punc {
95 1.13 jsm char c;
96 1.13 jsm const char *morse;
97 1.13 jsm } other[] = {
98 1.13 jsm { '.', ".-.-.-" },
99 1.13 jsm { ',', "--..--" },
100 1.13 jsm { ':', "---..." },
101 1.13 jsm { '?', "..--.." },
102 1.13 jsm { '\'', ".----." },
103 1.13 jsm { '-', "-....-" },
104 1.13 jsm { '/', "-..-." },
105 1.13 jsm { '(', "-.--." },
106 1.13 jsm { ')', "-.--.-" },
107 1.13 jsm { '"', ".-..-." },
108 1.13 jsm { '=', "-...-" },
109 1.13 jsm { '+', ".-.-." },
110 1.13 jsm { '\0', NULL }
111 1.13 jsm };
112 1.13 jsm
113 1.12 jsm int main(int, char *[]);
114 1.16 dholland static void morse(int);
115 1.16 dholland static void decode(const char *);
116 1.16 dholland static void show(const char *);
117 1.4 lukem
118 1.1 cgd static int sflag;
119 1.6 hubertf static int dflag;
120 1.1 cgd
121 1.4 lukem int
122 1.17 dholland main(int argc, char **argv)
123 1.1 cgd {
124 1.4 lukem int ch;
125 1.13 jsm char *p;
126 1.8 jsm
127 1.8 jsm /* Revoke setgid privileges */
128 1.9 mycroft setgid(getgid());
129 1.1 cgd
130 1.6 hubertf while ((ch = getopt(argc, argv, "ds")) != -1)
131 1.1 cgd switch((char)ch) {
132 1.6 hubertf case 'd':
133 1.6 hubertf dflag = 1;
134 1.6 hubertf break;
135 1.1 cgd case 's':
136 1.1 cgd sflag = 1;
137 1.1 cgd break;
138 1.1 cgd case '?':
139 1.1 cgd default:
140 1.6 hubertf fprintf(stderr, "usage: morse [-ds] [string ...]\n");
141 1.1 cgd exit(1);
142 1.1 cgd }
143 1.1 cgd argc -= optind;
144 1.1 cgd argv += optind;
145 1.1 cgd
146 1.6 hubertf if (dflag) {
147 1.6 hubertf if (*argv) {
148 1.6 hubertf do {
149 1.6 hubertf decode(*argv);
150 1.6 hubertf } while (*++argv);
151 1.13 jsm } else {
152 1.13 jsm char foo[10]; /* All morse chars shorter than this */
153 1.14 dholland int is_blank, i;
154 1.13 jsm
155 1.13 jsm i = 0;
156 1.14 dholland is_blank = 0;
157 1.13 jsm while ((ch = getchar()) != EOF) {
158 1.13 jsm if (ch == '-' || ch == '.') {
159 1.13 jsm foo[i++] = ch;
160 1.13 jsm if (i == 10) {
161 1.13 jsm /* overrun means gibberish--print 'x' and
162 1.13 jsm * advance */
163 1.13 jsm i = 0;
164 1.13 jsm putchar('x');
165 1.13 jsm while ((ch = getchar()) != EOF &&
166 1.13 jsm (ch == '.' || ch == '-'))
167 1.13 jsm ;
168 1.14 dholland is_blank = 1;
169 1.6 hubertf }
170 1.13 jsm } else if (i) {
171 1.13 jsm foo[i] = '\0';
172 1.13 jsm decode(foo);
173 1.13 jsm i = 0;
174 1.14 dholland is_blank = 0;
175 1.13 jsm } else if (isspace(ch)) {
176 1.14 dholland if (is_blank) {
177 1.13 jsm /* print whitespace for each double blank */
178 1.13 jsm putchar(' ');
179 1.14 dholland is_blank = 0;
180 1.13 jsm } else
181 1.14 dholland is_blank = 1;
182 1.6 hubertf }
183 1.6 hubertf }
184 1.6 hubertf }
185 1.6 hubertf putchar('\n');
186 1.13 jsm } else {
187 1.6 hubertf if (*argv)
188 1.6 hubertf do {
189 1.6 hubertf for (p = *argv; *p; ++p)
190 1.6 hubertf morse((int)*p);
191 1.13 jsm show("");
192 1.6 hubertf } while (*++argv);
193 1.6 hubertf else while ((ch = getchar()) != EOF)
194 1.6 hubertf morse(ch);
195 1.13 jsm show("...-.-"); /* SK */
196 1.6 hubertf }
197 1.6 hubertf
198 1.6 hubertf return 0;
199 1.6 hubertf }
200 1.6 hubertf
201 1.6 hubertf void
202 1.17 dholland decode(const char *s)
203 1.6 hubertf {
204 1.13 jsm int i;
205 1.13 jsm
206 1.13 jsm for (i = 0; i < 10; i++)
207 1.13 jsm if (strcmp(digit[i], s) == 0) {
208 1.13 jsm putchar('0' + i);
209 1.13 jsm return;
210 1.6 hubertf }
211 1.13 jsm
212 1.13 jsm for (i = 0; i < 26; i++)
213 1.13 jsm if (strcmp(alph[i], s) == 0) {
214 1.13 jsm putchar('A' + i);
215 1.6 hubertf return;
216 1.6 hubertf }
217 1.13 jsm i = 0;
218 1.13 jsm while (other[i].c) {
219 1.13 jsm if (strcmp(other[i].morse, s) == 0) {
220 1.13 jsm putchar(other[i].c);
221 1.13 jsm return;
222 1.6 hubertf }
223 1.13 jsm i++;
224 1.6 hubertf }
225 1.13 jsm if (strcmp("...-.-", s) == 0)
226 1.13 jsm return;
227 1.13 jsm putchar('x'); /* line noise */
228 1.1 cgd }
229 1.1 cgd
230 1.4 lukem void
231 1.17 dholland morse(int c)
232 1.1 cgd {
233 1.13 jsm int i;
234 1.13 jsm
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 (isspace(c))
240 1.13 jsm show(""); /* could show BT for a pause */
241 1.13 jsm else {
242 1.13 jsm i = 0;
243 1.13 jsm while (other[i].c) {
244 1.13 jsm if (other[i].c == c) {
245 1.13 jsm show(other[i].morse);
246 1.13 jsm break;
247 1.13 jsm }
248 1.13 jsm i++;
249 1.13 jsm }
250 1.13 jsm }
251 1.1 cgd }
252 1.1 cgd
253 1.4 lukem void
254 1.17 dholland show(const char *s)
255 1.1 cgd {
256 1.1 cgd if (sflag)
257 1.1 cgd printf(" %s", s);
258 1.1 cgd else for (; *s; ++s)
259 1.1 cgd printf(" %s", *s == '.' ? "dit" : "daw");
260 1.13 jsm printf("\n");
261 1.1 cgd }
262