gawk

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

commit 28fd5ade1800dca11489a48cf7a571293a63bbb9
parent f934f7f636baa18b6d873e7d64e6ac3c3d10df1f
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date:   Tue,  5 Jan 2021 01:02:25 -0800

Fix pipeitems() return value

If the main loop was reached OK (0) was always returned causing
commands which try to unwind the stack to act unexpectedly.

The particular fixes were `return error` in pipeitems() and `done
< 0` in gawk(), the rest is refactoring.

Diffstat:
Mcommand.c | 6+++---
Mgawk.h | 4++--
Mmain.c | 5++---
3 files changed, 7 insertions(+), 8 deletions(-)

diff --git a/command.c b/command.c @@ -183,7 +183,7 @@ sgoto(int argc, char const **argv, int depth, char const **addr) char const *newaddr[AR_NULL]; if (warg(1, 4, argc, argv)) - return ERROR; + return FAIL; newaddr[AR_HOST] = addr[AR_HOST]; newaddr[AR_PATH] = "/"; @@ -207,11 +207,11 @@ sunwind(int argc, char const **argv, int depth, char const **addr) unsigned int n; if (warg(1, 2, argc, argv)) - return ERROR; + return FAIL; if (argc == 1) n = 1; else if (strtorange(&n, 1, INT_MAX, argv[1])) - return ERROR; + return FAIL; return n; } diff --git a/gawk.h b/gawk.h @@ -4,9 +4,9 @@ #define LEN(X) (sizeof(X) / sizeof(*X)) enum address { AR_PATH, AR_HOST, AR_PORT, AR_NULL }; /* NOTE: see itemtoaddr() */ +enum cstatus { ERROR = -1, OK, UNWIND }; /* command status */ enum gphitem { GI_INFO, GI_PATH, GI_HOST, GI_PORT, GI_PLUS, GI_NULL }; -enum status { ERROR = -1, OK, UNWIND }; -enum pipe_status { FAIL = -2, NEXT, PASS }; +enum pstatus { NEXT = -2, FAIL, PASS }; /* pipe status */ typedef int (command)(int, char const **, int, char const **); diff --git a/main.c b/main.c @@ -271,9 +271,8 @@ pipeitems(char const *cache, int count, command **const commands, while (error != FAIL && fgets(buf, sizeof(buf), fp) && *buf != '.') if (gphsplit(item, sizeof(item), buf) == 0) error = runpipe(count, commands, indexes, acs, avs, item); - wfclose(fp); - return OK; + return error; } int @@ -370,7 +369,7 @@ gawk(char const **addr) while (!done && input(inbuf, sizeof(inbuf), IFS0, prompt, stdin) == 0) { execute: done = execute(cache, inbuf, depth, addr); - if (done == ERROR && !fatal) + if (done < 0 && !fatal) done = 0; }