xref: /original-bsd/games/morse/morse.c (revision 38ca7aa6)
1 /*
2  * Copyright (c) 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1988, 1993\n\
11 	The Regents of the University of California.  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)morse.c	8.1 (Berkeley) 05/31/93";
16 #endif /* not lint */
17 
18 #include <stdio.h>
19 #include <ctype.h>
20 
21 static char
22 	*digit[] = {
23 	"-----",
24 	".----",
25 	"..---",
26 	"...--",
27 	"....-",
28 	".....",
29 	"-....",
30 	"--...",
31 	"---..",
32 	"----.",
33 },
34 	*alph[] = {
35 	".-",
36 	"-...",
37 	"-.-.",
38 	"-..",
39 	".",
40 	"..-.",
41 	"--.",
42 	"....",
43 	"..",
44 	".---",
45 	"-.-",
46 	".-..",
47 	"--",
48 	"-.",
49 	"---",
50 	".--.",
51 	"--.-",
52 	".-.",
53 	"...",
54 	"-",
55 	"..-",
56 	"...-",
57 	".--",
58 	"-..-",
59 	"-.--",
60 	"--..",
61 };
62 
63 static int sflag;
64 
65 main(argc, argv)
66 	int argc;
67 	char **argv;
68 {
69 	extern char *optarg;
70 	extern int optind;
71 	register int ch;
72 	register char *p;
73 
74 	while ((ch = getopt(argc, argv, "s")) != EOF)
75 		switch((char)ch) {
76 		case 's':
77 			sflag = 1;
78 			break;
79 		case '?':
80 		default:
81 			fprintf(stderr, "usage: morse [string ...]");
82 			exit(1);
83 		}
84 	argc -= optind;
85 	argv += optind;
86 
87 	if (*argv)
88 		do {
89 			for (p = *argv; *p; ++p)
90 				morse((int)*p);
91 		} while (*++argv);
92 	else while ((ch = getchar()) != EOF)
93 		morse(ch);
94 }
95 
96 morse(c)
97 	register int c;
98 {
99 	if (isalpha(c))
100 		show(alph[c - (isupper(c) ? 'A' : 'a')]);
101 	else if (isdigit(c))
102 		show(digit[c - '0']);
103 	else if (c == ',')
104 		show("--..--");
105 	else if (c == '.')
106 		show(".-.-.-");
107 	else if (isspace(c))
108 		show(" ...\n");
109 }
110 
111 show(s)
112 	register char *s;
113 {
114 	if (sflag)
115 		printf(" %s", s);
116 	else for (; *s; ++s)
117 		printf(" %s", *s == '.' ? "dit" : "daw");
118 	printf(",\n");
119 }
120