2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Taught to send *real* morse by Lyndon Nerenberg (VE6BBM)
35 static const char copyright[] =
36 "@(#) Copyright (c) 1988, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)morse.c 8.1 (Berkeley) 5/31/93";
42 static const char rcsid[] =
46 #include <sys/ioctl.h>
61 /* Always use the speaker, let the open fail if -p is selected */
62 #define SPEAKER "/dev/speaker"
65 #define WHITESPACE " \t\n"
66 #define DELIMITERS " \t"
69 #include <dev/speaker/speaker.h>
77 static const struct morsetab mtab[] = {
130 {'=', "-...-"}, /* BT */
133 {'(', "-.--."}, /* KN */
136 {'+', ".-.-."}, /* AR */
137 {'@', ".--.-."}, /* AC */
141 /* prosigns without already assigned values */
143 {'#', ".-..."}, /* AS */
144 {'&', "...-.-"}, /* SK */
145 {'*', "...-."}, /* VE */
146 {'%', "-...-.-"}, /* BK */
152 * Code-points for some Latin1 chars in ISO-8859-1 encoding.
153 * UTF-8 encoded chars in the comments.
155 static const struct morsetab iso8859_1tab[] = {
156 {'\340', ".--.-"}, /* à */
157 {'\341', ".--.-"}, /* á */
158 {'\342', ".--.-"}, /* â */
159 {'\344', ".-.-"}, /* ä */
160 {'\347', "-.-.."}, /* ç */
161 {'\350', "..-.."}, /* è */
162 {'\351', "..-.."}, /* é */
163 {'\352', "-..-."}, /* ê */
164 {'\361', "--.--"}, /* ñ */
165 {'\366', "---."}, /* ö */
166 {'\374', "..--"}, /* ü */
172 * Code-points for some Greek chars in ISO-8859-7 encoding.
173 * UTF-8 encoded chars in the comments.
175 static const struct morsetab iso8859_7tab[] = {
177 * This table does not implement:
178 * - the special sequences for the seven diphthongs,
179 * - the punctuation differences.
180 * Implementing these features would introduce too many
181 * special-cases in the program's main loop.
182 * The diphthong sequences are:
185 * epsilon upsilon ---.
188 * omicron upsilon ..-
190 * The different punctuation symbols are:
194 {'\341', ".-"}, /* α, alpha */
195 {'\334', ".-"}, /* ά, alpha with acute */
196 {'\342', "-..."}, /* β, beta */
197 {'\343', "--."}, /* γ, gamma */
198 {'\344', "-.."}, /* δ, delta */
199 {'\345', "."}, /* ε, epsilon */
200 {'\335', "."}, /* έ, epsilon with acute */
201 {'\346', "--.."}, /* ζ, zeta */
202 {'\347', "...."}, /* η, eta */
203 {'\336', "...."}, /* ή, eta with acute */
204 {'\350', "-.-."}, /* θ, theta */
205 {'\351', ".."}, /* ι, iota */
206 {'\337', ".."}, /* ί, iota with acute */
207 {'\372', ".."}, /* ϊ, iota with diaeresis */
208 {'\300', ".."}, /* ΐ, iota with acute and diaeresis */
209 {'\352', "-.-"}, /* κ, kappa */
210 {'\353', ".-.."}, /* λ, lambda */
211 {'\354', "--"}, /* μ, mu */
212 {'\355', "-."}, /* ν, nu */
213 {'\356', "-..-"}, /* ξ, xi */
214 {'\357', "---"}, /* ο, omicron */
215 {'\374', "---"}, /* ό, omicron with acute */
216 {'\360', ".--."}, /* π, pi */
217 {'\361', ".-."}, /* ρ, rho */
218 {'\363', "..."}, /* σ, sigma */
219 {'\362', "..."}, /* ς, final sigma */
220 {'\364', "-"}, /* τ, tau */
221 {'\365', "-.--"}, /* υ, upsilon */
222 {'\375', "-.--"}, /* ύ, upsilon with acute */
223 {'\373', "-.--"}, /* ϋ, upsilon and diaeresis */
224 {'\340', "-.--"}, /* ΰ, upsilon with acute and diaeresis */
225 {'\366', "..-."}, /* φ, phi */
226 {'\367', "----"}, /* χ, chi */
227 {'\370', "--.-"}, /* ψ, psi */
228 {'\371', ".--"}, /* ω, omega */
229 {'\376', ".--"}, /* ώ, omega with acute */
235 * Code-points for the Cyrillic alphabet in KOI8-R encoding.
236 * UTF-8 encoded chars in the comments.
238 static const struct morsetab koi8rtab[] = {
239 {'\301', ".-"}, /* а, a */
240 {'\302', "-..."}, /* б, be */
241 {'\327', ".--"}, /* в, ve */
242 {'\307', "--."}, /* г, ge */
243 {'\304', "-.."}, /* д, de */
244 {'\305', "."}, /* е, ye */
245 {'\243', "."}, /* ё, yo, the same as ye */
246 {'\326', "...-"}, /* ж, she */
247 {'\332', "--.."}, /* з, ze */
248 {'\311', ".."}, /* и, i */
249 {'\312', ".---"}, /* й, i kratkoye */
250 {'\313', "-.-"}, /* к, ka */
251 {'\314', ".-.."}, /* л, el */
252 {'\315', "--"}, /* м, em */
253 {'\316', "-."}, /* н, en */
254 {'\317', "---"}, /* о, o */
255 {'\320', ".--."}, /* п, pe */
256 {'\322', ".-."}, /* р, er */
257 {'\323', "..."}, /* с, es */
258 {'\324', "-"}, /* т, te */
259 {'\325', "..-"}, /* у, u */
260 {'\306', "..-."}, /* ф, ef */
261 {'\310', "...."}, /* х, kha */
262 {'\303', "-.-."}, /* ц, ce */
263 {'\336', "---."}, /* ч, che */
264 {'\333', "----"}, /* ш, sha */
265 {'\335', "--.-"}, /* щ, shcha */
266 {'\331', "-.--"}, /* ы, yi */
267 {'\330', "-..-"}, /* ь, myakhkij znak */
268 {'\334', "..-.."}, /* э, ae */
269 {'\300', "..--"}, /* ю, yu */
270 {'\321', ".-.-"}, /* я, ya */
275 static void show(const char *), play(const char *), morse(char);
276 static void decode (char *), fdecode(FILE *);
277 static void ttyout(const char *);
278 static void sighandler(int);
280 static int pflag, lflag, rflag, sflag, eflag;
281 static int wpm = 20; /* effective words per minute */
282 static int cpm; /* effective words per minute between
284 #define FREQUENCY 600
285 static int freq = FREQUENCY;
286 static char *device; /* for tty-controlled generator */
290 #define WORD_SPACE (7 - CHAR_SPACE - 1)
291 static float dot_clock;
292 static float cdot_clock;
293 static int spkr, line;
294 static struct termios otty, ntty;
299 #define GETOPTOPTS "c:d:ef:lprsw:"
301 "usage: morse [-elprs] [-d device] [-w speed] [-c speed] [-f frequency] [string ...]\n"
303 #define GETOPTOPTS "c:d:ef:lrsw:"
305 "usage: morse [-elrs] [-d device] [-w speed] [-c speed] [-f frequency] [string ...]\n"
309 static const struct morsetab *hightab;
312 main(int argc, char *argv[])
317 while ((ch = getopt(argc, argv, GETOPTOPTS)) != -1)
327 setvbuf(stdout, 0, _IONBF, 0);
353 if ((sflag && lflag) || (sflag && rflag) || (lflag && rflag)) {
354 errx(1, "morse: only one of -l, -s, and -r allowed\n");
356 if ((pflag || device) && (sflag || lflag)) {
357 errx(1, "morse: only one of -p, -d and -l, -s allowed\n");
362 if ((pflag || device) && ((wpm < 1) || (wpm > 60) || (cpm < 1) || (cpm > 60))) {
363 errx(1, "morse: insane speed\n");
365 if ((pflag || device) && (freq == 0)) {
370 if ((spkr = open(SPEAKER, O_WRONLY, 0)) == -1) {
376 if ((line = open(device, O_WRONLY | O_NONBLOCK)) == -1) {
377 err(1, "open tty line");
379 if (tcgetattr(line, &otty) == -1) {
380 err(1, "tcgetattr() failed");
383 ntty.c_cflag |= CLOCAL;
384 tcsetattr(line, TCSANOW, &ntty);
385 lflags = fcntl(line, F_GETFL);
386 lflags &= ~O_NONBLOCK;
387 fcntl(line, F_SETFL, &lflags);
388 ioctl(line, TIOCMGET, &lflags);
389 lflags &= ~TIOCM_RTS;
391 ioctl(line, TIOCMSET, &lflags);
392 (void)signal(SIGHUP, sighandler);
393 (void)signal(SIGINT, sighandler);
394 (void)signal(SIGQUIT, sighandler);
395 (void)signal(SIGTERM, sighandler);
397 if (pflag || device) {
398 dot_clock = wpm / 2.4; /* dots/sec */
399 dot_clock = 1 / dot_clock; /* duration of a dot */
400 dot_clock = dot_clock / 2; /* dot_clock runs at twice */
402 dot_clock = dot_clock * 100; /* scale for ioctl */
404 cdot_clock = cpm / 2.4; /* dots/sec */
405 cdot_clock = 1 / cdot_clock; /* duration of a dot */
406 cdot_clock = cdot_clock / 2; /* dot_clock runs at twice */
408 cdot_clock = cdot_clock * 100; /* scale for ioctl */
414 if (setlocale(LC_CTYPE, "") != NULL &&
415 *(codeset = nl_langinfo(CODESET)) != '\0') {
416 if (strcmp(codeset, "KOI8-R") == 0)
418 else if (strcmp(codeset, "ISO8859-1") == 0 ||
419 strcmp(codeset, "ISO8859-15") == 0)
420 hightab = iso8859_1tab;
421 else if (strcmp(codeset, "ISO8859-7") == 0)
422 hightab = iso8859_7tab;
431 p = strtok(*argv, DELIMITERS);
438 p = strtok(NULL, DELIMITERS);
449 for (p = *argv; *p; ++p) {
459 while ((ch = getchar()) != EOF) {
466 tcsetattr(line, TCSANOW, &otty);
473 const struct morsetab *m;
475 if (isalpha((unsigned char)c))
476 c = tolower((unsigned char)c);
477 if ((c == '\r') || (c == '\n'))
490 for (m = ((unsigned char)c < 0x80? mtab: hightab);
491 m != NULL && m->inchar != '\0';
493 if (m->inchar == c) {
513 printf(" %s", *s == '.' ? *(s + 1) == '\0' ? "dit" :
525 for (c = s; *c != '\0'; c++) {
528 sound.frequency = freq;
529 sound.duration = dot_clock;
532 sound.frequency = freq;
533 sound.duration = dot_clock * DASH_LEN;
537 sound.duration = cdot_clock * WORD_SPACE;
542 if (sound.duration) {
543 if (ioctl(spkr, SPKRTONE, &sound) == -1) {
544 err(1, "ioctl play");
548 sound.duration = dot_clock;
549 if (ioctl(spkr, SPKRTONE, &sound) == -1) {
550 err(1, "ioctl rest");
554 sound.duration = cdot_clock * CHAR_SPACE;
555 ioctl(spkr, SPKRTONE, &sound);
560 ttyout(const char *s)
563 int duration, on, lflags;
565 for (c = s; *c != '\0'; c++) {
569 duration = dot_clock;
573 duration = dot_clock * DASH_LEN;
577 duration = cdot_clock * WORD_SPACE;
584 ioctl(line, TIOCMGET, &lflags);
586 ioctl(line, TIOCMSET, &lflags);
591 ioctl(line, TIOCMGET, &lflags);
592 lflags &= ~TIOCM_RTS;
593 ioctl(line, TIOCMSET, &lflags);
594 duration = dot_clock * 10000;
597 duration = cdot_clock * CHAR_SPACE * 10000;
602 fdecode(FILE *stream)
608 while (fgets(s, BUFSIZ - (s - buf), stream)) {
611 while (*p && isblank(*p)) {
614 while (*p && isspace(*p)) {
619 n = strpbrk(p, WHITESPACE);
621 /* The token was interrupted at the end
622 * of the buffer. Shift it to the begin
625 for (s = buf; *p; *s++ = *p++)
642 const struct morsetab *m;
645 for (m = mtab; m != NULL && m->inchar != '\0'; m++) {
646 if (strcmp(m->morse, p) == 0) {
653 for (m = hightab; m != NULL && m->inchar != '\0'; m++) {
654 if (strcmp(m->morse, p) == 0) {
664 sighandler(int signo)
667 ioctl(line, TIOCMSET, &olflags);
668 tcsetattr(line, TCSANOW, &otty);
670 signal(signo, SIG_DFL);
671 (void)kill(getpid(), signo);