Home | History | Annotate | Line # | Download | only in morse
morse.c revision 1.6
      1  1.6  hubertf /*	$NetBSD: morse.c,v 1.6 1998/11/18 14:22:32 hubertf 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.1      cgd  * 3. All advertising materials mentioning features or use of this software
     16  1.1      cgd  *    must display the following acknowledgement:
     17  1.1      cgd  *	This product includes software developed by the University of
     18  1.1      cgd  *	California, Berkeley and its contributors.
     19  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     20  1.1      cgd  *    may be used to endorse or promote products derived from this software
     21  1.1      cgd  *    without specific prior written permission.
     22  1.1      cgd  *
     23  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1      cgd  * SUCH DAMAGE.
     34  1.1      cgd  */
     35  1.1      cgd 
     36  1.4    lukem #include <sys/cdefs.h>
     37  1.1      cgd #ifndef lint
     38  1.4    lukem __COPYRIGHT("@(#) Copyright (c) 1988, 1993\n\
     39  1.4    lukem 	The Regents of the University of California.  All rights reserved.\n");
     40  1.1      cgd #endif /* not lint */
     41  1.1      cgd 
     42  1.1      cgd #ifndef lint
     43  1.3      cgd #if 0
     44  1.3      cgd static char sccsid[] = "@(#)morse.c	8.1 (Berkeley) 5/31/93";
     45  1.3      cgd #else
     46  1.6  hubertf __RCSID("$NetBSD: morse.c,v 1.6 1998/11/18 14:22:32 hubertf Exp $");
     47  1.3      cgd #endif
     48  1.1      cgd #endif /* not lint */
     49  1.1      cgd 
     50  1.4    lukem #include <ctype.h>
     51  1.1      cgd #include <stdio.h>
     52  1.6  hubertf #include <string.h>
     53  1.4    lukem #include <unistd.h>
     54  1.1      cgd 
     55  1.6  hubertf #define MORSE_COLON	"--..--"
     56  1.6  hubertf #define MORSE_PERIOD	".-.-.-"
     57  1.6  hubertf 
     58  1.6  hubertf 
     59  1.1      cgd static char
     60  1.1      cgd 	*digit[] = {
     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.1      cgd 	"---..",
     70  1.1      cgd 	"----.",
     71  1.1      cgd },
     72  1.1      cgd 	*alph[] = {
     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.1      cgd 	"--..",
     99  1.1      cgd };
    100  1.1      cgd 
    101  1.4    lukem int	main __P((int, char *[]));
    102  1.4    lukem void	morse __P((int));
    103  1.6  hubertf void	decode __P((const char *));
    104  1.4    lukem void	show __P((char *));
    105  1.4    lukem 
    106  1.1      cgd static int sflag;
    107  1.6  hubertf static int dflag;
    108  1.1      cgd 
    109  1.4    lukem int
    110  1.1      cgd main(argc, argv)
    111  1.1      cgd 	int argc;
    112  1.1      cgd 	char **argv;
    113  1.1      cgd {
    114  1.4    lukem 	int ch;
    115  1.6  hubertf 	char *s, *p;
    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.6  hubertf 		char **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.4    lukem 	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