morse.c (129299) | morse.c (147110) |
---|---|
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 --- 33 unchanged lines hidden (view full) --- 42 The Regents of the University of California. All rights reserved.\n"; 43#endif /* not lint */ 44 45#ifndef lint 46#if 0 47static char sccsid[] = "@(#)morse.c 8.1 (Berkeley) 5/31/93"; 48#endif 49static const char rcsid[] = | 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 --- 33 unchanged lines hidden (view full) --- 42 The Regents of the University of California. All rights reserved.\n"; 43#endif /* not lint */ 44 45#ifndef lint 46#if 0 47static char sccsid[] = "@(#)morse.c 8.1 (Berkeley) 5/31/93"; 48#endif 49static const char rcsid[] = |
50 "$FreeBSD: head/games/morse/morse.c 129299 2004-05-16 21:49:23Z ru $"; | 50 "$FreeBSD: head/games/morse/morse.c 147110 2005-06-07 19:01:41Z joerg $"; |
51#endif /* not lint */ 52 53#include <sys/time.h> 54 55#include <ctype.h> 56#include <fcntl.h> 57#include <langinfo.h> 58#include <locale.h> --- 202 unchanged lines hidden (view full) --- 261 262 {'\0', ""} 263}; 264 265void show(const char *), play(const char *), morse(char); 266void ttyout(const char *); 267void sighandler(int); 268 | 51#endif /* not lint */ 52 53#include <sys/time.h> 54 55#include <ctype.h> 56#include <fcntl.h> 57#include <langinfo.h> 58#include <locale.h> --- 202 unchanged lines hidden (view full) --- 261 262 {'\0', ""} 263}; 264 265void show(const char *), play(const char *), morse(char); 266void ttyout(const char *); 267void sighandler(int); 268 |
269#define GETOPTOPTS "d:ef:lsw:" | 269#define GETOPTOPTS "c:d:ef:lsw:" |
270#define USAGE \ | 270#define USAGE \ |
271"usage: morse [-els] [-d device] [-w speed] [-f frequency] [string ...]\n" | 271"usage: morse [-els] [-d device] [-w speed] [-c speed] [-f frequency] [string ...]\n" |
272 273static int pflag, lflag, sflag, eflag; | 272 273static int pflag, lflag, sflag, eflag; |
274static int wpm = 20; /* words per minute */ | 274static int wpm = 20; /* effective words per minute */ 275static int cpm; /* effective words per minute between 276 * characters */ |
275#define FREQUENCY 600 276static int freq = FREQUENCY; 277static char *device; /* for tty-controlled generator */ 278 279#define DASH_LEN 3 280#define CHAR_SPACE 3 281#define WORD_SPACE (7 - CHAR_SPACE - 1) 282static float dot_clock; | 277#define FREQUENCY 600 278static int freq = FREQUENCY; 279static char *device; /* for tty-controlled generator */ 280 281#define DASH_LEN 3 282#define CHAR_SPACE 3 283#define WORD_SPACE (7 - CHAR_SPACE - 1) 284static float dot_clock; |
285static float cdot_clock; |
|
283int spkr, line; 284struct termios otty, ntty; 285int olflags; 286 287#ifdef SPEAKER 288tone_t sound; 289#undef GETOPTOPTS | 286int spkr, line; 287struct termios otty, ntty; 288int olflags; 289 290#ifdef SPEAKER 291tone_t sound; 292#undef GETOPTOPTS |
290#define GETOPTOPTS "d:ef:lpsw:" | 293#define GETOPTOPTS "c:d:ef:lpsw:" |
291#undef USAGE 292#define USAGE \ | 294#undef USAGE 295#define USAGE \ |
293"usage: morse [-elps] [-d device] [-w speed] [-f frequency] [string ...]\n" | 296"usage: morse [-elps] [-d device] [-w speed] [-c speed] [-f frequency] [string ...]\n" |
294#endif 295 296static const struct morsetab *hightab; 297 298int 299main(int argc, char **argv) 300{ 301 int ch, lflags; 302 char *p, *codeset; 303 304 while ((ch = getopt(argc, argv, GETOPTOPTS)) != -1) 305 switch ((char) ch) { | 297#endif 298 299static const struct morsetab *hightab; 300 301int 302main(int argc, char **argv) 303{ 304 int ch, lflags; 305 char *p, *codeset; 306 307 while ((ch = getopt(argc, argv, GETOPTOPTS)) != -1) 308 switch ((char) ch) { |
309 case 'c': 310 cpm = atoi(optarg); 311 break; |
|
306 case 'd': 307 device = optarg; 308 break; 309 case 'e': 310 eflag = 1; 311 setvbuf(stdout, 0, _IONBF, 0); 312 break; 313 case 'f': --- 21 unchanged lines hidden (view full) --- 335 if (sflag && lflag) { 336 fputs("morse: only one of -l and -s allowed\n", stderr); 337 exit(1); 338 } 339 if ((pflag || device) && (sflag || lflag)) { 340 fputs("morse: only one of -p, -d and -l, -s allowed\n", stderr); 341 exit(1); 342 } | 312 case 'd': 313 device = optarg; 314 break; 315 case 'e': 316 eflag = 1; 317 setvbuf(stdout, 0, _IONBF, 0); 318 break; 319 case 'f': --- 21 unchanged lines hidden (view full) --- 341 if (sflag && lflag) { 342 fputs("morse: only one of -l and -s allowed\n", stderr); 343 exit(1); 344 } 345 if ((pflag || device) && (sflag || lflag)) { 346 fputs("morse: only one of -p, -d and -l, -s allowed\n", stderr); 347 exit(1); 348 } |
343 if ((pflag || device) && ((wpm < 1) || (wpm > 60))) { | 349 if (cpm == 0) 350 cpm = wpm; 351 if ((pflag || device) && ((wpm < 1) || (wpm > 60) || (cpm < 1) || (cpm > 60))) { |
344 fputs("morse: insane speed\n", stderr); 345 exit(1); 346 } 347 if ((pflag || device) && (freq == 0)) 348 freq = FREQUENCY; 349 350#ifdef SPEAKER 351 if (pflag) { --- 28 unchanged lines hidden (view full) --- 380 (void)signal(SIGTERM, sighandler); 381 } 382 if (pflag || device) { 383 dot_clock = wpm / 2.4; /* dots/sec */ 384 dot_clock = 1 / dot_clock; /* duration of a dot */ 385 dot_clock = dot_clock / 2; /* dot_clock runs at twice */ 386 /* the dot rate */ 387 dot_clock = dot_clock * 100; /* scale for ioctl */ | 352 fputs("morse: insane speed\n", stderr); 353 exit(1); 354 } 355 if ((pflag || device) && (freq == 0)) 356 freq = FREQUENCY; 357 358#ifdef SPEAKER 359 if (pflag) { --- 28 unchanged lines hidden (view full) --- 388 (void)signal(SIGTERM, sighandler); 389 } 390 if (pflag || device) { 391 dot_clock = wpm / 2.4; /* dots/sec */ 392 dot_clock = 1 / dot_clock; /* duration of a dot */ 393 dot_clock = dot_clock / 2; /* dot_clock runs at twice */ 394 /* the dot rate */ 395 dot_clock = dot_clock * 100; /* scale for ioctl */ |
396 397 cdot_clock = cpm / 2.4; /* dots/sec */ 398 cdot_clock = 1 / cdot_clock; /* duration of a dot */ 399 cdot_clock = cdot_clock / 2; /* dot_clock runs at twice */ 400 /* the dot rate */ 401 cdot_clock = cdot_clock * 100; /* scale for ioctl */ |
|
388 } 389 390 argc -= optind; 391 argv += optind; 392 393 if (setlocale(LC_CTYPE, "") != NULL && 394 *(codeset = nl_langinfo(CODESET)) != '\0') { 395 if (strcmp(codeset, "KOI8-R") == 0) --- 91 unchanged lines hidden (view full) --- 487 sound.duration = dot_clock; 488 break; 489 case '-': 490 sound.frequency = freq; 491 sound.duration = dot_clock * DASH_LEN; 492 break; 493 case ' ': 494 sound.frequency = 0; | 402 } 403 404 argc -= optind; 405 argv += optind; 406 407 if (setlocale(LC_CTYPE, "") != NULL && 408 *(codeset = nl_langinfo(CODESET)) != '\0') { 409 if (strcmp(codeset, "KOI8-R") == 0) --- 91 unchanged lines hidden (view full) --- 501 sound.duration = dot_clock; 502 break; 503 case '-': 504 sound.frequency = freq; 505 sound.duration = dot_clock * DASH_LEN; 506 break; 507 case ' ': 508 sound.frequency = 0; |
495 sound.duration = dot_clock * WORD_SPACE; | 509 sound.duration = cdot_clock * WORD_SPACE; |
496 break; 497 default: 498 sound.duration = 0; 499 } 500 if (sound.duration) { 501 if (ioctl(spkr, SPKRTONE, &sound) == -1) { 502 perror("ioctl play"); 503 exit(1); 504 } 505 } 506 sound.frequency = 0; 507 sound.duration = dot_clock; 508 if (ioctl(spkr, SPKRTONE, &sound) == -1) { 509 perror("ioctl rest"); 510 exit(1); 511 } 512 } 513 sound.frequency = 0; | 510 break; 511 default: 512 sound.duration = 0; 513 } 514 if (sound.duration) { 515 if (ioctl(spkr, SPKRTONE, &sound) == -1) { 516 perror("ioctl play"); 517 exit(1); 518 } 519 } 520 sound.frequency = 0; 521 sound.duration = dot_clock; 522 if (ioctl(spkr, SPKRTONE, &sound) == -1) { 523 perror("ioctl rest"); 524 exit(1); 525 } 526 } 527 sound.frequency = 0; |
514 sound.duration = dot_clock * CHAR_SPACE; | 528 sound.duration = cdot_clock * CHAR_SPACE; |
515 ioctl(spkr, SPKRTONE, &sound); 516#endif 517} 518 519void 520ttyout(const char *s) 521{ 522 const char *c; --- 6 unchanged lines hidden (view full) --- 529 duration = dot_clock; 530 break; 531 case '-': 532 on = 1; 533 duration = dot_clock * DASH_LEN; 534 break; 535 case ' ': 536 on = 0; | 529 ioctl(spkr, SPKRTONE, &sound); 530#endif 531} 532 533void 534ttyout(const char *s) 535{ 536 const char *c; --- 6 unchanged lines hidden (view full) --- 543 duration = dot_clock; 544 break; 545 case '-': 546 on = 1; 547 duration = dot_clock * DASH_LEN; 548 break; 549 case ' ': 550 on = 0; |
537 duration = dot_clock * WORD_SPACE; | 551 duration = cdot_clock * WORD_SPACE; |
538 break; 539 default: 540 on = 0; 541 duration = 0; 542 } 543 if (on) { 544 ioctl(line, TIOCMGET, &lflags); 545 lflags |= TIOCM_RTS; 546 ioctl(line, TIOCMSET, &lflags); 547 } 548 duration *= 10000; 549 if (duration) 550 usleep(duration); 551 ioctl(line, TIOCMGET, &lflags); 552 lflags &= ~TIOCM_RTS; 553 ioctl(line, TIOCMSET, &lflags); 554 duration = dot_clock * 10000; 555 usleep(duration); 556 } | 552 break; 553 default: 554 on = 0; 555 duration = 0; 556 } 557 if (on) { 558 ioctl(line, TIOCMGET, &lflags); 559 lflags |= TIOCM_RTS; 560 ioctl(line, TIOCMSET, &lflags); 561 } 562 duration *= 10000; 563 if (duration) 564 usleep(duration); 565 ioctl(line, TIOCMGET, &lflags); 566 lflags &= ~TIOCM_RTS; 567 ioctl(line, TIOCMSET, &lflags); 568 duration = dot_clock * 10000; 569 usleep(duration); 570 } |
557 duration = dot_clock * CHAR_SPACE * 10000; | 571 duration = cdot_clock * CHAR_SPACE * 10000; |
558 usleep(duration); 559} 560 561void 562sighandler(int signo) 563{ 564 565 ioctl(line, TIOCMSET, &olflags); 566 tcsetattr(line, TCSANOW, &otty); 567 568 signal(signo, SIG_DFL); 569 (void)kill(getpid(), signo); 570} | 572 usleep(duration); 573} 574 575void 576sighandler(int signo) 577{ 578 579 ioctl(line, TIOCMSET, &olflags); 580 tcsetattr(line, TCSANOW, &otty); 581 582 signal(signo, SIG_DFL); 583 (void)kill(getpid(), signo); 584} |