Skip to content

Commit

Permalink
[REJECTED] add-var-expansion-in-nsh-parse.patch
Browse files Browse the repository at this point in the history
  • Loading branch information
dagar authored and PX4 Jenkins committed Jun 15, 2018
1 parent 133f239 commit 3236f02
Showing 1 changed file with 76 additions and 23 deletions.
99 changes: 76 additions & 23 deletions nshlib/nsh_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ static FAR char *nsh_envexpand(FAR struct nsh_vtbl_s *vtbl,
#endif

static FAR char *nsh_argexpand(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline,
FAR char **allocation);
FAR char **allocation, int* isenvvar);
static FAR char *nsh_argument(FAR struct nsh_vtbl_s *vtbl, char **saveptr,
FAR NSH_MEMLIST_TYPE *memlist);
FAR NSH_MEMLIST_TYPE *memlist, int* isenvvar);

#ifndef CONFIG_NSH_DISABLESCRIPT
#ifndef CONFIG_NSH_DISABLE_LOOPS
Expand Down Expand Up @@ -1010,7 +1010,7 @@ static FAR char *nsh_envexpand(FAR struct nsh_vtbl_s *vtbl,

#if defined(CONFIG_NSH_ARGCAT) && defined(HAVE_MEMLIST)
static FAR char *nsh_argexpand(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline,
FAR char **allocation)
FAR char **allocation, int* isenvvar)
{
FAR char *working = cmdline;
FAR char *argument = NULL;
Expand Down Expand Up @@ -1129,8 +1129,8 @@ static FAR char *nsh_argexpand(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline,
*allocation = argument;

/* Find the end of the environment variable reference. If the
* dollar sign ('$') is followed by a right bracket ('{') then the
* variable name is terminated with the left bracket character
* dollar sign ('$') is followed by a left bracket ('{') then the
* variable name is terminated with the right bracket character
* ('}'). Otherwise, the variable name goes to the end of the
* argument.
*/
Expand Down Expand Up @@ -1168,6 +1168,10 @@ static FAR char *nsh_argexpand(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline,
* nsh_envexpand will return the NULL string.
*/

if (isenvvar) {
*isenvvar = 1;
}

envstr = nsh_envexpand(vtbl, ptr);

#ifndef CONFIG_NSH_DISABLESCRIPT
Expand Down Expand Up @@ -1199,7 +1203,7 @@ static FAR char *nsh_argexpand(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline,

#else
static FAR char *nsh_argexpand(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline,
FAR char **allocation)
FAR char **allocation, int* isenvvar)
{
FAR char *argument = (FAR char *)g_nullstring;

Expand Down Expand Up @@ -1235,6 +1239,9 @@ static FAR char *nsh_argexpand(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline,

if (*cmdline == '$')
{
if (isenvvar) {
*isenvvar = 1;
}
argument = nsh_envexpand(vtbl, cmdline + 1);
}
else
Expand All @@ -1257,7 +1264,7 @@ static FAR char *nsh_argexpand(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline,
****************************************************************************/

static FAR char *nsh_argument(FAR struct nsh_vtbl_s *vtbl, FAR char **saveptr,
FAR NSH_MEMLIST_TYPE *memlist)
FAR NSH_MEMLIST_TYPE *memlist, int* isenvvar)
{
FAR char *pbegin = *saveptr;
FAR char *pend = NULL;
Expand Down Expand Up @@ -1330,6 +1337,13 @@ static FAR char *nsh_argument(FAR struct nsh_vtbl_s *vtbl, FAR char **saveptr,

pbegin++;
term = "\"";

/* If this is an environment variable in double quotes, we don't want it split into
* multiple argument should CONFIG_NSH_ENABLEPX4PARSING be defined
* So just invalidate the flag pointer which would otherwise communictate such
* back up the call tree.
*/
isenvvar = NULL;
}
else
{
Expand Down Expand Up @@ -1400,11 +1414,11 @@ static FAR char *nsh_argument(FAR struct nsh_vtbl_s *vtbl, FAR char **saveptr,

/* Perform expansions as necessary for the argument */

argument = nsh_argexpand(vtbl, pbegin, &allocation);
argument = nsh_argexpand(vtbl, pbegin, &allocation, isenvvar);
}

/* If any memory was allocated for this argument, make sure that it is
* added to the list of memory to be freed at the end of commend
* added to the list of memory to be freed at the end of command
* processing.
*/

Expand Down Expand Up @@ -1521,7 +1535,7 @@ static int nsh_loop(FAR struct nsh_vtbl_s *vtbl, FAR char **ppcmd,

/* Get the cmd following the "while" or "until" */

*ppcmd = nsh_argument(vtbl, saveptr, memlist);
*ppcmd = nsh_argument(vtbl, saveptr, memlist, 0);
if (!*ppcmd)
{
nsh_output(vtbl, g_fmtarginvalid, "if");
Expand Down Expand Up @@ -1578,7 +1592,7 @@ static int nsh_loop(FAR struct nsh_vtbl_s *vtbl, FAR char **ppcmd,
{
/* Get the cmd following the "do" -- there may or may not be one */

*ppcmd = nsh_argument(vtbl, saveptr, memlist);
*ppcmd = nsh_argument(vtbl, saveptr, memlist, 0);

/* Verify that "do" is valid in this context */

Expand All @@ -1598,7 +1612,7 @@ static int nsh_loop(FAR struct nsh_vtbl_s *vtbl, FAR char **ppcmd,
{
/* Get the cmd following the "done" -- there should be one */

*ppcmd = nsh_argument(vtbl, saveptr, memlist);
*ppcmd = nsh_argument(vtbl, saveptr, memlist, 0);
if (*ppcmd)
{
nsh_output(vtbl, g_fmtarginvalid, "done");
Expand Down Expand Up @@ -1703,7 +1717,7 @@ static int nsh_itef(FAR struct nsh_vtbl_s *vtbl, FAR char **ppcmd,
{
/* Get the cmd following the if */

*ppcmd = nsh_argument(vtbl, saveptr, memlist);
*ppcmd = nsh_argument(vtbl, saveptr, memlist, 0);
if (!*ppcmd)
{
nsh_output(vtbl, g_fmtarginvalid, "if");
Expand Down Expand Up @@ -1741,7 +1755,7 @@ static int nsh_itef(FAR struct nsh_vtbl_s *vtbl, FAR char **ppcmd,
{
/* Get the cmd following the "then" -- there may or may not be one */

*ppcmd = nsh_argument(vtbl, saveptr, memlist);
*ppcmd = nsh_argument(vtbl, saveptr, memlist, 0);

/* Verify that "then" is valid in this context */

Expand All @@ -1760,7 +1774,7 @@ static int nsh_itef(FAR struct nsh_vtbl_s *vtbl, FAR char **ppcmd,
{
/* Get the cmd following the "else" -- there may or may not be one */

*ppcmd = nsh_argument(vtbl, saveptr, memlist);
*ppcmd = nsh_argument(vtbl, saveptr, memlist, 0);

/* Verify that "else" is valid in this context */

Expand All @@ -1779,7 +1793,7 @@ static int nsh_itef(FAR struct nsh_vtbl_s *vtbl, FAR char **ppcmd,
{
/* Get the cmd following the fi -- there should be one */

*ppcmd = nsh_argument(vtbl, saveptr, memlist);
*ppcmd = nsh_argument(vtbl, saveptr, memlist, 0);
if (*ppcmd)
{
nsh_output(vtbl, g_fmtarginvalid, "fi");
Expand Down Expand Up @@ -1851,10 +1865,10 @@ static int nsh_nice(FAR struct nsh_vtbl_s *vtbl, FAR char **ppcmd,

/* Get the cmd (or -d option of nice command) */

cmd = nsh_argument(vtbl, saveptr, memlist);
cmd = nsh_argument(vtbl, saveptr, memlist, 0);
if (cmd && strcmp(cmd, "-d") == 0)
{
FAR char *val = nsh_argument(vtbl, saveptr, memlist);
FAR char *val = nsh_argument(vtbl, saveptr, memlist, 0);
if (val)
{
char *endptr;
Expand All @@ -1865,7 +1879,7 @@ static int nsh_nice(FAR struct nsh_vtbl_s *vtbl, FAR char **ppcmd,
nsh_output(vtbl, g_fmtarginvalid, "nice");
return ERROR;
}
cmd = nsh_argument(vtbl, saveptr, memlist);
cmd = nsh_argument(vtbl, saveptr, memlist, 0);
}
}

Expand Down Expand Up @@ -1935,7 +1949,7 @@ static int nsh_parse_cmdparm(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline,
/* Parse out the command at the beginning of the line */

saveptr = cmdline;
cmd = nsh_argument(vtbl, &saveptr, &memlist);
cmd = nsh_argument(vtbl, &saveptr, &memlist, 0);

/* Check if any command was provided -OR- if command processing is
* currently disabled.
Expand Down Expand Up @@ -1969,7 +1983,7 @@ static int nsh_parse_cmdparm(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline,
argv[0] = cmd;
for (argc = 1; argc < MAX_ARGV_ENTRIES-1; argc++)
{
argv[argc] = nsh_argument(vtbl, &saveptr, &memlist);
argv[argc] = nsh_argument(vtbl, &saveptr, &memlist, 0);
if (!argv[argc])
{
break;
Expand Down Expand Up @@ -2036,7 +2050,7 @@ static int nsh_parse_command(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline)
/* Parse out the command at the beginning of the line */

saveptr = cmdline;
cmd = nsh_argument(vtbl, &saveptr, &memlist);
cmd = nsh_argument(vtbl, &saveptr, &memlist, 0);

#ifndef CONFIG_NSH_DISABLESCRIPT
#ifndef CONFIG_NSH_DISABLE_LOOPS
Expand Down Expand Up @@ -2104,15 +2118,54 @@ static int nsh_parse_command(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline)
*/

argv[0] = cmd;

#define CONFIG_NSH_ENABLEPX4PARSING

for (argc = 1; argc < MAX_ARGV_ENTRIES-1; argc++)
{
argv[argc] = nsh_argument(vtbl, &saveptr, &memlist);
int isenvvar = 0; /* flag for if an enviroment variable gets expanded */
argv[argc] = nsh_argument(vtbl, &saveptr, &memlist, &isenvvar);
if (!argv[argc])
{
break;
}

#ifdef CONFIG_NSH_ENABLEPX4PARSING
if (isenvvar)
{
while (argc < MAX_ARGV_ENTRIES-1) /* TODO: check this bounds check is correct */
{
FAR char *pbegin = argv[argc];

/* Find the end of the current token */
for (; *pbegin && !strchr(g_token_separator, *pbegin); pbegin++);

/* If end of string, we've processed the last token and we're done */
if ('\0' == *pbegin)
{
break;
}

/* Terminate the token to complete the argv variable */
*pbegin = '\0';

/* We've inserted an extra parameter, so bump the count */
argc++;

/* Move to the next character in the string of tokens */
pbegin++;

/* Throw away any extra separator chars between tokens */
for (; *pbegin && strchr(g_token_separator, *pbegin) != NULL; pbegin++);

/* Prepare to loop again on the next argument token */
argv[argc] = pbegin;
}
}
#endif /* CONFIG_NSH_ENABLEPX4PARSING */
}

/* Last argument vector must be empty */
argv[argc] = NULL;

/* Check if the command should run in background */
Expand Down

0 comments on commit 3236f02

Please sign in to comment.