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