Added logic for single quote arguments (verbatim).

This can then be used to quote Windows paths that are passed to mspdebug
by Energia:

> mspdebug ... "prog 'C:\Users\Path with Spaces\...\file.hex'"

Signed-off-by: Bruno Pena <brunompena@gmail.com>
This commit is contained in:
Bruno Pena 2018-02-27 22:55:05 +01:00
parent e88302e356
commit ad8063166e
1 changed files with 23 additions and 11 deletions

View File

@ -60,6 +60,8 @@ char *get_arg(char **text)
goto out; goto out;
else if (*end == '"') else if (*end == '"')
qstate = 1; qstate = 1;
else if (*end == '\'')
qstate = 2;
else else
*(rewrite++) = *end; *(rewrite++) = *end;
break; break;
@ -67,13 +69,23 @@ char *get_arg(char **text)
case 1: /* In quotes */ case 1: /* In quotes */
if (*end == '"') if (*end == '"')
qstate = 0; qstate = 0;
else if (*end == '\'')
qstate = 3;
else if (*end == '\\') else if (*end == '\\')
qstate = 2; qstate = 4;
else else
*(rewrite++) = *end; *(rewrite++) = *end;
break; break;
case 2: /* Backslash */ case 2: /* In quote (verbatim) */
case 3:
if (*end == '\'')
qstate -= 2;
else
*(rewrite++) = *end;
break;
case 4: /* Backslash */
if (*end == '\\') if (*end == '\\')
*(rewrite++) = '\\'; *(rewrite++) = '\\';
else if (*end == 'n') else if (*end == 'n')
@ -83,24 +95,24 @@ char *get_arg(char **text)
else if (*end == 't') else if (*end == 't')
*(rewrite++) = '\t'; *(rewrite++) = '\t';
else if (*end >= '0' && *end <= '3') { else if (*end >= '0' && *end <= '3') {
qstate = 30; qstate = 50;
qval = *end - '0'; qval = *end - '0';
} else if (*end == 'x') { } else if (*end == 'x') {
qstate = 40; qstate = 60;
qval = 0; qval = 0;
} else } else
*(rewrite++) = *end; *(rewrite++) = *end;
if (qstate == 2) if (qstate == 4)
qstate = 1; qstate = 1;
break; break;
case 30: /* Octal */ case 50: /* Octal */
case 31: case 51:
if (*end >= '0' && *end <= '7') if (*end >= '0' && *end <= '7')
qval = (qval << 3) | (*end - '0'); qval = (qval << 3) | (*end - '0');
if (qstate == 31) { if (qstate == 51) {
*(rewrite++) = qval; *(rewrite++) = qval;
qstate = 1; qstate = 1;
} else { } else {
@ -108,8 +120,8 @@ char *get_arg(char **text)
} }
break; break;
case 40: /* Hex */ case 60: /* Hex */
case 41: case 61:
if (isdigit(*end)) if (isdigit(*end))
qval = (qval << 4) | (*end - '0'); qval = (qval << 4) | (*end - '0');
else if (isupper(*end)) else if (isupper(*end))
@ -117,7 +129,7 @@ char *get_arg(char **text)
else if (islower(*end)) else if (islower(*end))
qval = (qval << 4) | (*end - 'a' + 10); qval = (qval << 4) | (*end - 'a' + 10);
if (qstate == 41) { if (qstate == 61) {
*(rewrite++) = qval; *(rewrite++) = qval;
qstate = 1; qstate = 1;
} else { } else {