diff --git a/src/pg.c b/src/pg.c index e99c91f..72cdb2a 100644 --- a/src/pg.c +++ b/src/pg.c @@ -20,8 +20,8 @@ #include /* errno */ #include /* bool */ #include /* fclose(3), feof(3), fgetc(3), fgets(3), fopen(3), - * fprintf(3), fputc(3), stderr, stdin, stdout, EOF, FILE, - * NULL */ + * fprintf(3), fputc(3), perror(3), stderr, stdin, stdout, + * EOF, FILE, NULL */ #include /* size_t */ #include /* strchr(3), strcmp(3), strerror(3), strtok(3) */ #include /* getopt(3) */ @@ -41,7 +41,6 @@ static struct { enum { LINES = 0, BYTES = 1 } type; } default_page_unit = { 22, LINES } /* Plan 9 default */; -static FILE *input; static char *prompt = ": "; static char *program_name = "pg"; @@ -80,17 +79,15 @@ strtok_quoted(char *str, char *sep) { case '\\': /* if literal \\, permute out a backslash */ if (in_escape) { permute_out(s, i--); } - in_escape = !in_escape; - break; case '"': if (in_escape) { // \" s[i] = s[i - 1]; - permute_out(s, i--); // permute out backslash + (void)permute_out(s, i--); // permute out backslash } else if (in_quotes != -1) { - permute_out(s, in_quotes); --i; // permute out first " - permute_out(s, i--); // permute out second " + (void)permute_out(s, in_quotes); --i; // first " + (void)permute_out(s, i--); // second " in_quotes = -1; } else { in_quotes = i; } break; @@ -137,14 +134,13 @@ cmd_quit(int argc, char **argv, char **envp) { return EX_UNAVAILABLE; } static int cmd_default_page(int argc, char **argv, char **envp) { - if (argc > 1) { return EX_USAGE; } // should be impossible else if (default_page_unit.type == BYTES) { - return pg_b_u(input, default_page_unit.quantity) == EOF + return pg_b_u(stdin, default_page_unit.quantity) == EOF ? EX_UNAVAILABLE : EX_OK; } else if (default_page_unit.type == LINES) { - return pg_l_u(input, default_page_unit.quantity, '\n') == EOF + return pg_l_u(stdin, default_page_unit.quantity, '\n') == EOF ? EX_UNAVAILABLE : EX_OK; } else { return EX_SOFTWARE; } @@ -156,7 +152,7 @@ cmd_page_down_lines(int argc, char **argv, char **envp) { case 1: return cmd_default_page(argc, argv, envp); case 2: /* not implemented */ default: - fprintf(stderr, "Usage: %s" /*" (lines)"*/ "\n", argv[0]); + (void)fprintf(stderr, "Usage: %s" /*" (lines)"*/ "\n", argv[0]); return EX_USAGE; } } @@ -164,9 +160,6 @@ cmd_page_down_lines(int argc, char **argv, char **envp) { /* A CmdMap must be NULL-terminated. */ static struct CmdMap{ char *name; int (*fn)(int, char **, char **); } builtins[] = { - { "", cmd_default_page }, - { "+", cmd_page_down_lines }, - /* don't make the user feel trapped */ { ":q", cmd_quit }, { ":q!", cmd_quit }, { "exit", cmd_quit }, { "q", cmd_quit }, { "Q", cmd_quit }, @@ -203,11 +196,19 @@ cmdline_exec(struct CmdMap *map, char *cmdline, char **envp) { } } - fprintf(stderr, "%s: %s: not found\n", program_name, argv[0]); + (void)fprintf(stderr, "%s: %s: not found\n", program_name, argv[0]); return EX_USAGE; } -int usage(char *argv0) { +static int +ioerr(char *argv0) { + perror(argv0); + + return EX_IOERR; +} + +static int +usage(char *argv0) { (void)fprintf(stderr, "Usage: %s [-p prompt]\n", argv0); return EX_USAGE; @@ -217,8 +218,6 @@ int main(int argc, char *argv[]) { unsigned char cmd[CMDLINE_MAX]; FILE *t; - input = stdin; - if (argc > 0) { int c; @@ -235,7 +234,7 @@ int main(int argc, char *argv[]) { if(argc > optind) { return usage(program_name); } - if((t = fopen("/dev/tty", "rb")) == NULL){ + if ((t = fopen("/dev/tty", "rb")) == NULL) { (void)fprintf( stderr, "%s: /dev/tty: %s\n", @@ -247,9 +246,12 @@ int main(int argc, char *argv[]) { } for (;;) { - fputs(prompt, stderr); - fgets((char *)cmd, (sizeof cmd) / (sizeof *cmd), t); - if(strchr((char *)cmd, '\n') == NULL) { /* fast-forward stream */ + if ( /* prompt and receive */ + fputs(prompt, stderr) == EOF + || fgets((char *)cmd, (sizeof cmd) / (sizeof *cmd), t) == NULL + ) { return ioerr(program_name); } + + if (strchr((char *)cmd, '\n') == NULL) { /* fast-forward stream */ int c; while ((c = fgetc(t)) != '\n') { @@ -257,7 +259,7 @@ int main(int argc, char *argv[]) { } } - if(feof(t)) { return EX_OK; } + if (feof(t)) { return EX_OK; } { int r;