1 1.23 rillig /* $NetBSD: morse.c,v 1.23 2024/10/12 19:26:24 rillig 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.15 lukem __COPYRIGHT("@(#) Copyright (c) 1988, 1993\ 34 1.15 lukem The Regents of the University of California. All rights reserved."); 35 1.23 rillig /* @(#)morse.c 8.1 (Berkeley) 5/31/93 */ 36 1.23 rillig __RCSID("$NetBSD: morse.c,v 1.23 2024/10/12 19:26:24 rillig Exp $"); 37 1.1 cgd 38 1.4 lukem #include <ctype.h> 39 1.1 cgd #include <stdio.h> 40 1.10 matt #include <stdlib.h> 41 1.6 hubertf #include <string.h> 42 1.4 lukem #include <unistd.h> 43 1.1 cgd 44 1.21 rillig static const char digit[][6] = { 45 1.1 cgd "-----", 46 1.1 cgd ".----", 47 1.1 cgd "..---", 48 1.1 cgd "...--", 49 1.1 cgd "....-", 50 1.1 cgd ".....", 51 1.1 cgd "-....", 52 1.1 cgd "--...", 53 1.1 cgd "---..", 54 1.1 cgd "----.", 55 1.21 rillig }; 56 1.21 rillig 57 1.21 rillig static const char alph[][5] = { 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.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.23 rillig static const struct { 87 1.13 jsm char c; 88 1.21 rillig const char morse[7]; 89 1.13 jsm } other[] = { 90 1.13 jsm { '.', ".-.-.-" }, 91 1.13 jsm { ',', "--..--" }, 92 1.13 jsm { ':', "---..." }, 93 1.13 jsm { '?', "..--.." }, 94 1.13 jsm { '\'', ".----." }, 95 1.13 jsm { '-', "-....-" }, 96 1.13 jsm { '/', "-..-." }, 97 1.13 jsm { '(', "-.--." }, 98 1.13 jsm { ')', "-.--.-" }, 99 1.13 jsm { '"', ".-..-." }, 100 1.13 jsm { '=', "-...-" }, 101 1.13 jsm { '+', ".-.-." }, 102 1.18 maya { '_', "..--.-" }, 103 1.20 mrg { '@', ".--.-." }, 104 1.21 rillig { '\0', "" } 105 1.13 jsm }; 106 1.13 jsm 107 1.16 dholland static void morse(int); 108 1.16 dholland static void decode(const char *); 109 1.16 dholland static void show(const char *); 110 1.4 lukem 111 1.1 cgd static int sflag; 112 1.6 hubertf static int dflag; 113 1.1 cgd 114 1.4 lukem int 115 1.17 dholland main(int argc, char **argv) 116 1.1 cgd { 117 1.4 lukem int ch; 118 1.13 jsm char *p; 119 1.8 jsm 120 1.8 jsm /* Revoke setgid privileges */ 121 1.9 mycroft setgid(getgid()); 122 1.1 cgd 123 1.6 hubertf while ((ch = getopt(argc, argv, "ds")) != -1) 124 1.23 rillig switch ((char)ch) { 125 1.6 hubertf case 'd': 126 1.6 hubertf dflag = 1; 127 1.6 hubertf break; 128 1.1 cgd case 's': 129 1.1 cgd sflag = 1; 130 1.1 cgd break; 131 1.1 cgd default: 132 1.6 hubertf fprintf(stderr, "usage: morse [-ds] [string ...]\n"); 133 1.1 cgd exit(1); 134 1.1 cgd } 135 1.1 cgd argc -= optind; 136 1.1 cgd argv += optind; 137 1.1 cgd 138 1.6 hubertf if (dflag) { 139 1.6 hubertf if (*argv) { 140 1.6 hubertf do { 141 1.6 hubertf decode(*argv); 142 1.6 hubertf } while (*++argv); 143 1.13 jsm } else { 144 1.13 jsm char foo[10]; /* All morse chars shorter than this */ 145 1.14 dholland int is_blank, i; 146 1.13 jsm 147 1.13 jsm i = 0; 148 1.14 dholland is_blank = 0; 149 1.13 jsm while ((ch = getchar()) != EOF) { 150 1.13 jsm if (ch == '-' || ch == '.') { 151 1.13 jsm foo[i++] = ch; 152 1.13 jsm if (i == 10) { 153 1.13 jsm /* overrun means gibberish--print 'x' and 154 1.13 jsm * advance */ 155 1.13 jsm i = 0; 156 1.13 jsm putchar('x'); 157 1.13 jsm while ((ch = getchar()) != EOF && 158 1.13 jsm (ch == '.' || ch == '-')) 159 1.13 jsm ; 160 1.14 dholland is_blank = 1; 161 1.6 hubertf } 162 1.13 jsm } else if (i) { 163 1.13 jsm foo[i] = '\0'; 164 1.13 jsm decode(foo); 165 1.13 jsm i = 0; 166 1.14 dholland is_blank = 0; 167 1.13 jsm } else if (isspace(ch)) { 168 1.14 dholland if (is_blank) { 169 1.13 jsm /* print whitespace for each double blank */ 170 1.13 jsm putchar(' '); 171 1.14 dholland is_blank = 0; 172 1.13 jsm } else 173 1.14 dholland is_blank = 1; 174 1.6 hubertf } 175 1.6 hubertf } 176 1.6 hubertf } 177 1.6 hubertf putchar('\n'); 178 1.13 jsm } else { 179 1.6 hubertf if (*argv) 180 1.6 hubertf do { 181 1.6 hubertf for (p = *argv; *p; ++p) 182 1.22 rillig morse((unsigned char)*p); 183 1.13 jsm show(""); 184 1.6 hubertf } while (*++argv); 185 1.6 hubertf else while ((ch = getchar()) != EOF) 186 1.6 hubertf morse(ch); 187 1.13 jsm show("...-.-"); /* SK */ 188 1.6 hubertf } 189 1.19 rillig 190 1.6 hubertf return 0; 191 1.6 hubertf } 192 1.6 hubertf 193 1.6 hubertf void 194 1.17 dholland decode(const char *s) 195 1.6 hubertf { 196 1.23 rillig for (size_t i = 0; i < __arraycount(digit); i++) 197 1.13 jsm if (strcmp(digit[i], s) == 0) { 198 1.13 jsm putchar('0' + i); 199 1.13 jsm return; 200 1.6 hubertf } 201 1.19 rillig 202 1.23 rillig for (size_t i = 0; i < __arraycount(alph); i++) 203 1.13 jsm if (strcmp(alph[i], s) == 0) { 204 1.13 jsm putchar('A' + i); 205 1.6 hubertf return; 206 1.6 hubertf } 207 1.23 rillig 208 1.23 rillig for (size_t i = 0; other[i].c; i++) 209 1.13 jsm if (strcmp(other[i].morse, s) == 0) { 210 1.13 jsm putchar(other[i].c); 211 1.13 jsm return; 212 1.6 hubertf } 213 1.23 rillig 214 1.23 rillig if (strcmp("...-.-", s) == 0) /* SK */ 215 1.13 jsm return; 216 1.23 rillig 217 1.13 jsm putchar('x'); /* line noise */ 218 1.1 cgd } 219 1.1 cgd 220 1.4 lukem void 221 1.17 dholland morse(int c) 222 1.1 cgd { 223 1.1 cgd if (isalpha(c)) 224 1.1 cgd show(alph[c - (isupper(c) ? 'A' : 'a')]); 225 1.1 cgd else if (isdigit(c)) 226 1.1 cgd show(digit[c - '0']); 227 1.1 cgd else if (isspace(c)) 228 1.13 jsm show(""); /* could show BT for a pause */ 229 1.23 rillig else 230 1.23 rillig for (int i = 0; other[i].c; i++) 231 1.13 jsm if (other[i].c == c) { 232 1.13 jsm show(other[i].morse); 233 1.13 jsm break; 234 1.13 jsm } 235 1.1 cgd } 236 1.1 cgd 237 1.4 lukem void 238 1.17 dholland show(const char *s) 239 1.1 cgd { 240 1.1 cgd if (sflag) 241 1.1 cgd printf(" %s", s); 242 1.1 cgd else for (; *s; ++s) 243 1.1 cgd printf(" %s", *s == '.' ? "dit" : "daw"); 244 1.13 jsm printf("\n"); 245 1.1 cgd } 246