psh: Implement a parser for "quoted" commands#182
Open
Conversation
JIRA: RTOS-512
Change adds support for quoted commands (single ', and double " quote), a quote inside quote requires an escape character \' or \" depending on the main type of quote used. Example: "argument text \"inner quote\" 'other quote' end" or 'argument text \'inner quote\' "other quote" end'. JIRA: RTOS-512
Let `echo` command output the arguments as received from parser, do not eat " quote mark. JIRA: RTOS-512
c520571 to
60ebaba
Compare
kemonats
reviewed
Jul 2, 2023
|
|
||
|
|
||
| static char *psh_stralloc(char *oldstr, const char *str) | ||
| char *psh_stralloc(char *oldstr, const char *str) |
Contributor
There was a problem hiding this comment.
A slightly simpler version of psh_stralloc()
char *psh_stralloc_t(char *oldstr, const char *str)
{
char *newstr = strdup(str);
if (newstr != NULL) {
free(oldstr);
}
return newstr;
}
kemonats
reviewed
Jul 2, 2023
Comment on lines
+845
to
+847
| while (*ptr != '\0' && isspace(*ptr)) { | ||
| ptr++; | ||
| } |
Contributor
There was a problem hiding this comment.
Suggested change
| while (*ptr != '\0' && isspace(*ptr)) { | |
| ptr++; | |
| } | |
| while (isspace(*ptr) != 0) { | |
| ptr++; | |
| } |
kemonats
reviewed
Jul 2, 2023
Comment on lines
+858
to
+860
| if (*ptr == '\0' || *(ptr - 1) != '\\') { | ||
| break; | ||
| } |
Contributor
There was a problem hiding this comment.
This can be added to skip the psh_unescape() function call if there is no backaslash in quotation marks.
On top declare:
int need_unscape = 0;
Suggested change
| if (*ptr == '\0' || *(ptr - 1) != '\\') { | |
| break; | |
| } | |
| if (*ptr == '\0') { | |
| break; | |
| } | |
| if (*(ptr - 1) == '\\') { | |
| need_unscape = 1; | |
| } | |
| else { | |
| break; | |
| } |
and return
return (need_unscape == 0) ? ret : psh_unescape(ret, quote);
kemonats
reviewed
Jul 5, 2023
Comment on lines
+872
to
+874
| while (*ptr != '\0' && isspace(*ptr)) { | ||
| ptr++; | ||
| } |
Contributor
There was a problem hiding this comment.
I see this was lost somewhere early.
Suggested change
| while (*ptr != '\0' && isspace(*ptr)) { | |
| ptr++; | |
| } |
You skip the whitespace characters at the beginning of the function.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Work in progress - do not merge.
I open this pull-request for discussion, maybe someone has already found better solution.
psh tests may fail because this change:
Description
This pull-request adds support for quoted commands (single
'and double"quote), a quote inside quote requires an escape character\'or\"depending on the main type of quote used. Example:"argument text \"inner quote\" 'other quote' end"or'argument text \'inner quote\' "other quote" end'The efects of this change:

Before the change (originally), the
strtok()function was used, which modifies the command line stored in the command history, I implemented the function behaves similarly except that it allows the use of quotes. There is one, but ... now it uses a scratchpad where it stores a copy of the command line it is working on. This is necessary because the characters",'and\"\"are overwritten and the text is moved in the buffer, without the scratch buffer, the command history would be modified and the command would lose its meaning.Support for
\without quotes is missing now, maybe I'll add this once I extend path completion to also support\, soe.g.
cat /dir\ with\ spaces/file\ with\ spacesis not supported in this pull-request, instead usecat "/dir with spaces/file with spaces":Motivation and Context
Need to support arguments that contains spaces and quotation marks.
Types of changes
How Has This Been Tested?
ia32-generic,nil-imxrt1176Checklist:
Special treatment