gawk

[old] Sed-like interface to the Gopher protocol
Log | Files | Refs | LICENSE

commit f6775db81d8a8d18bedc151cd907c6f537551bf0
parent 2a85db865534232ed9f453b71d3be564abd06a3e
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date:   Tue, 22 Dec 2020 00:18:03 -0800

Fix bug and give commands one more parameter

Fix mixed up argv/argc increments.

Give commands an index parameter which tells them which item they
were given. Really only useful for printing commands but worth the
simplicity it has given for practically no cost.

Diffstat:
Mmain.c | 49++++++++++++++++++-------------------------------
1 file changed, 18 insertions(+), 31 deletions(-)

diff --git a/main.c b/main.c @@ -38,7 +38,7 @@ #define ARG_SEP ":\t " #define CMD_SEP ";\n" -typedef int (command)(int, const char **, const char **); +typedef int (command)(int, const char **, int, const char **); typedef int (filter)(const char *, int, const char **, command); typedef int (stack_command)(int, const char **, int, const char *, const char *, const char *); @@ -305,17 +305,15 @@ gph_write(const char *host, const char *port, const char *request, const char *p } int -splitrun(int argc, const char **argv, char *request, command *func) +splitrun(int argc, const char **argv, int index, char *request, command *func) { char *fields[5]; - if (*request == '.') - return func(argc, argv, NULL); - else if (argsplit(fields, LEN(fields), request, "\t\r\n") != 4) { + if (argsplit(fields, LEN(fields), request, "\t\r\n") != 4) { warnx("Not a valid gopher item."); return 1; } - return func(argc, argv, (const char **)fields); + return func(argc, argv, index, (const char **)fields); } int @@ -343,10 +341,12 @@ findex(const char *cache, int argc, const char **argv, command *func) more = 1; for (i = 0; more && fgets(buf, sizeof(buf), fp) != NULL; ++i) { + if (*buf == '.') + break; more = 0; for (j = 0; j < ids; ++j) if (i == indexes[j]) - splitrun(argc, argv, buf, func); /* NOTE: errors ignored */ + splitrun(argc, argv, i, buf, func); /* NOTE: errors ignored */ else if (i < indexes[j]) more = 1; } @@ -360,6 +360,7 @@ fgroup(const char *cache, int argc, const char **argv, command *func) FILE *fp; char buf[LINE_MAX]; int anytype; + int i; fp = wfopen(cache, "r"); if (fp == NULL) @@ -369,10 +370,12 @@ fgroup(const char *cache, int argc, const char **argv, command *func) anytype = 1; else anytype = 0; - while (fgets(buf, sizeof(buf), fp) != NULL) { + for (i = 0; fgets(buf, sizeof(buf), fp) != NULL; ++i) { + if (*buf == '.') + break; if (anytype || argc < 1 || strchr(argv[0], *buf)) { if (argc < 2 || strcasestr(buf, argv[1])) - splitrun(argc - 2, argv + 2, buf, func); + splitrun(argc - 2, argv + 2, i, buf, func); } } @@ -434,12 +437,10 @@ sexit(int argc, const char **argv, int depth, const char *host, } int -cprint(int argc, const char **argv, const char **s) +cprint(int argc, const char **argv, int i, const char **s) { wunused(0, argc, argv); - if (s == NULL) - return 1; if (**s == 'i') printf(" %s\n", s[GI_INFO] + 1); else @@ -448,31 +449,17 @@ cprint(int argc, const char **argv, const char **s) } int -cprintn(int argc, const char **argv, const char **s) +cprintn(int argc, const char **argv, int i, const char **s) { - /* It's a shame but the counter has no way of knowing what - * item it's on in the gopher index, therefor it's not - * useful when restricting with smatch. - */ - static int n; - - if (s == NULL) { - n = 0; - return 1; - } - printf("%4d", n++); - return cprint(argc, argv, s); - + printf("%4d", i); + return cprint(argc, argv, i, s); } int -cfetch(int argc, const char **argv, const char **request) +cfetch(int argc, const char **argv, int i, const char **request) { const char *output; - /* NOTE: perhaps some message should be printed */ - if (request == NULL) - return 1; wunused(1, argc, argv); if (argc > 0) @@ -564,7 +551,7 @@ invalid_statement: } if (sc != NULL) - return sc(argc + 1, argv - 1, depth, host, port, path); + return sc(argc - 1, argv + 1, depth, host, port, path); if (f == NULL || c == NULL) goto invalid_statement; f(cache, argc - 1, argv + 1, c);