summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortedu <tedu@openbsd.org>2014-11-06 19:35:13 +0000
committertedu <tedu@openbsd.org>2014-11-06 19:35:13 +0000
commit05bf2a5decefc007e5d9868a66e6c69e64b1f438 (patch)
treea7344dac5d8c7e923b9a97aa5cdfcce732934082
parentprintf debugging worked! figured out the bug (in free()) just after (diff)
downloadwireguard-openbsd-05bf2a5decefc007e5d9868a66e6c69e64b1f438.tar.xz
wireguard-openbsd-05bf2a5decefc007e5d9868a66e6c69e64b1f438.zip
basic decode functionality
-rw-r--r--games/bcd/bcd.620
-rw-r--r--games/bcd/bcd.c68
2 files changed, 83 insertions, 5 deletions
diff --git a/games/bcd/bcd.6 b/games/bcd/bcd.6
index 9f26bdfebeb..fbf2c5b164a 100644
--- a/games/bcd/bcd.6
+++ b/games/bcd/bcd.6
@@ -1,4 +1,4 @@
-.\" $OpenBSD: bcd.6,v 1.17 2013/08/14 06:32:38 jmc Exp $
+.\" $OpenBSD: bcd.6,v 1.18 2014/11/06 19:35:13 tedu Exp $
.\"
.\" Copyright (c) 1988, 1991, 1993
.\" The Regents of the University of California. All rights reserved.
@@ -29,7 +29,7 @@
.\"
.\" @(#)bcd.6 8.1 (Berkeley) 5/31/93
.\"
-.Dd $Mdocdate: August 14 2013 $
+.Dd $Mdocdate: November 6 2014 $
.Dt BCD 6
.Os
.Sh NAME
@@ -39,7 +39,10 @@
.Nd reformat input as punch cards, paper tape or morse code
.Sh SYNOPSIS
.Nm bcd
-.Op Ar string ...
+.Oo
+.Fl d Oc \*(Ba
+.Ar string ...
+.Oc
.Nm ppt
.Oo
.Fl d Oo Fl b Oc \*(Ba
@@ -59,6 +62,17 @@ punched cards, paper tape, or morse code, respectively.
Acceptable input are command line arguments or the standard input.
.Pp
Available options for
+.Nm bcd :
+.Bl -tag -width Ds
+.It Fl d
+The
+.Fl d
+option for
+.Nm bcd
+decodes punch cards on the standard input back to ASCII.
+.El
+.Pp
+Available options for
.Nm ppt :
.Bl -tag -width Ds
.It Fl d Op Fl b
diff --git a/games/bcd/bcd.c b/games/bcd/bcd.c
index fd4f3a25972..550d5de9721 100644
--- a/games/bcd/bcd.c
+++ b/games/bcd/bcd.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: bcd.c,v 1.16 2014/11/04 17:58:26 tedu Exp $ */
+/* $OpenBSD: bcd.c,v 1.17 2014/11/06 19:35:13 tedu Exp $ */
/* $NetBSD: bcd.c,v 1.6 1995/04/24 12:22:23 cgd Exp $ */
/*
@@ -112,6 +112,7 @@ u_short holes[256] = {
void printonecard(char *, size_t);
void printcard(char *);
+int decode(char *buf);
#define COLUMNS 48
@@ -119,18 +120,24 @@ void printcard(char *);
int
main(int argc, char *argv[])
{
+ char cardline[1024];
/*
* The original bcd prompts with a "%" when reading from stdin,
* but this seems kind of silly. So this one doesn't.
*/
if (argc > 1) {
+ if (strcmp(argv[1], "-d") == 0) {
+ while (decode(cardline) == 0) {
+ printf("%s\n", cardline);
+ }
+ return 0;
+ }
while (--argc) {
argv++;
printcard(*argv);
}
} else {
- char cardline[1024];
while (fgets(cardline, sizeof(cardline), stdin))
printcard(cardline);
}
@@ -163,6 +170,10 @@ printonecard(char *str, size_t len)
for (p = str; p < end; ++p)
*p = toupper((unsigned char)*p);
+ for (p = str; p < end; p++) {
+ printf("%c: %x\n", *p, holes[(unsigned char)*p]);
+ }
+
/* top of card */
putchar(' ');
for (i = 1; i <= COLUMNS; ++i)
@@ -212,3 +223,56 @@ printonecard(char *str, size_t len)
putchar('|');
putchar('\n');
}
+
+#define LINES 12
+
+int
+decode(char *buf)
+{
+ int col, i;
+ 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 < 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;
+}