aboutsummaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorJilles Tjoelker <jilles@FreeBSD.org>2015-02-15 22:38:00 +0000
committerJilles Tjoelker <jilles@FreeBSD.org>2015-02-15 22:38:00 +0000
commit7034d8df0424980bdf5a90aea5b2e323d32a8644 (patch)
tree9c006dfe392d4e40da7d29eaefc60d233418311f /bin
parenta63352afc00f4d26cd762711d633c4fadc7b6d55 (diff)
downloadsrc-7034d8df0424980bdf5a90aea5b2e323d32a8644.tar.gz
src-7034d8df0424980bdf5a90aea5b2e323d32a8644.zip
sh: Various cleanups to expand.c:
* Remove some gotos. * Remove unused parameter. * Remove duplicate code.
Notes
Notes: svn path=/head/; revision=278826
Diffstat (limited to 'bin')
-rw-r--r--bin/sh/expand.c115
1 files changed, 55 insertions, 60 deletions
diff --git a/bin/sh/expand.c b/bin/sh/expand.c
index 6fa6d0d786ad..e1c1a2ec588e 100644
--- a/bin/sh/expand.c
+++ b/bin/sh/expand.c
@@ -105,11 +105,12 @@ static void expbackq(union node *, int, int);
static int subevalvar(char *, char *, int, int, int, int, int);
static char *evalvar(char *, int);
static int varisset(const char *, int);
+static void strtodest(const char *, int, int, int);
static void varvalue(const char *, int, int, int);
static void recordregion(int, int, int);
static void removerecordregions(int);
static void ifsbreakup(char *, struct arglist *);
-static void expandmeta(struct strlist *, int);
+static void expandmeta(struct strlist *);
static void expmeta(char *, char *);
static void addfname(char *);
static struct strlist *expsort(struct strlist *);
@@ -175,7 +176,7 @@ expandarg(union node *arg, struct arglist *arglist, int flag)
ifsbreakup(p, &exparg);
*exparg.lastp = NULL;
exparg.lastp = &exparg.list;
- expandmeta(exparg.list, flag);
+ expandmeta(exparg.list);
} else {
sp = (struct strlist *)stalloc(sizeof (struct strlist));
sp->text = p;
@@ -298,9 +299,9 @@ exptilde(char *p, int flag)
char c, *startp = p;
struct passwd *pw;
char *home;
- int quotes = flag & (EXP_FULL | EXP_CASE);
- while ((c = *p) != '\0') {
+ for (;;) {
+ c = *p;
switch(c) {
case CTLESC: /* This means CTL* are always considered quoted. */
case CTLVAR:
@@ -311,31 +312,27 @@ exptilde(char *p, int flag)
case CTLQUOTEMARK:
return (startp);
case ':':
- if (flag & EXP_VARTILDE)
- goto done;
- break;
+ if ((flag & EXP_VARTILDE) == 0)
+ break;
+ /* FALLTHROUGH */
+ case '\0':
case '/':
case CTLENDVAR:
- goto done;
+ *p = '\0';
+ if (*(startp+1) == '\0') {
+ home = lookupvar("HOME");
+ } else {
+ pw = getpwnam(startp+1);
+ home = pw != NULL ? pw->pw_dir : NULL;
+ }
+ *p = c;
+ if (home == NULL || *home == '\0')
+ return (startp);
+ strtodest(home, flag, VSNORMAL, 1);
+ return (p);
}
p++;
}
-done:
- *p = '\0';
- if (*(startp+1) == '\0') {
- home = lookupvar("HOME");
- } else {
- pw = getpwnam(startp+1);
- home = pw != NULL ? pw->pw_dir : NULL;
- }
- *p = c;
- if (home == NULL || *home == '\0')
- return (startp);
- if (quotes)
- STPUTS_QUOTES(home, DQSYNTAX, expdest);
- else
- STPUTS(home, expdest);
- return (p);
}
@@ -496,6 +493,17 @@ expbackq(union node *cmd, int quoted, int flag)
+static void
+recordleft(const char *str, const char *loc, char *startp)
+{
+ int amount;
+
+ amount = ((str - 1) - (loc - startp)) - expdest;
+ STADJUST(amount, expdest);
+ while (loc != str - 1)
+ *startp++ = *loc++;
+}
+
static int
subevalvar(char *p, char *str, int strloc, int subtype, int startloc,
int varflags, int quotes)
@@ -539,7 +547,8 @@ subevalvar(char *p, char *str, int strloc, int subtype, int startloc,
*loc = '\0';
if (patmatch(str, startp, quotes)) {
*loc = c;
- goto recordleft;
+ recordleft(str, loc, startp);
+ return 1;
}
*loc = c;
if (quotes && *loc == CTLESC)
@@ -553,7 +562,8 @@ subevalvar(char *p, char *str, int strloc, int subtype, int startloc,
*loc = '\0';
if (patmatch(str, startp, quotes)) {
*loc = c;
- goto recordleft;
+ recordleft(str, loc, startp);
+ return 1;
}
*loc = c;
loc--;
@@ -601,13 +611,6 @@ subevalvar(char *p, char *str, int strloc, int subtype, int startloc,
default:
abort();
}
-
-recordleft:
- amount = ((str - 1) - (loc - startp)) - expdest;
- STADJUST(amount, expdest);
- while (loc != str - 1)
- *startp++ = *loc++;
- return 1;
}
@@ -632,6 +635,7 @@ evalvar(char *p, int flag)
int varlenb;
int easy;
int quotes = flag & (EXP_FULL | EXP_CASE);
+ int record;
varflags = (unsigned char)*p++;
subtype = varflags & VSTYPE;
@@ -689,22 +693,15 @@ again: /* jump here after setting a variable with ${var=text} */
STADJUST(-varlenb, expdest);
}
} else {
- char const *syntax = (varflags & VSQUOTE) ? DQSYNTAX
- : BASESYNTAX;
-
if (subtype == VSLENGTH) {
for (;*val; val++)
if (!localeisutf8 ||
(*val & 0xC0) != 0x80)
varlen++;
}
- else {
- if (quotes)
- STPUTS_QUOTES(val, syntax, expdest);
- else
- STPUTS(val, expdest);
-
- }
+ else
+ strtodest(val, flag, subtype,
+ varflags & VSQUOTE);
}
}
@@ -718,15 +715,11 @@ again: /* jump here after setting a variable with ${var=text} */
switch (subtype) {
case VSLENGTH:
expdest = cvtnum(varlen, expdest);
- goto record;
+ record = 1;
+ break;
case VSNORMAL:
- if (!easy)
- break;
-record:
- recordregion(startloc, expdest - stackblock(),
- varflags & VSQUOTE || (ifsset() && ifsval()[0] == '\0' &&
- (*var == '@' || *var == '*')));
+ record = easy;
break;
case VSPLUS:
@@ -736,8 +729,7 @@ record:
(varflags & VSQUOTE ? EXP_LIT_QUOTED : 0));
break;
}
- if (easy)
- goto record;
+ record = easy;
break;
case VSTRIMLEFT:
@@ -759,7 +751,8 @@ record:
}
/* Remove any recorded regions beyond start of variable */
removerecordregions(startloc);
- goto record;
+ record = 1;
+ break;
case VSASSIGN:
case VSQUESTION:
@@ -776,8 +769,7 @@ record:
}
break;
}
- if (easy)
- goto record;
+ record = easy;
break;
case VSERROR:
@@ -789,6 +781,11 @@ record:
abort();
}
+ if (record)
+ recordregion(startloc, expdest - stackblock(),
+ varflags & VSQUOTE || (ifsset() && ifsval()[0] == '\0' &&
+ (*var == '@' || *var == '*')));
+
if (subtype != VSNORMAL) { /* skip to end of alternative */
int nesting = 1;
for (;;) {
@@ -1092,7 +1089,7 @@ static char expdir[PATH_MAX];
* The results are stored in the list exparg.
*/
static void
-expandmeta(struct strlist *str, int flag __unused)
+expandmeta(struct strlist *str)
{
char *p;
struct strlist **savelastp;
@@ -1474,16 +1471,14 @@ patmatch(const char *pattern, const char *string, int squoted)
endp = p;
if (*endp == '!' || *endp == '^')
endp++;
- for (;;) {
+ do {
while (*endp == CTLQUOTEMARK)
endp++;
if (*endp == 0)
goto dft; /* no matching ] */
if (*endp == CTLESC)
endp++;
- if (*++endp == ']')
- break;
- }
+ } while (*++endp != ']');
invert = 0;
if (*p == '!' || *p == '^') {
invert++;