Home | History | Annotate | Line # | Download | only in ksh
var.c revision 1.1.1.2
      1      1.1  jtc #include "sh.h"
      2      1.1  jtc #include "ksh_time.h"
      3      1.1  jtc #include "ksh_limval.h"
      4      1.1  jtc #include "ksh_stat.h"
      5      1.1  jtc #include <ctype.h>
      6      1.1  jtc 
      7      1.1  jtc /*
      8      1.1  jtc  * Variables
      9      1.1  jtc  *
     10      1.1  jtc  * WARNING: unreadable code, needs a rewrite
     11      1.1  jtc  *
     12      1.1  jtc  * if (flag&INTEGER), val.i contains integer value, and type contains base.
     13      1.1  jtc  * otherwise, (val.s + type) contains string value.
     14      1.1  jtc  * if (flag&EXPORT), val.s contains "name=value" for E-Z exporting.
     15      1.1  jtc  */
     16      1.1  jtc static	struct tbl vtemp;
     17      1.1  jtc static	struct table specials;
     18      1.1  jtc static char	*formatstr	ARGS((struct tbl *vp, const char *s));
     19      1.1  jtc static void	export		ARGS((struct tbl *vp, const char *val));
     20      1.1  jtc static int	special		ARGS((const char *name));
     21      1.1  jtc static void	getspec		ARGS((struct tbl *vp));
     22      1.1  jtc static void	setspec		ARGS((struct tbl *vp));
     23      1.1  jtc static void	unsetspec	ARGS((struct tbl *vp));
     24      1.1  jtc static struct tbl *arraysearch  ARGS((struct tbl *, int));
     25      1.1  jtc 
     26      1.1  jtc /*
     27      1.1  jtc  * create a new block for function calls and simple commands
     28      1.1  jtc  * assume caller has allocated and set up e->loc
     29      1.1  jtc  */
     30      1.1  jtc void
     31      1.1  jtc newblock()
     32      1.1  jtc {
     33      1.1  jtc 	register struct block *l;
     34      1.1  jtc 	static char *const empty[] = {null};
     35      1.1  jtc 
     36      1.1  jtc 	l = (struct block *) alloc(sizeof(struct block), ATEMP);
     37      1.1  jtc 	ainit(&l->area);
     38      1.1  jtc 	if (!e->loc) {
     39      1.1  jtc 		l->argc = 0;
     40      1.1  jtc 		l->argv = (char **) empty;
     41      1.1  jtc 	} else {
     42      1.1  jtc 		l->argc = e->loc->argc;
     43      1.1  jtc 		l->argv = e->loc->argv;
     44      1.1  jtc 	}
     45      1.1  jtc 	l->exit = l->error = NULL;
     46      1.1  jtc 	tinit(&l->vars, &l->area, 0);
     47      1.1  jtc 	tinit(&l->funs, &l->area, 0);
     48      1.1  jtc 	l->next = e->loc;
     49      1.1  jtc 	e->loc = l;
     50      1.1  jtc }
     51      1.1  jtc 
     52      1.1  jtc /*
     53      1.1  jtc  * pop a block handling special variables
     54      1.1  jtc  */
     55      1.1  jtc void
     56      1.1  jtc popblock()
     57      1.1  jtc {
     58      1.1  jtc 	register struct block *l = e->loc;
     59      1.1  jtc 	register struct tbl *vp, **vpp = l->vars.tbls, *vq;
     60      1.1  jtc 	register int i;
     61      1.1  jtc 
     62      1.1  jtc 	e->loc = l->next;	/* pop block */
     63      1.1  jtc 	for (i = l->vars.size; --i >= 0; )
     64      1.1  jtc 		if ((vp = *vpp++) != NULL && (vp->flag&SPECIAL))
     65      1.1  jtc 			if ((vq = global(vp->name))->flag & ISSET)
     66      1.1  jtc 				setspec(vq);
     67      1.1  jtc 			else
     68      1.1  jtc 				unsetspec(vq);
     69      1.1  jtc 	afreeall(&l->area);
     70      1.1  jtc 	afree(l, ATEMP);
     71      1.1  jtc }
     72      1.1  jtc 
     73      1.1  jtc /* called by main() to initialize variable data structures */
     74      1.1  jtc void
     75      1.1  jtc initvar()
     76      1.1  jtc {
     77      1.1  jtc 	static const struct {
     78      1.1  jtc 		const char *name;
     79      1.1  jtc 		int v;
     80      1.1  jtc 	} names[] = {
     81      1.1  jtc 			{ "COLUMNS",		V_COLUMNS },
     82      1.1  jtc 			{ "IFS",		V_IFS },
     83      1.1  jtc 			{ "OPTIND",		V_OPTIND },
     84      1.1  jtc 			{ "PATH",		V_PATH },
     85      1.1  jtc 			{ "POSIXLY_CORRECT",	V_POSIXLY_CORRECT },
     86      1.1  jtc 			{ "TMPDIR",		V_TMPDIR },
     87      1.1  jtc #ifdef HISTORY
     88      1.1  jtc 			{ "HISTFILE",		V_HISTFILE },
     89      1.1  jtc 			{ "HISTSIZE",		V_HISTSIZE },
     90      1.1  jtc #endif /* HISTORY */
     91      1.1  jtc #ifdef EDIT
     92      1.1  jtc 			{ "EDITOR",		V_EDITOR },
     93      1.1  jtc 			{ "VISUAL",		V_VISUAL },
     94      1.1  jtc #endif /* EDIT */
     95      1.1  jtc #ifdef KSH
     96      1.1  jtc 			{ "MAIL",		V_MAIL },
     97      1.1  jtc 			{ "MAILCHECK",		V_MAILCHECK },
     98      1.1  jtc 			{ "MAILPATH",		V_MAILPATH },
     99      1.1  jtc 			{ "RANDOM",		V_RANDOM },
    100      1.1  jtc 			{ "SECONDS",		V_SECONDS },
    101      1.1  jtc 			{ "TMOUT",		V_TMOUT },
    102      1.1  jtc #endif /* KSH */
    103      1.1  jtc 			{ (char *) 0,	0 }
    104      1.1  jtc 		};
    105      1.1  jtc 	int i;
    106      1.1  jtc 	struct tbl *tp;
    107      1.1  jtc 
    108      1.1  jtc 	tinit(&specials, APERM, 32); /* must be 2^n (currently 16 speciasl) */
    109      1.1  jtc 	for (i = 0; names[i].name; i++) {
    110      1.1  jtc 		tp = tenter(&specials, names[i].name, hash(names[i].name));
    111      1.1  jtc 		tp->flag = DEFINED|ISSET;
    112      1.1  jtc 		tp->type = names[i].v;
    113      1.1  jtc 	}
    114      1.1  jtc }
    115      1.1  jtc 
    116      1.1  jtc /* Used to calculate an array index for global()/local().  Sets *arrayp to
    117      1.1  jtc  * non-zero if this is an array, sets *valp to the array index, returns
    118      1.1  jtc  * the basename of the array.
    119      1.1  jtc  */
    120      1.1  jtc const char *
    121      1.1  jtc array_index_calc(n, arrayp, valp)
    122      1.1  jtc 	const char *n;
    123      1.1  jtc 	bool_t *arrayp;
    124      1.1  jtc 	int *valp;
    125      1.1  jtc {
    126      1.1  jtc 	const char *p;
    127      1.1  jtc 	int len;
    128      1.1  jtc 
    129      1.1  jtc 	*arrayp = FALSE;
    130      1.1  jtc 	p = skip_varname(n, FALSE);
    131      1.1  jtc 	if (p != n && *p == '[' && (len = array_ref_len(p))) {
    132      1.1  jtc 		char *sub, *tmp;
    133      1.1  jtc 		long rval;
    134      1.1  jtc 
    135      1.1  jtc 		/* Calculate the value of the subscript */
    136      1.1  jtc 		*arrayp = TRUE;
    137      1.1  jtc 		tmp = str_nsave(p+1, len-2, ATEMP);
    138      1.1  jtc 		sub = substitute(tmp, 0);
    139      1.1  jtc 		afree(tmp, ATEMP);
    140      1.1  jtc 		n = str_nsave(n, p - n, ATEMP);
    141      1.1  jtc 		evaluate(sub, &rval, FALSE);
    142      1.1  jtc 		if (rval < 0 || rval > ARRAYMAX)
    143      1.1  jtc 			errorf("%s: subscript out of range", n);
    144      1.1  jtc 		*valp = rval;
    145      1.1  jtc 		afree(sub, ATEMP);
    146      1.1  jtc 	}
    147      1.1  jtc 	return n;
    148      1.1  jtc }
    149      1.1  jtc 
    150      1.1  jtc /*
    151      1.1  jtc  * Search for variable, if not found create globally.
    152      1.1  jtc  */
    153      1.1  jtc struct tbl *
    154      1.1  jtc global(n)
    155      1.1  jtc 	register const char *n;
    156      1.1  jtc {
    157      1.1  jtc 	register struct block *l = e->loc;
    158      1.1  jtc 	register struct tbl *vp;
    159      1.1  jtc 	register int c;
    160      1.1  jtc 	unsigned h;
    161      1.1  jtc 	bool_t	 array;
    162      1.1  jtc 	int	 val;
    163      1.1  jtc 
    164      1.1  jtc 	/* Check to see if this is an array */
    165      1.1  jtc 	n = array_index_calc(n, &array, &val);
    166      1.1  jtc 	h = hash(n);
    167      1.1  jtc 	c = n[0];
    168      1.1  jtc 	if (!letter(c)) {
    169      1.1  jtc 		if (array)
    170      1.1  jtc 			errorf("bad substitution");
    171      1.1  jtc 		vp = &vtemp;
    172      1.1  jtc 		vp->flag = (DEFINED|RDONLY);
    173      1.1  jtc 		vp->type = 0;
    174      1.1  jtc 		vp->areap = ATEMP;
    175      1.1  jtc 		*vp->name = c;
    176      1.1  jtc 		if (digit(c)) {
    177      1.1  jtc 			for (c = 0; digit(*n); n++)
    178      1.1  jtc 				c = c*10 + *n-'0';
    179      1.1  jtc 			if (c <= l->argc)
    180      1.1  jtc 				setstr(vp, l->argv[c]);
    181      1.1  jtc 			return vp;
    182      1.1  jtc 		}
    183      1.1  jtc 		if (n[1] != '\0')
    184      1.1  jtc 			return vp;
    185      1.1  jtc 		vp->flag |= ISSET|INTEGER;
    186      1.1  jtc 		switch (c) {
    187      1.1  jtc 		  case '$':
    188      1.1  jtc 			vp->val.i = kshpid;
    189      1.1  jtc 			break;
    190      1.1  jtc 		  case '!':
    191      1.1  jtc 			/* If no job, expand to nothing */
    192      1.1  jtc 			if ((vp->val.i = j_async()) == 0)
    193      1.1  jtc 				vp->flag &= ~(ISSET|INTEGER);
    194      1.1  jtc 			break;
    195      1.1  jtc 		  case '?':
    196      1.1  jtc 			vp->val.i = exstat;
    197      1.1  jtc 			break;
    198      1.1  jtc 		  case '#':
    199      1.1  jtc 			vp->val.i = l->argc;
    200      1.1  jtc 			break;
    201      1.1  jtc 		  case '-':
    202      1.1  jtc 			vp->flag &= ~INTEGER;
    203      1.1  jtc 			vp->val.s = getoptions();
    204      1.1  jtc 			break;
    205      1.1  jtc 		  default:
    206      1.1  jtc 			vp->flag &= ~(ISSET|INTEGER);
    207      1.1  jtc 		}
    208      1.1  jtc 		return vp;
    209      1.1  jtc 	}
    210      1.1  jtc 	for (l = e->loc; ; l = l->next) {
    211      1.1  jtc 		vp = tsearch(&l->vars, n, h);
    212      1.1  jtc 		if (vp != NULL)
    213      1.1  jtc 			if (array)
    214      1.1  jtc 				return arraysearch(vp, val);
    215      1.1  jtc 			else
    216      1.1  jtc 				return vp;
    217      1.1  jtc 		if (l->next == NULL)
    218      1.1  jtc 			break;
    219      1.1  jtc 	}
    220      1.1  jtc 	vp = tenter(&l->vars, n, h);
    221      1.1  jtc 	if (array)
    222      1.1  jtc 		vp = arraysearch(vp, val);
    223      1.1  jtc 	vp->flag |= DEFINED;
    224      1.1  jtc 	if (special(n))
    225      1.1  jtc 		vp->flag |= SPECIAL;
    226      1.1  jtc 	return vp;
    227      1.1  jtc }
    228      1.1  jtc 
    229      1.1  jtc /*
    230      1.1  jtc  * Search for local variable, if not found create locally.
    231      1.1  jtc  */
    232      1.1  jtc struct tbl *
    233      1.1  jtc local(n, copy)
    234      1.1  jtc 	register const char *n;
    235      1.1  jtc 	bool_t copy;
    236      1.1  jtc {
    237      1.1  jtc 	register struct block *l = e->loc;
    238      1.1  jtc 	register struct tbl *vp;
    239      1.1  jtc 	unsigned h;
    240      1.1  jtc 	bool_t	 array;
    241      1.1  jtc 	int	 val;
    242      1.1  jtc 
    243      1.1  jtc 	/* Check to see if this is an array */
    244      1.1  jtc 	n = array_index_calc(n, &array, &val);
    245      1.1  jtc 	h = hash(n);
    246      1.1  jtc 	if (!letter(*n)) {
    247      1.1  jtc 		vp = &vtemp;
    248      1.1  jtc 		vp->flag = DEFINED|RDONLY;
    249      1.1  jtc 		vp->type = 0;
    250      1.1  jtc 		vp->areap = ATEMP;
    251      1.1  jtc 		return vp;
    252      1.1  jtc 	}
    253      1.1  jtc 	vp = tenter(&l->vars, n, h);
    254      1.1  jtc 	if (copy && !(vp->flag & DEFINED)) {
    255      1.1  jtc 		struct block *ll = l;
    256      1.1  jtc 		struct tbl *vq = (struct tbl *) 0;
    257      1.1  jtc 
    258      1.1  jtc 		while ((ll = ll->next) && !(vq = tsearch(&ll->vars, n, h)))
    259      1.1  jtc 			;
    260      1.1  jtc 		if (vq) {
    261      1.1  jtc 			vp->flag |= vq->flag & (EXPORT|INTEGER|RDONLY
    262      1.1  jtc 						|LJUST|RJUST|ZEROFIL
    263      1.1  jtc 						|LCASEV|UCASEV_AL|INT_U|INT_L);
    264      1.1  jtc 			if (vq->flag & INTEGER)
    265      1.1  jtc 				vp->type = vq->type;
    266      1.1  jtc 			vp->u2.field = vq->u2.field;
    267      1.1  jtc 		}
    268      1.1  jtc 	}
    269      1.1  jtc 	if (array)
    270      1.1  jtc 		vp = arraysearch(vp, val);
    271      1.1  jtc 	vp->flag |= DEFINED;
    272      1.1  jtc 	if (special(n))
    273      1.1  jtc 		vp->flag |= SPECIAL;
    274      1.1  jtc 	return vp;
    275      1.1  jtc }
    276      1.1  jtc 
    277      1.1  jtc /* get variable string value */
    278      1.1  jtc char *
    279      1.1  jtc str_val(vp)
    280      1.1  jtc 	register struct tbl *vp;
    281      1.1  jtc {
    282      1.1  jtc 	char *s;
    283      1.1  jtc 
    284      1.1  jtc 	if ((vp->flag&SPECIAL))
    285      1.1  jtc 		getspec(vp);
    286      1.1  jtc 	if (!(vp->flag&ISSET))
    287      1.1  jtc 		s = null;		/* special to dollar() */
    288      1.1  jtc 	else if (!(vp->flag&INTEGER))	/* string source */
    289      1.1  jtc 		s = vp->val.s + vp->type;
    290      1.1  jtc 	else {				/* integer source */
    291      1.1  jtc 		/* worst case number length is when base=2, so use BITS(long) */
    292      1.1  jtc 			     /* minus base #     number    null */
    293      1.1  jtc 		static char strbuf[1 + 2 + 1 + BITS(long) + 1];
    294      1.1  jtc 		const char *digits = (vp->flag & UCASEV_AL) ?
    295      1.1  jtc 				  "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    296      1.1  jtc 				: "0123456789abcdefghijklmnopqrstuvwxyz";
    297      1.1  jtc 		register unsigned long n;
    298      1.1  jtc 		register int base;
    299      1.1  jtc 
    300      1.1  jtc 		s = strbuf + sizeof(strbuf);
    301      1.1  jtc 		if (vp->flag & INT_U)
    302      1.1  jtc 			n = (unsigned long) vp->val.i;
    303      1.1  jtc 		else
    304      1.1  jtc 			n = (vp->val.i < 0) ? -vp->val.i : vp->val.i;
    305      1.1  jtc 		base = (vp->type == 0) ? 10 : vp->type;
    306      1.1  jtc 
    307      1.1  jtc 		*--s = '\0';
    308      1.1  jtc 		do {
    309      1.1  jtc 			*--s = digits[n % base];
    310      1.1  jtc 			n /= base;
    311      1.1  jtc 		} while (n != 0);
    312      1.1  jtc 		if (base != 10) {
    313      1.1  jtc 			*--s = '#';
    314      1.1  jtc 			*--s = digits[base % 10];
    315      1.1  jtc 			if (base >= 10)
    316      1.1  jtc 				*--s = digits[base / 10];
    317      1.1  jtc 		}
    318      1.1  jtc 		if (!(vp->flag & INT_U) && vp->val.i < 0)
    319      1.1  jtc 			*--s = '-';
    320      1.1  jtc 		if (vp->flag & (RJUST|LJUST)) /* case already dealt with */
    321      1.1  jtc 			s = formatstr(vp, s);
    322      1.1  jtc 	}
    323      1.1  jtc 	return s;
    324      1.1  jtc }
    325      1.1  jtc 
    326      1.1  jtc /* get variable integer value, with error checking */
    327      1.1  jtc long
    328      1.1  jtc intval(vp)
    329      1.1  jtc 	register struct tbl *vp;
    330      1.1  jtc {
    331      1.1  jtc 	long num;
    332      1.1  jtc 	int base;
    333      1.1  jtc 
    334      1.1  jtc 	base = getint(vp, &num);
    335      1.1  jtc 	if (base == -1)
    336      1.1  jtc 		/* XXX check calls - is error here ok by POSIX? */
    337      1.1  jtc 		errorf("%s: bad number", str_val(vp));
    338      1.1  jtc 	return num;
    339      1.1  jtc }
    340      1.1  jtc 
    341      1.1  jtc /* set variable to string value */
    342      1.1  jtc void
    343      1.1  jtc setstr(vq, s)
    344      1.1  jtc 	register struct tbl *vq;
    345      1.1  jtc 	const char *s;
    346      1.1  jtc {
    347      1.1  jtc 	if (!(vq->flag&INTEGER)) { /* string dest */
    348      1.1  jtc 		if ((vq->flag&ALLOC)) {
    349      1.1  jtc 			/* debugging */
    350      1.1  jtc 			if (s >= vq->val.s
    351      1.1  jtc 			    && s <= vq->val.s + strlen(vq->val.s))
    352      1.1  jtc 				internal_errorf(TRUE,
    353      1.1  jtc 				    "setstr: %s=%s: assigning to self",
    354      1.1  jtc 				    vq->name, s);
    355      1.1  jtc 			afree((void*)vq->val.s, vq->areap);
    356      1.1  jtc 		}
    357      1.1  jtc 		vq->flag &= ~(ISSET|ALLOC);
    358      1.1  jtc 		vq->type = 0;
    359      1.1  jtc 		if (s && (vq->flag & (UCASEV_AL|LCASEV|LJUST|RJUST)))
    360      1.1  jtc 			s = formatstr(vq, s);
    361      1.1  jtc 		if ((vq->flag&EXPORT))
    362      1.1  jtc 			export(vq, s);
    363      1.1  jtc 		else {
    364      1.1  jtc 			vq->val.s = str_save(s, vq->areap);
    365      1.1  jtc 			if (vq->val.s)		/* <sjg> don't lie */
    366      1.1  jtc 				vq->flag |= ALLOC;
    367      1.1  jtc 		}
    368      1.1  jtc 	} else			/* integer dest */
    369      1.1  jtc 		/* XXX is this correct? */
    370      1.1  jtc 		v_evaluate(vq, s, FALSE);
    371      1.1  jtc 	vq->flag |= ISSET;
    372      1.1  jtc 	if ((vq->flag&SPECIAL))
    373      1.1  jtc 		setspec(vq);
    374      1.1  jtc }
    375      1.1  jtc 
    376      1.1  jtc /* set variable to integer */
    377      1.1  jtc void
    378      1.1  jtc setint(vq, n)
    379      1.1  jtc 	register struct tbl *vq;
    380      1.1  jtc 	long n;
    381      1.1  jtc {
    382      1.1  jtc 	if (!(vq->flag&INTEGER)) {
    383      1.1  jtc 		register struct tbl *vp = &vtemp;
    384      1.1  jtc 		vp->flag = (ISSET|INTEGER);
    385      1.1  jtc 		vp->type = 0;
    386      1.1  jtc 		vp->areap = ATEMP;
    387      1.1  jtc 		vp->val.i = n;
    388      1.1  jtc 		setstr(vq, str_val(vp));
    389      1.1  jtc 	} else
    390      1.1  jtc 		vq->val.i = n;
    391      1.1  jtc 	vq->flag |= ISSET;
    392      1.1  jtc 	if ((vq->flag&SPECIAL))
    393      1.1  jtc 		setspec(vq);
    394      1.1  jtc }
    395      1.1  jtc 
    396      1.1  jtc int
    397      1.1  jtc getint(vp, nump)
    398      1.1  jtc 	struct tbl *vp;
    399      1.1  jtc 	long *nump;
    400      1.1  jtc {
    401      1.1  jtc 	register char *s;
    402      1.1  jtc 	register int c;
    403      1.1  jtc 	int base, neg;
    404      1.1  jtc 	int have_base = 0;
    405      1.1  jtc 	long num;
    406      1.1  jtc 
    407      1.1  jtc 	if (vp->flag&SPECIAL)
    408      1.1  jtc 		getspec(vp);
    409      1.1  jtc 	/* XXX is it possible for ISSET to be set and val.s to be 0? */
    410      1.1  jtc 	if (!(vp->flag&ISSET) || (!(vp->flag&INTEGER) && vp->val.s == NULL))
    411      1.1  jtc 		return -1;
    412      1.1  jtc 	if (vp->flag&INTEGER) {
    413      1.1  jtc 		*nump = vp->val.i;
    414      1.1  jtc 		return vp->type;
    415      1.1  jtc 	}
    416      1.1  jtc 	s = vp->val.s + vp->type;
    417      1.1  jtc 	if (s == NULL)	/* redundent given initial test */
    418      1.1  jtc 		s = null;
    419      1.1  jtc 	base = 10;
    420      1.1  jtc 	num = 0;
    421      1.1  jtc 	neg = 0;
    422      1.1  jtc 	for (c = *s++; c ; c = *s++) {
    423      1.1  jtc 		if (c == '-') {
    424      1.1  jtc 			neg++;
    425      1.1  jtc 		} else if (c == '#') {
    426      1.1  jtc 			base = (int) num;
    427      1.1  jtc 			if (have_base || base < 2 || base > 36)
    428      1.1  jtc 				return -1;
    429      1.1  jtc 			num = 0;
    430      1.1  jtc 			have_base = 1;
    431      1.1  jtc 		} else if (letnum(c)) {
    432      1.1  jtc 			if (isdigit(c))
    433      1.1  jtc 				c -= '0';
    434      1.1  jtc 			else if (islower(c))
    435      1.1  jtc 				c -= 'a' - 10; /* todo: assumes ascii */
    436      1.1  jtc 			else if (isupper(c))
    437      1.1  jtc 				c -= 'A' - 10; /* todo: assumes ascii */
    438      1.1  jtc 			else
    439      1.1  jtc 				c = -1; /* _: force error */
    440      1.1  jtc 			if (c < 0 || c >= base)
    441      1.1  jtc 				return -1;
    442      1.1  jtc 			num = num * base + c;
    443      1.1  jtc 		} else
    444      1.1  jtc 			return -1;
    445      1.1  jtc 	}
    446      1.1  jtc 	if (neg)
    447      1.1  jtc 		num = -num;
    448      1.1  jtc 	*nump = num;
    449      1.1  jtc 	return base;
    450      1.1  jtc }
    451      1.1  jtc 
    452      1.1  jtc /* convert variable vq to integer variable, setting its value from vp
    453      1.1  jtc  * (vq and vp may be the same)
    454      1.1  jtc  */
    455      1.1  jtc struct tbl *
    456      1.1  jtc setint_v(vq, vp)
    457      1.1  jtc 	register struct tbl *vq, *vp;
    458      1.1  jtc {
    459      1.1  jtc 	int base;
    460      1.1  jtc 	long num;
    461      1.1  jtc 
    462      1.1  jtc 	if ((base = getint(vp, &num)) == -1)
    463      1.1  jtc 		return NULL;
    464      1.1  jtc 	if (!(vq->flag & INTEGER) && (vq->flag & ALLOC)) {
    465      1.1  jtc 		vq->flag &= ~ALLOC;
    466      1.1  jtc 		afree(vq->val.s, vq->areap);
    467      1.1  jtc 	}
    468      1.1  jtc 	vq->val.i = num;
    469      1.1  jtc 	if (vq->type == 0) /* default base */
    470      1.1  jtc 		vq->type = base;
    471      1.1  jtc 	vq->flag |= ISSET|INTEGER;
    472      1.1  jtc 	if (vq->flag&SPECIAL)
    473      1.1  jtc 		setspec(vq);
    474      1.1  jtc 	return vq;
    475      1.1  jtc }
    476      1.1  jtc 
    477      1.1  jtc static char *
    478      1.1  jtc formatstr(vp, s)
    479      1.1  jtc 	struct tbl *vp;
    480      1.1  jtc 	const char *s;
    481      1.1  jtc {
    482      1.1  jtc 	int olen, nlen;
    483      1.1  jtc 	char *p, *q;
    484      1.1  jtc 
    485      1.1  jtc 	olen = strlen(s);
    486      1.1  jtc 
    487      1.1  jtc 	if (vp->flag & (RJUST|LJUST)) {
    488      1.1  jtc 		if (!vp->u2.field)	/* default field width */
    489      1.1  jtc 			vp->u2.field = olen;
    490      1.1  jtc 		nlen = vp->u2.field;
    491      1.1  jtc 	} else
    492      1.1  jtc 		nlen = olen;
    493      1.1  jtc 
    494      1.1  jtc 	p = (char *) alloc(nlen + 1, ATEMP);
    495      1.1  jtc 	if (vp->flag & (RJUST|LJUST)) {
    496      1.1  jtc 		int slen;
    497      1.1  jtc 
    498      1.1  jtc 		if (vp->flag & RJUST) {
    499      1.1  jtc 			const char *q = s + olen;
    500      1.1  jtc 			/* strip trailing spaces (at&t ksh uses q[-1] == ' ') */
    501      1.1  jtc 			while (q > s && isspace(q[-1]))
    502      1.1  jtc 				--q;
    503      1.1  jtc 			slen = q - s;
    504      1.1  jtc 			if (slen > vp->u2.field) {
    505      1.1  jtc 				s += slen - vp->u2.field;
    506      1.1  jtc 				slen = vp->u2.field;
    507      1.1  jtc 			}
    508      1.1  jtc 			shf_snprintf(p, nlen + 1,
    509      1.1  jtc 				((vp->flag & ZEROFIL) && digit(*s)) ?
    510      1.1  jtc 					  "%0*s%.*s" : "%*s%.*s",
    511      1.1  jtc 				vp->u2.field - slen, null, slen, s);
    512      1.1  jtc 		} else {
    513      1.1  jtc 			/* strip leading spaces/zeros */
    514      1.1  jtc 			while (isspace(*s))
    515      1.1  jtc 				s++;
    516      1.1  jtc 			if (vp->flag & ZEROFIL)
    517      1.1  jtc 				while (*s == '0')
    518      1.1  jtc 					s++;
    519      1.1  jtc 			shf_snprintf(p, nlen + 1, "%-*.*s",
    520      1.1  jtc 				vp->u2.field, vp->u2.field, s);
    521      1.1  jtc 		}
    522      1.1  jtc 	} else
    523      1.1  jtc 		memcpy(p, s, olen + 1);
    524      1.1  jtc 
    525      1.1  jtc 	if (vp->flag & UCASEV_AL) {
    526      1.1  jtc 		for (q = p; *q; q++)
    527      1.1  jtc 			if (islower(*q))
    528      1.1  jtc 				*q = toupper(*q);
    529      1.1  jtc 	} else if (vp->flag & LCASEV) {
    530      1.1  jtc 		for (q = p; *q; q++)
    531      1.1  jtc 			if (isupper(*q))
    532      1.1  jtc 				*q = tolower(*q);
    533      1.1  jtc 	}
    534      1.1  jtc 
    535      1.1  jtc 	return p;
    536      1.1  jtc }
    537      1.1  jtc 
    538      1.1  jtc /*
    539      1.1  jtc  * make vp->val.s be "name=value" for quick exporting.
    540      1.1  jtc  */
    541      1.1  jtc static void
    542      1.1  jtc export(vp, val)
    543      1.1  jtc 	register struct tbl *vp;
    544      1.1  jtc 	const char *val;
    545      1.1  jtc {
    546      1.1  jtc 	register char *xp;
    547      1.1  jtc 	char *op = (vp->flag&ALLOC) ? vp->val.s : NULL;
    548      1.1  jtc 	int namelen = strlen(vp->name);
    549      1.1  jtc 	int vallen = strlen(val) + 1;
    550      1.1  jtc 
    551      1.1  jtc 	vp->flag |= ALLOC;
    552      1.1  jtc 	xp = (char*)alloc(namelen + 1 + vallen, vp->areap);
    553      1.1  jtc 	memcpy(vp->val.s = xp, vp->name, namelen);
    554      1.1  jtc 	xp += namelen;
    555      1.1  jtc 	*xp++ = '=';
    556      1.1  jtc 	vp->type = xp - vp->val.s; /* offset to value */
    557      1.1  jtc 	memcpy(xp, val, vallen);
    558      1.1  jtc 	if (op != NULL)
    559      1.1  jtc 		afree((void*)op, vp->areap);
    560      1.1  jtc }
    561      1.1  jtc 
    562      1.1  jtc /*
    563      1.1  jtc  * lookup variable (according to (set&LOCAL)),
    564      1.1  jtc  * set its attributes (INTEGER, RDONLY, EXPORT, TRACE, LJUST, RJUST, ZEROFIL,
    565      1.1  jtc  * LCASEV, UCASEV_AL), and optionally set its value if an assignment.
    566      1.1  jtc  */
    567      1.1  jtc struct tbl *
    568      1.1  jtc typeset(var, set, clr, field, base)
    569      1.1  jtc 	register const char *var;
    570      1.1  jtc 	Tflag clr, set;
    571      1.1  jtc 	int field, base;
    572      1.1  jtc {
    573      1.1  jtc 	register struct tbl *vp;
    574      1.1  jtc 	struct tbl *vpbase, *t;
    575      1.1  jtc 	char *tvar;
    576      1.1  jtc 	const char *val;
    577      1.1  jtc 
    578      1.1  jtc 	/* check for valid variable name, search for value */
    579      1.1  jtc 	val = skip_varname(var, FALSE);
    580      1.1  jtc 	if (val == var)
    581      1.1  jtc 		return NULL;
    582      1.1  jtc 	if (*val == '[') {
    583      1.1  jtc 		int len;
    584      1.1  jtc 
    585      1.1  jtc 		len = array_ref_len(val);
    586      1.1  jtc 		if (len == 0)
    587      1.1  jtc 			return NULL;
    588      1.1  jtc 		/* IMPORT is only used when the shell starts up and is
    589      1.1  jtc 		 * setting up its environment.  Allow only simple array
    590      1.1  jtc 		 * references at this time since parameter/command substitution
    591      1.1  jtc 		 * is preformed on the [expression], which would be a major
    592      1.1  jtc 		 * security hole.
    593      1.1  jtc 		 */
    594      1.1  jtc 		if (set & IMPORT) {
    595      1.1  jtc 			int i;
    596      1.1  jtc 			for (i = 1; i < len - 1; i++)
    597      1.1  jtc 				if (!digit(val[i]))
    598      1.1  jtc 					return NULL;
    599      1.1  jtc 		}
    600      1.1  jtc 		val += len;
    601      1.1  jtc 	}
    602      1.1  jtc 	if (*val == '=')
    603      1.1  jtc 		tvar = str_nsave(var, val++ - var, ATEMP);
    604      1.1  jtc 	else {
    605      1.1  jtc 		/* Importing from original envirnment: must have an = */
    606      1.1  jtc 		if (set & IMPORT)
    607      1.1  jtc 			return NULL;
    608      1.1  jtc 		tvar = (char *) var;
    609      1.1  jtc 		val = NULL;
    610      1.1  jtc 	}
    611      1.1  jtc 
    612      1.1  jtc 	/* Prevent typeset from creating a local PATH/ENV/SHELL */
    613      1.1  jtc 	if (Flag(FRESTRICTED) && (strcmp(tvar, "PATH") == 0
    614      1.1  jtc 				  || strcmp(tvar, "ENV") == 0
    615      1.1  jtc 				  || strcmp(tvar, "SHELL") == 0))
    616      1.1  jtc 		errorf("%s: restricted", tvar);
    617      1.1  jtc 
    618      1.1  jtc 	vp = (set&LOCAL) ? local(tvar, (set & LOCAL_COPY) ? TRUE : FALSE)
    619      1.1  jtc 		: global(tvar);
    620      1.1  jtc 	set &= ~(LOCAL|LOCAL_COPY);
    621      1.1  jtc 
    622      1.1  jtc 	vpbase = (vp->flag & ARRAY) ? global(arrayname(var)) : vp;
    623      1.1  jtc 
    624      1.1  jtc 	/* only allow export flag to be set.  at&t ksh allows any attribute to
    625      1.1  jtc 	 * be changed, which means it can be truncated or modified
    626      1.1  jtc 	 * (-L/-R/-Z/-i).
    627      1.1  jtc 	 */
    628      1.1  jtc 	if ((vpbase->flag&RDONLY)
    629      1.1  jtc 	    && (val || clr || (set & ~EXPORT)))
    630      1.1  jtc 		/* XXX check calls - is error here ok by POSIX? */
    631      1.1  jtc 		errorf("%s: is read only", tvar);
    632      1.1  jtc 	if (val)
    633      1.1  jtc 		afree(tvar, ATEMP);
    634      1.1  jtc 
    635      1.1  jtc 	/* most calls are with set/clr == 0 */
    636      1.1  jtc 	if (set | clr) {
    637      1.1  jtc 		/* XXX if x[0] isn't set, there will be problems: need to have
    638      1.1  jtc 		 * one copy of attributes for arrays...
    639      1.1  jtc 		 */
    640      1.1  jtc 		for (t = vpbase; t; t = t->u.array) {
    641      1.1  jtc 			int fake_assign;
    642      1.1  jtc 			char UNINITIALIZED(*s);
    643      1.1  jtc 			char UNINITIALIZED(*free_me);
    644      1.1  jtc 
    645      1.1  jtc 			fake_assign = (t->flag & ISSET) && (!val || t != vp)
    646      1.1  jtc 				      && ((set & (UCASEV_AL|LCASEV|LJUST|RJUST|ZEROFIL))
    647      1.1  jtc 					  || ((t->flag & INTEGER) && (clr & INTEGER))
    648      1.1  jtc 					  || (!(t->flag & INTEGER) && (set & INTEGER)));
    649      1.1  jtc 			if (fake_assign) {
    650      1.1  jtc 				if (t->flag & INTEGER) {
    651      1.1  jtc 					s = str_val(t);
    652      1.1  jtc 					free_me = (char *) 0;
    653      1.1  jtc 				} else {
    654      1.1  jtc 					s = t->val.s + t->type;
    655      1.1  jtc 					free_me = (t->flag & ALLOC) ? t->val.s
    656      1.1  jtc 								  : (char *) 0;
    657      1.1  jtc 				}
    658      1.1  jtc 				t->flag &= ~ALLOC;
    659      1.1  jtc 			}
    660      1.1  jtc 			if (!(t->flag & INTEGER) && (set & INTEGER)) {
    661      1.1  jtc 				t->type = 0;
    662      1.1  jtc 				t->flag &= ~ALLOC;
    663      1.1  jtc 			}
    664      1.1  jtc 			t->flag = (t->flag | set) & ~clr;
    665      1.1  jtc 			/* Don't change base if assignment is to be done,
    666      1.1  jtc 			 * in case assignment fails.
    667      1.1  jtc 			 */
    668      1.1  jtc 			if ((set & INTEGER) && base > 0 && (!val || t != vp))
    669      1.1  jtc 				t->type = base;
    670      1.1  jtc 			if (set & (LJUST|RJUST|ZEROFIL))
    671      1.1  jtc 				t->u2.field = field;
    672      1.1  jtc 			if (fake_assign) {
    673      1.1  jtc 				setstr(t, s);
    674      1.1  jtc 				if (free_me)
    675      1.1  jtc 					afree((void *) free_me, t->areap);
    676      1.1  jtc 			}
    677      1.1  jtc 		}
    678      1.1  jtc 	}
    679      1.1  jtc 
    680      1.1  jtc 	if (val != NULL) {
    681      1.1  jtc 		if (vp->flag&INTEGER) {
    682      1.1  jtc 			/* do not zero base before assignment */
    683      1.1  jtc 			setstr(vp, val);
    684      1.1  jtc 			/* Done after assignment to override default */
    685      1.1  jtc 			if (base > 0)
    686      1.1  jtc 				vp->type = base;
    687      1.1  jtc 		} else
    688      1.1  jtc 			setstr(vp, val);
    689      1.1  jtc 	}
    690      1.1  jtc 
    691      1.1  jtc 	/* only x[0] is ever exported, so use vpbase */
    692      1.1  jtc 	if ((vpbase->flag&EXPORT) && !(vpbase->flag&INTEGER)
    693      1.1  jtc 	    && vpbase->type == 0)
    694      1.1  jtc 		export(vpbase, (vpbase->flag&ISSET) ? vpbase->val.s : null);
    695      1.1  jtc 
    696      1.1  jtc 	return vp;
    697      1.1  jtc }
    698      1.1  jtc 
    699      1.1  jtc /* Unset a variable.  array_ref is set if there was an array reference in
    700      1.1  jtc  * the name lookup (eg, x[2]).
    701      1.1  jtc  */
    702      1.1  jtc void
    703      1.1  jtc unset(vp, array_ref)
    704      1.1  jtc 	register struct tbl *vp;
    705      1.1  jtc 	int array_ref;
    706      1.1  jtc {
    707      1.1  jtc 	if (vp->flag & ALLOC)
    708      1.1  jtc 		afree((void*)vp->val.s, vp->areap);
    709      1.1  jtc 	if ((vp->flag & ARRAY) && !array_ref) {
    710      1.1  jtc 		struct tbl *a, *tmp;
    711      1.1  jtc 
    712      1.1  jtc 		/* Free up entire array */
    713      1.1  jtc 		for (a = vp->u.array; a; ) {
    714      1.1  jtc 			tmp = a;
    715      1.1  jtc 			a = a->u.array;
    716      1.1  jtc 			if (tmp->flag & ALLOC)
    717      1.1  jtc 				afree((void *) tmp->val.s, tmp->areap);
    718      1.1  jtc 			afree(tmp, tmp->areap);
    719      1.1  jtc 		}
    720      1.1  jtc 		vp->u.array = (struct tbl *) 0;
    721      1.1  jtc 	}
    722      1.1  jtc 	/* If foo[0] is being unset, the remainder of the array is kept... */
    723      1.1  jtc 	vp->flag &= SPECIAL | (array_ref ? ARRAY|DEFINED : 0);
    724      1.1  jtc 	if (vp->flag & SPECIAL)
    725      1.1  jtc 		unsetspec(vp);	/* responsible for `unspecial'ing var */
    726      1.1  jtc }
    727      1.1  jtc 
    728      1.1  jtc /* return a pointer to the first char past a legal variable name (returns the
    729      1.1  jtc  * argument if there is no legal name, returns * a pointer to the terminating
    730      1.1  jtc  * null if whole string is legal).
    731      1.1  jtc  */
    732      1.1  jtc char *
    733      1.1  jtc skip_varname(s, aok)
    734      1.1  jtc 	const char *s;
    735      1.1  jtc 	int aok;
    736      1.1  jtc {
    737      1.1  jtc 	int alen;
    738      1.1  jtc 
    739      1.1  jtc 	if (s && letter(*s)) {
    740      1.1  jtc 		while (*++s && letnum(*s))
    741      1.1  jtc 			;
    742      1.1  jtc 		if (aok && *s == '[' && (alen = array_ref_len(s)))
    743      1.1  jtc 			s += alen;
    744      1.1  jtc 	}
    745      1.1  jtc 	return (char *) s;
    746      1.1  jtc }
    747      1.1  jtc 
    748      1.1  jtc /* Return a pointer to the first character past any legal variable name.  */
    749      1.1  jtc char *
    750      1.1  jtc skip_wdvarname(s, aok)
    751      1.1  jtc 	const char *s;
    752      1.1  jtc 	int aok;	/* skip array de-reference? */
    753      1.1  jtc {
    754      1.1  jtc 	if (s[0] == CHAR && letter(s[1])) {
    755      1.1  jtc 		do
    756      1.1  jtc 			s += 2;
    757      1.1  jtc 		while (s[0] == CHAR && letnum(s[1]));
    758  1.1.1.2  jtc 		if (aok && s[0] == CHAR && s[1] == '[') {
    759      1.1  jtc 			/* skip possible array de-reference */
    760      1.1  jtc 			const char *p = s;
    761      1.1  jtc 			char c;
    762      1.1  jtc 			int depth = 0;
    763      1.1  jtc 
    764      1.1  jtc 			while (1) {
    765      1.1  jtc 				if (p[0] != CHAR)
    766      1.1  jtc 					break;
    767      1.1  jtc 				c = p[1];
    768      1.1  jtc 				p += 2;
    769      1.1  jtc 				if (c == '[')
    770      1.1  jtc 					depth++;
    771      1.1  jtc 				else if (c == ']' && --depth == 0) {
    772      1.1  jtc 					s = p;
    773      1.1  jtc 					break;
    774      1.1  jtc 				}
    775      1.1  jtc 			}
    776      1.1  jtc 		}
    777      1.1  jtc 	}
    778      1.1  jtc 	return (char *) s;
    779      1.1  jtc }
    780      1.1  jtc 
    781      1.1  jtc /* Check if coded string s is a variable name */
    782      1.1  jtc int
    783      1.1  jtc is_wdvarname(s, aok)
    784      1.1  jtc 	const char *s;
    785      1.1  jtc 	int aok;
    786      1.1  jtc {
    787      1.1  jtc 	char *p = skip_wdvarname(s, aok);
    788      1.1  jtc 
    789      1.1  jtc 	return p != s && p[0] == EOS;
    790      1.1  jtc }
    791      1.1  jtc 
    792      1.1  jtc /* Check if coded string s is a variable assignment */
    793      1.1  jtc int
    794      1.1  jtc is_wdvarassign(s)
    795      1.1  jtc 	const char *s;
    796      1.1  jtc {
    797      1.1  jtc 	char *p = skip_wdvarname(s, TRUE);
    798      1.1  jtc 
    799      1.1  jtc 	return p != s && p[0] == CHAR && p[1] == '=';
    800      1.1  jtc }
    801      1.1  jtc 
    802      1.1  jtc /*
    803      1.1  jtc  * Make the exported environment from the exported names in the dictionary.
    804      1.1  jtc  */
    805      1.1  jtc char **
    806      1.1  jtc makenv()
    807      1.1  jtc {
    808      1.1  jtc 	struct block *l = e->loc;
    809      1.1  jtc 	XPtrV env;
    810      1.1  jtc 	register struct tbl *vp, **vpp;
    811      1.1  jtc 	register int i;
    812      1.1  jtc 
    813      1.1  jtc 	XPinit(env, 64);
    814      1.1  jtc 	for (l = e->loc; l != NULL; l = l->next)
    815      1.1  jtc 		for (vpp = l->vars.tbls, i = l->vars.size; --i >= 0; )
    816      1.1  jtc 			if ((vp = *vpp++) != NULL
    817      1.1  jtc 			    && (vp->flag&(ISSET|EXPORT)) == (ISSET|EXPORT)) {
    818      1.1  jtc 				register struct block *l2;
    819      1.1  jtc 				register struct tbl *vp2;
    820      1.1  jtc 				unsigned h = hash(vp->name);
    821      1.1  jtc 
    822      1.1  jtc 				/* unexport any redefined instances */
    823      1.1  jtc 				for (l2 = l->next; l2 != NULL; l2 = l2->next) {
    824      1.1  jtc 					vp2 = tsearch(&l2->vars, vp->name, h);
    825      1.1  jtc 					if (vp2 != NULL)
    826      1.1  jtc 						vp2->flag &= ~EXPORT;
    827      1.1  jtc 				}
    828      1.1  jtc 				if ((vp->flag&INTEGER)) {
    829      1.1  jtc 					/* integer to string */
    830      1.1  jtc 					char *val;
    831      1.1  jtc 					val = str_val(vp);
    832      1.1  jtc 					vp->flag &= ~INTEGER;
    833      1.1  jtc 					setstr(vp, val);
    834      1.1  jtc 				}
    835      1.1  jtc 				XPput(env, vp->val.s);
    836      1.1  jtc 			}
    837      1.1  jtc 	XPput(env, NULL);
    838      1.1  jtc 	return (char **) XPclose(env);
    839      1.1  jtc }
    840      1.1  jtc 
    841      1.1  jtc /*
    842      1.1  jtc  * handle special variables with side effects - PATH, SECONDS.
    843      1.1  jtc  */
    844      1.1  jtc 
    845      1.1  jtc /* Test if name is a special parameter */
    846      1.1  jtc static int
    847      1.1  jtc special(name)
    848      1.1  jtc 	register const char * name;
    849      1.1  jtc {
    850      1.1  jtc 	register struct tbl *tp;
    851      1.1  jtc 
    852      1.1  jtc 	tp = tsearch(&specials, name, hash(name));
    853      1.1  jtc 	return tp ? tp->type : V_NONE;
    854      1.1  jtc }
    855      1.1  jtc 
    856      1.1  jtc #ifdef KSH
    857      1.1  jtc static	time_t	seconds;		/* time SECONDS last set */
    858      1.1  jtc #endif /* KSH */
    859      1.1  jtc 
    860      1.1  jtc static void
    861      1.1  jtc getspec(vp)
    862      1.1  jtc 	register struct tbl *vp;
    863      1.1  jtc {
    864      1.1  jtc 	switch (special(vp->name)) {
    865      1.1  jtc #ifdef KSH
    866      1.1  jtc 	  case V_SECONDS:
    867      1.1  jtc 		vp->flag &= ~SPECIAL;
    868      1.1  jtc 		setint(vp, (long) (time((time_t *)0) - seconds));
    869      1.1  jtc 		vp->flag |= SPECIAL;
    870      1.1  jtc 		break;
    871      1.1  jtc 	  case V_RANDOM:
    872      1.1  jtc 		vp->flag &= ~SPECIAL;
    873      1.1  jtc 		setint(vp, (long) (rand() & 0x7fff));
    874      1.1  jtc 		vp->flag |= SPECIAL;
    875      1.1  jtc 		break;
    876      1.1  jtc #endif /* KSH */
    877      1.1  jtc #ifdef HISTORY
    878      1.1  jtc 	  case V_HISTSIZE:
    879      1.1  jtc 		vp->flag &= ~SPECIAL;
    880      1.1  jtc 		setint(vp, (long) histsize);
    881      1.1  jtc 		vp->flag |= SPECIAL;
    882      1.1  jtc 		break;
    883      1.1  jtc #endif /* HISTORY */
    884      1.1  jtc 	}
    885      1.1  jtc }
    886      1.1  jtc 
    887      1.1  jtc static void
    888      1.1  jtc setspec(vp)
    889      1.1  jtc 	register struct tbl *vp;
    890      1.1  jtc {
    891      1.1  jtc 	char *s;
    892      1.1  jtc 
    893      1.1  jtc 	switch (special(vp->name)) {
    894      1.1  jtc 	  case V_PATH:
    895      1.1  jtc 		path = str_val(vp);
    896      1.1  jtc 		flushcom(1);	/* clear tracked aliases */
    897      1.1  jtc 		break;
    898      1.1  jtc 	  case V_IFS:
    899      1.1  jtc 		setctypes(s = str_val(vp), C_IFS);
    900      1.1  jtc 		ifs0 = *s;
    901      1.1  jtc 		break;
    902      1.1  jtc 	  case V_OPTIND:
    903      1.1  jtc 		getopts_reset((int) intval(vp));
    904      1.1  jtc 		break;
    905      1.1  jtc 	  case V_POSIXLY_CORRECT:
    906      1.1  jtc 		change_flag(FPOSIX, OF_SPECIAL, 1);
    907      1.1  jtc 		break;
    908      1.1  jtc 	  case V_TMPDIR:
    909      1.1  jtc 		if (tmpdir) {
    910      1.1  jtc 			afree(tmpdir, APERM);
    911      1.1  jtc 			tmpdir = (char *) 0;
    912      1.1  jtc 		}
    913      1.1  jtc 		/* Use tmpdir iff it is an absolute path, is writable and
    914      1.1  jtc 		 * searchable and is a directory...
    915      1.1  jtc 		 */
    916      1.1  jtc 		{
    917      1.1  jtc 			struct stat statb;
    918      1.1  jtc 			s = str_val(vp);
    919      1.1  jtc 			if (ISABSPATH(s) && eaccess(s, W_OK|X_OK) == 0
    920      1.1  jtc 			    && stat(s, &statb) == 0 && S_ISDIR(statb.st_mode))
    921      1.1  jtc 				tmpdir = str_save(s, APERM);
    922      1.1  jtc 		}
    923      1.1  jtc 		break;
    924      1.1  jtc #ifdef HISTORY
    925      1.1  jtc 	  case V_HISTSIZE:
    926      1.1  jtc 		vp->flag &= ~SPECIAL;
    927      1.1  jtc 		sethistsize((int) intval(vp));
    928      1.1  jtc 		vp->flag |= SPECIAL;
    929      1.1  jtc 		break;
    930      1.1  jtc 	  case V_HISTFILE:
    931      1.1  jtc 		sethistfile(str_val(vp));
    932      1.1  jtc 		break;
    933      1.1  jtc #endif /* HISTORY */
    934      1.1  jtc #ifdef EDIT
    935      1.1  jtc 	  case V_VISUAL:
    936      1.1  jtc 		set_editmode(str_val(vp));
    937      1.1  jtc 		break;
    938      1.1  jtc 	  case V_EDITOR:
    939      1.1  jtc 		if (!(global("VISUAL")->flag & ISSET))
    940      1.1  jtc 			set_editmode(str_val(vp));
    941      1.1  jtc 		break;
    942      1.1  jtc 	  case V_COLUMNS:
    943      1.1  jtc 		if ((x_cols = intval(vp)) <= MIN_COLS)
    944      1.1  jtc 			x_cols = MIN_COLS;
    945      1.1  jtc 		break;
    946      1.1  jtc #endif /* EDIT */
    947      1.1  jtc #ifdef KSH
    948      1.1  jtc 	  case V_MAIL:
    949      1.1  jtc 		mbset(str_val(vp));
    950      1.1  jtc 		break;
    951      1.1  jtc 	  case V_MAILPATH:
    952      1.1  jtc 		mpset(str_val(vp));
    953      1.1  jtc 		break;
    954      1.1  jtc 	  case V_MAILCHECK:
    955      1.1  jtc 		/* mail_check_set(intval(vp)); */
    956      1.1  jtc 		break;
    957      1.1  jtc 	  case V_RANDOM:
    958      1.1  jtc 		vp->flag &= ~SPECIAL;
    959      1.1  jtc 		srand((unsigned int)intval(vp));
    960      1.1  jtc 		vp->flag |= SPECIAL;
    961      1.1  jtc 		break;
    962      1.1  jtc 	  case V_SECONDS:
    963      1.1  jtc 		vp->flag &= ~SPECIAL;
    964      1.1  jtc 		seconds = time((time_t*) 0) - intval(vp);
    965      1.1  jtc 		vp->flag |= SPECIAL;
    966      1.1  jtc 		break;
    967      1.1  jtc 	  case V_TMOUT:
    968      1.1  jtc 		/* at&t ksh seems to do this (only listen if integer) */
    969      1.1  jtc 		if (vp->flag & INTEGER)
    970      1.1  jtc 			ksh_tmout = vp->val.i >= 0 ? vp->val.i : 0;
    971      1.1  jtc 		break;
    972      1.1  jtc #endif /* KSH */
    973      1.1  jtc 	}
    974      1.1  jtc }
    975      1.1  jtc 
    976      1.1  jtc static void
    977      1.1  jtc unsetspec(vp)
    978      1.1  jtc 	register struct tbl *vp;
    979      1.1  jtc {
    980      1.1  jtc 	switch (special(vp->name)) {
    981      1.1  jtc 	  case V_PATH:
    982      1.1  jtc 		path = def_path;
    983      1.1  jtc 		flushcom(1);	/* clear tracked aliases */
    984      1.1  jtc 		break;
    985      1.1  jtc 	  case V_IFS:
    986      1.1  jtc 		setctypes(" \t\n", C_IFS);
    987      1.1  jtc 		ifs0 = ' ';
    988      1.1  jtc 		break;
    989      1.1  jtc 	  case V_TMPDIR:
    990      1.1  jtc 		/* should not become unspecial */
    991      1.1  jtc 		if (tmpdir) {
    992      1.1  jtc 			afree(tmpdir, APERM);
    993      1.1  jtc 			tmpdir = (char *) 0;
    994      1.1  jtc 		}
    995      1.1  jtc 		break;
    996      1.1  jtc #ifdef KSH
    997      1.1  jtc 	  case V_MAIL:
    998      1.1  jtc 		mbset((char *) 0);
    999      1.1  jtc 		break;
   1000      1.1  jtc 	  case V_MAILPATH:
   1001      1.1  jtc 		mpset((char *) 0);
   1002      1.1  jtc 		break;
   1003      1.1  jtc 	  case V_TMOUT:
   1004      1.1  jtc 		/* at&t ksh doesn't do this. TMOUT becomes unspecial so
   1005      1.1  jtc 		 * future assignments don't have effect.  Could be
   1006      1.1  jtc 		 * useful (eg, after "TMOUT=60; unset TMOUT", user
   1007      1.1  jtc 		 * can't get rid of the timeout...).  Should be handled
   1008      1.1  jtc 		 * by generic unset code...
   1009      1.1  jtc 		 */
   1010      1.1  jtc 		ksh_tmout = 0;
   1011      1.1  jtc 		break;
   1012      1.1  jtc #endif /* KSH */
   1013      1.1  jtc 	  /* todo: generic action for specials (at&t says variables
   1014  1.1.1.2  jtc 	   * lose their special meaning when unset but global() checks
   1015      1.1  jtc 	   * the name of new vars to see if they are special)
   1016  1.1.1.2  jtc 	   * 	lose meaning: _, ERRNO, LINENO, MAILCHECK,
   1017      1.1  jtc 	   *		OPTARG, OPTIND, RANDOM, SECONDS, TMOUT.
   1018      1.1  jtc 	   *	unknown: MAIL, MAILPATH, HISTSIZE, HISTFILE,
   1019      1.1  jtc 	   *    no effect: IFS, COLUMNS, PATH, TMPDIR,
   1020      1.1  jtc 	   *		VISUAL, EDITOR,
   1021      1.1  jtc 	   *		POSIXLY_CORRECT (use set +o posix instead)
   1022      1.1  jtc 	   */
   1023      1.1  jtc 	}
   1024      1.1  jtc }
   1025      1.1  jtc 
   1026      1.1  jtc /*
   1027      1.1  jtc  * Search for (and possibly create) a table entry starting with
   1028      1.1  jtc  * vp, indexed by val.
   1029      1.1  jtc  */
   1030      1.1  jtc static struct tbl *
   1031      1.1  jtc arraysearch(vp, val)
   1032      1.1  jtc 	struct tbl *vp;
   1033      1.1  jtc 	int val;
   1034      1.1  jtc {
   1035      1.1  jtc 	struct tbl *prev, *curr, *new;
   1036      1.1  jtc 
   1037      1.1  jtc 	vp->flag |= ARRAY|DEFINED;
   1038      1.1  jtc 
   1039      1.1  jtc 	/* The table entry is always [0] */
   1040      1.1  jtc 	if (val == 0) {
   1041      1.1  jtc 		vp->index = 0;
   1042      1.1  jtc 		return vp;
   1043      1.1  jtc 	}
   1044      1.1  jtc 	prev = vp;
   1045      1.1  jtc 	curr = vp->u.array;
   1046      1.1  jtc 	while (curr && curr->index < val) {
   1047      1.1  jtc 		prev = curr;
   1048      1.1  jtc 		curr = curr->u.array;
   1049      1.1  jtc 	}
   1050      1.1  jtc 	if (curr && curr->index == val) {
   1051      1.1  jtc 		if (curr->flag&ISSET)
   1052      1.1  jtc 			return curr;
   1053      1.1  jtc 		else
   1054      1.1  jtc 			new = curr;
   1055      1.1  jtc 	} else
   1056      1.1  jtc 		new = (struct tbl *)alloc(sizeof(struct tbl)+strlen(vp->name)+1, vp->areap);
   1057      1.1  jtc 	strcpy(new->name, vp->name);
   1058      1.1  jtc 	new->flag = vp->flag & ~(ALLOC|DEFINED|ISSET|SPECIAL);
   1059      1.1  jtc 	new->type = vp->type;
   1060      1.1  jtc 	new->areap = vp->areap;
   1061      1.1  jtc 	new->u2.field = vp->u2.field;
   1062      1.1  jtc 	new->index = val;
   1063      1.1  jtc 	if (curr != new) {		/* not reusing old array entry */
   1064      1.1  jtc 		prev->u.array = new;
   1065      1.1  jtc 		new->u.array = curr;
   1066      1.1  jtc 	}
   1067      1.1  jtc 	return new;
   1068      1.1  jtc }
   1069      1.1  jtc 
   1070      1.1  jtc /* Return the length of an array reference (eg, [1+2]) - cp is assumed
   1071      1.1  jtc  * to point to the open bracket.  Returns 0 if there is no matching closing
   1072      1.1  jtc  * bracket.
   1073      1.1  jtc  */
   1074      1.1  jtc int
   1075      1.1  jtc array_ref_len(cp)
   1076      1.1  jtc 	const char *cp;
   1077      1.1  jtc {
   1078      1.1  jtc 	const char *s = cp;
   1079      1.1  jtc 	int c;
   1080      1.1  jtc 	int depth = 0;
   1081      1.1  jtc 
   1082      1.1  jtc 	while ((c = *s++) && (c != ']' || --depth))
   1083      1.1  jtc 		if (c == '[')
   1084      1.1  jtc 			depth++;
   1085      1.1  jtc 	if (!c)
   1086      1.1  jtc 		return 0;
   1087      1.1  jtc 	return s - cp;
   1088      1.1  jtc }
   1089      1.1  jtc 
   1090      1.1  jtc /*
   1091      1.1  jtc  * Make a copy of the base of an array name
   1092      1.1  jtc  */
   1093      1.1  jtc char *
   1094      1.1  jtc arrayname(str)
   1095      1.1  jtc 	const char *str;
   1096      1.1  jtc {
   1097      1.1  jtc 	const char *p;
   1098      1.1  jtc 
   1099      1.1  jtc 	if ((p = strchr(str, '[')) == 0)
   1100      1.1  jtc 		/* Shouldn't happen, but why worry? */
   1101      1.1  jtc 		return (char *) str;
   1102      1.1  jtc 
   1103      1.1  jtc 	return str_nsave(str, p - str, ATEMP);
   1104      1.1  jtc }
   1105      1.1  jtc 
   1106      1.1  jtc /* Set (or overwrite, if !reset) the array variable var to the values in vals.
   1107      1.1  jtc  */
   1108      1.1  jtc void
   1109      1.1  jtc set_array(var, reset, vals)
   1110      1.1  jtc 	const char *var;
   1111      1.1  jtc 	int reset;
   1112      1.1  jtc 	char **vals;
   1113      1.1  jtc {
   1114      1.1  jtc 	struct tbl *vp, *vq;
   1115      1.1  jtc 	int i;
   1116      1.1  jtc 
   1117      1.1  jtc 	/* to get local array, use "typeset foo; set -A foo" */
   1118      1.1  jtc 	vp = global(var);
   1119      1.1  jtc 
   1120      1.1  jtc 	/* Note: at&t ksh allows set -A but not set +A of a read-only var */
   1121      1.1  jtc 	if ((vp->flag&RDONLY))
   1122      1.1  jtc 		errorf("%s: is read only", var);
   1123      1.1  jtc 	/* This code is quite non-optimal */
   1124      1.1  jtc 	if (reset > 0)
   1125      1.1  jtc 		/* trash existing values and attributes */
   1126      1.1  jtc 		unset(vp, 0);
   1127      1.1  jtc 	/* todo: would be nice for assignment to completely succeed or
   1128      1.1  jtc 	 * completely fail.  Only really effects integer arrays:
   1129      1.1  jtc 	 * evaluation of some of vals[] may fail...
   1130      1.1  jtc 	 */
   1131      1.1  jtc 	for (i = 0; vals[i]; i++) {
   1132      1.1  jtc 		vq = arraysearch(vp, i);
   1133      1.1  jtc 		setstr(vq, vals[i]);
   1134      1.1  jtc 	}
   1135      1.1  jtc }
   1136