commit fffd975b08d08b6e98a0cd262310fdb2009f1623
parent f5df3aa9a53ef4f7951a3a639aa33960eb8e6308
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date: Wed, 6 Jan 2021 21:10:15 -0800
Add command negation
If a command is prefixed with `!' (configurable in config.h) the
PASS and NEXT values are inverted.
I was going to implement this within ftype() where I thought it
would be useful, but that makes no sense, might as aswell vastly
improve every command by adding it higher up.
Diffstat:
3 files changed, 16 insertions(+), 2 deletions(-)
diff --git a/config.def.h b/config.def.h
@@ -3,6 +3,9 @@
/* how long to wait for response in ms */
static int const timeout = 5 * 1000;
+/* character used to negate commands */
+static char const negate = '!';
+
/* default address if arguments are ommited */
static char *default_address[] = {
/* path, host, port */
diff --git a/gawk.h b/gawk.h
@@ -21,7 +21,7 @@ enum gphitem { GI_INFO, GI_PATH, GI_HOST, GI_PORT, GI_PLUS, GI_NULL };
/* command and pipeline status */
enum cstatus { ERROR = -1, OK, UNWIND };
-enum pstatus { NEXT = -2, FAIL, PASS };
+enum pstatus { FAIL = -2, NEXT, PASS };
typedef int (command)(int, char **, int, char **);
@@ -29,6 +29,7 @@ struct command {
char *argv[MY_ARGV_MAX];
command *f;
int argc;
+ int negate;
};
struct bind {
diff --git a/main.c b/main.c
@@ -274,8 +274,12 @@ runpipe(struct command *cmds, int *indexes, int count, char **item)
for (i = 0; i < count; ++i) {
r = cmds[i].f(cmds[i].argc, cmds[i].argv, indexes[i]++, item);
- if (r != PASS)
+ if (r != PASS && r != NEXT)
return r;
+ if (cmds[i].negate)
+ r = !r;
+ if (r)
+ return NEXT;
}
return PASS;
}
@@ -327,6 +331,12 @@ execute(char const *cache, char *input, int depth, char **addr)
warn(0, "command %d: Empty command.", i);
return ERROR;
}
+ if (cmds[i].argv[0][0] != negate) {
+ cmds[i].negate = 0;
+ } else {
+ cmds[i].negate = 1;
+ cmds[i].argv[0]++;
+ }
}
for (i = 0; i < nbufs; ++i) {