[BACK]Return to morse.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / games / morse

Annotation of src/games/morse/morse.c, Revision 1.18

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

CVSweb <webmaster@jp.NetBSD.org>