mirror of
https://github.com/openbsd/src.git
synced 2025-01-10 06:47:55 -08:00
allow printing longer lines than fit on a card by spilling onto more cards.
don't negatively index into the table for signed chars. ok pjanzen
This commit is contained in:
parent
aeb9def4ee
commit
50d1b26a76
@ -1,4 +1,4 @@
|
||||
/* $OpenBSD: bcd.c,v 1.13 2009/10/27 23:59:24 deraadt Exp $ */
|
||||
/* $OpenBSD: bcd.c,v 1.14 2014/11/04 17:50:23 tedu Exp $ */
|
||||
/* $NetBSD: bcd.c,v 1.6 1995/04/24 12:22:23 cgd Exp $ */
|
||||
|
||||
/*
|
||||
@ -110,44 +110,51 @@ u_short holes[256] = {
|
||||
*/
|
||||
#define bit(w,i) ((w)&(1<<(i)))
|
||||
|
||||
void printcard(char *);
|
||||
void printcard(char *, size_t);
|
||||
|
||||
#define COLUMNS 48
|
||||
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
char cardline[80];
|
||||
|
||||
/*
|
||||
* 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
|
||||
while (fgets(cardline, sizeof(cardline), stdin))
|
||||
printcard(cardline);
|
||||
while (--argc) {
|
||||
argv++;
|
||||
printcard(*argv, strlen(*argv));
|
||||
}
|
||||
} else {
|
||||
char cardline[1024];
|
||||
while (fgets(cardline, sizeof(cardline), stdin)) {
|
||||
char *p = cardline;
|
||||
size_t len = strlen(p);
|
||||
while (len > 0) {
|
||||
size_t amt = len > COLUMNS ? COLUMNS : len;
|
||||
printcard(p, amt);
|
||||
p += amt;
|
||||
len -= amt;
|
||||
}
|
||||
}
|
||||
}
|
||||
exit(0);
|
||||
}
|
||||
|
||||
#define COLUMNS 48
|
||||
|
||||
void
|
||||
printcard(char *str)
|
||||
printcard(char *str, size_t len)
|
||||
{
|
||||
static const char rowchars[] = " 123456789";
|
||||
int i, row;
|
||||
char *p;
|
||||
char *p, *end;
|
||||
|
||||
/* ruthlessly remove newlines and truncate at 48 characters. */
|
||||
str[strcspn(str, "\n")] = '\0';
|
||||
|
||||
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);
|
||||
|
||||
@ -163,8 +170,8 @@ printcard(char *str)
|
||||
*/
|
||||
p = str;
|
||||
putchar('/');
|
||||
for (i = 1; *p; i++, p++)
|
||||
if (holes[(int)*p])
|
||||
for (i = 1; p < end; i++, p++)
|
||||
if (holes[(unsigned char)*p])
|
||||
putchar(*p);
|
||||
else
|
||||
putchar(' ');
|
||||
@ -181,8 +188,8 @@ printcard(char *str)
|
||||
*/
|
||||
for (row = 0; row <= 11; ++row) {
|
||||
putchar('|');
|
||||
for (i = 0, p = str; *p; i++, p++) {
|
||||
if (bit(holes[(int)*p], 11 - row))
|
||||
for (i = 0, p = str; p < end; i++, p++) {
|
||||
if (bit(holes[(unsigned char)*p], 11 - row))
|
||||
putchar(']');
|
||||
else
|
||||
putchar(rowchars[row]);
|
||||
|
Loading…
Reference in New Issue
Block a user