git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 4/7] quote: provide sq_dequote_to_argv_array

From
Jeff King <peff@peff.net>
Date
Sep 13, 2011, 21:58 UTC
Message-ID
<20110913215808.GD24490@sigill.intra.peff.net>
In-Reply-To
<20110913215026.GA26743@sigill.intra.peff.net>

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>
---
 quote.c |   23 ++++++++++++++++++++---
 quote.h |    8 ++++++++
 2 files changed, 28 insertions(+), 3 deletions(-)
diff --git a/quote.c b/quote.c
index 63d3b01..87bc65e 100644
--- a/quote.c
+++ b/quote.c
@@ -1,5 +1,6 @@
 #include "cache.h"
 #include "quote.h"
+#include "argv-array.h"
 
 int quote_path_fully = 1;
 
@@ -120,7 +121,9 @@ void sq_quote_argv(struct strbuf *dst, const char** argv, size_t maxlen)
 	return sq_dequote_step(arg, NULL);
 }
 
-int sq_dequote_to_argv(char *arg, const char ***argv, int *nr, int *alloc)
+static int sq_dequote_to_argv_internal(char *arg,
+				       const char ***argv, int *nr, int *alloc,
+				       struct argv_array *array)
 {
 	char *next = arg;
 
@@ -130,13 +133,27 @@ int sq_dequote_to_argv(char *arg, const char ***argv, int *nr, int *alloc)
 		char *dequoted = sq_dequote_step(next, &next);
 		if (!dequoted)
 			return -1;
-		ALLOC_GROW(*argv, *nr + 1, *alloc);
-		(*argv)[(*nr)++] = dequoted;
+		if (argv) {
+			ALLOC_GROW(*argv, *nr + 1, *alloc);
+			(*argv)[(*nr)++] = dequoted;
+		}
+		if (array)
+			argv_array_push(array, dequoted);
 	} while (next);
 
 	return 0;
 }
 
+int sq_dequote_to_argv(char *arg, const char ***argv, int *nr, int *alloc)
+{
+	return sq_dequote_to_argv_internal(arg, argv, nr, alloc, NULL);
+}
+
+int sq_dequote_to_argv_array(char *arg, struct argv_array *array)
+{
+	return sq_dequote_to_argv_internal(arg, NULL, NULL, NULL, array);
+}
+
 /* 1 means: quote as octal
  * 0 means: quote as octal if (quote_path_fully)
  * -1 means: never quote
diff --git a/quote.h b/quote.h
index 252b0df..133155a 100644
--- a/quote.h
+++ b/quote.h
@@ -45,6 +45,14 @@
  */
 extern int sq_dequote_to_argv(char *arg, const char ***argv, int *nr, int *alloc);
 
+/*
+ * Same as above, but store the unquoted strings in an argv_array. We will
+ * still modify arg in place, but unlike sq_dequote_to_argv, the argv_array
+ * will duplicate and take ownership of the strings.
+ */
+struct argv_array;
+extern int sq_dequote_to_argv_array(char *arg, struct argv_array *);
+
 extern int unquote_c_style(struct strbuf *, const char *quoted, const char **endp);
 extern size_t quote_c_style(const char *name, struct strbuf *, FILE *, int no_dq);
 extern void quote_two_c_style(struct strbuf *, const char *, const char *, int);
-- 
1.7.7.rc1.2.gb2409
Previous: Junio C HamanoNext: Jeff King
Message 9 of 18 in “create argv_array API”
  1. 0/7 create argv_array APIJeff King, Sep 13, 2011
  2. 1/7 add sha1_array API docsJeff King, Sep 13, 2011
  3. 2/7 quote.h: fix bogus commentJeff King, Sep 13, 2011
  4. 3/7 refactor argv_array into generic codeJeff King, Sep 13, 2011
  5. Christian CouderSep 14, 2011
  6. Jeff KingSep 14, 2011
  7. Junio C HamanoSep 14, 2011
  8. Junio C HamanoSep 14, 2011
  9. 4/7 quote: provide sq_dequote_to_argv_arrayJeff King, Sep 13, 2011
  10. 5/7 bisect: use argv_array APIJeff King, Sep 13, 2011
  11. 6/7 checkout: use argv_array APIJeff King, Sep 13, 2011
  12. 7/7 run_hook: use argv_array APIJeff King, Sep 13, 2011
  13. [nit] diff func headers ignore contextJeff King, Sep 13, 2011
  14. Andreas EricssonSep 14, 2011
  15. Junio C HamanoSep 14, 2011
  16. Junio C HamanoSep 14, 2011
  17. Jeff KingSep 14, 2011
  18. Junio C HamanoSep 14, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.