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

186
src/pg.c
View File

@ -16,7 +16,9 @@
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
#include <assert.h> /* assert(3) */
#include <errno.h> /* errno */
#include <stdbool.h> /* bool */
#include <stdio.h> /* fclose(3), feof(3), fgetc(3), fgets(3), fopen(3),
* fprintf(3), fputc(3), stderr, stdin, stdout, EOF, FILE,
* NULL */
@ -38,15 +40,18 @@ 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 *permute_out(char *str, size_t index){
size_t j;
for(j = index; str[j - 1] != '\0'; ++j)
str[j - 1] = str[j];
static char *
permute_out(char *str, size_t i) {
for (
;
str[i - 1] != '\0';
str[i - 1] = str[i], ++i
);
return str;
}
@ -56,47 +61,42 @@ static char *permute_out(char *str, size_t index){
* literal. The behavior of strtok_quoted when '"' or '\\' are in sep is
* undefined. */
/* 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;
size_t i;
size_t j;
if(str != NULL)
s = str;
if (str != NULL) { s = str; }
while(strchr(sep, *s) == NULL)
if(*++s == '\0')
return NULL; /* no remaining characters except seperators */
while (strchr(sep, *s) == NULL) {
if(*++s == '\0') { return NULL; } /* no remaining except seps */
}
{
char in_escape;
int in_quotes;
bool in_escape = 0;
int in_quotes = -1; /* index of quote, or -1 if none */
in_escape = 0;
in_quotes = -1;
for(i = 0; s[i] != '\0'; ++i)
switch(s[i]){
for (size_t i = 0; s[i] != '\0'; ++i)
switch (s[i]) {
case '\\':
if(in_escape)
permute_out(s, i--);
/* if literal \\, permute out a backslash */
if (in_escape) { permute_out(s, i--); }
in_escape = !in_escape;
break;
case '"':
if(in_escape){
if (in_escape) { // \"
s[i] = s[i - 1];
permute_out(s, i--);
}else if(in_quotes != -1){
permute_out(s, in_quotes);
--i;
permute_out(s, i--);
permute_out(s, i--); // permute out backslash
} else if (in_quotes != -1) {
permute_out(s, in_quotes); --i; // permute out first "
permute_out(s, i--); // permute out second "
in_quotes = -1;
}else
in_quotes = i;
} else { in_quotes = i; }
break;
case '\0':
return s;
case '\0': return s;
default:
if(!in_escape && strchr(sep, s[i]) != NULL){
if (!in_escape && strchr(sep, s[i]) != NULL) {
s[i] = '\0';
return s;
}
@ -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
* stdout). */
static int pg_b_u(FILE *f, size_t l){
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;
while ((c = fgetc(f)) != EOF) {
if ((c = fputc(c, stdout)) == EOF || --l == 0) { break; }
}
return c;
}
/* 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){
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 == nl)) == 0)
break;
while ((c = fgetc(f)) != EOF) {
if ((c = fputc(c, stdout)) == EOF || (l -= (c == nl)) == 0) { break; }
}
return c;
}
static int cmd_quit(int argc, char **argv, char **envp){return EX_UNAVAILABLE;}
static int cmd_default_page(int argc, char **argv, char **envp){
static int
cmd_quit(int argc, char **argv, char **envp) { return EX_UNAVAILABLE; }
if(argc > 1) /* This shouldn't be possible. */
return EX_USAGE;
if(default_page_unit.type == BYTES)
static int
cmd_default_page(int argc, char **argv, char **envp) {
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
? EX_UNAVAILABLE
: 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
? EX_UNAVAILABLE
: EX_OK;
else
return EX_SOFTWARE;
} else { return EX_SOFTWARE; }
}
static int cmd_page_down_lines(int argc, char **argv, char **envp){
switch(argc){
case 1:
return cmd_default_page(argc, argv, envp);
static int
cmd_page_down_lines(int argc, char **argv, char **envp) {
switch (argc) {
case 1: return cmd_default_page(argc, argv, envp);
case 2: /* not implemented */
default:
fprintf(stderr, "Usage: %s" /*" (lines)"*/ "\n", argv[0]);
@ -175,86 +178,97 @@ builtins[] = {
#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, 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
* 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];
if((argv[(argc = 0)] = strtok(cmdline, whitespace)) == NULL){
while(cmdline[0] != '\0')
cmdline = &cmdline[1];
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);
} 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)
for (; map->name != NULL; map = &map[1]) {
if(strcmp(map->name, argv[0]) == 0) {
return map->fn(argc, argv, envp);
}
}
fprintf(stderr, "%s: %s: not found\n", program_name, argv[0]);
return EX_USAGE;
}
int usage(char *s){
fprintf(stderr, "Usage: %s (-p [prompt])\n", s);
int usage(char *argv0) {
(void)fprintf(stderr, "Usage: %s [-p prompt]\n", argv0);
return EX_USAGE;
}
int main(int argc, char *argv[]){
int main(int argc, char *argv[]) {
unsigned char cmd[CMDLINE_MAX];
FILE *t;
input = stdin;
if(argc < 1)
program_name = argv[0];
else{
if (argc > 0) {
int c;
while((c = getopt(argc, argv, "p:")) != -1)
switch(c){
program_name = argv[0];
while ((c = getopt(argc, argv, "p:")) != -1) {
switch (c) {
case 'p': prompt = optarg; break;
default:
return usage(program_name);
default: return usage(program_name);
}
}
}
if(argc > optind)
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));
(void)fprintf(
stderr,
"%s: /dev/tty: %s\n",
program_name,
strerror(errno)
);
return EX_OSERR;
}
for(;;){
for (;;) {
fputs(prompt, stderr);
fgets((char *)cmd, (sizeof cmd) / (sizeof *cmd), t);
if(strchr((char *)cmd, '\n') == NULL){ /* fast-forward stream */
if(strchr((char *)cmd, '\n') == NULL) { /* fast-forward stream */
int c;
while((c = fgetc(t)) != '\n')
if(c == EOF)
break;
while ((c = fgetc(t)) != '\n') {
if (c == EOF) { break; }
}
}
if(feof(t))
return EX_OK;
if(feof(t)) { return EX_OK; }
{ int r;
switch((r = cmdline_exec(builtins, (char *)cmd, NULL))){
{
int r;
switch ((r = cmdline_exec(builtins, (char *)cmd, NULL))){
case EX_OK: case EX_USAGE: break;
case EX_UNAVAILABLE: return EX_OK;
default: return r;
} }
}
}
}
/* UNREACHABLE */
return EX_SOFTWARE;
/* UNREACHABLE */ assert(0);
}