Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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>
  • Loading branch information...
commit 37e8161a04360d01edd0611135f5eae8e6a08224 1 parent c1189ca
Jeff King authored September 13, 2011 gitster committed September 14, 2011

Showing 2 changed files with 28 additions and 3 deletions. Show diff stats Hide diff stats

  1. 23  quote.c
  2. 8  quote.h
23  quote.c
... ...
@@ -1,5 +1,6 @@
1 1
 #include "cache.h"
2 2
 #include "quote.h"
  3
+#include "argv-array.h"
3 4
 
4 5
 int quote_path_fully = 1;
5 6
 
@@ -120,7 +121,9 @@ char *sq_dequote(char *arg)
120 121
 	return sq_dequote_step(arg, NULL);
121 122
 }
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)
124 127
 {
125 128
 	char *next = arg;
126 129
 
@@ -130,13 +133,27 @@ int sq_dequote_to_argv(char *arg, const char ***argv, int *nr, int *alloc)
130 133
 		char *dequoted = sq_dequote_step(next, &next);
131 134
 		if (!dequoted)
132 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);
135 142
 	} while (next);
136 143
 
137 144
 	return 0;
138 145
 }
139 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
+
140 157
 /* 1 means: quote as octal
141 158
  * 0 means: quote as octal if (quote_path_fully)
142 159
  * -1 means: never quote
8  quote.h
@@ -45,6 +45,14 @@ extern char *sq_dequote(char *);
45 45
  */
46 46
 extern int sq_dequote_to_argv(char *arg, const char ***argv, int *nr, int *alloc);
47 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
+
48 56
 extern int unquote_c_style(struct strbuf *, const char *quoted, const char **endp);
49 57
 extern size_t quote_c_style(const char *name, struct strbuf *, FILE *, int no_dq);
50 58
 extern void quote_two_c_style(struct strbuf *, const char *, const char *, int);

0 notes on commit 37e8161

Please sign in to comment.
Something went wrong with that request. Please try again.