From owner-svn-src-all@freebsd.org Sat Jan 6 15:10:05 2018 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 767A4EBD7B2; Sat, 6 Jan 2018 15:10:05 +0000 (UTC) (envelope-from freebsd@pdx.rh.CN85.dnsmgr.net) Received: from pdx.rh.CN85.dnsmgr.net (br1.CN84in.dnsmgr.net [69.59.192.140]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id D6FD5729A6; Sat, 6 Jan 2018 15:10:04 +0000 (UTC) (envelope-from freebsd@pdx.rh.CN85.dnsmgr.net) Received: from pdx.rh.CN85.dnsmgr.net (localhost [127.0.0.1]) by pdx.rh.CN85.dnsmgr.net (8.13.3/8.13.3) with ESMTP id w06FA3ws011988; Sat, 6 Jan 2018 07:10:03 -0800 (PST) (envelope-from freebsd@pdx.rh.CN85.dnsmgr.net) Received: (from freebsd@localhost) by pdx.rh.CN85.dnsmgr.net (8.13.3/8.13.3/Submit) id w06FA3sM011987; Sat, 6 Jan 2018 07:10:03 -0800 (PST) (envelope-from freebsd) From: "Rodney W. Grimes" Message-Id: <201801061510.w06FA3sM011987@pdx.rh.CN85.dnsmgr.net> Subject: Re: svn commit: r327614 - head/usr.bin/morse In-Reply-To: <201801060702.w0672Osg042505@repo.freebsd.org> To: Eitan Adler Date: Sat, 6 Jan 2018 07:10:03 -0800 (PST) CC: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Reply-To: rgrimes@freebsd.org X-Mailer: ELM [version 2.4ME+ PL121h (25)] MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 06 Jan 2018 15:10:05 -0000 [ Charset UTF-8 unsupported, converting... ] > Author: eadler > Date: Sat Jan 6 07:02:24 2018 > New Revision: 327614 > URL: https://svnweb.freebsd.org/changeset/base/327614 > > Log: > 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 > > Modified: > head/usr.bin/morse/morse.6 > head/usr.bin/morse/morse.c > > Modified: head/usr.bin/morse/morse.6 > ============================================================================== > --- head/usr.bin/morse/morse.6 Sat Jan 6 06:34:49 2018 (r327613) > +++ head/usr.bin/morse/morse.6 Sat Jan 6 07:02:24 2018 (r327614) > @@ -92,6 +92,11 @@ Echo each character before it is sent, used together w > .Fl p > or > .Fl d . > +.It Fl D > +Decode morse output consisting of dots and dashes (as generated by using > +the > +.Fl s > +option). Dflag here, program is rflag???? And I do not see an update to the SYNOPSYS section of the man page adding the rflag, something like: -.Op Fl elps +.Op Fl elprs Thanks, > .El > .Pp > The > > Modified: head/usr.bin/morse/morse.c > ============================================================================== > --- head/usr.bin/morse/morse.c Sat Jan 6 06:34:49 2018 (r327613) > +++ head/usr.bin/morse/morse.c Sat Jan 6 07:02:24 2018 (r327614) > @@ -46,6 +46,7 @@ static const char rcsid[] = > #include > > #include > +#include > #include > #include > #include > @@ -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 > #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); > + 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 @@ 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; > @@ -575,6 +594,68 @@ ttyout(const char *s) > } > duration = cdot_clock * CHAR_SPACE * 10000; > 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 > > -- Rod Grimes rgrimes@freebsd.org