diff --git a/docs/pg.1 b/docs/pg.1 index 53aced0..1a68e1c 100644 --- a/docs/pg.1 +++ b/docs/pg.1 @@ -11,8 +11,7 @@ pg \(en paginate .SH DESCRIPTION -Pg prints standard input to standard output, pausing every 22 lines and waiting -for a line feed from the tty before continuing. +Pg prints standard input to standard output, accepting commands between pages. .SH DIAGNOSTICS diff --git a/src/pg.c b/src/pg.c index 5130450..79aff64 100644 --- a/src/pg.c +++ b/src/pg.c @@ -16,11 +16,12 @@ * along with this program. If not, see https://www.gnu.org/licenses/. */ +#include /* errno */ #include /* fclose(3), feof(3), fgetc(3), fgets(3), fopen(3), * fprintf(3), fputc(3), stderr, stdin, stdout, EOF, FILE, * NULL */ #include /* size_t */ -#include /* strchr */ +#include /* strchr(3), strcmp(3), strerror(3), strtok(3) */ #if !defined EX_OK || !defined EX_SOFTWARE || !defined EX_USAGE # include @@ -32,45 +33,140 @@ * keyboard input without going through stdin. Also, a newline is now printed * before querying the keyboard. */ -static size_t l = 22; /* plan9 default */ +static char *whitespace = " \n\r\t\v"; + +static struct { + size_t quantity; + 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"; -static int print_lines(FILE *f, size_t l){ +/* 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_ARGSERROR, RET_OK, RET_NOCMD, RET_REQSHUTDOWN, RET_SOFTWARE }; + +static int page_bytes(FILE *f, size_t l){ + int c; + + while((c = fgetc(f)) != EOF) + if((c = fputc(c, stdout)) == EOF || --l == 0) + break; + + return c == EOF + ? RET_REQSHUTDOWN + : RET_OK; +} + +static int page_lines(FILE *f, size_t l){ int c; while((c = fgetc(f)) != EOF) if((c = fputc(c, stdout)) == EOF || (l -= (c == '\n')) == 0) break; - return c; + return c == EOF + ? RET_REQSHUTDOWN + : RET_OK; +} + +static int cmd_quit(int argc, char **argv){ return RET_REQSHUTDOWN; } +static int cmd_default_page(int argc, char **argv){ + + if(strtok(NULL, whitespace) != NULL) + return RET_ARGSERROR; /* This shouldn't be possible. */ + if(default_page_unit.type == BYTES) + return page_bytes(input, default_page_unit.quantity); + else if(default_page_unit.type == LINES) + return page_lines(input, default_page_unit.quantity); + else + return RET_SOFTWARE; +} + +/* A CmdMap must be NULL-terminated. */ +static struct CmdMap{ char *name; int (*fn)(int, char **); } cmd_map[] = { + { "", cmd_default_page }, + + /* don't make the user feel trapped */ + { "exit", cmd_quit }, { "q", cmd_quit }, { ":q", cmd_quit }, + { ":q!", cmd_quit }, { "quit", cmd_quit }, { "ZZ", cmd_quit }, + + { NULL, NULL } +}; + +#define ARGV_MAX 10 +/* Find and execute the command in the command map, given a corresponding + * command line. */ +static int cmdline_exec(struct CmdMap *map, char *cmdline){ + static int argc; + static char *argv[ARGV_MAX]; + + if((argv[(argc = 0)] = strtok(cmdline, whitespace)) == NULL){ + while(cmdline[0] != '\0') + cmdline = &cmdline[1]; + argv[argc] = cmdline; + argv[++argc] = NULL; + }else{ + while((argv[++argc] = strtok(NULL , whitespace)) != NULL + && argc < ARGV_MAX); + } + + for(; map->name != NULL; map = &map[1]) + if(strcmp(map->name, argv[0]) == 0) + return map->fn(argc, argv); + + fprintf(stderr, "%s: %s: not found\n", program_name, argv[0]); + return RET_NOCMD; } int main(int argc, char *argv[]){ unsigned char cmd[99+1]; - char *p; + int r; FILE *t; - if(argc == 0) - argv = &program_name; + input = stdin; + + if(argc > 0) + program_name = argv[0]; if(argc > 1){ - fprintf(stderr, "Usage: %s\n", argv[0]); + fprintf(stderr, "Usage: %s\n", program_name); return EX_USAGE; } if((t = fopen("/dev/tty", "rb")) == NULL){ - fprintf(stderr, "%s: Could not open /dev/tty\n", argv[0]); + fprintf(stderr, "%s: /dev/tty: %s\n", program_name, strerror(errno)); return EX_SOFTWARE; } - while(print_lines(stdin, l) != EOF){ - fgets((char *)cmd, (sizeof cmd) / (sizeof *cmd), t); - for(p = (char *)cmd; *p != '\0' && strchr(" \t", *p) != NULL; ++p); - if(feof(t) || (*p | 32) == 'q') + r = 0; + for(;;){ + if(r != RET_REQSHUTDOWN){ + fputs(prompt, stderr); + fgets((char *)cmd, (sizeof cmd) / (sizeof *cmd), t); + if(strchr((char *)cmd, '\n') == NULL){ /* fast-forward stream */ + int c; + + while((c = fgetc(t)) != '\n') + if(c == EOF) + break; + } + } + if(r == RET_REQSHUTDOWN || feof(t)){ + fclose(t); + return EX_OK; + } + + switch((r = cmdline_exec(cmd_map, (char *)cmd))){ + case RET_ARGSERROR: case RET_NOCMD: case RET_OK: break; + case RET_REQSHUTDOWN: case RET_SOFTWARE: default: + r = RET_REQSHUTDOWN; + } } -exit: fclose(t); - return EX_OK; + /* UNREACHABLE */ + return EX_SOFTWARE; }