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>
This commit is contained in:
parent
89fbda2425
commit
ebbc088e13
18
quote.c
18
quote.c
@ -72,7 +72,7 @@ void sq_quote_argv(struct strbuf *dst, const char** argv, size_t maxlen)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
char *sq_dequote(char *arg)
|
char *sq_dequote_step(char *arg, char **next)
|
||||||
{
|
{
|
||||||
char *dst = arg;
|
char *dst = arg;
|
||||||
char *src = arg;
|
char *src = arg;
|
||||||
@ -92,6 +92,8 @@ char *sq_dequote(char *arg)
|
|||||||
switch (*++src) {
|
switch (*++src) {
|
||||||
case '\0':
|
case '\0':
|
||||||
*dst = 0;
|
*dst = 0;
|
||||||
|
if (next)
|
||||||
|
*next = NULL;
|
||||||
return arg;
|
return arg;
|
||||||
case '\\':
|
case '\\':
|
||||||
c = *++src;
|
c = *++src;
|
||||||
@ -101,11 +103,23 @@ char *sq_dequote(char *arg)
|
|||||||
}
|
}
|
||||||
/* Fallthrough */
|
/* Fallthrough */
|
||||||
default:
|
default:
|
||||||
return NULL;
|
if (!next || !isspace(*src))
|
||||||
|
return NULL;
|
||||||
|
do {
|
||||||
|
c = *++src;
|
||||||
|
} while (isspace(c));
|
||||||
|
*dst = 0;
|
||||||
|
*next = src;
|
||||||
|
return arg;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *sq_dequote(char *arg)
|
||||||
|
{
|
||||||
|
return sq_dequote_step(arg, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
/* 1 means: quote as octal
|
/* 1 means: quote as octal
|
||||||
* 0 means: quote as octal if (quote_path_fully)
|
* 0 means: quote as octal if (quote_path_fully)
|
||||||
* -1 means: never quote
|
* -1 means: never quote
|
||||||
|
8
quote.h
8
quote.h
@ -39,6 +39,14 @@ extern void sq_quote_argv(struct strbuf *, const char **argv, size_t maxlen);
|
|||||||
*/
|
*/
|
||||||
extern char *sq_dequote(char *);
|
extern char *sq_dequote(char *);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Same as the above, but can be used to unwrap many arguments in the
|
||||||
|
* same string separated by space. "next" is changed to point to the
|
||||||
|
* next argument that should be passed as first parameter. When there
|
||||||
|
* is no more argument to be dequoted, "next" is updated to point to NULL.
|
||||||
|
*/
|
||||||
|
extern char *sq_dequote_step(char *arg, char **next);
|
||||||
|
|
||||||
extern int unquote_c_style(struct strbuf *, const char *quoted, const char **endp);
|
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 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);
|
extern void quote_two_c_style(struct strbuf *, const char *, const char *, int);
|
||||||
|
Loading…
Reference in New Issue
Block a user