home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.ee.lbl.gov
/
2014.05.ftp.ee.lbl.gov.tar
/
ftp.ee.lbl.gov
/
sst.tar.Z
/
sst.tar
/
sst
/
morse.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-01-10
|
5KB
|
308 lines
/* morse.c - turn ASCII text into morse code
**
** Copyright (C) 1989 by Jef Poskanzer.
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation. This software is provided"as is" without express or
** implied warranty.
*/
#include <stdio.h>
#include <fcntl.h>
#include <sys/signal.h>
#include"libsst.h"
#define max(a,b) ((a) > (b) ? (a) : (b))
#define MORSE_HZ 440
#define DEFAULT_WPM 10.0
int sst_fd;
int dot_usec, dash_usec, intertone_usec, interchar_usec, interword_usec;
main( argc, argv )
int argc;
char *argv[];
{
int argn, ic;
float wpm;
double atof();
int sighandler();
char *usage = "usage: %s [-wpm <n>] [file]\n";
argn = 1;
wpm = DEFAULT_WPM;
if ( argn < argc && argv[argn][0] == '-' )
{
if ( strncmp( argv[argn], "-wpm", max(strlen( argv[argn] ),2) ) == 0 )
{
++argn;
wpm = atof( argv[argn] );
}
else
{
fprintf( stderr, usage, argv[0] );
exit( 1 );
}
++argn;
}
if ( argn < argc )
{
if ( freopen( argv[argn], "r", stdin ) == NULL )
{
perror( argv[argn] );
exit( 1 );
}
++argn;
}
if ( argn != argc )
{
fprintf( stderr, usage, argv[0] );
exit( 1 );
}
dot_usec = 200000 * DEFAULT_WPM / wpm;
dash_usec = 500000 * DEFAULT_WPM / wpm;
intertone_usec = 200000 * DEFAULT_WPM / wpm;
interchar_usec = 500000 * DEFAULT_WPM / wpm;
interword_usec = 1000000 * DEFAULT_WPM / wpm;
sst_fd = sst_open( );
(void) signal( SIGHUP, sighandler );
(void) signal( SIGINT, sighandler );
while ( ( ic = getchar( ) ) != EOF )
{
switch ( ic )
{
case 'a':
case 'A':
doit( ".-" );
break;
case 'b':
case 'B':
doit( "-..." );
break;
case 'c':
case 'C':
doit( "-.-." );
break;
case 'd':
case 'D':
doit( "-.." );
break;
case 'e':
case 'E':
doit( "." );
break;
case 'f':
case 'F':
doit( "..-." );
break;
case 'g':
case 'G':
doit( "--." );
break;
case 'h':
case 'H':
doit( "...." );
break;
case 'i':
case 'I':
doit( ".." );
break;
case 'j':
case 'J':
doit( ".---" );
break;
case 'k':
case 'K':
doit( "-.-" );
break;
case 'l':
case 'L':
doit( ".-.." );
break;
case 'm':
case 'M':
doit( "--" );
break;
case 'n':
case 'N':
doit( "-." );
break;
case 'o':
case 'O':
doit( "---" );
break;
case 'p':
case 'P':
doit( ".--." );
break;
case 'q':
case 'Q':
doit( "--.-" );
break;
case 'r':
case 'R':
doit( ".-." );
break;
case 's':
case 'S':
doit( "..." );
break;
case 't':
case 'T':
doit( "-" );
break;
case 'u':
case 'U':
doit( ".--" );
break;
case 'v':
case 'V':
doit( "...-" );
break;
case 'w':
case 'W':
doit( ".--" );
break;
case 'x':
case 'X':
doit( "-..-" );
break;
case 'y':
case 'Y':
doit( "-.--" );
break;
case 'z':
case 'Z':
doit( "--.." );
break;
case '1':
doit( ".----" );
break;
case '2':
doit( "..---" );
break;
case '3':
doit( "...--" );
break;
case '4':
doit( "....-" );
break;
case '5':
doit( "....." );
break;
case '6':
doit( "-...." );
break;
case '7':
doit( "--..." );
break;
case '8':
doit( "---.." );
break;
case '9':
doit( "----." );
break;
case '0':
doit( "-----" );
break;
case '.':
doit( ".-.-.-" );
break;
case ',':
doit( "--..--" );
break;
case '?':
doit( "..--.." );
break;
case ':':
doit( "---..." );
break;
case ';':
doit( "-.-.-." );
break;
case ' ':
case '\t':
case '\n':
case '\r':
usleep( interword_usec );
break;
}
}
sst_close( sst_fd );
exit( 0 );
}
doit( s )
char *s;
{
for ( ; *s != '\0'; ++s )
{
if ( *s == '.' )
sst_tones( sst_fd, MORSE_HZ, 0, 0, 0, dot_usec );
else if ( *s == '-' )
sst_tones( sst_fd, MORSE_HZ, 0, 0, 0, dash_usec );
else
fprintf( stderr, "Say what?!?\n" );
usleep( intertone_usec );
}
usleep( interchar_usec );
}
int
sighandler( )
{
sst_close( sst_fd );
exit( 1 );
}