diff --git a/src/pg.c b/src/pg.c index 4bcf0a0..dfa21f9 100644 --- a/src/pg.c +++ b/src/pg.c @@ -24,18 +24,14 @@ #include /* strchr(3), strcmp(3), strerror(3), strtok(3) */ #include /* getopt(3) */ -#if !defined EX_OK || !defined EX_SOFTWARE || !defined EX_USAGE -# include -#endif +/* Commands start with cmd_. They take an argc and NULL-terminated argv, like + * main, and return a status from . Return values other than EX_OK + * and EX_USAGE cause pg(1) to exit with that value, except EX_UNAVAILABLE, + * which causes pg(1) to exit with the status EX_OK. */ +#include #define CMDLINE_MAX 99+1 /* Maximum length of command line. */ -/* Done while looking at plan9ports' src/cmd/p.c. - * Taken from plan9 are the interface (minus p(1)'s shell escaping and the - * ability to page multiple files) and the practice of reading /dev/tty to get - * keyboard input without going through stdin. Also, a newline is now printed - * before querying the keyboard. */ - static char *whitespace = " \n\r\t\v"; static struct { @@ -44,13 +40,8 @@ static struct { } default_page_unit = { 22, LINES } /* Plan 9 default */; static FILE *input; static char *prompt = ": "; - static char *program_name = "pg"; -/* Commands start with cmd_. They take an argc and NULL-terminated argv, like - * main, and return one of the following RET_ enums as a status. */ -enum{ RET_OK, RET_REQSHUTDOWN, RET_USAGE, RET_NOCMD, RET_SOFTWARE }; - /* Page at most l bytes from f without consideration of a buffer (print them to * stdout). */ static int pg_b_u(FILE *f, size_t l){ @@ -75,21 +66,21 @@ static int pg_l_u(FILE *f, size_t l, char nl){ return c; } -static int cmd_quit(int argc, char **argv){ return RET_REQSHUTDOWN; } +static int cmd_quit(int argc, char **argv){ return EX_UNAVAILABLE; } static int cmd_default_page(int argc, char **argv){ if(argc > 1) /* This shouldn't be possible. */ - return RET_USAGE; + return EX_USAGE; if(default_page_unit.type == BYTES) return pg_b_u(input, default_page_unit.quantity) == EOF - ? RET_REQSHUTDOWN - : RET_OK; + ? EX_UNAVAILABLE + : EX_OK; else if(default_page_unit.type == LINES) return pg_l_u(input, default_page_unit.quantity, '\n') == EOF - ? RET_REQSHUTDOWN - : RET_OK; + ? EX_UNAVAILABLE + : EX_OK; else - return RET_SOFTWARE; + return EX_SOFTWARE; } static int cmd_page_down_lines(int argc, char **argv){ @@ -99,12 +90,12 @@ static int cmd_page_down_lines(int argc, char **argv){ case 2: /* not implemented */ default: fprintf(stderr, "Usage: %s" /*" (lines)"*/ "\n", argv[0]); - return RET_USAGE; + return EX_USAGE; } } /* A CmdMap must be NULL-terminated. */ -static struct CmdMap{ char *name; int (*fn)(int, char **); } cmd_map[] = { +static struct CmdMap{ char *name; int (*fn)(int, char **); } builtins[] = { { "", cmd_default_page }, { "+", cmd_page_down_lines }, @@ -140,7 +131,7 @@ static int cmdline_exec(struct CmdMap *map, char *cmdline){ return map->fn(argc, argv); fprintf(stderr, "%s: %s: not found\n", program_name, argv[0]); - return RET_NOCMD; + return EX_USAGE; } int usage(char *s){ @@ -173,7 +164,7 @@ int main(int argc, char *argv[]){ if((t = fopen("/dev/tty", "rb")) == NULL){ fprintf(stderr, "%s: /dev/tty: %s\n", program_name, strerror(errno)); - return EX_SOFTWARE; + return EX_OSERR; } for(;;){ @@ -190,14 +181,12 @@ int main(int argc, char *argv[]){ if(feof(t)) return EX_OK; - switch(cmdline_exec(cmd_map, (char *)cmd)){ - case RET_USAGE: case RET_NOCMD: case RET_OK: - break; - case RET_SOFTWARE: - return EX_SOFTWARE; - case RET_REQSHUTDOWN: default: - return EX_OK; - } + { int r; + switch((r = cmdline_exec(builtins, (char *)cmd))){ + case EX_OK: case EX_USAGE: break; + case EX_UNAVAILABLE: return EX_OK; + default: return r; + } } } /* UNREACHABLE */