bcd.c revision 1.11
11.11Smatt/*	$NetBSD: bcd.c,v 1.11 2000/07/03 03:57:40 matt Exp $	*/
21.5Scgd
31.1Scgd/*
41.5Scgd * Copyright (c) 1989, 1993
51.5Scgd *	The Regents of the University of California.  All rights reserved.
61.1Scgd *
71.1Scgd * This code is derived from software contributed to Berkeley by
81.5Scgd * Steve Hayman of the Indiana University Computer Science Dept.
91.1Scgd *
101.1Scgd * Redistribution and use in source and binary forms, with or without
111.1Scgd * modification, are permitted provided that the following conditions
121.1Scgd * are met:
131.1Scgd * 1. Redistributions of source code must retain the above copyright
141.1Scgd *    notice, this list of conditions and the following disclaimer.
151.1Scgd * 2. Redistributions in binary form must reproduce the above copyright
161.1Scgd *    notice, this list of conditions and the following disclaimer in the
171.1Scgd *    documentation and/or other materials provided with the distribution.
181.1Scgd * 3. All advertising materials mentioning features or use of this software
191.1Scgd *    must display the following acknowledgement:
201.1Scgd *	This product includes software developed by the University of
211.1Scgd *	California, Berkeley and its contributors.
221.1Scgd * 4. Neither the name of the University nor the names of its contributors
231.1Scgd *    may be used to endorse or promote products derived from this software
241.1Scgd *    without specific prior written permission.
251.1Scgd *
261.1Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
271.1Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
281.1Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
291.1Scgd * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
301.1Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
311.1Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
321.1Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
331.1Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
341.1Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
351.1Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
361.1Scgd * SUCH DAMAGE.
371.1Scgd */
381.1Scgd
391.7Slukem#include <sys/cdefs.h>
401.1Scgd#ifndef lint
411.7Slukem__COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
421.7Slukem	The Regents of the University of California.  All rights reserved.\n");
431.1Scgd#endif /* not lint */
441.1Scgd
451.1Scgd#ifndef lint
461.7Slukem#if 0
471.5Scgdstatic char sccsid[] = "@(#)bcd.c	8.2 (Berkeley) 3/20/94";
481.7Slukem#else
491.11Smatt__RCSID("$NetBSD: bcd.c,v 1.11 2000/07/03 03:57:40 matt Exp $");
501.7Slukem#endif
511.1Scgd#endif /* not lint */
521.1Scgd
531.1Scgd/*
541.1Scgd * bcd --
551.1Scgd *
561.1Scgd * Read one line of standard input and produce something that looks like a
571.1Scgd * punch card.  An attempt to reimplement /usr/games/bcd.  All I looked at
581.1Scgd * was the man page.
591.1Scgd *
601.1Scgd * I couldn't find a BCD table handy so I wrote a shell script to deduce what
611.1Scgd * the patterns were that the old bcd was using for each possible 8-bit
621.1Scgd * character.  These are the results -- the low order 12 bits represent the
631.1Scgd * holes.  (A 1 bit is a hole.)  These may be wrong, but they match the old
641.1Scgd * program!
651.1Scgd *
661.1Scgd * Steve Hayman
671.1Scgd * sahayman@iuvax.cs.indiana.edu
681.1Scgd * 1989 11 30
691.5Scgd *
701.5Scgd *
711.5Scgd * I found an error in the table. The same error is found in the SunOS 4.1.1
721.5Scgd * version of bcd. It has apparently been around a long time. The error caused
731.5Scgd * 'Q' and 'R' to have the same punch code. I only noticed the error due to
741.5Scgd * someone pointing it out to me when the program was used to print a cover
751.5Scgd * for an APA!  The table was wrong in 4 places. The other error was masked
761.5Scgd * by the fact that the input is converted to upper case before lookup.
771.5Scgd *
781.5Scgd * Dyane Bruce
791.5Scgd * db@diana.ocunix.on.ca
801.5Scgd * Nov 5, 1993
811.1Scgd */
821.1Scgd
831.1Scgd#include <sys/types.h>
841.5Scgd
851.1Scgd#include <stdio.h>
861.11Smatt#include <stdlib.h>
871.6Scgd#include <string.h>
881.1Scgd#include <ctype.h>
891.8Shubertf#include <unistd.h>
901.1Scgd
911.9Shubertfconst u_short holes[256] = {
921.1Scgd    0x0,	 0x0,	  0x0,	   0x0,	    0x0,     0x0,     0x0,     0x0,
931.1Scgd    0x0,	 0x0,	  0x0,	   0x0,	    0x0,     0x0,     0x0,     0x0,
941.1Scgd    0x0,	 0x0,	  0x0,	   0x0,	    0x0,     0x0,     0x0,     0x0,
951.1Scgd    0x0,	 0x0,	  0x0,	   0x0,	    0x0,     0x0,     0x0,     0x0,
961.1Scgd    0x0,	 0x206,	  0x20a,   0x042,   0x442,   0x222,   0x800,   0x406,
971.1Scgd    0x812,	 0x412,	  0x422,   0xa00,   0x242,   0x400,   0x842,   0x300,
981.1Scgd    0x200,	 0x100,	  0x080,   0x040,   0x020,   0x010,   0x008,   0x004,
991.1Scgd    0x002,	 0x001,	  0x012,   0x40a,   0x80a,   0x212,   0x00a,   0x006,
1001.1Scgd    0x022,	 0x900,	  0x880,   0x840,   0x820,   0x810,   0x808,   0x804,
1011.1Scgd    0x802,	 0x801,	  0x500,   0x480,   0x440,   0x420,   0x410,   0x408,
1021.3Sjtc    0x404,	 0x402,	  0x401,   0x280,   0x240,   0x220,   0x210,   0x208,
1031.1Scgd    0x204,	 0x202,	  0x201,   0x082,   0x822,   0x600,   0x282,   0x30f,
1041.1Scgd    0x900,	 0x880,	  0x840,   0x820,   0x810,   0x808,   0x804,   0x802,
1051.1Scgd    0x801,	 0x500,	  0x480,   0x440,   0x420,   0x410,   0x408,   0x404,
1061.4Sjtc    0x402,	 0x401,	  0x280,   0x240,   0x220,   0x210,   0x208,   0x204,
1071.1Scgd    0x202,	 0x201,	  0x082,   0x806,   0x822,   0x600,   0x282,   0x0,
1081.1Scgd    0x0,	 0x0,	  0x0,	   0x0,	    0x0,     0x0,     0x0,     0x0,
1091.1Scgd    0x0,	 0x0,	  0x0,	   0x0,	    0x0,     0x0,     0x0,     0x0,
1101.1Scgd    0x0,	 0x0,	  0x0,	   0x0,	    0x0,     0x0,     0x0,     0x0,
1111.1Scgd    0x0,	 0x0,	  0x0,	   0x0,	    0x0,     0x0,     0x0,     0x0,
1121.1Scgd    0x206,	 0x20a,	  0x042,   0x442,   0x222,   0x800,   0x406,   0x812,
1131.1Scgd    0x412,	 0x422,	  0xa00,   0x242,   0x400,   0x842,   0x300,   0x200,
1141.1Scgd    0x100,	 0x080,	  0x040,   0x020,   0x010,   0x008,   0x004,   0x002,
1151.1Scgd    0x001,	 0x012,	  0x40a,   0x80a,   0x212,   0x00a,   0x006,   0x022,
1161.1Scgd    0x900,	 0x880,	  0x840,   0x820,   0x810,   0x808,   0x804,   0x802,
1171.1Scgd    0x801,	 0x500,	  0x480,   0x440,   0x420,   0x410,   0x408,   0x404,
1181.3Sjtc    0x402,	 0x401,	  0x280,   0x240,   0x220,   0x210,   0x208,   0x204,
1191.1Scgd    0x202,	 0x201,	  0x082,   0x806,   0x822,   0x600,   0x282,   0x30f,
1201.1Scgd    0x900,	 0x880,	  0x840,   0x820,   0x810,   0x808,   0x804,   0x802,
1211.1Scgd    0x801,	 0x500,	  0x480,   0x440,   0x420,   0x410,   0x408,   0x404,
1221.4Sjtc    0x402,	 0x401,	  0x280,   0x240,   0x220,   0x210,   0x208,   0x204,
1231.1Scgd    0x202,	 0x201,	  0x082,   0x806,   0x822,   0x600,   0x282,   0x0
1241.1Scgd};
1251.1Scgd
1261.1Scgd/*
1271.1Scgd * i'th bit of w.
1281.1Scgd */
1291.1Scgd#define	bit(w,i)	((w)&(1<<(i)))
1301.1Scgd
1311.7Slukemint	main __P((int, char *[]));
1321.7Slukemvoid	printcard __P((char *));
1331.7Slukem
1341.5Scgdint
1351.1Scgdmain(argc, argv)
1361.1Scgd	int argc;
1371.1Scgd	char **argv;
1381.1Scgd{
1391.1Scgd	char cardline[80];
1401.8Shubertf
1411.8Shubertf	/* revoke setgid privileges */
1421.10Smycroft	setgid(getgid());
1431.1Scgd
1441.1Scgd	/*
1451.1Scgd	 * The original bcd prompts with a "%" when reading from stdin,
1461.1Scgd	 * but this seems kind of silly.  So this one doesn't.
1471.1Scgd	 */
1481.1Scgd
1491.1Scgd	if (argc > 1) {
1501.1Scgd		while (--argc)
1511.1Scgd			printcard(*++argv);
1521.1Scgd	} else
1531.1Scgd		while (fgets(cardline, sizeof(cardline), stdin))
1541.1Scgd			printcard(cardline);
1551.1Scgd	exit(0);
1561.1Scgd}
1571.1Scgd
1581.1Scgd#define	COLUMNS	48
1591.1Scgd
1601.7Slukemvoid
1611.1Scgdprintcard(str)
1621.7Slukem	char *str;
1631.1Scgd{
1641.9Shubertf	static const char rowchars[] = "   123456789";
1651.7Slukem	int i, row;
1661.9Shubertf	unsigned char *p;
1671.1Scgd
1681.1Scgd	/* ruthlessly remove newlines and truncate at 48 characters. */
1691.7Slukem	if ((p = strchr(str, '\n')))
1701.1Scgd		*p = '\0';
1711.1Scgd
1721.1Scgd	if (strlen(str) > COLUMNS)
1731.1Scgd		str[COLUMNS] = '\0';
1741.1Scgd
1751.1Scgd	/* make string upper case. */
1761.1Scgd	for (p = str; *p; ++p)
1771.1Scgd		if (isascii(*p) && islower(*p))
1781.1Scgd			*p = toupper(*p);
1791.1Scgd
1801.1Scgd	 /* top of card */
1811.1Scgd	putchar(' ');
1821.1Scgd	for (i = 1; i <= COLUMNS; ++i)
1831.1Scgd		putchar('_');
1841.1Scgd	putchar('\n');
1851.1Scgd
1861.1Scgd	/*
1871.1Scgd	 * line of text.  Leave a blank if the character doesn't have
1881.1Scgd	 * a hole pattern.
1891.1Scgd	 */
1901.1Scgd	p = str;
1911.1Scgd	putchar('/');
1921.1Scgd	for (i = 1; *p; i++, p++)
1931.7Slukem		if (holes[(int)*p])
1941.1Scgd			putchar(*p);
1951.1Scgd		else
1961.1Scgd			putchar(' ');
1971.1Scgd	while (i++ <= COLUMNS)
1981.1Scgd		putchar(' ');
1991.1Scgd	putchar('|');
2001.1Scgd	putchar('\n');
2011.1Scgd
2021.1Scgd	/*
2031.1Scgd	 * 12 rows of potential holes; output a ']', which looks kind of
2041.1Scgd	 * like a hole, if the appropriate bit is set in the holes[] table.
2051.1Scgd	 * The original bcd output a '[', a backspace, five control A's,
2061.1Scgd	 * and then a ']'.  This seems a little excessive.
2071.1Scgd	 */
2081.1Scgd	for (row = 0; row <= 11; ++row) {
2091.1Scgd		putchar('|');
2101.1Scgd		for (i = 0, p = str; *p; i++, p++) {
2111.7Slukem			if (bit(holes[(int)*p], 11 - row))
2121.1Scgd				putchar(']');
2131.1Scgd			else
2141.1Scgd				putchar(rowchars[row]);
2151.1Scgd		}
2161.1Scgd		while (i++ < COLUMNS)
2171.1Scgd			putchar(rowchars[row]);
2181.1Scgd		putchar('|');
2191.1Scgd		putchar('\n');
2201.1Scgd	}
2211.1Scgd
2221.1Scgd	/* bottom of card */
2231.1Scgd	putchar('|');
2241.1Scgd	for (i = 1; i <= COLUMNS; i++)
2251.1Scgd		putchar('_');
2261.1Scgd	putchar('|');
2271.1Scgd	putchar('\n');
2281.1Scgd}
229