source: trunk/src/kash/var.c@ 1209

Last change on this file since 1209 was 1209, checked in by bird, 18 years ago

var.c ++.

  • Property svn:eol-style set to native
File size: 19.9 KB
Line 
1/* $NetBSD: var.c,v 1.36 2004/10/06 10:23:43 enami Exp $ */
2
3/*-
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Kenneth Almquist.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35#ifdef HAVE_SYS_CDEFS_H
36#include <sys/cdefs.h>
37#endif
38#ifndef lint
39#if 0
40static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 5/4/95";
41#else
42__RCSID("$NetBSD: var.c,v 1.36 2004/10/06 10:23:43 enami Exp $");
43#endif
44#endif /* not lint */
45
46#include <unistd.h>
47#include <stddef.h>
48#include <stdlib.h>
49#include <strings.h>
50#ifndef __sun__
51#include <paths.h>
52#else
53#define _PATH_DEFPATH "/usr/bin:/usr/sbin"
54#include <iso/limits_iso.h>
55#endif
56
57#ifdef PC_OS2_LIBPATHS
58#define INCL_BASE
59#include <os2.h>
60
61#ifndef LIBPATHSTRICT
62#define LIBPATHSTRICT 3
63#endif
64
65extern APIRET
66#ifdef APIENTRY
67 APIENTRY
68#endif
69 DosQueryHeaderInfo(HMODULE hmod, ULONG ulIndex, PVOID pvBuffer, ULONG cbBuffer, ULONG ulSubFunction);
70#define QHINF_EXEINFO 1 /* NE exeinfo. */
71#define QHINF_READRSRCTBL 2 /* Reads from the resource table. */
72#define QHINF_READFILE 3 /* Reads from the executable file. */
73#define QHINF_LIBPATHLENGTH 4 /* Gets the libpath length. */
74#define QHINF_LIBPATH 5 /* Gets the entire libpath. */
75#define QHINF_FIXENTRY 6 /* NE only */
76#define QHINF_STE 7 /* NE only */
77#define QHINF_MAPSEL 8 /* NE only */
78
79#endif
80
81
82/*
83 * Shell variables.
84 */
85
86#include "shell.h"
87#include "output.h"
88#include "expand.h"
89#include "nodes.h" /* for other headers */
90#include "eval.h" /* defines cmdenviron */
91#include "exec.h"
92#include "syntax.h"
93#include "options.h"
94#include "mail.h"
95#include "var.h"
96#include "memalloc.h"
97#include "error.h"
98#include "mystring.h"
99#include "parser.h"
100#include "show.h"
101#ifndef SMALL
102#include "myhistedit.h"
103#endif
104#include "shinstance.h"
105
106//#ifdef SMALL
107//#define VTABSIZE 39
108//#else
109//#define VTABSIZE 517
110//#endif
111
112
113struct varinit {
114 unsigned var_off;
115 int flags;
116 const char *text;
117 void (*func)(shinstance *, const char *);
118};
119
120
121//#if ATTY
122//struct var vatty;
123//#endif
124//#ifndef SMALL
125//struct var vhistsize;
126//struct var vterm;
127//#endif
128//struct var vifs;
129//struct var vmail;
130//struct var vmpath;
131//struct var vpath;
132//#ifdef _MSC_VER
133//struct var vpath2;
134//#endif
135//struct var vps1;
136//struct var vps2;
137//struct var vps4;
138//struct var vvers; - unused
139//struct var voptind;
140
141#ifdef PC_OS2_LIBPATHS
142//static struct var libpath_vars[4];
143static const char * const libpath_envs[4] = {"LIBPATH=", "BEGINLIBPATH=", "ENDLIBPATH=", "LIBPATHSTRICT="};
144#endif
145
146const struct varinit varinit[] = {
147#if ATTY
148 { offsetof(shinstance, vatty), VSTRFIXED|VTEXTFIXED|VUNSET, "ATTY=",
149 NULL },
150#endif
151#ifndef SMALL
152 { offsetof(shinstance, vhistsize), VSTRFIXED|VTEXTFIXED|VUNSET, "HISTSIZE=",
153 sethistsize },
154#endif
155 { offsetof(shinstance, vifs), VSTRFIXED|VTEXTFIXED, "IFS= \t\n",
156 NULL },
157 { offsetof(shinstance, vmail), VSTRFIXED|VTEXTFIXED|VUNSET, "MAIL=",
158 NULL },
159 { offsetof(shinstance, vmpath), VSTRFIXED|VTEXTFIXED|VUNSET, "MAILPATH=",
160 NULL },
161 { offsetof(shinstance, vpath), VSTRFIXED|VTEXTFIXED, "PATH=" _PATH_DEFPATH,
162 changepath },
163#ifdef _MSC_VER
164 { offsetof(shinstance, vpath2), VSTRFIXED|VTEXTFIXED, "Path=",
165 changepath },
166#endif
167 /*
168 * vps1 depends on uid
169 */
170 { offsetof(shinstance, vps2), VSTRFIXED|VTEXTFIXED, "PS2=> ",
171 NULL },
172 { offsetof(shinstance, vps4), VSTRFIXED|VTEXTFIXED, "PS4=+ ",
173 NULL },
174#ifndef SMALL
175 { offsetof(shinstance, vterm), VSTRFIXED|VTEXTFIXED|VUNSET, "TERM=",
176 setterm },
177#endif
178 { offsetof(shinstance, voptind), VSTRFIXED|VTEXTFIXED|VNOFUNC, "OPTIND=1",
179 getoptsreset },
180 { 0, 0, NULL,
181 NULL }
182};
183
184//struct var *vartab[VTABSIZE];
185
186STATIC int strequal(const char *, const char *);
187STATIC struct var *find_var(shinstance *, const char *, struct var ***, int *);
188
189/*
190 * Initialize the varable symbol tables and import the environment
191 */
192
193#ifdef mkinit
194INCLUDE "var.h"
195MKINIT char **environ;
196INIT {
197 char **envp;
198
199 initvar(psh);
200 for (envp = environ ; *envp ; envp++) {
201 if (strchr(*envp, '=')) {
202 setvareq(psh, *envp, VEXPORT|VTEXTFIXED);
203 }
204 }
205}
206#endif
207
208
209/*
210 * This routine initializes the builtin variables. It is called when the
211 * shell is initialized and again when a shell procedure is spawned.
212 */
213
214void
215initvar(shinstance *psh)
216{
217 const struct varinit *ip;
218 struct var *vp;
219 struct var **vpp;
220#ifdef PC_OS2_LIBPATHS
221 char *psz = ckmalloc(2048);
222 int rc;
223 int i;
224
225 for (i = 0; i < 4; i++)
226 {
227 psh->libpath_vars[i].flags = VSTRFIXED | VOS2LIBPATH;
228 psh->libpath_vars[i].func = NULL;
229
230 if (i > 0)
231 {
232 psz[0] = psz[1] = psz[2] = psz[3] = '\0';
233 rc = DosQueryExtLIBPATH(psz, i);
234 }
235 else
236 {
237 rc = DosQueryHeaderInfo(NULLHANDLE, 0, psz, 2048, QHINF_LIBPATH);
238 psh->libpath_vars[i].flags |= VREADONLY;
239 }
240 if (!rc && *psz)
241 {
242 int cch1 = strlen(libpath_envs[i]);
243 int cch2 = strlen(psz) + 1;
244 psh->libpath_vars[i].text = ckmalloc(cch1 + cch2);
245 memcpy(psh->libpath_vars[i].text, libpath_envs[i], cch1);
246 memcpy(psh->libpath_vars[i].text + cch1, psz, cch2);
247 }
248 else
249 {
250 psh->libpath_vars[i].flags |= VUNSET | VTEXTFIXED;
251 psh->libpath_vars[i].text = (char*)libpath_envs[i];
252 }
253 if (find_var(psh, psh->libpath_vars[i].text, &vpp, &psh->libpath_vars[i].name_len) != NULL)
254 continue;
255 psh->libpath_vars[i].next = *vpp;
256 *vpp = &psh->libpath_vars[i];
257 }
258 free(psz);
259#endif
260
261 for (ip = varinit; ip->text; ip++) {
262 vp = (struct var *)((char *)psh + ip->var_off);
263 if (find_var(psh, ip->text, &vpp, &vp->name_len) != NULL)
264 continue;
265 vp->next = *vpp;
266 *vpp = vp;
267 vp->text = strdup(ip->text);
268 vp->flags = ip->flags;
269 vp->func = ip->func;
270 }
271 /*
272 * PS1 depends on uid
273 */
274 if (find_var(psh, "PS1", &vpp, &psh->vps1.name_len) == NULL) {
275 psh->vps1.next = *vpp;
276 *vpp = &psh->vps1;
277 psh->vps1.text = strdup(sh_geteuid(psh) ? "PS1=$ " : "PS1=# ");
278 psh->vps1.flags = VSTRFIXED|VTEXTFIXED;
279 }
280}
281
282/*
283 * Safe version of setvar, returns 1 on success 0 on failure.
284 */
285
286int
287setvarsafe(shinstance *psh, const char *name, const char *val, int flags)
288{
289 struct jmploc jmploc;
290 struct jmploc *volatile savehandler = psh->handler;
291 int err = 0;
292#ifdef __GNUC__
293 (void) &err;
294#endif
295
296 if (setjmp(jmploc.loc))
297 err = 1;
298 else {
299 psh->handler = &jmploc;
300 setvar(psh, name, val, flags);
301 }
302 psh->handler = savehandler;
303 return err;
304}
305
306/*
307 * Set the value of a variable. The flags argument is ored with the
308 * flags of the variable. If val is NULL, the variable is unset.
309 */
310
311void
312setvar(shinstance *psh, const char *name, const char *val, int flags)
313{
314 const char *p;
315 const char *q;
316 char *d;
317 size_t len;
318 int namelen;
319 char *nameeq;
320 int isbad;
321
322 isbad = 0;
323 p = name;
324 if (! is_name(*p))
325 isbad = 1;
326 p++;
327 for (;;) {
328 if (! is_in_name(*p)) {
329 if (*p == '\0' || *p == '=')
330 break;
331 isbad = 1;
332 }
333 p++;
334 }
335 namelen = (int)(p - name);
336 if (isbad)
337 error(psh, "%.*s: bad variable name", namelen, name);
338 len = namelen + 2; /* 2 is space for '=' and '\0' */
339 if (val == NULL) {
340 flags |= VUNSET;
341 } else {
342 len += strlen(val);
343 }
344 d = nameeq = ckmalloc(len);
345 q = name;
346 while (--namelen >= 0)
347 *d++ = *q++;
348 *d++ = '=';
349 *d = '\0';
350 if (val)
351 scopy(val, d);
352 setvareq(psh, nameeq, flags);
353}
354
355
356
357/*
358 * Same as setvar except that the variable and value are passed in
359 * the first argument as name=value. Since the first argument will
360 * be actually stored in the table, it should not be a string that
361 * will go away.
362 */
363
364void
365setvareq(shinstance *psh, char *s, int flags)
366{
367 struct var *vp, **vpp;
368 int nlen;
369
370 if (aflag(psh))
371 flags |= VEXPORT;
372 vp = find_var(psh, s, &vpp, &nlen);
373 if (vp != NULL) {
374 if (vp->flags & VREADONLY)
375 error(psh, "%.*s: is read only", vp->name_len, s);
376 if (flags & VNOSET)
377 return;
378 INTOFF;
379
380 if (vp->func && (flags & VNOFUNC) == 0)
381 (*vp->func)(psh, s + vp->name_len + 1);
382
383 if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
384 ckfree(vp->text);
385
386 vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
387 vp->flags |= flags & ~VNOFUNC;
388 vp->text = s;
389#ifdef PC_OS2_LIBPATHS
390 if ((vp->flags & VOS2LIBPATH) && (vp->flags & VEXPORT))
391 vp->flags &= ~VEXPORT;
392#endif
393
394 /*
395 * We could roll this to a function, to handle it as
396 * a regular variable function callback, but why bother?
397 */
398 if (vp == &psh->vmpath || (vp == &psh->vmail && ! mpathset(psh)))
399 chkmail(psh, 1);
400 INTON;
401 return;
402 }
403 /* not found */
404 if (flags & VNOSET)
405 return;
406 vp = ckmalloc(sizeof (*vp));
407 vp->flags = flags & ~VNOFUNC;
408 vp->text = s;
409 vp->name_len = nlen;
410 vp->next = *vpp;
411 vp->func = NULL;
412 *vpp = vp;
413}
414
415
416
417/*
418 * Process a linked list of variable assignments.
419 */
420
421void
422listsetvar(shinstance *psh, struct strlist *list, int flags)
423{
424 struct strlist *lp;
425
426 INTOFF;
427 for (lp = list ; lp ; lp = lp->next) {
428 setvareq(psh, savestr(lp->text), flags);
429 }
430 INTON;
431}
432
433void
434listmklocal(shinstance *psh, struct strlist *list, int flags)
435{
436 struct strlist *lp;
437
438 for (lp = list ; lp ; lp = lp->next)
439 mklocal(psh, lp->text, flags);
440}
441
442
443/*
444 * Find the value of a variable. Returns NULL if not set.
445 */
446
447char *
448lookupvar(shinstance *psh, const char *name)
449{
450 struct var *v;
451
452 v = find_var(psh, name, NULL, NULL);
453 if (v == NULL || v->flags & VUNSET)
454 return NULL;
455 return v->text + v->name_len + 1;
456}
457
458
459
460/*
461 * Search the environment of a builtin command. If the second argument
462 * is nonzero, return the value of a variable even if it hasn't been
463 * exported.
464 */
465
466char *
467bltinlookup(shinstance *psh, const char *name, int doall)
468{
469 struct strlist *sp;
470 struct var *v;
471
472 for (sp = psh->cmdenviron ; sp ; sp = sp->next) {
473 if (strequal(sp->text, name))
474 return strchr(sp->text, '=') + 1;
475 }
476
477 v = find_var(psh, name, NULL, NULL);
478
479 if (v == NULL || v->flags & VUNSET || (!doall && !(v->flags & VEXPORT)))
480 return NULL;
481 return v->text + v->name_len + 1;
482}
483
484
485
486/*
487 * Generate a list of exported variables. This routine is used to construct
488 * the third argument to execve when executing a program.
489 */
490
491char **
492environment(shinstance *psh)
493{
494 int nenv;
495 struct var **vpp;
496 struct var *vp;
497 char **env;
498 char **ep;
499
500 nenv = 0;
501 for (vpp = psh->vartab ; vpp < psh->vartab + VTABSIZE ; vpp++) {
502 for (vp = *vpp ; vp ; vp = vp->next)
503 if (vp->flags & VEXPORT)
504 nenv++;
505 }
506 ep = env = stalloc(psh, (nenv + 1) * sizeof *env);
507 for (vpp = psh->vartab ; vpp < psh->vartab + VTABSIZE ; vpp++) {
508 for (vp = *vpp ; vp ; vp = vp->next)
509 if (vp->flags & VEXPORT)
510 *ep++ = vp->text;
511 }
512 *ep = NULL;
513
514#ifdef PC_OS2_LIBPATHS
515 /*
516 * Set the libpaths now as this is exec() time.
517 */
518 for (nenv = 0; nenv < 3; nenv++)
519 DosSetExtLIBPATH(strchr(psh->libpath_vars[nenv].text, '=') + 1, nenv);
520#endif
521
522 return env;
523}
524
525
526/*
527 * Called when a shell procedure is invoked to clear out nonexported
528 * variables. It is also necessary to reallocate variables of with
529 * VSTACK set since these are currently allocated on the stack.
530 */
531
532#ifdef mkinit
533void shprocvar(shinstance *psh);
534
535SHELLPROC {
536 shprocvar(psh);
537}
538#endif
539
540void
541shprocvar(shinstance *psh)
542{
543 struct var **vpp;
544 struct var *vp, **prev;
545
546 for (vpp = psh->vartab ; vpp < psh->vartab + VTABSIZE ; vpp++) {
547 for (prev = vpp ; (vp = *prev) != NULL ; ) {
548 if ((vp->flags & VEXPORT) == 0) {
549 *prev = vp->next;
550 if ((vp->flags & VTEXTFIXED) == 0)
551 ckfree(vp->text);
552 if ((vp->flags & VSTRFIXED) == 0)
553 ckfree(vp);
554 } else {
555 if (vp->flags & VSTACK) {
556 vp->text = savestr(vp->text);
557 vp->flags &=~ VSTACK;
558 }
559 prev = &vp->next;
560 }
561 }
562 }
563 initvar(psh);
564}
565
566
567
568/*
569 * Command to list all variables which are set. Currently this command
570 * is invoked from the set command when the set command is called without
571 * any variables.
572 */
573
574void
575print_quoted(shinstance *psh, const char *p)
576{
577 const char *q;
578
579 if (strcspn(p, "|&;<>()$`\\\"' \t\n*?[]#~=%") == strlen(p)) {
580 out1fmt(psh, "%s", p);
581 return;
582 }
583 while (*p) {
584 if (*p == '\'') {
585 out1fmt(psh, "\\'");
586 p++;
587 continue;
588 }
589 q = index(p, '\'');
590 if (!q) {
591 out1fmt(psh, "'%s'", p );
592 return;
593 }
594 out1fmt(psh, "'%.*s'", (int)(q - p), p );
595 p = q;
596 }
597}
598
599static int
600sort_var(const void *v_v1, const void *v_v2)
601{
602 const struct var * const *v1 = v_v1;
603 const struct var * const *v2 = v_v2;
604
605 /* XXX Will anyone notice we include the '=' of the shorter name? */
606 return strcoll((*v1)->text, (*v2)->text);
607}
608
609/*
610 * POSIX requires that 'set' (but not export or readonly) output the
611 * variables in lexicographic order - by the locale's collating order (sigh).
612 * Maybe we could keep them in an ordered balanced binary tree
613 * instead of hashed lists.
614 * For now just roll 'em through qsort for printing...
615 */
616
617int
618showvars(shinstance *psh, const char *name, int flag, int show_value)
619{
620 struct var **vpp;
621 struct var *vp;
622 const char *p;
623
624 static struct var **list; /* static in case we are interrupted */
625 static int list_len;
626 int count = 0;
627
628 if (!list) {
629 list_len = 32;
630 list = ckmalloc(list_len * sizeof(*list));
631 }
632
633 for (vpp = psh->vartab ; vpp < psh->vartab + VTABSIZE ; vpp++) {
634 for (vp = *vpp ; vp ; vp = vp->next) {
635 if (flag && !(vp->flags & flag))
636 continue;
637 if (vp->flags & VUNSET && !(show_value & 2))
638 continue;
639 if (count >= list_len) {
640 list = ckrealloc(list,
641 (list_len << 1) * sizeof(*list));
642 list_len <<= 1;
643 }
644 list[count++] = vp;
645 }
646 }
647
648 qsort(list, count, sizeof(*list), sort_var);
649
650 for (vpp = list; count--; vpp++) {
651 vp = *vpp;
652 if (name)
653 out1fmt(psh, "%s ", name);
654 for (p = vp->text ; *p != '=' ; p++)
655 out1c(psh, *p);
656 if (!(vp->flags & VUNSET) && show_value) {
657 out1fmt(psh, "=");
658 print_quoted(psh, ++p);
659 }
660 out1c(psh, '\n');
661 }
662 return 0;
663}
664
665
666
667/*
668 * The export and readonly commands.
669 */
670
671int
672exportcmd(shinstance *psh, int argc, char **argv)
673{
674 struct var *vp;
675 char *name;
676 const char *p;
677 int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
678 int pflag;
679
680 pflag = nextopt(psh, "p") == 'p' ? 3 : 0;
681 if (argc <= 1 || pflag) {
682 showvars(psh, pflag ? argv[0] : 0, flag, pflag );
683 return 0;
684 }
685
686 while ((name = *psh->argptr++) != NULL) {
687 if ((p = strchr(name, '=')) != NULL) {
688 p++;
689 } else {
690 vp = find_var(psh, name, NULL, NULL);
691 if (vp != NULL) {
692 vp->flags |= flag;
693 continue;
694 }
695 }
696 setvar(psh, name, p, flag);
697 }
698 return 0;
699}
700
701
702/*
703 * The "local" command.
704 */
705
706int
707localcmd(shinstance *psh, int argc, char **argv)
708{
709 char *name;
710
711 if (! in_function(psh))
712 error(psh, "Not in a function");
713 while ((name = *psh->argptr++) != NULL) {
714 mklocal(psh, name, 0);
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(shinstance *psh, const char *name, int flags)
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 char *p;
738 p = ckmalloc(sizeof_optlist);
739 lvp->text = memcpy(p, psh->optlist, sizeof_optlist);
740 vp = NULL;
741 } else {
742 vp = find_var(psh, name, &vpp, NULL);
743 if (vp == NULL) {
744 if (strchr(name, '='))
745 setvareq(psh, savestr(name), VSTRFIXED|flags);
746 else
747 setvar(psh, name, NULL, VSTRFIXED|flags);
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;
755 if (name[vp->name_len] == '=')
756 setvareq(psh, savestr(name), flags);
757 }
758 }
759 lvp->vp = vp;
760 lvp->next = psh->localvars;
761 psh->localvars = lvp;
762 INTON;
763}
764
765
766/*
767 * Called after a function returns.
768 */
769
770void
771poplocalvars(shinstance *psh)
772{
773 struct localvar *lvp;
774 struct var *vp;
775
776 while ((lvp = psh->localvars) != NULL) {
777 psh->localvars = lvp->next;
778 vp = lvp->vp;
779 TRACE((psh, "poplocalvar %s", vp ? vp->text : "-"));
780 if (vp == NULL) { /* $- saved */
781 memcpy(psh->optlist, lvp->text, sizeof_optlist);
782 ckfree(lvp->text);
783 } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
784 (void)unsetvar(psh, vp->text, 0);
785 } else {
786 if (vp->func && (vp->flags & VNOFUNC) == 0)
787 (*vp->func)(psh, lvp->text + vp->name_len + 1);
788 if ((vp->flags & VTEXTFIXED) == 0)
789 ckfree(vp->text);
790 vp->flags = lvp->flags;
791 vp->text = lvp->text;
792 }
793 ckfree(lvp);
794 }
795}
796
797
798int
799setvarcmd(shinstance *psh, int argc, char **argv)
800{
801 if (argc <= 2)
802 return unsetcmd(psh, argc, argv);
803 else if (argc == 3)
804 setvar(psh, argv[1], argv[2], 0);
805 else
806 error(psh, "List assignment not implemented");
807 return 0;
808}
809
810
811/*
812 * The unset builtin command. We unset the function before we unset the
813 * variable to allow a function to be unset when there is a readonly variable
814 * with the same name.
815 */
816
817int
818unsetcmd(shinstance *psh, int argc, char **argv)
819{
820 char **ap;
821 int i;
822 int flg_func = 0;
823 int flg_var = 0;
824 int ret = 0;
825
826 while ((i = nextopt(psh, "evf")) != '\0') {
827 if (i == 'f')
828 flg_func = 1;
829 else
830 flg_var = i;
831 }
832 if (flg_func == 0 && flg_var == 0)
833 flg_var = 1;
834
835 for (ap = psh->argptr; *ap ; ap++) {
836 if (flg_func)
837 ret |= unsetfunc(psh, *ap);
838 if (flg_var)
839 ret |= unsetvar(psh, *ap, flg_var == 'e');
840 }
841 return ret;
842}
843
844
845/*
846 * Unset the specified variable.
847 */
848
849int
850unsetvar(shinstance *psh, const char *s, int unexport)
851{
852 struct var **vpp;
853 struct var *vp;
854
855 vp = find_var(psh, s, &vpp, NULL);
856 if (vp == NULL)
857 return 1;
858
859 if (vp->flags & VREADONLY)
860 return (1);
861
862 INTOFF;
863 if (unexport) {
864 vp->flags &= ~VEXPORT;
865 } else {
866 if (vp->text[vp->name_len + 1] != '\0')
867 setvar(psh, s, nullstr, 0);
868 vp->flags &= ~VEXPORT;
869 vp->flags |= VUNSET;
870 if ((vp->flags & VSTRFIXED) == 0) {
871 if ((vp->flags & VTEXTFIXED) == 0)
872 ckfree(vp->text);
873 *vpp = vp->next;
874 ckfree(vp);
875 }
876 }
877 INTON;
878 return 0;
879}
880
881
882/*
883 * Returns true if the two strings specify the same varable. The first
884 * variable name is terminated by '='; the second may be terminated by
885 * either '=' or '\0'.
886 */
887
888STATIC int
889strequal(const char *p, const char *q)
890{
891 while (*p == *q++) {
892 if (*p++ == '=')
893 return 1;
894 }
895 if (*p == '=' && *(q - 1) == '\0')
896 return 1;
897 return 0;
898}
899
900/*
901 * Search for a variable.
902 * 'name' may be terminated by '=' or a NUL.
903 * vppp is set to the pointer to vp, or the list head if vp isn't found
904 * lenp is set to the number of charactets in 'name'
905 */
906
907STATIC struct var *
908find_var(shinstance *psh, const char *name, struct var ***vppp, int *lenp)
909{
910 unsigned int hashval;
911 int len;
912 struct var *vp, **vpp;
913 const char *p = name;
914
915 hashval = 0;
916 while (*p && *p != '=')
917 hashval = 2 * hashval + (unsigned char)*p++;
918 len = (int)(p - name);
919
920 if (lenp)
921 *lenp = len;
922 vpp = &psh->vartab[hashval % VTABSIZE];
923 if (vppp)
924 *vppp = vpp;
925
926 for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
927 if (vp->name_len != len)
928 continue;
929 if (memcmp(vp->text, name, len) != 0)
930 continue;
931 if (vppp)
932 *vppp = vpp;
933 return vp;
934 }
935 return NULL;
936}
Note: See TracBrowser for help on using the repository browser.