forked from bonsai/harakit
pg(1): add -p option
This commit is contained in:
parent
68919bf877
commit
9c1e0d785d
13
docs/pg.1
13
docs/pg.1
@ -9,10 +9,23 @@
|
||||
|
||||
pg \(en paginate
|
||||
|
||||
.SH SYNOPSIS
|
||||
|
||||
pg
|
||||
.RB ( -p
|
||||
.RB [ prompt ])
|
||||
|
||||
.SH DESCRIPTION
|
||||
|
||||
Pg prints standard input to standard output, accepting commands between pages.
|
||||
|
||||
.SH OPTIONS
|
||||
|
||||
.B -p
|
||||
.RS
|
||||
Replaces the default prompt (": ") with the next argument.
|
||||
.RE
|
||||
|
||||
.SH DIAGNOSTICS
|
||||
|
||||
Pg returns an unsuccessful exit code if the tty couldn't be opened or if pg was
|
||||
|
97
src/pg.c
97
src/pg.c
@ -22,11 +22,14 @@
|
||||
* NULL */
|
||||
#include <stdlib.h> /* size_t */
|
||||
#include <string.h> /* strchr(3), strcmp(3), strerror(3), strtok(3) */
|
||||
#include <unistd.h> /* getopt(3) */
|
||||
|
||||
#if !defined EX_OK || !defined EX_SOFTWARE || !defined EX_USAGE
|
||||
# include <sysexits.h>
|
||||
#endif
|
||||
|
||||
#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
|
||||
@ -46,48 +49,64 @@ 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_ARGSERROR, RET_OK, RET_NOCMD, RET_REQSHUTDOWN, RET_SOFTWARE };
|
||||
enum{ RET_OK, RET_REQSHUTDOWN, RET_USAGE, RET_NOCMD, RET_SOFTWARE };
|
||||
|
||||
static int page_bytes(FILE *f, size_t l){
|
||||
/* 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){
|
||||
int c;
|
||||
|
||||
while((c = fgetc(f)) != EOF)
|
||||
if((c = fputc(c, stdout)) == EOF || --l == 0)
|
||||
break;
|
||||
|
||||
return c == EOF
|
||||
? RET_REQSHUTDOWN
|
||||
: RET_OK;
|
||||
return c;
|
||||
}
|
||||
|
||||
static int page_lines(FILE *f, size_t l){
|
||||
/* Page at most l lines, which are lengths of characters terminated by nl, from
|
||||
* f without consideration of a buffer (print them to stdout). */
|
||||
static int pg_l_u(FILE *f, size_t l, char nl){
|
||||
int c;
|
||||
|
||||
while((c = fgetc(f)) != EOF)
|
||||
if((c = fputc(c, stdout)) == EOF || (l -= (c == '\n')) == 0)
|
||||
if((c = fputc(c, stdout)) == EOF || (l -= (c == nl)) == 0)
|
||||
break;
|
||||
|
||||
return c == EOF
|
||||
? RET_REQSHUTDOWN
|
||||
: RET_OK;
|
||||
return c;
|
||||
}
|
||||
|
||||
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(argc > 1) /* This shouldn't be possible. */
|
||||
return RET_USAGE;
|
||||
if(default_page_unit.type == BYTES)
|
||||
return page_bytes(input, default_page_unit.quantity);
|
||||
return pg_b_u(input, default_page_unit.quantity) == EOF
|
||||
? RET_REQSHUTDOWN
|
||||
: RET_OK;
|
||||
else if(default_page_unit.type == LINES)
|
||||
return page_lines(input, default_page_unit.quantity);
|
||||
return pg_l_u(input, default_page_unit.quantity, '\n') == EOF
|
||||
? RET_REQSHUTDOWN
|
||||
: RET_OK;
|
||||
else
|
||||
return RET_SOFTWARE;
|
||||
}
|
||||
static int cmd_page_down_lines(int argc, char **argv){
|
||||
|
||||
switch(argc){
|
||||
case 1:
|
||||
return cmd_default_page(argc, argv);
|
||||
case 2: /* not implemented */
|
||||
default:
|
||||
fprintf(stderr, "Usage: %s" /*" (lines)"*/ "\n", argv[0]);
|
||||
return RET_USAGE;
|
||||
}
|
||||
}
|
||||
|
||||
/* A CmdMap must be NULL-terminated. */
|
||||
static struct CmdMap{ char *name; int (*fn)(int, char **); } cmd_map[] = {
|
||||
{ "", cmd_default_page },
|
||||
{ "+", cmd_page_down_lines },
|
||||
|
||||
/* don't make the user feel trapped */
|
||||
{ "exit", cmd_quit }, { "q", cmd_quit }, { ":q", cmd_quit },
|
||||
@ -100,6 +119,9 @@ static struct CmdMap{ char *name; int (*fn)(int, char **); } cmd_map[] = {
|
||||
/* Find and execute the command in the command map, given a corresponding
|
||||
* command line. */
|
||||
static int cmdline_exec(struct CmdMap *map, char *cmdline){
|
||||
/* Command line word splitting is naive and based on whitespace ONLY; no
|
||||
* fancy quoting or escaping here. Adding that would (ideally) entail
|
||||
* replacing strtok(3) with something specific to this task. */
|
||||
static int argc;
|
||||
static char *argv[ARGV_MAX];
|
||||
|
||||
@ -109,7 +131,7 @@ static int cmdline_exec(struct CmdMap *map, char *cmdline){
|
||||
argv[argc] = cmdline;
|
||||
argv[++argc] = NULL;
|
||||
}else{
|
||||
while((argv[++argc] = strtok(NULL , whitespace)) != NULL
|
||||
while((argv[++argc] = strtok(NULL, whitespace)) != NULL
|
||||
&& argc < ARGV_MAX);
|
||||
}
|
||||
|
||||
@ -121,29 +143,40 @@ static int cmdline_exec(struct CmdMap *map, char *cmdline){
|
||||
return RET_NOCMD;
|
||||
}
|
||||
|
||||
int usage(char *s){
|
||||
fprintf(stderr, "Usage: %s (-p [prompt])\n", s);
|
||||
return EX_USAGE;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]){
|
||||
unsigned char cmd[99+1];
|
||||
int r;
|
||||
unsigned char cmd[CMDLINE_MAX];
|
||||
FILE *t;
|
||||
|
||||
input = stdin;
|
||||
|
||||
if(argc > 0)
|
||||
if(argc < 1)
|
||||
program_name = argv[0];
|
||||
else{
|
||||
int c;
|
||||
|
||||
if(argc > 1){
|
||||
fprintf(stderr, "Usage: %s\n", program_name);
|
||||
return EX_USAGE;
|
||||
while((c = getopt(argc, argv, "p:")) != -1)
|
||||
switch(c){
|
||||
case 'p': prompt = optarg; break;
|
||||
default:
|
||||
return usage(program_name);
|
||||
}
|
||||
}
|
||||
|
||||
if(argc > optind)
|
||||
return usage(program_name);
|
||||
|
||||
|
||||
if((t = fopen("/dev/tty", "rb")) == NULL){
|
||||
fprintf(stderr, "%s: /dev/tty: %s\n", program_name, strerror(errno));
|
||||
return EX_SOFTWARE;
|
||||
}
|
||||
|
||||
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 */
|
||||
@ -153,17 +186,17 @@ int main(int argc, char *argv[]){
|
||||
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:
|
||||
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_REQSHUTDOWN: case RET_SOFTWARE: default:
|
||||
r = RET_REQSHUTDOWN;
|
||||
case RET_SOFTWARE:
|
||||
return EX_SOFTWARE;
|
||||
case RET_REQSHUTDOWN: default:
|
||||
return EX_OK;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user