morse(6): implement support for decoding morse code

- Use `-r` for "reverse" mode and to match DragonFlyBSD.
- Move defines around to clear up logic
- use `errx` instead of `fprintf` and `exit`

PR:		35109
Submitted By:	philipp.mergenthaler@stud.uni-karlsruhe.de
Submitted on:	2002-02-19
Reviewed by:	kevans
This commit is contained in:
eadler 2018-01-06 07:02:24 +00:00
parent b3e85e1a3d
commit 1ffe779759
2 changed files with 120 additions and 34 deletions

View File

@ -92,6 +92,11 @@ Echo each character before it is sent, used together with either
.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

View File

@ -46,6 +46,7 @@ static const char rcsid[] =
#include <sys/ioctl.h>
#include <ctype.h>
#include <err.h>
#include <fcntl.h>
#include <langinfo.h>
#include <locale.h>
@ -59,6 +60,9 @@ static const char rcsid[] =
/* Always use the speaker, let the open fail if -p is selected */
#define SPEAKER "/dev/speaker"
#define WHITESPACE " \t\n"
#define DELIMITERS " \t"
#ifdef SPEAKER
#include <dev/speaker/speaker.h>
#endif
@ -267,14 +271,11 @@ static const struct morsetab koi8rtab[] = {
};
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 @@ static int olflags;
#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 [-elps] [-d device] [-w speed] [-c speed] [-f frequency] [string ...]\n"
"usage: morse [-elprs] [-d device] [-w speed] [-c speed] [-f frequency] [string ...]\n"
#else
#define GETOPTOPTS "c:d:ef:lrsw:"
#define USAGE \
"usage: morse [-elrs] [-d device] [-w speed] [-c speed] [-f frequency] [string ...]\n"
#endif
static const struct morsetab *hightab;
@ -331,6 +335,9 @@ main(int argc, char **argv)
pflag = 1;
break;
#endif
case 'r':
rflag = 1;
break;
case 's':
sflag = 1;
break;
@ -339,42 +346,36 @@ main(int argc, char **argv)
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);
}
if ((pflag || device) && (freq == 0))
if ((pflag || device) && ((wpm < 1) || (wpm > 60) || (cpm < 1) || (cpm > 60))) {
errx(1, "morse: insane speed\n");
}
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 @@ main(int argc, char **argv)
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 @@ play(const char *s)
}
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 @@ ttyout(const char *s)
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)
{