sh: Fix duplicate prototypes for builtins.
[dragonfly.git] / bin / sh / var.c
CommitLineData
984263bc
MD
1/*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
7 *
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.
23 *
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.
1de703da
MD
35 *
36 * @(#)var.c 8.3 (Berkeley) 5/4/95
c3e3bc7a 37 * $FreeBSD: src/bin/sh/var.c,v 1.62 2011/06/13 21:03:27 jilles Exp $
984263bc
MD
38 */
39
984263bc
MD
40#include <unistd.h>
41#include <stdlib.h>
42
43/*
44 * Shell variables.
45 */
46
47#include <locale.h>
be4e801b 48#include <langinfo.h>
6a25bb17 49#include <paths.h>
984263bc
MD
50
51#include "shell.h"
52#include "output.h"
53#include "expand.h"
54#include "nodes.h" /* for other headers */
55#include "eval.h" /* defines cmdenviron */
56#include "exec.h"
57#include "syntax.h"
58#include "options.h"
59#include "mail.h"
60#include "var.h"
61#include "memalloc.h"
62#include "error.h"
63#include "mystring.h"
64#include "parser.h"
c3e3bc7a 65#include "builtins.h"
984263bc
MD
66#ifndef NO_HISTORY
67#include "myhistedit.h"
68#endif
69
70
71#define VTABSIZE 39
72
73
74struct varinit {
75 struct var *var;
76 int flags;
934863d7 77 const char *text;
984263bc
MD
78 void (*func)(const char *);
79};
80
81
984263bc
MD
82#ifndef NO_HISTORY
83struct var vhistsize;
99512ac4 84struct var vterm;
984263bc
MD
85#endif
86struct var vifs;
87struct var vmail;
88struct var vmpath;
89struct var vpath;
90struct var vppid;
91struct var vps1;
92struct var vps2;
0d5aaed6 93struct var vps4;
984263bc 94struct var vvers;
99512ac4 95static struct var voptind;
984263bc 96
32931063
PA
97int forcelocal;
98
99512ac4 99static const struct varinit varinit[] = {
984263bc 100#ifndef NO_HISTORY
99512ac4 101 { &vhistsize, VUNSET, "HISTSIZE=",
984263bc
MD
102 sethistsize },
103#endif
99512ac4 104 { &vifs, 0, "IFS= \t\n",
984263bc 105 NULL },
99512ac4 106 { &vmail, VUNSET, "MAIL=",
984263bc 107 NULL },
99512ac4 108 { &vmpath, VUNSET, "MAILPATH=",
984263bc 109 NULL },
99512ac4 110 { &vpath, 0, "PATH=" _PATH_DEFPATH,
984263bc 111 changepath },
99512ac4 112 { &vppid, VUNSET, "PPID=",
984263bc
MD
113 NULL },
114 /*
115 * vps1 depends on uid
116 */
99512ac4 117 { &vps2, 0, "PS2=> ",
984263bc 118 NULL },
99512ac4 119 { &vps4, 0, "PS4=+ ",
0d5aaed6 120 NULL },
99512ac4
PA
121#ifndef NO_HISTORY
122 { &vterm, VUNSET, "TERM=",
123 setterm },
124#endif
125 { &voptind, 0, "OPTIND=1",
984263bc
MD
126 getoptsreset },
127 { NULL, 0, NULL,
128 NULL }
129};
130
99512ac4 131static struct var *vartab[VTABSIZE];
984263bc 132
99512ac4
PA
133static const char *const locale_names[7] = {
134 "LC_COLLATE", "LC_CTYPE", "LC_MONETARY",
135 "LC_NUMERIC", "LC_TIME", "LC_MESSAGES", NULL
136};
137static const int locale_categories[7] = {
138 LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME, LC_MESSAGES, 0
139};
140
99512ac4 141static int varequal(const char *, const char *);
7ea41a21 142static struct var *find_var(const char *, struct var ***, int *);
99512ac4 143static int localevar(const char *);
984263bc
MD
144
145/*
492efe05 146 * Initialize the variable symbol tables and import the environment.
984263bc
MD
147 */
148
149#ifdef mkinit
150INCLUDE "var.h"
934863d7 151MKINIT char **environ;
984263bc
MD
152INIT {
153 char **envp;
984263bc
MD
154
155 initvar();
156 for (envp = environ ; *envp ; envp++) {
157 if (strchr(*envp, '=')) {
158 setvareq(*envp, VEXPORT|VTEXTFIXED);
159 }
160 }
161}
162#endif
163
164
165/*
166 * This routine initializes the builtin variables. It is called when the
99512ac4 167 * shell is initialized.
984263bc
MD
168 */
169
170void
171initvar(void)
172{
173 char ppid[20];
174 const struct varinit *ip;
175 struct var *vp;
176 struct var **vpp;
177
178 for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
7ea41a21
PA
179 if (find_var(ip->text, &vpp, &vp->name_len) != NULL)
180 continue;
181 vp->next = *vpp;
182 *vpp = vp;
183 vp->text = __DECONST(char *, ip->text);
184 vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED;
185 vp->func = ip->func;
984263bc
MD
186 }
187 /*
188 * PS1 depends on uid
189 */
7ea41a21 190 if (find_var("PS1", &vpp, &vps1.name_len) == NULL) {
984263bc
MD
191 vps1.next = *vpp;
192 *vpp = &vps1;
99512ac4 193 vps1.text = __DECONST(char *, geteuid() ? "PS1=$ " : "PS1=# ");
984263bc
MD
194 vps1.flags = VSTRFIXED|VTEXTFIXED;
195 }
196 if ((vppid.flags & VEXPORT) == 0) {
197 fmtstr(ppid, sizeof(ppid), "%d", (int)getppid());
198 setvarsafe("PPID", ppid, 0);
199 }
200}
201
202/*
203 * Safe version of setvar, returns 1 on success 0 on failure.
204 */
205
206int
492efe05 207setvarsafe(const char *name, const char *val, int flags)
984263bc
MD
208{
209 struct jmploc jmploc;
99512ac4 210 struct jmploc *const savehandler = handler;
984263bc 211 int err = 0;
99512ac4 212 int inton;
984263bc 213
99512ac4 214 inton = is_int_on();
984263bc
MD
215 if (setjmp(jmploc.loc))
216 err = 1;
217 else {
218 handler = &jmploc;
219 setvar(name, val, flags);
220 }
221 handler = savehandler;
99512ac4 222 SETINTON(inton);
984263bc
MD
223 return err;
224}
225
226/*
fab926b5 227 * Set the value of a variable. The flags argument is stored with the
984263bc
MD
228 * flags of the variable. If val is NULL, the variable is unset.
229 */
230
231void
492efe05 232setvar(const char *name, const char *val, int flags)
984263bc 233{
99512ac4 234 const char *p;
984263bc
MD
235 int len;
236 int namelen;
237 char *nameeq;
238 int isbad;
239
240 isbad = 0;
99512ac4
PA
241 p = name;
242 if (!is_name(*p))
984263bc 243 isbad = 1;
99512ac4 244 p++;
984263bc 245 for (;;) {
99512ac4
PA
246 if (!is_in_name(*p)) {
247 if (*p == '\0' || *p == '=')
984263bc
MD
248 break;
249 isbad = 1;
250 }
99512ac4 251 p++;
984263bc 252 }
99512ac4 253 namelen = p - name;
984263bc
MD
254 if (isbad)
255 error("%.*s: bad variable name", namelen, name);
256 len = namelen + 2; /* 2 is space for '=' and '\0' */
257 if (val == NULL) {
258 flags |= VUNSET;
259 } else {
260 len += strlen(val);
261 }
99512ac4
PA
262 nameeq = ckmalloc(len);
263 memcpy(nameeq, name, namelen);
264 nameeq[namelen] = '=';
984263bc 265 if (val)
99512ac4
PA
266 scopy(val, nameeq + namelen + 1);
267 else
268 nameeq[namelen + 1] = '\0';
984263bc
MD
269 setvareq(nameeq, flags);
270}
271
99512ac4 272static int
492efe05 273localevar(const char *s)
984263bc 274{
99512ac4 275 const char *const *ss;
984263bc
MD
276
277 if (*s != 'L')
278 return 0;
279 if (varequal(s + 1, "ANG"))
280 return 1;
281 if (strncmp(s + 1, "C_", 2) != 0)
282 return 0;
99512ac4
PA
283 if (varequal(s + 3, "ALL"))
284 return 1;
285 for (ss = locale_names; *ss ; ss++)
286 if (varequal(s + 3, *ss + 3))
984263bc
MD
287 return 1;
288 return 0;
289}
290
291/*
5d0641a4
PA
292 * Sets/unsets an environment variable from a pointer that may actually be a
293 * pointer into environ where the string should not be manipulated.
294 */
295static void
99512ac4 296change_env(const char *s, int set)
5d0641a4
PA
297{
298 char *eqp;
299 char *ss;
300
301 ss = savestr(s);
302 if ((eqp = strchr(ss, '=')) != NULL)
303 *eqp = '\0';
304 if (set && eqp != NULL) {
305 if (setenv(ss, eqp + 1, 1) != 0)
306 error("setenv: cannot set %s=%s", ss, eqp + 1);
307 } else
308 unsetenv(ss);
309 ckfree(ss);
310}
311
312
313/*
984263bc
MD
314 * Same as setvar except that the variable and value are passed in
315 * the first argument as name=value. Since the first argument will
316 * be actually stored in the table, it should not be a string that
317 * will go away.
318 */
319
320void
321setvareq(char *s, int flags)
322{
323 struct var *vp, **vpp;
7ea41a21 324 int nlen;
984263bc
MD
325
326 if (aflag)
327 flags |= VEXPORT;
32931063
PA
328 if (forcelocal && !(flags & (VNOSET | VNOLOCAL)))
329 mklocal(s);
7ea41a21
PA
330 vp = find_var(s, &vpp, &nlen);
331 if (vp != NULL) {
332 if (vp->flags & VREADONLY)
333 error("%.*s: is read only", vp->name_len, s);
334 if (flags & VNOSET)
984263bc 335 return;
7ea41a21
PA
336 INTOFF;
337
338 if (vp->func && (flags & VNOFUNC) == 0)
339 (*vp->func)(s + vp->name_len + 1);
340
341 if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
342 ckfree(vp->text);
343
344 vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
345 vp->flags |= flags;
346 vp->text = s;
347
348 /*
349 * We could roll this to a function, to handle it as
350 * a regular variable function callback, but why bother?
351 *
352 * Note: this assumes iflag is not set to 1 initially.
353 * As part of init(), this is called before arguments
354 * are looked at.
355 */
356 if ((vp == &vmpath || (vp == &vmail && ! mpathset())) &&
357 iflag == 1)
358 chkmail(1);
359 if ((vp->flags & VEXPORT) && localevar(s)) {
360 change_env(s, 1);
361 setlocale(LC_ALL, "");
362 updatecharset();
984263bc 363 }
7ea41a21
PA
364 INTON;
365 return;
984263bc
MD
366 }
367 /* not found */
99512ac4
PA
368 if (flags & VNOSET)
369 return;
984263bc
MD
370 vp = ckmalloc(sizeof (*vp));
371 vp->flags = flags;
372 vp->text = s;
7ea41a21 373 vp->name_len = nlen;
984263bc
MD
374 vp->next = *vpp;
375 vp->func = NULL;
376 INTOFF;
377 *vpp = vp;
378 if ((vp->flags & VEXPORT) && localevar(s)) {
5d0641a4 379 change_env(s, 1);
57fed2af 380 setlocale(LC_ALL, "");
be4e801b 381 updatecharset();
984263bc
MD
382 }
383 INTON;
384}
385
386
387
388/*
389 * Process a linked list of variable assignments.
390 */
391
392void
99512ac4 393listsetvar(struct strlist *list, int flags)
984263bc
MD
394{
395 struct strlist *lp;
396
397 INTOFF;
398 for (lp = list ; lp ; lp = lp->next) {
99512ac4 399 setvareq(savestr(lp->text), flags);
984263bc
MD
400 }
401 INTON;
402}
403
404
405
406/*
407 * Find the value of a variable. Returns NULL if not set.
408 */
409
410char *
492efe05 411lookupvar(const char *name)
984263bc
MD
412{
413 struct var *v;
414
7ea41a21
PA
415 v = find_var(name, NULL, NULL);
416 if (v == NULL || v->flags & VUNSET)
417 return NULL;
418 return v->text + v->name_len + 1;
984263bc
MD
419}
420
421
422
423/*
424 * Search the environment of a builtin command. If the second argument
425 * is nonzero, return the value of a variable even if it hasn't been
426 * exported.
427 */
428
429char *
492efe05 430bltinlookup(const char *name, int doall)
984263bc
MD
431{
432 struct strlist *sp;
433 struct var *v;
99512ac4 434 char *result;
984263bc 435
99512ac4 436 result = NULL;
984263bc
MD
437 for (sp = cmdenviron ; sp ; sp = sp->next) {
438 if (varequal(sp->text, name))
99512ac4 439 result = strchr(sp->text, '=') + 1;
984263bc 440 }
99512ac4
PA
441 if (result != NULL)
442 return result;
7ea41a21
PA
443
444 v = find_var(name, NULL, NULL);
445 if (v == NULL || v->flags & VUNSET ||
446 (!doall && (v->flags & VEXPORT) == 0))
447 return NULL;
448 return v->text + v->name_len + 1;
984263bc
MD
449}
450
451
99512ac4
PA
452/*
453 * Set up locale for a builtin (LANG/LC_* assignments).
454 */
455void
456bltinsetlocale(void)
457{
458 struct strlist *lp;
459 int act = 0;
460 char *loc, *locdef;
461 int i;
462
463 for (lp = cmdenviron ; lp ; lp = lp->next) {
464 if (localevar(lp->text)) {
465 act = 1;
466 break;
467 }
468 }
469 if (!act)
470 return;
471 loc = bltinlookup("LC_ALL", 0);
472 INTOFF;
473 if (loc != NULL) {
474 setlocale(LC_ALL, loc);
475 INTON;
be4e801b 476 updatecharset();
99512ac4
PA
477 return;
478 }
479 locdef = bltinlookup("LANG", 0);
480 for (i = 0; locale_names[i] != NULL; i++) {
481 loc = bltinlookup(locale_names[i], 0);
482 if (loc == NULL)
483 loc = locdef;
484 if (loc != NULL)
485 setlocale(locale_categories[i], loc);
486 }
487 INTON;
be4e801b 488 updatecharset();
99512ac4
PA
489}
490
491/*
492 * Undo the effect of bltinlocaleset().
493 */
494void
495bltinunsetlocale(void)
496{
497 struct strlist *lp;
498
499 INTOFF;
500 for (lp = cmdenviron ; lp ; lp = lp->next) {
501 if (localevar(lp->text)) {
502 setlocale(LC_ALL, "");
be4e801b 503 updatecharset();
99512ac4
PA
504 return;
505 }
506 }
507 INTON;
508}
509
be4e801b
PA
510/*
511 * Update the localeisutf8 flag.
512 */
513void
514updatecharset(void)
515{
516 char *charset;
517
518 charset = nl_langinfo(CODESET);
519 localeisutf8 = !strcmp(charset, "UTF-8");
520}
984263bc 521
145a4981
PA
522void
523initcharset(void)
524{
525 updatecharset();
526 initial_localeisutf8 = localeisutf8;
527}
528
984263bc
MD
529/*
530 * Generate a list of exported variables. This routine is used to construct
531 * the third argument to execve when executing a program.
532 */
533
534char **
535environment(void)
536{
537 int nenv;
538 struct var **vpp;
539 struct var *vp;
540 char **env, **ep;
541
542 nenv = 0;
543 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
544 for (vp = *vpp ; vp ; vp = vp->next)
545 if (vp->flags & VEXPORT)
546 nenv++;
547 }
548 ep = env = stalloc((nenv + 1) * sizeof *env);
549 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
550 for (vp = *vpp ; vp ; vp = vp->next)
551 if (vp->flags & VEXPORT)
552 *ep++ = vp->text;
553 }
554 *ep = NULL;
555 return env;
556}
557
558
fab926b5
PA
559static int
560var_compare(const void *a, const void *b)
561{
562 const char *const *sa, *const *sb;
563
564 sa = a;
565 sb = b;
566 /*
567 * This compares two var=value strings which creates a different
568 * order from what you would probably expect. POSIX is somewhat
569 * ambiguous on what should be sorted exactly.
570 */
571 return strcoll(*sa, *sb);
572}
573
984263bc
MD
574
575/*
99512ac4
PA
576 * Command to list all variables which are set. This is invoked from the
577 * set command when it is called without any options or operands.
984263bc
MD
578 */
579
580int
581showvarscmd(int argc __unused, char **argv __unused)
582{
583 struct var **vpp;
584 struct var *vp;
585 const char *s;
fab926b5
PA
586 const char **vars;
587 int i, n;
984263bc 588
fab926b5
PA
589 /*
590 * POSIX requires us to sort the variables.
591 */
592 n = 0;
593 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
594 for (vp = *vpp; vp; vp = vp->next) {
595 if (!(vp->flags & VUNSET))
596 n++;
984263bc
MD
597 }
598 }
fab926b5
PA
599
600 INTON;
601 vars = ckmalloc(n * sizeof(*vars));
602 i = 0;
603 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
604 for (vp = *vpp; vp; vp = vp->next) {
605 if (!(vp->flags & VUNSET))
606 vars[i++] = vp->text;
607 }
608 }
609
610 qsort(vars, n, sizeof(*vars), var_compare);
611 for (i = 0; i < n; i++) {
99512ac4
PA
612 s = strchr(vars[i], '=');
613 s++;
614 outbin(vars[i], s - vars[i], out1);
615 out1qstr(s);
fab926b5
PA
616 out1c('\n');
617 }
618 ckfree(vars);
619 INTOFF;
620
984263bc
MD
621 return 0;
622}
623
624
625
626/*
627 * The export and readonly commands.
628 */
629
630int
631exportcmd(int argc, char **argv)
632{
633 struct var **vpp;
634 struct var *vp;
635 char *name;
636 char *p;
637 char *cmdname;
638 int ch, values;
639 int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
640
641 cmdname = argv[0];
642 optreset = optind = 1;
643 opterr = 0;
644 values = 0;
645 while ((ch = getopt(argc, argv, "p")) != -1) {
646 switch (ch) {
647 case 'p':
648 values = 1;
649 break;
650 case '?':
651 default:
652 error("unknown option: -%c", optopt);
653 }
654 }
655 argc -= optind;
656 argv += optind;
657
fab926b5
PA
658 if (values && argc != 0)
659 error("-p requires no arguments");
984263bc 660 if (argc != 0) {
fab926b5 661 while ((name = *argv++) != NULL) {
984263bc
MD
662 if ((p = strchr(name, '=')) != NULL) {
663 p++;
664 } else {
7ea41a21
PA
665 vp = find_var(name, NULL, NULL);
666 if (vp != NULL) {
667 vp->flags |= flag;
668 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
669 change_env(vp->text, 1);
670 setlocale(LC_ALL, "");
671 updatecharset();
984263bc 672 }
7ea41a21 673 continue;
984263bc
MD
674 }
675 }
676 setvar(name, p, flag);
984263bc
MD
677 }
678 } else {
679 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
680 for (vp = *vpp ; vp ; vp = vp->next) {
681 if (vp->flags & flag) {
682 if (values) {
683 out1str(cmdname);
684 out1c(' ');
685 }
984263bc 686 if (values && !(vp->flags & VUNSET)) {
010baf5e
PA
687 outbin(vp->text,
688 vp->name_len + 1, out1);
689 out1qstr(vp->text +
690 vp->name_len + 1);
99512ac4 691 } else
010baf5e 692 outbin(vp->text, vp->name_len,
99512ac4 693 out1);
984263bc
MD
694 out1c('\n');
695 }
696 }
697 }
698 }
699 return 0;
700}
701
702
703/*
704 * The "local" command.
705 */
706
707int
708localcmd(int argc __unused, char **argv __unused)
709{
710 char *name;
711
712 if (! in_function())
713 error("Not in a function");
714 while ((name = *argptr++) != NULL) {
715 mklocal(name);
716 }
717 return 0;
718}
719
720
721/*
722 * Make a variable a local variable. When a variable is made local, it's
723 * value and flags are saved in a localvar structure. The saved values
724 * will be restored when the shell function returns. We handle the name
725 * "-" as a special case.
726 */
727
728void
729mklocal(char *name)
730{
731 struct localvar *lvp;
732 struct var **vpp;
733 struct var *vp;
734
735 INTOFF;
736 lvp = ckmalloc(sizeof (struct localvar));
737 if (name[0] == '-' && name[1] == '\0') {
738 lvp->text = ckmalloc(sizeof optlist);
739 memcpy(lvp->text, optlist, sizeof optlist);
740 vp = NULL;
741 } else {
7ea41a21 742 vp = find_var(name, &vpp, NULL);
984263bc
MD
743 if (vp == NULL) {
744 if (strchr(name, '='))
32931063 745 setvareq(savestr(name), VSTRFIXED | VNOLOCAL);
984263bc 746 else
32931063 747 setvar(name, NULL, VSTRFIXED | VNOLOCAL);
984263bc
MD
748 vp = *vpp; /* the new variable */
749 lvp->text = NULL;
750 lvp->flags = VUNSET;
751 } else {
752 lvp->text = vp->text;
753 lvp->flags = vp->flags;
754 vp->flags |= VSTRFIXED|VTEXTFIXED;
7ea41a21 755 if (name[vp->name_len] == '=')
32931063 756 setvareq(savestr(name), VNOLOCAL);
984263bc
MD
757 }
758 }
759 lvp->vp = vp;
760 lvp->next = localvars;
761 localvars = lvp;
762 INTON;
763}
764
765
766/*
767 * Called after a function returns.
768 */
769
770void
771poplocalvars(void)
772{
773 struct localvar *lvp;
774 struct var *vp;
775
776 while ((lvp = localvars) != NULL) {
777 localvars = lvp->next;
778 vp = lvp->vp;
779 if (vp == NULL) { /* $- saved */
780 memcpy(optlist, lvp->text, sizeof optlist);
781 ckfree(lvp->text);
99512ac4 782 optschanged();
984263bc 783 } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
57fed2af 784 unsetvar(vp->text);
984263bc
MD
785 } else {
786 if ((vp->flags & VTEXTFIXED) == 0)
787 ckfree(vp->text);
788 vp->flags = lvp->flags;
789 vp->text = lvp->text;
790 }
791 ckfree(lvp);
792 }
793}
794
795
796int
797setvarcmd(int argc, char **argv)
798{
799 if (argc <= 2)
800 return unsetcmd(argc, argv);
801 else if (argc == 3)
802 setvar(argv[1], argv[2], 0);
803 else
99512ac4 804 error("too many arguments");
984263bc
MD
805 return 0;
806}
807
808
809/*
99512ac4 810 * The unset builtin command.
984263bc
MD
811 */
812
813int
814unsetcmd(int argc __unused, char **argv __unused)
815{
816 char **ap;
817 int i;
818 int flg_func = 0;
819 int flg_var = 0;
820 int ret = 0;
821
822 while ((i = nextopt("vf")) != '\0') {
823 if (i == 'f')
824 flg_func = 1;
825 else
826 flg_var = 1;
827 }
828 if (flg_func == 0 && flg_var == 0)
829 flg_var = 1;
830
831 for (ap = argptr; *ap ; ap++) {
832 if (flg_func)
833 ret |= unsetfunc(*ap);
834 if (flg_var)
835 ret |= unsetvar(*ap);
836 }
837 return ret;
838}
839
840
841/*
842 * Unset the specified variable.
843 */
844
845int
492efe05 846unsetvar(const char *s)
984263bc
MD
847{
848 struct var **vpp;
849 struct var *vp;
850
7ea41a21
PA
851 vp = find_var(s, &vpp, NULL);
852 if (vp == NULL)
853 return (0);
854 if (vp->flags & VREADONLY)
855 return (1);
856 INTOFF;
857 if (vp->text[vp->name_len + 1] != '\0')
858 setvar(s, nullstr, 0);
859 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
860 change_env(__DECONST(char *, s), 0);
861 setlocale(LC_ALL, "");
862 updatecharset();
863 }
864 vp->flags &= ~VEXPORT;
865 vp->flags |= VUNSET;
866 if ((vp->flags & VSTRFIXED) == 0) {
867 if ((vp->flags & VTEXTFIXED) == 0)
868 ckfree(vp->text);
869 *vpp = vp->next;
870 ckfree(vp);
984263bc 871 }
7ea41a21 872 INTON;
31b6347c 873 return (0);
984263bc
MD
874}
875
876
877
878/*
653b82f9 879 * Returns true if the two strings specify the same variable. The first
984263bc
MD
880 * variable name is terminated by '='; the second may be terminated by
881 * either '=' or '\0'.
882 */
883
99512ac4 884static int
492efe05 885varequal(const char *p, const char *q)
984263bc
MD
886{
887 while (*p == *q++) {
888 if (*p++ == '=')
889 return 1;
890 }
891 if (*p == '=' && *(q - 1) == '\0')
892 return 1;
893 return 0;
894}
7ea41a21
PA
895
896/*
897 * Search for a variable.
898 * 'name' may be terminated by '=' or a NUL.
899 * vppp is set to the pointer to vp, or the list head if vp isn't found
653b82f9 900 * lenp is set to the number of characters in 'name'
7ea41a21
PA
901 */
902
903static struct var *
904find_var(const char *name, struct var ***vppp, int *lenp)
905{
906 unsigned int hashval;
907 int len;
908 struct var *vp, **vpp;
909 const char *p = name;
910
911 hashval = 0;
912 while (*p && *p != '=')
913 hashval = 2 * hashval + (unsigned char)*p++;
914 len = p - name;
915
916 if (lenp)
917 *lenp = len;
918 vpp = &vartab[hashval % VTABSIZE];
919 if (vppp)
920 *vppp = vpp;
921
922 for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
923 if (vp->name_len != len)
924 continue;
925 if (memcmp(vp->text, name, len) != 0)
926 continue;
927 if (vppp)
928 *vppp = vpp;
929 return vp;
930 }
931 return NULL;
932}