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)
36 static const char copyright[] =
37 "@(#) Copyright (c) 1988, 1993\n\
38 The Regents of the University of California. All rights reserved.\n";
43 static char sccsid[] = "@(#)morse.c 8.1 (Berkeley) 5/31/93";
45 static const char rcsid[] =
62 /* Always use the speaker, let the open fail if -p is selected */
63 #define SPEAKER "/dev/speaker"
66 #include <dev/speaker/speaker.h>
74 static const struct morsetab mtab[] = {
127 {'=', "-...-"}, /* BT */
130 {'(', "-.--."}, /* KN */
133 {'+', ".-.-."}, /* AR */
134 {'@', ".--.-."}, /* AC */
136 /* prosigns without already assigned values */
138 {'#', ".-..."}, /* AS */
139 {'&', "...-.-"}, /* SK */
140 {'*', "...-."}, /* VE */
141 {'%', "-...-.-"}, /* BK */
147 * Code-points for some Latin1 chars in ISO-8859-1 encoding.
148 * UTF-8 encoded chars in the comments.
150 static const struct morsetab iso8859_1tab[] = {
151 {'\340', ".--.-"}, /* à */
152 {'\341', ".--.-"}, /* á */
153 {'\342', ".--.-"}, /* â */
154 {'\344', ".-.-"}, /* ä */
155 {'\347', "-.-.."}, /* ç */
156 {'\350', "..-.."}, /* è */
157 {'\351', "..-.."}, /* é */
158 {'\352', "-..-."}, /* ê */
159 {'\366', "---."}, /* ö */
160 {'\374', "..--"}, /* ü */
166 * Code-points for some Greek chars in ISO-8859-7 encoding.
167 * UTF-8 encoded chars in the comments.
169 static const struct morsetab iso8859_7tab[] = {
171 * This table does not implement:
172 * - the special sequences for the seven diphthongs,
173 * - the punctuation differences.
174 * Implementing these features would introduce too many
175 * special-cases in the program's main loop.
176 * The diphthong sequences are:
179 * epsilon upsilon ---.
182 * omicron upsilon ..-
184 * The different punctuation symbols are:
188 {'\341', ".-"}, /* α, alpha */
189 {'\334', ".-"}, /* ά, alpha with acute */
190 {'\342', "-..."}, /* β, beta */
191 {'\343', "--."}, /* γ, gamma */
192 {'\344', "-.."}, /* δ, delta */
193 {'\345', "."}, /* ε, epsilon */
194 {'\335', "."}, /* έ, epsilon with acute */
195 {'\346', "--.."}, /* ζ, zeta */
196 {'\347', "...."}, /* η, eta */
197 {'\336', "...."}, /* ή, eta with acute */
198 {'\350', "-.-."}, /* θ, theta */
199 {'\351', ".."}, /* ι, iota */
200 {'\337', ".."}, /* ί, iota with acute */
201 {'\372', ".."}, /* ϊ, iota with diaeresis */
202 {'\300', ".."}, /* ΐ, iota with acute and diaeresis */
203 {'\352', "-.-"}, /* κ, kappa */
204 {'\353', ".-.."}, /* λ, lambda */
205 {'\354', "--"}, /* μ, mu */
206 {'\355', "-."}, /* ν, nu */
207 {'\356', "-..-"}, /* ξ, xi */
208 {'\357', "---"}, /* ο, omicron */
209 {'\374', "---"}, /* ό, omicron with acute */
210 {'\360', ".--."}, /* π, pi */
211 {'\361', ".-."}, /* ρ, rho */
212 {'\363', "..."}, /* σ, sigma */
213 {'\362', "..."}, /* ς, final sigma */
214 {'\364', "-"}, /* τ, tau */
215 {'\365', "-.--"}, /* υ, upsilon */
216 {'\375', "-.--"}, /* ύ, upsilon with acute */
217 {'\373', "-.--"}, /* ϋ, upsilon and diaeresis */
218 {'\340', "-.--"}, /* ΰ, upsilon with acute and diaeresis */
219 {'\366', "..-."}, /* φ, phi */
220 {'\367', "----"}, /* χ, chi */
221 {'\370', "--.-"}, /* ψ, psi */
222 {'\371', ".--"}, /* ω, omega */
223 {'\376', ".--"}, /* ώ, omega with acute */
229 * Code-points for the Cyrillic alphabet in KOI8-R encoding.
230 * UTF-8 encoded chars in the comments.
232 static const struct morsetab koi8rtab[] = {
233 {'\301', ".-"}, /* а, a */
234 {'\302', "-..."}, /* б, be */
235 {'\327', ".--"}, /* в, ve */
236 {'\307', "--."}, /* г, ge */
237 {'\304', "-.."}, /* д, de */
238 {'\305', "."}, /* е, ye */
239 {'\243', "."}, /* ё, yo, the same as ye */
240 {'\326', "...-"}, /* ж, she */
241 {'\332', "--.."}, /* з, ze */
242 {'\311', ".."}, /* и, i */
243 {'\312', ".---"}, /* й, i kratkoye */
244 {'\313', "-.-"}, /* к, ka */
245 {'\314', ".-.."}, /* л, el */
246 {'\315', "--"}, /* м, em */
247 {'\316', "-."}, /* н, en */
248 {'\317', "---"}, /* о, o */
249 {'\320', ".--."}, /* п, pe */
250 {'\322', ".-."}, /* р, er */
251 {'\323', "..."}, /* с, es */
252 {'\324', "-"}, /* т, te */
253 {'\325', "..-"}, /* у, u */
254 {'\306', "..-."}, /* ф, ef */
255 {'\310', "...."}, /* х, kha */
256 {'\303', "-.-."}, /* ц, ce */
257 {'\336', "---."}, /* ч, che */
258 {'\333', "----"}, /* ш, sha */
259 {'\335', "--.-"}, /* щ, shcha */
260 {'\331', "-.--"}, /* ы, yi */
261 {'\330', "-..-"}, /* ь, myakhkij znak */
262 {'\334', "..-.."}, /* э, ae */
263 {'\300', "..--"}, /* ю, yu */
264 {'\321', ".-.-"}, /* я, ya */
269 void show(const char *), play(const char *), morse(char);
270 void ttyout(const char *);
271 void sighandler(int);
273 #define GETOPTOPTS "c:d:ef:lsw:"
275 "usage: morse [-els] [-d device] [-w speed] [-c speed] [-f frequency] [string ...]\n"
277 static int pflag, lflag, sflag, eflag;
278 static int wpm = 20; /* effective words per minute */
279 static int cpm; /* effective words per minute between
281 #define FREQUENCY 600
282 static int freq = FREQUENCY;
283 static char *device; /* for tty-controlled generator */
287 #define WORD_SPACE (7 - CHAR_SPACE - 1)
288 static float dot_clock;
289 static float cdot_clock;
291 struct termios otty, ntty;
297 #define GETOPTOPTS "c:d:ef:lpsw:"
300 "usage: morse [-elps] [-d device] [-w speed] [-c speed] [-f frequency] [string ...]\n"
303 static const struct morsetab *hightab;
306 main(int argc, char **argv)
311 while ((ch = getopt(argc, argv, GETOPTOPTS)) != -1)
321 setvbuf(stdout, 0, _IONBF, 0);
342 fputs(USAGE, stderr);
345 if (sflag && lflag) {
346 fputs("morse: only one of -l and -s allowed\n", stderr);
349 if ((pflag || device) && (sflag || lflag)) {
350 fputs("morse: only one of -p, -d and -l, -s allowed\n", stderr);
355 if ((pflag || device) && ((wpm < 1) || (wpm > 60) || (cpm < 1) || (cpm > 60))) {
356 fputs("morse: insane speed\n", stderr);
359 if ((pflag || device) && (freq == 0))
364 if ((spkr = open(SPEAKER, O_WRONLY, 0)) == -1) {
371 if ((line = open(device, O_WRONLY | O_NONBLOCK)) == -1) {
372 perror("open tty line");
375 if (tcgetattr(line, &otty) == -1) {
376 perror("tcgetattr() failed");
380 ntty.c_cflag |= CLOCAL;
381 tcsetattr(line, TCSANOW, &ntty);
382 lflags = fcntl(line, F_GETFL);
383 lflags &= ~O_NONBLOCK;
384 fcntl(line, F_SETFL, &lflags);
385 ioctl(line, TIOCMGET, &lflags);
386 lflags &= ~TIOCM_RTS;
388 ioctl(line, TIOCMSET, &lflags);
389 (void)signal(SIGHUP, sighandler);
390 (void)signal(SIGINT, sighandler);
391 (void)signal(SIGQUIT, sighandler);
392 (void)signal(SIGTERM, sighandler);
394 if (pflag || device) {
395 dot_clock = wpm / 2.4; /* dots/sec */
396 dot_clock = 1 / dot_clock; /* duration of a dot */
397 dot_clock = dot_clock / 2; /* dot_clock runs at twice */
399 dot_clock = dot_clock * 100; /* scale for ioctl */
401 cdot_clock = cpm / 2.4; /* dots/sec */
402 cdot_clock = 1 / cdot_clock; /* duration of a dot */
403 cdot_clock = cdot_clock / 2; /* dot_clock runs at twice */
405 cdot_clock = cdot_clock * 100; /* scale for ioctl */
411 if (setlocale(LC_CTYPE, "") != NULL &&
412 *(codeset = nl_langinfo(CODESET)) != '\0') {
413 if (strcmp(codeset, "KOI8-R") == 0)
415 else if (strcmp(codeset, "ISO8859-1") == 0 ||
416 strcmp(codeset, "ISO8859-15") == 0)
417 hightab = iso8859_1tab;
418 else if (strcmp(codeset, "ISO8859-7") == 0)
419 hightab = iso8859_7tab;
426 for (p = *argv; *p; ++p) {
436 while ((ch = getchar()) != EOF) {
443 tcsetattr(line, TCSANOW, &otty);
450 const struct morsetab *m;
452 if (isalpha((unsigned char)c))
453 c = tolower((unsigned char)c);
454 if ((c == '\r') || (c == '\n'))
467 for (m = ((unsigned char)c < 0x80? mtab: hightab);
468 m != NULL && m->inchar != '\0';
470 if (m->inchar == c) {
490 printf(" %s", *s == '.' ? *(s + 1) == '\0' ? "dit" :
502 for (c = s; *c != '\0'; c++) {
505 sound.frequency = freq;
506 sound.duration = dot_clock;
509 sound.frequency = freq;
510 sound.duration = dot_clock * DASH_LEN;
514 sound.duration = cdot_clock * WORD_SPACE;
519 if (sound.duration) {
520 if (ioctl(spkr, SPKRTONE, &sound) == -1) {
521 perror("ioctl play");
526 sound.duration = dot_clock;
527 if (ioctl(spkr, SPKRTONE, &sound) == -1) {
528 perror("ioctl rest");
533 sound.duration = cdot_clock * CHAR_SPACE;
534 ioctl(spkr, SPKRTONE, &sound);
539 ttyout(const char *s)
542 int duration, on, lflags;
544 for (c = s; *c != '\0'; c++) {
548 duration = dot_clock;
552 duration = dot_clock * DASH_LEN;
556 duration = cdot_clock * WORD_SPACE;
563 ioctl(line, TIOCMGET, &lflags);
565 ioctl(line, TIOCMSET, &lflags);
570 ioctl(line, TIOCMGET, &lflags);
571 lflags &= ~TIOCM_RTS;
572 ioctl(line, TIOCMSET, &lflags);
573 duration = dot_clock * 10000;
576 duration = cdot_clock * CHAR_SPACE * 10000;
581 sighandler(int signo)
584 ioctl(line, TIOCMSET, &olflags);
585 tcsetattr(line, TCSANOW, &otty);
587 signal(signo, SIG_DFL);
588 (void)kill(getpid(), signo);