summaryrefslogtreecommitdiffstats
path: root/games/morse
diff options
context:
space:
mode:
authorjoerg <joerg@FreeBSD.org>1995-08-27 14:06:15 +0000
committerjoerg <joerg@FreeBSD.org>1995-08-27 14:06:15 +0000
commit136398cad8e9e85870c298eb505c9d1cb86b537f (patch)
tree770a6ce957ee474617fa83bfc9da78fbc7a76661 /games/morse
parent1d51f50e5a7d95f2963ce8fd2ee91e7e5a2d1a87 (diff)
downloadFreeBSD-src-136398cad8e9e85870c298eb505c9d1cb86b537f.zip
FreeBSD-src-136398cad8e9e85870c298eb505c9d1cb86b537f.tar.gz
Make morse(6) realling playing the characters on the speaker.
Minor additions by me, mostly taken from the suggestion in PR # bin/709. Submitted by: lyndon@orthanc.com (Lyndon Nerenberg, VE7TCP/VE6BBM)
Diffstat (limited to 'games/morse')
-rw-r--r--games/morse/Makefile2
-rw-r--r--games/morse/morse.c365
2 files changed, 294 insertions, 73 deletions
diff --git a/games/morse/Makefile b/games/morse/Makefile
index 1d23a0a..4441189 100644
--- a/games/morse/Makefile
+++ b/games/morse/Makefile
@@ -3,6 +3,6 @@
PROG= morse
NOMAN= noman
HIDEGAME=hidegame
+CFLAGS += -DSPEAKER=\"/dev/speaker\"
.include <bsd.prog.mk>
-
diff --git a/games/morse/morse.c b/games/morse/morse.c
index ca8a7a3..a78e29f 100644
--- a/games/morse/morse.c
+++ b/games/morse/morse.c
@@ -31,6 +31,11 @@
* SUCH DAMAGE.
*/
+/*
+ * Taught to send *real* morse by Lyndon Nerenberg (VE7TCP/VE6BBM)
+ * <lyndon@orthanc.com>
+ */
+
#ifndef lint
static char copyright[] =
"@(#) Copyright (c) 1988, 1993\n\
@@ -43,103 +48,319 @@ static char sccsid[] = "@(#)morse.c 8.1 (Berkeley) 5/31/93";
#include <stdio.h>
#include <ctype.h>
+#include <locale.h>
+#include <stdlib.h>
+
+#ifdef SPEAKER
+#include <machine/speaker.h>
+#include <fcntl.h>
+#endif
+
+struct morsetab {
+ char inchar;
+ char *morse;
+};
+
+static struct morsetab mtab[] = {
+
+ /* letters */
+
+ 'a', ".-",
+ 'b', "-...",
+ 'c', "-.-.",
+ 'd', "-..",
+ 'e', ".",
+ 'f', "..-.",
+ 'g', "--.",
+ 'h', "....",
+ 'i', "..",
+ 'j', ".---",
+ 'k', "-.-",
+ 'l', ".-..",
+ 'm', "--",
+ 'n', "-.",
+ 'o', "---",
+ 'p', ".--.",
+ 'q', "--.-",
+ 'r', ".-.",
+ 's', "...",
+ 't', "-",
+ 'u', "..-",
+ 'v', "...-",
+ 'w', ".--",
+ 'x', "-..-",
+ 'y', "-.--",
+ 'z', "--..",
+
+ /* digits */
+
+ '0', "-----",
+ '1', ".----",
+ '2', "..---",
+ '3', "...--",
+ '4', "....-",
+ '5', ".....",
+ '6', "-....",
+ '7', "--...",
+ '8', "---..",
+ '9', "----.",
+
+ /* punctuation */
-static char
- *digit[] = {
- "-----",
- ".----",
- "..---",
- "...--",
- "....-",
- ".....",
- "-....",
- "--...",
- "---..",
- "----.",
-},
- *alph[] = {
- ".-",
- "-...",
- "-.-.",
- "-..",
- ".",
- "..-.",
- "--.",
- "....",
- "..",
- ".---",
- "-.-",
- ".-..",
- "--",
- "-.",
- "---",
- ".--.",
- "--.-",
- ".-.",
- "...",
- "-",
- "..-",
- "...-",
- ".--",
- "-..-",
- "-.--",
- "--..",
+ ',', "--..--",
+ '.', ".-.-.-",
+ '?', "..--..",
+ '/', "-..-.",
+ '-', "-....-",
+ '=', "-...-", /* BT */
+ ':', "---...",
+ ';', "-.-.-.",
+ '(', "-.--.", /* KN */
+ ')', "-.--.-",
+ '$', "...-..-",
+ '+', ".-.-.", /* AR */
+
+ /* prosigns without already assigned values */
+
+ '#', ".-...", /* AS */
+ '@', "...-.-", /* SK */
+ '*', "...-.", /* VE */
+ '%', "-...-.-", /* BK */
+
+ '\0', ""
+};
+
+
+static struct morsetab iso8859tab[] = {
+ 'á', ".--.-",
+ 'à', ".--.-",
+ 'â', ".--.-",
+ 'ä', ".-.-",
+ 'ç', "-.-..",
+ 'é', "..-..",
+ 'è', "..-..",
+ 'ê', "-..-.",
+ 'ö', "---.",
+ 'ü', "..--",
+
+ '\0', ""
+};
+
+static struct morsetab koi8rtab[] = {
+ /*
+ * the cyrillic alphabet; you'll need a KOI8R font in order
+ * to see the actual characters
+ */
+ 'Á', ".-", /* a */
+ 'Â', "-...", /* be */
+ '×', ".--", /* ve */
+ 'Ç', "--.", /* ge */
+ 'Ä', "-..", /* de */
+ 'Å', ".", /* ye */
+ 'Ö', "...-", /* she */
+ 'Ú', "--..", /* ze */
+ 'É', "..", /* i */
+ 'Ê', ".---", /* i kratkoye */
+ 'Ë', "-.-", /* ka */
+ 'Ì', ".-..", /* el */
+ 'Í', "--", /* em */
+ 'Î', "-.", /* en */
+ 'Ï', "---", /* o */
+ 'Ð', ".--.", /* pe */
+ 'Ò', ".-.", /* er */
+ 'Ó', "...", /* es */
+ 'Ô', "-", /* te */
+ 'Õ', "..-", /* u */
+ 'Æ', "..-.", /* ef */
+ 'È', "....", /* kha */
+ 'Ã', "-.-.", /* ce */
+ 'Þ', "---.", /* che */
+ 'Û', "----", /* sha */
+ 'Ý', "--.-", /* shcha */
+ 'Ù', "-.--", /* yi */
+ 'Ø', "-..-", /* myakhkij znak */
+ 'Ü', "..-..", /* ae */
+ 'À', "..--", /* yu */
+ 'Ñ', ".-.-", /* ya */
+
+ '\0', ""
};
-static int sflag;
+void show(const char *), play(const char *), morse(char);
+
+static int pflag, sflag;
+static int wpm = 20; /* words per minute */
+#define FREQUENCY 600
+static int freq = FREQUENCY;
+
+#ifdef SPEAKER
+#define DASH_LEN 3
+#define CHAR_SPACE 3
+#define WORD_SPACE (7 - CHAR_SPACE - 1)
+static float dot_clock;
+int spkr;
+tone_t sound;
+#endif
-main(argc, argv)
- int argc;
- char **argv;
+static struct morsetab *hightab = iso8859tab;
+
+int
+main(int argc, char **argv)
{
- extern char *optarg;
- extern int optind;
- register int ch;
- register char *p;
+ extern char *optarg;
+ extern int optind;
+ register int ch;
+ register char *p;
- while ((ch = getopt(argc, argv, "s")) != EOF)
- switch((char)ch) {
+ while ((ch = getopt(argc, argv, "spw:f:")) != EOF)
+ switch ((char) ch) {
+ case 'f':
+ freq = atoi(optarg);
+ break;
+ case 'p':
+ pflag = 1;
+ break;
case 's':
sflag = 1;
break;
+ case 'w':
+ wpm = atoi(optarg);
+ break;
case '?':
default:
- fprintf(stderr, "usage: morse [string ...]");
+ fputs("usage: morse [-s] [-p] [-w speed] [-f frequency] [string ...]\n", stderr);
+ exit(1);
+ }
+ if (pflag && sflag) {
+ fputs("morse: only one of -p and -s allowed\n", stderr);
+ exit(1);
+ }
+ if (pflag && ((wpm < 1) || (wpm > 60))) {
+ fputs("morse: insane speed\n", stderr);
+ exit(1);
+ }
+ if (pflag && (freq == 0))
+ freq = FREQUENCY;
+
+ (void)setuid(getuid());
+#ifdef SPEAKER
+ if (pflag) {
+ if ((spkr = open(SPEAKER, O_WRONLY, 0)) == -1) {
+ perror(SPEAKER);
exit(1);
}
+ dot_clock = wpm / 2.4; /* dots/sec */
+ dot_clock = 1 / dot_clock; /* duration of a dot */
+ dot_clock = dot_clock / 2; /* dot_clock runs at twice */
+ /* the dot rate */
+ dot_clock = dot_clock * 100; /* scale for ioctl */
+ }
+#endif
argc -= optind;
argv += optind;
- if (*argv)
+ if((p = getenv("LC_CTYPE")) || (p = getenv("LANG"))) {
+ if(strlen(p) >= strlen("KOI8-R") &&
+ strcasecmp(&p[strlen(p) - strlen("KOI8-R")], "KOI8-R") == 0)
+ hightab = koi8rtab;
+ setlocale(LC_CTYPE, p);
+ } else {
+ setlocale(LC_CTYPE, "");
+ }
+
+ if (*argv) {
do {
- for (p = *argv; *p; ++p)
- morse((int)*p);
+ for (p = *argv; *p; ++p) {
+ morse((int) *p);
+ }
+ morse((int) ' ');
} while (*++argv);
- else while ((ch = getchar()) != EOF)
- morse(ch);
+ } else {
+ while ((ch = getchar()) != EOF)
+ morse(ch);
+ }
+ exit(0);
}
-morse(c)
- register int c;
+void
+morse(char c)
{
+ struct morsetab *m;
+
if (isalpha(c))
- show(alph[c - (isupper(c) ? 'A' : 'a')]);
- else if (isdigit(c))
- show(digit[c - '0']);
- else if (c == ',')
- show("--..--");
- else if (c == '.')
- show(".-.-.-");
- else if (isspace(c))
- show(" ...\n");
+ c = tolower(c);
+ if ((c == '\r') || (c == '\n'))
+ c = ' ';
+ if (c == ' ') {
+ if (pflag) {
+ play(" ");
+ return;
+ } else {
+ show("");
+ return;
+ }
+ }
+ for (m = ((unsigned char)c < 0x80? mtab: hightab);
+ m->inchar != '\0';
+ m++) {
+ if (m->inchar == c) {
+ if (pflag) {
+ play(m->morse);
+ } else
+ show(m->morse);
+ }
+ }
}
-show(s)
- register char *s;
+void
+show(const char *s)
{
if (sflag)
printf(" %s", s);
- else for (; *s; ++s)
- printf(" %s", *s == '.' ? "dit" : "daw");
- printf(",\n");
+ else
+ for (; *s; ++s)
+ printf(" %s", *s == '.' ? "dit" : "dah");
+ printf("\n");
+}
+
+void
+play(const char *s)
+{
+#ifdef SPEAKER
+ const char *c;
+
+ for (c = s; *c != '\0'; c++) {
+ switch ((int) *c) {
+ case '.':
+ sound.frequency = freq;
+ sound.duration = dot_clock;
+ break;
+ case '-':
+ sound.frequency = freq;
+ sound.duration = dot_clock * DASH_LEN;
+ break;
+ case ' ':
+ sound.frequency = 0;
+ sound.duration = dot_clock * WORD_SPACE;
+ break;
+ default:
+ sound.duration = 0;
+ }
+ if (sound.duration) {
+ if (ioctl(spkr, SPKRTONE, &sound) == -1) {
+ perror("ioctl play");
+ exit(1);
+ }
+ }
+ sound.frequency = 0;
+ sound.duration = dot_clock;
+ if (ioctl(spkr, SPKRTONE, &sound) == -1) {
+ perror("ioctl rest");
+ exit(1);
+ }
+ }
+ sound.frequency = 0;
+ sound.duration = dot_clock * CHAR_SPACE;
+ ioctl(spkr, SPKRTONE, &sound);
+#endif
}
OpenPOWER on IntegriCloud