diff options
author | Sumit Bose <sbose@redhat.com> | 2010-05-21 09:36:04 +0200 |
---|---|---|
committer | Stephen Gallagher <sgallagh@redhat.com> | 2010-05-27 14:44:12 -0400 |
commit | 8b420102e0d91edb2acbd2e8a40fc7d9995de2ba (patch) | |
tree | 56456ff9213b2037a6914d6df6f97d2a80863979 /src/util | |
parent | c2caac87520c2f5a7db764d4827d1ad4cadcb696 (diff) | |
download | sssd-8b420102e0d91edb2acbd2e8a40fc7d9995de2ba.tar.gz sssd-8b420102e0d91edb2acbd2e8a40fc7d9995de2ba.tar.bz2 sssd-8b420102e0d91edb2acbd2e8a40fc7d9995de2ba.zip |
Move parse_args() to util
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/util.c | 99 | ||||
-rw-r--r-- | src/util/util.h | 2 |
2 files changed, 101 insertions, 0 deletions
diff --git a/src/util/util.c b/src/util/util.c index 57ceb597..9a973122 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -136,3 +136,102 @@ int split_on_separator(TALLOC_CTX *mem_ctx, const char *str, return EOK; } + +static void free_args(char **args) +{ + int i; + + if (args) { + for (i = 0; args[i]; i++) free(args[i]); + free(args); + } +} + +/* parse a string into arguments. + * arguments are separated by a space + * '\' is an escape character and can be used only to escape + * itself or the white space. + */ +char **parse_args(const char *str) +{ + const char *p; + char **ret, **r; + char *tmp; + int num; + int i, e; + + tmp = malloc(strlen(str) + 1); + if (!tmp) return NULL; + + ret = NULL; + num = 0; + e = 0; + i = 0; + p = str; + while (*p) { + switch (*p) { + case '\\': + if (e) { + tmp[i] = '\\'; + i++; + e = 0; + } else { + e = 1; + } + break; + case ' ': + if (e) { + tmp[i] = ' '; + i++; + e = 0; + } else { + tmp[i] = '\0'; + i++; + } + break; + default: + if (e) { + tmp[i] = '\\'; + i++; + e = 0; + } + tmp[i] = *p; + i++; + break; + } + + p++; + + /* check if this was the last char */ + if (*p == '\0') { + if (e) { + tmp[i] = '\\'; + i++; + e = 0; + } + tmp[i] = '\0'; + i++; + } + if (tmp[i-1] != '\0' || strlen(tmp) == 0) { + /* check next char and skip multiple spaces */ + continue; + } + + r = realloc(ret, (num + 2) * sizeof(char *)); + if (!r) goto fail; + ret = r; + ret[num+1] = NULL; + ret[num] = strdup(tmp); + if (!ret[num]) goto fail; + num++; + i = 0; + } + + free(tmp); + return ret; + +fail: + free(tmp); + free_args(ret); + return NULL; +} diff --git a/src/util/util.h b/src/util/util.h index 58c51a0a..1277305e 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -317,4 +317,6 @@ errno_t check_and_open_readonly(const char *filename, int *fd, const uid_t uid, /* from util.c */ int split_on_separator(TALLOC_CTX *mem_ctx, const char *str, const char sep, bool trim, char ***_list, int *size); + +char **parse_args(const char *str); #endif /* __SSSD_UTIL_H__ */ |