commit 7d5052fe69b9d5d25fe810cee4938bf8ceafeac3
parent d620008fecd52c39c3eb5296bd3ba8025a0ab11c
Author: Jacob R. Edwards <n/a>
Date: Sat, 8 Oct 2022 17:07:19 -0700
Require at least one argument with the client's in-built xargs
This is very useful for commands whose function is vastly different
with no arguments, such as truncate and remove. (For instance, you
would want
$ apc list | stest -d | apc remove ^
to ensure there are no directories in the queue, and, if there
weren't any, simply remove nothing instead of the current item.)
Additionally, update error messages and exit early on errors.
Diffstat:
M | apc/apc.c | | | 50 | ++++++++++++++++++++++++++++---------------------- |
1 file changed, 28 insertions(+), 22 deletions(-)
diff --git a/apc/apc.c b/apc/apc.c
@@ -29,6 +29,9 @@
#define pledge(a,b) (0)
#endif
+static char *ewrite = "Unable to write request";
+static char *eread = "Unable to read response";
+
#include <ap/ap.h>
static char subchr = '^';
@@ -87,35 +90,38 @@ apc_stream(struct apc *apc, FILE *fp)
err = NULL;
while (!err && (len = getline(&buf, &size, fp)) >= 0) {
if (write(apc->con->sock, buf, len) != len)
- err = "unable to write to server";
+ err = ewrite;
else if (printresponse(apc))
- err = "unable to handle response";
+ err = eread;
}
free(buf);
if (ferror(stdin))
- err = "unable to read from standard input";
+ return "Unable to read from standard input";
return err;
}
-char *
+long
xargs(struct apc *apc, FILE *fp)
{
char *buf;
+ long nargs;
size_t size;
ssize_t len;
+ nargs = 0;
buf = NULL;
size = 0;
while ((len = getline(&buf, &size, stdin)) >= 0) {
if (len && buf[len - 1] == '\n')
buf[--len] = 0;
if (apc_bufword(apc, buf))
- return "unable to add word";
+ return -nargs;
+ ++nargs;
}
free(buf);
if (ferror(stdin))
- return "unable to read line";
- return NULL;
+ return -nargs;
+ return nargs;
}
int
@@ -129,7 +135,7 @@ main(int argc, char *argv[])
apc = apc_new(NULL);
if (apc == NULL)
- die("unable to connect to server");
+ die("Unable to connect to server");
if (pledge("stdio", NULL))
die("pledge");
@@ -137,9 +143,10 @@ main(int argc, char *argv[])
--argc;
++argv;
- if (!argc)
- err = apc_stream(apc, stdin);
- else {
+ if (!argc) {
+ if ((err = apc_stream(apc, stdin)))
+ die(err);
+ } else {
insert = 0;
if (argv[argc - 1][0] == subchr) {
if (argv[argc - 1][1] == subchr) {
@@ -150,21 +157,20 @@ main(int argc, char *argv[])
}
}
- err = NULL;
- for (i = 0; !err && i < argc; ++i)
+ for (i = 0; i < argc; ++i)
if (apc_bufword(apc, argv[i]))
- err = "unable to write word";
- if (!err && (!insert || !(err = xargs(apc, stdin)))) {
- if (apc_write(apc))
- err = "unable to write command";
- else if (printresponse(apc))
- err = "unable print response";
- }
+ die(ewrite);
+
+ if (insert && xargs(apc, stdin) <= 1)
+ die("Unable to get arguments from stdin");
+
+ if (apc_write(apc))
+ die(ewrite);
+ else if (printresponse(apc))
+ die(eread);
}
status = (apc->status && strcmp(apc->status, "ok") != 0);
apc_free(apc);
- if (err)
- die(err);
return status;
}