From e7c38752c35cd621b5c314041874f1f31c8ec3e7 Mon Sep 17 00:00:00 2001 From: zrj Date: Tue, 6 Sep 2016 16:35:45 +0300 Subject: [PATCH] bcd(6): Add decoding option and few extras. While there, do the same for ppt(6). Taken-from: OpenBSD --- games/bcd/Makefile | 4 - games/bcd/bcd.6 | 49 +++++++--- games/bcd/bcd.c | 163 ++++++++++++++++++++++++++------- games/ppt/Makefile | 6 +- games/{bcd/bcd.6 => ppt/ppt.6} | 59 +++++++++--- games/ppt/ppt.c | 135 ++++++++++++++++++++++++--- 6 files changed, 330 insertions(+), 86 deletions(-) copy games/{bcd/bcd.6 => ppt/ppt.6} (67%) diff --git a/games/bcd/Makefile b/games/bcd/Makefile index 999a5cde98..d8acf7a7b1 100644 --- a/games/bcd/Makefile +++ b/games/bcd/Makefile @@ -1,10 +1,6 @@ # @(#)Makefile 8.1 (Berkeley) 5/31/93 -# $FreeBSD: src/games/bcd/Makefile,v 1.2.6.2 2001/04/25 09:28:45 ru Exp $ -# $DragonFly: src/games/bcd/Makefile,v 1.4 2006/10/08 16:22:35 pavalos Exp $ PROG= bcd MAN= bcd.6 -MLINKS= bcd.6 ppt.6 .include - diff --git a/games/bcd/bcd.6 b/games/bcd/bcd.6 index e2c2a97349..8dbead7c74 100644 --- a/games/bcd/bcd.6 +++ b/games/bcd/bcd.6 @@ -26,28 +26,49 @@ .\" SUCH DAMAGE. .\" .\" @(#)bcd.6 8.1 (Berkeley) 5/31/93 -.\" $FreeBSD: src/games/bcd/bcd.6,v 1.7.2.4 2001/03/05 18:10:20 ru Exp $ -.\" $DragonFly: src/games/bcd/bcd.6,v 1.2 2003/06/17 04:25:23 dillon Exp $ +.\" $OpenBSD: bcd.6,v 1.21 2014/11/20 01:35:47 schwarze Exp $ .\" -.Dd December 7, 2000 +.Dd September 6, 2016 .Dt BCD 6 .Os .Sh NAME -.Nm bcd , -.Nm ppt -.Nd "reformat input as punch cards or paper tape" +.Nm bcd +.Nd reformat input as punch cards .Sh SYNOPSIS .Nm +.Op Fl l .Op Ar string ... -.Nm ppt -.Op Ar string ... +.Nm +.Fl d +.Op Fl l .Sh DESCRIPTION -The commands +The .Nm -and -.Nm ppt -read the given input and reformat it in the form of punched cards or -paper tape. +command reads the given input and reformats it in the form of punched cards. Acceptable input are command line arguments or the standard input. +.Pp +The options are as follows: +.Bl -tag -width Ds +.It Fl d +Decode punch cards on the standard input back to ASCII. +.It Fl l +Create or decode 80 column punch cards. +The default is 48 columns. +.El .Sh SEE ALSO -.Xr morse 6 +.Xr ppt 6 +.Sh STANDARDS +.Rs +.%I ISO 1681:1973 +.%R "Information processing--Unpunched paper cards--Specification" +.Re +.Pp +.Rs +.%I ISO 1682:1973 +.%R "Information processing--80 columns punched paper cards--Dimensions and location of rectangular punched holes" +.Re +.Sh HISTORY +The +.Nm +command first appeared in +.Bx 3 . diff --git a/games/bcd/bcd.c b/games/bcd/bcd.c index 530d05959e..a5f98797ed 100644 --- a/games/bcd/bcd.c +++ b/games/bcd/bcd.c @@ -31,8 +31,8 @@ * * @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved. * @(#)bcd.c 8.2 (Berkeley) 3/20/94 - * $FreeBSD: src/games/bcd/bcd.c,v 1.8 1999/11/30 03:48:41 billf Exp $ - * $DragonFly: src/games/bcd/bcd.c,v 1.3 2005/08/03 13:26:19 eirikn Exp $ + * $NetBSD: bcd.c,v 1.6 1995/04/24 12:22:23 cgd Exp $ + * $OpenBSD: bcd.c,v 1.25 2016/03/07 12:07:55 mestre Exp $ */ /* @@ -65,14 +65,13 @@ * Nov 5, 1993 */ -#include - +#include #include +#include #include -#include #include -u_short holes[256] = { +static const u_short holes[256] = { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, @@ -112,54 +111,96 @@ u_short holes[256] = { */ #define bit(w,i) ((w)&(1<<(i))) -static void printcard(char *); +static void printonecard(char *, size_t); +static void printcard(char *); +static int decode(char *buf); + +static unsigned int columns = 48; int -main(int argc, char **argv) +main(int argc, char *argv[]) { - char cardline[80]; + char cardline[1024]; + int dflag = 0; + int ch; /* revoke setgid privileges */ setgid(getgid()); + while ((ch = getopt(argc, argv, "dl")) != -1) { + switch (ch) { + case 'd': + dflag = 1; + break; + case 'l': + columns = 80; + break; + default: + fprintf(stderr, "usage: %s [-l] [string ...]\n", + getprogname()); + fprintf(stderr, "usage: %s -d [-l]\n", getprogname()); + return 1; + } + } + argc -= optind; + argv += optind; + + if (dflag) { + while (decode(cardline) == 0) { + printf("%s\n", cardline); + } + return 0; + } + + /* * The original bcd prompts with a "%" when reading from stdin, * but this seems kind of silly. So this one doesn't. */ - - if (argc > 1) { - while (--argc) - printcard(*++argv); - } else + if (argc > 0) { + while (argc--) { + printcard(*argv); + argv++; + } + } else { while (fgets(cardline, sizeof(cardline), stdin)) printcard(cardline); - return (0); + } + return 0; } -#define COLUMNS 48 - -static void +void printcard(char *str) { - static char rowchars[] = " 123456789"; - int i, row; - char *p; + size_t len = strlen(str); + + while (len > 0) { + size_t amt = len > columns ? columns : len; + printonecard(str, amt); + str += amt; + len -= amt; + } +} - /* ruthlessly remove newlines and truncate at 48 characters. */ - if ((p = index(str, '\n'))) - *p = '\0'; +void +printonecard(char *str, size_t len) +{ + static const char rowchars[] = " 123456789"; + unsigned int i; + int row; + char *p, *end; - if (strlen(str) > COLUMNS) - str[COLUMNS] = '\0'; + end = str + len; /* make string upper case. */ - for (p = str; *p; ++p) + for (p = str; p < end; ++p) { if (isascii(*p) && islower(*p)) *p = toupper(*p); + } - /* top of card */ + /* top of card */ putchar(' '); - for (i = 1; i <= COLUMNS; ++i) + for (i = 1; i <= columns; ++i) putchar('_'); putchar('\n'); @@ -169,12 +210,12 @@ printcard(char *str) */ p = str; putchar('/'); - for (i = 1; *p; i++, p++) + for (i = 1; p < end; i++, p++) if (holes[(int)*p]) putchar(*p); else putchar(' '); - while (i++ <= COLUMNS) + while (i++ <= columns) putchar(' '); putchar('|'); putchar('\n'); @@ -187,13 +228,13 @@ printcard(char *str) */ for (row = 0; row <= 11; ++row) { putchar('|'); - for (i = 0, p = str; *p; i++, p++) { + for (i = 0, p = str; p < end; i++, p++) { if (bit(holes[(int)*p], 11 - row)) putchar(']'); else putchar(rowchars[row]); } - while (i++ < COLUMNS) + while (i++ < columns) putchar(rowchars[row]); putchar('|'); putchar('\n'); @@ -201,8 +242,62 @@ printcard(char *str) /* bottom of card */ putchar('|'); - for (i = 1; i <= COLUMNS; i++) + for (i = 1; i <= columns; i++) putchar('_'); putchar('|'); putchar('\n'); } + +#define LINES 12 + +int +decode(char *buf) +{ + unsigned int i; + int col; + char lines[LINES][1024]; + char tmp[1024]; + + /* top of card; if missing signal no more input */ + if (fgets(tmp, sizeof(tmp), stdin) == NULL) + return 1; + /* text line, ignored */ + if (fgets(tmp, sizeof(tmp), stdin) == NULL) + return -1; + /* twelve lines of data */ + for (i = 0; i < LINES; i++) + if (fgets(lines[i], sizeof(lines[i]), stdin) == NULL) + return -1; + /* bottom of card */ + if (fgets(tmp, sizeof(tmp), stdin) == NULL) + return -1; + + for (i = 0; i < LINES; i++) { + if (strlen(lines[i]) < columns + 2) + return -1; + if (lines[i][0] != '|' || lines[i][columns + 1] != '|') + return -1; + memmove(&lines[i][0], &lines[i][1], columns); + lines[i][columns] = 0; + } + for (col = 0; col < (int)columns; col++) { + unsigned int val = 0; + for (i = 0; i < LINES; i++) + if (lines[i][col] == ']') + val |= 1 << (11 - i); + buf[col] = ' '; + for (i = 0; i < 256; i++) + if (holes[i] == val && holes[i]) { + buf[col] = i; + break; + } + } + buf[col] = 0; + for (col = columns - 1; col >= 0; col--) { + if (buf[col] == ' ') + buf[col] = '\0'; + else + break; + } + return 0; +} diff --git a/games/ppt/Makefile b/games/ppt/Makefile index a37f21ee68..abd3a2d43b 100644 --- a/games/ppt/Makefile +++ b/games/ppt/Makefile @@ -1,10 +1,6 @@ # @(#)Makefile 8.1 (Berkeley) 5/31/93 -# -# $DragonFly: src/games/ppt/Makefile,v 1.4 2006/10/08 16:22:36 pavalos Exp $ -# PROG= ppt -NOMAN= noman +MAN= ppt.6 .include - diff --git a/games/bcd/bcd.6 b/games/ppt/ppt.6 similarity index 67% copy from games/bcd/bcd.6 copy to games/ppt/ppt.6 index e2c2a97349..e420eeab23 100644 --- a/games/bcd/bcd.6 +++ b/games/ppt/ppt.6 @@ -1,3 +1,4 @@ +.\" .\" Copyright (c) 1988, 1991, 1993 .\" The Regents of the University of California. All rights reserved. .\" @@ -26,28 +27,58 @@ .\" SUCH DAMAGE. .\" .\" @(#)bcd.6 8.1 (Berkeley) 5/31/93 -.\" $FreeBSD: src/games/bcd/bcd.6,v 1.7.2.4 2001/03/05 18:10:20 ru Exp $ -.\" $DragonFly: src/games/bcd/bcd.6,v 1.2 2003/06/17 04:25:23 dillon Exp $ +.\" $OpenBSD: ppt.6,v 1.2 2014/11/18 02:25:12 schwarze Exp $ .\" -.Dd December 7, 2000 -.Dt BCD 6 +.Dd September 6, 2016 +.Dt PPT 6 .Os .Sh NAME -.Nm bcd , .Nm ppt -.Nd "reformat input as punch cards or paper tape" +.Nd reformat input as paper tape .Sh SYNOPSIS .Nm .Op Ar string ... -.Nm ppt -.Op Ar string ... +.Nm +.Fl d +.Op Fl b .Sh DESCRIPTION -The commands +The .Nm -and -.Nm ppt -read the given input and reformat it in the form of punched cards or -paper tape. +command reads the given input and reformats it in the form of paper tape. Acceptable input are command line arguments or the standard input. +.Pp +The options are as follows: +.Bl -tag -width 7n +.It Fl d Op Fl b +Decode paper tape on the standard input back to ASCII. +Unprintable characters are visually encoded with +.Xr vis 3 +unless the +.Fl b +option (binary) is also specified. +A lowercase +.Sq x +is printed for undecipherable input lines. +.El .Sh SEE ALSO -.Xr morse 6 +.Xr bcd 6 +.Sh STANDARDS +.Rs +.%I ECMA-10 +.%R "ECMA Standard for Data Interchange on Punched Tape" +.Re +.Sh HISTORY +The +.Nm +command first appeared in +.Bx 4.3 Tahoe . +The options +.Fl d +and +.Fl b +first appeared in +.Ox 3.3 . +.Sh AUTHORS +.An Keith Bostic +.An Paul Janzen Aq Mt pjanzen@foatdi.net +.An Michael Shalayeff Aq Mt mickey@lucifier.net diff --git a/games/ppt/ppt.c b/games/ppt/ppt.c index a0f362d5d3..312f2bc9be 100644 --- a/games/ppt/ppt.c +++ b/games/ppt/ppt.c @@ -28,33 +28,105 @@ * * @(#) Copyright (c) 1988, 1993 The Regents of the University of California. All rights reserved. * @(#)ppt.c 8.1 (Berkeley) 5/31/93 - * $FreeBSD: src/games/ppt/ppt.c,v 1.7 1999/12/12 06:30:45 billf Exp $ - * $DragonFly: src/games/ppt/ppt.c,v 1.4 2005/03/18 23:20:34 swildner Exp $ + * $NetBSD: ppt.c,v 1.4 1995/03/23 08:35:40 cgd Exp $ + * $OpenBSD: ppt.c,v 1.17 2016/03/07 12:07:56 mestre Exp $ */ +#include #include #include +#include +#include +#include + +#define EDGE "___________" + +static void usage(void); static void putppt(int); +static int getppt(const char *buf); + +void +usage(void) +{ + fprintf(stderr, "usage: %s [string ...]\n", getprogname()); + fprintf(stderr, "usage: %s -d [-b]\n", getprogname()); + exit(1); +} int main(int argc, char **argv) { - int c; - char *p; - - puts("___________"); - if (argc > 1) - while ((p = *++argv)) - for (; *p != '\0'; ++p) - putppt((int)*p); - else while ((c = getchar()) != EOF) - putppt(c); - puts("___________"); - exit(0); + char *p, buf[132]; + int c, start, seenl, dflag, bflag; + + /* revoke setgid privileges */ + setgid(getgid()); + + dflag = bflag = 0; + while ((c = getopt(argc, argv, "bdh")) != -1) + switch(c) { + case 'd': + dflag = 1; + break; + case 'b': + bflag = 1; + break; + case 'h': + default: + usage(); + } + if (bflag && !dflag) + usage(); + argc -= optind; + argv += optind; + + if (dflag) { + if (argc > 0) + usage(); + + seenl = start = 0; + while (fgets(buf, sizeof(buf), stdin) != NULL) { + c = getppt(buf); + if (c == -2) + continue; + if (c == -1) { + if (start) + /* lost sync */ + putchar('x'); + continue; + } + start = 1; + if (bflag) + putchar(c); + else { + char vbuf[5]; + vis(vbuf, c, VIS_NOSLASH, 0); + fputs(vbuf, stdout); + } + seenl = (c == '\n'); + } + if (!feof(stdin)) + err(1, "fgets"); + if (!seenl && !bflag) + putchar('\n'); + } else { + puts(EDGE); + if (argc > 0) + while ((p = *argv++)) { + for (; *p != '\0'; ++p) + putppt((int)*p); + if (*argv) + putppt((int)' '); + } + else while ((c = getchar()) != EOF) + putppt(c); + puts(EDGE); + } + return 0; } -static void +void putppt(int c) { int i; @@ -71,3 +143,36 @@ putppt(int c) putchar('|'); putchar('\n'); } + +int +getppt(const char *buf) +{ + int c; + + /* Demand left-aligned paper tape, but allow comments to the right */ + if (strncmp(buf, EDGE, strlen(EDGE)) == 0) + return (-2); + if (strlen(buf) < 12 || buf[0] != '|' || buf[10] != '|' || + buf[6] != '.' || strspn(buf, "| o.") < 11) + return (-1); + + c = 0; + if (buf[1] != ' ') + c |= 0200; + if (buf[2] != ' ') + c |= 0100; + if (buf[3] != ' ') + c |= 0040; + if (buf[4] != ' ') + c |= 0020; + if (buf[5] != ' ') + c |= 0010; + if (buf[7] != ' ') + c |= 0004; + if (buf[8] != ' ') + c |= 0002; + if (buf[9] != ' ') + c |= 0001; + + return (c); +} -- 2.41.0