gawk

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

commit d905d8c95e9d61cca2bc751183c599a4186f4691
parent 6a736a4f6e86cd193617740f46d21cbfa8bec59c
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date:   Sun, 20 Dec 2020 20:46:33 -0800

Use a better method for warning of too many arguments

Implemented a simple function to check if there were too many
arguments and print a warning it so.

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

diff --git a/main.c b/main.c @@ -41,6 +41,13 @@ static char *gphfmt[] = { "gophmt", NULL }; int gawk(int depth, const char *tmpdir, const char *host, const char *path, const char *port); +void +wunused(int max, int argc, const char **ap) +{ + if (argc > max) + warnx("warning: '%s' and %d more arguments unused.", ap[max], argc - (max + 1)); +} + int exists(const char *path) { @@ -325,6 +332,8 @@ cgoto(int argc, const char **argv, int depth, const char *tmpdir, const char *ho char npath[PATH_MAX]; const char *nhost, *nport; + wunused(3, argc, argv); + nhost = host; nport = port; strcpy(npath, path); @@ -349,6 +358,8 @@ cback(int argc, const char **argv) char *ep; unsigned long back; + wunused(1, argc, argv); + if (argc == 0) return 1; /* back `1`, not an error */ @@ -405,28 +416,18 @@ execute(int command, int argc, const char **argv, int depth, const char *cache, { switch (command) { case 'q': - if (argc != 0) - goto too_many_args; + wunused(0, argc, argv); return depth; case 'p': - if (argc > 2) - goto too_many_args; efmt(cmatch, cache, argc, argv); return 0; case 'b': - if (argc > 1) - goto too_many_args; return cback(argc, argv); case 'g': - if (argc > 4) - goto too_many_args; return cgoto(argc, argv, depth, tmpdir, host, path, port); default: warnx("invalid command '%c'", command); return 0; -too_many_args: /* this is terrible... */ - warnc(E2BIG, "'%c'", command); - return 0; } }