morse.c revision 1.18 1 1.18 maya /* $NetBSD: morse.c,v 1.18 2018/01/16 06:20:24 maya 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.18 maya __RCSID("$NetBSD: morse.c,v 1.18 2018/01/16 06:20:24 maya 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.18 maya { '_', "..--.-" },
111 1.13 jsm { '\0', NULL }
112 1.13 jsm };
113 1.13 jsm
114 1.12 jsm int main(int, char *[]);
115 1.16 dholland static void morse(int);
116 1.16 dholland static void decode(const char *);
117 1.16 dholland static void show(const char *);
118 1.4 lukem
119 1.1 cgd static int sflag;
120 1.6 hubertf static int dflag;
121 1.1 cgd
122 1.4 lukem int
123 1.17 dholland main(int argc, char **argv)
124 1.1 cgd {
125 1.4 lukem int ch;
126 1.13 jsm char *p;
127 1.8 jsm
128 1.8 jsm /* Revoke setgid privileges */
129 1.9 mycroft setgid(getgid());
130 1.1 cgd
131 1.6 hubertf while ((ch = getopt(argc, argv, "ds")) != -1)
132 1.1 cgd switch((char)ch) {
133 1.6 hubertf case 'd':
134 1.6 hubertf dflag = 1;
135 1.6 hubertf break;
136 1.1 cgd case 's':
137 1.1 cgd sflag = 1;
138 1.1 cgd break;
139 1.1 cgd case '?':
140 1.1 cgd default:
141 1.6 hubertf fprintf(stderr, "usage: morse [-ds] [string ...]\n");
142 1.1 cgd exit(1);
143 1.1 cgd }
144 1.1 cgd argc -= optind;
145 1.1 cgd argv += optind;
146 1.1 cgd
147 1.6 hubertf if (dflag) {
148 1.6 hubertf if (*argv) {
149 1.6 hubertf do {
150 1.6 hubertf decode(*argv);
151 1.6 hubertf } while (*++argv);
152 1.13 jsm } else {
153 1.13 jsm char foo[10]; /* All morse chars shorter than this */
154 1.14 dholland int is_blank, i;
155 1.13 jsm
156 1.13 jsm i = 0;
157 1.14 dholland is_blank = 0;
158 1.13 jsm while ((ch = getchar()) != EOF) {
159 1.13 jsm if (ch == '-' || ch == '.') {
160 1.13 jsm foo[i++] = ch;
161 1.13 jsm if (i == 10) {
162 1.13 jsm /* overrun means gibberish--print 'x' and
163 1.13 jsm * advance */
164 1.13 jsm i = 0;
165 1.13 jsm putchar('x');
166 1.13 jsm while ((ch = getchar()) != EOF &&
167 1.13 jsm (ch == '.' || ch == '-'))
168 1.13 jsm ;
169 1.14 dholland is_blank = 1;
170 1.6 hubertf }
171 1.13 jsm } else if (i) {
172 1.13 jsm foo[i] = '\0';
173 1.13 jsm decode(foo);
174 1.13 jsm i = 0;
175 1.14 dholland is_blank = 0;
176 1.13 jsm } else if (isspace(ch)) {
177 1.14 dholland if (is_blank) {
178 1.13 jsm /* print whitespace for each double blank */
179 1.13 jsm putchar(' ');
180 1.14 dholland is_blank = 0;
181 1.13 jsm } else
182 1.14 dholland is_blank = 1;
183 1.6 hubertf }
184 1.6 hubertf }
185 1.6 hubertf }
186 1.6 hubertf putchar('\n');
187 1.13 jsm } else {
188 1.6 hubertf if (*argv)
189 1.6 hubertf do {
190 1.6 hubertf for (p = *argv; *p; ++p)
191 1.6 hubertf morse((int)*p);
192 1.13 jsm show("");
193 1.6 hubertf } while (*++argv);
194 1.6 hubertf else while ((ch = getchar()) != EOF)
195 1.6 hubertf morse(ch);
196 1.13 jsm show("...-.-"); /* SK */
197 1.6 hubertf }
198 1.6 hubertf
199 1.6 hubertf return 0;
200 1.6 hubertf }
201 1.6 hubertf
202 1.6 hubertf void
203 1.17 dholland decode(const char *s)
204 1.6 hubertf {
205 1.13 jsm int i;
206 1.13 jsm
207 1.13 jsm for (i = 0; i < 10; i++)
208 1.13 jsm if (strcmp(digit[i], s) == 0) {
209 1.13 jsm putchar('0' + i);
210 1.13 jsm return;
211 1.6 hubertf }
212 1.13 jsm
213 1.13 jsm for (i = 0; i < 26; i++)
214 1.13 jsm if (strcmp(alph[i], s) == 0) {
215 1.13 jsm putchar('A' + i);
216 1.6 hubertf return;
217 1.6 hubertf }
218 1.13 jsm i = 0;
219 1.13 jsm while (other[i].c) {
220 1.13 jsm if (strcmp(other[i].morse, s) == 0) {
221 1.13 jsm putchar(other[i].c);
222 1.13 jsm return;
223 1.6 hubertf }
224 1.13 jsm i++;
225 1.6 hubertf }
226 1.13 jsm if (strcmp("...-.-", s) == 0)
227 1.13 jsm return;
228 1.13 jsm putchar('x'); /* line noise */
229 1.1 cgd }
230 1.1 cgd
231 1.4 lukem void
232 1.17 dholland morse(int c)
233 1.1 cgd {
234 1.13 jsm int i;
235 1.13 jsm
236 1.1 cgd if (isalpha(c))
237 1.1 cgd show(alph[c - (isupper(c) ? 'A' : 'a')]);
238 1.1 cgd else if (isdigit(c))
239 1.1 cgd show(digit[c - '0']);
240 1.1 cgd else if (isspace(c))
241 1.13 jsm show(""); /* could show BT for a pause */
242 1.13 jsm else {
243 1.13 jsm i = 0;
244 1.13 jsm while (other[i].c) {
245 1.13 jsm if (other[i].c == c) {
246 1.13 jsm show(other[i].morse);
247 1.13 jsm break;
248 1.13 jsm }
249 1.13 jsm i++;
250 1.13 jsm }
251 1.13 jsm }
252 1.1 cgd }
253 1.1 cgd
254 1.4 lukem void
255 1.17 dholland show(const char *s)
256 1.1 cgd {
257 1.1 cgd if (sflag)
258 1.1 cgd printf(" %s", s);
259 1.1 cgd else for (; *s; ++s)
260 1.1 cgd printf(" %s", *s == '.' ? "dit" : "daw");
261 1.13 jsm printf("\n");
262 1.1 cgd }
263