summaryrefslogtreecommitdiff
path: root/src/shared/strv.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2014-11-10 23:44:34 +0100
committerLennart Poettering <lennart@poettering.net>2014-11-10 23:45:12 +0100
commitf88e6be5ee31ff0e45fabcdedaf26d3be0d4817a (patch)
tree6eaee98e65ef337eddc31ea60a47ae444c8e2a1d /src/shared/strv.c
parent27e9c5af817147ea1c678769e45e83f2e4b4ae96 (diff)
strv: rework strv_split_quoted() to use unquote_first_word()
This should make the unquoting scheme a bit less naive.
Diffstat (limited to 'src/shared/strv.c')
-rw-r--r--src/shared/strv.c69
1 files changed, 35 insertions, 34 deletions
diff --git a/src/shared/strv.c b/src/shared/strv.c
index 00857e40a7..aeb93eac6b 100644
--- a/src/shared/strv.c
+++ b/src/shared/strv.c
@@ -248,40 +248,6 @@ char **strv_split(const char *s, const char *separator) {
return r;
}
-int strv_split_quoted(char ***t, const char *s) {
- const char *word, *state;
- size_t l;
- unsigned n, i;
- char **r;
-
- assert(s);
-
- n = 0;
- FOREACH_WORD_QUOTED(word, l, s, state)
- n++;
- if (!isempty(state))
- /* bad syntax */
- return -EINVAL;
-
- r = new(char*, n+1);
- if (!r)
- return -ENOMEM;
-
- i = 0;
- FOREACH_WORD_QUOTED(word, l, s, state) {
- r[i] = cunescape_length(word, l);
- if (!r[i]) {
- strv_free(r);
- return -ENOMEM;
- }
- i++;
- }
-
- r[i] = NULL;
- *t = r;
- return 0;
-}
-
char **strv_split_newlines(const char *s) {
char **l;
unsigned n;
@@ -307,6 +273,41 @@ char **strv_split_newlines(const char *s) {
return l;
}
+int strv_split_quoted(char ***t, const char *s, bool relax) {
+ size_t n = 0, allocated = 0;
+ _cleanup_strv_free_ char **l = NULL;
+ int r;
+
+ assert(t);
+ assert(s);
+
+ for (;;) {
+ _cleanup_free_ char *word = NULL;
+
+ r = unquote_first_word(&s, &word, relax);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ break;
+
+ if (!GREEDY_REALLOC(l, allocated, n + 2))
+ return -ENOMEM;
+
+ l[n++] = word;
+ word = NULL;
+
+ l[n] = NULL;
+ }
+
+ if (!l)
+ l = new0(char*, 1);
+
+ *t = l;
+ l = NULL;
+
+ return 0;
+}
+
char *strv_join(char **l, const char *separator) {
char *r, *e;
char **s;