xref: /original-bsd/games/morse/morse.c (revision e9b82df0)
1 /*
2  * Copyright (c) 1988 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 char copyright[] =
10 "@(#) Copyright (c) 1988 Regents of the University of California.\n\
11  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)morse.c	5.2 (Berkeley) 06/01/90";
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 static
97 morse(c)
98 	register int c;
99 {
100 	if (isalpha(c))
101 		show(alph[c - (isupper(c) ? 'A' : 'a')]);
102 	else if (isdigit(c))
103 		show(digit[c - '0']);
104 	else if (c == ',')
105 		show("--..--");
106 	else if (c == '.')
107 		show(".-.-.-");
108 	else if (isspace(c))
109 		show(" ...\n");
110 }
111 
112 static
113 show(s)
114 	register char *s;
115 {
116 	if (sflag)
117 		printf(" %s", s);
118 	else for (; *s; ++s)
119 		printf(" %s", *s == '.' ? "dit" : "daw");
120 	printf(",\n");
121 }
122