sh(1): Update BUGS section for UTF-8 support.
[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
be4e801b 37 * $FreeBSD: src/bin/sh/var.c,v 1.57 2011/05/06 22:31: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"
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
99512ac4 96static const struct varinit varinit[] = {
984263bc 97#ifndef NO_HISTORY
99512ac4 98 { &vhistsize, VUNSET, "HISTSIZE=",
984263bc
MD
99 sethistsize },
100#endif
99512ac4 101 { &vifs, 0, "IFS= \t\n",
984263bc 102 NULL },
99512ac4 103 { &vmail, VUNSET, "MAIL=",
984263bc 104 NULL },
99512ac4 105 { &vmpath, VUNSET, "MAILPATH=",
984263bc 106 NULL },
99512ac4 107 { &vpath, 0, "PATH=" _PATH_DEFPATH,
984263bc 108 changepath },
99512ac4 109 { &vppid, VUNSET, "PPID=",
984263bc
MD
110 NULL },
111 /*
112 * vps1 depends on uid
113 */
99512ac4 114 { &vps2, 0, "PS2=> ",
984263bc 115 NULL },
99512ac4 116 { &vps4, 0, "PS4=+ ",
0d5aaed6 117 NULL },
99512ac4
PA
118#ifndef NO_HISTORY
119 { &vterm, VUNSET, "TERM=",
120 setterm },
121#endif
122 { &voptind, 0, "OPTIND=1",
984263bc
MD
123 getoptsreset },
124 { NULL, 0, NULL,
125 NULL }
126};
127
99512ac4 128static struct var *vartab[VTABSIZE];
984263bc 129
99512ac4
PA
130static const char *const locale_names[7] = {
131 "LC_COLLATE", "LC_CTYPE", "LC_MONETARY",
132 "LC_NUMERIC", "LC_TIME", "LC_MESSAGES", NULL
133};
134static const int locale_categories[7] = {
135 LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME, LC_MESSAGES, 0
136};
137
138static struct var **hashvar(const char *);
139static int varequal(const char *, const char *);
140static int localevar(const char *);
984263bc
MD
141
142/*
492efe05 143 * Initialize the variable symbol tables and import the environment.
984263bc
MD
144 */
145
146#ifdef mkinit
147INCLUDE "var.h"
934863d7 148MKINIT char **environ;
984263bc
MD
149INIT {
150 char **envp;
984263bc
MD
151
152 initvar();
153 for (envp = environ ; *envp ; envp++) {
154 if (strchr(*envp, '=')) {
155 setvareq(*envp, VEXPORT|VTEXTFIXED);
156 }
157 }
158}
159#endif
160
161
162/*
163 * This routine initializes the builtin variables. It is called when the
99512ac4 164 * shell is initialized.
984263bc
MD
165 */
166
167void
168initvar(void)
169{
170 char ppid[20];
171 const struct varinit *ip;
172 struct var *vp;
173 struct var **vpp;
174
175 for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
176 if ((vp->flags & VEXPORT) == 0) {
177 vpp = hashvar(ip->text);
178 vp->next = *vpp;
179 *vpp = vp;
99512ac4
PA
180 vp->text = __DECONST(char *, ip->text);
181 vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED;
984263bc
MD
182 vp->func = ip->func;
183 }
184 }
185 /*
186 * PS1 depends on uid
187 */
188 if ((vps1.flags & VEXPORT) == 0) {
189 vpp = hashvar("PS1=");
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
5d0641a4
PA
290/*
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
984263bc
MD
312/*
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;
323 int len;
324
325 if (aflag)
326 flags |= VEXPORT;
327 vpp = hashvar(s);
328 for (vp = *vpp ; vp ; vp = vp->next) {
329 if (varequal(s, vp->text)) {
330 if (vp->flags & VREADONLY) {
331 len = strchr(s, '=') - s;
332 error("%.*s: is read only", len, s);
333 }
99512ac4
PA
334 if (flags & VNOSET)
335 return;
984263bc
MD
336 INTOFF;
337
338 if (vp->func && (flags & VNOFUNC) == 0)
339 (*vp->func)(strchr(s, '=') + 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?
99512ac4
PA
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.
984263bc 355 */
99512ac4
PA
356 if ((vp == &vmpath || (vp == &vmail && ! mpathset())) &&
357 iflag == 1)
984263bc
MD
358 chkmail(1);
359 if ((vp->flags & VEXPORT) && localevar(s)) {
5d0641a4 360 change_env(s, 1);
57fed2af 361 setlocale(LC_ALL, "");
be4e801b 362 updatecharset();
984263bc
MD
363 }
364 INTON;
365 return;
366 }
367 }
368 /* not found */
99512ac4
PA
369 if (flags & VNOSET)
370 return;
984263bc
MD
371 vp = ckmalloc(sizeof (*vp));
372 vp->flags = flags;
373 vp->text = s;
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
415 for (v = *hashvar(name) ; v ; v = v->next) {
416 if (varequal(v->text, name)) {
417 if (v->flags & VUNSET)
418 return NULL;
419 return strchr(v->text, '=') + 1;
420 }
421 }
422 return NULL;
423}
424
425
426
427/*
428 * Search the environment of a builtin command. If the second argument
429 * is nonzero, return the value of a variable even if it hasn't been
430 * exported.
431 */
432
433char *
492efe05 434bltinlookup(const char *name, int doall)
984263bc
MD
435{
436 struct strlist *sp;
437 struct var *v;
99512ac4 438 char *result;
984263bc 439
99512ac4 440 result = NULL;
984263bc
MD
441 for (sp = cmdenviron ; sp ; sp = sp->next) {
442 if (varequal(sp->text, name))
99512ac4 443 result = strchr(sp->text, '=') + 1;
984263bc 444 }
99512ac4
PA
445 if (result != NULL)
446 return result;
984263bc
MD
447 for (v = *hashvar(name) ; v ; v = v->next) {
448 if (varequal(v->text, name)) {
449 if ((v->flags & VUNSET)
450 || (!doall && (v->flags & VEXPORT) == 0))
451 return NULL;
452 return strchr(v->text, '=') + 1;
453 }
454 }
455 return NULL;
456}
457
458
99512ac4
PA
459/*
460 * Set up locale for a builtin (LANG/LC_* assignments).
461 */
462void
463bltinsetlocale(void)
464{
465 struct strlist *lp;
466 int act = 0;
467 char *loc, *locdef;
468 int i;
469
470 for (lp = cmdenviron ; lp ; lp = lp->next) {
471 if (localevar(lp->text)) {
472 act = 1;
473 break;
474 }
475 }
476 if (!act)
477 return;
478 loc = bltinlookup("LC_ALL", 0);
479 INTOFF;
480 if (loc != NULL) {
481 setlocale(LC_ALL, loc);
482 INTON;
be4e801b 483 updatecharset();
99512ac4
PA
484 return;
485 }
486 locdef = bltinlookup("LANG", 0);
487 for (i = 0; locale_names[i] != NULL; i++) {
488 loc = bltinlookup(locale_names[i], 0);
489 if (loc == NULL)
490 loc = locdef;
491 if (loc != NULL)
492 setlocale(locale_categories[i], loc);
493 }
494 INTON;
be4e801b 495 updatecharset();
99512ac4
PA
496}
497
498/*
499 * Undo the effect of bltinlocaleset().
500 */
501void
502bltinunsetlocale(void)
503{
504 struct strlist *lp;
505
506 INTOFF;
507 for (lp = cmdenviron ; lp ; lp = lp->next) {
508 if (localevar(lp->text)) {
509 setlocale(LC_ALL, "");
be4e801b 510 updatecharset();
99512ac4
PA
511 return;
512 }
513 }
514 INTON;
515}
516
be4e801b
PA
517/*
518 * Update the localeisutf8 flag.
519 */
520void
521updatecharset(void)
522{
523 char *charset;
524
525 charset = nl_langinfo(CODESET);
526 localeisutf8 = !strcmp(charset, "UTF-8");
527}
984263bc
MD
528
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 {
665 vpp = hashvar(name);
666 for (vp = *vpp ; vp ; vp = vp->next) {
667 if (varequal(vp->text, name)) {
668
669 vp->flags |= flag;
670 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
5d0641a4 671 change_env(vp->text, 1);
57fed2af 672 setlocale(LC_ALL, "");
be4e801b 673 updatecharset();
984263bc
MD
674 }
675 goto found;
676 }
677 }
678 }
679 setvar(name, p, flag);
680found:;
681 }
682 } else {
683 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
684 for (vp = *vpp ; vp ; vp = vp->next) {
685 if (vp->flags & flag) {
686 if (values) {
687 out1str(cmdname);
688 out1c(' ');
689 }
99512ac4 690 p = strchr(vp->text, '=');
984263bc 691 if (values && !(vp->flags & VUNSET)) {
99512ac4
PA
692 p++;
693 outbin(vp->text, p - vp->text,
694 out1);
695 out1qstr(p);
696 } else
697 outbin(vp->text, p - vp->text,
698 out1);
984263bc
MD
699 out1c('\n');
700 }
701 }
702 }
703 }
704 return 0;
705}
706
707
708/*
709 * The "local" command.
710 */
711
712int
713localcmd(int argc __unused, char **argv __unused)
714{
715 char *name;
716
717 if (! in_function())
718 error("Not in a function");
719 while ((name = *argptr++) != NULL) {
720 mklocal(name);
721 }
722 return 0;
723}
724
725
726/*
727 * Make a variable a local variable. When a variable is made local, it's
728 * value and flags are saved in a localvar structure. The saved values
729 * will be restored when the shell function returns. We handle the name
730 * "-" as a special case.
731 */
732
733void
734mklocal(char *name)
735{
736 struct localvar *lvp;
737 struct var **vpp;
738 struct var *vp;
739
740 INTOFF;
741 lvp = ckmalloc(sizeof (struct localvar));
742 if (name[0] == '-' && name[1] == '\0') {
743 lvp->text = ckmalloc(sizeof optlist);
744 memcpy(lvp->text, optlist, sizeof optlist);
745 vp = NULL;
746 } else {
747 vpp = hashvar(name);
748 for (vp = *vpp ; vp && ! varequal(vp->text, name) ; vp = vp->next);
749 if (vp == NULL) {
750 if (strchr(name, '='))
751 setvareq(savestr(name), VSTRFIXED);
752 else
753 setvar(name, NULL, VSTRFIXED);
754 vp = *vpp; /* the new variable */
755 lvp->text = NULL;
756 lvp->flags = VUNSET;
757 } else {
758 lvp->text = vp->text;
759 lvp->flags = vp->flags;
760 vp->flags |= VSTRFIXED|VTEXTFIXED;
761 if (strchr(name, '='))
762 setvareq(savestr(name), 0);
763 }
764 }
765 lvp->vp = vp;
766 lvp->next = localvars;
767 localvars = lvp;
768 INTON;
769}
770
771
772/*
773 * Called after a function returns.
774 */
775
776void
777poplocalvars(void)
778{
779 struct localvar *lvp;
780 struct var *vp;
781
782 while ((lvp = localvars) != NULL) {
783 localvars = lvp->next;
784 vp = lvp->vp;
785 if (vp == NULL) { /* $- saved */
786 memcpy(optlist, lvp->text, sizeof optlist);
787 ckfree(lvp->text);
99512ac4 788 optschanged();
984263bc 789 } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
57fed2af 790 unsetvar(vp->text);
984263bc
MD
791 } else {
792 if ((vp->flags & VTEXTFIXED) == 0)
793 ckfree(vp->text);
794 vp->flags = lvp->flags;
795 vp->text = lvp->text;
796 }
797 ckfree(lvp);
798 }
799}
800
801
802int
803setvarcmd(int argc, char **argv)
804{
805 if (argc <= 2)
806 return unsetcmd(argc, argv);
807 else if (argc == 3)
808 setvar(argv[1], argv[2], 0);
809 else
99512ac4 810 error("too many arguments");
984263bc
MD
811 return 0;
812}
813
814
815/*
99512ac4 816 * The unset builtin command.
984263bc
MD
817 */
818
819int
820unsetcmd(int argc __unused, char **argv __unused)
821{
822 char **ap;
823 int i;
824 int flg_func = 0;
825 int flg_var = 0;
826 int ret = 0;
827
828 while ((i = nextopt("vf")) != '\0') {
829 if (i == 'f')
830 flg_func = 1;
831 else
832 flg_var = 1;
833 }
834 if (flg_func == 0 && flg_var == 0)
835 flg_var = 1;
836
837 for (ap = argptr; *ap ; ap++) {
838 if (flg_func)
839 ret |= unsetfunc(*ap);
840 if (flg_var)
841 ret |= unsetvar(*ap);
842 }
843 return ret;
844}
845
846
847/*
848 * Unset the specified variable.
849 */
850
851int
492efe05 852unsetvar(const char *s)
984263bc
MD
853{
854 struct var **vpp;
855 struct var *vp;
856
857 vpp = hashvar(s);
858 for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
859 if (varequal(vp->text, s)) {
860 if (vp->flags & VREADONLY)
861 return (1);
862 INTOFF;
863 if (*(strchr(vp->text, '=') + 1) != '\0')
864 setvar(s, nullstr, 0);
865 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
99130033 866 change_env(__DECONST(char *, s), 0);
984263bc 867 setlocale(LC_ALL, "");
be4e801b 868 updatecharset();
984263bc
MD
869 }
870 vp->flags &= ~VEXPORT;
871 vp->flags |= VUNSET;
872 if ((vp->flags & VSTRFIXED) == 0) {
873 if ((vp->flags & VTEXTFIXED) == 0)
874 ckfree(vp->text);
875 *vpp = vp->next;
876 ckfree(vp);
877 }
878 INTON;
879 return (0);
880 }
881 }
31b6347c 882 return (0);
984263bc
MD
883}
884
885
886
887/*
888 * Find the appropriate entry in the hash table from the name.
889 */
890
99512ac4 891static struct var **
492efe05 892hashvar(const char *p)
984263bc
MD
893{
894 unsigned int hashval;
895
896 hashval = ((unsigned char) *p) << 4;
897 while (*p && *p != '=')
898 hashval += (unsigned char) *p++;
899 return &vartab[hashval % VTABSIZE];
900}
901
902
903
904/*
905 * Returns true if the two strings specify the same varable. The first
906 * variable name is terminated by '='; the second may be terminated by
907 * either '=' or '\0'.
908 */
909
99512ac4 910static int
492efe05 911varequal(const char *p, const char *q)
984263bc
MD
912{
913 while (*p == *q++) {
914 if (*p++ == '=')
915 return 1;
916 }
917 if (*p == '=' && *(q - 1) == '\0')
918 return 1;
919 return 0;
920}