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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Taught to send *real* morse by Lyndon Nerenberg (VE7TCP/VE6BBM)
36 * <lyndon@orthanc.com>
40 static const char copyright[] =
41 "@(#) Copyright (c) 1988, 1993\n\
42 The Regents of the University of California. All rights reserved.\n";
47 static char sccsid[] = "@(#)morse.c 8.1 (Berkeley) 5/31/93";
49 static const char rcsid[] =
67 #include <dev/speaker/speaker.h>
75 static const struct morsetab mtab[] = {
128 {'=', "-...-"}, /* BT */
131 {'(', "-.--."}, /* KN */
134 {'+', ".-.-."}, /* AR */
135 {'@', ".--.-."}, /* AC */
137 /* prosigns without already assigned values */
139 {'#', ".-..."}, /* AS */
140 {'&', "...-.-"}, /* SK */
141 {'*', "...-."}, /* VE */
142 {'%', "-...-.-"}, /* BK */
148 static const struct morsetab iso8859_1tab[] = {
163 static const struct morsetab iso8859_7tab[] = {
165 * The greek alphabet; you'll need an 8859-7 font in order
166 * to see the actual characters.
167 * This table does not implement:
168 * - the special sequences for the seven diphthongs,
169 * - the punctuation differences.
170 * Implementing these features would introduce too many
171 * special-cases in the program's main loop.
172 * The diphtong sequences are:
175 * epsilon upsilon ---.
178 * omikron upsilon ..-
180 * The different punctuation symbols are:
184 {'á', ".-"}, /* alpha */
185 {'Ü', ".-"}, /* alpha with acute */
186 {'â', "-..."}, /* beta */
187 {'ã', "--."}, /* gamma */
188 {'ä', "-.."}, /* delta */
189 {'å', "."}, /* epsilon */
190 {'Ý', "."}, /* epsilon with acute */
191 {'æ', "--.."}, /* zeta */
192 {'ç', "...."}, /* eta */
193 {'Þ', "...."}, /* eta with acute */
194 {'è', "-.-."}, /* theta */
195 {'é', ".."}, /* iota */
196 {'ß', ".."}, /* iota with acute */
197 {'ú', ".."}, /* iota with diairesis */
198 {'À', ".."}, /* iota with acute and diairesis */
199 {'ê', "-.-"}, /* kappa */
200 {'ë', ".-.."}, /* lamda */
201 {'ì', "--"}, /* mu */
202 {'í', "-."}, /* nu */
203 {'î', "-..-"}, /* xi */
204 {'ï', "---"}, /* omicron */
205 {'ü', "---"}, /* omicron with acute */
206 {'ð', ".--."}, /* pi */
207 {'ñ', ".-."}, /* rho */
208 {'ó', "..."}, /* sigma */
209 {'ò', "..."}, /* final sigma */
210 {'ô', "-"}, /* tau */
211 {'õ', "-.--"}, /* upsilon */
212 {'ý', "-.--"}, /* upsilon with acute */
213 {'û', "-.--"}, /* upsilon and diairesis */
214 {'à', "-.--"}, /* upsilon with acute and diairesis */
215 {'ö', "..-."}, /* phi */
216 {'÷', "----"}, /* chi */
217 {'ø', "--.-"}, /* psi */
218 {'ù', ".--"}, /* omega */
219 {'þ', ".--"}, /* omega with acute */
224 static const struct morsetab koi8rtab[] = {
226 * the cyrillic alphabet; you'll need a KOI8R font in order
227 * to see the actual characters
230 {'Â', "-..."}, /* be */
231 {'×', ".--"}, /* ve */
232 {'Ç', "--."}, /* ge */
233 {'Ä', "-.."}, /* de */
235 {'£', "."}, /* yo, the same as ye */
236 {'Ö', "...-"}, /* she */
237 {'Ú', "--.."}, /* ze */
239 {'Ê', ".---"}, /* i kratkoye */
240 {'Ë', "-.-"}, /* ka */
241 {'Ì', ".-.."}, /* el */
242 {'Í', "--"}, /* em */
243 {'Î', "-."}, /* en */
244 {'Ï', "---"}, /* o */
245 {'Ð', ".--."}, /* pe */
246 {'Ò', ".-."}, /* er */
247 {'Ó', "..."}, /* es */
249 {'Õ', "..-"}, /* u */
250 {'Æ', "..-."}, /* ef */
251 {'È', "...."}, /* kha */
252 {'Ã', "-.-."}, /* ce */
253 {'Þ', "---."}, /* che */
254 {'Û', "----"}, /* sha */
255 {'Ý', "--.-"}, /* shcha */
256 {'Ù', "-.--"}, /* yi */
257 {'Ø', "-..-"}, /* myakhkij znak */
258 {'Ü', "..-.."}, /* ae */
259 {'À', "..--"}, /* yu */
260 {'Ñ', ".-.-"}, /* ya */
265 void show(const char *), play(const char *), morse(char);
266 void ttyout(const char *);
267 void sighandler(int);
269 #define GETOPTOPTS "c:d:ef:lsw:"
271 "usage: morse [-els] [-d device] [-w speed] [-c speed] [-f frequency] [string ...]\n"
273 static int pflag, lflag, sflag, eflag;
274 static int wpm = 20; /* effective words per minute */
275 static int cpm; /* effective words per minute between
277 #define FREQUENCY 600
278 static int freq = FREQUENCY;
279 static char *device; /* for tty-controlled generator */
283 #define WORD_SPACE (7 - CHAR_SPACE - 1)
284 static float dot_clock;
285 static float cdot_clock;
287 struct termios otty, ntty;
293 #define GETOPTOPTS "c:d:ef:lpsw:"
296 "usage: morse [-elps] [-d device] [-w speed] [-c speed] [-f frequency] [string ...]\n"
299 static const struct morsetab *hightab;
302 main(int argc, char **argv)
307 while ((ch = getopt(argc, argv, GETOPTOPTS)) != -1)
317 setvbuf(stdout, 0, _IONBF, 0);
338 fputs(USAGE, stderr);
341 if (sflag && lflag) {
342 fputs("morse: only one of -l and -s allowed\n", stderr);
345 if ((pflag || device) && (sflag || lflag)) {
346 fputs("morse: only one of -p, -d and -l, -s allowed\n", stderr);
351 if ((pflag || device) && ((wpm < 1) || (wpm > 60) || (cpm < 1) || (cpm > 60))) {
352 fputs("morse: insane speed\n", stderr);
355 if ((pflag || device) && (freq == 0))
360 if ((spkr = open(SPEAKER, O_WRONLY, 0)) == -1) {
367 if ((line = open(device, O_WRONLY | O_NONBLOCK)) == -1) {
368 perror("open tty line");
371 if (tcgetattr(line, &otty) == -1) {
372 perror("tcgetattr() failed");
376 ntty.c_cflag |= CLOCAL;
377 tcsetattr(line, TCSANOW, &ntty);
378 lflags = fcntl(line, F_GETFL);
379 lflags &= ~O_NONBLOCK;
380 fcntl(line, F_SETFL, &lflags);
381 ioctl(line, TIOCMGET, &lflags);
382 lflags &= ~TIOCM_RTS;
384 ioctl(line, TIOCMSET, &lflags);
385 (void)signal(SIGHUP, sighandler);
386 (void)signal(SIGINT, sighandler);
387 (void)signal(SIGQUIT, sighandler);
388 (void)signal(SIGTERM, sighandler);
390 if (pflag || device) {
391 dot_clock = wpm / 2.4; /* dots/sec */
392 dot_clock = 1 / dot_clock; /* duration of a dot */
393 dot_clock = dot_clock / 2; /* dot_clock runs at twice */
395 dot_clock = dot_clock * 100; /* scale for ioctl */
397 cdot_clock = cpm / 2.4; /* dots/sec */
398 cdot_clock = 1 / cdot_clock; /* duration of a dot */
399 cdot_clock = cdot_clock / 2; /* dot_clock runs at twice */
401 cdot_clock = cdot_clock * 100; /* scale for ioctl */
407 if (setlocale(LC_CTYPE, "") != NULL &&
408 *(codeset = nl_langinfo(CODESET)) != '\0') {
409 if (strcmp(codeset, "KOI8-R") == 0)
411 else if (strcmp(codeset, "ISO8859-1") == 0 ||
412 strcmp(codeset, "ISO8859-15") == 0)
413 hightab = iso8859_1tab;
414 else if (strcmp(codeset, "ISO8859-7") == 0)
415 hightab = iso8859_7tab;
422 for (p = *argv; *p; ++p) {
432 while ((ch = getchar()) != EOF) {
439 tcsetattr(line, TCSANOW, &otty);
446 const struct morsetab *m;
448 if (isalpha((unsigned char)c))
449 c = tolower((unsigned char)c);
450 if ((c == '\r') || (c == '\n'))
463 for (m = ((unsigned char)c < 0x80? mtab: hightab);
464 m != NULL && m->inchar != '\0';
466 if (m->inchar == c) {
486 printf(" %s", *s == '.' ? "dit" : "dah");
497 for (c = s; *c != '\0'; c++) {
500 sound.frequency = freq;
501 sound.duration = dot_clock;
504 sound.frequency = freq;
505 sound.duration = dot_clock * DASH_LEN;
509 sound.duration = cdot_clock * WORD_SPACE;
514 if (sound.duration) {
515 if (ioctl(spkr, SPKRTONE, &sound) == -1) {
516 perror("ioctl play");
521 sound.duration = dot_clock;
522 if (ioctl(spkr, SPKRTONE, &sound) == -1) {
523 perror("ioctl rest");
528 sound.duration = cdot_clock * CHAR_SPACE;
529 ioctl(spkr, SPKRTONE, &sound);
534 ttyout(const char *s)
537 int duration, on, lflags;
539 for (c = s; *c != '\0'; c++) {
543 duration = dot_clock;
547 duration = dot_clock * DASH_LEN;
551 duration = cdot_clock * WORD_SPACE;
558 ioctl(line, TIOCMGET, &lflags);
560 ioctl(line, TIOCMSET, &lflags);
565 ioctl(line, TIOCMGET, &lflags);
566 lflags &= ~TIOCM_RTS;
567 ioctl(line, TIOCMSET, &lflags);
568 duration = dot_clock * 10000;
571 duration = cdot_clock * CHAR_SPACE * 10000;
576 sighandler(int signo)
579 ioctl(line, TIOCMSET, &olflags);
580 tcsetattr(line, TCSANOW, &otty);
582 signal(signo, SIG_DFL);
583 (void)kill(getpid(), signo);