From 196abe367c46cf3c2337b4b1da8bb2c084065c7d Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Fri, 25 Dec 2015 22:06:56 +0100 Subject: satr run select jobs + satrm can remove multiple jobs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/client.c | 37 +++++++++++++++++++++++++++++++++++++ src/client.h | 20 ++++++++++++++++++++ src/sat.c | 36 +----------------------------------- src/satr.c | 32 ++++++++++++++++++++++++++++---- src/satrm.c | 32 +++++++++++++++++++++++++++----- 5 files changed, 113 insertions(+), 44 deletions(-) diff --git a/src/client.c b/src/client.c index fb08c49..9df327e 100644 --- a/src/client.c +++ b/src/client.c @@ -20,6 +20,7 @@ * DEALINGS IN THE SOFTWARE. */ #include "client.h" +#include @@ -47,3 +48,39 @@ send_command(enum command cmd, size_t n, const char *restrict msg) return 0 /* TODO */ } + + +/** + * Return the number of bytes required to store a string array. + * + * @param array The string array. + * @return The number of bytes required to store the array. + */ +size_t +measure_array(char *array[]) +{ + size_t rc = 1; + for (; *array; array++) + rc += strlen(*array) + 1; + return rc * sizeof(char); +} + + +/** + * Store a string array. + * + * @param storage The buffer where the array is to be stored. + * @param array The array to store. + * @return Where in the buffer the array ends. + */ +char * +store_array(char *restrict storage, char *array[]) +{ + for (; *array; array++) { + storage = stpcpy(storage, *array); + *storage++ = 0; + } + *storage++ = 0; + return storage; +} + diff --git a/src/client.h b/src/client.h index 2e5cf9d..6a2b91b 100644 --- a/src/client.h +++ b/src/client.h @@ -22,6 +22,7 @@ #include + /** * Commands for `send_command`. */ @@ -49,6 +50,7 @@ enum command }; + /** * Send a command to satd. Start satd if it is not running. * @@ -62,3 +64,21 @@ enum command */ int send_command(enum command cmd, size_t n, const char *restrict msg); + +/** + * Return the number of bytes required to store a string array. + * + * @param array The string array. + * @return The number of bytes required to store the array. + */ +size_t measure_array(char *array[]) + +/** + * Store a string array. + * + * @param storage The buffer where the array is to be stored. + * @param array The array to store. + * @return Where in the buffer the array ends. + */ +char *store_array(char *restrict storage, char *array[]); + diff --git a/src/sat.c b/src/sat.c index e0ede5c..6074c10 100644 --- a/src/sat.c +++ b/src/sat.c @@ -25,6 +25,7 @@ #include #include "parse_time.h" +#include "client.h" @@ -47,41 +48,6 @@ usage(void) } -/** - * Return the number of bytes required to store a string array. - * - * @param array The string array. - * @return The number of bytes required to store the array. - */ -static size_t -measure_array(char *array[]) -{ - size_t rc = 1; - for (; *array; array++) - rc += strlen(*array) + 1; - return rc * sizeof(char); -} - - -/** - * Store a string array. - * - * @param storage The buffer where the array is to be stored. - * @param array The array to store. - * @return Where in the buffer the array ends. - */ -static char * -store_array(char *restrict storage, char *array[]) -{ - for (; *array; array++) { - storage = stpcpy(storage, *array); - *storage++ = 0; - } - *storage++ = 0; - return storage; -} - - /** * Queue a job for later execution. * diff --git a/src/satr.c b/src/satr.c index df85ee2..b3ebd62 100644 --- a/src/satr.c +++ b/src/satr.c @@ -40,7 +40,7 @@ char *argv0 = "satr"; static void usage(void) { - fprintf(stderr, "usage: %s\n", + fprintf(stderr, "usage: %s [JOB-ID]...\n", strrchr(argv0) ? (strrchr(argv0) + 1) : argv0); exit(2); } @@ -59,11 +59,35 @@ usage(void) int main(int argc, char *argv[]) { + size_t n = 0; + char *msg = NULL; + int i; + if (argc > 0) argv0 = argv[0]; - if (argc > 1) usage(); /* TODO possibility to select job */ + if (argc < 2) + goto run; + if (!strcmp(argv[1], "--") + argv++, argc--; + for (i = 1; i < argc; i++) + if (argv[i][0] == '-') + usage(); + + if (!(msg = malloc(n = measure_array(argv + 1)))) + goto fail; + store_array(msg, argv + 1); - if (send_command(SAT_RUN, 0, NULL)) - return errno ? (perror(argv0), 1) : 3; +run: + if (send_command(SAT_RUN, n, msg)) { + if (errno) + goto fail; + free(msg); + return 3; + } return 0; + +fail: + perror(*argv); + free(msg); + return 1; } diff --git a/src/satrm.c b/src/satrm.c index b3dafcd..8097b18 100644 --- a/src/satrm.c +++ b/src/satrm.c @@ -40,7 +40,7 @@ char *argv0 = "satrm"; static void usage(void) { - fprintf(stderr, "usage: %s JOB-ID\n", + fprintf(stderr, "usage: %s JOB-ID...\n", strrchr(argv0) ? (strrchr(argv0) + 1) : argv0); exit(2); } @@ -60,11 +60,33 @@ usage(void) int main(int argc, char *argv[]) { - if (argc > 0) argv0 = argv[0]; - if (argc != 2) usage(); + size_t n; + char *msg; + int i; - if (send_command(SAT_REMOVE, 0, argv[1])) - return errno ? (perror(argv0), 1) : 3; + if (argc > 0) argv0 = argv[0]; + if (argc < 2) usage(); + if (!strcmp(argv[1], "--") + argv++, argc--; + for (i = 1; i < argc; i++) + if (argv[i][0] == '-') + usage(); + + if (!(msg = malloc(n = measure_array(argv + 1)))) + goto fail; + store_array(msg, argv + 1); + + if (send_command(SAT_REMOVE, n, msg)) { + if (errno) + goto fail; + free(msg); + return 3; + } return 0; + +fail: + perror(*argv); + free(msg); + return 1; } -- cgit v1.2.3-70-g09d2