xref: /original-bsd/games/morse/morse.c (revision c02b9ad6)
1 /*
2  * Copyright (c) 1988 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 char copyright[] =
20 "@(#) Copyright (c) 1988 Regents of the University of California.\n\
21  All rights reserved.\n";
22 #endif /* not lint */
23 
24 #ifndef lint
25 static char sccsid[] = "@(#)morse.c	5.1 (Berkeley) 08/12/88";
26 #endif /* not lint */
27 
28 #include <stdio.h>
29 #include <ctype.h>
30 
31 static char
32 	*digit[] = {
33 	"-----",
34 	".----",
35 	"..---",
36 	"...--",
37 	"....-",
38 	".....",
39 	"-....",
40 	"--...",
41 	"---..",
42 	"----.",
43 },
44 	*alph[] = {
45 	".-",
46 	"-...",
47 	"-.-.",
48 	"-..",
49 	".",
50 	"..-.",
51 	"--.",
52 	"....",
53 	"..",
54 	".---",
55 	"-.-",
56 	".-..",
57 	"--",
58 	"-.",
59 	"---",
60 	".--.",
61 	"--.-",
62 	".-.",
63 	"...",
64 	"-",
65 	"..-",
66 	"...-",
67 	".--",
68 	"-..-",
69 	"-.--",
70 	"--..",
71 };
72 
73 static int sflag;
74 
75 main(argc, argv)
76 	int argc;
77 	char **argv;
78 {
79 	extern char *optarg;
80 	extern int optind;
81 	register int ch;
82 	register char *p;
83 
84 	while ((ch = getopt(argc, argv, "s")) != EOF)
85 		switch((char)ch) {
86 		case 's':
87 			sflag = 1;
88 			break;
89 		case '?':
90 		default:
91 			fprintf(stderr, "usage: morse [string ...]");
92 			exit(1);
93 		}
94 	argc -= optind;
95 	argv += optind;
96 
97 	if (*argv)
98 		do {
99 			for (p = *argv; *p; ++p)
100 				morse((int)*p);
101 		} while (*++argv);
102 	else while ((ch = getchar()) != EOF)
103 		morse(ch);
104 }
105 
106 static
107 morse(c)
108 	register int c;
109 {
110 	if (isalpha(c))
111 		show(alph[c - (isupper(c) ? 'A' : 'a')]);
112 	else if (isdigit(c))
113 		show(digit[c - '0']);
114 	else if (c == ',')
115 		show("--..--");
116 	else if (c == '.')
117 		show(".-.-.-");
118 	else if (isspace(c))
119 		show(" ...\n");
120 }
121 
122 static
123 show(s)
124 	register char *s;
125 {
126 	if (sflag)
127 		printf(" %s", s);
128 	else for (; *s; ++s)
129 		printf(" %s", *s == '.' ? "dit" : "daw");
130 	printf(",\n");
131 }
132