xref: /dragonfly/games/bcd/bcd.c (revision cb633bb4)
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. 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  * @(#) Copyright (c) 1989, 1993 The Regents of the University of California.  All rights reserved.
37  * @(#)bcd.c	8.2 (Berkeley) 3/20/94
38  * $FreeBSD: src/games/bcd/bcd.c,v 1.8 1999/11/30 03:48:41 billf Exp $
39  * $DragonFly: src/games/bcd/bcd.c,v 1.3 2005/08/03 13:26:19 eirikn Exp $
40  */
41 
42 /*
43  * bcd --
44  *
45  * Read one line of standard input and produce something that looks like a
46  * punch card.  An attempt to reimplement /usr/games/bcd.  All I looked at
47  * was the man page.
48  *
49  * I couldn't find a BCD table handy so I wrote a shell script to deduce what
50  * the patterns were that the old bcd was using for each possible 8-bit
51  * character.  These are the results -- the low order 12 bits represent the
52  * holes.  (A 1 bit is a hole.)  These may be wrong, but they match the old
53  * program!
54  *
55  * Steve Hayman
56  * sahayman@iuvax.cs.indiana.edu
57  * 1989 11 30
58  *
59  *
60  * I found an error in the table. The same error is found in the SunOS 4.1.1
61  * version of bcd. It has apparently been around a long time. The error caused
62  * 'Q' and 'R' to have the same punch code. I only noticed the error due to
63  * someone pointing it out to me when the program was used to print a cover
64  * for an APA!  The table was wrong in 4 places. The other error was masked
65  * by the fact that the input is converted to upper case before lookup.
66  *
67  * Dyane Bruce
68  * db@diana.ocunix.on.ca
69  * Nov 5, 1993
70  */
71 
72 #include <sys/types.h>
73 
74 #include <stdio.h>
75 #include <string.h>
76 #include <unistd.h>
77 #include <ctype.h>
78 
79 static void	printcard(char *);
80 
81 u_short holes[256] = {
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,	 0x0,	  0x0,	   0x0,	    0x0,     0x0,     0x0,     0x0,
86     0x0,	 0x206,	  0x20a,   0x042,   0x442,   0x222,   0x800,   0x406,
87     0x812,	 0x412,	  0x422,   0xa00,   0x242,   0x400,   0x842,   0x300,
88     0x200,	 0x100,	  0x080,   0x040,   0x020,   0x010,   0x008,   0x004,
89     0x002,	 0x001,	  0x012,   0x40a,   0x80a,   0x212,   0x00a,   0x006,
90     0x022,	 0x900,	  0x880,   0x840,   0x820,   0x810,   0x808,   0x804,
91     0x802,	 0x801,	  0x500,   0x480,   0x440,   0x420,   0x410,   0x408,
92     0x404,	 0x402,	  0x401,   0x280,   0x240,   0x220,   0x210,   0x208,
93     0x204,	 0x202,	  0x201,   0x082,   0x822,   0x600,   0x282,   0x30f,
94     0x900,	 0x880,	  0x840,   0x820,   0x810,   0x808,   0x804,   0x802,
95     0x801,	 0x500,	  0x480,   0x440,   0x420,   0x410,   0x408,   0x404,
96     0x402,	 0x401,	  0x280,   0x240,   0x220,   0x210,   0x208,   0x204,
97     0x202,	 0x201,	  0x082,   0x806,   0x822,   0x600,   0x282,   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     0x0,	 0x0,	  0x0,	   0x0,	    0x0,     0x0,     0x0,     0x0,
102     0x206,	 0x20a,	  0x042,   0x442,   0x222,   0x800,   0x406,   0x812,
103     0x412,	 0x422,	  0xa00,   0x242,   0x400,   0x842,   0x300,   0x200,
104     0x100,	 0x080,	  0x040,   0x020,   0x010,   0x008,   0x004,   0x002,
105     0x001,	 0x012,	  0x40a,   0x80a,   0x212,   0x00a,   0x006,   0x022,
106     0x900,	 0x880,	  0x840,   0x820,   0x810,   0x808,   0x804,   0x802,
107     0x801,	 0x500,	  0x480,   0x440,   0x420,   0x410,   0x408,   0x404,
108     0x402,	 0x401,	  0x280,   0x240,   0x220,   0x210,   0x208,   0x204,
109     0x202,	 0x201,	  0x082,   0x806,   0x822,   0x600,   0x282,   0x30f,
110     0x900,	 0x880,	  0x840,   0x820,   0x810,   0x808,   0x804,   0x802,
111     0x801,	 0x500,	  0x480,   0x440,   0x420,   0x410,   0x408,   0x404,
112     0x402,	 0x401,	  0x280,   0x240,   0x220,   0x210,   0x208,   0x204,
113     0x202,	 0x201,	  0x082,   0x806,   0x822,   0x600,   0x282,   0x0
114 };
115 
116 /*
117  * i'th bit of w.
118  */
119 #define	bit(w,i)	((w)&(1<<(i)))
120 
121 int
122 main(int argc, char **argv)
123 {
124 	char cardline[80];
125 
126 	/* revoke setgid privileges */
127 	setgid(getgid());
128 
129 	/*
130 	 * The original bcd prompts with a "%" when reading from stdin,
131 	 * but this seems kind of silly.  So this one doesn't.
132 	 */
133 
134 	if (argc > 1) {
135 		while (--argc)
136 			printcard(*++argv);
137 	} else
138 		while (fgets(cardline, sizeof(cardline), stdin))
139 			printcard(cardline);
140 	return(0);
141 }
142 
143 #define	COLUMNS	48
144 
145 static void
146 printcard(char *str)
147 {
148 	static char rowchars[] = "   123456789";
149 	int i, row;
150 	char *p;
151 
152 	/* ruthlessly remove newlines and truncate at 48 characters. */
153 	if ((p = index(str, '\n')))
154 		*p = '\0';
155 
156 	if (strlen(str) > COLUMNS)
157 		str[COLUMNS] = '\0';
158 
159 	/* make string upper case. */
160 	for (p = str; *p; ++p)
161 		if (isascii(*p) && islower(*p))
162 			*p = toupper(*p);
163 
164 	 /* top of card */
165 	putchar(' ');
166 	for (i = 1; i <= COLUMNS; ++i)
167 		putchar('_');
168 	putchar('\n');
169 
170 	/*
171 	 * line of text.  Leave a blank if the character doesn't have
172 	 * a hole pattern.
173 	 */
174 	p = str;
175 	putchar('/');
176 	for (i = 1; *p; i++, p++)
177 		if (holes[(int)*p])
178 			putchar(*p);
179 		else
180 			putchar(' ');
181 	while (i++ <= COLUMNS)
182 		putchar(' ');
183 	putchar('|');
184 	putchar('\n');
185 
186 	/*
187 	 * 12 rows of potential holes; output a ']', which looks kind of
188 	 * like a hole, if the appropriate bit is set in the holes[] table.
189 	 * The original bcd output a '[', a backspace, five control A's,
190 	 * and then a ']'.  This seems a little excessive.
191 	 */
192 	for (row = 0; row <= 11; ++row) {
193 		putchar('|');
194 		for (i = 0, p = str; *p; i++, p++) {
195 			if (bit(holes[(int)*p], 11 - row))
196 				putchar(']');
197 			else
198 				putchar(rowchars[row]);
199 		}
200 		while (i++ < COLUMNS)
201 			putchar(rowchars[row]);
202 		putchar('|');
203 		putchar('\n');
204 	}
205 
206 	/* bottom of card */
207 	putchar('|');
208 	for (i = 1; i <= COLUMNS; i++)
209 		putchar('_');
210 	putchar('|');
211 	putchar('\n');
212 }
213