Home | History | Annotate | Line # | Download | only in morse
morse.c revision 1.11
      1  1.11      agc /*	$NetBSD: morse.c,v 1.11 2003/08/07 09:37:30 agc 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.11      agc __RCSID("$NetBSD: morse.c,v 1.11 2003/08/07 09:37:30 agc 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.6  hubertf #define MORSE_COLON	"--..--"
     53   1.6  hubertf #define MORSE_PERIOD	".-.-.-"
     54   1.6  hubertf 
     55   1.6  hubertf 
     56   1.7      jsm static const char
     57   1.7      jsm 	*const digit[] = {
     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.1      cgd 	"--...",
     66   1.1      cgd 	"---..",
     67   1.1      cgd 	"----.",
     68   1.1      cgd },
     69   1.7      jsm 	*const alph[] = {
     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.1      cgd 	"-.--",
     95   1.1      cgd 	"--..",
     96   1.1      cgd };
     97   1.1      cgd 
     98   1.4    lukem int	main __P((int, char *[]));
     99   1.4    lukem void	morse __P((int));
    100   1.6  hubertf void	decode __P((const char *));
    101   1.7      jsm void	show __P((const char *));
    102   1.4    lukem 
    103   1.1      cgd static int sflag;
    104   1.6  hubertf static int dflag;
    105   1.1      cgd 
    106   1.4    lukem int
    107   1.1      cgd main(argc, argv)
    108   1.1      cgd 	int argc;
    109   1.1      cgd 	char **argv;
    110   1.1      cgd {
    111   1.4    lukem 	int ch;
    112   1.6  hubertf 	char *s, *p;
    113   1.8      jsm 
    114   1.8      jsm 	/* Revoke setgid privileges */
    115   1.9  mycroft 	setgid(getgid());
    116   1.1      cgd 
    117   1.6  hubertf 	while ((ch = getopt(argc, argv, "ds")) != -1)
    118   1.1      cgd 		switch((char)ch) {
    119   1.6  hubertf 		case 'd':
    120   1.6  hubertf 			dflag = 1;
    121   1.6  hubertf 			break;
    122   1.1      cgd 		case 's':
    123   1.1      cgd 			sflag = 1;
    124   1.1      cgd 			break;
    125   1.1      cgd 		case '?':
    126   1.1      cgd 		default:
    127   1.6  hubertf 			fprintf(stderr, "usage: morse [-ds] [string ...]\n");
    128   1.1      cgd 			exit(1);
    129   1.1      cgd 		}
    130   1.1      cgd 	argc -= optind;
    131   1.1      cgd 	argv += optind;
    132   1.1      cgd 
    133   1.6  hubertf 	if (dflag) {
    134   1.6  hubertf 		if (*argv) {
    135   1.6  hubertf 			do {
    136   1.6  hubertf 				s=strchr(*argv, ',');
    137   1.6  hubertf 
    138   1.6  hubertf 				if (s)
    139   1.6  hubertf 					*s='\0';
    140   1.6  hubertf 
    141   1.6  hubertf 				decode(*argv);
    142   1.6  hubertf 			} while (*++argv);
    143   1.6  hubertf 		}else{
    144   1.6  hubertf 			char buf[1024];
    145   1.6  hubertf 
    146   1.6  hubertf 			while (fgets(buf, 1024, stdin)) {
    147   1.6  hubertf 				s=buf;
    148   1.6  hubertf 
    149   1.6  hubertf 				while (*s && isspace(*s))
    150   1.6  hubertf 					s++;
    151   1.6  hubertf 
    152   1.6  hubertf 				if (*s) {
    153   1.6  hubertf 					p=strtok(s, " \n\t");
    154   1.6  hubertf 
    155   1.6  hubertf 					while (p) {
    156   1.6  hubertf 						s=strchr(p, ',');
    157   1.6  hubertf 
    158   1.6  hubertf 						if (s)
    159   1.6  hubertf 							*s='\0';
    160   1.6  hubertf 
    161   1.6  hubertf 						decode(p);
    162   1.6  hubertf 						p=strtok(NULL, " \n\t");
    163   1.6  hubertf 					}
    164   1.6  hubertf 				}
    165   1.6  hubertf 			}
    166   1.6  hubertf 		}
    167   1.6  hubertf 		putchar('\n');
    168   1.6  hubertf 	}else{
    169   1.6  hubertf 		if (*argv)
    170   1.6  hubertf 			do {
    171   1.6  hubertf 				for (p = *argv; *p; ++p)
    172   1.6  hubertf 					morse((int)*p);
    173   1.6  hubertf 			} while (*++argv);
    174   1.6  hubertf 		else while ((ch = getchar()) != EOF)
    175   1.6  hubertf 			morse(ch);
    176   1.6  hubertf 	}
    177   1.6  hubertf 
    178   1.6  hubertf 	return 0;
    179   1.6  hubertf }
    180   1.6  hubertf 
    181   1.6  hubertf void
    182   1.6  hubertf decode(s)
    183   1.6  hubertf 	const char *s;
    184   1.6  hubertf {
    185   1.6  hubertf 	if (strcmp(s, MORSE_COLON) == 0){
    186   1.6  hubertf 		putchar(',');
    187   1.6  hubertf 	} else if (strcmp(s, MORSE_PERIOD) == 0){
    188   1.6  hubertf 		putchar('.');
    189   1.6  hubertf 	} else {
    190   1.6  hubertf 		int found;
    191   1.7      jsm 		const char *const *a;
    192   1.6  hubertf 		int size;
    193   1.6  hubertf 		int i;
    194   1.6  hubertf 
    195   1.6  hubertf 		found=0;
    196   1.6  hubertf 		a=digit;
    197   1.6  hubertf 		size=sizeof(digit)/sizeof(digit[0]);
    198   1.6  hubertf 		for (i=0; i<size; i++) {
    199   1.6  hubertf 			if (strcmp(a[i], s) == 0) {
    200   1.6  hubertf 				found = 1;
    201   1.6  hubertf 				break;
    202   1.6  hubertf 			}
    203   1.6  hubertf 		}
    204   1.6  hubertf 
    205   1.6  hubertf 		if (found) {
    206   1.6  hubertf 			putchar('0'+i);
    207   1.6  hubertf 			return;
    208   1.6  hubertf 		}
    209   1.6  hubertf 
    210   1.6  hubertf 		found=0;
    211   1.6  hubertf 		a=alph;
    212   1.6  hubertf 		size=sizeof(alph)/sizeof(alph[0]);
    213   1.6  hubertf 		for (i=0; i<size; i++) {
    214   1.6  hubertf 			if (strcmp(a[i], s) == 0) {
    215   1.6  hubertf 				found = 1;
    216   1.6  hubertf 				break;
    217   1.6  hubertf 			}
    218   1.6  hubertf 		}
    219   1.6  hubertf 
    220   1.6  hubertf 		if (found)
    221   1.6  hubertf 			putchar('a'+i);
    222   1.6  hubertf 		else
    223   1.6  hubertf 			putchar(' ');
    224   1.6  hubertf 	}
    225   1.1      cgd }
    226   1.1      cgd 
    227   1.4    lukem void
    228   1.1      cgd morse(c)
    229   1.4    lukem 	int c;
    230   1.1      cgd {
    231   1.1      cgd 	if (isalpha(c))
    232   1.1      cgd 		show(alph[c - (isupper(c) ? 'A' : 'a')]);
    233   1.1      cgd 	else if (isdigit(c))
    234   1.1      cgd 		show(digit[c - '0']);
    235   1.1      cgd 	else if (c == ',')
    236   1.6  hubertf 		show(MORSE_COLON);
    237   1.1      cgd 	else if (c == '.')
    238   1.6  hubertf 		show(MORSE_PERIOD);
    239   1.1      cgd 	else if (isspace(c))
    240   1.1      cgd 		show(" ...\n");
    241   1.1      cgd }
    242   1.1      cgd 
    243   1.4    lukem void
    244   1.1      cgd show(s)
    245   1.7      jsm 	const char *s;
    246   1.1      cgd {
    247   1.1      cgd 	if (sflag)
    248   1.1      cgd 		printf(" %s", s);
    249   1.1      cgd 	else for (; *s; ++s)
    250   1.1      cgd 		printf(" %s", *s == '.' ? "dit" : "daw");
    251   1.1      cgd 	printf(",\n");
    252   1.1      cgd }
    253