Home | History | Annotate | Line # | Download | only in morse
morse.c revision 1.13.22.1
      1  1.13.22.1     matt /*	morse.c,v 1.13 2004/02/13 23:16:11 jsm Exp	*/
      2        1.3      cgd 
      3        1.1      cgd /*
      4        1.3      cgd  * Copyright (c) 1988, 1993
      5        1.3      cgd  *	The Regents of the University of California.  All rights reserved.
      6        1.1      cgd  *
      7        1.1      cgd  * Redistribution and use in source and binary forms, with or without
      8        1.1      cgd  * modification, are permitted provided that the following conditions
      9        1.1      cgd  * are met:
     10        1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     11        1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     12        1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13        1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     14        1.1      cgd  *    documentation and/or other materials provided with the distribution.
     15       1.11      agc  * 3. Neither the name of the University nor the names of its contributors
     16        1.1      cgd  *    may be used to endorse or promote products derived from this software
     17        1.1      cgd  *    without specific prior written permission.
     18        1.1      cgd  *
     19        1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20        1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21        1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22        1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23        1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24        1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25        1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26        1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27        1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28        1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29        1.1      cgd  * SUCH DAMAGE.
     30        1.1      cgd  */
     31        1.1      cgd 
     32        1.4    lukem #include <sys/cdefs.h>
     33        1.1      cgd #ifndef lint
     34        1.4    lukem __COPYRIGHT("@(#) Copyright (c) 1988, 1993\n\
     35        1.4    lukem 	The Regents of the University of California.  All rights reserved.\n");
     36        1.1      cgd #endif /* not lint */
     37        1.1      cgd 
     38        1.1      cgd #ifndef lint
     39        1.3      cgd #if 0
     40        1.3      cgd static char sccsid[] = "@(#)morse.c	8.1 (Berkeley) 5/31/93";
     41        1.3      cgd #else
     42  1.13.22.1     matt __RCSID("morse.c,v 1.13 2004/02/13 23:16:11 jsm Exp");
     43        1.3      cgd #endif
     44        1.1      cgd #endif /* not lint */
     45        1.1      cgd 
     46        1.4    lukem #include <ctype.h>
     47        1.1      cgd #include <stdio.h>
     48       1.10     matt #include <stdlib.h>
     49        1.6  hubertf #include <string.h>
     50        1.4    lukem #include <unistd.h>
     51        1.1      cgd 
     52        1.7      jsm static const char
     53        1.7      jsm 	*const digit[] = {
     54        1.1      cgd 	"-----",
     55        1.1      cgd 	".----",
     56        1.1      cgd 	"..---",
     57        1.1      cgd 	"...--",
     58        1.1      cgd 	"....-",
     59        1.1      cgd 	".....",
     60        1.1      cgd 	"-....",
     61        1.1      cgd 	"--...",
     62        1.1      cgd 	"---..",
     63        1.1      cgd 	"----.",
     64        1.1      cgd },
     65        1.7      jsm 	*const alph[] = {
     66        1.1      cgd 	".-",
     67        1.1      cgd 	"-...",
     68        1.1      cgd 	"-.-.",
     69        1.1      cgd 	"-..",
     70        1.1      cgd 	".",
     71        1.1      cgd 	"..-.",
     72        1.1      cgd 	"--.",
     73        1.1      cgd 	"....",
     74        1.1      cgd 	"..",
     75        1.1      cgd 	".---",
     76        1.1      cgd 	"-.-",
     77        1.1      cgd 	".-..",
     78        1.1      cgd 	"--",
     79        1.1      cgd 	"-.",
     80        1.1      cgd 	"---",
     81        1.1      cgd 	".--.",
     82        1.1      cgd 	"--.-",
     83        1.1      cgd 	".-.",
     84        1.1      cgd 	"...",
     85        1.1      cgd 	"-",
     86        1.1      cgd 	"..-",
     87        1.1      cgd 	"...-",
     88        1.1      cgd 	".--",
     89        1.1      cgd 	"-..-",
     90        1.1      cgd 	"-.--",
     91        1.1      cgd 	"--..",
     92        1.1      cgd };
     93        1.1      cgd 
     94       1.13      jsm const struct punc {
     95       1.13      jsm 	char c;
     96       1.13      jsm 	const char *morse;
     97       1.13      jsm } other[] = {
     98       1.13      jsm 	{ '.', ".-.-.-" },
     99       1.13      jsm 	{ ',', "--..--" },
    100       1.13      jsm 	{ ':', "---..." },
    101       1.13      jsm 	{ '?', "..--.." },
    102       1.13      jsm 	{ '\'', ".----." },
    103       1.13      jsm 	{ '-', "-....-" },
    104       1.13      jsm 	{ '/', "-..-." },
    105       1.13      jsm 	{ '(', "-.--." },
    106       1.13      jsm 	{ ')', "-.--.-" },
    107       1.13      jsm 	{ '"', ".-..-." },
    108       1.13      jsm 	{ '=', "-...-" },
    109       1.13      jsm 	{ '+', ".-.-." },
    110       1.13      jsm 	{ '\0', NULL }
    111       1.13      jsm };
    112       1.13      jsm 
    113       1.12      jsm int	main(int, char *[]);
    114       1.12      jsm void	morse(int);
    115       1.12      jsm void	decode(const char *);
    116       1.12      jsm void	show(const char *);
    117        1.4    lukem 
    118        1.1      cgd static int sflag;
    119        1.6  hubertf static int dflag;
    120        1.1      cgd 
    121        1.4    lukem int
    122        1.1      cgd main(argc, argv)
    123        1.1      cgd 	int argc;
    124        1.1      cgd 	char **argv;
    125        1.1      cgd {
    126        1.4    lukem 	int ch;
    127       1.13      jsm 	char *p;
    128        1.8      jsm 
    129        1.8      jsm 	/* Revoke setgid privileges */
    130        1.9  mycroft 	setgid(getgid());
    131        1.1      cgd 
    132        1.6  hubertf 	while ((ch = getopt(argc, argv, "ds")) != -1)
    133        1.1      cgd 		switch((char)ch) {
    134        1.6  hubertf 		case 'd':
    135        1.6  hubertf 			dflag = 1;
    136        1.6  hubertf 			break;
    137        1.1      cgd 		case 's':
    138        1.1      cgd 			sflag = 1;
    139        1.1      cgd 			break;
    140        1.1      cgd 		case '?':
    141        1.1      cgd 		default:
    142        1.6  hubertf 			fprintf(stderr, "usage: morse [-ds] [string ...]\n");
    143        1.1      cgd 			exit(1);
    144        1.1      cgd 		}
    145        1.1      cgd 	argc -= optind;
    146        1.1      cgd 	argv += optind;
    147        1.1      cgd 
    148        1.6  hubertf 	if (dflag) {
    149        1.6  hubertf 		if (*argv) {
    150        1.6  hubertf 			do {
    151        1.6  hubertf 				decode(*argv);
    152        1.6  hubertf 			} while (*++argv);
    153       1.13      jsm 		} else {
    154       1.13      jsm 			char foo[10];	/* All morse chars shorter than this */
    155  1.13.22.1     matt 			int is_blank, i;
    156       1.13      jsm 
    157       1.13      jsm 			i = 0;
    158  1.13.22.1     matt 			is_blank = 0;
    159       1.13      jsm 			while ((ch = getchar()) != EOF) {
    160       1.13      jsm 				if (ch == '-' || ch == '.') {
    161       1.13      jsm 					foo[i++] = ch;
    162       1.13      jsm 					if (i == 10) {
    163       1.13      jsm 						/* overrun means gibberish--print 'x' and
    164       1.13      jsm 						 * advance */
    165       1.13      jsm 						i = 0;
    166       1.13      jsm 						putchar('x');
    167       1.13      jsm 						while ((ch = getchar()) != EOF &&
    168       1.13      jsm 						    (ch == '.' || ch == '-'))
    169       1.13      jsm 							;
    170  1.13.22.1     matt 						is_blank = 1;
    171        1.6  hubertf 					}
    172       1.13      jsm 				} else if (i) {
    173       1.13      jsm 					foo[i] = '\0';
    174       1.13      jsm 					decode(foo);
    175       1.13      jsm 					i = 0;
    176  1.13.22.1     matt 					is_blank = 0;
    177       1.13      jsm 				} else if (isspace(ch)) {
    178  1.13.22.1     matt 					if (is_blank) {
    179       1.13      jsm 						/* print whitespace for each double blank */
    180       1.13      jsm 						putchar(' ');
    181  1.13.22.1     matt 						is_blank = 0;
    182       1.13      jsm 					} else
    183  1.13.22.1     matt 						is_blank = 1;
    184        1.6  hubertf 				}
    185        1.6  hubertf 			}
    186        1.6  hubertf 		}
    187        1.6  hubertf 		putchar('\n');
    188       1.13      jsm 	} else {
    189        1.6  hubertf 		if (*argv)
    190        1.6  hubertf 			do {
    191        1.6  hubertf 				for (p = *argv; *p; ++p)
    192        1.6  hubertf 					morse((int)*p);
    193       1.13      jsm 				show("");
    194        1.6  hubertf 			} while (*++argv);
    195        1.6  hubertf 		else while ((ch = getchar()) != EOF)
    196        1.6  hubertf 			morse(ch);
    197       1.13      jsm 		show("...-.-");	/* SK */
    198        1.6  hubertf 	}
    199        1.6  hubertf 
    200        1.6  hubertf 	return 0;
    201        1.6  hubertf }
    202        1.6  hubertf 
    203        1.6  hubertf void
    204        1.6  hubertf decode(s)
    205        1.6  hubertf 	const char *s;
    206        1.6  hubertf {
    207       1.13      jsm 	int i;
    208       1.13      jsm 
    209       1.13      jsm 	for (i = 0; i < 10; i++)
    210       1.13      jsm 		if (strcmp(digit[i], s) == 0) {
    211       1.13      jsm 			putchar('0' + i);
    212       1.13      jsm 			return;
    213        1.6  hubertf 		}
    214       1.13      jsm 
    215       1.13      jsm 	for (i = 0; i < 26; i++)
    216       1.13      jsm 		if (strcmp(alph[i], s) == 0) {
    217       1.13      jsm 			putchar('A' + i);
    218        1.6  hubertf 			return;
    219        1.6  hubertf 		}
    220       1.13      jsm 	i = 0;
    221       1.13      jsm 	while (other[i].c) {
    222       1.13      jsm 		if (strcmp(other[i].morse, s) == 0) {
    223       1.13      jsm 			putchar(other[i].c);
    224       1.13      jsm 			return;
    225        1.6  hubertf 		}
    226       1.13      jsm 		i++;
    227        1.6  hubertf 	}
    228       1.13      jsm 	if (strcmp("...-.-", s) == 0)
    229       1.13      jsm 		return;
    230       1.13      jsm 	putchar('x');	/* line noise */
    231        1.1      cgd }
    232        1.1      cgd 
    233        1.4    lukem void
    234        1.1      cgd morse(c)
    235        1.4    lukem 	int c;
    236        1.1      cgd {
    237       1.13      jsm 	int i;
    238       1.13      jsm 
    239        1.1      cgd 	if (isalpha(c))
    240        1.1      cgd 		show(alph[c - (isupper(c) ? 'A' : 'a')]);
    241        1.1      cgd 	else if (isdigit(c))
    242        1.1      cgd 		show(digit[c - '0']);
    243        1.1      cgd 	else if (isspace(c))
    244       1.13      jsm 		show("");  /* could show BT for a pause */
    245       1.13      jsm 	else {
    246       1.13      jsm 		i = 0;
    247       1.13      jsm 		while (other[i].c) {
    248       1.13      jsm 			if (other[i].c == c) {
    249       1.13      jsm 				show(other[i].morse);
    250       1.13      jsm 				break;
    251       1.13      jsm 			}
    252       1.13      jsm 			i++;
    253       1.13      jsm 		}
    254       1.13      jsm 	}
    255        1.1      cgd }
    256        1.1      cgd 
    257        1.4    lukem void
    258        1.1      cgd show(s)
    259        1.7      jsm 	const char *s;
    260        1.1      cgd {
    261        1.1      cgd 	if (sflag)
    262        1.1      cgd 		printf(" %s", s);
    263        1.1      cgd 	else for (; *s; ++s)
    264        1.1      cgd 		printf(" %s", *s == '.' ? "dit" : "daw");
    265       1.13      jsm 	printf("\n");
    266        1.1      cgd }
    267