From 97fe23139626f1c181213c527159b8f5bb9b93a9 Mon Sep 17 00:00:00 2001 From: Peter Avalos Date: Sun, 21 Aug 2011 13:26:53 -0700 Subject: [PATCH] sh: Do parameter expansion before printing PS4 (set -x). Obtained-from: FreeBSD 222907 --- bin/sh/eval.c | 7 ++-- bin/sh/expand.c | 3 +- bin/sh/parser.c | 46 +++++++++++++++++++++- bin/sh/parser.h | 3 +- bin/sh/sh.1 | 4 +- tools/regression/bin/sh/execution/set-x3.0 | 9 +++++ 6 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 tools/regression/bin/sh/execution/set-x3.0 diff --git a/bin/sh/eval.c b/bin/sh/eval.c index 567a040d00..1bf6a0a57b 100644 --- a/bin/sh/eval.c +++ b/bin/sh/eval.c @@ -34,7 +34,7 @@ * SUCH DAMAGE. * * @(#)eval.c 8.9 (Berkeley) 6/8/95 - * $FreeBSD: src/bin/sh/eval.c,v 1.107 2011/06/05 14:13:15 jilles Exp $ + * $FreeBSD: src/bin/sh/eval.c,v 1.108 2011/06/09 23:12:23 jilles Exp $ */ #include @@ -748,8 +748,9 @@ evalcommand(union node *cmd, int flgs, struct backcmd *backcmd) /* Print the command if xflag is set. */ if (xflag) { char sep = 0; - const char *p; - out2str(ps4val()); + const char *p, *ps4; + ps4 = expandstr(ps4val()); + out2str(ps4 != NULL ? ps4 : ps4val()); for (sp = varlist.list ; sp ; sp = sp->next) { if (sep != 0) out2c(' '); diff --git a/bin/sh/expand.c b/bin/sh/expand.c index fae5a3d889..1f8298548c 100644 --- a/bin/sh/expand.c +++ b/bin/sh/expand.c @@ -36,7 +36,7 @@ * SUCH DAMAGE. * * @(#)expand.c 8.5 (Berkeley) 5/15/95 - * $FreeBSD: src/bin/sh/expand.c,v 1.86 2011/05/27 15:56:13 jilles Exp $ + * $FreeBSD: src/bin/sh/expand.c,v 1.87 2011/06/09 23:12:23 jilles Exp $ */ #include @@ -173,6 +173,7 @@ expandarg(union node *arg, struct arglist *arglist, int flag) ifslastp = NULL; argstr(arg->narg.text, flag); if (arglist == NULL) { + STACKSTRNUL(expdest); return; /* here document expanded */ } STPUTC('\0', expdest); diff --git a/bin/sh/parser.c b/bin/sh/parser.c index 86823338f8..82a2a770ec 100644 --- a/bin/sh/parser.c +++ b/bin/sh/parser.c @@ -34,7 +34,7 @@ * SUCH DAMAGE. * * @(#)parser.c 8.7 (Berkeley) 5/16/95 - * $FreeBSD: src/bin/sh/parser.c,v 1.112 2011/05/21 22:03:06 jilles Exp $ + * $FreeBSD: src/bin/sh/parser.c,v 1.113 2011/06/09 23:12:23 jilles Exp $ */ #include @@ -2027,3 +2027,47 @@ getprompt(void *unused __unused) ps[i] = '\0'; return (ps); } + + +const char * +expandstr(char *ps) +{ + union node n; + struct jmploc jmploc; + struct jmploc *const savehandler = handler; + const int saveprompt = doprompt; + struct parsefile *const savetopfile = getcurrentfile(); + struct parser_temp *const saveparser_temp = parser_temp; + const char *result = NULL; + + if (!setjmp(jmploc.loc)) { + handler = &jmploc; + parser_temp = NULL; + setinputstring(ps, 1); + doprompt = 0; + readtoken1(pgetc(), DQSYNTAX, __DECONST(char *, "\n\n"), 0); + if (backquotelist != NULL) + error("Command substitution not allowed here"); + + n.narg.type = NARG; + n.narg.next = NULL; + n.narg.text = wordtext; + n.narg.backquote = backquotelist; + + expandarg(&n, NULL, 0); + result = stackblock(); + INTOFF; + } + handler = savehandler; + doprompt = saveprompt; + popfilesupto(savetopfile); + if (parser_temp != saveparser_temp) { + parser_temp_free_all(); + parser_temp = saveparser_temp; + } + if (result != NULL) { + INTON; + } else if (exception == EXINT) + raise(SIGINT); + return result; +} diff --git a/bin/sh/parser.h b/bin/sh/parser.h index 202e9784b8..c0d5fddc0b 100644 --- a/bin/sh/parser.h +++ b/bin/sh/parser.h @@ -34,7 +34,7 @@ * SUCH DAMAGE. * * @(#)parser.h 8.3 (Berkeley) 5/4/95 - * $FreeBSD: src/bin/sh/parser.h,v 1.17 2011/05/21 22:03:06 jilles Exp $ + * $FreeBSD: src/bin/sh/parser.h,v 1.18 2011/06/09 23:12:23 jilles Exp $ */ /* control characters in argument strings */ @@ -86,3 +86,4 @@ void fixredir(union node *, const char *, int); int goodname(const char *); int isassignment(const char *); const char *getprompt(void *); +const char *expandstr(char *); diff --git a/bin/sh/sh.1 b/bin/sh/sh.1 index d3fbe815b8..05e9ecf15b 100644 --- a/bin/sh/sh.1 +++ b/bin/sh/sh.1 @@ -34,7 +34,7 @@ .\" SUCH DAMAGE. .\" .\" from: @(#)sh.1 8.6 (Berkeley) 5/4/95 -.\" $FreeBSD: src/bin/sh/sh.1,v 1.165 2011/05/21 22:03:06 jilles Exp $ +.\" $FreeBSD: src/bin/sh/sh.1,v 1.166 2011/06/09 23:12:23 jilles Exp $ .\" .Dd August 21, 2011 .Dt SH 1 @@ -328,7 +328,7 @@ Useful for debugging. Write each command (preceded by the value of the .Va PS4 -variable) +variable subjected to parameter expansion and arithmetic expansion) to standard error before it is executed. Useful for debugging. .It "\ \ " Em tabcomplete diff --git a/tools/regression/bin/sh/execution/set-x3.0 b/tools/regression/bin/sh/execution/set-x3.0 new file mode 100644 index 0000000000..aa4fb59cf8 --- /dev/null +++ b/tools/regression/bin/sh/execution/set-x3.0 @@ -0,0 +1,9 @@ +# $FreeBSD: src/tools/regression/bin/sh/execution/set-x3.0,v 1.1 2011/06/09 23:12:23 jilles Exp $ + +key='must contain this' +PS4='$key+ ' +{ r=`set -x; { :; } 2>&1 >/dev/null`; } 2>/dev/null +case $r in +*"$key"*) true ;; +*) false ;; +esac -- 2.41.0