npc(1): interpret all retvals

This commit is contained in:
dtb 2024-07-29 10:53:58 -06:00
parent 361b34c50f
commit 549fa98bdb
Signed by: trinity
GPG Key ID: 34C0543BBB6AF81B

View File

@ -17,17 +17,23 @@
* along with this program. If not, see https://www.gnu.org/licenses/. * along with this program. If not, see https://www.gnu.org/licenses/.
*/ */
#include <stdio.h> /* fprintf(3), fputs(3), getc(3), putc(3), stdin, stdout, #include <stdio.h> /* fprintf(3), fputs(3), getc(3), perror(3), putc(3), stdin,
* EOF */ * stdout, EOF */
#include <stdlib.h> /* EXIT_FAILURE */ #include <sysexits.h> /* EX_IOERR, EX_OK, EX_USAGE */
#include <unistd.h> /* getopt(3) */ #include <unistd.h> /* getopt(3) */
#include <sysexits.h> /* EX_OK, EX_USAGE */
char *program_name = "npc"; char *program_name = "npc";
static int
ioerr(char *argv0, int err) {
perror(argv0);
return EX_IOERR;
}
static int static int
usage(char *argv0) { usage(char *argv0) {
fprintf(stderr, "Usage: %s [-et]\n", argv0); (void)fprintf(stderr, "Usage: %s [-et]\n", argv0);
return EX_USAGE; return EX_USAGE;
} }
@ -52,16 +58,23 @@ int main(int argc, char *argv[]) {
if (argc > optind) { return usage(program_name); } if (argc > optind) { return usage(program_name); }
while ((c = getc(stdin)) != EOF) { while ((c = getc(stdin)) != EOF) {
if ((c & 0x80) != 0) { fputs("M-", stdout); } if ((c & 0x80) != 0 && fputs("M-", stdout) == EOF) {
return ioerr(argv[0]);
}
switch (c ^ 0x80 /* 0b 1000 0000 */) { switch (c ^ 0x80 /* 0b 1000 0000 */) {
case 0x7f: fputs("^?", stdout); break; /* delete character */ case 0x7f: /* ASCII DEL (127d) */
case '\n': if (showend) { putc('$', stdout); } if(fputs("^?", stdout) == EOF) { return ioerr(argv[0]); }
break;
case '\n':
if (showend && fputc('$', stdout) == EOF) {
return ioerr(argv[0]); }
}
default: default:
if (c >= ' ' || c == '\n' || (!showtab && c == '\t')) { if (c >= ' ' || c == '\n' || (!showtab && c == '\t')) {
putc(c, stdout); if (fputc(c, stdout) == EOF) { return ioerr(argv[0]); }
} else { } else if (fprintf(stdout, "^%c", c + '@') < 0) {
fprintf(stdout, "^%c", c + '@'); return ioerr(argv[0]);
} }
} }
} }