VirtualBox

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

Last change on this file since 3646 was 3569, checked in by bird, 3 years ago

kash: Some variable handling fixes that showed up when using 'local' on stuff in a shell function (RemoveDirFromPath in vbox' tools/env.sh).

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