VirtualBox

source: kBuild/trunk/src/kash/var.c@ 2427

Last change on this file since 2427 was 2393, checked in by bird, 15 years ago

kash: prompt.

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette