gawk

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

commit 2aa849677382af4d00a99adaefa84eeecdcbb21d
parent 49b468652779baf59e0c76b34dcbef80367ca1ed
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date:   Tue, 22 Dec 2020 14:38:39 -0800

Use an array for the host, path, and port parameters

This simplifies function calls and ensures that you don't pass them
in the wrong order.

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

diff --git a/main.c b/main.c @@ -43,15 +43,25 @@ #define MY_URL_MAX 72 enum gphitem { GI_INFO, GI_PATH, GI_HOST, GI_PORT, GI_NULL }; +/* NOTE: before changing see reqtoaddr() */ +enum address { AR_PATH, AR_HOST, AR_PORT, AR_NULL }; 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 *); +typedef int (stack_command)(int, const char **, int, const char **); -int gawk(const char *, const char *, const char *); +int gawk(const char **); static int timeout = 5 * 1000; static char tmpdir[] = "/tmp/gawk-XXXXXXXXXXX"; +static const char *default_address[] = { "/", "localhost", "70" }; + +/* This exists to make it easy to track down later */ +const char ** +reqtoaddr(const char **request) +{ + return request + 1; +} FILE * wfopen(const char *path, const char *mode) @@ -292,15 +302,15 @@ gph_write_resp(int sock, const char *path) } int -gph_write(const char *host, const char *port, const char *request, const char *path) +gph_write(const char **addr, const char *path) { int sock; - sock = resolve(host, port); + sock = resolve(addr[AR_HOST], addr[AR_PORT]); if (sock == -1) return 1; - if (gph_request(sock, request) != 0 || + if (gph_request(sock, addr[AR_PATH]) != 0 || gph_write_resp(sock, path) != 0) { close(sock); return 1; @@ -417,35 +427,34 @@ fgroup(const char *cache, int argc, const char **argv, command *func) * the second the path, and the optional third the port. */ int -sgoto(int argc, const char **argv, int depth, const char *host, - const char *port, const char *path) +sgoto(int argc, const char **argv, int depth, const char **addr) { - char npath[MY_PATH_MAX]; - const char *nhost, *nport; + char tmp[MY_PATH_MAX]; + const char *newaddr[AR_NULL]; wunused(3, argc, argv); - nhost = host; - nport = port; - strcpy(npath, path); + strcpy(tmp, addr[AR_PATH]); + newaddr[AR_HOST] = addr[AR_HOST]; + newaddr[AR_PORT] = addr[AR_PORT]; + newaddr[AR_PATH] = tmp; if (argc == 0) - newpath(npath, "/"); + newpath(tmp, "/"); else if (argc == 1) - newpath(npath, argv[0]); + newpath(tmp, argv[0]); else { - newpath(npath, argv[1]); - nhost = argv[0]; + newpath(tmp, argv[1]); + newaddr[AR_HOST] = argv[0]; if (argc == 3) - nport = argv[2]; + newaddr[AR_PORT] = argv[2]; } - return gawk(nhost, npath, nport); + return gawk(newaddr); } int -sunwind(int argc, const char **argv, int depth, const char *host, - const char *port, const char *path) +sunwind(int argc, const char **argv, int depth, const char **addr) { unsigned int n; @@ -459,8 +468,7 @@ sunwind(int argc, const char **argv, int depth, const char *host, } int -sexit(int argc, const char **argv, int depth, const char *host, - const char *port, const char *path) +sexit(int argc, const char **argv, int depth, const char **addr) { wunused(0, argc, argv); return depth; @@ -499,7 +507,7 @@ cfetch(int argc, const char **argv, int i, const char **request) return 0; } - if (gph_write(request[GI_HOST], request[GI_PORT], request[GI_PATH], output) != 0) + if (gph_write(reqtoaddr(request), output) != 0) return 1; warnx("'%s/%s' written to '%s'", request[GI_HOST], request[GI_PATH], output); @@ -584,7 +592,7 @@ getstackc(int c) int execute(int argc, const char **argv, int depth, const char *cache, - const char *host, const char *path, const char *port) + const char **addr) { command *c; filter *f; @@ -614,7 +622,7 @@ invalid_statement: } if (sc != NULL) - return sc(argc - 1, argv + 1, depth, host, port, path); + return sc(argc - 1, argv + 1, depth, addr); if (f == NULL || c == NULL) goto invalid_statement; f(cache, argc - 1, argv + 1, c); @@ -622,17 +630,17 @@ invalid_statement: } void -getcache(char *cache, const char *host, const char *port, const char *path) +getcache(char *cache, const char **addr) { char tmp[MY_PATH_MAX]; - strlcpy(tmp, path, MY_PATH_MAX); + strlcpy(tmp, addr[AR_PATH], MY_PATH_MAX); tr(tmp, '/', '-'); - snprintf(cache, MY_PATH_MAX, "%s/%s-%s-%s", tmpdir, host, port, tmp); + snprintf(cache, MY_PATH_MAX, "%s/%s-%s-%s", tmpdir, addr[AR_HOST], addr[AR_PORT], tmp); } int -gawk(const char *host, const char *path, const char *port) +gawk(const char **addr) { static int depth; char *argv[MY_ARGV_MAX]; @@ -643,12 +651,12 @@ gawk(const char *host, const char *path, const char *port) int done; int mycache; - getcache(cache, host, port, path); + getcache(cache, addr); if (exists(cache)) mycache = 0; else { mycache = 1; - if (gph_write(host, port, path, cache) == 1) + if (gph_write(addr, cache) == 1) return 0; /* let the user handle it */ } @@ -656,14 +664,13 @@ gawk(const char *host, const char *path, const char *port) ++depth; done = 0; - snprintf(prompt, sizeof(prompt), "(%d) [%s] %s ", depth, host, path); + snprintf(prompt, sizeof(prompt), "(%d) [%s] %s ", depth, addr[AR_HOST], addr[AR_PATH]); while (!done && input(inbuf, sizeof(inbuf), CMD_SEP, prompt, stdin) == 0) { argc = argsplit(argv, LEN(argv), inbuf, ARG_SEP); if (argc < 0) { warnx("Too many arguments."); } else if (argc > 0) { - done = execute(argc, (const char **)argv, depth, cache, - host, path, port); + done = execute(argc, (const char **)argv, depth, cache, addr); } } @@ -690,7 +697,7 @@ main(int argc, char *argv[]) if (mkdtemp(tmpdir) == NULL) err(1, "mkdtemp '%s'", tmpdir); - status = sgoto(argc - 1, (const char **)argv + 1, 0, "localhost", "70", "/"); + status = sgoto(argc - 1, (const char **)argv + 1, 0, default_address); if (status) fprintf(stderr, "usage: %s [path] | [host] [path] [port]\n", getprogname());