1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32: #include <sys/cdefs.h>
33: #ifndef lint
34: __COPYRIGHT("@(#) Copyright (c) 1988, 1993\n\
35: The Regents of the University of California. All rights reserved.\n");
36: #endif
37:
38: #ifndef lint
39: #if 0
40: static char sccsid[] = "@(#)morse.c 8.1 (Berkeley) 5/31/93";
41: #else
42: __RCSID("$NetBSD: morse.c,v 1.13 2004/02/13 23:16:11 jsm Exp $");
43: #endif
44: #endif
45:
46: #include <ctype.h>
47: #include <stdio.h>
48: #include <stdlib.h>
49: #include <string.h>
50: #include <unistd.h>
51:
52: static const char
53: *const digit[] = {
54: "-----",
55: ".----",
56: "..---",
57: "...--",
58: "....-",
59: ".....",
60: "-....",
61: "--...",
62: "---..",
63: "----.",
64: },
65: *const alph[] = {
66: ".-",
67: "-...",
68: "-.-.",
69: "-..",
70: ".",
71: "..-.",
72: "--.",
73: "....",
74: "..",
75: ".---",
76: "-.-",
77: ".-..",
78: "--",
79: "-.",
80: "---",
81: ".--.",
82: "--.-",
83: ".-.",
84: "...",
85: "-",
86: "..-",
87: "...-",
88: ".--",
89: "-..-",
90: "-.--",
91: "--..",
92: };
93:
94: const struct punc {
95: char c;
96: const char *morse;
97: } other[] = {
98: { '.', ".-.-.-" },
99: { ',', "--..--" },
100: { ':', "---..." },
101: { '?', "..--.." },
102: { '\'', ".----." },
103: { '-', "-....-" },
104: { '/', "-..-." },
105: { '(', "-.--." },
106: { ')', "-.--.-" },
107: { '"', ".-..-." },
108: { '=', "-...-" },
109: { '+', ".-.-." },
110: { '\0', NULL }
111: };
112:
113: int main(int, char *[]);
114: void morse(int);
115: void decode(const char *);
116: void show(const char *);
117:
118: static int sflag;
119: static int dflag;
120:
121: int
122: main(argc, argv)
123: int argc;
124: char **argv;
125: {
126: int ch;
127: char *p;
128:
129:
130: setregid(getgid(), getgid());
131:
132: while ((ch = getopt(argc, argv, "ds")) != -1)
133: switch((char)ch) {
134: case 'd':
135: dflag = 1;
136: break;
137: case 's':
138: sflag = 1;
139: break;
140: case '?':
141: default:
142: fprintf(stderr, "usage: morse [-ds] [string ...]\n");
143: exit(1);
144: }
145: argc -= optind;
146: argv += optind;
147:
148: if (dflag) {
149: if (*argv) {
150: do {
151: decode(*argv);
152: } while (*++argv);
153: } else {
154: char foo[10];
155: int isblank, i;
156:
157: i = 0;
158: isblank = 0;
159: while ((ch = getchar()) != EOF) {
160: if (ch == '-' || ch == '.') {
161: foo[i++] = ch;
162: if (i == 10) {
163:
164:
165: i = 0;
166: putchar('x');
167: while ((ch = getchar()) != EOF &&
168: (ch == '.' || ch == '-'))
169: ;
170: isblank = 1;
171: }
172: } else if (i) {
173: foo[i] = '\0';
174: decode(foo);
175: i = 0;
176: isblank = 0;
177: } else if (isspace(ch)) {
178: if (isblank) {
179:
180: putchar(' ');
181: isblank = 0;
182: } else
183: isblank = 1;
184: }
185: }
186: }
187: putchar('\n');
188: } else {
189: if (*argv)
190: do {
191: for (p = *argv; *p; ++p)
192: morse((int)*p);
193: show("");
194: } while (*++argv);
195: else while ((ch = getchar()) != EOF)
196: morse(ch);
197: show("...-.-");
198: }
199:
200: return 0;
201: }
202:
203: void
204: decode(s)
205: const char *s;
206: {
207: int i;
208:
209: for (i = 0; i < 10; i++)
210: if (strcmp(digit[i], s) == 0) {
211: putchar('0' + i);
212: return;
213: }
214:
215: for (i = 0; i < 26; i++)
216: if (strcmp(alph[i], s) == 0) {
217: putchar('A' + i);
218: return;
219: }
220: i = 0;
221: while (other[i].c) {
222: if (strcmp(other[i].morse, s) == 0) {
223: putchar(other[i].c);
224: return;
225: }
226: i++;
227: }
228: if (strcmp("...-.-", s) == 0)
229: return;
230: putchar('x');
231: }
232:
233: void
234: morse(c)
235: int c;
236: {
237: int i;
238:
239: if (isalpha(c))
240: show(alph[c - (isupper(c) ? 'A' : 'a')]);
241: else if (isdigit(c))
242: show(digit[c - '0']);
243: else if (isspace(c))
244: show("");
245: else {
246: i = 0;
247: while (other[i].c) {
248: if (other[i].c == c) {
249: show(other[i].morse);
250: break;
251: }
252: i++;
253: }
254: }
255: }
256:
257: void
258: show(s)
259: const char *s;
260: {
261: if (sflag)
262: printf(" %s", s);
263: else for (; *s; ++s)
264: printf(" %s", *s == '.' ? "dit" : "daw");
265: printf("\n");
266: }