Some use may type command like this at the virsh shell:
virsh # somecmd 'some arg'
because some users often use single quote in linux shell.
Signed-off-by: Lai Jiangshan <laijs(a)cn.fujitsu.com>
---
diff --git a/tools/virsh.c b/tools/virsh.c
index b96071d..a5b438b 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -10213,6 +10213,7 @@ static int vshCommandArgvParse(vshControl *ctl, int nargs, char
**argv)
static int
vshCommandStringGetArg(vshControl *ctl, vshCommandParser *parser, char **res)
{
+ bool single_quote = false;
bool double_quote = false;
int sz = 0;
char *p = parser->pos, *q;
@@ -10232,14 +10233,23 @@ vshCommandStringGetArg(vshControl *ctl, vshCommandParser
*parser, char **res)
copy:
while (*p) {
/* end of token is blank space or ';' */
- if (!double_quote && (*p == ' ' || *p == '\t' || *p ==
';'))
+ if (!double_quote && !single_quote
+ && (*p == ' ' || *p == '\t' || *p ==
';'))
break;
- if (*p == '\\') { /* escape */
+ if (!double_quote && *p == '\'') { /* single quote */
+ single_quote = !single_quote;
+ p++;
+ continue;
+ } else if (!single_quote && *p == '\\') { /* escape */
+ /*
+ * The same as the bash, a \ in "" is an escaper,
+ * but a \ in '' is not an escaper.
+ */
p++;
if (*p == '\0')
break;
- } else if (*p == '"') { /* double quote */
+ } else if (!single_quote && *p == '"') { /* double quote */
double_quote = !double_quote;
p++;
continue;