mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
Merge pull request #794 from Kijewski/shell-args-escape
shell: Allow escaped characters in shell
This commit is contained in:
commit
eb0ceeb773
@ -95,18 +95,30 @@ static void handle_input_line(shell_t *shell, char *line)
|
||||
/* first we need to calculate the number of arguments */
|
||||
unsigned argc = 0;
|
||||
char *pos = line;
|
||||
int contains_esc_seq = 0;
|
||||
while (1) {
|
||||
if (*pos > ' ') {
|
||||
/* found an argument */
|
||||
if (*pos == '"') {
|
||||
/* it's an quoted argument */
|
||||
if (*pos == '"' || *pos == '\'') {
|
||||
/* it's a quoted argument */
|
||||
const char quote_char = *pos;
|
||||
do {
|
||||
++pos;
|
||||
if (!*pos) {
|
||||
puts(INCORRECT_QUOTING);
|
||||
return;
|
||||
}
|
||||
} while (*pos != '"');
|
||||
else if (*pos == '\\') {
|
||||
/* skip over the next character */
|
||||
++contains_esc_seq;
|
||||
++pos;
|
||||
if (!*pos) {
|
||||
puts(INCORRECT_QUOTING);
|
||||
return;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
} while (*pos != quote_char);
|
||||
if (pos[1] > ' ') {
|
||||
puts(INCORRECT_QUOTING);
|
||||
return;
|
||||
@ -115,6 +127,15 @@ static void handle_input_line(shell_t *shell, char *line)
|
||||
else {
|
||||
/* it's an unquoted argument */
|
||||
do {
|
||||
if (*pos == '\\') {
|
||||
/* skip over the next character */
|
||||
++contains_esc_seq;
|
||||
++pos;
|
||||
if (!*pos) {
|
||||
puts(INCORRECT_QUOTING);
|
||||
return;
|
||||
}
|
||||
}
|
||||
++pos;
|
||||
if (*pos == '"') {
|
||||
puts(INCORRECT_QUOTING);
|
||||
@ -148,7 +169,7 @@ static void handle_input_line(shell_t *shell, char *line)
|
||||
while (!*pos) {
|
||||
++pos;
|
||||
}
|
||||
if (*pos == '"') {
|
||||
if (*pos == '"' || *pos == '\'') {
|
||||
++pos;
|
||||
}
|
||||
argv[i] = pos;
|
||||
@ -156,6 +177,19 @@ static void handle_input_line(shell_t *shell, char *line)
|
||||
++pos;
|
||||
}
|
||||
}
|
||||
for (char **arg = argv; contains_esc_seq && *arg; ++arg) {
|
||||
for (char *c = *arg; *c; ++c) {
|
||||
if (*c != '\\') {
|
||||
continue;
|
||||
}
|
||||
for (char *d = c; *d; ++d) {
|
||||
*d = d[1];
|
||||
}
|
||||
if (--contains_esc_seq == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* then we call the appropriate handler */
|
||||
shell_command_handler_t handler = find_handler(shell->command_list, argv[0]);
|
||||
|
Loading…
Reference in New Issue
Block a user