bcd.c revision 1.4
11.1Scgd/*
21.1Scgd * Copyright (c) 1989 The Regents of the University of California.
31.1Scgd * All rights reserved.
41.1Scgd *
51.1Scgd * This code is derived from software contributed to Berkeley by
61.1Scgd * Steve Hayman of the Indiana University Computer Science Dept..
71.1Scgd *
81.1Scgd * Redistribution and use in source and binary forms, with or without
91.1Scgd * modification, are permitted provided that the following conditions
101.1Scgd * are met:
111.1Scgd * 1. Redistributions of source code must retain the above copyright
121.1Scgd *    notice, this list of conditions and the following disclaimer.
131.1Scgd * 2. Redistributions in binary form must reproduce the above copyright
141.1Scgd *    notice, this list of conditions and the following disclaimer in the
151.1Scgd *    documentation and/or other materials provided with the distribution.
161.1Scgd * 3. All advertising materials mentioning features or use of this software
171.1Scgd *    must display the following acknowledgement:
181.1Scgd *	This product includes software developed by the University of
191.1Scgd *	California, Berkeley and its contributors.
201.1Scgd * 4. Neither the name of the University nor the names of its contributors
211.1Scgd *    may be used to endorse or promote products derived from this software
221.1Scgd *    without specific prior written permission.
231.1Scgd *
241.1Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251.1Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261.1Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271.1Scgd * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281.1Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291.1Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301.1Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311.1Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321.1Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331.1Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341.1Scgd * SUCH DAMAGE.
351.1Scgd */
361.1Scgd
371.1Scgd#ifndef lint
381.1Scgdchar copyright[] =
391.1Scgd"@(#) Copyright (c) 1989 The Regents of the University of California.\n\
401.1Scgd All rights reserved.\n";
411.1Scgd#endif /* not lint */
421.1Scgd
431.1Scgd#ifndef lint
441.2Smycroft/*static char sccsid[] = "from: @(#)bcd.c	4.4 (Berkeley) 6/1/90";*/
451.3Sjtcstatic char rcsid[] = "$Id: bcd.c,v 1.4 1993/11/05 20:22:35 jtc Exp $";
461.1Scgd#endif /* not lint */
471.1Scgd
481.1Scgd/*
491.1Scgd * bcd --
501.1Scgd *
511.1Scgd * Read one line of standard input and produce something that looks like a
521.1Scgd * punch card.  An attempt to reimplement /usr/games/bcd.  All I looked at
531.1Scgd * was the man page.
541.1Scgd *
551.1Scgd * I couldn't find a BCD table handy so I wrote a shell script to deduce what
561.1Scgd * the patterns were that the old bcd was using for each possible 8-bit
571.1Scgd * character.  These are the results -- the low order 12 bits represent the
581.1Scgd * holes.  (A 1 bit is a hole.)  These may be wrong, but they match the old
591.1Scgd * program!
601.1Scgd *
611.1Scgd * Steve Hayman
621.1Scgd * sahayman@iuvax.cs.indiana.edu
631.1Scgd * 1989 11 30
641.1Scgd */
651.1Scgd
661.1Scgd#include <sys/types.h>
671.1Scgd#include <stdio.h>
681.1Scgd#include <ctype.h>
691.1Scgd
701.1Scgdu_short holes[256] = {
711.1Scgd    0x0,	 0x0,	  0x0,	   0x0,	    0x0,     0x0,     0x0,     0x0,
721.1Scgd    0x0,	 0x0,	  0x0,	   0x0,	    0x0,     0x0,     0x0,     0x0,
731.1Scgd    0x0,	 0x0,	  0x0,	   0x0,	    0x0,     0x0,     0x0,     0x0,
741.1Scgd    0x0,	 0x0,	  0x0,	   0x0,	    0x0,     0x0,     0x0,     0x0,
751.1Scgd    0x0,	 0x206,	  0x20a,   0x042,   0x442,   0x222,   0x800,   0x406,
761.1Scgd    0x812,	 0x412,	  0x422,   0xa00,   0x242,   0x400,   0x842,   0x300,
771.1Scgd    0x200,	 0x100,	  0x080,   0x040,   0x020,   0x010,   0x008,   0x004,
781.1Scgd    0x002,	 0x001,	  0x012,   0x40a,   0x80a,   0x212,   0x00a,   0x006,
791.1Scgd    0x022,	 0x900,	  0x880,   0x840,   0x820,   0x810,   0x808,   0x804,
801.1Scgd    0x802,	 0x801,	  0x500,   0x480,   0x440,   0x420,   0x410,   0x408,
811.3Sjtc    0x404,	 0x402,	  0x401,   0x280,   0x240,   0x220,   0x210,   0x208,
821.1Scgd    0x204,	 0x202,	  0x201,   0x082,   0x822,   0x600,   0x282,   0x30f,
831.1Scgd    0x900,	 0x880,	  0x840,   0x820,   0x810,   0x808,   0x804,   0x802,
841.1Scgd    0x801,	 0x500,	  0x480,   0x440,   0x420,   0x410,   0x408,   0x404,
851.4Sjtc    0x402,	 0x401,	  0x280,   0x240,   0x220,   0x210,   0x208,   0x204,
861.1Scgd    0x202,	 0x201,	  0x082,   0x806,   0x822,   0x600,   0x282,   0x0,
871.1Scgd    0x0,	 0x0,	  0x0,	   0x0,	    0x0,     0x0,     0x0,     0x0,
881.1Scgd    0x0,	 0x0,	  0x0,	   0x0,	    0x0,     0x0,     0x0,     0x0,
891.1Scgd    0x0,	 0x0,	  0x0,	   0x0,	    0x0,     0x0,     0x0,     0x0,
901.1Scgd    0x0,	 0x0,	  0x0,	   0x0,	    0x0,     0x0,     0x0,     0x0,
911.1Scgd    0x206,	 0x20a,	  0x042,   0x442,   0x222,   0x800,   0x406,   0x812,
921.1Scgd    0x412,	 0x422,	  0xa00,   0x242,   0x400,   0x842,   0x300,   0x200,
931.1Scgd    0x100,	 0x080,	  0x040,   0x020,   0x010,   0x008,   0x004,   0x002,
941.1Scgd    0x001,	 0x012,	  0x40a,   0x80a,   0x212,   0x00a,   0x006,   0x022,
951.1Scgd    0x900,	 0x880,	  0x840,   0x820,   0x810,   0x808,   0x804,   0x802,
961.1Scgd    0x801,	 0x500,	  0x480,   0x440,   0x420,   0x410,   0x408,   0x404,
971.3Sjtc    0x402,	 0x401,	  0x280,   0x240,   0x220,   0x210,   0x208,   0x204,
981.1Scgd    0x202,	 0x201,	  0x082,   0x806,   0x822,   0x600,   0x282,   0x30f,
991.1Scgd    0x900,	 0x880,	  0x840,   0x820,   0x810,   0x808,   0x804,   0x802,
1001.1Scgd    0x801,	 0x500,	  0x480,   0x440,   0x420,   0x410,   0x408,   0x404,
1011.4Sjtc    0x402,	 0x401,	  0x280,   0x240,   0x220,   0x210,   0x208,   0x204,
1021.1Scgd    0x202,	 0x201,	  0x082,   0x806,   0x822,   0x600,   0x282,   0x0
1031.1Scgd};
1041.1Scgd
1051.1Scgd/*
1061.1Scgd * i'th bit of w.
1071.1Scgd */
1081.1Scgd#define	bit(w,i)	((w)&(1<<(i)))
1091.1Scgd
1101.1Scgdmain(argc, argv)
1111.1Scgd	int argc;
1121.1Scgd	char **argv;
1131.1Scgd{
1141.1Scgd	char cardline[80];
1151.1Scgd
1161.1Scgd	/*
1171.1Scgd	 * The original bcd prompts with a "%" when reading from stdin,
1181.1Scgd	 * but this seems kind of silly.  So this one doesn't.
1191.1Scgd	 */
1201.1Scgd
1211.1Scgd	if (argc > 1) {
1221.1Scgd		while (--argc)
1231.1Scgd			printcard(*++argv);
1241.1Scgd	} else
1251.1Scgd		while (fgets(cardline, sizeof(cardline), stdin))
1261.1Scgd			printcard(cardline);
1271.1Scgd	exit(0);
1281.1Scgd}
1291.1Scgd
1301.1Scgd#define	COLUMNS	48
1311.1Scgd
1321.1Scgdprintcard(str)
1331.1Scgd	register char *str;
1341.1Scgd{
1351.1Scgd	static char rowchars[] = "   123456789";
1361.1Scgd	register int i, row;
1371.1Scgd	register char *p;
1381.1Scgd	char *index();
1391.1Scgd
1401.1Scgd	/* ruthlessly remove newlines and truncate at 48 characters. */
1411.1Scgd	if ((p = index(str, '\n')))
1421.1Scgd		*p = '\0';
1431.1Scgd
1441.1Scgd	if (strlen(str) > COLUMNS)
1451.1Scgd		str[COLUMNS] = '\0';
1461.1Scgd
1471.1Scgd	/* make string upper case. */
1481.1Scgd	for (p = str; *p; ++p)
1491.1Scgd		if (isascii(*p) && islower(*p))
1501.1Scgd			*p = toupper(*p);
1511.1Scgd
1521.1Scgd	 /* top of card */
1531.1Scgd	putchar(' ');
1541.1Scgd	for (i = 1; i <= COLUMNS; ++i)
1551.1Scgd		putchar('_');
1561.1Scgd	putchar('\n');
1571.1Scgd
1581.1Scgd	/*
1591.1Scgd	 * line of text.  Leave a blank if the character doesn't have
1601.1Scgd	 * a hole pattern.
1611.1Scgd	 */
1621.1Scgd	p = str;
1631.1Scgd	putchar('/');
1641.1Scgd	for (i = 1; *p; i++, p++)
1651.1Scgd		if (holes[*p])
1661.1Scgd			putchar(*p);
1671.1Scgd		else
1681.1Scgd			putchar(' ');
1691.1Scgd	while (i++ <= COLUMNS)
1701.1Scgd		putchar(' ');
1711.1Scgd	putchar('|');
1721.1Scgd	putchar('\n');
1731.1Scgd
1741.1Scgd	/*
1751.1Scgd	 * 12 rows of potential holes; output a ']', which looks kind of
1761.1Scgd	 * like a hole, if the appropriate bit is set in the holes[] table.
1771.1Scgd	 * The original bcd output a '[', a backspace, five control A's,
1781.1Scgd	 * and then a ']'.  This seems a little excessive.
1791.1Scgd	 */
1801.1Scgd	for (row = 0; row <= 11; ++row) {
1811.1Scgd		putchar('|');
1821.1Scgd		for (i = 0, p = str; *p; i++, p++) {
1831.1Scgd			if (bit(holes[*p], 11 - row))
1841.1Scgd				putchar(']');
1851.1Scgd			else
1861.1Scgd				putchar(rowchars[row]);
1871.1Scgd		}
1881.1Scgd		while (i++ < COLUMNS)
1891.1Scgd			putchar(rowchars[row]);
1901.1Scgd		putchar('|');
1911.1Scgd		putchar('\n');
1921.1Scgd	}
1931.1Scgd
1941.1Scgd	/* bottom of card */
1951.1Scgd	putchar('|');
1961.1Scgd	for (i = 1; i <= COLUMNS; i++)
1971.1Scgd		putchar('_');
1981.1Scgd	putchar('|');
1991.1Scgd	putchar('\n');
2001.1Scgd}
201