mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2026-01-01 01:41:18 +01:00
Allow escaped characters in shell
Per #708. This patch allows escaped characters in the shell, and makes the apostrophe a quotation mark. The escape character is backslash. The term "escape character" is used liberally in here: if a backslash is encountered in the command line, the next character will be taken verbatim. No escape sequences are understood, i.e. `"\n"` is just the letter `n`, not a new line.
This commit is contained in:
parent
0c14597ec2
commit
03e174ed96
@ -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…
x
Reference in New Issue
Block a user