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