add opendir alias
[minix.git] / commands / ash / var.c
blob061d3a5a027e545f632333e31545edc396db213f
1 /*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. 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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 5/4/95";
36 #endif
37 #endif /* not lint */
38 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD: src/bin/sh/var.c,v 1.26.2.1 2004/09/30 04:41:55 des Exp $");
43 #include <unistd.h>
44 #include <stdlib.h>
45 #ifndef NO_PATHS_H
46 #include <paths.h>
47 #endif
50 * Shell variables.
53 #include <locale.h>
55 #include "shell.h"
56 #include "output.h"
57 #include "expand.h"
58 #include "nodes.h" /* for other headers */
59 #include "eval.h" /* defines cmdenviron */
60 #include "exec.h"
61 #include "syntax.h"
62 #include "options.h"
63 #include "mail.h"
64 #include "var.h"
65 #include "memalloc.h"
66 #include "error.h"
67 #include "mystring.h"
68 #include "parser.h"
69 #if !defined(NO_HISTORY)
70 #include "myhistedit.h"
71 #endif
73 #include "builtins.h"
75 #ifndef _PATH_DEFPATH
76 #define _PATH_DEFPATH "/usr/bin:/bin"
77 #endif
79 #define VTABSIZE 39
82 struct varinit {
83 struct var *var;
84 int flags;
85 char *text;
86 void (*func)(const char *);
90 #ifndef NO_HISTORY
91 struct var vhistsize;
92 #endif
93 struct var vifs;
94 struct var vmail;
95 struct var vmpath;
96 struct var vpath;
97 struct var vppid;
98 struct var vps1;
99 struct var vps2;
100 struct var vpse;
101 struct var vvers;
102 STATIC struct var voptind;
104 STATIC const struct varinit varinit[] = {
105 #if !defined(NO_HISTORY)
106 { &vhistsize, VSTRFIXED|VTEXTFIXED|VUNSET, "HISTSIZE=",
107 sethistsize },
108 #endif
109 { &vifs, VSTRFIXED|VTEXTFIXED, "IFS= \t\n",
110 NULL },
111 { &vmail, VSTRFIXED|VTEXTFIXED|VUNSET, "MAIL=",
112 NULL },
113 { &vmpath, VSTRFIXED|VTEXTFIXED|VUNSET, "MAILPATH=",
114 NULL },
115 { &vpath, VSTRFIXED|VTEXTFIXED, "PATH=" _PATH_DEFPATH,
116 changepath },
117 { &vppid, VSTRFIXED|VTEXTFIXED|VUNSET, "PPID=",
118 NULL },
120 * vps1 depends on uid
122 { &vps2, VSTRFIXED|VTEXTFIXED, "PS2=> ",
123 NULL },
124 { &vpse, VSTRFIXED|VTEXTFIXED|VUNSET, "PSE=",
125 NULL },
126 { &voptind, VSTRFIXED|VTEXTFIXED, "OPTIND=1",
127 getoptsreset },
128 { NULL, 0, NULL,
129 NULL }
132 STATIC struct var *vartab[VTABSIZE];
134 STATIC struct var **hashvar(char *);
135 STATIC int varequal(char *, char *);
136 STATIC int localevar(char *);
139 * Initialize the varable symbol tables and import the environment
142 #ifdef mkinit
143 INCLUDE "var.h"
144 INIT {
145 char **envp;
146 extern char **environ;
148 initvar();
149 for (envp = environ ; *envp ; envp++) {
150 if (strchr(*envp, '=')) {
151 setvareq(*envp, VEXPORT|VTEXTFIXED);
155 #endif
159 * This routine initializes the builtin variables. It is called when the
160 * shell is initialized and again when a shell procedure is spawned.
163 void
164 initvar(void)
166 char ppid[20];
167 const struct varinit *ip;
168 struct var *vp;
169 struct var **vpp;
171 for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
172 if ((vp->flags & VEXPORT) == 0) {
173 vpp = hashvar(ip->text);
174 vp->next = *vpp;
175 *vpp = vp;
176 vp->text = ip->text;
177 vp->flags = ip->flags;
178 vp->func = ip->func;
182 * PS1 depends on uid
184 if ((vps1.flags & VEXPORT) == 0) {
185 vpp = hashvar("PS1=");
186 vps1.next = *vpp;
187 *vpp = &vps1;
188 vps1.text = geteuid() ? "PS1=$ " : "PS1=# ";
189 vps1.flags = VSTRFIXED|VTEXTFIXED;
191 if ((vppid.flags & VEXPORT) == 0) {
192 fmtstr(ppid, sizeof(ppid), "%d", (int)getppid());
193 setvarsafe("PPID", ppid, 0);
198 * Safe version of setvar, returns 1 on success 0 on failure.
202 setvarsafe(char *name, char *val, int flags)
204 struct jmploc jmploc;
205 struct jmploc *volatile savehandler = handler;
206 int err = 0;
207 #if __GNUC__
208 /* Avoid longjmp clobbering */
209 (void) &err;
210 #endif
212 if (setjmp(jmploc.loc))
213 err = 1;
214 else {
215 handler = &jmploc;
216 setvar(name, val, flags);
218 handler = savehandler;
219 return err;
223 * Set the value of a variable. The flags argument is tored with the
224 * flags of the variable. If val is NULL, the variable is unset.
227 void
228 setvar(char *name, char *val, int flags)
230 char *p, *q;
231 int len;
232 int namelen;
233 char *nameeq;
234 int isbad;
236 isbad = 0;
237 p = name;
238 if (! is_name(*p))
239 isbad = 1;
240 p++;
241 for (;;) {
242 if (! is_in_name(*p)) {
243 if (*p == '\0' || *p == '=')
244 break;
245 isbad = 1;
247 p++;
249 namelen = p - name;
250 if (isbad)
251 error("%.*s: bad variable name", namelen, name);
252 len = namelen + 2; /* 2 is space for '=' and '\0' */
253 if (val == NULL) {
254 flags |= VUNSET;
255 } else {
256 len += strlen(val);
258 p = nameeq = ckmalloc(len);
259 q = name;
260 while (--namelen >= 0)
261 *p++ = *q++;
262 *p++ = '=';
263 *p = '\0';
264 if (val)
265 scopy(val, p);
266 setvareq(nameeq, flags);
269 STATIC int
270 localevar(char *s)
272 static char *lnames[7] = {
273 "ALL", "COLLATE", "CTYPE", "MONETARY",
274 "NUMERIC", "TIME", NULL
276 char **ss;
278 if (*s != 'L')
279 return 0;
280 if (varequal(s + 1, "ANG"))
281 return 1;
282 if (strncmp(s + 1, "C_", 2) != 0)
283 return 0;
284 for (ss = lnames; *ss ; ss++)
285 if (varequal(s + 3, *ss))
286 return 1;
287 return 0;
291 * Same as setvar except that the variable and value are passed in
292 * the first argument as name=value. Since the first argument will
293 * be actually stored in the table, it should not be a string that
294 * will go away.
297 void
298 setvareq(char *s, int flags)
300 struct var *vp, **vpp;
301 int len;
303 if (aflag)
304 flags |= VEXPORT;
305 vpp = hashvar(s);
306 for (vp = *vpp ; vp ; vp = vp->next) {
307 if (varequal(s, vp->text)) {
308 if (vp->flags & VREADONLY) {
309 len = strchr(s, '=') - s;
310 error("%.*s: is read only", len, s);
312 INTOFF;
314 if (vp->func && (flags & VNOFUNC) == 0)
315 (*vp->func)(strchr(s, '=') + 1);
317 if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
318 ckfree(vp->text);
320 vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
321 vp->flags |= flags;
322 vp->text = s;
325 * We could roll this to a function, to handle it as
326 * a regular variable function callback, but why bother?
328 if (vp == &vmpath || (vp == &vmail && ! mpathset()))
329 chkmail(1);
330 if ((vp->flags & VEXPORT) && localevar(s)) {
331 putenv(s);
332 (void) setlocale(LC_ALL, "");
334 INTON;
335 return;
338 /* not found */
339 vp = ckmalloc(sizeof (*vp));
340 vp->flags = flags;
341 vp->text = s;
342 vp->next = *vpp;
343 vp->func = NULL;
344 INTOFF;
345 *vpp = vp;
346 if ((vp->flags & VEXPORT) && localevar(s)) {
347 putenv(s);
348 (void) setlocale(LC_ALL, "");
350 INTON;
356 * Process a linked list of variable assignments.
359 void
360 listsetvar(struct strlist *list)
362 struct strlist *lp;
364 INTOFF;
365 for (lp = list ; lp ; lp = lp->next) {
366 setvareq(savestr(lp->text), 0);
368 INTON;
374 * Find the value of a variable. Returns NULL if not set.
377 char *
378 lookupvar(char *name)
380 struct var *v;
382 for (v = *hashvar(name) ; v ; v = v->next) {
383 if (varequal(v->text, name)) {
384 if (v->flags & VUNSET)
385 return NULL;
386 return strchr(v->text, '=') + 1;
389 return NULL;
395 * Search the environment of a builtin command. If the second argument
396 * is nonzero, return the value of a variable even if it hasn't been
397 * exported.
400 char *
401 bltinlookup(char *name, int doall)
403 struct strlist *sp;
404 struct var *v;
406 for (sp = cmdenviron ; sp ; sp = sp->next) {
407 if (varequal(sp->text, name))
408 return strchr(sp->text, '=') + 1;
410 for (v = *hashvar(name) ; v ; v = v->next) {
411 if (varequal(v->text, name)) {
412 if ((v->flags & VUNSET)
413 || (!doall && (v->flags & VEXPORT) == 0))
414 return NULL;
415 return strchr(v->text, '=') + 1;
418 return NULL;
424 * Generate a list of exported variables. This routine is used to construct
425 * the third argument to execve when executing a program.
428 char **
429 environment(void)
431 int nenv;
432 struct var **vpp;
433 struct var *vp;
434 char **env, **ep;
436 nenv = 0;
437 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
438 for (vp = *vpp ; vp ; vp = vp->next)
439 if (vp->flags & VEXPORT)
440 nenv++;
442 ep = env = stalloc((nenv + 1) * sizeof *env);
443 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
444 for (vp = *vpp ; vp ; vp = vp->next)
445 if (vp->flags & VEXPORT)
446 *ep++ = vp->text;
448 *ep = NULL;
449 return env;
454 * Called when a shell procedure is invoked to clear out nonexported
455 * variables. It is also necessary to reallocate variables of with
456 * VSTACK set since these are currently allocated on the stack.
459 #ifdef mkinit
460 SHELLPROC {
461 shprocvar();
463 #endif
465 void
466 shprocvar(void)
468 struct var **vpp;
469 struct var *vp, **prev;
471 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
472 for (prev = vpp ; (vp = *prev) != NULL ; ) {
473 if ((vp->flags & VEXPORT) == 0) {
474 *prev = vp->next;
475 if ((vp->flags & VTEXTFIXED) == 0)
476 ckfree(vp->text);
477 if ((vp->flags & VSTRFIXED) == 0)
478 ckfree(vp);
479 } else {
480 if (vp->flags & VSTACK) {
481 vp->text = savestr(vp->text);
482 vp->flags &=~ VSTACK;
484 prev = &vp->next;
488 initvar();
494 * Command to list all variables which are set. Currently this command
495 * is invoked from the set command when the set command is called without
496 * any variables.
500 showvarscmd(int argc __unused, char **argv __unused)
502 struct var **vpp;
503 struct var *vp;
504 const char *s;
506 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
507 for (vp = *vpp ; vp ; vp = vp->next) {
508 if (vp->flags & VUNSET)
509 continue;
510 for (s = vp->text; *s != '='; s++)
511 out1c(*s);
512 out1c('=');
513 out1qstr(s + 1);
514 out1c('\n');
517 return 0;
523 * The export and readonly commands.
527 exportcmd(int argc, char **argv)
529 struct var **vpp;
530 struct var *vp;
531 char *name;
532 char *p;
533 char *cmdname;
534 int ch, values;
535 int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
537 cmdname = argv[0];
538 optreset = optind = 1;
539 opterr = 0;
540 values = 0;
541 while ((ch = getopt(argc, argv, "p")) != -1) {
542 switch (ch) {
543 case 'p':
544 values = 1;
545 break;
546 case '?':
547 default:
548 error("unknown option: -%c", optopt);
551 argc -= optind;
552 argv += optind;
554 listsetvar(cmdenviron);
555 if (argc != 0) {
556 while ((name = *argptr++) != NULL) {
557 if ((p = strchr(name, '=')) != NULL) {
558 p++;
559 } else {
560 vpp = hashvar(name);
561 for (vp = *vpp ; vp ; vp = vp->next) {
562 if (varequal(vp->text, name)) {
564 vp->flags |= flag;
565 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
566 putenv(vp->text);
567 (void) setlocale(LC_ALL, "");
569 goto found;
573 setvar(name, p, flag);
574 found:;
576 } else {
577 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
578 for (vp = *vpp ; vp ; vp = vp->next) {
579 if (vp->flags & flag) {
580 if (values) {
581 out1str(cmdname);
582 out1c(' ');
584 for (p = vp->text ; *p != '=' ; p++)
585 out1c(*p);
586 if (values && !(vp->flags & VUNSET)) {
587 out1c('=');
588 out1qstr(p + 1);
590 out1c('\n');
595 return 0;
600 * The "local" command.
604 localcmd(int argc __unused, char **argv __unused)
606 char *name;
608 if (! in_function())
609 error("Not in a function");
610 while ((name = *argptr++) != NULL) {
611 mklocal(name);
613 return 0;
618 * Make a variable a local variable. When a variable is made local, it's
619 * value and flags are saved in a localvar structure. The saved values
620 * will be restored when the shell function returns. We handle the name
621 * "-" as a special case.
624 void
625 mklocal(char *name)
627 struct localvar *lvp;
628 struct var **vpp;
629 struct var *vp;
631 INTOFF;
632 lvp = ckmalloc(sizeof (struct localvar));
633 if (name[0] == '-' && name[1] == '\0') {
634 lvp->text = ckmalloc(sizeof optlist);
635 memcpy(lvp->text, optlist, sizeof optlist);
636 vp = NULL;
637 } else {
638 vpp = hashvar(name);
639 for (vp = *vpp ; vp && ! varequal(vp->text, name) ; vp = vp->next);
640 if (vp == NULL) {
641 if (strchr(name, '='))
642 setvareq(savestr(name), VSTRFIXED);
643 else
644 setvar(name, NULL, VSTRFIXED);
645 vp = *vpp; /* the new variable */
646 lvp->text = NULL;
647 lvp->flags = VUNSET;
648 } else {
649 lvp->text = vp->text;
650 lvp->flags = vp->flags;
651 vp->flags |= VSTRFIXED|VTEXTFIXED;
652 if (strchr(name, '='))
653 setvareq(savestr(name), 0);
656 lvp->vp = vp;
657 lvp->next = localvars;
658 localvars = lvp;
659 INTON;
664 * Called after a function returns.
667 void
668 poplocalvars(void)
670 struct localvar *lvp;
671 struct var *vp;
673 while ((lvp = localvars) != NULL) {
674 localvars = lvp->next;
675 vp = lvp->vp;
676 if (vp == NULL) { /* $- saved */
677 memcpy(optlist, lvp->text, sizeof optlist);
678 ckfree(lvp->text);
679 } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
680 (void)unsetvar(vp->text);
681 } else {
682 if ((vp->flags & VTEXTFIXED) == 0)
683 ckfree(vp->text);
684 vp->flags = lvp->flags;
685 vp->text = lvp->text;
687 ckfree(lvp);
693 setvarcmd(int argc, char **argv)
695 if (argc <= 2)
696 return unsetcmd(argc, argv);
697 else if (argc == 3)
698 setvar(argv[1], argv[2], 0);
699 else
700 error("List assignment not implemented");
701 return 0;
706 * The unset builtin command. We unset the function before we unset the
707 * variable to allow a function to be unset when there is a readonly variable
708 * with the same name.
712 unsetcmd(int argc __unused, char **argv __unused)
714 char **ap;
715 int i;
716 int flg_func = 0;
717 int flg_var = 0;
718 int ret = 0;
720 while ((i = nextopt("vf")) != '\0') {
721 if (i == 'f')
722 flg_func = 1;
723 else
724 flg_var = 1;
726 if (flg_func == 0 && flg_var == 0)
727 flg_var = 1;
729 for (ap = argptr; *ap ; ap++) {
730 if (flg_func)
731 ret |= unsetfunc(*ap);
732 if (flg_var)
733 ret |= unsetvar(*ap);
735 return ret;
740 * Unset the specified variable.
744 unsetvar(char *s)
746 struct var **vpp;
747 struct var *vp;
749 vpp = hashvar(s);
750 for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
751 if (varequal(vp->text, s)) {
752 if (vp->flags & VREADONLY)
753 return (1);
754 INTOFF;
755 if (*(strchr(vp->text, '=') + 1) != '\0')
756 setvar(s, nullstr, 0);
757 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
758 unsetenv(s);
759 setlocale(LC_ALL, "");
761 vp->flags &= ~VEXPORT;
762 vp->flags |= VUNSET;
763 if ((vp->flags & VSTRFIXED) == 0) {
764 if ((vp->flags & VTEXTFIXED) == 0)
765 ckfree(vp->text);
766 *vpp = vp->next;
767 ckfree(vp);
769 INTON;
770 return (0);
774 return (0);
780 * Find the appropriate entry in the hash table from the name.
783 STATIC struct var **
784 hashvar(char *p)
786 unsigned int hashval;
788 hashval = ((unsigned char) *p) << 4;
789 while (*p && *p != '=')
790 hashval += (unsigned char) *p++;
791 return &vartab[hashval % VTABSIZE];
797 * Returns true if the two strings specify the same varable. The first
798 * variable name is terminated by '='; the second may be terminated by
799 * either '=' or '\0'.
802 STATIC int
803 varequal(char *p, char *q)
805 while (*p == *q++) {
806 if (*p++ == '=')
807 return 1;
809 if (*p == '=' && *(q - 1) == '\0')
810 return 1;
811 return 0;
815 * $PchId: var.c,v 1.5 2006/05/22 12:28:49 philip Exp $