-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
psh: Implement a parser for "quoted" commands #182
base: master
Are you sure you want to change the base?
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
@@ -77,7 +77,7 @@ const psh_appentry_t *psh_findapp(char *appname) | |||
} | |||
|
|||
|
|||
static char *psh_stralloc(char *oldstr, const char *str) | |||
char *psh_stralloc(char *oldstr, const char *str) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like it.
while (*ptr != '\0' && isspace(*ptr)) { | ||
ptr++; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
while (*ptr != '\0' && isspace(*ptr)) { | |
ptr++; | |
} | |
while (isspace(*ptr) != 0) { | |
ptr++; | |
} |
if (*ptr == '\0' || *(ptr - 1) != '\\') { | ||
break; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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;
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);
while (*ptr != '\0' && isspace(*ptr)) { | ||
ptr++; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see this was lost somewhere early.
while (*ptr != '\0' && isspace(*ptr)) { | |
ptr++; | |
} |
You skip the whitespace characters at the beginning of the function.
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\ spaces
is 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-imxrt1176
Checklist:
Special treatment