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