From 04a23330e4a2085ee91980c223c5e4f089ebbe97 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Wed, 4 Apr 2018 14:58:01 +0200 Subject: Merge preprocessing of command lines - Common function for splitting string into argument vector - Preprocess & split command lines while parsing the service file - Specify "before" and "after" dependencies in a single line Signed-off-by: David Oberhollenzer --- lib/src/splitkv.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) (limited to 'lib/src/splitkv.c') diff --git a/lib/src/splitkv.c b/lib/src/splitkv.c index 3631ea8..7d652d3 100644 --- a/lib/src/splitkv.c +++ b/lib/src/splitkv.c @@ -15,7 +15,9 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ +#include #include +#include #include "util.h" @@ -86,3 +88,74 @@ int unescape(char *src) *(dst++) = '\0'; return 0; } + +char **split_argv(char *str) +{ + size_t i = 0, cap = 0, new_cap; + char **argv = NULL, **new; + char *ptr; + + ptr = str; + + for (;;) { + if (*ptr == ' ') { + ++ptr; + continue; + } + + if (i == cap) { + new_cap = cap ? cap * 2 : 16; + new = realloc(argv, sizeof(argv[0]) * new_cap); + + if (new == NULL) { + free(argv); + errno = ENOMEM; + return NULL; + } + + cap = new_cap; + argv = new; + } + + if (*ptr == '\0') { + argv[i++] = NULL; + break; + } + + argv[i++] = ptr; + + if (*ptr == '"') { + ++ptr; + while (*ptr != '\0' && *ptr != '"') { + if (ptr[0] == '\\' && ptr[1] != '\0') + ++ptr; + + ++ptr; + } + + if (*ptr == '"') + ++ptr; + + if (*ptr == ' ') { + *(ptr++) = '\0'; + } else if (*ptr != '\0') { + goto fail_str; + } + + if (unescape(argv[i - 1])) + goto fail_str; + } else { + while (*ptr != '\0' && *ptr != ' ') + ++ptr; + + if (*ptr == ' ') + *(ptr++) = '\0'; + } + } + + return argv; +fail_str: + free(argv); + errno = EINVAL; + return NULL; +} -- cgit v1.2.3