My own rewrite of the BSD morse code recreational utility
Revisão | 27e89af5bab875e5d53b6057109c0dd7237e62ed (tree) |
---|---|
Hora | 2021-08-29 21:25:53 |
Autor | Joel Matthew Rees <joel.rees@gmai...> |
Commiter | Joel Matthew Rees |
first step not working right
@@ -122,11 +122,94 @@ const struct punc { | ||
122 | 122 | { '\0', NULL } |
123 | 123 | }; |
124 | 124 | |
125 | +char errorcode[] = "........"; /* JMR20210829 */ | |
126 | + | |
127 | + | |
128 | +/* JMR20210829 */ | |
129 | +const char * | |
130 | +tomorseif(c) | |
131 | + int c; | |
132 | +{ | |
133 | + int i; | |
134 | + | |
135 | + if (isalpha(c)) | |
136 | + return alph[c - (isupper(c)) ? 'A' : 'a']; | |
137 | + else if (isdigit(c)) | |
138 | + return digit[c - '0']; | |
139 | + else if (isspace(c)) | |
140 | + return " "; /* BT has a different purpose. */ | |
141 | + else { | |
142 | + for ( i = 0; other[i].c; ++i ) { | |
143 | + if (other[i].c == c) { | |
144 | + return other[i].morse ; | |
145 | + } | |
146 | + } | |
147 | + } | |
148 | + return errorcode; | |
149 | +} | |
150 | + | |
125 | 151 | |
126 | 152 | static int sflag; |
153 | + | |
154 | +/* JMR20210829 */ | |
155 | +static char vocbuf[ 8 * 4 + 1 ]; /* Enough to copy errorcode[] */ | |
156 | + | |
157 | +const char * | |
158 | +tovocalmorse( code ) | |
159 | + const char * code; | |
160 | +{ | |
161 | + if (sflag) | |
162 | + return code; | |
163 | + else { | |
164 | + char * insertion = vocbuf; | |
165 | + while ( *code ) { | |
166 | + const char * conv = (*code == '.') | |
167 | + ? "dit" | |
168 | + : (*code == '-') ? "daw" : " "; | |
169 | + strcpy(insertion, conv); | |
170 | + insertion += strlen(conv); | |
171 | + if ( *++code == '\0' ) | |
172 | + break; | |
173 | + * insertion++ = ' '; | |
174 | + } | |
175 | + * insertion++ = '\n'; | |
176 | + * insertion = '\0'; | |
177 | + return vocbuf; | |
178 | + } | |
179 | +} | |
180 | + | |
181 | + | |
127 | 182 | static int dflag; |
128 | 183 | |
129 | 184 | |
185 | +/* JMR20210829 */ | |
186 | +int | |
187 | +totextif(s) | |
188 | + const char *s; | |
189 | +{ | |
190 | + int i; | |
191 | + | |
192 | + for (i = 0; i < 10; i++) | |
193 | + if (strcmp(digit[i], s) == 0) { | |
194 | + return '0' + i; | |
195 | + } | |
196 | + | |
197 | + for (i = 0; i < 26; i++) | |
198 | + if (strcmp(alph[i], s) == 0) { | |
199 | + return 'A' + i; | |
200 | + } | |
201 | + i = 0; | |
202 | + while (other[i].c) { | |
203 | + if (strcmp(other[i].morse, s) == 0) { | |
204 | + return other[i].c; | |
205 | + } | |
206 | + i++; | |
207 | + } | |
208 | + if (strcmp("...-.-", s) == 0) /* Why SK but no KA? */ | |
209 | + return '\n'; | |
210 | + return 'x'; /* line noise */ | |
211 | +} | |
212 | + | |
130 | 213 | void |
131 | 214 | decode(s) |
132 | 215 | const char *s; |
@@ -226,8 +309,11 @@ main(argc, argv) | ||
226 | 309 | |
227 | 310 | if (dflag) { |
228 | 311 | if (*argv) { |
312 | + char outstr; | |
229 | 313 | do { |
230 | - decode(*argv); | |
314 | +/* decode(*argv); */ | |
315 | + outstr = totextif( argv ); | |
316 | + putchar( outstr ); | |
231 | 317 | } while (*++argv); |
232 | 318 | } else { |
233 | 319 | char foo[10]; /* All morse chars shorter than this */ |