2006-05-27 00:17:21 +02:00
|
|
|
/* $OpenBSD: var.c,v 1.30 2006/05/21 18:40:39 otto Exp $ */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
#include "sh.h"
|
|
|
|
|
2007-07-01 23:10:29 +02:00
|
|
|
__RCSID("$MirOS: src/bin/mksh/var.c,v 1.40 2007/07/01 21:10:29 tg Exp $");
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Variables
|
|
|
|
*
|
|
|
|
* WARNING: unreadable code, needs a rewrite
|
|
|
|
*
|
|
|
|
* if (flag&INTEGER), val.i contains integer value, and type contains base.
|
|
|
|
* otherwise, (val.s + type) contains string value.
|
|
|
|
* if (flag&EXPORT), val.s contains "name=value" for E-Z exporting.
|
|
|
|
*/
|
2006-01-29 21:10:16 +01:00
|
|
|
static struct tbl vtemp;
|
|
|
|
static struct table specials;
|
2005-05-23 05:06:10 +02:00
|
|
|
static char *formatstr(struct tbl *, const char *);
|
|
|
|
static void export(struct tbl *, const char *);
|
|
|
|
static int special(const char *);
|
|
|
|
static void unspecial(const char *);
|
|
|
|
static void getspec(struct tbl *);
|
|
|
|
static void setspec(struct tbl *);
|
|
|
|
static void unsetspec(struct tbl *);
|
|
|
|
static struct tbl *arraysearch(struct tbl *, int);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* create a new block for function calls and simple commands
|
|
|
|
* assume caller has allocated and set up e->loc
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
newblock(void)
|
|
|
|
{
|
|
|
|
struct block *l;
|
2007-03-04 01:13:17 +01:00
|
|
|
static const char *empty[] = { null };
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
l = (struct block *) alloc(sizeof(struct block), ATEMP);
|
|
|
|
l->flags = 0;
|
|
|
|
ainit(&l->area); /* todo: could use e->area (l->area => l->areap) */
|
|
|
|
if (!e->loc) {
|
|
|
|
l->argc = 0;
|
2006-11-12 15:58:16 +01:00
|
|
|
l->argv = empty;
|
2005-05-23 05:06:10 +02:00
|
|
|
} else {
|
|
|
|
l->argc = e->loc->argc;
|
|
|
|
l->argv = e->loc->argv;
|
|
|
|
}
|
|
|
|
l->exit = l->error = NULL;
|
2006-01-29 21:04:54 +01:00
|
|
|
ktinit(&l->vars, &l->area, 0);
|
|
|
|
ktinit(&l->funs, &l->area, 0);
|
2005-05-23 05:06:10 +02:00
|
|
|
l->next = e->loc;
|
|
|
|
e->loc = l;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pop a block handling special variables
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
popblock(void)
|
|
|
|
{
|
|
|
|
struct block *l = e->loc;
|
|
|
|
struct tbl *vp, **vpp = l->vars.tbls, *vq;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
e->loc = l->next; /* pop block */
|
|
|
|
for (i = l->vars.size; --i >= 0; )
|
|
|
|
if ((vp = *vpp++) != NULL && (vp->flag&SPECIAL)) {
|
|
|
|
if ((vq = global(vp->name))->flag & ISSET)
|
|
|
|
setspec(vq);
|
|
|
|
else
|
|
|
|
unsetspec(vq);
|
|
|
|
}
|
|
|
|
if (l->flags & BF_DOGETOPTS)
|
|
|
|
user_opt = l->getopts_state;
|
|
|
|
afreeall(&l->area);
|
|
|
|
afree(l, ATEMP);
|
|
|
|
}
|
|
|
|
|
2006-11-10 07:45:28 +01:00
|
|
|
/* called by main() to initialise variable data structures */
|
2005-05-23 05:06:10 +02:00
|
|
|
void
|
|
|
|
initvar(void)
|
|
|
|
{
|
|
|
|
static const struct {
|
|
|
|
const char *name;
|
|
|
|
int v;
|
|
|
|
} names[] = {
|
|
|
|
{ "COLUMNS", V_COLUMNS },
|
|
|
|
{ "IFS", V_IFS },
|
|
|
|
{ "OPTIND", V_OPTIND },
|
|
|
|
{ "PATH", V_PATH },
|
|
|
|
{ "TMPDIR", V_TMPDIR },
|
2007-03-03 22:36:08 +01:00
|
|
|
#if HAVE_PERSISTENT_HISTORY
|
2005-05-23 05:06:10 +02:00
|
|
|
{ "HISTFILE", V_HISTFILE },
|
2006-11-10 06:23:14 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
{ "HISTSIZE", V_HISTSIZE },
|
|
|
|
{ "RANDOM", V_RANDOM },
|
|
|
|
{ "SECONDS", V_SECONDS },
|
|
|
|
{ "TMOUT", V_TMOUT },
|
|
|
|
{ "LINENO", V_LINENO },
|
|
|
|
{ NULL, 0 }
|
|
|
|
};
|
|
|
|
int i;
|
|
|
|
struct tbl *tp;
|
|
|
|
|
2006-01-29 21:04:54 +01:00
|
|
|
ktinit(&specials, APERM, 32); /* must be 2^n (currently 17 specials) */
|
2005-05-23 05:06:10 +02:00
|
|
|
for (i = 0; names[i].name; i++) {
|
2006-01-29 21:04:54 +01:00
|
|
|
tp = ktenter(&specials, names[i].name, hash(names[i].name));
|
2005-05-23 05:06:10 +02:00
|
|
|
tp->flag = DEFINED|ISSET;
|
|
|
|
tp->type = names[i].v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Used to calculate an array index for global()/local(). Sets *arrayp to
|
|
|
|
* non-zero if this is an array, sets *valp to the array index, returns
|
|
|
|
* the basename of the array.
|
|
|
|
*/
|
|
|
|
static const char *
|
|
|
|
array_index_calc(const char *n, bool *arrayp, int *valp)
|
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
*arrayp = false;
|
|
|
|
p = skip_varname(n, false);
|
|
|
|
if (p != n && *p == '[' && (len = array_ref_len(p))) {
|
|
|
|
char *sub, *tmp;
|
|
|
|
long rval;
|
|
|
|
|
|
|
|
/* Calculate the value of the subscript */
|
|
|
|
*arrayp = true;
|
|
|
|
tmp = str_nsave(p+1, len-2, ATEMP);
|
|
|
|
sub = substitute(tmp, 0);
|
|
|
|
afree(tmp, ATEMP);
|
|
|
|
n = str_nsave(n, p - n, ATEMP);
|
|
|
|
evaluate(sub, &rval, KSH_UNWIND_ERROR, true);
|
|
|
|
if (rval < 0 || rval > 2147483647)
|
2005-10-21 13:55:23 +02:00
|
|
|
errorf("%s: subscript %ld out of range", n, rval);
|
2005-05-23 05:06:10 +02:00
|
|
|
*valp = rval;
|
|
|
|
afree(sub, ATEMP);
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Search for variable, if not found create globally.
|
|
|
|
*/
|
|
|
|
struct tbl *
|
|
|
|
global(const char *n)
|
|
|
|
{
|
|
|
|
struct block *l = e->loc;
|
|
|
|
struct tbl *vp;
|
|
|
|
int c;
|
|
|
|
unsigned h;
|
|
|
|
bool array;
|
|
|
|
int val;
|
|
|
|
|
|
|
|
/* Check to see if this is an array */
|
|
|
|
n = array_index_calc(n, &array, &val);
|
|
|
|
h = hash(n);
|
|
|
|
c = n[0];
|
2006-11-10 08:52:04 +01:00
|
|
|
if (!ksh_isalphx(c)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
if (array)
|
|
|
|
errorf("bad substitution");
|
|
|
|
vp = &vtemp;
|
|
|
|
vp->flag = DEFINED;
|
|
|
|
vp->type = 0;
|
|
|
|
vp->areap = ATEMP;
|
|
|
|
*vp->name = c;
|
2006-11-10 08:52:04 +01:00
|
|
|
if (ksh_isdigit(c)) {
|
|
|
|
for (c = 0; ksh_isdigit(*n); n++)
|
2005-05-23 05:06:10 +02:00
|
|
|
c = c*10 + *n-'0';
|
|
|
|
if (c <= l->argc)
|
|
|
|
/* setstr can't fail here */
|
|
|
|
setstr(vp, l->argv[c], KSH_RETURN_ERROR);
|
|
|
|
vp->flag |= RDONLY;
|
|
|
|
return vp;
|
|
|
|
}
|
|
|
|
vp->flag |= RDONLY;
|
|
|
|
if (n[1] != '\0')
|
|
|
|
return vp;
|
|
|
|
vp->flag |= ISSET|INTEGER;
|
|
|
|
switch (c) {
|
|
|
|
case '$':
|
|
|
|
vp->val.i = kshpid;
|
|
|
|
break;
|
|
|
|
case '!':
|
|
|
|
/* If no job, expand to nothing */
|
|
|
|
if ((vp->val.i = j_async()) == 0)
|
|
|
|
vp->flag &= ~(ISSET|INTEGER);
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
vp->val.i = exstat;
|
|
|
|
break;
|
|
|
|
case '#':
|
|
|
|
vp->val.i = l->argc;
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
vp->flag &= ~INTEGER;
|
|
|
|
vp->val.s = getoptions();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
vp->flag &= ~(ISSET|INTEGER);
|
|
|
|
}
|
|
|
|
return vp;
|
|
|
|
}
|
|
|
|
for (l = e->loc; ; l = l->next) {
|
2006-01-29 21:04:54 +01:00
|
|
|
vp = ktsearch(&l->vars, n, h);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (vp != NULL) {
|
|
|
|
if (array)
|
|
|
|
return arraysearch(vp, val);
|
|
|
|
else
|
|
|
|
return vp;
|
|
|
|
}
|
|
|
|
if (l->next == NULL)
|
|
|
|
break;
|
|
|
|
}
|
2006-01-29 21:04:54 +01:00
|
|
|
vp = ktenter(&l->vars, n, h);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (array)
|
|
|
|
vp = arraysearch(vp, val);
|
|
|
|
vp->flag |= DEFINED;
|
|
|
|
if (special(n))
|
|
|
|
vp->flag |= SPECIAL;
|
|
|
|
return vp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Search for local variable, if not found create locally.
|
|
|
|
*/
|
|
|
|
struct tbl *
|
|
|
|
local(const char *n, bool copy)
|
|
|
|
{
|
|
|
|
struct block *l = e->loc;
|
|
|
|
struct tbl *vp;
|
|
|
|
unsigned h;
|
|
|
|
bool array;
|
|
|
|
int val;
|
|
|
|
|
|
|
|
/* Check to see if this is an array */
|
|
|
|
n = array_index_calc(n, &array, &val);
|
|
|
|
h = hash(n);
|
2006-11-10 08:52:04 +01:00
|
|
|
if (!ksh_isalphx(*n)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
vp = &vtemp;
|
|
|
|
vp->flag = DEFINED|RDONLY;
|
|
|
|
vp->type = 0;
|
|
|
|
vp->areap = ATEMP;
|
|
|
|
return vp;
|
|
|
|
}
|
2006-01-29 21:04:54 +01:00
|
|
|
vp = ktenter(&l->vars, n, h);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (copy && !(vp->flag & DEFINED)) {
|
|
|
|
struct block *ll = l;
|
|
|
|
struct tbl *vq = NULL;
|
|
|
|
|
2006-01-29 21:04:54 +01:00
|
|
|
while ((ll = ll->next) && !(vq = ktsearch(&ll->vars, n, h)))
|
2005-05-23 05:06:10 +02:00
|
|
|
;
|
|
|
|
if (vq) {
|
|
|
|
vp->flag |= vq->flag &
|
|
|
|
(EXPORT | INTEGER | RDONLY | LJUST | RJUST |
|
|
|
|
ZEROFIL | LCASEV | UCASEV_AL | INT_U | INT_L);
|
|
|
|
if (vq->flag & INTEGER)
|
|
|
|
vp->type = vq->type;
|
|
|
|
vp->u2.field = vq->u2.field;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (array)
|
|
|
|
vp = arraysearch(vp, val);
|
|
|
|
vp->flag |= DEFINED;
|
|
|
|
if (special(n))
|
|
|
|
vp->flag |= SPECIAL;
|
|
|
|
return vp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get variable string value */
|
|
|
|
char *
|
|
|
|
str_val(struct tbl *vp)
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
if ((vp->flag&SPECIAL))
|
|
|
|
getspec(vp);
|
|
|
|
if (!(vp->flag&ISSET))
|
|
|
|
s = null; /* special to dollar() */
|
|
|
|
else if (!(vp->flag&INTEGER)) /* string source */
|
|
|
|
s = vp->val.s + vp->type;
|
|
|
|
else { /* integer source */
|
|
|
|
/* worst case number length is when base=2, so use BITS(long) */
|
|
|
|
/* minus base # number null */
|
2006-05-27 00:17:21 +02:00
|
|
|
char strbuf[1 + 2 + 1 + 8 * sizeof(long) + 1];
|
2005-05-23 05:06:10 +02:00
|
|
|
const char *digits = (vp->flag & UCASEV_AL) ?
|
|
|
|
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" :
|
|
|
|
"0123456789abcdefghijklmnopqrstuvwxyz";
|
|
|
|
unsigned long n;
|
|
|
|
int base;
|
|
|
|
|
|
|
|
s = strbuf + sizeof(strbuf);
|
|
|
|
if (vp->flag & INT_U)
|
|
|
|
n = (unsigned long) vp->val.i;
|
|
|
|
else
|
|
|
|
n = (vp->val.i < 0) ? -vp->val.i : vp->val.i;
|
|
|
|
base = (vp->type == 0) ? 10 : vp->type;
|
|
|
|
|
|
|
|
*--s = '\0';
|
|
|
|
do {
|
|
|
|
*--s = digits[n % base];
|
|
|
|
n /= base;
|
|
|
|
} while (n != 0);
|
|
|
|
if (base != 10) {
|
|
|
|
*--s = '#';
|
|
|
|
*--s = digits[base % 10];
|
|
|
|
if (base >= 10)
|
|
|
|
*--s = digits[base / 10];
|
|
|
|
}
|
|
|
|
if (!(vp->flag & INT_U) && vp->val.i < 0)
|
|
|
|
*--s = '-';
|
|
|
|
if (vp->flag & (RJUST|LJUST)) /* case already dealt with */
|
|
|
|
s = formatstr(vp, s);
|
2006-05-27 00:17:21 +02:00
|
|
|
else
|
|
|
|
s = str_save(s, ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get variable integer value, with error checking */
|
|
|
|
long
|
|
|
|
intval(struct tbl *vp)
|
|
|
|
{
|
|
|
|
long num;
|
|
|
|
int base;
|
|
|
|
|
|
|
|
base = getint(vp, &num, false);
|
|
|
|
if (base == -1)
|
|
|
|
/* XXX check calls - is error here ok by POSIX? */
|
|
|
|
errorf("%s: bad number", str_val(vp));
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set variable to string value */
|
|
|
|
int
|
|
|
|
setstr(struct tbl *vq, const char *s, int error_ok)
|
|
|
|
{
|
|
|
|
int no_ro_check = error_ok & 0x4;
|
|
|
|
error_ok &= ~0x4;
|
|
|
|
if ((vq->flag & RDONLY) && !no_ro_check) {
|
|
|
|
warningf(true, "%s: is read only", vq->name);
|
|
|
|
if (!error_ok)
|
|
|
|
errorf(null);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!(vq->flag&INTEGER)) { /* string dest */
|
|
|
|
if ((vq->flag&ALLOC)) {
|
|
|
|
/* debugging */
|
|
|
|
if (s >= vq->val.s &&
|
|
|
|
s <= vq->val.s + strlen(vq->val.s))
|
2007-05-13 19:51:24 +02:00
|
|
|
internal_errorf(
|
2005-05-23 05:06:10 +02:00
|
|
|
"setstr: %s=%s: assigning to self",
|
|
|
|
vq->name, s);
|
|
|
|
afree((void*)vq->val.s, vq->areap);
|
|
|
|
}
|
|
|
|
vq->flag &= ~(ISSET|ALLOC);
|
|
|
|
vq->type = 0;
|
|
|
|
if (s && (vq->flag & (UCASEV_AL|LCASEV|LJUST|RJUST)))
|
|
|
|
s = formatstr(vq, s);
|
|
|
|
if ((vq->flag&EXPORT))
|
|
|
|
export(vq, s);
|
|
|
|
else {
|
|
|
|
vq->val.s = str_save(s, vq->areap);
|
|
|
|
vq->flag |= ALLOC;
|
|
|
|
}
|
|
|
|
} else { /* integer dest */
|
|
|
|
if (!v_evaluate(vq, s, error_ok, true))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
vq->flag |= ISSET;
|
|
|
|
if ((vq->flag&SPECIAL))
|
|
|
|
setspec(vq);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set variable to integer */
|
|
|
|
void
|
|
|
|
setint(struct tbl *vq, long int n)
|
|
|
|
{
|
|
|
|
if (!(vq->flag&INTEGER)) {
|
|
|
|
struct tbl *vp = &vtemp;
|
|
|
|
vp->flag = (ISSET|INTEGER);
|
|
|
|
vp->type = 0;
|
|
|
|
vp->areap = ATEMP;
|
|
|
|
vp->val.i = n;
|
|
|
|
/* setstr can't fail here */
|
|
|
|
setstr(vq, str_val(vp), KSH_RETURN_ERROR);
|
|
|
|
} else
|
|
|
|
vq->val.i = n;
|
|
|
|
vq->flag |= ISSET;
|
|
|
|
if ((vq->flag&SPECIAL))
|
|
|
|
setspec(vq);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
getint(struct tbl *vp, long int *nump, bool arith)
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
int c;
|
|
|
|
int base, neg;
|
|
|
|
int have_base = 0;
|
|
|
|
long num;
|
|
|
|
|
|
|
|
if (vp->flag&SPECIAL)
|
|
|
|
getspec(vp);
|
|
|
|
/* XXX is it possible for ISSET to be set and val.s to be 0? */
|
|
|
|
if (!(vp->flag&ISSET) || (!(vp->flag&INTEGER) && vp->val.s == NULL))
|
|
|
|
return -1;
|
|
|
|
if (vp->flag&INTEGER) {
|
|
|
|
*nump = vp->val.i;
|
|
|
|
return vp->type;
|
|
|
|
}
|
|
|
|
s = vp->val.s + vp->type;
|
|
|
|
if (s == NULL) /* redundant given initial test */
|
|
|
|
s = null;
|
|
|
|
base = 10;
|
|
|
|
num = 0;
|
|
|
|
neg = 0;
|
|
|
|
if (arith && *s == '0' && *(s+1)) {
|
|
|
|
s++;
|
|
|
|
if (*s == 'x' || *s == 'X') {
|
|
|
|
s++;
|
|
|
|
base = 16;
|
2006-05-10 20:54:13 +02:00
|
|
|
} else if (vp->flag & ZEROFIL) {
|
|
|
|
while (*s == '0')
|
|
|
|
s++;
|
2005-05-23 05:06:10 +02:00
|
|
|
} else
|
|
|
|
base = 8;
|
|
|
|
have_base++;
|
|
|
|
}
|
|
|
|
for (c = *s++; c ; c = *s++) {
|
|
|
|
if (c == '-') {
|
|
|
|
neg++;
|
2006-11-10 08:52:04 +01:00
|
|
|
continue;
|
2005-05-23 05:06:10 +02:00
|
|
|
} else if (c == '#') {
|
|
|
|
base = (int) num;
|
|
|
|
if (have_base || base < 2 || base > 36)
|
|
|
|
return -1;
|
|
|
|
num = 0;
|
|
|
|
have_base = 1;
|
2006-11-10 08:52:04 +01:00
|
|
|
continue;
|
|
|
|
} else if (ksh_isdigit(c))
|
|
|
|
c -= '0';
|
|
|
|
else if (ksh_islower(c))
|
|
|
|
c -= 'a' - 10;
|
|
|
|
else if (ksh_isupper(c))
|
|
|
|
c -= 'A' - 10;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
if (c < 0 || c >= base)
|
2005-05-23 05:06:10 +02:00
|
|
|
return -1;
|
2006-11-10 08:52:04 +01:00
|
|
|
num = num * base + c;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
if (neg)
|
|
|
|
num = -num;
|
|
|
|
*nump = num;
|
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* convert variable vq to integer variable, setting its value from vp
|
|
|
|
* (vq and vp may be the same)
|
|
|
|
*/
|
|
|
|
struct tbl *
|
|
|
|
setint_v(struct tbl *vq, struct tbl *vp, bool arith)
|
|
|
|
{
|
|
|
|
int base;
|
|
|
|
long num;
|
|
|
|
|
|
|
|
if ((base = getint(vp, &num, arith)) == -1)
|
|
|
|
return NULL;
|
|
|
|
if (!(vq->flag & INTEGER) && (vq->flag & ALLOC)) {
|
|
|
|
vq->flag &= ~ALLOC;
|
|
|
|
afree(vq->val.s, vq->areap);
|
|
|
|
}
|
|
|
|
vq->val.i = num;
|
|
|
|
if (vq->type == 0) /* default base */
|
|
|
|
vq->type = base;
|
|
|
|
vq->flag |= ISSET|INTEGER;
|
|
|
|
if (vq->flag&SPECIAL)
|
|
|
|
setspec(vq);
|
|
|
|
return vq;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
formatstr(struct tbl *vp, const char *s)
|
|
|
|
{
|
|
|
|
int olen, nlen;
|
|
|
|
char *p, *q;
|
|
|
|
|
|
|
|
olen = strlen(s);
|
|
|
|
|
|
|
|
if (vp->flag & (RJUST|LJUST)) {
|
|
|
|
if (!vp->u2.field) /* default field width */
|
|
|
|
vp->u2.field = olen;
|
|
|
|
nlen = vp->u2.field;
|
|
|
|
} else
|
|
|
|
nlen = olen;
|
|
|
|
|
|
|
|
p = (char *) alloc(nlen + 1, ATEMP);
|
|
|
|
if (vp->flag & (RJUST|LJUST)) {
|
|
|
|
int slen;
|
|
|
|
|
|
|
|
if (vp->flag & RJUST) {
|
|
|
|
const char *qq = s + olen;
|
|
|
|
/* strip trailing spaces (at&t uses qq[-1] == ' ') */
|
2006-11-10 08:18:58 +01:00
|
|
|
while (qq > s && ksh_isspace(qq[-1]))
|
2005-05-23 05:06:10 +02:00
|
|
|
--qq;
|
|
|
|
slen = qq - s;
|
|
|
|
if (slen > vp->u2.field) {
|
|
|
|
s += slen - vp->u2.field;
|
|
|
|
slen = vp->u2.field;
|
|
|
|
}
|
2006-11-10 02:13:52 +01:00
|
|
|
if (vp->u2.field - slen)
|
|
|
|
memset(p, (vp->flag & ZEROFIL) ? '0' : ' ',
|
|
|
|
vp->u2.field - slen);
|
|
|
|
shf_snprintf(p + vp->u2.field - slen,
|
|
|
|
nlen + 1 - (vp->u2.field - slen),
|
|
|
|
"%.*s", slen, s);
|
2005-05-23 05:06:10 +02:00
|
|
|
} else {
|
|
|
|
/* strip leading spaces/zeros */
|
2006-11-10 08:18:58 +01:00
|
|
|
while (ksh_isspace(*s))
|
2005-05-23 05:06:10 +02:00
|
|
|
s++;
|
|
|
|
if (vp->flag & ZEROFIL)
|
|
|
|
while (*s == '0')
|
|
|
|
s++;
|
|
|
|
shf_snprintf(p, nlen + 1, "%-*.*s",
|
|
|
|
vp->u2.field, vp->u2.field, s);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
memcpy(p, s, olen + 1);
|
|
|
|
|
|
|
|
if (vp->flag & UCASEV_AL) {
|
|
|
|
for (q = p; *q; q++)
|
2006-11-10 08:18:58 +01:00
|
|
|
*q = ksh_toupper(*q);
|
2005-05-23 05:06:10 +02:00
|
|
|
} else if (vp->flag & LCASEV) {
|
|
|
|
for (q = p; *q; q++)
|
2006-11-10 08:18:58 +01:00
|
|
|
*q = ksh_tolower(*q);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* make vp->val.s be "name=value" for quick exporting.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
export(struct tbl *vp, const char *val)
|
|
|
|
{
|
|
|
|
char *xp;
|
|
|
|
char *op = (vp->flag&ALLOC) ? vp->val.s : NULL;
|
|
|
|
int namelen = strlen(vp->name);
|
|
|
|
int vallen = strlen(val) + 1;
|
|
|
|
|
|
|
|
vp->flag |= ALLOC;
|
|
|
|
xp = (char*)alloc(namelen + 1 + vallen, vp->areap);
|
|
|
|
memcpy(vp->val.s = xp, vp->name, namelen);
|
|
|
|
xp += namelen;
|
|
|
|
*xp++ = '=';
|
|
|
|
vp->type = xp - vp->val.s; /* offset to value */
|
|
|
|
memcpy(xp, val, vallen);
|
|
|
|
if (op != NULL)
|
|
|
|
afree((void*)op, vp->areap);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* lookup variable (according to (set&LOCAL)),
|
|
|
|
* set its attributes (INTEGER, RDONLY, EXPORT, TRACE, LJUST, RJUST, ZEROFIL,
|
|
|
|
* LCASEV, UCASEV_AL), and optionally set its value if an assignment.
|
|
|
|
*/
|
|
|
|
struct tbl *
|
|
|
|
typeset(const char *var, Tflag set, Tflag clr, int field, int base)
|
|
|
|
{
|
|
|
|
struct tbl *vp;
|
|
|
|
struct tbl *vpbase, *t;
|
|
|
|
char *tvar;
|
|
|
|
const char *val;
|
|
|
|
|
|
|
|
/* check for valid variable name, search for value */
|
|
|
|
val = skip_varname(var, false);
|
|
|
|
if (val == var)
|
|
|
|
return NULL;
|
|
|
|
if (*val == '[') {
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = array_ref_len(val);
|
|
|
|
if (len == 0)
|
|
|
|
return NULL;
|
|
|
|
/* IMPORT is only used when the shell starts up and is
|
|
|
|
* setting up its environment. Allow only simple array
|
|
|
|
* references at this time since parameter/command substitution
|
2006-01-30 13:37:24 +01:00
|
|
|
* is preformed on the [expression] which would be a major
|
2005-05-23 05:06:10 +02:00
|
|
|
* security hole.
|
|
|
|
*/
|
|
|
|
if (set & IMPORT) {
|
|
|
|
int i;
|
|
|
|
for (i = 1; i < len - 1; i++)
|
2006-11-10 08:52:04 +01:00
|
|
|
if (!ksh_isdigit(val[i]))
|
2005-05-23 05:06:10 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
val += len;
|
|
|
|
}
|
|
|
|
if (*val == '=')
|
|
|
|
tvar = str_nsave(var, val++ - var, ATEMP);
|
|
|
|
else {
|
|
|
|
/* Importing from original environment: must have an = */
|
|
|
|
if (set & IMPORT)
|
|
|
|
return NULL;
|
2006-11-12 15:58:16 +01:00
|
|
|
tvar = str_save(var, ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
val = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Prevent typeset from creating a local PATH/ENV/SHELL */
|
|
|
|
if (Flag(FRESTRICTED) && (strcmp(tvar, "PATH") == 0 ||
|
|
|
|
strcmp(tvar, "ENV") == 0 || strcmp(tvar, "SHELL") == 0))
|
|
|
|
errorf("%s: restricted", tvar);
|
|
|
|
|
|
|
|
vp = (set&LOCAL) ? local(tvar, (set & LOCAL_COPY) ? true : false) :
|
|
|
|
global(tvar);
|
|
|
|
set &= ~(LOCAL|LOCAL_COPY);
|
|
|
|
|
|
|
|
vpbase = (vp->flag & ARRAY) ? global(arrayname(var)) : vp;
|
|
|
|
|
|
|
|
/* only allow export flag to be set. at&t ksh allows any attribute to
|
2006-01-30 13:37:24 +01:00
|
|
|
* be changed which means it can be truncated or modified (-L/-R/-Z/-i)
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
if ((vpbase->flag&RDONLY) &&
|
|
|
|
(val || clr || (set & ~EXPORT)))
|
|
|
|
/* XXX check calls - is error here ok by POSIX? */
|
|
|
|
errorf("%s: is read only", tvar);
|
2006-11-12 15:58:16 +01:00
|
|
|
afree(tvar, ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* most calls are with set/clr == 0 */
|
|
|
|
if (set | clr) {
|
|
|
|
int ok = 1;
|
|
|
|
/* XXX if x[0] isn't set, there will be problems: need to have
|
|
|
|
* one copy of attributes for arrays...
|
|
|
|
*/
|
|
|
|
for (t = vpbase; t; t = t->u.array) {
|
|
|
|
int fake_assign;
|
|
|
|
char *s = NULL;
|
|
|
|
char *free_me = NULL;
|
|
|
|
|
|
|
|
fake_assign = (t->flag & ISSET) && (!val || t != vp) &&
|
|
|
|
((set & (UCASEV_AL|LCASEV|LJUST|RJUST|ZEROFIL)) ||
|
|
|
|
((t->flag & INTEGER) && (clr & INTEGER)) ||
|
|
|
|
(!(t->flag & INTEGER) && (set & INTEGER)));
|
|
|
|
if (fake_assign) {
|
|
|
|
if (t->flag & INTEGER) {
|
|
|
|
s = str_val(t);
|
|
|
|
free_me = NULL;
|
|
|
|
} else {
|
|
|
|
s = t->val.s + t->type;
|
|
|
|
free_me = (t->flag & ALLOC) ? t->val.s :
|
|
|
|
NULL;
|
|
|
|
}
|
|
|
|
t->flag &= ~ALLOC;
|
|
|
|
}
|
|
|
|
if (!(t->flag & INTEGER) && (set & INTEGER)) {
|
|
|
|
t->type = 0;
|
|
|
|
t->flag &= ~ALLOC;
|
|
|
|
}
|
|
|
|
t->flag = (t->flag | set) & ~clr;
|
|
|
|
/* Don't change base if assignment is to be done,
|
|
|
|
* in case assignment fails.
|
|
|
|
*/
|
|
|
|
if ((set & INTEGER) && base > 0 && (!val || t != vp))
|
|
|
|
t->type = base;
|
|
|
|
if (set & (LJUST|RJUST|ZEROFIL))
|
|
|
|
t->u2.field = field;
|
|
|
|
if (fake_assign) {
|
|
|
|
if (!setstr(t, s, KSH_RETURN_ERROR)) {
|
|
|
|
/* Somewhat arbitrary action here:
|
|
|
|
* zap contents of variable, but keep
|
|
|
|
* the flag settings.
|
|
|
|
*/
|
|
|
|
ok = 0;
|
|
|
|
if (t->flag & INTEGER)
|
|
|
|
t->flag &= ~ISSET;
|
|
|
|
else {
|
|
|
|
if (t->flag & ALLOC)
|
|
|
|
afree((void*) t->val.s,
|
|
|
|
t->areap);
|
|
|
|
t->flag &= ~(ISSET|ALLOC);
|
|
|
|
t->type = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (free_me)
|
|
|
|
afree((void *) free_me, t->areap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!ok)
|
|
|
|
errorf(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (val != NULL) {
|
|
|
|
if (vp->flag&INTEGER) {
|
|
|
|
/* do not zero base before assignment */
|
|
|
|
setstr(vp, val, KSH_UNWIND_ERROR | 0x4);
|
|
|
|
/* Done after assignment to override default */
|
|
|
|
if (base > 0)
|
|
|
|
vp->type = base;
|
|
|
|
} else
|
|
|
|
/* setstr can't fail (readonly check already done) */
|
|
|
|
setstr(vp, val, KSH_RETURN_ERROR | 0x4);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* only x[0] is ever exported, so use vpbase */
|
|
|
|
if ((vpbase->flag&EXPORT) && !(vpbase->flag&INTEGER) &&
|
|
|
|
vpbase->type == 0)
|
|
|
|
export(vpbase, (vpbase->flag&ISSET) ? vpbase->val.s : null);
|
|
|
|
|
|
|
|
return vp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unset a variable. array_ref is set if there was an array reference in
|
|
|
|
* the name lookup (eg, x[2]).
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
unset(struct tbl *vp, int array_ref)
|
|
|
|
{
|
|
|
|
if (vp->flag & ALLOC)
|
|
|
|
afree((void*)vp->val.s, vp->areap);
|
|
|
|
if ((vp->flag & ARRAY) && !array_ref) {
|
|
|
|
struct tbl *a, *tmp;
|
|
|
|
|
|
|
|
/* Free up entire array */
|
|
|
|
for (a = vp->u.array; a; ) {
|
|
|
|
tmp = a;
|
|
|
|
a = a->u.array;
|
|
|
|
if (tmp->flag & ALLOC)
|
|
|
|
afree((void *) tmp->val.s, tmp->areap);
|
|
|
|
afree(tmp, tmp->areap);
|
|
|
|
}
|
|
|
|
vp->u.array = NULL;
|
|
|
|
}
|
|
|
|
/* If foo[0] is being unset, the remainder of the array is kept... */
|
|
|
|
vp->flag &= SPECIAL | (array_ref ? ARRAY|DEFINED : 0);
|
|
|
|
if (vp->flag & SPECIAL)
|
|
|
|
unsetspec(vp); /* responsible for 'unspecial'ing var */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return a pointer to the first char past a legal variable name (returns the
|
|
|
|
* argument if there is no legal name, returns * a pointer to the terminating
|
|
|
|
* null if whole string is legal).
|
|
|
|
*/
|
2006-11-12 15:58:16 +01:00
|
|
|
const char *
|
2005-05-23 05:06:10 +02:00
|
|
|
skip_varname(const char *s, int aok)
|
|
|
|
{
|
|
|
|
int alen;
|
|
|
|
|
2006-11-10 08:52:04 +01:00
|
|
|
if (s && ksh_isalphx(*s)) {
|
|
|
|
while (*++s && ksh_isalnux(*s))
|
2005-05-23 05:06:10 +02:00
|
|
|
;
|
|
|
|
if (aok && *s == '[' && (alen = array_ref_len(s)))
|
|
|
|
s += alen;
|
|
|
|
}
|
2006-11-12 15:58:16 +01:00
|
|
|
return (s);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return a pointer to the first character past any legal variable name. */
|
2006-11-12 15:58:16 +01:00
|
|
|
const char *
|
2005-05-23 05:06:10 +02:00
|
|
|
skip_wdvarname(const char *s,
|
|
|
|
int aok) /* skip array de-reference? */
|
|
|
|
{
|
2006-11-10 08:52:04 +01:00
|
|
|
if (s[0] == CHAR && ksh_isalphx(s[1])) {
|
2005-05-23 05:06:10 +02:00
|
|
|
do {
|
|
|
|
s += 2;
|
2006-11-10 08:52:04 +01:00
|
|
|
} while (s[0] == CHAR && ksh_isalnux(s[1]));
|
2005-05-23 05:06:10 +02:00
|
|
|
if (aok && s[0] == CHAR && s[1] == '[') {
|
|
|
|
/* skip possible array de-reference */
|
|
|
|
const char *p = s;
|
|
|
|
char c;
|
|
|
|
int depth = 0;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (p[0] != CHAR)
|
|
|
|
break;
|
|
|
|
c = p[1];
|
|
|
|
p += 2;
|
|
|
|
if (c == '[')
|
|
|
|
depth++;
|
|
|
|
else if (c == ']' && --depth == 0) {
|
|
|
|
s = p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-11-12 15:58:16 +01:00
|
|
|
return (s);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if coded string s is a variable name */
|
|
|
|
int
|
|
|
|
is_wdvarname(const char *s, int aok)
|
|
|
|
{
|
2006-11-12 15:58:16 +01:00
|
|
|
const char *p = skip_wdvarname(s, aok);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
return p != s && p[0] == EOS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if coded string s is a variable assignment */
|
|
|
|
int
|
|
|
|
is_wdvarassign(const char *s)
|
|
|
|
{
|
2006-11-12 15:58:16 +01:00
|
|
|
const char *p = skip_wdvarname(s, true);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
return p != s && p[0] == CHAR && p[1] == '=';
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make the exported environment from the exported names in the dictionary.
|
|
|
|
*/
|
|
|
|
char **
|
|
|
|
makenv(void)
|
|
|
|
{
|
2007-07-01 23:10:29 +02:00
|
|
|
struct block *l;
|
2005-05-23 05:06:10 +02:00
|
|
|
XPtrV env;
|
|
|
|
struct tbl *vp, **vpp;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
XPinit(env, 64);
|
|
|
|
for (l = e->loc; l != NULL; l = l->next)
|
|
|
|
for (vpp = l->vars.tbls, i = l->vars.size; --i >= 0; )
|
|
|
|
if ((vp = *vpp++) != NULL &&
|
|
|
|
(vp->flag&(ISSET|EXPORT)) == (ISSET|EXPORT)) {
|
|
|
|
struct block *l2;
|
|
|
|
struct tbl *vp2;
|
|
|
|
unsigned h = hash(vp->name);
|
|
|
|
|
|
|
|
/* unexport any redefined instances */
|
|
|
|
for (l2 = l->next; l2 != NULL; l2 = l2->next) {
|
2006-01-29 21:04:54 +01:00
|
|
|
vp2 = ktsearch(&l2->vars, vp->name, h);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (vp2 != NULL)
|
|
|
|
vp2->flag &= ~EXPORT;
|
|
|
|
}
|
|
|
|
if ((vp->flag&INTEGER)) {
|
|
|
|
/* integer to string */
|
|
|
|
char *val;
|
|
|
|
val = str_val(vp);
|
|
|
|
vp->flag &= ~(INTEGER|RDONLY);
|
|
|
|
/* setstr can't fail here */
|
|
|
|
setstr(vp, val, KSH_RETURN_ERROR);
|
|
|
|
}
|
|
|
|
XPput(env, vp->val.s);
|
|
|
|
}
|
|
|
|
XPput(env, NULL);
|
|
|
|
return (char **) XPclose(env);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Someone has set the srand() value, therefore from now on
|
|
|
|
* we return values from rand() instead of arc4random()
|
|
|
|
*/
|
2006-01-29 21:10:16 +01:00
|
|
|
#if HAVE_ARC4RANDOM
|
2006-11-19 17:43:43 +01:00
|
|
|
static int use_rand = 0;
|
2006-01-29 21:10:16 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Called after a fork in parent to bump the random number generator.
|
|
|
|
* Done to ensure children will not get the same random number sequence
|
|
|
|
* if the parent doesn't use $RANDOM.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
change_random(void)
|
|
|
|
{
|
2006-01-29 21:10:16 +01:00
|
|
|
#if HAVE_ARC4RANDOM
|
2005-05-23 05:06:10 +02:00
|
|
|
if (use_rand)
|
2006-01-29 21:10:16 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
rand();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* handle special variables with side effects - PATH, SECONDS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Test if name is a special parameter */
|
|
|
|
static int
|
|
|
|
special(const char *name)
|
|
|
|
{
|
|
|
|
struct tbl *tp;
|
|
|
|
|
2006-01-29 21:04:54 +01:00
|
|
|
tp = ktsearch(&specials, name, hash(name));
|
2005-05-23 05:06:10 +02:00
|
|
|
return tp && (tp->flag & ISSET) ? tp->type : V_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make a variable non-special */
|
|
|
|
static void
|
|
|
|
unspecial(const char *name)
|
|
|
|
{
|
|
|
|
struct tbl *tp;
|
|
|
|
|
2006-01-29 21:04:54 +01:00
|
|
|
tp = ktsearch(&specials, name, hash(name));
|
2005-05-23 05:06:10 +02:00
|
|
|
if (tp)
|
2006-01-29 21:04:54 +01:00
|
|
|
ktdelete(tp);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static time_t seconds; /* time SECONDS last set */
|
|
|
|
static int user_lineno; /* what user set $LINENO to */
|
|
|
|
|
|
|
|
static void
|
|
|
|
getspec(struct tbl *vp)
|
|
|
|
{
|
|
|
|
switch (special(vp->name)) {
|
|
|
|
case V_SECONDS:
|
|
|
|
vp->flag &= ~SPECIAL;
|
|
|
|
/* On start up the value of SECONDS is used before seconds
|
|
|
|
* has been set - don't do anything in this case
|
|
|
|
* (see initcoms[] in main.c).
|
|
|
|
*/
|
2006-11-10 05:22:13 +01:00
|
|
|
if (vp->flag & ISSET) {
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
setint(vp, tv.tv_sec - seconds);
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
vp->flag |= SPECIAL;
|
|
|
|
break;
|
|
|
|
case V_RANDOM:
|
|
|
|
vp->flag &= ~SPECIAL;
|
2006-01-29 21:10:16 +01:00
|
|
|
#if HAVE_ARC4RANDOM
|
2006-11-19 17:43:43 +01:00
|
|
|
if (!use_rand)
|
|
|
|
setint(vp, arc4random() & 0x7FFF);
|
2005-05-23 05:06:10 +02:00
|
|
|
else
|
2005-05-23 16:22:03 +02:00
|
|
|
#endif
|
2006-11-19 17:43:43 +01:00
|
|
|
setint(vp, rand() & 0x7FFF);
|
2005-05-23 05:06:10 +02:00
|
|
|
vp->flag |= SPECIAL;
|
|
|
|
break;
|
|
|
|
case V_HISTSIZE:
|
|
|
|
vp->flag &= ~SPECIAL;
|
|
|
|
setint(vp, (long) histsize);
|
|
|
|
vp->flag |= SPECIAL;
|
|
|
|
break;
|
|
|
|
case V_OPTIND:
|
|
|
|
vp->flag &= ~SPECIAL;
|
|
|
|
setint(vp, (long) user_opt.uoptind);
|
|
|
|
vp->flag |= SPECIAL;
|
|
|
|
break;
|
|
|
|
case V_LINENO:
|
|
|
|
vp->flag &= ~SPECIAL;
|
|
|
|
setint(vp, (long) current_lineno + user_lineno);
|
|
|
|
vp->flag |= SPECIAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
setspec(struct tbl *vp)
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
switch (special(vp->name)) {
|
|
|
|
case V_PATH:
|
|
|
|
if (path)
|
|
|
|
afree(path, APERM);
|
2007-06-07 01:28:17 +02:00
|
|
|
s = str_val(vp);
|
|
|
|
path = str_save(s, APERM);
|
2005-05-23 05:06:10 +02:00
|
|
|
flushcom(1); /* clear tracked aliases */
|
|
|
|
break;
|
|
|
|
case V_IFS:
|
|
|
|
setctypes(s = str_val(vp), C_IFS);
|
|
|
|
ifs0 = *s;
|
|
|
|
break;
|
|
|
|
case V_OPTIND:
|
|
|
|
vp->flag &= ~SPECIAL;
|
|
|
|
getopts_reset((int) intval(vp));
|
|
|
|
vp->flag |= SPECIAL;
|
|
|
|
break;
|
|
|
|
case V_TMPDIR:
|
|
|
|
if (tmpdir) {
|
|
|
|
afree(tmpdir, APERM);
|
|
|
|
tmpdir = NULL;
|
|
|
|
}
|
|
|
|
/* Use tmpdir iff it is an absolute path, is writable and
|
|
|
|
* searchable and is a directory...
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
struct stat statb;
|
|
|
|
|
|
|
|
s = str_val(vp);
|
2006-11-10 07:16:25 +01:00
|
|
|
if (s[0] == '/' && access(s, W_OK|X_OK) == 0 &&
|
2005-05-23 05:06:10 +02:00
|
|
|
stat(s, &statb) == 0 && S_ISDIR(statb.st_mode))
|
|
|
|
tmpdir = str_save(s, APERM);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case V_HISTSIZE:
|
|
|
|
vp->flag &= ~SPECIAL;
|
|
|
|
sethistsize((int) intval(vp));
|
|
|
|
vp->flag |= SPECIAL;
|
|
|
|
break;
|
2007-03-03 22:36:08 +01:00
|
|
|
#if HAVE_PERSISTENT_HISTORY
|
2005-05-23 05:06:10 +02:00
|
|
|
case V_HISTFILE:
|
|
|
|
sethistfile(str_val(vp));
|
|
|
|
break;
|
2006-11-10 06:23:14 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
case V_COLUMNS:
|
|
|
|
if ((x_cols = intval(vp)) <= MIN_COLS)
|
|
|
|
x_cols = MIN_COLS;
|
|
|
|
break;
|
|
|
|
case V_RANDOM:
|
|
|
|
vp->flag &= ~SPECIAL;
|
|
|
|
srand((unsigned int)intval(vp));
|
2006-01-29 21:16:51 +01:00
|
|
|
#if HAVE_ARC4RANDOM
|
2005-05-23 05:06:10 +02:00
|
|
|
use_rand = 1;
|
2006-06-21 21:35:38 +02:00
|
|
|
#if HAVE_ARC4RANDOM_PUSH
|
2005-05-23 05:06:10 +02:00
|
|
|
arc4random_push((unsigned)vp ^ (unsigned)rand());
|
2006-01-29 21:16:51 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
#endif
|
|
|
|
vp->flag |= SPECIAL;
|
|
|
|
break;
|
|
|
|
case V_SECONDS:
|
|
|
|
vp->flag &= ~SPECIAL;
|
2006-11-10 05:22:13 +01:00
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
seconds = tv.tv_sec - intval(vp);
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
vp->flag |= SPECIAL;
|
|
|
|
break;
|
|
|
|
case V_TMOUT:
|
|
|
|
/* at&t ksh seems to do this (only listen if integer) */
|
|
|
|
if (vp->flag & INTEGER)
|
|
|
|
ksh_tmout = vp->val.i >= 0 ? vp->val.i : 0;
|
|
|
|
break;
|
|
|
|
case V_LINENO:
|
|
|
|
vp->flag &= ~SPECIAL;
|
|
|
|
/* The -1 is because line numbering starts at 1. */
|
|
|
|
user_lineno = (unsigned int) intval(vp) - current_lineno - 1;
|
|
|
|
vp->flag |= SPECIAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
unsetspec(struct tbl *vp)
|
|
|
|
{
|
|
|
|
switch (special(vp->name)) {
|
|
|
|
case V_PATH:
|
|
|
|
if (path)
|
|
|
|
afree(path, APERM);
|
|
|
|
path = str_save(def_path, APERM);
|
|
|
|
flushcom(1); /* clear tracked aliases */
|
|
|
|
break;
|
|
|
|
case V_IFS:
|
|
|
|
setctypes(" \t\n", C_IFS);
|
|
|
|
ifs0 = ' ';
|
|
|
|
break;
|
|
|
|
case V_TMPDIR:
|
|
|
|
/* should not become unspecial */
|
|
|
|
if (tmpdir) {
|
|
|
|
afree(tmpdir, APERM);
|
|
|
|
tmpdir = NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case V_LINENO:
|
|
|
|
case V_RANDOM:
|
|
|
|
case V_SECONDS:
|
|
|
|
case V_TMOUT: /* at&t ksh leaves previous value in place */
|
|
|
|
unspecial(vp->name);
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* at&t ksh man page says OPTIND, OPTARG and _ lose special meaning,
|
|
|
|
* but OPTARG does not (still set by getopts) and _ is also still
|
|
|
|
* set in various places.
|
|
|
|
* Don't know what at&t does for:
|
|
|
|
* HISTSIZE, HISTFILE,
|
|
|
|
* Unsetting these in at&t ksh does not loose the 'specialness':
|
2005-07-04 14:07:41 +02:00
|
|
|
* no effect: IFS, COLUMNS, PATH, TMPDIR
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Search for (and possibly create) a table entry starting with
|
|
|
|
* vp, indexed by val.
|
|
|
|
*/
|
|
|
|
static struct tbl *
|
|
|
|
arraysearch(struct tbl *vp, int val)
|
|
|
|
{
|
|
|
|
struct tbl *prev, *curr, *new;
|
|
|
|
size_t namelen = strlen(vp->name) + 1;
|
|
|
|
|
|
|
|
vp->flag |= ARRAY|DEFINED;
|
2006-05-27 00:17:21 +02:00
|
|
|
vp->index = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
/* The table entry is always [0] */
|
2006-05-27 00:17:21 +02:00
|
|
|
if (val == 0)
|
2005-05-23 05:06:10 +02:00
|
|
|
return vp;
|
|
|
|
prev = vp;
|
|
|
|
curr = vp->u.array;
|
|
|
|
while (curr && curr->index < val) {
|
|
|
|
prev = curr;
|
|
|
|
curr = curr->u.array;
|
|
|
|
}
|
|
|
|
if (curr && curr->index == val) {
|
|
|
|
if (curr->flag&ISSET)
|
|
|
|
return curr;
|
|
|
|
else
|
|
|
|
new = curr;
|
|
|
|
} else
|
|
|
|
new = (struct tbl *)alloc(sizeof(struct tbl) + namelen,
|
|
|
|
vp->areap);
|
|
|
|
strlcpy(new->name, vp->name, namelen);
|
|
|
|
new->flag = vp->flag & ~(ALLOC|DEFINED|ISSET|SPECIAL);
|
|
|
|
new->type = vp->type;
|
|
|
|
new->areap = vp->areap;
|
|
|
|
new->u2.field = vp->u2.field;
|
|
|
|
new->index = val;
|
|
|
|
if (curr != new) { /* not reusing old array entry */
|
|
|
|
prev->u.array = new;
|
|
|
|
new->u.array = curr;
|
|
|
|
}
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the length of an array reference (eg, [1+2]) - cp is assumed
|
|
|
|
* to point to the open bracket. Returns 0 if there is no matching closing
|
|
|
|
* bracket.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
array_ref_len(const char *cp)
|
|
|
|
{
|
|
|
|
const char *s = cp;
|
|
|
|
int c;
|
|
|
|
int depth = 0;
|
|
|
|
|
|
|
|
while ((c = *s++) && (c != ']' || --depth))
|
|
|
|
if (c == '[')
|
|
|
|
depth++;
|
|
|
|
if (!c)
|
|
|
|
return 0;
|
|
|
|
return s - cp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make a copy of the base of an array name
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
arrayname(const char *str)
|
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
if ((p = cstrchr(str, '[')) == 0)
|
2005-05-23 05:06:10 +02:00
|
|
|
/* Shouldn't happen, but why worry? */
|
2006-11-12 15:58:16 +01:00
|
|
|
return str_save(str, ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
return str_nsave(str, p - str, ATEMP);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set (or overwrite, if !reset) the array variable var to the values in vals.
|
|
|
|
*/
|
|
|
|
void
|
2007-03-04 01:13:17 +01:00
|
|
|
set_array(const char *var, int reset, const char **vals)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
struct tbl *vp, *vq;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* to get local array, use "typeset foo; set -A foo" */
|
|
|
|
vp = global(var);
|
|
|
|
|
|
|
|
/* Note: at&t ksh allows set -A but not set +A of a read-only var */
|
|
|
|
if ((vp->flag&RDONLY))
|
|
|
|
errorf("%s: is read only", var);
|
|
|
|
/* This code is quite non-optimal */
|
|
|
|
if (reset > 0)
|
|
|
|
/* trash existing values and attributes */
|
|
|
|
unset(vp, 0);
|
|
|
|
/* todo: would be nice for assignment to completely succeed or
|
|
|
|
* completely fail. Only really effects integer arrays:
|
|
|
|
* evaluation of some of vals[] may fail...
|
|
|
|
*/
|
|
|
|
for (i = 0; vals[i]; i++) {
|
|
|
|
vq = arraysearch(vp, i);
|
|
|
|
/* would be nice to deal with errors here... (see above) */
|
|
|
|
setstr(vq, vals[i], KSH_RETURN_ERROR);
|
|
|
|
}
|
|
|
|
}
|