Skip to content

Commit 37e8161

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c1189ca commit 37e8161

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

quote.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "cache.h"
22
#include "quote.h"
3+
#include "argv-array.h"
34

45
int quote_path_fully = 1;
56

@@ -120,7 +121,9 @@ char *sq_dequote(char *arg)
120121
return sq_dequote_step(arg, NULL);
121122
}
122123

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)
124127
{
125128
char *next = arg;
126129

@@ -130,13 +133,27 @@ int sq_dequote_to_argv(char *arg, const char ***argv, int *nr, int *alloc)
130133
char *dequoted = sq_dequote_step(next, &next);
131134
if (!dequoted)
132135
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);
135142
} while (next);
136143

137144
return 0;
138145
}
139146

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+
140157
/* 1 means: quote as octal
141158
* 0 means: quote as octal if (quote_path_fully)
142159
* -1 means: never quote

quote.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ extern char *sq_dequote(char *);
4545
*/
4646
extern int sq_dequote_to_argv(char *arg, const char ***argv, int *nr, int *alloc);
4747

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+
4856
extern int unquote_c_style(struct strbuf *, const char *quoted, const char **endp);
4957
extern size_t quote_c_style(const char *name, struct strbuf *, FILE *, int no_dq);
5058
extern void quote_two_c_style(struct strbuf *, const char *, const char *, int);

0 commit comments

Comments
 (0)