@matheusbsbs / matheus-bagliano / commits / ebbc088e13
Fork 0

quote: implement "sq_dequote_many" to unwrap many args in one string

The sq_dequote() function does not allow parsing a string with more than one single-quoted parameter easily; use its code to implement a new API sq_dequote_step() to allow the caller iterate through such a string to parse them one-by-one. The original sq_dequote() becomes a thin wrapper around it. Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Christian Couder committed Mar 29, 2009 at 11:44 UTC ebbc088e13e1bf0dbf8eb08b00519602c176f864
2 files changed +24 -2
quote.c
+16 -2
@@ -72,7 +72,7 @@ void sq_quote_argv(struct strbuf *dst, const char** argv, size_t maxlen)
72 }
73 }
74
75 -char *sq_dequote(char *arg)
75 +char *sq_dequote_step(char *arg, char **next)
76 {
77 char *dst = arg;
78 char *src = arg;
@@ -92,6 +92,8 @@ char *sq_dequote(char *arg)
92 switch (*++src) {
93 case '\0':
94 *dst = 0;
95 + if (next)
96 + *next = NULL;
97 return arg;
98 case '\\':
99 c = *++src;
@@ -101,11 +103,23 @@ char *sq_dequote(char *arg)
103 }
104 /* Fallthrough */
105 default:
104 - return NULL;
106 + if (!next || !isspace(*src))
107 + return NULL;
108 + do {
109 + c = *++src;
110 + } while (isspace(c));
111 + *dst = 0;
112 + *next = src;
113 + return arg;
114 }
115 }
116 }
117
118 +char *sq_dequote(char *arg)
119 +{
120 + return sq_dequote_step(arg, NULL);
121 +}
122 +
123 /* 1 means: quote as octal
124 * 0 means: quote as octal if (quote_path_fully)
125 * -1 means: never quote
quote.h
+8
@@ -39,6 +39,14 @@ extern void sq_quote_argv(struct strbuf *, const char **argv, size_t maxlen);
39 */
40 extern char *sq_dequote(char *);
41
42 +/*
43 + * Same as the above, but can be used to unwrap many arguments in the
44 + * same string separated by space. "next" is changed to point to the
45 + * next argument that should be passed as first parameter. When there
46 + * is no more argument to be dequoted, "next" is updated to point to NULL.
47 + */
48 +extern char *sq_dequote_step(char *arg, char **next);
49 +
50 extern int unquote_c_style(struct strbuf *, const char *quoted, const char **endp);
51 extern size_t quote_c_style(const char *name, struct strbuf *, FILE *, int no_dq);
52 extern void quote_two_c_style(struct strbuf *, const char *, const char *, int);