commit ecfbbf7a705824c60d0ed4751c2789755116fe18 from: Sven M. Hallberg date: Thu Oct 26 12:48:20 2023 UTC integrate teacher mode in morse.c commit - 1ae907d2e841d807a0de44f703a26a5ec3f410c6 commit + ecfbbf7a705824c60d0ed4751c2789755116fe18 blob - 2ea3db963cfa9d513ccc8fc228060a286a43a41f blob + 82140b64ce703be970966fa7cd6af02bbfb857df --- Makefile +++ Makefile @@ -7,7 +7,7 @@ all: $(TGTS) clean: rm -f $(TGTS) $(OBJS) -morse: morse.o cw.o +morse: morse.o teach.o cw.o $(CC) -o $@ $(CFLAGS) $(LDFLAGS) $> $(LIBS) morseplay: morseplay.o cw.o $(CC) -o $@ $(CFLAGS) $(LDFLAGS) $> $(LIBS) blob - c8830ee06a1d800c6c076202a244f50d1f193601 blob + fe30dd2da8f1c457dae2cb8fc9392eca9c7988e4 --- README +++ README @@ -9,7 +9,7 @@ How to build: How to use: - $ ./morseteach + $ ./morse -t ... follow the on-screen instructions ... blob - a17e9f627e9042947ee425f134ee7d3a07e7f30a blob + 7be34dc5fec95edd82a8c8011e032459258be59d --- morse.c +++ morse.c @@ -131,10 +131,13 @@ void decode(char *); char *encode(int); void show(char *); void play(char *); +void teach(void); /* from teach.c */ static int sflag = 0; static int dflag = 0; static int aflag = 0; +static int tflag = 0; +int Dflag = 0; /* level of diagnostics (shared with teach.c) */ /* audio parameters */ static int speed = 24; /* base speed [wpm] */ @@ -148,11 +151,12 @@ main(int argc, char *argv[]) int ch; char *p; - /* pledge set needed for stdio and sndio */ - if (pledge("stdio rpath wpath cpath inet unix dns audio", NULL) == -1) + /* pledge set needed for stdio, sndio, and teacher */ + if (pledge("stdio rpath wpath cpath inet unix dns audio tty", NULL) + == -1) err(1, "pledge"); - while ((ch = getopt(argc, argv, "adsh")) != -1) + while ((ch = getopt(argc, argv, "adDsth")) != -1) switch(ch) { case 'a': aflag = 1; @@ -160,21 +164,28 @@ main(int argc, char *argv[]) case 'd': dflag = 1; break; + case 'D': + Dflag++; + break; case 's': sflag = 1; break; + case 't': + tflag = 1; + break; case 'h': default: fprintf(stderr, - "usage: %s [-a] [-d | -s] [string ...]\n", - getprogname()); + "usage: %s [-a] [-d | -s] [string ...]\n" + " %s -t\n", + getprogname(), getprogname()); return 1; } argc -= optind; argv += optind; /* audio initialization */ - if (aflag) { + if (aflag || tflag) { /* environment variables */ getenvnum("MORSE_SPEED", "wpm", &speed); pspeed = speed; /* default */ @@ -187,18 +198,23 @@ main(int argc, char *argv[]) cwinit(); - /* after sio_open() only "audio" remains needed */ - if (pledge("stdio audio", NULL) == -1) + /* after sio_open() sndio only needs "audio" */ + if (pledge("stdio audio tty", NULL) == -1) err(1, "pledge"); + /* also drop "tty" unless in teacher mode */ + if (!tflag) + if (pledge("stdio audio", NULL) == -1) + err(1, "pledge"); + cwstart(); } else { - /* no audio - drop access to all but stdio */ + /* no audio, no teacher - drop access to all but stdio */ if (pledge("stdio", NULL) == -1) err(1, "pledge"); } - setvbuf(stdout, NULL, _IOLBF, 0); /* always use line buffering */ + setvbuf(stdout, NULL, _IOLBF, 0); /* line buffering by default */ if (dflag) { if (*argv) { decode(*argv); @@ -209,6 +225,9 @@ main(int argc, char *argv[]) } else while ((ch = getchar()) != EOF) parse(ch); putchar('\n'); + } else if (tflag) { + teach(); + play("...-.-"); /* SK */ } else { if (*argv) do { @@ -218,9 +237,9 @@ main(int argc, char *argv[]) } while (*++argv); else while ((ch = getchar()) != EOF) morse(ch); - show("...-.-"); /* SK */ + show("...-.-"); /* SK */ } - if (aflag) + if (aflag || tflag) cwstop(); return 0; } @@ -378,3 +397,13 @@ play(char *s) silence(ditlen); } } + +/* the audio hook for teacher mode */ +void +sound(char c) +{ + char *s; + + if ((s = encode(c)) != NULL) + play(s); +}