forked from bonsai/harakit
pg(1): Simplify exit statuses from subcommands
This commit is contained in:
parent
9c1e0d785d
commit
eb454a6dad
55
src/pg.c
55
src/pg.c
@ -24,18 +24,14 @@
|
|||||||
#include <string.h> /* strchr(3), strcmp(3), strerror(3), strtok(3) */
|
#include <string.h> /* strchr(3), strcmp(3), strerror(3), strtok(3) */
|
||||||
#include <unistd.h> /* getopt(3) */
|
#include <unistd.h> /* getopt(3) */
|
||||||
|
|
||||||
#if !defined EX_OK || !defined EX_SOFTWARE || !defined EX_USAGE
|
/* Commands start with cmd_. They take an argc and NULL-terminated argv, like
|
||||||
# include <sysexits.h>
|
* main, and return a status from <sysexits.h>. Return values other than EX_OK
|
||||||
#endif
|
* 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 <sysexits.h>
|
||||||
|
|
||||||
#define CMDLINE_MAX 99+1 /* Maximum length of command line. */
|
#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 char *whitespace = " \n\r\t\v";
|
||||||
|
|
||||||
static struct {
|
static struct {
|
||||||
@ -44,13 +40,8 @@ static struct {
|
|||||||
} default_page_unit = { 22, LINES } /* Plan 9 default */;
|
} default_page_unit = { 22, LINES } /* Plan 9 default */;
|
||||||
static FILE *input;
|
static FILE *input;
|
||||||
static char *prompt = ": ";
|
static char *prompt = ": ";
|
||||||
|
|
||||||
static char *program_name = "pg";
|
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
|
/* Page at most l bytes from f without consideration of a buffer (print them to
|
||||||
* stdout). */
|
* stdout). */
|
||||||
static int pg_b_u(FILE *f, size_t l){
|
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;
|
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){
|
static int cmd_default_page(int argc, char **argv){
|
||||||
|
|
||||||
if(argc > 1) /* This shouldn't be possible. */
|
if(argc > 1) /* This shouldn't be possible. */
|
||||||
return RET_USAGE;
|
return EX_USAGE;
|
||||||
if(default_page_unit.type == BYTES)
|
if(default_page_unit.type == BYTES)
|
||||||
return pg_b_u(input, default_page_unit.quantity) == EOF
|
return pg_b_u(input, default_page_unit.quantity) == EOF
|
||||||
? RET_REQSHUTDOWN
|
? EX_UNAVAILABLE
|
||||||
: RET_OK;
|
: EX_OK;
|
||||||
else if(default_page_unit.type == LINES)
|
else if(default_page_unit.type == LINES)
|
||||||
return pg_l_u(input, default_page_unit.quantity, '\n') == EOF
|
return pg_l_u(input, default_page_unit.quantity, '\n') == EOF
|
||||||
? RET_REQSHUTDOWN
|
? EX_UNAVAILABLE
|
||||||
: RET_OK;
|
: EX_OK;
|
||||||
else
|
else
|
||||||
return RET_SOFTWARE;
|
return EX_SOFTWARE;
|
||||||
}
|
}
|
||||||
static int cmd_page_down_lines(int argc, char **argv){
|
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 */
|
case 2: /* not implemented */
|
||||||
default:
|
default:
|
||||||
fprintf(stderr, "Usage: %s" /*" (lines)"*/ "\n", argv[0]);
|
fprintf(stderr, "Usage: %s" /*" (lines)"*/ "\n", argv[0]);
|
||||||
return RET_USAGE;
|
return EX_USAGE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* A CmdMap must be NULL-terminated. */
|
/* 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_default_page },
|
||||||
{ "+", cmd_page_down_lines },
|
{ "+", cmd_page_down_lines },
|
||||||
|
|
||||||
@ -140,7 +131,7 @@ static int cmdline_exec(struct CmdMap *map, char *cmdline){
|
|||||||
return map->fn(argc, argv);
|
return map->fn(argc, argv);
|
||||||
|
|
||||||
fprintf(stderr, "%s: %s: not found\n", program_name, argv[0]);
|
fprintf(stderr, "%s: %s: not found\n", program_name, argv[0]);
|
||||||
return RET_NOCMD;
|
return EX_USAGE;
|
||||||
}
|
}
|
||||||
|
|
||||||
int usage(char *s){
|
int usage(char *s){
|
||||||
@ -173,7 +164,7 @@ int main(int argc, char *argv[]){
|
|||||||
|
|
||||||
if((t = fopen("/dev/tty", "rb")) == NULL){
|
if((t = fopen("/dev/tty", "rb")) == NULL){
|
||||||
fprintf(stderr, "%s: /dev/tty: %s\n", program_name, strerror(errno));
|
fprintf(stderr, "%s: /dev/tty: %s\n", program_name, strerror(errno));
|
||||||
return EX_SOFTWARE;
|
return EX_OSERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
for(;;){
|
for(;;){
|
||||||
@ -190,14 +181,12 @@ int main(int argc, char *argv[]){
|
|||||||
if(feof(t))
|
if(feof(t))
|
||||||
return EX_OK;
|
return EX_OK;
|
||||||
|
|
||||||
switch(cmdline_exec(cmd_map, (char *)cmd)){
|
{ int r;
|
||||||
case RET_USAGE: case RET_NOCMD: case RET_OK:
|
switch((r = cmdline_exec(builtins, (char *)cmd))){
|
||||||
break;
|
case EX_OK: case EX_USAGE: break;
|
||||||
case RET_SOFTWARE:
|
case EX_UNAVAILABLE: return EX_OK;
|
||||||
return EX_SOFTWARE;
|
default: return r;
|
||||||
case RET_REQSHUTDOWN: default:
|
} }
|
||||||
return EX_OK;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* UNREACHABLE */
|
/* UNREACHABLE */
|
||||||
|
Loading…
Reference in New Issue
Block a user