1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-12-03 09:00:21 +00:00

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:
Jilles Tjoelker 2015-11-01 22:07:40 +00:00
parent 0265aa0a15
commit 046bfe5240
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=290244
3 changed files with 11 additions and 11 deletions

View File

@ -750,7 +750,7 @@ isdeclarationcmd(struct narg *arg)
}
static void
xtracecommand(struct arglist *varlist, struct arglist *arglist)
xtracecommand(struct arglist *varlist, int argc, char **argv)
{
char sep = 0;
const char *text, *p, *ps4;
@ -771,8 +771,8 @@ xtracecommand(struct arglist *varlist, struct arglist *arglist)
out2qstr(text);
sep = ' ';
}
for (i = 0; i < arglist->count; i++) {
text = arglist->args[i];
for (i = 0; i < argc; i++) {
text = argv[i];
if (sep != 0)
out2c(' ');
out2qstr(text);
@ -849,6 +849,8 @@ evalcommand(union node *cmd, int flags, struct backcmd *backcmd)
do_clearcmdentry = 0;
oexitstatus = exitstatus;
exitstatus = 0;
/* Add one slot at the beginning for tryexec(). */
appendarglist(&arglist, nullstr);
for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
if (varflag && isassignment(argp->narg.text)) {
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;
expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
}
appendarglist(&arglist, nullstr);
expredir(cmd->ncmd.redirect);
argc = arglist.count;
/* Add one slot at the beginning for tryexec(). */
argv = stalloc(sizeof (char *) * (argc + 2));
argv++;
argc = arglist.count - 2;
argv = &arglist.args[1];
memcpy(argv, arglist.args, sizeof(*argv) * argc);
argv[argc] = NULL;
lastarg = NULL;
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. */
if (xflag)
xtracecommand(&varlist, &arglist);
xtracecommand(&varlist, argc, argv);
/* Now locate the command. */
if (argc == 0) {

View File

@ -114,7 +114,6 @@ static void expmeta(char *, char *, struct arglist *);
static int expsortcmp(const void *, const void *);
static int patmatch(const char *, const char *, int);
static char *cvtnum(int, char *);
static void appendarglist(struct arglist *, char *);
static int collate_range_cmp(wchar_t, wchar_t);
void
@ -126,7 +125,7 @@ emptyarglist(struct arglist *list)
list->capacity = sizeof(list->smallarg) / sizeof(list->smallarg[0]);
}
static void
void
appendarglist(struct arglist *list, char *str)
{
char **newargs;

View File

@ -52,6 +52,7 @@ struct arglist {
void emptyarglist(struct arglist *);
void appendarglist(struct arglist *, char *);
union node;
void expandarg(union node *, struct arglist *, int);
void rmescapes(char *);