commit c1cb9f7e79d6caf90a71fe2e186b8697f273dc5a
parent 033dc22130ac8897bdb3bc1e07413fbeac89f360
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date: Wed, 6 Jan 2021 15:14:25 -0800
Refactor execute()
While it's just as long it's more logical. The last error is also
more informative that the command could exist, just not in the
context of a pipeline.
Diffstat:
M | main.c | | | 30 | ++++++++++++++---------------- |
1 file changed, 14 insertions(+), 16 deletions(-)
diff --git a/main.c b/main.c
@@ -307,15 +307,11 @@ runpipes(char const *cache, struct command *cmds, int count)
int
execute(char const *cache, char *input, int depth, char **addr)
{
- char *bufs[MY_PIPE_MAX];
-
int i;
int nbufs;
+ char *bufs[MY_PIPE_MAX];
struct command cmds[MY_PIPE_MAX];
- if (*input == '\0')
- return ERROR;
-
nbufs = argsplit(bufs, LEN(bufs), input, separators[1], 0);
if (nbufs < 0) {
warn(0, "Pipeline too long.");
@@ -326,9 +322,10 @@ execute(char const *cache, char *input, int depth, char **addr)
cmds[i].argc = argsplit(cmds[i].argv, LEN(cmds[i].argv),
bufs[i], separators[2], 1);
if (cmds[i].argc < 1) {
- if (cmds[i].argc == 0)
- warn(0, "Empty pipe (#%d)", ++i);
- else warn(E2BIG, "'%s'", cmds[i].argv[0]);
+ if (cmds[i].argc < 0)
+ warn(E2BIG, "command %d", i);
+ else if (nbufs > 1)
+ warn(0, "command %d: Empty command.", i);
return ERROR;
}
}
@@ -336,15 +333,16 @@ execute(char const *cache, char *input, int depth, char **addr)
for (i = 0; i < nbufs; ++i) {
cmds[i].f = getcom(cmds[i].argv[0], itembinds, LEN(itembinds));
if (cmds[i].f == NULL) {
- if (i == 0) {
- cmds[i].f = getcom(cmds[i].argv[0], binds,
- LEN(binds));
- if (cmds[i].f != NULL)
- return cmds[i].f(cmds[i].argc,
- cmds[i].argv, depth, addr);
+ if (i > 0) {
+ warn(0, "'%s': Not a pipeline command.",
+ cmds[i].argv[0]);
+ return ERROR;
+ }
+ cmds[i].f = getcom(cmds[i].argv[0], binds, LEN(binds));
+ if (cmds[i].f != NULL) {
+ return cmds[i].f(cmds[i].argc, cmds[i].argv,
+ depth, addr);
}
- warn(0, "'%s': Not a command.", cmds[i].argv[0]);
- return ERROR;
}
}