]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - bin/sh/var.c
Upgrade our Clang in base to r114020, from upstream's release_28 branch.
[FreeBSD/FreeBSD.git] / bin / sh / var.c
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  * 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.
19  *
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.
31  */
32
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>
39 __FBSDID("$FreeBSD$");
40
41 #include <unistd.h>
42 #include <stdlib.h>
43 #include <paths.h>
44
45 /*
46  * Shell variables.
47  */
48
49 #include <locale.h>
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
73 struct varinit {
74         struct var *var;
75         int flags;
76         const char *text;
77         void (*func)(const char *);
78 };
79
80
81 #ifndef NO_HISTORY
82 struct var vhistsize;
83 struct var vterm;
84 #endif
85 struct var vifs;
86 struct var vmail;
87 struct var vmpath;
88 struct var vpath;
89 struct var vppid;
90 struct var vps1;
91 struct var vps2;
92 struct var vps4;
93 struct var vvers;
94 STATIC struct var voptind;
95
96 STATIC const struct varinit varinit[] = {
97 #ifndef NO_HISTORY
98         { &vhistsize,   VUNSET,                         "HISTSIZE=",
99           sethistsize },
100 #endif
101         { &vifs,        0,                              "IFS= \t\n",
102           NULL },
103         { &vmail,       VUNSET,                         "MAIL=",
104           NULL },
105         { &vmpath,      VUNSET,                         "MAILPATH=",
106           NULL },
107         { &vpath,       0,                              "PATH=" _PATH_DEFPATH,
108           changepath },
109         { &vppid,       VUNSET,                         "PPID=",
110           NULL },
111         /*
112          * vps1 depends on uid
113          */
114         { &vps2,        0,                              "PS2=> ",
115           NULL },
116         { &vps4,        0,                              "PS4=+ ",
117           NULL },
118 #ifndef NO_HISTORY
119         { &vterm,       VUNSET,                         "TERM=",
120           setterm },
121 #endif
122         { &voptind,     0,                              "OPTIND=1",
123           getoptsreset },
124         { NULL, 0,                              NULL,
125           NULL }
126 };
127
128 STATIC struct var *vartab[VTABSIZE];
129
130 STATIC const char *const locale_names[7] = {
131         "LC_COLLATE", "LC_CTYPE", "LC_MONETARY",
132         "LC_NUMERIC", "LC_TIME", "LC_MESSAGES", NULL
133 };
134 STATIC const int locale_categories[7] = {
135         LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME, LC_MESSAGES, 0
136 };
137
138 STATIC struct var **hashvar(const char *);
139 STATIC int varequal(const char *, const char *);
140 STATIC int localevar(const char *);
141
142 /*
143  * Initialize the variable symbol tables and import the environment.
144  */
145
146 #ifdef mkinit
147 INCLUDE "var.h"
148 MKINIT char **environ;
149 INIT {
150         char **envp;
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
164  * shell is initialized and again when a shell procedure is spawned.
165  */
166
167 void
168 initvar(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;
180                         vp->text = __DECONST(char *, ip->text);
181                         vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED;
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;
192                 vps1.text = __DECONST(char *, geteuid() ? "PS1=$ " : "PS1=# ");
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
205 int
206 setvarsafe(const char *name, const char *val, int flags)
207 {
208         struct jmploc jmploc;
209         struct jmploc *const savehandler = handler;
210         int err = 0;
211         int inton;
212
213         inton = is_int_on();
214         if (setjmp(jmploc.loc))
215                 err = 1;
216         else {
217                 handler = &jmploc;
218                 setvar(name, val, flags);
219         }
220         handler = savehandler;
221         SETINTON(inton);
222         return err;
223 }
224
225 /*
226  * Set the value of a variable.  The flags argument is stored with the
227  * flags of the variable.  If val is NULL, the variable is unset.
228  */
229
230 void
231 setvar(const char *name, const char *val, int flags)
232 {
233         const char *p;
234         int len;
235         int namelen;
236         char *nameeq;
237         int isbad;
238
239         isbad = 0;
240         p = name;
241         if (! is_name(*p))
242                 isbad = 1;
243         p++;
244         for (;;) {
245                 if (! is_in_name(*p)) {
246                         if (*p == '\0' || *p == '=')
247                                 break;
248                         isbad = 1;
249                 }
250                 p++;
251         }
252         namelen = p - name;
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         }
261         nameeq = ckmalloc(len);
262         memcpy(nameeq, name, namelen);
263         nameeq[namelen] = '=';
264         if (val)
265                 scopy(val, nameeq + namelen + 1);
266         else
267                 nameeq[namelen + 1] = '\0';
268         setvareq(nameeq, flags);
269 }
270
271 STATIC int
272 localevar(const char *s)
273 {
274         const char *const *ss;
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;
282         if (varequal(s + 3, "ALL"))
283                 return 1;
284         for (ss = locale_names; *ss ; ss++)
285                 if (varequal(s + 3, *ss + 3))
286                         return 1;
287         return 0;
288 }
289
290
291 /*
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  */
295 static void
296 change_env(const char *s, int set)
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                 (void) setenv(ss, eqp + 1, 1);
306         else
307                 (void) unsetenv(ss);
308         ckfree(ss);
309
310         return;
311 }
312
313
314 /*
315  * Same as setvar except that the variable and value are passed in
316  * the first argument as name=value.  Since the first argument will
317  * be actually stored in the table, it should not be a string that
318  * will go away.
319  */
320
321 void
322 setvareq(char *s, int flags)
323 {
324         struct var *vp, **vpp;
325         int len;
326
327         if (aflag)
328                 flags |= VEXPORT;
329         vpp = hashvar(s);
330         for (vp = *vpp ; vp ; vp = vp->next) {
331                 if (varequal(s, vp->text)) {
332                         if (vp->flags & VREADONLY) {
333                                 len = strchr(s, '=') - s;
334                                 error("%.*s: is read only", len, s);
335                         }
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?
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                                 (void) setlocale(LC_ALL, "");
362                         }
363                         INTON;
364                         return;
365                 }
366         }
367         /* not found */
368         vp = ckmalloc(sizeof (*vp));
369         vp->flags = flags;
370         vp->text = s;
371         vp->next = *vpp;
372         vp->func = NULL;
373         INTOFF;
374         *vpp = vp;
375         if ((vp->flags & VEXPORT) && localevar(s)) {
376                 change_env(s, 1);
377                 (void) setlocale(LC_ALL, "");
378         }
379         INTON;
380 }
381
382
383
384 /*
385  * Process a linked list of variable assignments.
386  */
387
388 void
389 listsetvar(struct strlist *list)
390 {
391         struct strlist *lp;
392
393         INTOFF;
394         for (lp = list ; lp ; lp = lp->next) {
395                 setvareq(savestr(lp->text), 0);
396         }
397         INTON;
398 }
399
400
401
402 /*
403  * Find the value of a variable.  Returns NULL if not set.
404  */
405
406 char *
407 lookupvar(const char *name)
408 {
409         struct var *v;
410
411         for (v = *hashvar(name) ; v ; v = v->next) {
412                 if (varequal(v->text, name)) {
413                         if (v->flags & VUNSET)
414                                 return NULL;
415                         return strchr(v->text, '=') + 1;
416                 }
417         }
418         return NULL;
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
429 char *
430 bltinlookup(const char *name, int doall)
431 {
432         struct strlist *sp;
433         struct var *v;
434         char *result;
435
436         result = NULL;
437         for (sp = cmdenviron ; sp ; sp = sp->next) {
438                 if (varequal(sp->text, name))
439                         result = strchr(sp->text, '=') + 1;
440         }
441         if (result != NULL)
442                 return result;
443         for (v = *hashvar(name) ; v ; v = v->next) {
444                 if (varequal(v->text, name)) {
445                         if ((v->flags & VUNSET)
446                          || (!doall && (v->flags & VEXPORT) == 0))
447                                 return NULL;
448                         return strchr(v->text, '=') + 1;
449                 }
450         }
451         return NULL;
452 }
453
454
455 /*
456  * Set up locale for a builtin (LANG/LC_* assignments).
457  */
458 void
459 bltinsetlocale(void)
460 {
461         struct strlist *lp;
462         int act = 0;
463         char *loc, *locdef;
464         int i;
465
466         for (lp = cmdenviron ; lp ; lp = lp->next) {
467                 if (localevar(lp->text)) {
468                         act = 1;
469                         break;
470                 }
471         }
472         if (!act)
473                 return;
474         loc = bltinlookup("LC_ALL", 0);
475         INTOFF;
476         if (loc != NULL) {
477                 setlocale(LC_ALL, loc);
478                 INTON;
479                 return;
480         }
481         locdef = bltinlookup("LANG", 0);
482         for (i = 0; locale_names[i] != NULL; i++) {
483                 loc = bltinlookup(locale_names[i], 0);
484                 if (loc == NULL)
485                         loc = locdef;
486                 if (loc != NULL)
487                         setlocale(locale_categories[i], loc);
488         }
489         INTON;
490 }
491
492 /*
493  * Undo the effect of bltinlocaleset().
494  */
495 void
496 bltinunsetlocale(void)
497 {
498         struct strlist *lp;
499
500         INTOFF;
501         for (lp = cmdenviron ; lp ; lp = lp->next) {
502                 if (localevar(lp->text)) {
503                         setlocale(LC_ALL, "");
504                         return;
505                 }
506         }
507         INTON;
508 }
509
510
511 /*
512  * Generate a list of exported variables.  This routine is used to construct
513  * the third argument to execve when executing a program.
514  */
515
516 char **
517 environment(void)
518 {
519         int nenv;
520         struct var **vpp;
521         struct var *vp;
522         char **env, **ep;
523
524         nenv = 0;
525         for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
526                 for (vp = *vpp ; vp ; vp = vp->next)
527                         if (vp->flags & VEXPORT)
528                                 nenv++;
529         }
530         ep = env = stalloc((nenv + 1) * sizeof *env);
531         for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
532                 for (vp = *vpp ; vp ; vp = vp->next)
533                         if (vp->flags & VEXPORT)
534                                 *ep++ = vp->text;
535         }
536         *ep = NULL;
537         return env;
538 }
539
540
541 /*
542  * Called when a shell procedure is invoked to clear out nonexported
543  * variables.  It is also necessary to reallocate variables of with
544  * VSTACK set since these are currently allocated on the stack.
545  */
546
547 MKINIT void shprocvar(void);
548
549 #ifdef mkinit
550 SHELLPROC {
551         shprocvar();
552 }
553 #endif
554
555 void
556 shprocvar(void)
557 {
558         struct var **vpp;
559         struct var *vp, **prev;
560
561         for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
562                 for (prev = vpp ; (vp = *prev) != NULL ; ) {
563                         if ((vp->flags & VEXPORT) == 0) {
564                                 *prev = vp->next;
565                                 if ((vp->flags & VTEXTFIXED) == 0)
566                                         ckfree(vp->text);
567                                 if ((vp->flags & VSTRFIXED) == 0)
568                                         ckfree(vp);
569                         } else {
570                                 if (vp->flags & VSTACK) {
571                                         vp->text = savestr(vp->text);
572                                         vp->flags &=~ VSTACK;
573                                 }
574                                 prev = &vp->next;
575                         }
576                 }
577         }
578         initvar();
579 }
580
581
582 static int
583 var_compare(const void *a, const void *b)
584 {
585         const char *const *sa, *const *sb;
586
587         sa = a;
588         sb = b;
589         /*
590          * This compares two var=value strings which creates a different
591          * order from what you would probably expect.  POSIX is somewhat
592          * ambiguous on what should be sorted exactly.
593          */
594         return strcoll(*sa, *sb);
595 }
596
597
598 /*
599  * Command to list all variables which are set.  Currently this command
600  * is invoked from the set command when the set command is called without
601  * any variables.
602  */
603
604 int
605 showvarscmd(int argc __unused, char **argv __unused)
606 {
607         struct var **vpp;
608         struct var *vp;
609         const char *s;
610         const char **vars;
611         int i, n;
612
613         /*
614          * POSIX requires us to sort the variables.
615          */
616         n = 0;
617         for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
618                 for (vp = *vpp; vp; vp = vp->next) {
619                         if (!(vp->flags & VUNSET))
620                                 n++;
621                 }
622         }
623
624         INTON;
625         vars = ckmalloc(n * sizeof(*vars));
626         i = 0;
627         for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
628                 for (vp = *vpp; vp; vp = vp->next) {
629                         if (!(vp->flags & VUNSET))
630                                 vars[i++] = vp->text;
631                 }
632         }
633
634         qsort(vars, n, sizeof(*vars), var_compare);
635         for (i = 0; i < n; i++) {
636                 for (s = vars[i]; *s != '='; s++)
637                         out1c(*s);
638                 out1c('=');
639                 out1qstr(s + 1);
640                 out1c('\n');
641         }
642         ckfree(vars);
643         INTOFF;
644
645         return 0;
646 }
647
648
649
650 /*
651  * The export and readonly commands.
652  */
653
654 int
655 exportcmd(int argc, char **argv)
656 {
657         struct var **vpp;
658         struct var *vp;
659         char *name;
660         char *p;
661         char *cmdname;
662         int ch, values;
663         int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
664
665         cmdname = argv[0];
666         optreset = optind = 1;
667         opterr = 0;
668         values = 0;
669         while ((ch = getopt(argc, argv, "p")) != -1) {
670                 switch (ch) {
671                 case 'p':
672                         values = 1;
673                         break;
674                 case '?':
675                 default:
676                         error("unknown option: -%c", optopt);
677                 }
678         }
679         argc -= optind;
680         argv += optind;
681
682         if (values && argc != 0)
683                 error("-p requires no arguments");
684         if (argc != 0) {
685                 while ((name = *argv++) != NULL) {
686                         if ((p = strchr(name, '=')) != NULL) {
687                                 p++;
688                         } else {
689                                 vpp = hashvar(name);
690                                 for (vp = *vpp ; vp ; vp = vp->next) {
691                                         if (varequal(vp->text, name)) {
692
693                                                 vp->flags |= flag;
694                                                 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
695                                                         change_env(vp->text, 1);
696                                                         (void) setlocale(LC_ALL, "");
697                                                 }
698                                                 goto found;
699                                         }
700                                 }
701                         }
702                         setvar(name, p, flag);
703 found:;
704                 }
705         } else {
706                 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
707                         for (vp = *vpp ; vp ; vp = vp->next) {
708                                 if (vp->flags & flag) {
709                                         if (values) {
710                                                 out1str(cmdname);
711                                                 out1c(' ');
712                                         }
713                                         for (p = vp->text ; *p != '=' ; p++)
714                                                 out1c(*p);
715                                         if (values && !(vp->flags & VUNSET)) {
716                                                 out1c('=');
717                                                 out1qstr(p + 1);
718                                         }
719                                         out1c('\n');
720                                 }
721                         }
722                 }
723         }
724         return 0;
725 }
726
727
728 /*
729  * The "local" command.
730  */
731
732 int
733 localcmd(int argc __unused, char **argv __unused)
734 {
735         char *name;
736
737         if (! in_function())
738                 error("Not in a function");
739         while ((name = *argptr++) != NULL) {
740                 mklocal(name);
741         }
742         return 0;
743 }
744
745
746 /*
747  * Make a variable a local variable.  When a variable is made local, it's
748  * value and flags are saved in a localvar structure.  The saved values
749  * will be restored when the shell function returns.  We handle the name
750  * "-" as a special case.
751  */
752
753 void
754 mklocal(char *name)
755 {
756         struct localvar *lvp;
757         struct var **vpp;
758         struct var *vp;
759
760         INTOFF;
761         lvp = ckmalloc(sizeof (struct localvar));
762         if (name[0] == '-' && name[1] == '\0') {
763                 lvp->text = ckmalloc(sizeof optlist);
764                 memcpy(lvp->text, optlist, sizeof optlist);
765                 vp = NULL;
766         } else {
767                 vpp = hashvar(name);
768                 for (vp = *vpp ; vp && ! varequal(vp->text, name) ; vp = vp->next);
769                 if (vp == NULL) {
770                         if (strchr(name, '='))
771                                 setvareq(savestr(name), VSTRFIXED);
772                         else
773                                 setvar(name, NULL, VSTRFIXED);
774                         vp = *vpp;      /* the new variable */
775                         lvp->text = NULL;
776                         lvp->flags = VUNSET;
777                 } else {
778                         lvp->text = vp->text;
779                         lvp->flags = vp->flags;
780                         vp->flags |= VSTRFIXED|VTEXTFIXED;
781                         if (strchr(name, '='))
782                                 setvareq(savestr(name), 0);
783                 }
784         }
785         lvp->vp = vp;
786         lvp->next = localvars;
787         localvars = lvp;
788         INTON;
789 }
790
791
792 /*
793  * Called after a function returns.
794  */
795
796 void
797 poplocalvars(void)
798 {
799         struct localvar *lvp;
800         struct var *vp;
801
802         while ((lvp = localvars) != NULL) {
803                 localvars = lvp->next;
804                 vp = lvp->vp;
805                 if (vp == NULL) {       /* $- saved */
806                         memcpy(optlist, lvp->text, sizeof optlist);
807                         ckfree(lvp->text);
808                 } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
809                         (void)unsetvar(vp->text);
810                 } else {
811                         if ((vp->flags & VTEXTFIXED) == 0)
812                                 ckfree(vp->text);
813                         vp->flags = lvp->flags;
814                         vp->text = lvp->text;
815                 }
816                 ckfree(lvp);
817         }
818 }
819
820
821 int
822 setvarcmd(int argc, char **argv)
823 {
824         if (argc <= 2)
825                 return unsetcmd(argc, argv);
826         else if (argc == 3)
827                 setvar(argv[1], argv[2], 0);
828         else
829                 error("List assignment not implemented");
830         return 0;
831 }
832
833
834 /*
835  * The unset builtin command.  We unset the function before we unset the
836  * variable to allow a function to be unset when there is a readonly variable
837  * with the same name.
838  */
839
840 int
841 unsetcmd(int argc __unused, char **argv __unused)
842 {
843         char **ap;
844         int i;
845         int flg_func = 0;
846         int flg_var = 0;
847         int ret = 0;
848
849         while ((i = nextopt("vf")) != '\0') {
850                 if (i == 'f')
851                         flg_func = 1;
852                 else
853                         flg_var = 1;
854         }
855         if (flg_func == 0 && flg_var == 0)
856                 flg_var = 1;
857
858         for (ap = argptr; *ap ; ap++) {
859                 if (flg_func)
860                         ret |= unsetfunc(*ap);
861                 if (flg_var)
862                         ret |= unsetvar(*ap);
863         }
864         return ret;
865 }
866
867
868 /*
869  * Unset the specified variable.
870  */
871
872 int
873 unsetvar(const char *s)
874 {
875         struct var **vpp;
876         struct var *vp;
877
878         vpp = hashvar(s);
879         for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
880                 if (varequal(vp->text, s)) {
881                         if (vp->flags & VREADONLY)
882                                 return (1);
883                         INTOFF;
884                         if (*(strchr(vp->text, '=') + 1) != '\0')
885                                 setvar(s, nullstr, 0);
886                         if ((vp->flags & VEXPORT) && localevar(vp->text)) {
887                                 change_env(s, 0);
888                                 setlocale(LC_ALL, "");
889                         }
890                         vp->flags &= ~VEXPORT;
891                         vp->flags |= VUNSET;
892                         if ((vp->flags & VSTRFIXED) == 0) {
893                                 if ((vp->flags & VTEXTFIXED) == 0)
894                                         ckfree(vp->text);
895                                 *vpp = vp->next;
896                                 ckfree(vp);
897                         }
898                         INTON;
899                         return (0);
900                 }
901         }
902
903         return (0);
904 }
905
906
907
908 /*
909  * Find the appropriate entry in the hash table from the name.
910  */
911
912 STATIC struct var **
913 hashvar(const char *p)
914 {
915         unsigned int hashval;
916
917         hashval = ((unsigned char) *p) << 4;
918         while (*p && *p != '=')
919                 hashval += (unsigned char) *p++;
920         return &vartab[hashval % VTABSIZE];
921 }
922
923
924
925 /*
926  * Returns true if the two strings specify the same varable.  The first
927  * variable name is terminated by '='; the second may be terminated by
928  * either '=' or '\0'.
929  */
930
931 STATIC int
932 varequal(const char *p, const char *q)
933 {
934         while (*p == *q++) {
935                 if (*p++ == '=')
936                         return 1;
937         }
938         if (*p == '=' && *(q - 1) == '\0')
939                 return 1;
940         return 0;
941 }