Patrick Dignan <pat_dignan@dell.com>
Serge Hallyn <serge.hallyn@canonical.com>
Nikunj A. Dadhania <nikunj@linux.vnet.ibm.com>
+ Lai Jiangshan <laijs@cn.fujitsu.com>
[....send patches to get your name here....]
#define VSH_TK_DATA 2
#define VSH_TK_END 3
-static int
+static int ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4)
vshCommandGetToken(vshControl *ctl, char *str, char **end, char **res)
{
int tk = VSH_TK_NONE;
- int quote = FALSE;
+ bool double_quote = false;
int sz = 0;
char *p = str;
+ char *q = vshStrdup(ctl, str);
char *tkstr = NULL;
*end = NULL;
+ *res = q;
while (p && *p && (*p == ' ' || *p == '\t'))
p++;
*end = ++p; /* = \0 or begin of next command */
return VSH_TK_END;
}
+
while (*p) {
/* end of token is blank space or ';' */
- if ((quote == FALSE && (*p == ' ' || *p == '\t')) || *p == ';')
+ if (!double_quote && (*p == ' ' || *p == '\t' || *p == ';'))
break;
/* end of option name could be '=' */
p += 2;
} else {
tk = VSH_TK_DATA;
- if (*p == '"') {
- quote = TRUE;
- p++;
- } else {
- quote = FALSE;
- }
}
tkstr = p; /* begin of token */
- } else if (quote && *p == '"') {
- quote = FALSE;
+ }
+
+ if (*p == '"') {
+ double_quote = !double_quote;
p++;
- break; /* end of "..." token */
+ continue;
}
- p++;
+
+ *q++ = *p++;
sz++;
}
- if (quote) {
+ if (double_quote) {
vshError(ctl, "%s", _("missing \""));
return VSH_TK_ERROR;
}
- if (tkstr == NULL || *tkstr == '\0' || p == NULL)
+ if (tkstr == NULL || *tkstr == '\0' || sz == 0)
return VSH_TK_END;
- if (sz == 0)
- return VSH_TK_END;
-
- *res = vshMalloc(ctl, sz + 1);
- memcpy(*res, tkstr, sz);
- *(*res + sz) = '\0';
+ *q = '\0';
*end = p;
return tk;
}