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