Index: usr.bin/morse/morse.6 =================================================================== --- usr.bin/morse/morse.6 +++ usr.bin/morse/morse.6 @@ -92,6 +92,11 @@ .Fl p or .Fl d . +.It Fl D +Decode morse output consisting of dots and dashes (as generated by using +the +.Fl s +option). .El .Pp The Index: usr.bin/morse/morse.c =================================================================== --- usr.bin/morse/morse.c +++ usr.bin/morse/morse.c @@ -46,6 +46,7 @@ #include #include +#include #include #include #include @@ -57,7 +58,10 @@ #include /* Always use the speaker, let the open fail if -p is selected */ -#define SPEAKER "/dev/speaker" +//#define SPEAKER "/dev/speaker" + +#define WHITESPACE " \t\n" +#define DELIMITERS " \t" #ifdef SPEAKER #include @@ -267,14 +271,11 @@ }; static void show(const char *), play(const char *), morse(char); +static void decode (char *), fdecode(FILE *); static void ttyout(const char *); static void sighandler(int); -#define GETOPTOPTS "c:d:ef:lsw:" -#define USAGE \ -"usage: morse [-els] [-d device] [-w speed] [-c speed] [-f frequency] [string ...]\n" - -static int pflag, lflag, sflag, eflag; +static int pflag, lflag, rflag, sflag, eflag; static int wpm = 20; /* effective words per minute */ static int cpm; /* effective words per minute between * characters */ @@ -293,11 +294,14 @@ #ifdef SPEAKER static tone_t sound; -#undef GETOPTOPTS -#define GETOPTOPTS "c:d:ef:lpsw:" -#undef USAGE +#define GETOPTOPTS "c:d:ef:lprsw:" +#define USAGE \ +"usage: morse [-elprs] [-d device] [-w speed] [-c speed] [-f frequency] [string ...]\n" +#else +#define GETOPTOPTS "c:d:ef:lrsw:" #define USAGE \ -"usage: morse [-elps] [-d device] [-w speed] [-c speed] [-f frequency] [string ...]\n" +"usage: morse [-elrs] [-d device] [-w speed] [-c speed] [-f frequency] [string ...]\n" + #endif static const struct morsetab *hightab; @@ -331,6 +335,9 @@ pflag = 1; break; #endif + case 'r': + rflag = 1; + break; case 's': sflag = 1; break; @@ -339,42 +346,36 @@ break; case '?': default: - fputs(USAGE, stderr); - exit(1); + errx(1, USAGE); } - if (sflag && lflag) { - fputs("morse: only one of -l and -s allowed\n", stderr); - exit(1); + if ((sflag && lflag) || (sflag && rflag) || (lflag && rflag)) { + errx(1, "morse: only one of -l, -s, and -r allowed\n"); } if ((pflag || device) && (sflag || lflag)) { - fputs("morse: only one of -p, -d and -l, -s allowed\n", stderr); - exit(1); + errx(1, "morse: only one of -p, -d and -l, -s allowed\n"); } - if (cpm == 0) + if (cpm == 0) { cpm = wpm; + } if ((pflag || device) && ((wpm < 1) || (wpm > 60) || (cpm < 1) || (cpm > 60))) { - fputs("morse: insane speed\n", stderr); - exit(1); + errx(1, "morse: insane speed\n"); } - if ((pflag || device) && (freq == 0)) + if ((pflag || device) && (freq == 0)) { freq = FREQUENCY; - + } #ifdef SPEAKER if (pflag) { if ((spkr = open(SPEAKER, O_WRONLY, 0)) == -1) { - perror(SPEAKER); - exit(1); + err(1, SPEAKER); } } else #endif if (device) { if ((line = open(device, O_WRONLY | O_NONBLOCK)) == -1) { - perror("open tty line"); - exit(1); + err(1, "open tty line"); } if (tcgetattr(line, &otty) == -1) { - perror("tcgetattr() failed"); - exit(1); + err(1, "tcgetattr() failed"); } ntty = otty; ntty.c_cflag |= CLOCAL; @@ -419,9 +420,29 @@ hightab = iso8859_7tab; } - if (lflag) + if (lflag) { printf("m"); - if (*argv) { + } + if (rflag) { + if (*argv) { + do { + p = strtok(*argv, DELIMITERS); + if (p == NULL) { + decode(*argv); + } + else { + while (p) { + decode(p); + p = strtok(NULL, DELIMITERS); + } + } + } while (*++argv); + putchar('\n'); + } else { + fdecode(stdin); + } + } + else if (*argv) { do { for (p = *argv; *p; ++p) { if (eflag) @@ -518,15 +539,13 @@ } if (sound.duration) { if (ioctl(spkr, SPKRTONE, &sound) == -1) { - perror("ioctl play"); - exit(1); + err(1, "ioctl play"); } } sound.frequency = 0; sound.duration = dot_clock; if (ioctl(spkr, SPKRTONE, &sound) == -1) { - perror("ioctl rest"); - exit(1); + err(1, "ioctl rest"); } } sound.frequency = 0; @@ -577,6 +596,68 @@ usleep(duration); } +void +fdecode(FILE *stream) +{ + char *n, *p, *s; + char buf[BUFSIZ]; + + s = buf; + while (fgets(s, BUFSIZ - (s - buf), stdin)) { + p = buf; + + while (*p && isblank(*p)) { + p++; + } + while (*p && isspace(*p)) { + p++; + putchar (' '); + } + while (*p) { + n = strpbrk(p, WHITESPACE); + if (n == NULL) { + /* The token was interrupted at the end + * of the buffer. Shift it to the begin + * of the buffer. + */ + for (s = buf; *p; *s++ = *p++) + ; + } else { + *n = '\0'; + n++; + decode(p); + p = n; + } + } + } + putchar('\n'); +} + +void +decode(char *p) +{ + char c; + const struct morsetab *m; + + c = ' '; + for (m = mtab; m != NULL && m->inchar != '\0'; m++) { + if (strcmp(m->morse, p) == 0) { + c = m->inchar; + break; + } + } + + if (c == ' ') + for (m = hightab; m != NULL && m->inchar != '\0'; m++) { + if (strcmp(m->morse, p) == 0) { + c = m->inchar; + break; + } + } + + putchar(c); +} + static void sighandler(int signo) {