morse.c (2491) | morse.c (10352) |
---|---|
1/* 2 * Copyright (c) 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 17 unchanged lines hidden (view full) --- 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 | 1/* 2 * Copyright (c) 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 17 unchanged lines hidden (view full) --- 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 |
34/* 35 * Taught to send *real* morse by Lyndon Nerenberg (VE7TCP/VE6BBM) 36 * <lyndon@orthanc.com> 37 */ 38 |
|
34#ifndef lint 35static char copyright[] = 36"@(#) Copyright (c) 1988, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38#endif /* not lint */ 39 40#ifndef lint 41static char sccsid[] = "@(#)morse.c 8.1 (Berkeley) 5/31/93"; 42#endif /* not lint */ 43 44#include <stdio.h> 45#include <ctype.h> | 39#ifndef lint 40static char copyright[] = 41"@(#) Copyright (c) 1988, 1993\n\ 42 The Regents of the University of California. All rights reserved.\n"; 43#endif /* not lint */ 44 45#ifndef lint 46static char sccsid[] = "@(#)morse.c 8.1 (Berkeley) 5/31/93"; 47#endif /* not lint */ 48 49#include <stdio.h> 50#include <ctype.h> |
51#include <locale.h> 52#include <stdlib.h> |
|
46 | 53 |
47static char 48 *digit[] = { 49 "-----", 50 ".----", 51 "..---", 52 "...--", 53 "....-", 54 ".....", 55 "-....", 56 "--...", 57 "---..", 58 "----.", 59}, 60 *alph[] = { 61 ".-", 62 "-...", 63 "-.-.", 64 "-..", 65 ".", 66 "..-.", 67 "--.", 68 "....", 69 "..", 70 ".---", 71 "-.-", 72 ".-..", 73 "--", 74 "-.", 75 "---", 76 ".--.", 77 "--.-", 78 ".-.", 79 "...", 80 "-", 81 "..-", 82 "...-", 83 ".--", 84 "-..-", 85 "-.--", 86 "--..", | 54#ifdef SPEAKER 55#include <machine/speaker.h> 56#include <fcntl.h> 57#endif 58 59struct morsetab { 60 char inchar; 61 char *morse; |
87}; 88 | 62}; 63 |
89static int sflag; | 64static struct morsetab mtab[] = { |
90 | 65 |
91main(argc, argv) 92 int argc; 93 char **argv; | 66 /* letters */ 67 68 'a', ".-", 69 'b', "-...", 70 'c', "-.-.", 71 'd', "-..", 72 'e', ".", 73 'f', "..-.", 74 'g', "--.", 75 'h', "....", 76 'i', "..", 77 'j', ".---", 78 'k', "-.-", 79 'l', ".-..", 80 'm', "--", 81 'n', "-.", 82 'o', "---", 83 'p', ".--.", 84 'q', "--.-", 85 'r', ".-.", 86 's', "...", 87 't', "-", 88 'u', "..-", 89 'v', "...-", 90 'w', ".--", 91 'x', "-..-", 92 'y', "-.--", 93 'z', "--..", 94 95 /* digits */ 96 97 '0', "-----", 98 '1', ".----", 99 '2', "..---", 100 '3', "...--", 101 '4', "....-", 102 '5', ".....", 103 '6', "-....", 104 '7', "--...", 105 '8', "---..", 106 '9', "----.", 107 108 /* punctuation */ 109 110 ',', "--..--", 111 '.', ".-.-.-", 112 '?', "..--..", 113 '/', "-..-.", 114 '-', "-....-", 115 '=', "-...-", /* BT */ 116 ':', "---...", 117 ';', "-.-.-.", 118 '(', "-.--.", /* KN */ 119 ')', "-.--.-", 120 '$', "...-..-", 121 '+', ".-.-.", /* AR */ 122 123 /* prosigns without already assigned values */ 124 125 '#', ".-...", /* AS */ 126 '@', "...-.-", /* SK */ 127 '*', "...-.", /* VE */ 128 '%', "-...-.-", /* BK */ 129 130 '\0', "" 131}; 132 133 134static struct morsetab iso8859tab[] = { 135 '�', ".--.-", 136 '�', ".--.-", 137 '�', ".--.-", 138 '�', ".-.-", 139 '�', "-.-..", 140 '�', "..-..", 141 '�', "..-..", 142 '�', "-..-.", 143 '�', "---.", 144 '�', "..--", 145 146 '\0', "" 147}; 148 149static struct morsetab koi8rtab[] = { 150 /* 151 * the cyrillic alphabet; you'll need a KOI8R font in order 152 * to see the actual characters 153 */ 154 '�', ".-", /* a */ 155 '�', "-...", /* be */ 156 '�', ".--", /* ve */ 157 '�', "--.", /* ge */ 158 '�', "-..", /* de */ 159 '�', ".", /* ye */ 160 '�', "...-", /* she */ 161 '�', "--..", /* ze */ 162 '�', "..", /* i */ 163 '�', ".---", /* i kratkoye */ 164 '�', "-.-", /* ka */ 165 '�', ".-..", /* el */ 166 '�', "--", /* em */ 167 '�', "-.", /* en */ 168 '�', "---", /* o */ 169 '�', ".--.", /* pe */ 170 '�', ".-.", /* er */ 171 '�', "...", /* es */ 172 '�', "-", /* te */ 173 '�', "..-", /* u */ 174 '�', "..-.", /* ef */ 175 '�', "....", /* kha */ 176 '�', "-.-.", /* ce */ 177 '�', "---.", /* che */ 178 '�', "----", /* sha */ 179 '�', "--.-", /* shcha */ 180 '�', "-.--", /* yi */ 181 '�', "-..-", /* myakhkij znak */ 182 '�', "..-..", /* ae */ 183 '�', "..--", /* yu */ 184 '�', ".-.-", /* ya */ 185 186 '\0', "" 187}; 188 189void show(const char *), play(const char *), morse(char); 190 191static int pflag, sflag; 192static int wpm = 20; /* words per minute */ 193#define FREQUENCY 600 194static int freq = FREQUENCY; 195 196#ifdef SPEAKER 197#define DASH_LEN 3 198#define CHAR_SPACE 3 199#define WORD_SPACE (7 - CHAR_SPACE - 1) 200static float dot_clock; 201int spkr; 202tone_t sound; 203#endif 204 205static struct morsetab *hightab = iso8859tab; 206 207int 208main(int argc, char **argv) |
94{ | 209{ |
95 extern char *optarg; 96 extern int optind; 97 register int ch; 98 register char *p; | 210 extern char *optarg; 211 extern int optind; 212 register int ch; 213 register char *p; |
99 | 214 |
100 while ((ch = getopt(argc, argv, "s")) != EOF) 101 switch((char)ch) { | 215 while ((ch = getopt(argc, argv, "spw:f:")) != EOF) 216 switch ((char) ch) { 217 case 'f': 218 freq = atoi(optarg); 219 break; 220 case 'p': 221 pflag = 1; 222 break; |
102 case 's': 103 sflag = 1; 104 break; | 223 case 's': 224 sflag = 1; 225 break; |
226 case 'w': 227 wpm = atoi(optarg); 228 break; |
|
105 case '?': 106 default: | 229 case '?': 230 default: |
107 fprintf(stderr, "usage: morse [string ...]"); | 231 fputs("usage: morse [-s] [-p] [-w speed] [-f frequency] [string ...]\n", stderr); |
108 exit(1); 109 } | 232 exit(1); 233 } |
234 if (pflag && sflag) { 235 fputs("morse: only one of -p and -s allowed\n", stderr); 236 exit(1); 237 } 238 if (pflag && ((wpm < 1) || (wpm > 60))) { 239 fputs("morse: insane speed\n", stderr); 240 exit(1); 241 } 242 if (pflag && (freq == 0)) 243 freq = FREQUENCY; 244 245 (void)setuid(getuid()); 246#ifdef SPEAKER 247 if (pflag) { 248 if ((spkr = open(SPEAKER, O_WRONLY, 0)) == -1) { 249 perror(SPEAKER); 250 exit(1); 251 } 252 dot_clock = wpm / 2.4; /* dots/sec */ 253 dot_clock = 1 / dot_clock; /* duration of a dot */ 254 dot_clock = dot_clock / 2; /* dot_clock runs at twice */ 255 /* the dot rate */ 256 dot_clock = dot_clock * 100; /* scale for ioctl */ 257 } 258#endif |
|
110 argc -= optind; 111 argv += optind; 112 | 259 argc -= optind; 260 argv += optind; 261 |
113 if (*argv) | 262 if((p = getenv("LC_CTYPE")) || (p = getenv("LANG"))) { 263 if(strlen(p) >= strlen("KOI8-R") && 264 strcasecmp(&p[strlen(p) - strlen("KOI8-R")], "KOI8-R") == 0) 265 hightab = koi8rtab; 266 setlocale(LC_CTYPE, p); 267 } else { 268 setlocale(LC_CTYPE, ""); 269 } 270 271 if (*argv) { |
114 do { | 272 do { |
115 for (p = *argv; *p; ++p) 116 morse((int)*p); | 273 for (p = *argv; *p; ++p) { 274 morse((int) *p); 275 } 276 morse((int) ' '); |
117 } while (*++argv); | 277 } while (*++argv); |
118 else while ((ch = getchar()) != EOF) 119 morse(ch); | 278 } else { 279 while ((ch = getchar()) != EOF) 280 morse(ch); 281 } 282 exit(0); |
120} 121 | 283} 284 |
122morse(c) 123 register int c; | 285void 286morse(char c) |
124{ | 287{ |
288 struct morsetab *m; 289 |
|
125 if (isalpha(c)) | 290 if (isalpha(c)) |
126 show(alph[c - (isupper(c) ? 'A' : 'a')]); 127 else if (isdigit(c)) 128 show(digit[c - '0']); 129 else if (c == ',') 130 show("--..--"); 131 else if (c == '.') 132 show(".-.-.-"); 133 else if (isspace(c)) 134 show(" ...\n"); | 291 c = tolower(c); 292 if ((c == '\r') || (c == '\n')) 293 c = ' '; 294 if (c == ' ') { 295 if (pflag) { 296 play(" "); 297 return; 298 } else { 299 show(""); 300 return; 301 } 302 } 303 for (m = ((unsigned char)c < 0x80? mtab: hightab); 304 m->inchar != '\0'; 305 m++) { 306 if (m->inchar == c) { 307 if (pflag) { 308 play(m->morse); 309 } else 310 show(m->morse); 311 } 312 } |
135} 136 | 313} 314 |
137show(s) 138 register char *s; | 315void 316show(const char *s) |
139{ 140 if (sflag) 141 printf(" %s", s); | 317{ 318 if (sflag) 319 printf(" %s", s); |
142 else for (; *s; ++s) 143 printf(" %s", *s == '.' ? "dit" : "daw"); 144 printf(",\n"); | 320 else 321 for (; *s; ++s) 322 printf(" %s", *s == '.' ? "dit" : "dah"); 323 printf("\n"); |
145} | 324} |
325 326void 327play(const char *s) 328{ 329#ifdef SPEAKER 330 const char *c; 331 332 for (c = s; *c != '\0'; c++) { 333 switch ((int) *c) { 334 case '.': 335 sound.frequency = freq; 336 sound.duration = dot_clock; 337 break; 338 case '-': 339 sound.frequency = freq; 340 sound.duration = dot_clock * DASH_LEN; 341 break; 342 case ' ': 343 sound.frequency = 0; 344 sound.duration = dot_clock * WORD_SPACE; 345 break; 346 default: 347 sound.duration = 0; 348 } 349 if (sound.duration) { 350 if (ioctl(spkr, SPKRTONE, &sound) == -1) { 351 perror("ioctl play"); 352 exit(1); 353 } 354 } 355 sound.frequency = 0; 356 sound.duration = dot_clock; 357 if (ioctl(spkr, SPKRTONE, &sound) == -1) { 358 perror("ioctl rest"); 359 exit(1); 360 } 361 } 362 sound.frequency = 0; 363 sound.duration = dot_clock * CHAR_SPACE; 364 ioctl(spkr, SPKRTONE, &sound); 365#endif 366} |
|