summaryrefslogtreecommitdiffstats
path: root/bin/sh/var.c
diff options
context:
space:
mode:
authorjilles <jilles@FreeBSD.org>2011-05-08 16:15:50 +0000
committerjilles <jilles@FreeBSD.org>2011-05-08 16:15:50 +0000
commitbf0e27838c7f0e2558fb05339e6f8b74848281c9 (patch)
tree29dcfd157d5ba88cc4918050d4e7403d7502b9d8 /bin/sh/var.c
parenta2816e3bed5782537b0c8e30da147b0d61e3fb25 (diff)
downloadFreeBSD-src-bf0e27838c7f0e2558fb05339e6f8b74848281c9.zip
FreeBSD-src-bf0e27838c7f0e2558fb05339e6f8b74848281c9.tar.gz
sh: Optimize variable code by storing the length of the name.
Obtained from: NetBSD
Diffstat (limited to 'bin/sh/var.c')
-rw-r--r--bin/sh/var.c248
1 files changed, 124 insertions, 124 deletions
diff --git a/bin/sh/var.c b/bin/sh/var.c
index 5c87a1f..33890db 100644
--- a/bin/sh/var.c
+++ b/bin/sh/var.c
@@ -136,8 +136,8 @@ static const int locale_categories[7] = {
LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME, LC_MESSAGES, 0
};
-static struct var **hashvar(const char *);
static int varequal(const char *, const char *);
+static struct var *find_var(const char *, struct var ***, int *);
static int localevar(const char *);
/*
@@ -174,20 +174,18 @@ initvar(void)
struct var **vpp;
for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
- if ((vp->flags & VEXPORT) == 0) {
- vpp = hashvar(ip->text);
- vp->next = *vpp;
- *vpp = vp;
- vp->text = __DECONST(char *, ip->text);
- vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED;
- vp->func = ip->func;
- }
+ if (find_var(ip->text, &vpp, &vp->name_len) != NULL)
+ continue;
+ vp->next = *vpp;
+ *vpp = vp;
+ vp->text = __DECONST(char *, ip->text);
+ vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED;
+ vp->func = ip->func;
}
/*
* PS1 depends on uid
*/
- if ((vps1.flags & VEXPORT) == 0) {
- vpp = hashvar("PS1=");
+ if (find_var("PS1", &vpp, &vps1.name_len) == NULL) {
vps1.next = *vpp;
*vpp = &vps1;
vps1.text = __DECONST(char *, geteuid() ? "PS1=$ " : "PS1=# ");
@@ -323,50 +321,46 @@ void
setvareq(char *s, int flags)
{
struct var *vp, **vpp;
- int len;
+ int nlen;
if (aflag)
flags |= VEXPORT;
- vpp = hashvar(s);
- for (vp = *vpp ; vp ; vp = vp->next) {
- if (varequal(s, vp->text)) {
- if (vp->flags & VREADONLY) {
- len = strchr(s, '=') - s;
- error("%.*s: is read only", len, s);
- }
- if (flags & VNOSET)
- return;
- INTOFF;
-
- if (vp->func && (flags & VNOFUNC) == 0)
- (*vp->func)(strchr(s, '=') + 1);
-
- if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
- ckfree(vp->text);
-
- vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
- vp->flags |= flags;
- vp->text = s;
-
- /*
- * We could roll this to a function, to handle it as
- * a regular variable function callback, but why bother?
- *
- * Note: this assumes iflag is not set to 1 initially.
- * As part of init(), this is called before arguments
- * are looked at.
- */
- if ((vp == &vmpath || (vp == &vmail && ! mpathset())) &&
- iflag == 1)
- chkmail(1);
- if ((vp->flags & VEXPORT) && localevar(s)) {
- change_env(s, 1);
- (void) setlocale(LC_ALL, "");
- updatecharset();
- }
- INTON;
+ vp = find_var(s, &vpp, &nlen);
+ if (vp != NULL) {
+ if (vp->flags & VREADONLY)
+ error("%.*s: is read only", vp->name_len, s);
+ if (flags & VNOSET)
return;
+ INTOFF;
+
+ if (vp->func && (flags & VNOFUNC) == 0)
+ (*vp->func)(s + vp->name_len + 1);
+
+ if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
+ ckfree(vp->text);
+
+ vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
+ vp->flags |= flags;
+ vp->text = s;
+
+ /*
+ * We could roll this to a function, to handle it as
+ * a regular variable function callback, but why bother?
+ *
+ * Note: this assumes iflag is not set to 1 initially.
+ * As part of init(), this is called before arguments
+ * are looked at.
+ */
+ if ((vp == &vmpath || (vp == &vmail && ! mpathset())) &&
+ iflag == 1)
+ chkmail(1);
+ if ((vp->flags & VEXPORT) && localevar(s)) {
+ change_env(s, 1);
+ (void) setlocale(LC_ALL, "");
+ updatecharset();
}
+ INTON;
+ return;
}
/* not found */
if (flags & VNOSET)
@@ -374,6 +368,7 @@ setvareq(char *s, int flags)
vp = ckmalloc(sizeof (*vp));
vp->flags = flags;
vp->text = s;
+ vp->name_len = nlen;
vp->next = *vpp;
vp->func = NULL;
INTOFF;
@@ -415,14 +410,10 @@ lookupvar(const char *name)
{
struct var *v;
- for (v = *hashvar(name) ; v ; v = v->next) {
- if (varequal(v->text, name)) {
- if (v->flags & VUNSET)
- return NULL;
- return strchr(v->text, '=') + 1;
- }
- }
- return NULL;
+ v = find_var(name, NULL, NULL);
+ if (v == NULL || v->flags & VUNSET)
+ return NULL;
+ return v->text + v->name_len + 1;
}
@@ -447,15 +438,12 @@ bltinlookup(const char *name, int doall)
}
if (result != NULL)
return result;
- for (v = *hashvar(name) ; v ; v = v->next) {
- if (varequal(v->text, name)) {
- if ((v->flags & VUNSET)
- || (!doall && (v->flags & VEXPORT) == 0))
- return NULL;
- return strchr(v->text, '=') + 1;
- }
- }
- return NULL;
+
+ v = find_var(name, NULL, NULL);
+ if (v == NULL || v->flags & VUNSET ||
+ (!doall && (v->flags & VEXPORT) == 0))
+ return NULL;
+ return v->text + v->name_len + 1;
}
@@ -665,22 +653,18 @@ exportcmd(int argc, char **argv)
if ((p = strchr(name, '=')) != NULL) {
p++;
} else {
- vpp = hashvar(name);
- for (vp = *vpp ; vp ; vp = vp->next) {
- if (varequal(vp->text, name)) {
-
- vp->flags |= flag;
- if ((vp->flags & VEXPORT) && localevar(vp->text)) {
- change_env(vp->text, 1);
- (void) setlocale(LC_ALL, "");
- updatecharset();
- }
- goto found;
+ vp = find_var(name, NULL, NULL);
+ if (vp != NULL) {
+ vp->flags |= flag;
+ if ((vp->flags & VEXPORT) && localevar(vp->text)) {
+ change_env(vp->text, 1);
+ (void) setlocale(LC_ALL, "");
+ updatecharset();
}
+ continue;
}
}
setvar(name, p, flag);
-found:;
}
} else {
for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
@@ -747,8 +731,7 @@ mklocal(char *name)
memcpy(lvp->text, optlist, sizeof optlist);
vp = NULL;
} else {
- vpp = hashvar(name);
- for (vp = *vpp ; vp && ! varequal(vp->text, name) ; vp = vp->next);
+ vp = find_var(name, &vpp, NULL);
if (vp == NULL) {
if (strchr(name, '='))
setvareq(savestr(name), VSTRFIXED);
@@ -761,7 +744,7 @@ mklocal(char *name)
lvp->text = vp->text;
lvp->flags = vp->flags;
vp->flags |= VSTRFIXED|VTEXTFIXED;
- if (strchr(name, '='))
+ if (name[vp->name_len] == '=')
setvareq(savestr(name), 0);
}
}
@@ -857,55 +840,34 @@ unsetvar(const char *s)
struct var **vpp;
struct var *vp;
- vpp = hashvar(s);
- for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
- if (varequal(vp->text, s)) {
- if (vp->flags & VREADONLY)
- return (1);
- INTOFF;
- if (*(strchr(vp->text, '=') + 1) != '\0')
- setvar(s, nullstr, 0);
- if ((vp->flags & VEXPORT) && localevar(vp->text)) {
- change_env(s, 0);
- setlocale(LC_ALL, "");
- updatecharset();
- }
- vp->flags &= ~VEXPORT;
- vp->flags |= VUNSET;
- if ((vp->flags & VSTRFIXED) == 0) {
- if ((vp->flags & VTEXTFIXED) == 0)
- ckfree(vp->text);
- *vpp = vp->next;
- ckfree(vp);
- }
- INTON;
- return (0);
- }
+ vp = find_var(s, &vpp, NULL);
+ if (vp == NULL)
+ return (0);
+ if (vp->flags & VREADONLY)
+ return (1);
+ INTOFF;
+ if (vp->text[vp->name_len + 1] != '\0')
+ setvar(s, nullstr, 0);
+ if ((vp->flags & VEXPORT) && localevar(vp->text)) {
+ change_env(s, 0);
+ setlocale(LC_ALL, "");
+ updatecharset();
}
-
+ vp->flags &= ~VEXPORT;
+ vp->flags |= VUNSET;
+ if ((vp->flags & VSTRFIXED) == 0) {
+ if ((vp->flags & VTEXTFIXED) == 0)
+ ckfree(vp->text);
+ *vpp = vp->next;
+ ckfree(vp);
+ }
+ INTON;
return (0);
}
/*
- * Find the appropriate entry in the hash table from the name.
- */
-
-static struct var **
-hashvar(const char *p)
-{
- unsigned int hashval;
-
- hashval = ((unsigned char) *p) << 4;
- while (*p && *p != '=')
- hashval += (unsigned char) *p++;
- return &vartab[hashval % VTABSIZE];
-}
-
-
-
-/*
* Returns true if the two strings specify the same varable. The first
* variable name is terminated by '='; the second may be terminated by
* either '=' or '\0'.
@@ -922,3 +884,41 @@ varequal(const char *p, const char *q)
return 1;
return 0;
}
+
+/*
+ * Search for a variable.
+ * 'name' may be terminated by '=' or a NUL.
+ * vppp is set to the pointer to vp, or the list head if vp isn't found
+ * lenp is set to the number of charactets in 'name'
+ */
+
+static struct var *
+find_var(const char *name, struct var ***vppp, int *lenp)
+{
+ unsigned int hashval;
+ int len;
+ struct var *vp, **vpp;
+ const char *p = name;
+
+ hashval = 0;
+ while (*p && *p != '=')
+ hashval = 2 * hashval + (unsigned char)*p++;
+ len = p - name;
+
+ if (lenp)
+ *lenp = len;
+ vpp = &vartab[hashval % VTABSIZE];
+ if (vppp)
+ *vppp = vpp;
+
+ for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
+ if (vp->name_len != len)
+ continue;
+ if (memcmp(vp->text, name, len) != 0)
+ continue;
+ if (vppp)
+ *vppp = vpp;
+ return vp;
+ }
+ return NULL;
+}
OpenPOWER on IntegriCloud