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