quote: provide sq_dequote_to_argv_array

This is similar to sq_dequote_to_argv, but more convenient if you have an argv_array. It's tempting to just feed the components of the argv_array to sq_dequote_to_argv instead, but: 1. It wouldn't maintain the NULL-termination invariant of argv_array. 2. It doesn't match the memory ownership policy of argv_array (in which each component is free-able, not a pointer into a separate buffer). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Sep 13, 2011 at 17:58 UTC 37e8161a04360d01edd0611135f5eae8e6a08224
2 files changed +28 -3
quote.c
+20 -3
@@ -1,5 +1,6 @@
1 #include "cache.h"
2 #include "quote.h"
3 +#include "argv-array.h"
4
5 int quote_path_fully = 1;
6
@@ -120,7 +121,9 @@ char *sq_dequote(char *arg)
121 return sq_dequote_step(arg, NULL);
122 }
123
123 -int sq_dequote_to_argv(char *arg, const char ***argv, int *nr, int *alloc)
124 +static int sq_dequote_to_argv_internal(char *arg,
125 + const char ***argv, int *nr, int *alloc,
126 + struct argv_array *array)
127 {
128 char *next = arg;
129
@@ -130,13 +133,27 @@ int sq_dequote_to_argv(char *arg, const char ***argv, int *nr, int *alloc)
133 char *dequoted = sq_dequote_step(next, &next);
134 if (!dequoted)
135 return -1;
133 - ALLOC_GROW(*argv, *nr + 1, *alloc);
134 - (*argv)[(*nr)++] = dequoted;
136 + if (argv) {
137 + ALLOC_GROW(*argv, *nr + 1, *alloc);
138 + (*argv)[(*nr)++] = dequoted;
139 + }
140 + if (array)
141 + argv_array_push(array, dequoted);
142 } while (next);
143
144 return 0;
145 }
146
147 +int sq_dequote_to_argv(char *arg, const char ***argv, int *nr, int *alloc)
148 +{
149 + return sq_dequote_to_argv_internal(arg, argv, nr, alloc, NULL);
150 +}
151 +
152 +int sq_dequote_to_argv_array(char *arg, struct argv_array *array)
153 +{
154 + return sq_dequote_to_argv_internal(arg, NULL, NULL, NULL, array);
155 +}
156 +
157 /* 1 means: quote as octal
158 * 0 means: quote as octal if (quote_path_fully)
159 * -1 means: never quote
quote.h
+8
@@ -45,6 +45,14 @@ extern char *sq_dequote(char *);
45 */
46 extern int sq_dequote_to_argv(char *arg, const char ***argv, int *nr, int *alloc);
47
48 +/*
49 + * Same as above, but store the unquoted strings in an argv_array. We will
50 + * still modify arg in place, but unlike sq_dequote_to_argv, the argv_array
51 + * will duplicate and take ownership of the strings.
52 + */
53 +struct argv_array;
54 +extern int sq_dequote_to_argv_array(char *arg, struct argv_array *);
55 +
56 extern int unquote_c_style(struct strbuf *, const char *quoted, const char **endp);
57 extern size_t quote_c_style(const char *name, struct strbuf *, FILE *, int no_dq);
58 extern void quote_two_c_style(struct strbuf *, const char *, const char *, int);