1
0
mirror of https://github.com/openbsd/src.git synced 2025-01-10 06:47:55 -08:00

basic decode functionality

This commit is contained in:
tedu 2014-11-06 19:35:13 +00:00
parent 38782901fa
commit 05bf2a5dec
2 changed files with 83 additions and 5 deletions

View File

@ -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

View File

@ -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;
}