1 /*- 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Steve Hayman of the Indiana University Computer Science Dept. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved. 33 * @(#)bcd.c 8.2 (Berkeley) 3/20/94 34 * $NetBSD: bcd.c,v 1.6 1995/04/24 12:22:23 cgd Exp $ 35 * $OpenBSD: bcd.c,v 1.25 2016/03/07 12:07:55 mestre Exp $ 36 */ 37 38 /* 39 * bcd -- 40 * 41 * Read one line of standard input and produce something that looks like a 42 * punch card. An attempt to reimplement /usr/games/bcd. All I looked at 43 * was the man page. 44 * 45 * I couldn't find a BCD table handy so I wrote a shell script to deduce what 46 * the patterns were that the old bcd was using for each possible 8-bit 47 * character. These are the results -- the low order 12 bits represent the 48 * holes. (A 1 bit is a hole.) These may be wrong, but they match the old 49 * program! 50 * 51 * Steve Hayman 52 * sahayman@iuvax.cs.indiana.edu 53 * 1989 11 30 54 * 55 * 56 * I found an error in the table. The same error is found in the SunOS 4.1.1 57 * version of bcd. It has apparently been around a long time. The error caused 58 * 'Q' and 'R' to have the same punch code. I only noticed the error due to 59 * someone pointing it out to me when the program was used to print a cover 60 * for an APA! The table was wrong in 4 places. The other error was masked 61 * by the fact that the input is converted to upper case before lookup. 62 * 63 * Dyane Bruce 64 * db@diana.ocunix.on.ca 65 * Nov 5, 1993 66 */ 67 68 #include <ctype.h> 69 #include <stdio.h> 70 #include <stdlib.h> 71 #include <string.h> 72 #include <unistd.h> 73 74 static const u_short holes[256] = { 75 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 76 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 77 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 78 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 79 0x0, 0x206, 0x20a, 0x042, 0x442, 0x222, 0x800, 0x406, 80 0x812, 0x412, 0x422, 0xa00, 0x242, 0x400, 0x842, 0x300, 81 0x200, 0x100, 0x080, 0x040, 0x020, 0x010, 0x008, 0x004, 82 0x002, 0x001, 0x012, 0x40a, 0x80a, 0x212, 0x00a, 0x006, 83 0x022, 0x900, 0x880, 0x840, 0x820, 0x810, 0x808, 0x804, 84 0x802, 0x801, 0x500, 0x480, 0x440, 0x420, 0x410, 0x408, 85 0x404, 0x402, 0x401, 0x280, 0x240, 0x220, 0x210, 0x208, 86 0x204, 0x202, 0x201, 0x082, 0x822, 0x600, 0x282, 0x30f, 87 0x900, 0x880, 0x840, 0x820, 0x810, 0x808, 0x804, 0x802, 88 0x801, 0x500, 0x480, 0x440, 0x420, 0x410, 0x408, 0x404, 89 0x402, 0x401, 0x280, 0x240, 0x220, 0x210, 0x208, 0x204, 90 0x202, 0x201, 0x082, 0x806, 0x822, 0x600, 0x282, 0x0, 91 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 92 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 93 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 94 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 95 0x206, 0x20a, 0x042, 0x442, 0x222, 0x800, 0x406, 0x812, 96 0x412, 0x422, 0xa00, 0x242, 0x400, 0x842, 0x300, 0x200, 97 0x100, 0x080, 0x040, 0x020, 0x010, 0x008, 0x004, 0x002, 98 0x001, 0x012, 0x40a, 0x80a, 0x212, 0x00a, 0x006, 0x022, 99 0x900, 0x880, 0x840, 0x820, 0x810, 0x808, 0x804, 0x802, 100 0x801, 0x500, 0x480, 0x440, 0x420, 0x410, 0x408, 0x404, 101 0x402, 0x401, 0x280, 0x240, 0x220, 0x210, 0x208, 0x204, 102 0x202, 0x201, 0x082, 0x806, 0x822, 0x600, 0x282, 0x30f, 103 0x900, 0x880, 0x840, 0x820, 0x810, 0x808, 0x804, 0x802, 104 0x801, 0x500, 0x480, 0x440, 0x420, 0x410, 0x408, 0x404, 105 0x402, 0x401, 0x280, 0x240, 0x220, 0x210, 0x208, 0x204, 106 0x202, 0x201, 0x082, 0x806, 0x822, 0x600, 0x282, 0x0 107 }; 108 109 /* 110 * i'th bit of w. 111 */ 112 #define bit(w,i) ((w)&(1<<(i))) 113 114 static void printonecard(char *, size_t); 115 static void printcard(char *); 116 static int decode(char *buf); 117 118 static unsigned int columns = 48; 119 120 int 121 main(int argc, char *argv[]) 122 { 123 char cardline[1024]; 124 int dflag = 0; 125 int ch; 126 127 /* revoke setgid privileges */ 128 setgid(getgid()); 129 130 while ((ch = getopt(argc, argv, "dl")) != -1) { 131 switch (ch) { 132 case 'd': 133 dflag = 1; 134 break; 135 case 'l': 136 columns = 80; 137 break; 138 default: 139 fprintf(stderr, "usage: %s [-l] [string ...]\n", 140 getprogname()); 141 fprintf(stderr, "usage: %s -d [-l]\n", getprogname()); 142 return 1; 143 } 144 } 145 argc -= optind; 146 argv += optind; 147 148 if (dflag) { 149 while (decode(cardline) == 0) { 150 printf("%s\n", cardline); 151 } 152 return 0; 153 } 154 155 156 /* 157 * The original bcd prompts with a "%" when reading from stdin, 158 * but this seems kind of silly. So this one doesn't. 159 */ 160 if (argc > 0) { 161 while (argc--) { 162 printcard(*argv); 163 argv++; 164 } 165 } else { 166 while (fgets(cardline, sizeof(cardline), stdin)) 167 printcard(cardline); 168 } 169 return 0; 170 } 171 172 void 173 printcard(char *str) 174 { 175 size_t len = strlen(str); 176 177 while (len > 0) { 178 size_t amt = len > columns ? columns : len; 179 printonecard(str, amt); 180 str += amt; 181 len -= amt; 182 } 183 } 184 185 void 186 printonecard(char *str, size_t len) 187 { 188 static const char rowchars[] = " 123456789"; 189 unsigned int i; 190 int row; 191 char *p, *end; 192 193 end = str + len; 194 195 /* make string upper case. */ 196 for (p = str; p < end; ++p) { 197 if (isascii(*p) && islower(*p)) 198 *p = toupper(*p); 199 } 200 201 /* top of card */ 202 putchar(' '); 203 for (i = 1; i <= columns; ++i) 204 putchar('_'); 205 putchar('\n'); 206 207 /* 208 * line of text. Leave a blank if the character doesn't have 209 * a hole pattern. 210 */ 211 p = str; 212 putchar('/'); 213 for (i = 1; p < end; i++, p++) 214 if (holes[(int)*p]) 215 putchar(*p); 216 else 217 putchar(' '); 218 while (i++ <= columns) 219 putchar(' '); 220 putchar('|'); 221 putchar('\n'); 222 223 /* 224 * 12 rows of potential holes; output a ']', which looks kind of 225 * like a hole, if the appropriate bit is set in the holes[] table. 226 * The original bcd output a '[', a backspace, five control A's, 227 * and then a ']'. This seems a little excessive. 228 */ 229 for (row = 0; row <= 11; ++row) { 230 putchar('|'); 231 for (i = 0, p = str; p < end; i++, p++) { 232 if (bit(holes[(int)*p], 11 - row)) 233 putchar(']'); 234 else 235 putchar(rowchars[row]); 236 } 237 while (i++ < columns) 238 putchar(rowchars[row]); 239 putchar('|'); 240 putchar('\n'); 241 } 242 243 /* bottom of card */ 244 putchar('|'); 245 for (i = 1; i <= columns; i++) 246 putchar('_'); 247 putchar('|'); 248 putchar('\n'); 249 } 250 251 #define LINES 12 252 253 int 254 decode(char *buf) 255 { 256 unsigned int i; 257 int col; 258 char lines[LINES][1024]; 259 char tmp[1024]; 260 261 /* top of card; if missing signal no more input */ 262 if (fgets(tmp, sizeof(tmp), stdin) == NULL) 263 return 1; 264 /* text line, ignored */ 265 if (fgets(tmp, sizeof(tmp), stdin) == NULL) 266 return -1; 267 /* twelve lines of data */ 268 for (i = 0; i < LINES; i++) 269 if (fgets(lines[i], sizeof(lines[i]), stdin) == NULL) 270 return -1; 271 /* bottom of card */ 272 if (fgets(tmp, sizeof(tmp), stdin) == NULL) 273 return -1; 274 275 for (i = 0; i < LINES; i++) { 276 if (strlen(lines[i]) < columns + 2) 277 return -1; 278 if (lines[i][0] != '|' || lines[i][columns + 1] != '|') 279 return -1; 280 memmove(&lines[i][0], &lines[i][1], columns); 281 lines[i][columns] = 0; 282 } 283 for (col = 0; col < (int)columns; col++) { 284 unsigned int val = 0; 285 for (i = 0; i < LINES; i++) 286 if (lines[i][col] == ']') 287 val |= 1 << (11 - i); 288 buf[col] = ' '; 289 for (i = 0; i < 256; i++) 290 if (holes[i] == val && holes[i]) { 291 buf[col] = i; 292 break; 293 } 294 } 295 buf[col] = 0; 296 for (col = columns - 1; col >= 0; col--) { 297 if (buf[col] == ' ') 298 buf[col] = '\0'; 299 else 300 break; 301 } 302 return 0; 303 } 304