bcd.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* $NetBSD: bcd.c,v 1.13 2004/01/27 20:30:29 jsm Exp $ */
  2. /*
  3. * Copyright (c) 1989, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * This code is derived from software contributed to Berkeley by
  7. * Steve Hayman of the Indiana University Computer Science Dept.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #include <sys/cdefs.h>
  34. #ifndef lint
  35. __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
  36. The Regents of the University of California. All rights reserved.\n");
  37. #endif /* not lint */
  38. #ifndef lint
  39. #if 0
  40. static char sccsid[] = "@(#)bcd.c 8.2 (Berkeley) 3/20/94";
  41. #else
  42. __RCSID("$NetBSD: bcd.c,v 1.13 2004/01/27 20:30:29 jsm Exp $");
  43. #endif
  44. #endif /* not lint */
  45. /*
  46. * bcd --
  47. *
  48. * Read one line of standard input and produce something that looks like a
  49. * punch card. An attempt to reimplement /usr/games/bcd. All I looked at
  50. * was the man page.
  51. *
  52. * I couldn't find a BCD table handy so I wrote a shell script to deduce what
  53. * the patterns were that the old bcd was using for each possible 8-bit
  54. * character. These are the results -- the low order 12 bits represent the
  55. * holes. (A 1 bit is a hole.) These may be wrong, but they match the old
  56. * program!
  57. *
  58. * Steve Hayman
  59. * sahayman@iuvax.cs.indiana.edu
  60. * 1989 11 30
  61. *
  62. *
  63. * I found an error in the table. The same error is found in the SunOS 4.1.1
  64. * version of bcd. It has apparently been around a long time. The error caused
  65. * 'Q' and 'R' to have the same punch code. I only noticed the error due to
  66. * someone pointing it out to me when the program was used to print a cover
  67. * for an APA! The table was wrong in 4 places. The other error was masked
  68. * by the fact that the input is converted to upper case before lookup.
  69. *
  70. * Dyane Bruce
  71. * db@diana.ocunix.on.ca
  72. * Nov 5, 1993
  73. */
  74. #include <sys/types.h>
  75. #include <stdio.h>
  76. #include <stdlib.h>
  77. #include <string.h>
  78. #include <ctype.h>
  79. #include <unistd.h>
  80. const u_short holes[256] = {
  81. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  82. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  83. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  84. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  85. 0x0, 0x206, 0x20a, 0x042, 0x442, 0x222, 0x800, 0x406,
  86. 0x812, 0x412, 0x422, 0xa00, 0x242, 0x400, 0x842, 0x300,
  87. 0x200, 0x100, 0x080, 0x040, 0x020, 0x010, 0x008, 0x004,
  88. 0x002, 0x001, 0x012, 0x40a, 0x80a, 0x212, 0x00a, 0x006,
  89. 0x022, 0x900, 0x880, 0x840, 0x820, 0x810, 0x808, 0x804,
  90. 0x802, 0x801, 0x500, 0x480, 0x440, 0x420, 0x410, 0x408,
  91. 0x404, 0x402, 0x401, 0x280, 0x240, 0x220, 0x210, 0x208,
  92. 0x204, 0x202, 0x201, 0x082, 0x822, 0x600, 0x282, 0x30f,
  93. 0x900, 0x880, 0x840, 0x820, 0x810, 0x808, 0x804, 0x802,
  94. 0x801, 0x500, 0x480, 0x440, 0x420, 0x410, 0x408, 0x404,
  95. 0x402, 0x401, 0x280, 0x240, 0x220, 0x210, 0x208, 0x204,
  96. 0x202, 0x201, 0x082, 0x806, 0x822, 0x600, 0x282, 0x0,
  97. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  98. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  99. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  100. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  101. 0x206, 0x20a, 0x042, 0x442, 0x222, 0x800, 0x406, 0x812,
  102. 0x412, 0x422, 0xa00, 0x242, 0x400, 0x842, 0x300, 0x200,
  103. 0x100, 0x080, 0x040, 0x020, 0x010, 0x008, 0x004, 0x002,
  104. 0x001, 0x012, 0x40a, 0x80a, 0x212, 0x00a, 0x006, 0x022,
  105. 0x900, 0x880, 0x840, 0x820, 0x810, 0x808, 0x804, 0x802,
  106. 0x801, 0x500, 0x480, 0x440, 0x420, 0x410, 0x408, 0x404,
  107. 0x402, 0x401, 0x280, 0x240, 0x220, 0x210, 0x208, 0x204,
  108. 0x202, 0x201, 0x082, 0x806, 0x822, 0x600, 0x282, 0x30f,
  109. 0x900, 0x880, 0x840, 0x820, 0x810, 0x808, 0x804, 0x802,
  110. 0x801, 0x500, 0x480, 0x440, 0x420, 0x410, 0x408, 0x404,
  111. 0x402, 0x401, 0x280, 0x240, 0x220, 0x210, 0x208, 0x204,
  112. 0x202, 0x201, 0x082, 0x806, 0x822, 0x600, 0x282, 0x0
  113. };
  114. /*
  115. * i'th bit of w.
  116. */
  117. #define bit(w,i) ((w)&(1<<(i)))
  118. int main(int, char *[]);
  119. void printcard(unsigned char *);
  120. int
  121. main(argc, argv)
  122. int argc;
  123. char **argv;
  124. {
  125. char cardline[80];
  126. /* revoke setgid privileges */
  127. setregid(getgid(), getgid());
  128. /*
  129. * The original bcd prompts with a "%" when reading from stdin,
  130. * but this seems kind of silly. So this one doesn't.
  131. */
  132. if (argc > 1) {
  133. while (--argc)
  134. printcard((unsigned char *)*++argv);
  135. } else
  136. while (fgets(cardline, sizeof(cardline), stdin))
  137. printcard((unsigned char *)cardline);
  138. exit(0);
  139. }
  140. #define COLUMNS 48
  141. void
  142. printcard(str)
  143. unsigned char *str;
  144. {
  145. static const char rowchars[] = " 123456789";
  146. int i, row;
  147. unsigned char *p;
  148. /* ruthlessly remove newlines and truncate at 48 characters. */
  149. if ((p = (unsigned char *)strchr((char *)str, '\n')))
  150. *p = '\0';
  151. if (strlen((char *)str) > COLUMNS)
  152. str[COLUMNS] = '\0';
  153. /* make string upper case. */
  154. for (p = str; *p; ++p)
  155. if (isascii(*p) && islower(*p))
  156. *p = toupper(*p);
  157. /* top of card */
  158. putchar(' ');
  159. for (i = 1; i <= COLUMNS; ++i)
  160. putchar('_');
  161. putchar('\n');
  162. /*
  163. * line of text. Leave a blank if the character doesn't have
  164. * a hole pattern.
  165. */
  166. p = str;
  167. putchar('/');
  168. for (i = 1; *p; i++, p++)
  169. if (holes[(int)*p])
  170. putchar(*p);
  171. else
  172. putchar(' ');
  173. while (i++ <= COLUMNS)
  174. putchar(' ');
  175. putchar('|');
  176. putchar('\n');
  177. /*
  178. * 12 rows of potential holes; output a ']', which looks kind of
  179. * like a hole, if the appropriate bit is set in the holes[] table.
  180. * The original bcd output a '[', a backspace, five control A's,
  181. * and then a ']'. This seems a little excessive.
  182. */
  183. for (row = 0; row <= 11; ++row) {
  184. putchar('|');
  185. for (i = 0, p = str; *p; i++, p++) {
  186. if (bit(holes[(int)*p], 11 - row))
  187. putchar(']');
  188. else
  189. putchar(rowchars[row]);
  190. }
  191. while (i++ < COLUMNS)
  192. putchar(rowchars[row]);
  193. putchar('|');
  194. putchar('\n');
  195. }
  196. /* bottom of card */
  197. putchar('|');
  198. for (i = 1; i <= COLUMNS; i++)
  199. putchar('_');
  200. putchar('|');
  201. putchar('\n');
  202. }