pg(1): update style

This commit is contained in:
dtb 2024-07-31 02:40:38 -06:00
parent 383d53168f
commit 73b84f9719
Signed by: trinity
GPG Key ID: 34C0543BBB6AF81B

164
src/pg.c
View File

@ -16,7 +16,9 @@
* 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 <assert.h> /* assert(3) */
#include <errno.h> /* errno */ #include <errno.h> /* errno */
#include <stdbool.h> /* bool */
#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 */
@ -38,15 +40,18 @@ static struct {
size_t quantity; size_t quantity;
enum { LINES = 0, BYTES = 1 } type; enum { LINES = 0, BYTES = 1 } type;
} 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";
static char *permute_out(char *str, size_t index){ static char *
size_t j; permute_out(char *str, size_t i) {
for (
for(j = index; str[j - 1] != '\0'; ++j) ;
str[j - 1] = str[j]; str[i - 1] != '\0';
str[i - 1] = str[i], ++i
);
return str; return str;
} }
@ -56,45 +61,40 @@ static char *permute_out(char *str, size_t index){
* literal. The behavior of strtok_quoted when '"' or '\\' are in sep is * literal. The behavior of strtok_quoted when '"' or '\\' are in sep is
* undefined. */ * undefined. */
/* TODO: Seems to only ever return NULL. */ /* TODO: Seems to only ever return NULL. */
static char *strtok_quoted(char *str, char *sep){ static char *
strtok_quoted(char *str, char *sep) {
static char *s; static char *s;
size_t i;
size_t j;
if(str != NULL) if (str != NULL) { s = str; }
s = str;
while(strchr(sep, *s) == NULL) while (strchr(sep, *s) == NULL) {
if(*++s == '\0') if(*++s == '\0') { return NULL; } /* no remaining except seps */
return NULL; /* no remaining characters except seperators */ }
{ {
char in_escape; bool in_escape = 0;
int in_quotes; int in_quotes = -1; /* index of quote, or -1 if none */
in_escape = 0; for (size_t i = 0; s[i] != '\0'; ++i)
in_quotes = -1;
for(i = 0; s[i] != '\0'; ++i)
switch (s[i]) { switch (s[i]) {
case '\\': case '\\':
if(in_escape) /* if literal \\, permute out a backslash */
permute_out(s, i--); if (in_escape) { permute_out(s, i--); }
in_escape = !in_escape; in_escape = !in_escape;
break; break;
case '"': case '"':
if(in_escape){ if (in_escape) { // \"
s[i] = s[i - 1]; s[i] = s[i - 1];
permute_out(s, i--); permute_out(s, i--); // permute out backslash
} else if (in_quotes != -1) { } else if (in_quotes != -1) {
permute_out(s, in_quotes); permute_out(s, in_quotes); --i; // permute out first "
--i; permute_out(s, i--); // permute out second "
permute_out(s, i--);
in_quotes = -1; in_quotes = -1;
}else } else { in_quotes = i; }
in_quotes = i;
break; break;
case '\0': case '\0': return s;
return s;
default: default:
if (!in_escape && strchr(sep, s[i]) != NULL) { if (!in_escape && strchr(sep, s[i]) != NULL) {
s[i] = '\0'; s[i] = '\0';
@ -108,49 +108,52 @@ static char *strtok_quoted(char *str, char *sep){
/* 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) {
int c; int c;
while((c = fgetc(f)) != EOF) while ((c = fgetc(f)) != EOF) {
if((c = fputc(c, stdout)) == EOF || --l == 0) if ((c = fputc(c, stdout)) == EOF || --l == 0) { break; }
break; }
return c; return c;
} }
/* Page at most l lines, which are lengths of characters terminated by nl, from /* Page at most l lines, which are lengths of characters terminated by nl, from
* f without consideration of a buffer (print them to stdout). */ * f without consideration of a buffer (print them to stdout). */
static int pg_l_u(FILE *f, size_t l, char nl){ static int
pg_l_u(FILE *f, size_t l, char nl) {
int c; int c;
while((c = fgetc(f)) != EOF) while ((c = fgetc(f)) != EOF) {
if((c = fputc(c, stdout)) == EOF || (l -= (c == nl)) == 0) if ((c = fputc(c, stdout)) == EOF || (l -= (c == nl)) == 0) { break; }
break; }
return c; return c;
} }
static int cmd_quit(int argc, char **argv, char **envp){return EX_UNAVAILABLE;} static int
static int cmd_default_page(int argc, char **argv, char **envp){ cmd_quit(int argc, char **argv, char **envp) { return EX_UNAVAILABLE; }
if(argc > 1) /* This shouldn't be possible. */ static int
return EX_USAGE; cmd_default_page(int argc, char **argv, char **envp) {
if(default_page_unit.type == BYTES)
if (argc > 1) { return EX_USAGE; } // should be impossible
else 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
? EX_UNAVAILABLE ? EX_UNAVAILABLE
: EX_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
? EX_UNAVAILABLE ? EX_UNAVAILABLE
: EX_OK; : EX_OK;
else } else { return EX_SOFTWARE; }
return EX_SOFTWARE;
} }
static int cmd_page_down_lines(int argc, char **argv, char **envp){
static int
cmd_page_down_lines(int argc, char **argv, char **envp) {
switch (argc) { switch (argc) {
case 1: case 1: return cmd_default_page(argc, argv, envp);
return cmd_default_page(argc, argv, envp);
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]);
@ -175,7 +178,8 @@ builtins[] = {
#define ARGV_MAX 10 #define ARGV_MAX 10
/* Find and execute the command in the command map, given a corresponding /* Find and execute the command in the command map, given a corresponding
* command line. */ * command line. */
static int cmdline_exec(struct CmdMap *map, char *cmdline, char **envp){ static int
cmdline_exec(struct CmdMap *map, char *cmdline, char **envp) {
/* Command line word splitting is naive and based on whitespace ONLY; no /* Command line word splitting is naive and based on whitespace ONLY; no
* fancy quoting or escaping here. Adding that would (ideally) entail * fancy quoting or escaping here. Adding that would (ideally) entail
* replacing strtok(3) with something specific to this task. */ * replacing strtok(3) with something specific to this task. */
@ -183,25 +187,29 @@ static int cmdline_exec(struct CmdMap *map, char *cmdline, char **envp){
static char *argv[ARGV_MAX]; static char *argv[ARGV_MAX];
if ((argv[(argc = 0)] = strtok(cmdline, whitespace)) == NULL) { if ((argv[(argc = 0)] = strtok(cmdline, whitespace)) == NULL) {
while(cmdline[0] != '\0') while (cmdline[0] != '\0') { cmdline = &cmdline[1]; }
cmdline = &cmdline[1];
argv[argc] = cmdline; argv[argc] = cmdline;
argv[++argc] = NULL; argv[++argc] = NULL;
} else { } else {
while((argv[++argc] = strtok(NULL, whitespace)) != NULL while (
&& argc < ARGV_MAX); (argv[++argc] = strtok(NULL, whitespace)) != NULL
&& argc < ARGV_MAX
);
} }
for(; map->name != NULL; map = &map[1]) for (; map->name != NULL; map = &map[1]) {
if(strcmp(map->name, argv[0]) == 0) if(strcmp(map->name, argv[0]) == 0) {
return map->fn(argc, argv, envp); return map->fn(argc, argv, envp);
}
}
fprintf(stderr, "%s: %s: not found\n", program_name, argv[0]); fprintf(stderr, "%s: %s: not found\n", program_name, argv[0]);
return EX_USAGE; return EX_USAGE;
} }
int usage(char *s){ int usage(char *argv0) {
fprintf(stderr, "Usage: %s (-p [prompt])\n", s); (void)fprintf(stderr, "Usage: %s [-p prompt]\n", argv0);
return EX_USAGE; return EX_USAGE;
} }
@ -211,25 +219,30 @@ int main(int argc, char *argv[]){
input = stdin; input = stdin;
if(argc < 1) if (argc > 0) {
program_name = argv[0];
else{
int c; int c;
while((c = getopt(argc, argv, "p:")) != -1) program_name = argv[0];
while ((c = getopt(argc, argv, "p:")) != -1) {
switch (c) { switch (c) {
case 'p': prompt = optarg; break; case 'p': prompt = optarg; break;
default: default: return usage(program_name);
return usage(program_name); }
} }
} }
if(argc > optind) if(argc > optind) { return usage(program_name); }
return usage(program_name);
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)); (void)fprintf(
stderr,
"%s: /dev/tty: %s\n",
program_name,
strerror(errno)
);
return EX_OSERR; return EX_OSERR;
} }
@ -239,22 +252,23 @@ int main(int argc, char *argv[]){
if(strchr((char *)cmd, '\n') == NULL) { /* fast-forward stream */ if(strchr((char *)cmd, '\n') == NULL) { /* fast-forward stream */
int c; int c;
while((c = fgetc(t)) != '\n') while ((c = fgetc(t)) != '\n') {
if(c == EOF) if (c == EOF) { break; }
break; }
} }
if(feof(t)) if(feof(t)) { return EX_OK; }
return EX_OK;
{
int r;
{ int r;
switch ((r = cmdline_exec(builtins, (char *)cmd, NULL))){ switch ((r = cmdline_exec(builtins, (char *)cmd, NULL))){
case EX_OK: case EX_USAGE: break; case EX_OK: case EX_USAGE: break;
case EX_UNAVAILABLE: return EX_OK; case EX_UNAVAILABLE: return EX_OK;
default: return r; default: return r;
} } }
}
} }
/* UNREACHABLE */ /* UNREACHABLE */ assert(0);
return EX_SOFTWARE;
} }