gawk

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

commit 2590cb93cc652a98c49f57a4efb72770dd192228
parent 60ad9365f7061fe906c25d9dee119a2a07e780fd
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date:   Sun, 20 Dec 2020 13:19:59 -0800

Move command execution to another function

Diffstat:
Mmain.c | 83++++++++++++++++++++++++++++++++++++++++++++++---------------------------------
1 file changed, 48 insertions(+), 35 deletions(-)

diff --git a/main.c b/main.c @@ -325,7 +325,7 @@ gawkat(const char *path) } int -cgoto(int argc, char **argv, int depth, const char *tmpdir, const char *host, const char *path, const char *port) +cgoto(int argc, const char **argv, int depth, const char *tmpdir, const char *host, const char *path, const char *port) { char npath[PATH_MAX]; const char *nhost, *nport; @@ -349,7 +349,7 @@ cgoto(int argc, char **argv, int depth, const char *tmpdir, const char *host, co } int -cback(int argc, char **argv) +cback(int argc, const char **argv) { int back; @@ -366,11 +366,49 @@ cback(int argc, char **argv) } int +execute(int command, int argc, const char **argv, int depth, const char *cache, + const char *host, const char *path, const char *port) +{ + int done; + + switch (command) { + case 'q': + if (argc != 0) + goto too_many_args; /* break would do, but this is more clear */ + return depth; + break; + case 'p': + gawkat(cache); + return 0; + break; + case 'b': + if (argc > 1) + goto too_many_args; + return cback(argc, argv); + break; + case 'g': + if (argc > 4) + goto too_many_args; + done = cgoto(argc, argv, depth, cache, host, path, port); + if (done == 0) + gawkat(cache); + return done; + break; + default: + warnx("invalid command '%c'", command); + return 0; +too_many_args: /* this is terrible... */ + warnc(E2BIG, "'%c'", command); + return 0; + } +} + +int gawk(int depth, const char *tmpdir, const char *host, const char *path, const char *port) { char *argv[ARGV_MAX]; char cache[PATH_MAX]; - char inbuf[PATH_MAX + HOST_NAME_MAX + PROTO_MAX]; + char inbuf[ARG_MAX]; char prompt[64]; int argc; int done; @@ -391,38 +429,13 @@ gawk(int depth, const char *tmpdir, const char *host, const char *path, const ch snprintf(prompt, sizeof(prompt), "[%s] %s ", host, path); gawkat(cache); while (!done && input(inbuf, sizeof(inbuf), prompt, stdin) == 0) { - if (*inbuf == '\0') - continue; - argc = argsplit(argv, LEN(argv), inbuf + 1, ARG_SEP); - if (argc < 0) { -too_many_args: - warnx("'%c': Too many arguments.", *inbuf); - continue; - } - - switch (*inbuf) { - case 'q': - if (argc != 0) - goto too_many_args; - done = depth; - break; - case 'p': - gawkat(cache); - break; - case 'b': - if (argc > 1) - goto too_many_args; - done = cback(argc, argv); - break; - case 'g': - if (argc > 4) - goto too_many_args; - done = cgoto(argc, argv, depth, tmpdir, host, path, port); - if (done == 0) - gawkat(cache); - break; - default: - warnx("invalid command '%c'", *inbuf); + if (*inbuf != '\0') { + argc = argsplit(argv, LEN(argv), inbuf + 1, ARG_SEP); + if (argc < 0) { + warnc(E2BIG, "'%c'", *inbuf); + } else { + done = execute(*inbuf, argc, (const char **)argv, depth, cache, host, path, port); + } } }