shell: Always use explicit large file API
[dash.git] / src / var.c
blobef9c2bde8643aad8cac1b97b5d71f2ed40d03e9c
1 /*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1997-2005
5 * Herbert Xu <herbert@gondor.apana.org.au>. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Kenneth Almquist.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
35 #include <unistd.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #ifdef HAVE_PATHS_H
39 #include <paths.h>
40 #endif
43 * Shell variables.
46 #include "shell.h"
47 #include "output.h"
48 #include "expand.h"
49 #include "nodes.h" /* for other headers */
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"
58 #include "parser.h"
59 #include "show.h"
60 #ifndef SMALL
61 #include "myhistedit.h"
62 #endif
63 #include "system.h"
66 #define VTABSIZE 39
69 struct localvar_list {
70 struct localvar_list *next;
71 struct localvar *lv;
74 MKINIT struct localvar_list *localvar_stack;
76 const char defpathvar[] =
77 "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin";
78 char defifsvar[] = "IFS= \t\n";
79 MKINIT char defoptindvar[] = "OPTIND=1";
81 int lineno;
82 char linenovar[sizeof("LINENO=")+sizeof(int)*CHAR_BIT/3+1] = "LINENO=";
84 /* Some macros in var.h depend on the order, add new variables to the end. */
85 struct var varinit[] = {
86 #if ATTY
87 { 0, VSTRFIXED|VTEXTFIXED|VUNSET, "ATTY\0", 0 },
88 #endif
89 { 0, VSTRFIXED|VTEXTFIXED, defifsvar, 0 },
90 { 0, VSTRFIXED|VTEXTFIXED|VUNSET, "MAIL\0", changemail },
91 { 0, VSTRFIXED|VTEXTFIXED|VUNSET, "MAILPATH\0", changemail },
92 { 0, VSTRFIXED|VTEXTFIXED, defpathvar, changepath },
93 { 0, VSTRFIXED|VTEXTFIXED, "PS1=$ ", 0 },
94 { 0, VSTRFIXED|VTEXTFIXED, "PS2=> ", 0 },
95 { 0, VSTRFIXED|VTEXTFIXED, "PS4=+ ", 0 },
96 { 0, VSTRFIXED|VTEXTFIXED, defoptindvar, getoptsreset },
97 #ifdef WITH_LINENO
98 { 0, VSTRFIXED|VTEXTFIXED, linenovar, 0 },
99 #endif
100 #ifndef SMALL
101 { 0, VSTRFIXED|VTEXTFIXED|VUNSET, "TERM\0", 0 },
102 { 0, VSTRFIXED|VTEXTFIXED|VUNSET, "HISTSIZE\0", sethistsize },
103 #endif
106 STATIC struct var *vartab[VTABSIZE];
108 STATIC struct var **hashvar(const char *);
109 STATIC int vpcmp(const void *, const void *);
110 STATIC struct var **findvar(struct var **, const char *);
113 * Initialize the varable symbol tables and import the environment
116 #ifdef mkinit
117 INCLUDE <unistd.h>
118 INCLUDE <sys/types.h>
119 INCLUDE <sys/stat.h>
120 INCLUDE "cd.h"
121 INCLUDE "output.h"
122 INCLUDE "var.h"
123 MKINIT char **environ;
124 INIT {
125 char **envp;
126 static char ppid[32] = "PPID=";
127 const char *p;
128 struct stat64 st1, st2;
130 initvar();
131 for (envp = environ ; *envp ; envp++) {
132 p = endofname(*envp);
133 if (p != *envp && *p == '=') {
134 setvareq(*envp, VEXPORT|VTEXTFIXED);
138 setvareq(defifsvar, VTEXTFIXED);
139 setvareq(defoptindvar, VTEXTFIXED);
141 fmtstr(ppid + 5, sizeof(ppid) - 5, "%ld", (long) getppid());
142 setvareq(ppid, VTEXTFIXED);
144 p = lookupvar("PWD");
145 if (p)
146 if (*p != '/' || stat64(p, &st1) || stat64(".", &st2) ||
147 st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)
148 p = 0;
149 setpwd(p, 0);
152 RESET {
153 unwindlocalvars(0);
155 #endif
159 * This routine initializes the builtin variables. It is called when the
160 * shell is initialized.
163 void
164 initvar(void)
166 struct var *vp;
167 struct var *end;
168 struct var **vpp;
170 vp = varinit;
171 end = vp + sizeof(varinit) / sizeof(varinit[0]);
172 do {
173 vpp = hashvar(vp->text);
174 vp->next = *vpp;
175 *vpp = vp;
176 } while (++vp < end);
178 * PS1 depends on uid
180 if (!geteuid())
181 vps1.text = "PS1=# ";
185 * Set the value of a variable. The flags argument is ored with the
186 * flags of the variable. If val is NULL, the variable is unset.
189 struct var *setvar(const char *name, const char *val, int flags)
191 char *p, *q;
192 size_t namelen;
193 char *nameeq;
194 size_t vallen;
195 struct var *vp;
197 q = endofname(name);
198 p = strchrnul(q, '=');
199 namelen = p - name;
200 if (!namelen || p != q)
201 sh_error("%.*s: bad variable name", namelen, name);
202 vallen = 0;
203 if (val == NULL) {
204 flags |= VUNSET;
205 } else {
206 vallen = strlen(val);
208 INTOFF;
209 p = mempcpy(nameeq = ckmalloc(namelen + vallen + 2), name, namelen);
210 if (val) {
211 *p++ = '=';
212 p = mempcpy(p, val, vallen);
214 *p = '\0';
215 vp = setvareq(nameeq, flags | VNOSAVE);
216 INTON;
218 return vp;
222 * Set the given integer as the value of a variable. The flags argument is
223 * ored with the flags of the variable.
226 intmax_t setvarint(const char *name, intmax_t val, int flags)
228 int len = max_int_length(sizeof(val));
229 char buf[len];
231 fmtstr(buf, len, "%" PRIdMAX, val);
232 setvar(name, buf, flags);
233 return val;
239 * Same as setvar except that the variable and value are passed in
240 * the first argument as name=value. Since the first argument will
241 * be actually stored in the table, it should not be a string that
242 * will go away.
243 * Called with interrupts off.
246 struct var *setvareq(char *s, int flags)
248 struct var *vp, **vpp;
250 vpp = hashvar(s);
251 flags |= (VEXPORT & (((unsigned) (1 - aflag)) - 1));
252 vpp = findvar(vpp, s);
253 vp = *vpp;
254 if (vp) {
255 if (vp->flags & VREADONLY) {
256 const char *n;
258 if (flags & VNOSAVE)
259 free(s);
260 n = vp->text;
261 sh_error("%.*s: is read only", strchrnul(n, '=') - n,
265 if (flags & VNOSET)
266 goto out;
268 if (vp->func && (flags & VNOFUNC) == 0)
269 (*vp->func)(strchrnul(s, '=') + 1);
271 if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
272 ckfree(vp->text);
274 if (((flags & (VEXPORT|VREADONLY|VSTRFIXED|VUNSET)) |
275 (vp->flags & VSTRFIXED)) == VUNSET) {
276 *vpp = vp->next;
277 ckfree(vp);
278 out_free:
279 if ((flags & (VTEXTFIXED|VSTACK|VNOSAVE)) == VNOSAVE)
280 ckfree(s);
281 goto out;
284 flags |= vp->flags & ~(VTEXTFIXED|VSTACK|VNOSAVE|VUNSET);
285 } else {
286 if (flags & VNOSET)
287 goto out;
288 if ((flags & (VEXPORT|VREADONLY|VSTRFIXED|VUNSET)) == VUNSET)
289 goto out_free;
290 /* not found */
291 vp = ckmalloc(sizeof (*vp));
292 vp->next = *vpp;
293 vp->func = NULL;
294 *vpp = vp;
296 if (!(flags & (VTEXTFIXED|VSTACK|VNOSAVE)))
297 s = savestr(s);
298 vp->text = s;
299 vp->flags = flags;
301 out:
302 return vp;
306 * Find the value of a variable. Returns NULL if not set.
309 char *
310 lookupvar(const char *name)
312 struct var *v;
314 if ((v = *findvar(hashvar(name), name)) && !(v->flags & VUNSET)) {
315 #ifdef WITH_LINENO
316 if (v == &vlineno && v->text == linenovar) {
317 fmtstr(linenovar+7, sizeof(linenovar)-7, "%d", lineno);
319 #endif
320 return strchrnul(v->text, '=') + 1;
322 return NULL;
325 intmax_t lookupvarint(const char *name)
327 return atomax(lookupvar(name) ?: nullstr, 0);
333 * Generate a list of variables satisfying the given conditions.
336 char **
337 listvars(int on, int off, char ***end)
339 struct var **vpp;
340 struct var *vp;
341 char **ep;
342 int mask;
344 STARTSTACKSTR(ep);
345 vpp = vartab;
346 mask = on | off;
347 do {
348 for (vp = *vpp ; vp ; vp = vp->next)
349 if ((vp->flags & mask) == on) {
350 if (ep == stackstrend())
351 ep = growstackstr();
352 *ep++ = (char *) vp->text;
354 } while (++vpp < vartab + VTABSIZE);
355 if (ep == stackstrend())
356 ep = growstackstr();
357 if (end)
358 *end = ep;
359 *ep++ = NULL;
360 return grabstackstr(ep);
366 * POSIX requires that 'set' (but not export or readonly) output the
367 * variables in lexicographic order - by the locale's collating order (sigh).
368 * Maybe we could keep them in an ordered balanced binary tree
369 * instead of hashed lists.
370 * For now just roll 'em through qsort for printing...
374 showvars(const char *prefix, int on, int off)
376 const char *sep;
377 char **ep, **epend;
379 ep = listvars(on, off, &epend);
380 qsort(ep, epend - ep, sizeof(char *), vpcmp);
382 sep = *prefix ? spcstr : prefix;
384 for (; ep < epend; ep++) {
385 const char *p;
386 const char *q;
388 p = strchrnul(*ep, '=');
389 q = nullstr;
390 if (*p)
391 q = single_quote(++p);
393 out1fmt("%s%s%.*s%s\n", prefix, sep, (int)(p - *ep), *ep, q);
396 return 0;
402 * The export and readonly commands.
406 exportcmd(int argc, char **argv)
408 struct var *vp;
409 char *name;
410 const char *p;
411 char **aptr;
412 int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
413 int notp;
415 notp = nextopt("p") - 'p';
416 if (notp && ((name = *(aptr = argptr)))) {
417 do {
418 if ((p = strchr(name, '=')) != NULL) {
419 p++;
420 } else {
421 if ((vp = *findvar(hashvar(name), name))) {
422 vp->flags |= flag;
423 continue;
426 setvar(name, p, flag);
427 } while ((name = *++aptr) != NULL);
428 } else {
429 showvars(argv[0], flag, 0);
431 return 0;
436 * The "local" command.
440 localcmd(int argc, char **argv)
442 char *name;
444 if (!localvar_stack)
445 sh_error("not in a function");
447 argv = argptr;
448 while ((name = *argv++) != NULL) {
449 mklocal(name, 0);
451 return 0;
456 * Make a variable a local variable. When a variable is made local, it's
457 * value and flags are saved in a localvar structure. The saved values
458 * will be restored when the shell function returns. We handle the name
459 * "-" as a special case.
462 void mklocal(char *name, int flags)
464 struct localvar *lvp;
465 struct var **vpp;
466 struct var *vp;
468 INTOFF;
469 lvp = ckmalloc(sizeof (struct localvar));
470 if (name[0] == '-' && name[1] == '\0') {
471 char *p;
472 p = ckmalloc(sizeof(optlist));
473 lvp->text = memcpy(p, optlist, sizeof(optlist));
474 vp = NULL;
475 } else {
476 char *eq;
478 vpp = hashvar(name);
479 vp = *findvar(vpp, name);
480 eq = strchr(name, '=');
481 if (vp == NULL) {
482 if (eq)
483 vp = setvareq(name, VSTRFIXED | flags);
484 else
485 vp = setvar(name, NULL, VSTRFIXED | flags);
486 lvp->flags = VUNSET;
487 } else {
488 lvp->text = vp->text;
489 lvp->flags = vp->flags;
490 vp->flags |= VSTRFIXED|VTEXTFIXED;
491 if (eq)
492 setvareq(name, flags);
495 lvp->vp = vp;
496 lvp->next = localvar_stack->lv;
497 localvar_stack->lv = lvp;
498 INTON;
503 * Called after a function returns.
504 * Interrupts must be off.
507 static void
508 poplocalvars(void)
510 struct localvar_list *ll;
511 struct localvar *lvp, *next;
512 struct var *vp;
514 INTOFF;
515 ll = localvar_stack;
516 localvar_stack = ll->next;
518 next = ll->lv;
519 ckfree(ll);
521 while ((lvp = next) != NULL) {
522 next = lvp->next;
523 vp = lvp->vp;
524 TRACE(("poplocalvar %s\n", vp ? vp->text : "-"));
525 if (vp == NULL) { /* $- saved */
526 memcpy(optlist, lvp->text, sizeof(optlist));
527 ckfree(lvp->text);
528 optschanged();
529 } else if (lvp->flags == VUNSET) {
530 vp->flags &= ~(VSTRFIXED|VREADONLY);
531 unsetvar(vp->text);
532 } else {
533 if (vp->func)
534 (*vp->func)(strchrnul(lvp->text, '=') + 1);
535 if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
536 ckfree(vp->text);
537 vp->flags = lvp->flags;
538 vp->text = lvp->text;
540 ckfree(lvp);
542 INTON;
547 * Create a new localvar environment.
549 struct localvar_list *pushlocalvars(int push)
551 struct localvar_list *ll;
552 struct localvar_list *top;
554 top = localvar_stack;
555 if (!push)
556 goto out;
558 INTOFF;
559 ll = ckmalloc(sizeof(*ll));
560 ll->lv = NULL;
561 ll->next = top;
562 localvar_stack = ll;
563 INTON;
565 out:
566 return top;
570 void unwindlocalvars(struct localvar_list *stop)
572 while (localvar_stack != stop)
573 poplocalvars();
578 * The unset builtin command. We unset the function before we unset the
579 * variable to allow a function to be unset when there is a readonly variable
580 * with the same name.
584 unsetcmd(int argc, char **argv)
586 char **ap;
587 int i;
588 int flag = 0;
590 while ((i = nextopt("vf")) != '\0') {
591 flag = i;
594 for (ap = argptr; *ap ; ap++) {
595 if (flag != 'f') {
596 unsetvar(*ap);
597 continue;
599 if (flag != 'v')
600 unsetfunc(*ap);
602 return 0;
607 * Unset the specified variable.
610 void unsetvar(const char *s)
612 setvar(s, 0, 0);
618 * Find the appropriate entry in the hash table from the name.
621 STATIC struct var **
622 hashvar(const char *p)
624 unsigned int hashval;
626 hashval = ((unsigned char) *p) << 4;
627 while (*p && *p != '=')
628 hashval += (unsigned char) *p++;
629 return &vartab[hashval % VTABSIZE];
635 * Compares two strings up to the first = or '\0'. The first
636 * string must be terminated by '='; the second may be terminated by
637 * either '=' or '\0'.
641 varcmp(const char *p, const char *q)
643 int c, d;
645 while ((c = *p) == (d = *q)) {
646 if (!c || c == '=')
647 goto out;
648 p++;
649 q++;
651 if (c == '=')
652 c = 0;
653 if (d == '=')
654 d = 0;
655 out:
656 return c - d;
659 STATIC int
660 vpcmp(const void *a, const void *b)
662 return varcmp(*(const char **)a, *(const char **)b);
665 STATIC struct var **
666 findvar(struct var **vpp, const char *name)
668 for (; *vpp; vpp = &(*vpp)->next) {
669 if (varequal((*vpp)->text, name)) {
670 break;
673 return vpp;