npc(1): interpret all retvals
This commit is contained in:
parent
361b34c50f
commit
549fa98bdb
35
src/npc.c
35
src/npc.c
@ -17,17 +17,23 @@
|
||||
* 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,
|
||||
* EOF */
|
||||
#include <stdlib.h> /* EXIT_FAILURE */
|
||||
#include <stdio.h> /* fprintf(3), fputs(3), getc(3), perror(3), putc(3), stdin,
|
||||
* stdout, EOF */
|
||||
#include <sysexits.h> /* EX_IOERR, EX_OK, EX_USAGE */
|
||||
#include <unistd.h> /* getopt(3) */
|
||||
#include <sysexits.h> /* EX_OK, EX_USAGE */
|
||||
|
||||
char *program_name = "npc";
|
||||
|
||||
static int
|
||||
ioerr(char *argv0, int err) {
|
||||
perror(argv0);
|
||||
|
||||
return EX_IOERR;
|
||||
}
|
||||
|
||||
static int
|
||||
usage(char *argv0) {
|
||||
fprintf(stderr, "Usage: %s [-et]\n", argv0);
|
||||
(void)fprintf(stderr, "Usage: %s [-et]\n", argv0);
|
||||
|
||||
return EX_USAGE;
|
||||
}
|
||||
@ -52,16 +58,23 @@ int main(int argc, char *argv[]) {
|
||||
if (argc > optind) { return usage(program_name); }
|
||||
|
||||
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 */) {
|
||||
case 0x7f: fputs("^?", stdout); break; /* delete character */
|
||||
case '\n': if (showend) { putc('$', stdout); }
|
||||
case 0x7f: /* ASCII DEL (127d) */
|
||||
if(fputs("^?", stdout) == EOF) { return ioerr(argv[0]); }
|
||||
break;
|
||||
case '\n':
|
||||
if (showend && fputc('$', stdout) == EOF) {
|
||||
return ioerr(argv[0]); }
|
||||
}
|
||||
default:
|
||||
if (c >= ' ' || c == '\n' || (!showtab && c == '\t')) {
|
||||
putc(c, stdout);
|
||||
} else {
|
||||
fprintf(stdout, "^%c", c + '@');
|
||||
if (fputc(c, stdout) == EOF) { return ioerr(argv[0]); }
|
||||
} else if (fprintf(stdout, "^%c", c + '@') < 0) {
|
||||
return ioerr(argv[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user