Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 19 Feb 2002 11:46:10 +0100 (CET)
From:      Philipp Mergenthaler <philipp.mergenthaler@stud.uni-karlsruhe.de>
To:        FreeBSD-gnats-submit@freebsd.org
Subject:   bin/35109: [PATCH] games/morse: add ability to decode morse code
Message-ID:  <200202191046.g1JAk9R05228@i609a.hadiko.de>

next in thread | raw e-mail | index | archive | help


>Number:         35109
>Category:       bin
>Synopsis:       [PATCH] games/morse: add ability to decode morse code
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Tue Feb 19 02:50:01 PST 2002
>Closed-Date:
>Last-Modified:
>Originator:     Philipp Mergenthaler
>Release:        FreeBSD 5.0-CURRENT i386
>Organization:
University of Karlsruhe
>Environment:
System: FreeBSD i609a.hadiko.de 5.0-CURRENT FreeBSD 5.0-CURRENT #474: Mon Feb 18 21:56:26 CET 2002 p@i609a.hadiko.de:/usr/src/sys/i386/compile/I609 i386


>Description:
The first patch adds the option "-D" to morse(6) to decode morse code
(consisting of dots and dashes) that is given as command line arguments
or on the standard input.  This is inspired by and partially taken from
NetBSD's morse(6).
It's somewhat enhanced though - you can give several letters per command
line argument and it deals correctly with long lines when reading from
standard input.

The second patch adds codes for three characters: underline, apostrophe
and ñ (n with tilde).


>How-To-Repeat:
>Fix:

Index: morse.6
===================================================================
RCS file: /ncvs/src/games/morse/morse.6,v
retrieving revision 1.10
diff -u -r1.10 morse.6
--- morse.6	7 Aug 2001 15:48:28 -0000	1.10
+++ morse.6	19 Feb 2002 04:14:32 -0000
@@ -74,13 +74,18 @@
 .Fl p ,
 but use the RTS line of
 .Ar device
-(which must by a tty device)
+(which must be a tty device)
 in order to emit the morse code.
 .It Fl e
 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
Index: morse.c
===================================================================
RCS file: /ncvs/src/games/morse/morse.c,v
retrieving revision 1.14
diff -u -r1.14 morse.c
--- morse.c	26 Jun 2001 01:43:52 -0000	1.14
+++ morse.c	19 Feb 2002 09:50:38 -0000
@@ -198,15 +198,17 @@
 	{'\0', ""}
 };
 
-void            show(const char *), play(const char *), morse(char);
+void            show(const char *), play(const char *), morse(char),
+		    decode (char *), fdecode(FILE *);
 void		ttyout(const char *);
 void		sighandler(int);
 
-#define GETOPTOPTS "d:ef:sw:"
+#define GETOPTOPTS "d:ef:sw:D"
 #define USAGE \
-"usage: morse [-s] [-e] [-d device] [-w speed] [-f frequency] [string ...]\n"
+"usage: morse [-s] [-e] [-D] [-d device] [-w speed] [-f frequency] [string ...]\n"
+#define WHITESPACE " \t\n"
 
-static int      pflag, sflag, eflag;
+static int      pflag, sflag, eflag, dflag;
 static int      wpm = 20;	/* words per minute */
 #define FREQUENCY 600
 static int      freq = FREQUENCY;
@@ -223,10 +225,10 @@
 #ifdef SPEAKER
 tone_t          sound;
 #undef GETOPTOPTS
-#define GETOPTOPTS "d:ef:psw:"
+#define GETOPTOPTS "d:ef:psw:D"
 #undef USAGE
 #define USAGE \
-"usage: morse [-s] [-p] [-e] [-d device] [-w speed] [-f frequency] [string ...]\n"
+"usage: morse [-s] [-p] [-e] [-D] [-d device] [-w speed] [-f frequency] [string ...]\n"
 #endif
 
 static const struct morsetab *hightab;
@@ -242,6 +244,9 @@
 		case 'd':
 			device = optarg;
 			break;
+		case 'D':
+			dflag = 1;
+			break;
 		case 'e':
 			eflag = 1;
 			setvbuf(stdout, 0, _IONBF, 0);
@@ -328,22 +333,42 @@
 			hightab = iso8859tab;
 	}
 
-	if (*argv) {
-		do {
-			for (p = *argv; *p; ++p) {
+	if (dflag) {
+		if (*argv) {
+			do {
+				p=strtok(*argv, " \t");
+				if (p == NULL)
+					decode(*argv);
+				else {
+					while (p) {
+						decode(p);
+						p=strtok(NULL, " \t");
+					}
+				}
+			} while (*++argv);
+			putchar('\n');
+		} else 
+			fdecode(stdin);
+
+
+	} else {
+		if (*argv) {
+			do {
+				for (p = *argv; *p; ++p) {
+					if (eflag)
+						putchar(*p);
+					morse(*p);
+				}
 				if (eflag)
-					putchar(*p);
-				morse(*p);
+					putchar(' ');
+				morse(' ');
+			} while (*++argv);
+		} else {
+			while ((ch = getchar()) != EOF) {
+				if (eflag)
+					putchar(ch);
+				morse(ch);
 			}
-			if (eflag)
-				putchar(' ');
-			morse(' ');
-		} while (*++argv);
-	} else {
-		while ((ch = getchar()) != EOF) {
-			if (eflag)
-				putchar(ch);
-			morse(ch);
 		}
 	}
 	if (device)
@@ -490,4 +515,69 @@
 
 	signal(signo, SIG_DFL);
 	(void)kill(getpid(), signo);
+}
+
+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);
 }








--- morse.c	Tue Feb 19 11:19:09 2002
+++ morse.c.new	Tue Feb 19 11:21:42 2002
@@ -130,6 +130,8 @@
 	{')', "-.--.-"},
 	{'$', "...-..-"},
 	{'+', ".-.-."},		/* AR */
+	{'_', "..--.-"},
+	{'\'', ".---."},
 
 	/* prosigns without already assigned values */
 
@@ -151,6 +153,7 @@
 	{'é', "..-.."},
 	{'è', "..-.."},
 	{'ê', "-..-."},
+	{'ñ', "--.--"},
 	{'ö', "---."},
 	{'ü', "..--"},
 
>Release-Note:
>Audit-Trail:
>Unformatted:

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-bugs" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200202191046.g1JAk9R05228>