int c, d;
while ((c = *p) == (d = *q)) {
- if (!c || c == '=')
+ if (c == '\0' || c == '=')
goto out;
p++;
q++;
}
static void FAST_FUNC
-setvar2(const char *name, const char *val)
+setvar0(const char *name, const char *val)
{
setvar(name, val, 0);
}
free(vp);
INT_ON;
} else {
- setvar2(s, 0);
+ setvar0(s, NULL);
vp->flags &= ~VEXPORT;
}
ok:
arith_t result;
math_state.lookupvar = lookupvar;
- math_state.setvar = setvar2;
+ math_state.setvar = setvar0;
//math_state.endofname = endofname;
INT_OFF;
switch (subtype) {
case VSASSIGN:
- setvar2(varname, startp);
+ setvar0(varname, startp);
amount = startp - expdest;
STADJUST(amount, expdest);
return startp;
loopnest++;
flags &= EV_TESTED;
for (sp = arglist.list; sp; sp = sp->next) {
- setvar2(n->nfor.var, sp->text);
+ setvar0(n->nfor.var, sp->text);
evaltree(n->nfor.body, flags);
if (evalskip) {
if (evalskip == SKIPCONT && --skipcount <= 0) {
struct localvar *lvp;
struct var **vpp;
struct var *vp;
+ char *eq = strchr(name, '=');
INT_OFF;
- lvp = ckzalloc(sizeof(struct localvar));
+ /* Cater for duplicate "local". Examples:
+ * x=0; f() { local x=1; echo $x; local x; echo $x; }; f; echo $x
+ * x=0; f() { local x=1; echo $x; local x=2; echo $x; }; f; echo $x
+ */
+ lvp = localvars;
+ while (lvp) {
+ if (varcmp(lvp->vp->var_text, name) == 0) {
+ if (eq)
+ setvareq(name, 0);
+ /* else:
+ * it's a duplicate "local VAR" declaration, do nothing
+ */
+ return;
+ }
+ lvp = lvp->next;
+ }
+
+ lvp = ckzalloc(sizeof(*lvp));
if (LONE_DASH(name)) {
char *p;
p = ckmalloc(sizeof(optlist));
lvp->text = memcpy(p, optlist, sizeof(optlist));
vp = NULL;
} else {
- char *eq;
-
vpp = hashvar(name);
vp = *findvar(vpp, name);
- eq = strchr(name, '=');
if (vp == NULL) {
+ /* variable did not exist yet */
if (eq)
setvareq(name, VSTRFIXED);
else
} else {
lvp->text = vp->var_text;
lvp->flags = vp->flags;
+ /* make sure neither "struct var" nor string gets freed
+ * during (un)setting:
+ */
vp->flags |= VSTRFIXED|VTEXTFIXED;
if (eq)
setvareq(name, 0);
else
/* "local VAR" unsets VAR: */
- setvar(name, NULL, 0);
+ setvar0(name, NULL);
}
}
lvp->vp = vp;
* '_' in 'vi' command mode during line editing...
* However I implemented that within libedit itself.
*/
- setvar2("_", lastarg);
+ setvar0("_", lastarg);
}
popstackmark(&smark);
}
* to jump out of it.
*/
INT_OFF;
- r = shell_builtin_read(setvar2,
+ r = shell_builtin_read(setvar0,
argptr,
bltinlookup("IFS"), /* can be NULL */
read_flags,
}
}
- setvar2("PPID", utoa(getppid()));
+ setvar0("PPID", utoa(getppid()));
#if ENABLE_ASH_BASH_COMPAT
p = lookupvar("SHLVL");
setvar("SHLVL", utoa((p ? atoi(p) : 0) + 1), VEXPORT);
if (!lookupvar("HOSTNAME")) {
struct utsname uts;
uname(&uts);
- setvar2("HOSTNAME", uts.nodename);
+ setvar0("HOSTNAME", uts.nodename);
}
#endif
p = lookupvar("PWD");
hp = lookupvar("HOME");
if (hp) {
hp = concat_path_file(hp, ".ash_history");
- setvar2("HISTFILE", hp);
+ setvar0("HISTFILE", hp);
free((char*)hp);
hp = lookupvar("HISTFILE");
}