* reordered a little bit
[mascara-docs.git] / i86 / elks / elkscmd / ash / var.c
blob36ddaf78b12f778ed29ff251e83abed4b2b60e79
1 /*-
2 * Copyright (c) 1991 The Regents of the University of California.
3 * All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
37 #ifndef lint
38 static char sccsid[] = "@(#)var.c 5.3 (Berkeley) 4/12/91";
39 #endif /* not lint */
42 * Shell variables.
45 #include "shell.h"
46 #include "output.h"
47 #include "expand.h"
48 #include "nodes.h" /* for other headers */
49 #include "eval.h" /* defines cmdenviron */
50 #include "exec.h"
51 #include "syntax.h"
52 #include "options.h"
53 #include "mail.h"
54 #include "var.h"
55 #include "memalloc.h"
56 #include "error.h"
57 #include "mystring.h"
60 #define VTABSIZE 39
63 struct varinit {
64 struct var *var;
65 int flags;
66 char *text;
70 #if ATTY
71 struct var vatty;
72 #endif
73 struct var vifs;
74 struct var vmail;
75 struct var vmpath;
76 struct var vpath;
77 struct var vps1;
78 struct var vps2;
79 struct var vpse;
80 struct var vvers;
81 #if ATTY
82 struct var vterm;
83 #endif
85 const struct varinit varinit[] = {
86 #if ATTY
87 {&vatty, VSTRFIXED|VTEXTFIXED|VUNSET, "ATTY="},
88 #endif
89 {&vifs, VSTRFIXED|VTEXTFIXED, "IFS= \t\n"},
90 {&vmail, VSTRFIXED|VTEXTFIXED|VUNSET, "MAIL="},
91 {&vmpath, VSTRFIXED|VTEXTFIXED|VUNSET, "MAILPATH="},
92 {&vpath, VSTRFIXED|VTEXTFIXED, "PATH=:/bin:/usr/bin"},
93 /*
94 * vps1 depends on uid
96 {&vps2, VSTRFIXED|VTEXTFIXED, "PS2=> "},
97 {&vpse, VSTRFIXED|VTEXTFIXED|VUNSET, "PSE="},
98 #if ATTY
99 {&vterm, VSTRFIXED|VTEXTFIXED|VUNSET, "TERM="},
100 #endif
101 {NULL, 0, NULL}
104 struct var *vartab[VTABSIZE];
106 STATIC void unsetvar __P((char *));
107 STATIC struct var **hashvar __P((char *));
108 STATIC int varequal __P((char *, char *));
111 * Initialize the varable symbol tables and import the environment
114 #ifdef mkinit
115 INCLUDE "var.h"
116 INIT {
117 char **envp;
118 extern char **environ;
120 initvar();
121 for (envp = environ ; *envp ; envp++) {
122 if (strchr(*envp, '=')) {
123 setvareq(*envp, VEXPORT|VTEXTFIXED);
127 #endif
131 * This routine initializes the builtin variables. It is called when the
132 * shell is initialized and again when a shell procedure is spawned.
135 void
136 initvar() {
137 register const struct varinit *ip;
138 register struct var *vp;
139 struct var **vpp;
141 for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
142 if ((vp->flags & VEXPORT) == 0) {
143 vpp = hashvar(ip->text);
144 vp->next = *vpp;
145 *vpp = vp;
146 vp->text = ip->text;
147 vp->flags = ip->flags;
151 * PS1 depends on uid
153 if ((vps1.flags & VEXPORT) == 0) {
154 vpp = hashvar("PS1=");
155 vps1.next = *vpp;
156 *vpp = &vps1;
157 vps1.text = getuid() ? "PS1=$ " : "PS1=# ";
158 vps1.flags = VSTRFIXED|VTEXTFIXED;
163 * Set the value of a variable. The flags argument is ored with the
164 * flags of the variable. If val is NULL, the variable is unset.
167 void
168 setvar(name, val, flags)
169 char *name;
170 char *val;
172 register char *p;
173 register char *q;
174 int len;
175 int namelen;
176 char *nameeq;
177 int isbad;
179 isbad = 0;
180 p = name;
181 if (! is_name(*p++))
182 isbad = 1;
183 for (;;) {
184 if (! is_in_name(*p)) {
185 if (*p == '\0' || *p == '=')
186 break;
187 isbad = 1;
189 p++;
191 namelen = p - name;
192 if (isbad)
193 error("%.*s: is read only", namelen, name);
194 len = namelen + 2; /* 2 is space for '=' and '\0' */
195 if (val == NULL) {
196 flags |= VUNSET;
197 } else {
198 len += strlen(val);
200 p = nameeq = ckmalloc(len);
201 q = name;
202 while (--namelen >= 0)
203 *p++ = *q++;
204 *p++ = '=';
205 *p = '\0';
206 if (val)
207 scopy(val, p);
208 setvareq(nameeq, flags);
214 * Same as setvar except that the variable and value are passed in
215 * the first argument as name=value. Since the first argument will
216 * be actually stored in the table, it should not be a string that
217 * will go away.
220 void
221 setvareq(s, flags)
222 char *s;
224 register struct var *vp;
225 register struct var **vpp;
227 vpp = hashvar(s);
228 for (vp = *vpp ; vp ; vp = vp->next) {
229 if (varequal(s, vp->text)) {
230 if (vp->flags & VREADONLY) {
231 int len = strchr(s, '=') - s;
232 error("%.*s: is read only", len, s);
234 INTOFF;
235 if (vp == &vpath)
236 changepath(s + 5); /* 5 = strlen("PATH=") */
237 if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
238 ckfree(vp->text);
239 vp->flags &=~ (VTEXTFIXED|VSTACK|VUNSET);
240 vp->flags |= flags;
241 vp->text = s;
242 #ifndef _SMALL_
243 if (vp == &vmpath || (vp == &vmail && ! mpathset()))
244 chkmail(1);
245 #endif
246 INTON;
247 return;
250 /* not found */
251 vp = ckmalloc(sizeof (*vp));
252 vp->flags = flags;
253 vp->text = s;
254 vp->next = *vpp;
255 *vpp = vp;
261 * Process a linked list of variable assignments.
264 void
265 listsetvar(list)
266 struct strlist *list;
268 register struct strlist *lp;
270 INTOFF;
271 for (lp = list ; lp ; lp = lp->next) {
272 setvareq(savestr(lp->text), 0);
274 INTON;
280 * Find the value of a variable. Returns NULL if not set.
283 char *
284 lookupvar(name)
285 register char *name;
287 register struct var *v;
289 for (v = *hashvar(name) ; v ; v = v->next) {
290 if (varequal(v->text, name)) {
291 if (v->flags & VUNSET)
292 return NULL;
293 return strchr(v->text, '=') + 1;
296 return NULL;
302 * Search the environment of a builtin command. If the second argument
303 * is nonzero, return the value of a variable even if it hasn't been
304 * exported.
307 char *
308 bltinlookup(name, doall)
309 char *name;
311 register struct strlist *sp;
312 register struct var *v;
314 for (sp = cmdenviron ; sp ; sp = sp->next) {
315 if (varequal(sp->text, name))
316 return strchr(sp->text, '=') + 1;
318 for (v = *hashvar(name) ; v ; v = v->next) {
319 if (varequal(v->text, name)) {
320 if (v->flags & VUNSET
321 || ! doall && (v->flags & VEXPORT) == 0)
322 return NULL;
323 return strchr(v->text, '=') + 1;
326 return NULL;
332 * Generate a list of exported variables. This routine is used to construct
333 * the third argument to execve when executing a program.
336 char **
337 environment() {
338 int nenv;
339 register struct var **vpp;
340 register struct var *vp;
341 char **env;
342 char **ep;
344 nenv = 0;
345 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
346 for (vp = *vpp ; vp ; vp = vp->next)
347 if (vp->flags & VEXPORT)
348 nenv++;
350 ep = env = stalloc((nenv + 1) * sizeof *env);
351 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
352 for (vp = *vpp ; vp ; vp = vp->next)
353 if (vp->flags & VEXPORT)
354 *ep++ = vp->text;
356 *ep = NULL;
357 return env;
362 * Called when a shell procedure is invoked to clear out nonexported
363 * variables. It is also necessary to reallocate variables of with
364 * VSTACK set since these are currently allocated on the stack.
367 #ifdef mkinit
368 MKINIT void shprocvar();
370 SHELLPROC {
371 shprocvar();
373 #endif
375 void
376 shprocvar() {
377 register struct var **vpp;
378 register struct var *vp;
379 struct var **prev;
381 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
382 for (prev = vpp ; (vp = *prev) != NULL ; ) {
383 if ((vp->flags & VEXPORT) == 0) {
384 *prev = vp->next;
385 if ((vp->flags & VTEXTFIXED) == 0)
386 ckfree(vp->text);
387 if ((vp->flags & VSTRFIXED) == 0)
388 ckfree(vp);
389 } else {
390 if (vp->flags & VSTACK) {
391 vp->text = savestr(vp->text);
392 vp->flags &=~ VSTACK;
394 prev = &vp->next;
398 initvar();
404 * Command to list all variables which are set. Currently this command
405 * is invoked from the set command when the set command is called without
406 * any variables.
410 showvarscmd(argc, argv) char **argv; {
411 register struct var **vpp;
412 register struct var *vp;
414 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
415 for (vp = *vpp ; vp ; vp = vp->next) {
416 if ((vp->flags & VUNSET) == 0)
417 out1fmt("%s\n", vp->text);
420 return 0;
426 * The export and readonly commands.
430 exportcmd(argc, argv) char **argv; {
431 struct var **vpp;
432 register struct var *vp;
433 char *name;
434 register char *p;
435 int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
437 listsetvar(cmdenviron);
438 if (argc > 1) {
439 while ((name = *argptr++) != NULL) {
440 if ((p = strchr(name, '=')) != NULL) {
441 p++;
442 } else {
443 vpp = hashvar(name);
444 for (vp = *vpp ; vp ; vp = vp->next) {
445 if (varequal(vp->text, name)) {
446 vp->flags |= flag;
447 goto found;
451 setvar(name, p, flag);
452 found:;
454 } else {
455 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
456 for (vp = *vpp ; vp ; vp = vp->next) {
457 if (vp->flags & flag) {
458 for (p = vp->text ; *p != '=' ; p++)
459 out1c(*p);
460 out1c('\n');
465 return 0;
470 * The "local" command.
473 localcmd(argc, argv) char **argv; {
474 register char *name;
476 if (! in_function())
477 error("Not in a function");
478 while ((name = *argptr++) != NULL) {
479 mklocal(name);
481 return 0;
486 * Make a variable a local variable. When a variable is made local, it's
487 * value and flags are saved in a localvar structure. The saved values
488 * will be restored when the shell function returns. We handle the name
489 * "-" as a special case.
492 void
493 mklocal(name)
494 char *name;
496 register struct localvar *lvp;
497 struct var **vpp;
498 register struct var *vp;
500 INTOFF;
501 lvp = ckmalloc(sizeof (struct localvar));
502 if (name[0] == '-' && name[1] == '\0') {
503 lvp->text = ckmalloc(sizeof optval);
504 bcopy(optval, lvp->text, sizeof optval);
505 vp = NULL;
506 } else {
507 vpp = hashvar(name);
508 for (vp = *vpp ; vp && ! varequal(vp->text, name) ; vp = vp->next);
509 if (vp == NULL) {
510 if (strchr(name, '='))
511 setvareq(savestr(name), VSTRFIXED);
512 else
513 setvar(name, NULL, VSTRFIXED);
514 vp = *vpp; /* the new variable */
515 lvp->text = NULL;
516 lvp->flags = VUNSET;
517 } else {
518 lvp->text = vp->text;
519 lvp->flags = vp->flags;
520 vp->flags |= VSTRFIXED|VTEXTFIXED;
521 if (strchr(name, '='))
522 setvareq(savestr(name), 0);
525 lvp->vp = vp;
526 lvp->next = localvars;
527 localvars = lvp;
528 INTON;
533 * Called after a function returns.
536 void
537 poplocalvars() {
538 register struct localvar *lvp;
539 register struct var *vp;
541 while ((lvp = localvars) != NULL) {
542 localvars = lvp->next;
543 vp = lvp->vp;
544 if (vp == NULL) { /* $- saved */
545 bcopy(lvp->text, optval, sizeof optval);
546 ckfree(lvp->text);
547 } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
548 unsetvar(vp->text);
549 } else {
550 if ((vp->flags & VTEXTFIXED) == 0)
551 ckfree(vp->text);
552 vp->flags = lvp->flags;
553 vp->text = lvp->text;
555 ckfree(lvp);
560 setvarcmd(argc, argv) register char **argv; {
561 if (argc <= 2)
562 return unsetcmd(argc, argv);
563 else if (argc == 3)
564 setvar(argv[1], argv[2], 0);
565 else
566 error("List assignment not implemented");
567 return 0;
572 * The unset builtin command. We unset the function before we unset the
573 * variable to allow a function to be unset when there is a readonly variable
574 * with the same name.
577 unsetcmd(argc, argv) char **argv; {
578 register char **ap;
580 for (ap = argv + 1 ; *ap ; ap++) {
581 unsetfunc(*ap);
582 unsetvar(*ap);
584 return 0;
589 * Unset the specified variable.
592 STATIC void
593 unsetvar(s)
594 char *s;
596 register struct var **vpp;
597 register struct var *vp;
599 vpp = hashvar(s);
600 for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
601 if (varequal(vp->text, s)) {
602 INTOFF;
603 if (*(strchr(vp->text, '=') + 1) != '\0'
604 || vp->flags & VREADONLY) {
605 setvar(s, nullstr, 0);
607 vp->flags &=~ VEXPORT;
608 vp->flags |= VUNSET;
609 if ((vp->flags & VSTRFIXED) == 0) {
610 if ((vp->flags & VTEXTFIXED) == 0)
611 ckfree(vp->text);
612 *vpp = vp->next;
613 ckfree(vp);
615 INTON;
616 return;
624 * Find the appropriate entry in the hash table from the name.
627 STATIC struct var **
628 hashvar(p)
629 register char *p;
631 unsigned int hashval;
633 hashval = *p << 4;
634 while (*p && *p != '=')
635 hashval += *p++;
636 return &vartab[hashval % VTABSIZE];
642 * Returns true if the two strings specify the same varable. The first
643 * variable name is terminated by '='; the second may be terminated by
644 * either '=' or '\0'.
647 STATIC int
648 varequal(p, q)
649 register char *p, *q;
651 while (*p == *q++) {
652 if (*p++ == '=')
653 return 1;
655 if (*p == '=' && *(q - 1) == '\0')
656 return 1;
657 return 0;