12490Sjkh/*
22490Sjkh * Copyright (c) 1989, 1993
32490Sjkh *	The Regents of the University of California.  All rights reserved.
42490Sjkh *
52490Sjkh * This code is derived from software contributed to Berkeley by
62490Sjkh * Steve Hayman of the Indiana University Computer Science Dept.
72490Sjkh *
82490Sjkh * Redistribution and use in source and binary forms, with or without
92490Sjkh * modification, are permitted provided that the following conditions
102490Sjkh * are met:
112490Sjkh * 1. Redistributions of source code must retain the above copyright
122490Sjkh *    notice, this list of conditions and the following disclaimer.
132490Sjkh * 2. Redistributions in binary form must reproduce the above copyright
142490Sjkh *    notice, this list of conditions and the following disclaimer in the
152490Sjkh *    documentation and/or other materials provided with the distribution.
16203932Simp * 3. Neither the name of the University nor the names of its contributors
172490Sjkh *    may be used to endorse or promote products derived from this software
182490Sjkh *    without specific prior written permission.
192490Sjkh *
202490Sjkh * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
212490Sjkh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
222490Sjkh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
232490Sjkh * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
242490Sjkh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
252490Sjkh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
262490Sjkh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
272490Sjkh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
282490Sjkh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
292490Sjkh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
302490Sjkh * SUCH DAMAGE.
312490Sjkh */
322490Sjkh
332490Sjkh#ifndef lint
3453920Sbillfstatic const char copyright[] =
352490Sjkh"@(#) Copyright (c) 1989, 1993\n\
362490Sjkh	The Regents of the University of California.  All rights reserved.\n";
372490Sjkh#endif /* not lint */
382490Sjkh
392490Sjkh#ifndef lint
4053920Sbillf#if 0
412490Sjkhstatic char sccsid[] = "@(#)bcd.c	8.2 (Berkeley) 3/20/94";
4253920Sbillf#endif
4353920Sbillfstatic const char rcsid[] =
4453920Sbillf "$FreeBSD$";
452490Sjkh#endif /* not lint */
462490Sjkh
472490Sjkh/*
482490Sjkh * bcd --
492490Sjkh *
502490Sjkh * Read one line of standard input and produce something that looks like a
512490Sjkh * punch card.  An attempt to reimplement /usr/games/bcd.  All I looked at
522490Sjkh * was the man page.
532490Sjkh *
542490Sjkh * I couldn't find a BCD table handy so I wrote a shell script to deduce what
552490Sjkh * the patterns were that the old bcd was using for each possible 8-bit
562490Sjkh * character.  These are the results -- the low order 12 bits represent the
572490Sjkh * holes.  (A 1 bit is a hole.)  These may be wrong, but they match the old
582490Sjkh * program!
592490Sjkh *
602490Sjkh * Steve Hayman
612490Sjkh * sahayman@iuvax.cs.indiana.edu
622490Sjkh * 1989 11 30
632490Sjkh *
642490Sjkh *
652490Sjkh * I found an error in the table. The same error is found in the SunOS 4.1.1
662490Sjkh * version of bcd. It has apparently been around a long time. The error caused
672490Sjkh * 'Q' and 'R' to have the same punch code. I only noticed the error due to
682490Sjkh * someone pointing it out to me when the program was used to print a cover
692490Sjkh * for an APA!  The table was wrong in 4 places. The other error was masked
702490Sjkh * by the fact that the input is converted to upper case before lookup.
712490Sjkh *
722490Sjkh * Dyane Bruce
732490Sjkh * db@diana.ocunix.on.ca
742490Sjkh * Nov 5, 1993
752490Sjkh */
762490Sjkh
772490Sjkh#include <sys/types.h>
782490Sjkh
79131989Sstefanf#include <ctype.h>
802490Sjkh#include <stdio.h>
81131989Sstefanf#include <stdlib.h>
8235875Sjb#include <string.h>
83131989Sstefanf#include <unistd.h>
842490Sjkh
85227101Sedstatic u_short holes[256] = {
862490Sjkh    0x0,	 0x0,	  0x0,	   0x0,	    0x0,     0x0,     0x0,     0x0,
872490Sjkh    0x0,	 0x0,	  0x0,	   0x0,	    0x0,     0x0,     0x0,     0x0,
882490Sjkh    0x0,	 0x0,	  0x0,	   0x0,	    0x0,     0x0,     0x0,     0x0,
892490Sjkh    0x0,	 0x0,	  0x0,	   0x0,	    0x0,     0x0,     0x0,     0x0,
902490Sjkh    0x0,	 0x206,	  0x20a,   0x042,   0x442,   0x222,   0x800,   0x406,
912490Sjkh    0x812,	 0x412,	  0x422,   0xa00,   0x242,   0x400,   0x842,   0x300,
922490Sjkh    0x200,	 0x100,	  0x080,   0x040,   0x020,   0x010,   0x008,   0x004,
932490Sjkh    0x002,	 0x001,	  0x012,   0x40a,   0x80a,   0x212,   0x00a,   0x006,
942490Sjkh    0x022,	 0x900,	  0x880,   0x840,   0x820,   0x810,   0x808,   0x804,
952490Sjkh    0x802,	 0x801,	  0x500,   0x480,   0x440,   0x420,   0x410,   0x408,
962490Sjkh    0x404,	 0x402,	  0x401,   0x280,   0x240,   0x220,   0x210,   0x208,
972490Sjkh    0x204,	 0x202,	  0x201,   0x082,   0x822,   0x600,   0x282,   0x30f,
982490Sjkh    0x900,	 0x880,	  0x840,   0x820,   0x810,   0x808,   0x804,   0x802,
992490Sjkh    0x801,	 0x500,	  0x480,   0x440,   0x420,   0x410,   0x408,   0x404,
1002490Sjkh    0x402,	 0x401,	  0x280,   0x240,   0x220,   0x210,   0x208,   0x204,
1012490Sjkh    0x202,	 0x201,	  0x082,   0x806,   0x822,   0x600,   0x282,   0x0,
1022490Sjkh    0x0,	 0x0,	  0x0,	   0x0,	    0x0,     0x0,     0x0,     0x0,
1032490Sjkh    0x0,	 0x0,	  0x0,	   0x0,	    0x0,     0x0,     0x0,     0x0,
1042490Sjkh    0x0,	 0x0,	  0x0,	   0x0,	    0x0,     0x0,     0x0,     0x0,
1052490Sjkh    0x0,	 0x0,	  0x0,	   0x0,	    0x0,     0x0,     0x0,     0x0,
1062490Sjkh    0x206,	 0x20a,	  0x042,   0x442,   0x222,   0x800,   0x406,   0x812,
1072490Sjkh    0x412,	 0x422,	  0xa00,   0x242,   0x400,   0x842,   0x300,   0x200,
1082490Sjkh    0x100,	 0x080,	  0x040,   0x020,   0x010,   0x008,   0x004,   0x002,
1092490Sjkh    0x001,	 0x012,	  0x40a,   0x80a,   0x212,   0x00a,   0x006,   0x022,
1102490Sjkh    0x900,	 0x880,	  0x840,   0x820,   0x810,   0x808,   0x804,   0x802,
1112490Sjkh    0x801,	 0x500,	  0x480,   0x440,   0x420,   0x410,   0x408,   0x404,
1122490Sjkh    0x402,	 0x401,	  0x280,   0x240,   0x220,   0x210,   0x208,   0x204,
1132490Sjkh    0x202,	 0x201,	  0x082,   0x806,   0x822,   0x600,   0x282,   0x30f,
1142490Sjkh    0x900,	 0x880,	  0x840,   0x820,   0x810,   0x808,   0x804,   0x802,
1152490Sjkh    0x801,	 0x500,	  0x480,   0x440,   0x420,   0x410,   0x408,   0x404,
1162490Sjkh    0x402,	 0x401,	  0x280,   0x240,   0x220,   0x210,   0x208,   0x204,
1172490Sjkh    0x202,	 0x201,	  0x082,   0x806,   0x822,   0x600,   0x282,   0x0
1182490Sjkh};
1192490Sjkh
120227101Sedstatic void printcard(char *);
121131989Sstefanf
1222490Sjkh/*
1232490Sjkh * i'th bit of w.
1242490Sjkh */
1252490Sjkh#define	bit(w,i)	((w)&(1<<(i)))
1262490Sjkh
1272490Sjkhint
128131989Sstefanfmain(int argc, char **argv)
1292490Sjkh{
1302490Sjkh	char cardline[80];
1312490Sjkh
1322490Sjkh	/*
1332490Sjkh	 * The original bcd prompts with a "%" when reading from stdin,
1342490Sjkh	 * but this seems kind of silly.  So this one doesn't.
1352490Sjkh	 */
1362490Sjkh
1372490Sjkh	if (argc > 1) {
1382490Sjkh		while (--argc)
1392490Sjkh			printcard(*++argv);
1402490Sjkh	} else
1412490Sjkh		while (fgets(cardline, sizeof(cardline), stdin))
1422490Sjkh			printcard(cardline);
1432490Sjkh	exit(0);
1442490Sjkh}
1452490Sjkh
1462490Sjkh#define	COLUMNS	48
1472490Sjkh
148227101Sedstatic void
149131989Sstefanfprintcard(char *str)
1502490Sjkh{
1512490Sjkh	static char rowchars[] = "   123456789";
15253210Sbillf	int i, row;
15353210Sbillf	char *p;
1542490Sjkh
1552490Sjkh	/* ruthlessly remove newlines and truncate at 48 characters. */
156131989Sstefanf	if ((p = strchr(str, '\n')))
1572490Sjkh		*p = '\0';
1582490Sjkh
1592490Sjkh	if (strlen(str) > COLUMNS)
1602490Sjkh		str[COLUMNS] = '\0';
1612490Sjkh
1622490Sjkh	/* make string upper case. */
1632490Sjkh	for (p = str; *p; ++p)
1642490Sjkh		if (isascii(*p) && islower(*p))
1652490Sjkh			*p = toupper(*p);
1662490Sjkh
1672490Sjkh	 /* top of card */
1682490Sjkh	putchar(' ');
1692490Sjkh	for (i = 1; i <= COLUMNS; ++i)
1702490Sjkh		putchar('_');
1712490Sjkh	putchar('\n');
1722490Sjkh
1732490Sjkh	/*
1742490Sjkh	 * line of text.  Leave a blank if the character doesn't have
1752490Sjkh	 * a hole pattern.
1762490Sjkh	 */
1772490Sjkh	p = str;
1782490Sjkh	putchar('/');
1792490Sjkh	for (i = 1; *p; i++, p++)
180131989Sstefanf		if (holes[(unsigned char)*p])
1812490Sjkh			putchar(*p);
1822490Sjkh		else
1832490Sjkh			putchar(' ');
1842490Sjkh	while (i++ <= COLUMNS)
1852490Sjkh		putchar(' ');
1862490Sjkh	putchar('|');
1872490Sjkh	putchar('\n');
1882490Sjkh
1892490Sjkh	/*
1902490Sjkh	 * 12 rows of potential holes; output a ']', which looks kind of
1912490Sjkh	 * like a hole, if the appropriate bit is set in the holes[] table.
1922490Sjkh	 * The original bcd output a '[', a backspace, five control A's,
1932490Sjkh	 * and then a ']'.  This seems a little excessive.
1942490Sjkh	 */
1952490Sjkh	for (row = 0; row <= 11; ++row) {
1962490Sjkh		putchar('|');
1972490Sjkh		for (i = 0, p = str; *p; i++, p++) {
198131989Sstefanf			if (bit(holes[(unsigned char)*p], 11 - row))
1992490Sjkh				putchar(']');
2002490Sjkh			else
2012490Sjkh				putchar(rowchars[row]);
2022490Sjkh		}
2032490Sjkh		while (i++ < COLUMNS)
2042490Sjkh			putchar(rowchars[row]);
2052490Sjkh		putchar('|');
2062490Sjkh		putchar('\n');
2072490Sjkh	}
2082490Sjkh
2092490Sjkh	/* bottom of card */
2102490Sjkh	putchar('|');
2112490Sjkh	for (i = 1; i <= COLUMNS; i++)
2122490Sjkh		putchar('_');
2132490Sjkh	putchar('|');
2142490Sjkh	putchar('\n');
2152490Sjkh}
216