sh: Avoid copying argv for simple commands.
Add dummy entries before and after so arglist's array is directly usable as argv.
This commit is contained in:
parent
0265aa0a15
commit
046bfe5240
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=290244
@ -750,7 +750,7 @@ isdeclarationcmd(struct narg *arg)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
xtracecommand(struct arglist *varlist, struct arglist *arglist)
|
xtracecommand(struct arglist *varlist, int argc, char **argv)
|
||||||
{
|
{
|
||||||
char sep = 0;
|
char sep = 0;
|
||||||
const char *text, *p, *ps4;
|
const char *text, *p, *ps4;
|
||||||
@ -771,8 +771,8 @@ xtracecommand(struct arglist *varlist, struct arglist *arglist)
|
|||||||
out2qstr(text);
|
out2qstr(text);
|
||||||
sep = ' ';
|
sep = ' ';
|
||||||
}
|
}
|
||||||
for (i = 0; i < arglist->count; i++) {
|
for (i = 0; i < argc; i++) {
|
||||||
text = arglist->args[i];
|
text = argv[i];
|
||||||
if (sep != 0)
|
if (sep != 0)
|
||||||
out2c(' ');
|
out2c(' ');
|
||||||
out2qstr(text);
|
out2qstr(text);
|
||||||
@ -849,6 +849,8 @@ evalcommand(union node *cmd, int flags, struct backcmd *backcmd)
|
|||||||
do_clearcmdentry = 0;
|
do_clearcmdentry = 0;
|
||||||
oexitstatus = exitstatus;
|
oexitstatus = exitstatus;
|
||||||
exitstatus = 0;
|
exitstatus = 0;
|
||||||
|
/* Add one slot at the beginning for tryexec(). */
|
||||||
|
appendarglist(&arglist, nullstr);
|
||||||
for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
|
for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
|
||||||
if (varflag && isassignment(argp->narg.text)) {
|
if (varflag && isassignment(argp->narg.text)) {
|
||||||
expandarg(argp, varflag == 1 ? &varlist : &arglist,
|
expandarg(argp, varflag == 1 ? &varlist : &arglist,
|
||||||
@ -858,13 +860,11 @@ evalcommand(union node *cmd, int flags, struct backcmd *backcmd)
|
|||||||
varflag = isdeclarationcmd(&argp->narg) ? 2 : 0;
|
varflag = isdeclarationcmd(&argp->narg) ? 2 : 0;
|
||||||
expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
|
expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
|
||||||
}
|
}
|
||||||
|
appendarglist(&arglist, nullstr);
|
||||||
expredir(cmd->ncmd.redirect);
|
expredir(cmd->ncmd.redirect);
|
||||||
argc = arglist.count;
|
argc = arglist.count - 2;
|
||||||
/* Add one slot at the beginning for tryexec(). */
|
argv = &arglist.args[1];
|
||||||
argv = stalloc(sizeof (char *) * (argc + 2));
|
|
||||||
argv++;
|
|
||||||
|
|
||||||
memcpy(argv, arglist.args, sizeof(*argv) * argc);
|
|
||||||
argv[argc] = NULL;
|
argv[argc] = NULL;
|
||||||
lastarg = NULL;
|
lastarg = NULL;
|
||||||
if (iflag && funcnest == 0 && argc > 0)
|
if (iflag && funcnest == 0 && argc > 0)
|
||||||
@ -872,7 +872,7 @@ evalcommand(union node *cmd, int flags, struct backcmd *backcmd)
|
|||||||
|
|
||||||
/* Print the command if xflag is set. */
|
/* Print the command if xflag is set. */
|
||||||
if (xflag)
|
if (xflag)
|
||||||
xtracecommand(&varlist, &arglist);
|
xtracecommand(&varlist, argc, argv);
|
||||||
|
|
||||||
/* Now locate the command. */
|
/* Now locate the command. */
|
||||||
if (argc == 0) {
|
if (argc == 0) {
|
||||||
|
@ -114,7 +114,6 @@ static void expmeta(char *, char *, struct arglist *);
|
|||||||
static int expsortcmp(const void *, const void *);
|
static int expsortcmp(const void *, const void *);
|
||||||
static int patmatch(const char *, const char *, int);
|
static int patmatch(const char *, const char *, int);
|
||||||
static char *cvtnum(int, char *);
|
static char *cvtnum(int, char *);
|
||||||
static void appendarglist(struct arglist *, char *);
|
|
||||||
static int collate_range_cmp(wchar_t, wchar_t);
|
static int collate_range_cmp(wchar_t, wchar_t);
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -126,7 +125,7 @@ emptyarglist(struct arglist *list)
|
|||||||
list->capacity = sizeof(list->smallarg) / sizeof(list->smallarg[0]);
|
list->capacity = sizeof(list->smallarg) / sizeof(list->smallarg[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
void
|
||||||
appendarglist(struct arglist *list, char *str)
|
appendarglist(struct arglist *list, char *str)
|
||||||
{
|
{
|
||||||
char **newargs;
|
char **newargs;
|
||||||
|
@ -52,6 +52,7 @@ struct arglist {
|
|||||||
|
|
||||||
|
|
||||||
void emptyarglist(struct arglist *);
|
void emptyarglist(struct arglist *);
|
||||||
|
void appendarglist(struct arglist *, char *);
|
||||||
union node;
|
union node;
|
||||||
void expandarg(union node *, struct arglist *, int);
|
void expandarg(union node *, struct arglist *, int);
|
||||||
void rmescapes(char *);
|
void rmescapes(char *);
|
||||||
|
Loading…
Reference in New Issue
Block a user