Annotation of 43BSDReno/games/bcd/bcd.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Copyright (c) 1989 The Regents of the University of California.
                      3:  * 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 are permitted provided
                      9:  * that: (1) source distributions retain this entire copyright notice and
                     10:  * comment, and (2) distributions including binaries display the following
                     11:  * acknowledgement:  ``This product includes software developed by the
                     12:  * University of California, Berkeley and its contributors'' in the
                     13:  * documentation or other materials provided with the distribution and in
                     14:  * all advertising materials mentioning features or use of this software.
                     15:  * Neither the name of the University nor the names of its contributors may
                     16:  * be used to endorse or promote products derived from this software without
                     17:  * specific prior written permission.
                     18:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
                     19:  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
                     20:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
                     21:  */
                     22: 
                     23: #ifndef lint
                     24: char copyright[] =
                     25: "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
                     26:  All rights reserved.\n";
                     27: #endif /* not lint */
                     28: 
                     29: #ifndef lint
                     30: static char sccsid[] = "@(#)bcd.c      4.4 (Berkeley) 6/1/90";
                     31: #endif /* not lint */
                     32: 
                     33: /*
                     34:  * bcd --
                     35:  *
                     36:  * Read one line of standard input and produce something that looks like a
                     37:  * punch card.  An attempt to reimplement /usr/games/bcd.  All I looked at
                     38:  * was the man page.
                     39:  *
                     40:  * I couldn't find a BCD table handy so I wrote a shell script to deduce what
                     41:  * the patterns were that the old bcd was using for each possible 8-bit
                     42:  * character.  These are the results -- the low order 12 bits represent the
                     43:  * holes.  (A 1 bit is a hole.)  These may be wrong, but they match the old
                     44:  * program!
                     45:  *
                     46:  * Steve Hayman
                     47:  * [email protected]
                     48:  * 1989 11 30
                     49:  */
                     50: 
                     51: #include <sys/types.h>
                     52: #include <stdio.h>
                     53: #include <ctype.h>
                     54: 
                     55: u_short holes[256] = {
                     56:     0x0,        0x0,     0x0,     0x0,     0x0,     0x0,     0x0,     0x0,
                     57:     0x0,        0x0,     0x0,     0x0,     0x0,     0x0,     0x0,     0x0,
                     58:     0x0,        0x0,     0x0,     0x0,     0x0,     0x0,     0x0,     0x0,
                     59:     0x0,        0x0,     0x0,     0x0,     0x0,     0x0,     0x0,     0x0,
                     60:     0x0,        0x206,   0x20a,   0x042,   0x442,   0x222,   0x800,   0x406,
                     61:     0x812,      0x412,   0x422,   0xa00,   0x242,   0x400,   0x842,   0x300,
                     62:     0x200,      0x100,   0x080,   0x040,   0x020,   0x010,   0x008,   0x004,
                     63:     0x002,      0x001,   0x012,   0x40a,   0x80a,   0x212,   0x00a,   0x006,
                     64:     0x022,      0x900,   0x880,   0x840,   0x820,   0x810,   0x808,   0x804,
                     65:     0x802,      0x801,   0x500,   0x480,   0x440,   0x420,   0x410,   0x408,
                     66:     0x404,      0x402,   0x402,   0x280,   0x240,   0x220,   0x210,   0x208,
                     67:     0x204,      0x202,   0x201,   0x082,   0x822,   0x600,   0x282,   0x30f,
                     68:     0x900,      0x880,   0x840,   0x820,   0x810,   0x808,   0x804,   0x802,
                     69:     0x801,      0x500,   0x480,   0x440,   0x420,   0x410,   0x408,   0x404,
                     70:     0x402,      0x402,   0x280,   0x240,   0x220,   0x210,   0x208,   0x204,
                     71:     0x202,      0x201,   0x082,   0x806,   0x822,   0x600,   0x282,   0x0,
                     72:     0x0,        0x0,     0x0,     0x0,     0x0,     0x0,     0x0,     0x0,
                     73:     0x0,        0x0,     0x0,     0x0,     0x0,     0x0,     0x0,     0x0,
                     74:     0x0,        0x0,     0x0,     0x0,     0x0,     0x0,     0x0,     0x0,
                     75:     0x0,        0x0,     0x0,     0x0,     0x0,     0x0,     0x0,     0x0,
                     76:     0x206,      0x20a,   0x042,   0x442,   0x222,   0x800,   0x406,   0x812,
                     77:     0x412,      0x422,   0xa00,   0x242,   0x400,   0x842,   0x300,   0x200,
                     78:     0x100,      0x080,   0x040,   0x020,   0x010,   0x008,   0x004,   0x002,
                     79:     0x001,      0x012,   0x40a,   0x80a,   0x212,   0x00a,   0x006,   0x022,
                     80:     0x900,      0x880,   0x840,   0x820,   0x810,   0x808,   0x804,   0x802,
                     81:     0x801,      0x500,   0x480,   0x440,   0x420,   0x410,   0x408,   0x404,
                     82:     0x402,      0x402,   0x280,   0x240,   0x220,   0x210,   0x208,   0x204,
                     83:     0x202,      0x201,   0x082,   0x806,   0x822,   0x600,   0x282,   0x30f,
                     84:     0x900,      0x880,   0x840,   0x820,   0x810,   0x808,   0x804,   0x802,
                     85:     0x801,      0x500,   0x480,   0x440,   0x420,   0x410,   0x408,   0x404,
                     86:     0x402,      0x402,   0x280,   0x240,   0x220,   0x210,   0x208,   0x204,
                     87:     0x202,      0x201,   0x082,   0x806,   0x822,   0x600,   0x282,   0x0
                     88: };
                     89: 
                     90: /*
                     91:  * i'th bit of w.
                     92:  */
                     93: #define        bit(w,i)        ((w)&(1<<(i)))
                     94: 
                     95: main(argc, argv)
                     96:        int argc;
                     97:        char **argv;
                     98: {
                     99:        char cardline[80];
                    100: 
                    101:        /*
                    102:         * The original bcd prompts with a "%" when reading from stdin,
                    103:         * but this seems kind of silly.  So this one doesn't.
                    104:         */
                    105: 
                    106:        if (argc > 1) {
                    107:                while (--argc)
                    108:                        printcard(*++argv);
                    109:        } else
                    110:                while (fgets(cardline, sizeof(cardline), stdin))
                    111:                        printcard(cardline);
                    112:        exit(0);
                    113: }
                    114: 
                    115: #define        COLUMNS 48
                    116: 
                    117: printcard(str)
                    118:        register char *str;
                    119: {
                    120:        static char rowchars[] = "   123456789";
                    121:        register int i, row;
                    122:        register char *p;
                    123:        char *index();
                    124: 
                    125:        /* ruthlessly remove newlines and truncate at 48 characters. */
                    126:        if ((p = index(str, '\n')))
                    127:                *p = '\0';
                    128: 
                    129:        if (strlen(str) > COLUMNS)
                    130:                str[COLUMNS] = '\0';
                    131: 
                    132:        /* make string upper case. */
                    133:        for (p = str; *p; ++p)
                    134:                if (isascii(*p) && islower(*p))
                    135:                        *p = toupper(*p);
                    136: 
                    137:         /* top of card */
                    138:        putchar(' ');
                    139:        for (i = 1; i <= COLUMNS; ++i)
                    140:                putchar('_');
                    141:        putchar('\n');
                    142: 
                    143:        /*
                    144:         * line of text.  Leave a blank if the character doesn't have
                    145:         * a hole pattern.
                    146:         */
                    147:        p = str;
                    148:        putchar('/');
                    149:        for (i = 1; *p; i++, p++)
                    150:                if (holes[*p])
                    151:                        putchar(*p);
                    152:                else
                    153:                        putchar(' ');
                    154:        while (i++ <= COLUMNS)
                    155:                putchar(' ');
                    156:        putchar('|');
                    157:        putchar('\n');
                    158: 
                    159:        /*
                    160:         * 12 rows of potential holes; output a ']', which looks kind of
                    161:         * like a hole, if the appropriate bit is set in the holes[] table.
                    162:         * The original bcd output a '[', a backspace, five control A's,
                    163:         * and then a ']'.  This seems a little excessive.
                    164:         */
                    165:        for (row = 0; row <= 11; ++row) {
                    166:                putchar('|');
                    167:                for (i = 0, p = str; *p; i++, p++) {
                    168:                        if (bit(holes[*p], 11 - row))
                    169:                                putchar(']');
                    170:                        else
                    171:                                putchar(rowchars[row]);
                    172:                }
                    173:                while (i++ < COLUMNS)
                    174:                        putchar(rowchars[row]);
                    175:                putchar('|');
                    176:                putchar('\n');
                    177:        }
                    178: 
                    179:        /* bottom of card */
                    180:        putchar('|');
                    181:        for (i = 1; i <= COLUMNS; i++)
                    182:                putchar('_');
                    183:        putchar('|');
                    184:        putchar('\n');
                    185: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.