gawk

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

commit 05d38cd29b52872828180923986df34304192fd8
parent 6ca5285e56b8e65f9e2bda9abb74b9771693e318
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date:   Mon, 21 Dec 2020 19:02:39 -0800

Use global and static variables to lessen paramaters

- Make use of static variables to keep track of depth in gawk()
- Use a global variable for temp dir instead of passing it up the stack

Diffstat:
Mmain.c | 43+++++++++++++++++++++++--------------------
1 file changed, 23 insertions(+), 20 deletions(-)

diff --git a/main.c b/main.c @@ -40,10 +40,11 @@ enum gphitem { GI_INFO, GI_PATH, GI_HOST, GI_PORT, GI_NULL }; -int gawk(int depth, const char *tmpdir, const char *host, const char *path, const char *port); +int gawk(const char *, const char *, const char *); static int timeout = 5 * 1000; static char *gphfmt[] = { "gophmt", NULL }; +static char tmpdir[] = "/tmp/gawk-XXXXXXXXXXX"; FILE * wfopen(const char *path, const char *mode) @@ -391,8 +392,8 @@ exfmt(int (*putter)(const char *, int, const char **), const char *path, int arg * the second the path, and the optional third the port. */ int -cgoto(int argc, const char **argv, int depth, const char *tmpdir, const char *host, - const char *path, const char *port) +cgoto(int argc, const char **argv, const char *host, const char *path, + const char *port) { char npath[PATH_MAX]; const char *nhost, *nport; @@ -414,7 +415,7 @@ cgoto(int argc, const char **argv, int depth, const char *tmpdir, const char *ho nport = argv[2]; } - return gawk(depth, tmpdir, nhost, npath, nport); + return gawk(nhost, npath, nport); } /* argv[0], if present, is the number of backtracks to make which @@ -539,7 +540,7 @@ cfetch(int argc, const char **argv, const char *cache, const char *host, const c int execute(int command, int argc, const char **argv, int depth, const char *cache, - const char *tmpdir, const char *host, const char *path, const char *port) + const char *host, const char *path, const char *port) { switch (command) { case 'U': case 'q': /* unwind the whole stack */ @@ -549,7 +550,7 @@ execute(int command, int argc, const char **argv, int depth, const char *cache, exfmt(cmatch, cache, argc, argv); return 0; case 's': case 'g': /* goto new location */ - return cgoto(argc, argv, depth, tmpdir, host, path, port); + return cgoto(argc, argv, host, path, port); case 'u': case 'b': /* unwind [n] */ return cback(argc, argv); case 'w': case 'f': /* write to file */ @@ -562,7 +563,7 @@ execute(int command, int argc, const char **argv, int depth, const char *cache, } void -getcache(char *cache, const char *tmpdir, const char *host, const char *port, const char *path) +getcache(char *cache, const char *host, const char *port, const char *path) { char tmp[PATH_MAX]; @@ -572,8 +573,9 @@ getcache(char *cache, const char *tmpdir, const char *host, const char *port, co } int -gawk(int depth, const char *tmpdir, const char *host, const char *path, const char *port) +gawk(const char *host, const char *path, const char *port) { + static int depth; char *argv[ARGV_MAX]; char *start; char cache[PATH_MAX]; @@ -583,9 +585,7 @@ gawk(int depth, const char *tmpdir, const char *host, const char *path, const ch int done; int unlinkit; - ++depth; - - getcache(cache, tmpdir, host, port, path); + getcache(cache, host, port, path); if (exists(cache)) unlinkit = 0; else { @@ -594,6 +594,9 @@ gawk(int depth, const char *tmpdir, const char *host, const char *path, const ch return 0; /* let the user handle it */ } + /* NOTE: do not return until --depth */ + ++depth; + done = 0; snprintf(prompt, sizeof(prompt), "[%s] %s ", host, path); while (!done && input(inbuf, sizeof(inbuf), CMD_SEP, prompt, stdin) == 0) { @@ -604,13 +607,14 @@ gawk(int depth, const char *tmpdir, const char *host, const char *path, const ch warnc(E2BIG, "'%c'", *start); } else { done = execute(*start, argc, - (const char **)argv, - depth, cache, tmpdir, - host, path, port); + (const char **)argv, depth, cache, host, + path, port); } } } + --depth; + if (unlinkit && unlink(cache) == -1) warn("unable to unlink '%s'", cache); if (ferror(stdin)) { @@ -626,19 +630,18 @@ gawk(int depth, const char *tmpdir, const char *host, const char *path, const ch int main(int argc, char *argv[]) { - char temp[] = "/tmp/gawk-XXXXXXXXXXX"; int status; - if (mkdtemp(temp) == NULL) - err(1, "mkdtemp '%s'", temp); + if (mkdtemp(tmpdir) == NULL) + err(1, "mkdtemp '%s'", tmpdir); - status = cgoto(argc - 1, (const char **)argv + 1, 0, temp, "localhost", "/", "70"); + status = cgoto(argc - 1, (const char **)argv + 1, "localhost", "/", "70"); if (status) fprintf(stderr, "usage: %s [path] | [host] [path] [port]\n", getprogname()); - if (rmdir(temp) == -1) - err(1, "rmdir '%s'", temp); + if (rmdir(tmpdir) == -1) + err(1, "rmdir '%s'", tmpdir); return status; }