morse.c 5.2 KB

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