morse.c (1796f7b1ff301c81fea3b1c2aec46cf1b1333955) morse.c (d213312147e13a62f9bd18bd0ee292690dbdad92)
1/*
2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 32 unchanged lines hidden (view full) ---

41#endif
42static const char rcsid[] =
43 "$FreeBSD$";
44
45#include <sys/time.h>
46#include <sys/ioctl.h>
47
48#include <ctype.h>
1/*
2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 32 unchanged lines hidden (view full) ---

41#endif
42static const char rcsid[] =
43 "$FreeBSD$";
44
45#include <sys/time.h>
46#include <sys/ioctl.h>
47
48#include <ctype.h>
49#include <err.h>
49#include <fcntl.h>
50#include <langinfo.h>
51#include <locale.h>
52#include <signal.h>
53#include <stdio.h>
54#include <stdlib.h>
55#include <string.h>
56#include <termios.h>
57#include <unistd.h>
58
59/* Always use the speaker, let the open fail if -p is selected */
60#define SPEAKER "/dev/speaker"
61
50#include <fcntl.h>
51#include <langinfo.h>
52#include <locale.h>
53#include <signal.h>
54#include <stdio.h>
55#include <stdlib.h>
56#include <string.h>
57#include <termios.h>
58#include <unistd.h>
59
60/* Always use the speaker, let the open fail if -p is selected */
61#define SPEAKER "/dev/speaker"
62
63#define WHITESPACE " \t\n"
64#define DELIMITERS " \t"
65
62#ifdef SPEAKER
63#include <dev/speaker/speaker.h>
64#endif
65
66struct morsetab {
67 const char inchar;
68 const char *morse;
69};

--- 192 unchanged lines hidden (view full) ---

262 {'\334', "..-.."}, /* э, ae */
263 {'\300', "..--"}, /* ю, yu */
264 {'\321', ".-.-"}, /* я, ya */
265
266 {'\0', ""}
267};
268
269static void show(const char *), play(const char *), morse(char);
66#ifdef SPEAKER
67#include <dev/speaker/speaker.h>
68#endif
69
70struct morsetab {
71 const char inchar;
72 const char *morse;
73};

--- 192 unchanged lines hidden (view full) ---

266 {'\334', "..-.."}, /* э, ae */
267 {'\300', "..--"}, /* ю, yu */
268 {'\321', ".-.-"}, /* я, ya */
269
270 {'\0', ""}
271};
272
273static void show(const char *), play(const char *), morse(char);
274static void decode (char *), fdecode(FILE *);
270static void ttyout(const char *);
271static void sighandler(int);
272
275static void ttyout(const char *);
276static void sighandler(int);
277
273#define GETOPTOPTS "c:d:ef:lsw:"
274#define USAGE \
275"usage: morse [-els] [-d device] [-w speed] [-c speed] [-f frequency] [string ...]\n"
276
277static int pflag, lflag, sflag, eflag;
278static int pflag, lflag, rflag, sflag, eflag;
278static int wpm = 20; /* effective words per minute */
279static int cpm; /* effective words per minute between
280 * characters */
281#define FREQUENCY 600
282static int freq = FREQUENCY;
283static char *device; /* for tty-controlled generator */
284
285#define DASH_LEN 3
286#define CHAR_SPACE 3
287#define WORD_SPACE (7 - CHAR_SPACE - 1)
288static float dot_clock;
289static float cdot_clock;
290static int spkr, line;
291static struct termios otty, ntty;
292static int olflags;
293
294#ifdef SPEAKER
295static tone_t sound;
279static int wpm = 20; /* effective words per minute */
280static int cpm; /* effective words per minute between
281 * characters */
282#define FREQUENCY 600
283static int freq = FREQUENCY;
284static char *device; /* for tty-controlled generator */
285
286#define DASH_LEN 3
287#define CHAR_SPACE 3
288#define WORD_SPACE (7 - CHAR_SPACE - 1)
289static float dot_clock;
290static float cdot_clock;
291static int spkr, line;
292static struct termios otty, ntty;
293static int olflags;
294
295#ifdef SPEAKER
296static tone_t sound;
296#undef GETOPTOPTS
297#define GETOPTOPTS "c:d:ef:lpsw:"
298#undef USAGE
297#define GETOPTOPTS "c:d:ef:lprsw:"
299#define USAGE \
298#define USAGE \
300"usage: morse [-elps] [-d device] [-w speed] [-c speed] [-f frequency] [string ...]\n"
299"usage: morse [-elprs] [-d device] [-w speed] [-c speed] [-f frequency] [string ...]\n"
300#else
301#define GETOPTOPTS "c:d:ef:lrsw:"
302#define USAGE \
303"usage: morse [-elrs] [-d device] [-w speed] [-c speed] [-f frequency] [string ...]\n"
304
301#endif
302
303static const struct morsetab *hightab;
304
305int
306main(int argc, char **argv)
307{
308 int ch, lflags;

--- 17 unchanged lines hidden (view full) ---

326 case 'l':
327 lflag = 1;
328 break;
329#ifdef SPEAKER
330 case 'p':
331 pflag = 1;
332 break;
333#endif
305#endif
306
307static const struct morsetab *hightab;
308
309int
310main(int argc, char **argv)
311{
312 int ch, lflags;

--- 17 unchanged lines hidden (view full) ---

330 case 'l':
331 lflag = 1;
332 break;
333#ifdef SPEAKER
334 case 'p':
335 pflag = 1;
336 break;
337#endif
338 case 'r':
339 rflag = 1;
340 break;
334 case 's':
335 sflag = 1;
336 break;
337 case 'w':
338 wpm = atoi(optarg);
339 break;
340 case '?':
341 default:
341 case 's':
342 sflag = 1;
343 break;
344 case 'w':
345 wpm = atoi(optarg);
346 break;
347 case '?':
348 default:
342 fputs(USAGE, stderr);
343 exit(1);
349 errx(1, USAGE);
344 }
350 }
345 if (sflag && lflag) {
346 fputs("morse: only one of -l and -s allowed\n", stderr);
347 exit(1);
351 if ((sflag && lflag) || (sflag && rflag) || (lflag && rflag)) {
352 errx(1, "morse: only one of -l, -s, and -r allowed\n");
348 }
349 if ((pflag || device) && (sflag || lflag)) {
353 }
354 if ((pflag || device) && (sflag || lflag)) {
350 fputs("morse: only one of -p, -d and -l, -s allowed\n", stderr);
351 exit(1);
355 errx(1, "morse: only one of -p, -d and -l, -s allowed\n");
352 }
356 }
353 if (cpm == 0)
357 if (cpm == 0) {
354 cpm = wpm;
358 cpm = wpm;
359 }
355 if ((pflag || device) && ((wpm < 1) || (wpm > 60) || (cpm < 1) || (cpm > 60))) {
360 if ((pflag || device) && ((wpm < 1) || (wpm > 60) || (cpm < 1) || (cpm > 60))) {
356 fputs("morse: insane speed\n", stderr);
357 exit(1);
361 errx(1, "morse: insane speed\n");
358 }
362 }
359 if ((pflag || device) && (freq == 0))
363 if ((pflag || device) && (freq == 0)) {
360 freq = FREQUENCY;
364 freq = FREQUENCY;
361
365 }
362#ifdef SPEAKER
363 if (pflag) {
364 if ((spkr = open(SPEAKER, O_WRONLY, 0)) == -1) {
366#ifdef SPEAKER
367 if (pflag) {
368 if ((spkr = open(SPEAKER, O_WRONLY, 0)) == -1) {
365 perror(SPEAKER);
366 exit(1);
369 err(1, SPEAKER);
367 }
368 } else
369#endif
370 if (device) {
371 if ((line = open(device, O_WRONLY | O_NONBLOCK)) == -1) {
370 }
371 } else
372#endif
373 if (device) {
374 if ((line = open(device, O_WRONLY | O_NONBLOCK)) == -1) {
372 perror("open tty line");
373 exit(1);
375 err(1, "open tty line");
374 }
375 if (tcgetattr(line, &otty) == -1) {
376 }
377 if (tcgetattr(line, &otty) == -1) {
376 perror("tcgetattr() failed");
377 exit(1);
378 err(1, "tcgetattr() failed");
378 }
379 ntty = otty;
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);

--- 28 unchanged lines hidden (view full) ---

414 hightab = koi8rtab;
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;
420 }
421
379 }
380 ntty = otty;
381 ntty.c_cflag |= CLOCAL;
382 tcsetattr(line, TCSANOW, &ntty);
383 lflags = fcntl(line, F_GETFL);
384 lflags &= ~O_NONBLOCK;
385 fcntl(line, F_SETFL, &lflags);
386 ioctl(line, TIOCMGET, &lflags);

--- 28 unchanged lines hidden (view full) ---

415 hightab = koi8rtab;
416 else if (strcmp(codeset, "ISO8859-1") == 0 ||
417 strcmp(codeset, "ISO8859-15") == 0)
418 hightab = iso8859_1tab;
419 else if (strcmp(codeset, "ISO8859-7") == 0)
420 hightab = iso8859_7tab;
421 }
422
422 if (lflag)
423 if (lflag) {
423 printf("m");
424 printf("m");
424 if (*argv) {
425 }
426 if (rflag) {
427 if (*argv) {
428 do {
429 p = strtok(*argv, DELIMITERS);
430 if (p == NULL) {
431 decode(*argv);
432 }
433 else {
434 while (p) {
435 decode(p);
436 p = strtok(NULL, DELIMITERS);
437 }
438 }
439 } while (*++argv);
440 putchar('\n');
441 } else {
442 fdecode(stdin);
443 }
444 }
445 else if (*argv) {
425 do {
426 for (p = *argv; *p; ++p) {
427 if (eflag)
428 putchar(*p);
429 morse(*p);
430 }
431 if (eflag)
432 putchar(' ');

--- 80 unchanged lines hidden (view full) ---

513 sound.frequency = 0;
514 sound.duration = cdot_clock * WORD_SPACE;
515 break;
516 default:
517 sound.duration = 0;
518 }
519 if (sound.duration) {
520 if (ioctl(spkr, SPKRTONE, &sound) == -1) {
446 do {
447 for (p = *argv; *p; ++p) {
448 if (eflag)
449 putchar(*p);
450 morse(*p);
451 }
452 if (eflag)
453 putchar(' ');

--- 80 unchanged lines hidden (view full) ---

534 sound.frequency = 0;
535 sound.duration = cdot_clock * WORD_SPACE;
536 break;
537 default:
538 sound.duration = 0;
539 }
540 if (sound.duration) {
541 if (ioctl(spkr, SPKRTONE, &sound) == -1) {
521 perror("ioctl play");
522 exit(1);
542 err(1, "ioctl play");
523 }
524 }
525 sound.frequency = 0;
526 sound.duration = dot_clock;
527 if (ioctl(spkr, SPKRTONE, &sound) == -1) {
543 }
544 }
545 sound.frequency = 0;
546 sound.duration = dot_clock;
547 if (ioctl(spkr, SPKRTONE, &sound) == -1) {
528 perror("ioctl rest");
529 exit(1);
548 err(1, "ioctl rest");
530 }
531 }
532 sound.frequency = 0;
533 sound.duration = cdot_clock * CHAR_SPACE;
534 ioctl(spkr, SPKRTONE, &sound);
535#endif
536}
537

--- 34 unchanged lines hidden (view full) ---

572 ioctl(line, TIOCMSET, &lflags);
573 duration = dot_clock * 10000;
574 usleep(duration);
575 }
576 duration = cdot_clock * CHAR_SPACE * 10000;
577 usleep(duration);
578}
579
549 }
550 }
551 sound.frequency = 0;
552 sound.duration = cdot_clock * CHAR_SPACE;
553 ioctl(spkr, SPKRTONE, &sound);
554#endif
555}
556

--- 34 unchanged lines hidden (view full) ---

591 ioctl(line, TIOCMSET, &lflags);
592 duration = dot_clock * 10000;
593 usleep(duration);
594 }
595 duration = cdot_clock * CHAR_SPACE * 10000;
596 usleep(duration);
597}
598
599void
600fdecode(FILE *stream)
601{
602 char *n, *p, *s;
603 char buf[BUFSIZ];
604
605 s = buf;
606 while (fgets(s, BUFSIZ - (s - buf), stdin)) {
607 p = buf;
608
609 while (*p && isblank(*p)) {
610 p++;
611 }
612 while (*p && isspace(*p)) {
613 p++;
614 putchar (' ');
615 }
616 while (*p) {
617 n = strpbrk(p, WHITESPACE);
618 if (n == NULL) {
619 /* The token was interrupted at the end
620 * of the buffer. Shift it to the begin
621 * of the buffer.
622 */
623 for (s = buf; *p; *s++ = *p++)
624 ;
625 } else {
626 *n = '\0';
627 n++;
628 decode(p);
629 p = n;
630 }
631 }
632 }
633 putchar('\n');
634}
635
636void
637decode(char *p)
638{
639 char c;
640 const struct morsetab *m;
641
642 c = ' ';
643 for (m = mtab; m != NULL && m->inchar != '\0'; m++) {
644 if (strcmp(m->morse, p) == 0) {
645 c = m->inchar;
646 break;
647 }
648 }
649
650 if (c == ' ')
651 for (m = hightab; m != NULL && m->inchar != '\0'; m++) {
652 if (strcmp(m->morse, p) == 0) {
653 c = m->inchar;
654 break;
655 }
656 }
657
658 putchar(c);
659}
660
580static void
581sighandler(int signo)
582{
583
584 ioctl(line, TIOCMSET, &olflags);
585 tcsetattr(line, TCSANOW, &otty);
586
587 signal(signo, SIG_DFL);
588 (void)kill(getpid(), signo);
589}
661static void
662sighandler(int signo)
663{
664
665 ioctl(line, TIOCMSET, &olflags);
666 tcsetattr(line, TCSANOW, &otty);
667
668 signal(signo, SIG_DFL);
669 (void)kill(getpid(), signo);
670}