* "set" and associated routines
* doset - performs the logic of the set command
* operate - performs arithmetic function of the set command
* unset - undefines variables
* xfree - frees argument storage if it is dynamic
* savestr - salts away a copy of the argument string in a safe place
* putn - returns a pointer to a string representing the argument integer
* getn - returns the number which the string argument represents or 0
* value - returns the value of a variable which is argument
* adrof - returns a pointer to the structure for the variable argument
* set - sets the argument variable to the second argument value
* doset performs the logic of the set command.
* current ops are += -= ++ -- &= |= *= /= %= and =
* Magic operator @ actually does an unset. Good for use conditionally!
for (vp
= p
; letter(*p
); p
++)
bferr(": Garbage after @");
if (!any(op
, "+-") || c
!= op
|| *p
) {
set(vp
, operate(op
, vp
, p
));
* xfree frees possibly non-dynamic storage.
* It insures thats its argument is in the heap
if (cp
>= end
&& cp
< &cp
)
return (strcpy(calloc(1, strlen(s
) + 1), s
));
* putn takes an integer and returns a pointer to
* its string representation.
* putp indexes the number as it is formed.
sprintf(number, "%d", n);
return (savestr(number
));
* getn is used by doset to get numbers from strings.
* null string is considered to be 0 (ala SNOBOL).
n
= n
* base
+ *cp
++ - '0';
bferr(": Badly formed number");
* value takes a string name of shell
* variable and returns a pointer to its value
return (value1(var
, &shvhed
));
register struct shvar
*vp
;
return (vp
== 0 ? "" : vp
->value
);
/* static */ struct shvar
*shprev
;
* adrof takes a variable name and returns
* a pointer to its structure or 0.
* A side effect is to make shprev point to the
* structure before this one for obvious reasons.
register struct shvar
*vp
;
bferr(": Bad parameter");
return (adrof1(var
, &shvhed
));
register struct shvar
*vp
;
for (vp
= shprev
->next
; vp
!= 0; vp
= vp
->next
) {
cmp
= strcmp(vp
->name
, var
);
* set sets the variable argument to
* The caller is responsible for putting value
set1(var
, value
, &shvhed
);
register struct shvar
*vp
;
vp
= calloc(1, sizeof *vp
);
register struct shvar
*vp
;
bferr2(var
, ": Undefined");