pg(1): finish style changes
This commit is contained in:
parent
73b84f9719
commit
c01af89e52
50
src/pg.c
50
src/pg.c
@ -20,8 +20,8 @@
|
|||||||
#include <errno.h> /* errno */
|
#include <errno.h> /* errno */
|
||||||
#include <stdbool.h> /* bool */
|
#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), perror(3), stderr, stdin, stdout,
|
||||||
* NULL */
|
* EOF, FILE, NULL */
|
||||||
#include <stdlib.h> /* size_t */
|
#include <stdlib.h> /* size_t */
|
||||||
#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) */
|
||||||
@ -41,7 +41,6 @@ static struct {
|
|||||||
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 char *prompt = ": ";
|
static char *prompt = ": ";
|
||||||
static char *program_name = "pg";
|
static char *program_name = "pg";
|
||||||
|
|
||||||
@ -80,17 +79,15 @@ strtok_quoted(char *str, char *sep) {
|
|||||||
case '\\':
|
case '\\':
|
||||||
/* if literal \\, permute out a backslash */
|
/* if literal \\, permute out a backslash */
|
||||||
if (in_escape) { 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 backslash
|
(void)permute_out(s, i--); // permute out backslash
|
||||||
} else if (in_quotes != -1) {
|
} else if (in_quotes != -1) {
|
||||||
permute_out(s, in_quotes); --i; // permute out first "
|
(void)permute_out(s, in_quotes); --i; // first "
|
||||||
permute_out(s, i--); // permute out second "
|
(void)permute_out(s, i--); // second "
|
||||||
in_quotes = -1;
|
in_quotes = -1;
|
||||||
} else { in_quotes = i; }
|
} else { in_quotes = i; }
|
||||||
break;
|
break;
|
||||||
@ -137,14 +134,13 @@ 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_default_page(int argc, char **argv, char **envp) {
|
||||||
|
|
||||||
if (argc > 1) { return EX_USAGE; } // should be impossible
|
if (argc > 1) { return EX_USAGE; } // should be impossible
|
||||||
else if (default_page_unit.type == BYTES) {
|
else if (default_page_unit.type == BYTES) {
|
||||||
return pg_b_u(input, default_page_unit.quantity) == EOF
|
return pg_b_u(stdin, 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(stdin, default_page_unit.quantity, '\n') == EOF
|
||||||
? EX_UNAVAILABLE
|
? EX_UNAVAILABLE
|
||||||
: EX_OK;
|
: EX_OK;
|
||||||
} else { return EX_SOFTWARE; }
|
} else { return EX_SOFTWARE; }
|
||||||
@ -156,7 +152,7 @@ cmd_page_down_lines(int argc, char **argv, char **envp) {
|
|||||||
case 1: return cmd_default_page(argc, argv, envp);
|
case 1: 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]);
|
(void)fprintf(stderr, "Usage: %s" /*" (lines)"*/ "\n", argv[0]);
|
||||||
return EX_USAGE;
|
return EX_USAGE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -164,9 +160,6 @@ cmd_page_down_lines(int argc, char **argv, char **envp) {
|
|||||||
/* A CmdMap must be NULL-terminated. */
|
/* A CmdMap must be NULL-terminated. */
|
||||||
static struct CmdMap{ char *name; int (*fn)(int, char **, char **); }
|
static struct CmdMap{ char *name; int (*fn)(int, char **, char **); }
|
||||||
builtins[] = {
|
builtins[] = {
|
||||||
{ "", cmd_default_page },
|
|
||||||
{ "+", cmd_page_down_lines },
|
|
||||||
|
|
||||||
/* don't make the user feel trapped */
|
/* don't make the user feel trapped */
|
||||||
{ ":q", cmd_quit }, { ":q!", cmd_quit },
|
{ ":q", cmd_quit }, { ":q!", cmd_quit },
|
||||||
{ "exit", cmd_quit }, { "q", cmd_quit }, { "Q", cmd_quit },
|
{ "exit", cmd_quit }, { "q", cmd_quit }, { "Q", cmd_quit },
|
||||||
@ -203,11 +196,19 @@ cmdline_exec(struct CmdMap *map, char *cmdline, char **envp) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(stderr, "%s: %s: not found\n", program_name, argv[0]);
|
(void)fprintf(stderr, "%s: %s: not found\n", program_name, argv[0]);
|
||||||
return EX_USAGE;
|
return EX_USAGE;
|
||||||
}
|
}
|
||||||
|
|
||||||
int usage(char *argv0) {
|
static int
|
||||||
|
ioerr(char *argv0) {
|
||||||
|
perror(argv0);
|
||||||
|
|
||||||
|
return EX_IOERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
usage(char *argv0) {
|
||||||
(void)fprintf(stderr, "Usage: %s [-p prompt]\n", argv0);
|
(void)fprintf(stderr, "Usage: %s [-p prompt]\n", argv0);
|
||||||
|
|
||||||
return EX_USAGE;
|
return EX_USAGE;
|
||||||
@ -217,8 +218,6 @@ int main(int argc, char *argv[]) {
|
|||||||
unsigned char cmd[CMDLINE_MAX];
|
unsigned char cmd[CMDLINE_MAX];
|
||||||
FILE *t;
|
FILE *t;
|
||||||
|
|
||||||
input = stdin;
|
|
||||||
|
|
||||||
if (argc > 0) {
|
if (argc > 0) {
|
||||||
int c;
|
int c;
|
||||||
|
|
||||||
@ -235,7 +234,7 @@ int main(int argc, char *argv[]) {
|
|||||||
if(argc > optind) { return usage(program_name); }
|
if(argc > optind) { return usage(program_name); }
|
||||||
|
|
||||||
|
|
||||||
if((t = fopen("/dev/tty", "rb")) == NULL){
|
if ((t = fopen("/dev/tty", "rb")) == NULL) {
|
||||||
(void)fprintf(
|
(void)fprintf(
|
||||||
stderr,
|
stderr,
|
||||||
"%s: /dev/tty: %s\n",
|
"%s: /dev/tty: %s\n",
|
||||||
@ -247,9 +246,12 @@ int main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
fputs(prompt, stderr);
|
if ( /* prompt and receive */
|
||||||
fgets((char *)cmd, (sizeof cmd) / (sizeof *cmd), t);
|
fputs(prompt, stderr) == EOF
|
||||||
if(strchr((char *)cmd, '\n') == NULL) { /* fast-forward stream */
|
|| fgets((char *)cmd, (sizeof cmd) / (sizeof *cmd), t) == NULL
|
||||||
|
) { return ioerr(program_name); }
|
||||||
|
|
||||||
|
if (strchr((char *)cmd, '\n') == NULL) { /* fast-forward stream */
|
||||||
int c;
|
int c;
|
||||||
|
|
||||||
while ((c = fgetc(t)) != '\n') {
|
while ((c = fgetc(t)) != '\n') {
|
||||||
@ -257,7 +259,7 @@ int main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(feof(t)) { return EX_OK; }
|
if (feof(t)) { return EX_OK; }
|
||||||
|
|
||||||
{
|
{
|
||||||
int r;
|
int r;
|
||||||
|
Loading…
Reference in New Issue
Block a user