From 96522b887c2da10d21c4b148f13efe4ff0371ada Mon Sep 17 00:00:00 2001 From: Steve Price Date: Mon, 28 Apr 1997 03:28:43 +0000 Subject: [PATCH] Sync with NetBSD's revision 1.29 of this file. Obtained from: NetBSD --- bin/sh/expand.c | 82 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 55 insertions(+), 27 deletions(-) diff --git a/bin/sh/expand.c b/bin/sh/expand.c index ace411e9c0b5..5d28bec66d82 100644 --- a/bin/sh/expand.c +++ b/bin/sh/expand.c @@ -33,7 +33,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id$ + * $Id: expand.c,v 1.16 1997/02/22 13:58:26 peter Exp $ */ #ifndef lint @@ -97,7 +97,7 @@ STATIC char *exptilde __P((char *, int)); STATIC void expbackq __P((union node *, int, int)); STATIC int subevalvar __P((char *, char *, int, int, int, int)); STATIC char *evalvar __P((char *, int)); -STATIC int varisset __P((char *)); +STATIC int varisset __P((char *, int)); STATIC void varvalue __P((char *, int, int)); STATIC void recordregion __P((int, int, int)); STATIC void ifsbreakup __P((char *, struct arglist *)); @@ -210,10 +210,10 @@ expandarg(arg, arglist, flag) STATIC void argstr(p, flag) - register char *p; + char *p; int flag; { - register char c; + char c; int quotes = flag & (EXP_FULL | EXP_CASE); /* do CTLESC */ int firsteq = 1; @@ -325,6 +325,16 @@ expari(flag) int result; int quotes = flag & (EXP_FULL | EXP_CASE); + while (ifsfirst.next != NULL) { + struct ifsregion *ifsp; + INTOFF; + ifsp = ifsfirst.next->next; + ckfree(ifsfirst.next); + ifsfirst.next = ifsp; + INTON; + } + ifslastp = NULL; + /* * This routine is slightly over-compilcated for * efficiency. First we make sure there is @@ -487,7 +497,7 @@ subevalvar(p, str, strloc, subtype, startloc, varflags) return 0; case VSTRIMLEFT: - for (loc = startp; loc < str - 1; loc++) { + for (loc = startp; loc < str; loc++) { c = *loc; *loc = '\0'; if (patmatch(str, startp)) { @@ -576,7 +586,7 @@ evalvar(p, flag) p = strchr(p, '=') + 1; again: /* jump here after setting a variable with ${var=text} */ if (special) { - set = varisset(var); + set = varisset(var, varflags & VSNUL); val = NULL; } else { val = lookupvar(var); @@ -591,12 +601,10 @@ again: /* jump here after setting a variable with ${var=text} */ if (set && subtype != VSPLUS) { /* insert the value of the variable */ if (special) { - char *exp, *oexpdest = expdest; varvalue(var, varflags & VSQUOTE, flag & EXP_FULL); if (subtype == VSLENGTH) { - for (exp = oexpdest;exp != expdest; exp++) - varlen++; - expdest = oexpdest; + varlen = expdest - stackblock() - startloc; + STADJUST(-varlen, expdest); } } else { char const *syntax = (varflags & VSQUOTE) ? DQSYNTAX @@ -662,6 +670,10 @@ record: if (subevalvar(p, NULL, expdest - stackblock(), subtype, startloc, varflags)) goto record; + else { + int amount = (expdest - pat) + 1; + STADJUST(-amount, expdest); + } break; case VSASSIGN: @@ -708,23 +720,39 @@ record: */ STATIC int -varisset(name) +varisset(name, nulok) char *name; - { - char **ap; + int nulok; +{ - if (*name == '!') { - if (backgndpid == -1) - return 0; - } else if (*name == '@' || *name == '*') { + if (*name == '!') + return backgndpid != -1; + else if (*name == '@' || *name == '*') { if (*shellparam.p == NULL) return 0; + + if (nulok) { + char **av; + + for (av = shellparam.p; *av; av++) + if (**av != '\0') + return 1; + return 0; + } } else if (is_digit(*name)) { + char *ap; int num = atoi(name); - ap = shellparam.p; - while (--num >= 0) - if (*ap++ == NULL) - return 0; + + if (num > shellparam.nparam) + return 0; + + if (num == 0) + ap = arg0; + else + ap = shellparam.p[num - 1]; + + if (nulok && (ap == NULL || *ap == '\0')) + return 0; } return 1; } @@ -829,7 +857,7 @@ recordregion(start, end, nulonly) int end; int nulonly; { - register struct ifsregion *ifsp; + struct ifsregion *ifsp; if (ifslastp == NULL) { ifsp = &ifsfirst; @@ -859,7 +887,7 @@ ifsbreakup(string, arglist) struct ifsregion *ifsp; struct strlist *sp; char *start; - register char *p; + char *p; char *q; char *ifs; int ifsspc; @@ -986,7 +1014,7 @@ expmeta(enddir, name) char *enddir; char *name; { - register char *p; + char *p; char *q; char *start; char *endname; @@ -1202,8 +1230,8 @@ pmatch(pattern, string) char *pattern; char *string; { - register char *p, *q; - register char c; + char *p, *q; + char c; p = pattern; q = string; @@ -1302,7 +1330,7 @@ void rmescapes(str) char *str; { - register char *p, *q; + char *p, *q; p = str; while (*p != CTLESC) {