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:
parent
b3e85e1a3d
commit
1ffe779759
@ -92,6 +92,11 @@ Echo each character before it is sent, used together with either
|
|||||||
.Fl p
|
.Fl p
|
||||||
or
|
or
|
||||||
.Fl d .
|
.Fl d .
|
||||||
|
.It Fl D
|
||||||
|
Decode morse output consisting of dots and dashes (as generated by using
|
||||||
|
the
|
||||||
|
.Fl s
|
||||||
|
option).
|
||||||
.El
|
.El
|
||||||
.Pp
|
.Pp
|
||||||
The
|
The
|
||||||
|
@ -46,6 +46,7 @@ static const char rcsid[] =
|
|||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <err.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <langinfo.h>
|
#include <langinfo.h>
|
||||||
#include <locale.h>
|
#include <locale.h>
|
||||||
@ -59,6 +60,9 @@ static const char rcsid[] =
|
|||||||
/* Always use the speaker, let the open fail if -p is selected */
|
/* 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
|
#ifdef SPEAKER
|
||||||
#include <dev/speaker/speaker.h>
|
#include <dev/speaker/speaker.h>
|
||||||
#endif
|
#endif
|
||||||
@ -267,14 +271,11 @@ static const struct morsetab koi8rtab[] = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
static void show(const char *), play(const char *), morse(char);
|
static void show(const char *), play(const char *), morse(char);
|
||||||
|
static void decode (char *), fdecode(FILE *);
|
||||||
static void ttyout(const char *);
|
static void ttyout(const char *);
|
||||||
static void sighandler(int);
|
static void sighandler(int);
|
||||||
|
|
||||||
#define GETOPTOPTS "c:d:ef:lsw:"
|
static int pflag, lflag, rflag, sflag, eflag;
|
||||||
#define USAGE \
|
|
||||||
"usage: morse [-els] [-d device] [-w speed] [-c speed] [-f frequency] [string ...]\n"
|
|
||||||
|
|
||||||
static int pflag, lflag, sflag, eflag;
|
|
||||||
static int wpm = 20; /* effective words per minute */
|
static int wpm = 20; /* effective words per minute */
|
||||||
static int cpm; /* effective words per minute between
|
static int cpm; /* effective words per minute between
|
||||||
* characters */
|
* characters */
|
||||||
@ -293,11 +294,14 @@ static int olflags;
|
|||||||
|
|
||||||
#ifdef SPEAKER
|
#ifdef SPEAKER
|
||||||
static tone_t sound;
|
static tone_t sound;
|
||||||
#undef GETOPTOPTS
|
#define GETOPTOPTS "c:d:ef:lprsw:"
|
||||||
#define GETOPTOPTS "c:d:ef:lpsw:"
|
|
||||||
#undef USAGE
|
|
||||||
#define USAGE \
|
#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
|
#endif
|
||||||
|
|
||||||
static const struct morsetab *hightab;
|
static const struct morsetab *hightab;
|
||||||
@ -331,6 +335,9 @@ main(int argc, char **argv)
|
|||||||
pflag = 1;
|
pflag = 1;
|
||||||
break;
|
break;
|
||||||
#endif
|
#endif
|
||||||
|
case 'r':
|
||||||
|
rflag = 1;
|
||||||
|
break;
|
||||||
case 's':
|
case 's':
|
||||||
sflag = 1;
|
sflag = 1;
|
||||||
break;
|
break;
|
||||||
@ -339,42 +346,36 @@ main(int argc, char **argv)
|
|||||||
break;
|
break;
|
||||||
case '?':
|
case '?':
|
||||||
default:
|
default:
|
||||||
fputs(USAGE, stderr);
|
errx(1, USAGE);
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
if (sflag && lflag) {
|
if ((sflag && lflag) || (sflag && rflag) || (lflag && rflag)) {
|
||||||
fputs("morse: only one of -l and -s allowed\n", stderr);
|
errx(1, "morse: only one of -l, -s, and -r allowed\n");
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
if ((pflag || device) && (sflag || lflag)) {
|
if ((pflag || device) && (sflag || lflag)) {
|
||||||
fputs("morse: only one of -p, -d and -l, -s allowed\n", stderr);
|
errx(1, "morse: only one of -p, -d and -l, -s allowed\n");
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
if (cpm == 0)
|
if (cpm == 0) {
|
||||||
cpm = wpm;
|
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;
|
freq = FREQUENCY;
|
||||||
|
}
|
||||||
#ifdef SPEAKER
|
#ifdef SPEAKER
|
||||||
if (pflag) {
|
if (pflag) {
|
||||||
if ((spkr = open(SPEAKER, O_WRONLY, 0)) == -1) {
|
if ((spkr = open(SPEAKER, O_WRONLY, 0)) == -1) {
|
||||||
perror(SPEAKER);
|
err(1, SPEAKER);
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
} else
|
} else
|
||||||
#endif
|
#endif
|
||||||
if (device) {
|
if (device) {
|
||||||
if ((line = open(device, O_WRONLY | O_NONBLOCK)) == -1) {
|
if ((line = open(device, O_WRONLY | O_NONBLOCK)) == -1) {
|
||||||
perror("open tty line");
|
err(1, "open tty line");
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
if (tcgetattr(line, &otty) == -1) {
|
if (tcgetattr(line, &otty) == -1) {
|
||||||
perror("tcgetattr() failed");
|
err(1, "tcgetattr() failed");
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
ntty = otty;
|
ntty = otty;
|
||||||
ntty.c_cflag |= CLOCAL;
|
ntty.c_cflag |= CLOCAL;
|
||||||
@ -419,9 +420,29 @@ main(int argc, char **argv)
|
|||||||
hightab = iso8859_7tab;
|
hightab = iso8859_7tab;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lflag)
|
if (lflag) {
|
||||||
printf("m");
|
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 {
|
do {
|
||||||
for (p = *argv; *p; ++p) {
|
for (p = *argv; *p; ++p) {
|
||||||
if (eflag)
|
if (eflag)
|
||||||
@ -518,15 +539,13 @@ play(const char *s)
|
|||||||
}
|
}
|
||||||
if (sound.duration) {
|
if (sound.duration) {
|
||||||
if (ioctl(spkr, SPKRTONE, &sound) == -1) {
|
if (ioctl(spkr, SPKRTONE, &sound) == -1) {
|
||||||
perror("ioctl play");
|
err(1, "ioctl play");
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sound.frequency = 0;
|
sound.frequency = 0;
|
||||||
sound.duration = dot_clock;
|
sound.duration = dot_clock;
|
||||||
if (ioctl(spkr, SPKRTONE, &sound) == -1) {
|
if (ioctl(spkr, SPKRTONE, &sound) == -1) {
|
||||||
perror("ioctl rest");
|
err(1, "ioctl rest");
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sound.frequency = 0;
|
sound.frequency = 0;
|
||||||
@ -577,6 +596,68 @@ ttyout(const char *s)
|
|||||||
usleep(duration);
|
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
|
static void
|
||||||
sighandler(int signo)
|
sighandler(int signo)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user