Skip to content

Commit

Permalink
[shell] Add handling of escape and tabs. (#1604)
Browse files Browse the repository at this point in the history
* [shell] Add handling of escape and tabs.

* [shell] Add unit tests for new escape handling.

* [shell] Fix typo in tests.  Posted #1605.
  • Loading branch information
turon authored Jul 15, 2020
1 parent 475b533 commit dc05393
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/lib/shell/shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ int shell_line_read(char * buffer, size_t max)
}
break;
default:
if (isprint((int) *inptr))
if (isprint((int) *inptr) || *inptr == '\t')
{
streamer_printf(streamer_get(), "%c", *inptr);
}
Expand Down Expand Up @@ -147,6 +147,21 @@ int Shell::ExecCommand(int argc, char * argv[])
return retval;
}

static bool IsSeparator(char aChar)
{
return (aChar == ' ') || (aChar == '\t') || (aChar == '\r') || (aChar == '\n');
}

static bool IsEscape(char aChar)
{
return (aChar == '\\');
}

static bool IsEscapable(char aChar)
{
return IsSeparator(aChar) || IsEscape(aChar);
}

int Shell::TokenizeLine(char * buffer, char ** tokens, int max_tokens)
{
int len = strlen(buffer);
Expand All @@ -166,10 +181,15 @@ int Shell::TokenizeLine(char * buffer, char ** tokens, int max_tokens)

for (; i < len && cursor < max_tokens; i++)
{
if (buffer[i] == ' ')
if (IsEscape(buffer[i]) && IsEscapable(buffer[i + 1]))
{
// include the null terminator: strlen(cmd) = strlen(cmd + 1) + 1
memmove(&buffer[i], &buffer[i + 1], strlen(&buffer[i]));
}
else if (IsSeparator(buffer[i]))
{
buffer[i] = 0;
if (buffer[i + 1] != ' ')
if (!IsSeparator(buffer[i + 1]))
{
tokens[cursor++] = &buffer[i + 1];
}
Expand Down
3 changes: 3 additions & 0 deletions src/lib/shell/tests/TestShell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ static const struct test_shell_vector test_vector_shell_tokenizer[] = {
{ .line = " leading space", .argv = (const char *[]){ "leading", "space" } },
{ .line = "trailing space ", .argv = (const char *[]){ "trailing", "space", "" } },
{ .line = "no_space", .argv = (const char *[]){ "no_space" } },
{ .line = "escaped\\ space", .argv = (const char *[]){ "escaped space" } },
{ .line = "escape\\\\", .argv = (const char *[]){ "escape\\" } },
{ .line = "extended\\ escaped\\ space and\\ more", .argv = (const char *[]){ "extended escaped space", "and more" } },
{ .line = " ", .argv = (const char *[]){ "" } },
{ .line = "", .argv = (const char *[]){} },
};
Expand Down

0 comments on commit dc05393

Please sign in to comment.