gawk

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

commit 83d25f33ff40201e4d45af45066143deb78465f5
parent 28f1dab416f6ebc2a1faff0232af8505807eba1b
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date:   Sun, 20 Dec 2020 19:29:21 -0800

Prepare for more printing commands

Now efmt, formerly gawkat, takes a printing function as an argument
so it can be reused.

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

diff --git a/main.c b/main.c @@ -205,7 +205,7 @@ fetch(int sock, const char *path) warn("poll"); return 1; case 0: - warn("timeout"); + warnx("timeout."); return 1; } @@ -227,31 +227,25 @@ fetch(int sock, const char *path) } int -putfile(const char *path) +fput(FILE *fp) { - FILE *in; char buf[BUFSIZE]; size_t bytes; - int error; - - in = wfopen(path, "r"); - if (in == NULL) - return 1; - while ((bytes = fread(buf, 1, sizeof(buf), in)) > 0) { + while ((bytes = fread(buf, 1, sizeof(buf), fp)) > 0) { /* NOTE: using fwrite and `stdout` causes piping - * to not work. */ + * to not work. + */ if (write(STDOUT_FILENO, buf, bytes) != bytes) { - fclose(in); warn("unable to write buffer"); return 1; } } - error = ferror(in); - if (fclose(in) || error) - warn("file error ocoured '%s'", path); + if (ferror(fp)) { + warn("putfd FILE"); return 1; + } return 0; } @@ -294,7 +288,7 @@ pipedup(int old, int new, int fildes[2]) } int -gawkat(const char *path) +efmt(int (*putter)(const char *, int, const char **), const char *path, int argc, const char **argv) { int fds[2]; int i; @@ -323,7 +317,7 @@ gawkat(const char *path) case 0: if (pipedup(fds[1], STDOUT_FILENO, fds) != 0) _exit(1); - if (putfile(path) != 0) + if (putter(path, argc, argv) != 0) _exit(1); _exit(0); } @@ -385,6 +379,27 @@ cback(int argc, const char **argv) } int +cputs(const char *path, int argc, const char **argv) +{ + FILE *fp; + + fp = wfopen(path, "r"); + if (fp == NULL) + return 1; + + if (fput(fp) != 0) { + fclose(fp); + return 1; + } + + if (fclose(fp)) { + warn("fclose '%s'", path); + return 1; + } + return 0; +} + +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) { @@ -394,7 +409,7 @@ execute(int command, int argc, const char **argv, int depth, const char *cache, goto too_many_args; return depth; case 'p': - gawkat(cache); + efmt(cputs, cache, argc, argv); return 0; case 'b': if (argc > 1)