forked from bonsai/harakit
pg(1): put command system in
This commit is contained in:
parent
ea8b58554e
commit
68919bf877
@ -11,8 +11,7 @@ pg \(en paginate
|
|||||||
|
|
||||||
.SH DESCRIPTION
|
.SH DESCRIPTION
|
||||||
|
|
||||||
Pg prints standard input to standard output, pausing every 22 lines and waiting
|
Pg prints standard input to standard output, accepting commands between pages.
|
||||||
for a line feed from the tty before continuing.
|
|
||||||
|
|
||||||
.SH DIAGNOSTICS
|
.SH DIAGNOSTICS
|
||||||
|
|
||||||
|
126
src/pg.c
126
src/pg.c
@ -16,11 +16,12 @@
|
|||||||
* along with this program. If not, see https://www.gnu.org/licenses/.
|
* along with this program. If not, see https://www.gnu.org/licenses/.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <errno.h> /* errno */
|
||||||
#include <stdio.h> /* fclose(3), feof(3), fgetc(3), fgets(3), fopen(3),
|
#include <stdio.h> /* fclose(3), feof(3), fgetc(3), fgets(3), fopen(3),
|
||||||
* fprintf(3), fputc(3), stderr, stdin, stdout, EOF, FILE,
|
* fprintf(3), fputc(3), stderr, stdin, stdout, EOF, FILE,
|
||||||
* NULL */
|
* NULL */
|
||||||
#include <stdlib.h> /* size_t */
|
#include <stdlib.h> /* size_t */
|
||||||
#include <string.h> /* strchr */
|
#include <string.h> /* strchr(3), strcmp(3), strerror(3), strtok(3) */
|
||||||
|
|
||||||
#if !defined EX_OK || !defined EX_SOFTWARE || !defined EX_USAGE
|
#if !defined EX_OK || !defined EX_SOFTWARE || !defined EX_USAGE
|
||||||
# include <sysexits.h>
|
# include <sysexits.h>
|
||||||
@ -32,45 +33,140 @@
|
|||||||
* keyboard input without going through stdin. Also, a newline is now printed
|
* keyboard input without going through stdin. Also, a newline is now printed
|
||||||
* before querying the keyboard. */
|
* 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 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;
|
int c;
|
||||||
|
|
||||||
while((c = fgetc(f)) != EOF)
|
while((c = fgetc(f)) != EOF)
|
||||||
if((c = fputc(c, stdout)) == EOF || (l -= (c == '\n')) == 0)
|
if((c = fputc(c, stdout)) == EOF || (l -= (c == '\n')) == 0)
|
||||||
break;
|
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[]){
|
int main(int argc, char *argv[]){
|
||||||
unsigned char cmd[99+1];
|
unsigned char cmd[99+1];
|
||||||
char *p;
|
int r;
|
||||||
FILE *t;
|
FILE *t;
|
||||||
|
|
||||||
if(argc == 0)
|
input = stdin;
|
||||||
argv = &program_name;
|
|
||||||
|
if(argc > 0)
|
||||||
|
program_name = argv[0];
|
||||||
|
|
||||||
if(argc > 1){
|
if(argc > 1){
|
||||||
fprintf(stderr, "Usage: %s\n", argv[0]);
|
fprintf(stderr, "Usage: %s\n", program_name);
|
||||||
return EX_USAGE;
|
return EX_USAGE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if((t = fopen("/dev/tty", "rb")) == NULL){
|
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;
|
return EX_SOFTWARE;
|
||||||
}
|
}
|
||||||
|
|
||||||
while(print_lines(stdin, l) != EOF){
|
r = 0;
|
||||||
fgets((char *)cmd, (sizeof cmd) / (sizeof *cmd), t);
|
for(;;){
|
||||||
for(p = (char *)cmd; *p != '\0' && strchr(" \t", *p) != NULL; ++p);
|
if(r != RET_REQSHUTDOWN){
|
||||||
if(feof(t) || (*p | 32) == 'q')
|
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;
|
break;
|
||||||
|
case RET_REQSHUTDOWN: case RET_SOFTWARE: default:
|
||||||
|
r = RET_REQSHUTDOWN;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
exit: fclose(t);
|
/* UNREACHABLE */
|
||||||
return EX_OK;
|
return EX_SOFTWARE;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user