gawk

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

commit 06851ef57ec48bea6d8f8a3c7e1a9afc8b9b482d
parent 9e89b90aa01feaae6d3b31a86fc25cb84cf9ef91
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date:   Tue,  5 Jan 2021 23:07:01 -0800

Check item types for relevent commands

Add helper functions type() and wtype() which check it the type
they are given is what the caller wants. Since a plain type function
was used it replaced the same thing in ftype().

warg() was moved to command.c as it's only used there.

Diffstat:
Mcommand.c | 49++++++++++++++++++++++++++++++++++++++-----------
Mgawk.h | 2+-
Mutil.c | 12------------
Mutil.h | 1-
4 files changed, 39 insertions(+), 25 deletions(-)

diff --git a/command.c b/command.c @@ -26,6 +26,36 @@ #define LOCAL /* functions local to this file */ +LOCAL int +type(int c, char const *types, int want) +{ + if (strchr(types, c) != NULL) + return !want; + else return want; +} + +LOCAL int +wtype(char const *argv0, int c, char const *types, int want) +{ + if (type(c, types, want) == 1) { + warn(0, "%s: '%c': Invalid type", argv0, c); + return 1; + } + return 0; +} + +LOCAL int +warg(int min, int max, int argc, char const **ap) +{ + if (max > 0 && argc > max) + warn(0, "'%s': '%s'... unused.", *ap, ap[max]); + else if (min > 0 && argc < min) + warn(0, "'%s': Not enough arguments.", *ap); + else + return 0; + return 1; +} + int cextern(int argc, char const **argv, int ino, char const **item) { @@ -68,7 +98,7 @@ cfetch(int argc, char const **argv, int ino, char const **item) char const *path; int bin; - if (warg(1, 2, argc, argv)) + if (warg(1, 2, argc, argv) || wtype(*argv, **item, "i3", 0)) return FAIL; if (argv[1]) @@ -93,12 +123,8 @@ cgoto7(int argc, char const **argv, int ino, char const **item) { char tmp[MY_PATH_MAX]; - if (warg(2, 2, argc, argv)) - return FAIL; - if (**item != '7') { - warn(EFTYPE, "%d", ino); + if (wtype(*argv, **item, "7", 1) || warg(2, 2, argc, argv)) return FAIL; - } /* NOTE: the user inserts `?` for a space */ snprintf(tmp, sizeof(tmp), "%s?%s", item[GI_PATH], argv[1]); @@ -209,17 +235,18 @@ fstring(int argc, char const **argv, int ino, char const **item) int ftype(int argc, char const **argv, int ino, char const **item) { - if (warg(2, 2, argc, argv)) - return FAIL; - if (strchr(argv[1], *item[GI_INFO]) != NULL) + if (warg(2, 2, argc, argv)) + return FAIL; + if (type(**item, argv[1], 1)) return PASS; - return NEXT; + return NEXT; } int sexit(int argc, char const **argv, int depth, char const **addr) { - warg(1, 1, argc, argv); + if (warg(1, 1, argc, argv)) + return FAIL; return depth; } diff --git a/gawk.h b/gawk.h @@ -15,7 +15,7 @@ #define MY_PROMPT_MAX 64 #define MY_URL_MAX 72 -/* NOTE: don't change address order (see itemtoaddr and sgoto) */ +/* NOTE: don't change address order (see itemtoaddr, sgoto and all commands) */ enum address { AR_PATH, AR_HOST, AR_PORT, AR_NULL }; enum gphitem { GI_INFO, GI_PATH, GI_HOST, GI_PORT, GI_PLUS, GI_NULL }; diff --git a/util.c b/util.c @@ -68,18 +68,6 @@ strtorange(unsigned int *r, unsigned int min, unsigned int max, char const *s) } int -warg(int min, int max, int argc, char const **ap) -{ - if (max > 0 && argc > max) - warn(0, "'%s': '%s'... unused.", *ap, ap[max]); - else if (min > 0 && argc < min) - warn(0, "'%s': Not enough arguments.", *ap); - else - return 0; - return 1; -} - -int wfclose(FILE *fp) { int error; diff --git a/util.h b/util.h @@ -8,7 +8,6 @@ FILE * wfopen(char const *, char const *); int exists(char const *); -int warg(int, int, int, char const **); int wfclose(FILE *); int wremove(char const *); #undef strtonum