gawk

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

commit 9203ee6788b8c9927dbf21db9b8036580676aad6
parent 72781f4fb731184263cca9cf52dca23a7ce72fe9
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date:   Sun, 20 Dec 2020 23:59:27 -0800

Organize functions and document cgoto() and cback()

The functions are organized as such:
1. warning wrapper
2. general utility
3. specific utility
4. network utility
5. pipe
6. command
7. central

Diffstat:
Mmain.c | 186+++++++++++++++++++++++++++++++++++++++++--------------------------------------
1 file changed, 97 insertions(+), 89 deletions(-)

diff --git a/main.c b/main.c @@ -45,6 +45,30 @@ static char *gphfmt[] = { "gophmt", NULL }; int gawk(int depth, const char *tmpdir, const char *host, const char *path, const char *port); +FILE * +wfopen(const char *path, const char *mode) +{ + FILE *fp; + + fp = fopen(path, mode); + if (fp == NULL) + warn("unable to open '%s' ('%s')", path, mode); + return fp; +} + +int +wfclose(FILE *fp) +{ + int error; + + error = ferror(fp); + if (fclose(fp) || error) { + warn("unable to close file"); + return EOF; + } + return 0; +} + void wunused(int max, int argc, const char **ap) { @@ -53,6 +77,23 @@ wunused(int max, int argc, const char **ap) } int +argsplit(char **argv, int arg_max, char *s, const char *sep) +{ + int argc; + + for (argc = 0; argc < arg_max && + (argv[argc] = strsep(&s, sep)) != NULL;) { + if (*argv[argc] != '\0') + ++argc; + } + + if (argc == arg_max && + argv[argc - 1][strcspn(argv[argc - 1], sep)] == '\0') + return -1; + return argc; +} + +int exists(const char *path) { struct stat sb; @@ -64,28 +105,45 @@ exists(const char *path) return 1; } -FILE * -wfopen(const char *path, const char *mode) +int +strtorange(unsigned int min, int max, const char *s) { - FILE *fp; + unsigned long n; + char *ep; - fp = fopen(path, mode); - if (fp == NULL) - warn("unable to open '%s' ('%s')", path, mode); - return fp; + n = strtoul(s, &ep, 0); + if (*s == '\0' || *ep != '\0') { + warnc(EINVAL, "not a number '%s'", s); + return -1; + } else if ((errno == ERANGE && n == ULONG_MAX) || + n < min || n > max) { + warnc(ERANGE, "'%s'", s); + return -1; + } + + return n; } -int -wfclose(FILE *fp) +/* `path` old path, `newpath` new path. Result put into `path`. */ +void +newpath(char *path, const char *newpath) { - int error; + char tmp[PATH_MAX]; - error = ferror(fp); - if (fclose(fp) || error) { - warn("unable to close file"); - return EOF; + if (*newpath == '/') { + strlcpy(path, newpath, PATH_MAX); + } else { + snprintf(tmp, PATH_MAX, "%s/%s", path, newpath); + strlcpy(path, tmp, PATH_MAX); } - return 0; +} + +void +tr(char *s, int orig, int repl) +{ + for (; *s != '\0'; ++s) + if (*s == orig) + *s = repl; } int @@ -164,53 +222,14 @@ too_big: return 0; } -/* `path` old path, `newpath` new path. Result put into `path`. */ -void -newpath(char *path, const char *newpath) -{ - char tmp[PATH_MAX]; - - if (*newpath == '/') { - strlcpy(path, newpath, PATH_MAX); - } else { - snprintf(tmp, PATH_MAX, "%s/%s", path, newpath); - strlcpy(path, tmp, PATH_MAX); - } -} - -void -tr(char *s, int orig, int repl) -{ - for (; *s != '\0'; ++s) - if (*s == orig) - *s = repl; -} - -void -getcache(char *cache, const char *tmpdir, const char *host, const char *port, const char *path) -{ - char tmp[PATH_MAX]; - - strlcpy(tmp, path, PATH_MAX); - tr(tmp, '/', '-'); - snprintf(cache, PATH_MAX, "%s/%s-%s-%s", tmpdir, host, port, tmp); -} - int -argsplit(char **argv, int arg_max, char *s, const char *sep) +gphsend(int sock, const char *request) { - int argc; - - for (argc = 0; argc < arg_max && - (argv[argc] = strsep(&s, sep)) != NULL;) { - if (*argv[argc] != '\0') - ++argc; + if (send(sock, request, strlen(request), 0) == -1) { + warn("unable to send request"); + return 1; } - - if (argc == arg_max && - argv[argc - 1][strcspn(argv[argc - 1], sep)] == '\0') - return -1; - return argc; + return 0; } int @@ -249,16 +268,6 @@ fetch(int sock, const char *path) } int -gphsend(int sock, const char *request) -{ - if (send(sock, request, strlen(request), 0) == -1) { - warn("unable to send request"); - return 1; - } - return 0; -} - -int selwrite(const char *host, const char *port, const char *selector, const char *file) { int sock; @@ -330,6 +339,11 @@ exfmt(int (*putter)(const char *, int, const char **), const char *path, int arg return 0; } +/* if lacking arguments goes to the root (`/') keeping the host and + * port. If one argument is given it is taken as a path, if more + * than one is given argv[0] is the host, argv[1] is the path, and + * if present argv[2] is the port. + */ int cgoto(int argc, const char **argv, int depth, const char *tmpdir, const char *host, const char *path, const char *port) @@ -357,25 +371,9 @@ cgoto(int argc, const char **argv, int depth, const char *tmpdir, const char *ho return gawk(depth, tmpdir, nhost, npath, nport); } -int -strtorange(unsigned int min, int max, const char *s) -{ - unsigned long n; - char *ep; - - n = strtoul(s, &ep, 0); - if (*s == '\0' || *ep != '\0') { - warnc(EINVAL, "not a number '%s'", s); - return -1; - } else if ((errno == ERANGE && n == ULONG_MAX) || - n < min || n > max) { - warnc(ERANGE, "'%s'", s); - return -1; - } - - return n; -} - +/* argv[0], if present, is the number of backtracks to make which + * is by default 1. + */ int cback(int argc, const char **argv) { @@ -431,7 +429,7 @@ cmatch(const char *path, int argc, const char **argv) * download. If present argv[1] is the location to write the file * to, otherwise it is written to the basename(3) of it's selector * string. -*/ + */ int cfetch(int argc, const char **argv, const char *cache, const char *host, const char *port) { @@ -522,6 +520,16 @@ 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) +{ + char tmp[PATH_MAX]; + + strlcpy(tmp, path, PATH_MAX); + tr(tmp, '/', '-'); + snprintf(cache, PATH_MAX, "%s/%s-%s-%s", tmpdir, host, port, tmp); +} + int gawk(int depth, const char *tmpdir, const char *host, const char *path, const char *port) {