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
|
---|
37 | static 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 |
|
---|
55 | extern 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 |
|
---|
104 | struct 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];
|
---|
134 | static const char * const libpath_envs[4] = {"LIBPATH=", "BEGINLIBPATH=", "ENDLIBPATH=", "LIBPATHSTRICT="};
|
---|
135 | #endif
|
---|
136 |
|
---|
137 | const struct varinit varinit[] = {
|
---|
138 | #if ATTY
|
---|
139 | { offsetof(shinstance, vatty), VSTRFIXED|VTEXTFIXED|VUNSET, "ATTY=",
|
---|
140 | NULL },
|
---|
141 | #endif
|
---|
142 | #ifndef SMALL
|
---|
143 | { offsetof(shinstance, vhistsize), VSTRFIXED|VTEXTFIXED|VUNSET, "HISTSIZE=",
|
---|
144 | sethistsize },
|
---|
145 | #endif
|
---|
146 | { offsetof(shinstance, vifs), VSTRFIXED|VTEXTFIXED, "IFS= \t\n",
|
---|
147 | NULL },
|
---|
148 | { offsetof(shinstance, vmail), VSTRFIXED|VTEXTFIXED|VUNSET, "MAIL=",
|
---|
149 | NULL },
|
---|
150 | { offsetof(shinstance, vmpath), VSTRFIXED|VTEXTFIXED|VUNSET, "MAILPATH=",
|
---|
151 | NULL },
|
---|
152 | { offsetof(shinstance, vpath), VSTRFIXED|VTEXTFIXED, "PATH=" _PATH_DEFPATH,
|
---|
153 | changepath },
|
---|
154 | /*
|
---|
155 | * vps1 depends on uid
|
---|
156 | */
|
---|
157 | { offsetof(shinstance, vps2), VSTRFIXED|VTEXTFIXED, "PS2=> ",
|
---|
158 | NULL },
|
---|
159 | { offsetof(shinstance, vps4), VSTRFIXED|VTEXTFIXED, "PS4=+ ",
|
---|
160 | NULL },
|
---|
161 | #ifndef SMALL
|
---|
162 | { offsetof(shinstance, vterm), VSTRFIXED|VTEXTFIXED|VUNSET, "TERM=",
|
---|
163 | setterm },
|
---|
164 | #endif
|
---|
165 | { offsetof(shinstance, voptind), VSTRFIXED|VTEXTFIXED|VNOFUNC, "OPTIND=1",
|
---|
166 | getoptsreset },
|
---|
167 | { 0, 0, NULL,
|
---|
168 | NULL }
|
---|
169 | };
|
---|
170 |
|
---|
171 | //struct var *vartab[VTABSIZE];
|
---|
172 |
|
---|
173 | STATIC int strequal(const char *, const char *);
|
---|
174 | STATIC 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
|
---|
181 | INCLUDE "var.h"
|
---|
182 |
|
---|
183 | INIT {
|
---|
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 |
|
---|
201 | void
|
---|
202 | initvar(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 | 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|VTEXTFIXED;
|
---|
270 | }
|
---|
271 | }
|
---|
272 |
|
---|
273 | /*
|
---|
274 | * Safe version of setvar, returns 1 on success 0 on failure.
|
---|
275 | */
|
---|
276 |
|
---|
277 | int
|
---|
278 | setvarsafe(shinstance *psh, const char *name, const char *val, int flags)
|
---|
279 | {
|
---|
280 | struct jmploc jmploc;
|
---|
281 | struct jmploc *volatile savehandler = psh->handler;
|
---|
282 | int err = 0;
|
---|
283 | #ifdef __GNUC__
|
---|
284 | (void) &err;
|
---|
285 | #endif
|
---|
286 |
|
---|
287 | if (setjmp(jmploc.loc))
|
---|
288 | err = 1;
|
---|
289 | else {
|
---|
290 | psh->handler = &jmploc;
|
---|
291 | setvar(psh, name, val, flags);
|
---|
292 | }
|
---|
293 | psh->handler = savehandler;
|
---|
294 | return err;
|
---|
295 | }
|
---|
296 |
|
---|
297 | /*
|
---|
298 | * Set the value of a variable. The flags argument is ored with the
|
---|
299 | * flags of the variable. If val is NULL, the variable is unset.
|
---|
300 | */
|
---|
301 |
|
---|
302 | void
|
---|
303 | setvar(shinstance *psh, const char *name, const char *val, int flags)
|
---|
304 | {
|
---|
305 | const char *p;
|
---|
306 | const char *q;
|
---|
307 | char *d;
|
---|
308 | size_t len;
|
---|
309 | int namelen;
|
---|
310 | char *nameeq;
|
---|
311 | int isbad;
|
---|
312 |
|
---|
313 | isbad = 0;
|
---|
314 | p = name;
|
---|
315 | if (! is_name(*p))
|
---|
316 | isbad = 1;
|
---|
317 | p++;
|
---|
318 | for (;;) {
|
---|
319 | if (! is_in_name(*p)) {
|
---|
320 | if (*p == '\0' || *p == '=')
|
---|
321 | break;
|
---|
322 | isbad = 1;
|
---|
323 | }
|
---|
324 | p++;
|
---|
325 | }
|
---|
326 | namelen = (int)(p - name);
|
---|
327 | if (isbad)
|
---|
328 | error(psh, "%.*s: bad variable name", namelen, name);
|
---|
329 | len = namelen + 2; /* 2 is space for '=' and '\0' */
|
---|
330 | if (val == NULL) {
|
---|
331 | flags |= VUNSET;
|
---|
332 | } else {
|
---|
333 | len += strlen(val);
|
---|
334 | }
|
---|
335 | d = nameeq = ckmalloc(psh, len);
|
---|
336 | q = name;
|
---|
337 | while (--namelen >= 0)
|
---|
338 | *d++ = *q++;
|
---|
339 | *d++ = '=';
|
---|
340 | *d = '\0';
|
---|
341 | if (val)
|
---|
342 | scopy(val, d);
|
---|
343 | setvareq(psh, nameeq, flags);
|
---|
344 | }
|
---|
345 |
|
---|
346 |
|
---|
347 |
|
---|
348 | /*
|
---|
349 | * Same as setvar except that the variable and value are passed in
|
---|
350 | * the first argument as name=value. Since the first argument will
|
---|
351 | * be actually stored in the table, it should not be a string that
|
---|
352 | * will go away.
|
---|
353 | */
|
---|
354 |
|
---|
355 | void
|
---|
356 | setvareq(shinstance *psh, char *s, int flags)
|
---|
357 | {
|
---|
358 | struct var *vp, **vpp;
|
---|
359 | int nlen;
|
---|
360 |
|
---|
361 | fprintf(stderr, "debug: setvareq '%s'\n", s);
|
---|
362 | #if defined(_MSC_VER) || defined(_WIN32)
|
---|
363 | /* On Windows PATH is often spelled 'Path', correct this here. */
|
---|
364 | if ( s[0] == 'P'
|
---|
365 | && s[1] == 'a'
|
---|
366 | && s[2] == 't'
|
---|
367 | && s[3] == 'h'
|
---|
368 | && (s[4] == '\0' || s[4] == '=') ) {
|
---|
369 | s[1] = 'A';
|
---|
370 | s[2] = 'T';
|
---|
371 | s[3] = 'H';
|
---|
372 | fprintf(stderr, "debug: setvareq '%s' FIXED\n", s);
|
---|
373 | }
|
---|
374 | #endif
|
---|
375 |
|
---|
376 | if (aflag(psh))
|
---|
377 | flags |= VEXPORT;
|
---|
378 | vp = find_var(psh, s, &vpp, &nlen);
|
---|
379 | if (vp != NULL) {
|
---|
380 | if (vp->flags & VREADONLY)
|
---|
381 | error(psh, "%.*s: is read only", vp->name_len, s);
|
---|
382 | if (flags & VNOSET)
|
---|
383 | return;
|
---|
384 | INTOFF;
|
---|
385 |
|
---|
386 | if (vp->func && (flags & VNOFUNC) == 0)
|
---|
387 | (*vp->func)(psh, s + vp->name_len + 1);
|
---|
388 |
|
---|
389 | if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
|
---|
390 | ckfree(psh, vp->text);
|
---|
391 |
|
---|
392 | vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
|
---|
393 | vp->flags |= flags & ~VNOFUNC;
|
---|
394 | vp->text = s;
|
---|
395 | #ifdef PC_OS2_LIBPATHS
|
---|
396 | if ((vp->flags & VOS2LIBPATH) && (vp->flags & VEXPORT))
|
---|
397 | vp->flags &= ~VEXPORT;
|
---|
398 | #endif
|
---|
399 |
|
---|
400 | /*
|
---|
401 | * We could roll this to a function, to handle it as
|
---|
402 | * a regular variable function callback, but why bother?
|
---|
403 | */
|
---|
404 | if (vp == &psh->vmpath || (vp == &psh->vmail && ! mpathset(psh)))
|
---|
405 | chkmail(psh, 1);
|
---|
406 | INTON;
|
---|
407 | return;
|
---|
408 | }
|
---|
409 | /* not found */
|
---|
410 | if (flags & VNOSET)
|
---|
411 | return;
|
---|
412 |
|
---|
413 | vp = ckmalloc(psh, sizeof (*vp));
|
---|
414 | vp->flags = flags & ~VNOFUNC;
|
---|
415 | vp->text = s;
|
---|
416 | vp->name_len = nlen;
|
---|
417 | vp->next = *vpp;
|
---|
418 | vp->func = NULL;
|
---|
419 | *vpp = vp;
|
---|
420 | }
|
---|
421 |
|
---|
422 |
|
---|
423 |
|
---|
424 | /*
|
---|
425 | * Process a linked list of variable assignments.
|
---|
426 | */
|
---|
427 |
|
---|
428 | void
|
---|
429 | listsetvar(shinstance *psh, struct strlist *list, int flags)
|
---|
430 | {
|
---|
431 | struct strlist *lp;
|
---|
432 |
|
---|
433 | INTOFF;
|
---|
434 | for (lp = list ; lp ; lp = lp->next) {
|
---|
435 | setvareq(psh, savestr(psh, lp->text), flags);
|
---|
436 | }
|
---|
437 | INTON;
|
---|
438 | }
|
---|
439 |
|
---|
440 | void
|
---|
441 | listmklocal(shinstance *psh, struct strlist *list, int flags)
|
---|
442 | {
|
---|
443 | struct strlist *lp;
|
---|
444 |
|
---|
445 | for (lp = list ; lp ; lp = lp->next)
|
---|
446 | mklocal(psh, lp->text, flags);
|
---|
447 | }
|
---|
448 |
|
---|
449 |
|
---|
450 | /*
|
---|
451 | * Find the value of a variable. Returns NULL if not set.
|
---|
452 | */
|
---|
453 |
|
---|
454 | char *
|
---|
455 | lookupvar(shinstance *psh, const char *name)
|
---|
456 | {
|
---|
457 | struct var *v;
|
---|
458 |
|
---|
459 | v = find_var(psh, name, NULL, NULL);
|
---|
460 | if (v == NULL || v->flags & VUNSET)
|
---|
461 | return NULL;
|
---|
462 | return v->text + v->name_len + 1;
|
---|
463 | }
|
---|
464 |
|
---|
465 |
|
---|
466 |
|
---|
467 | /*
|
---|
468 | * Search the environment of a builtin command. If the second argument
|
---|
469 | * is nonzero, return the value of a variable even if it hasn't been
|
---|
470 | * exported.
|
---|
471 | */
|
---|
472 |
|
---|
473 | char *
|
---|
474 | bltinlookup(shinstance *psh, const char *name, int doall)
|
---|
475 | {
|
---|
476 | struct strlist *sp;
|
---|
477 | struct var *v;
|
---|
478 |
|
---|
479 | for (sp = psh->cmdenviron ; sp ; sp = sp->next) {
|
---|
480 | if (strequal(sp->text, name))
|
---|
481 | return strchr(sp->text, '=') + 1;
|
---|
482 | }
|
---|
483 |
|
---|
484 | v = find_var(psh, name, NULL, NULL);
|
---|
485 |
|
---|
486 | if (v == NULL || v->flags & VUNSET || (!doall && !(v->flags & VEXPORT)))
|
---|
487 | return NULL;
|
---|
488 | return v->text + v->name_len + 1;
|
---|
489 | }
|
---|
490 |
|
---|
491 |
|
---|
492 |
|
---|
493 | /*
|
---|
494 | * Generate a list of exported variables. This routine is used to construct
|
---|
495 | * the third argument to execve when executing a program.
|
---|
496 | */
|
---|
497 |
|
---|
498 | char **
|
---|
499 | environment(shinstance *psh)
|
---|
500 | {
|
---|
501 | int nenv;
|
---|
502 | struct var **vpp;
|
---|
503 | struct var *vp;
|
---|
504 | char **env;
|
---|
505 | char **ep;
|
---|
506 |
|
---|
507 | nenv = 0;
|
---|
508 | for (vpp = psh->vartab ; vpp < psh->vartab + VTABSIZE ; vpp++) {
|
---|
509 | for (vp = *vpp ; vp ; vp = vp->next)
|
---|
510 | if (vp->flags & VEXPORT)
|
---|
511 | nenv++;
|
---|
512 | }
|
---|
513 | ep = env = stalloc(psh, (nenv + 1) * sizeof *env);
|
---|
514 | for (vpp = psh->vartab ; vpp < psh->vartab + VTABSIZE ; vpp++) {
|
---|
515 | for (vp = *vpp ; vp ; vp = vp->next)
|
---|
516 | if (vp->flags & VEXPORT)
|
---|
517 | *ep++ = vp->text;
|
---|
518 | }
|
---|
519 | *ep = NULL;
|
---|
520 |
|
---|
521 | #ifdef PC_OS2_LIBPATHS
|
---|
522 | /*
|
---|
523 | * Set the libpaths now as this is exec() time.
|
---|
524 | */
|
---|
525 | for (nenv = 0; nenv < 3; nenv++)
|
---|
526 | DosSetExtLIBPATH(strchr(psh->libpath_vars[nenv].text, '=') + 1, nenv);
|
---|
527 | #endif
|
---|
528 |
|
---|
529 | return env;
|
---|
530 | }
|
---|
531 |
|
---|
532 |
|
---|
533 | /*
|
---|
534 | * Called when a shell procedure is invoked to clear out nonexported
|
---|
535 | * variables. It is also necessary to reallocate variables of with
|
---|
536 | * VSTACK set since these are currently allocated on the stack.
|
---|
537 | */
|
---|
538 |
|
---|
539 | #ifdef mkinit
|
---|
540 | void shprocvar(shinstance *psh);
|
---|
541 |
|
---|
542 | SHELLPROC {
|
---|
543 | shprocvar(psh);
|
---|
544 | }
|
---|
545 | #endif
|
---|
546 |
|
---|
547 | void
|
---|
548 | shprocvar(shinstance *psh)
|
---|
549 | {
|
---|
550 | struct var **vpp;
|
---|
551 | struct var *vp, **prev;
|
---|
552 |
|
---|
553 | for (vpp = psh->vartab ; vpp < psh->vartab + VTABSIZE ; vpp++) {
|
---|
554 | for (prev = vpp ; (vp = *prev) != NULL ; ) {
|
---|
555 | if ((vp->flags & VEXPORT) == 0) {
|
---|
556 | *prev = vp->next;
|
---|
557 | if ((vp->flags & VTEXTFIXED) == 0)
|
---|
558 | ckfree(psh, vp->text);
|
---|
559 | if ((vp->flags & VSTRFIXED) == 0)
|
---|
560 | ckfree(psh, vp);
|
---|
561 | } else {
|
---|
562 | if (vp->flags & VSTACK) {
|
---|
563 | vp->text = savestr(psh, vp->text);
|
---|
564 | vp->flags &=~ VSTACK;
|
---|
565 | }
|
---|
566 | prev = &vp->next;
|
---|
567 | }
|
---|
568 | }
|
---|
569 | }
|
---|
570 | initvar(psh);
|
---|
571 | }
|
---|
572 |
|
---|
573 |
|
---|
574 |
|
---|
575 | /*
|
---|
576 | * Command to list all variables which are set. Currently this command
|
---|
577 | * is invoked from the set command when the set command is called without
|
---|
578 | * any variables.
|
---|
579 | */
|
---|
580 |
|
---|
581 | void
|
---|
582 | print_quoted(shinstance *psh, const char *p)
|
---|
583 | {
|
---|
584 | const char *q;
|
---|
585 |
|
---|
586 | if (strcspn(p, "|&;<>()$`\\\"' \t\n*?[]#~=%") == strlen(p)) {
|
---|
587 | out1fmt(psh, "%s", p);
|
---|
588 | return;
|
---|
589 | }
|
---|
590 | while (*p) {
|
---|
591 | if (*p == '\'') {
|
---|
592 | out1fmt(psh, "\\'");
|
---|
593 | p++;
|
---|
594 | continue;
|
---|
595 | }
|
---|
596 | q = strchr(p, '\'');
|
---|
597 | if (!q) {
|
---|
598 | out1fmt(psh, "'%s'", p );
|
---|
599 | return;
|
---|
600 | }
|
---|
601 | out1fmt(psh, "'%.*s'", (int)(q - p), p );
|
---|
602 | p = q;
|
---|
603 | }
|
---|
604 | }
|
---|
605 |
|
---|
606 | static int
|
---|
607 | sort_var(const void *v_v1, const void *v_v2)
|
---|
608 | {
|
---|
609 | const struct var * const *v1 = v_v1;
|
---|
610 | const struct var * const *v2 = v_v2;
|
---|
611 |
|
---|
612 | /* XXX Will anyone notice we include the '=' of the shorter name? */
|
---|
613 | return strcoll((*v1)->text, (*v2)->text);
|
---|
614 | }
|
---|
615 |
|
---|
616 | /*
|
---|
617 | * POSIX requires that 'set' (but not export or readonly) output the
|
---|
618 | * variables in lexicographic order - by the locale's collating order (sigh).
|
---|
619 | * Maybe we could keep them in an ordered balanced binary tree
|
---|
620 | * instead of hashed lists.
|
---|
621 | * For now just roll 'em through qsort for printing...
|
---|
622 | */
|
---|
623 |
|
---|
624 | int
|
---|
625 | showvars(shinstance *psh, const char *name, int flag, int show_value)
|
---|
626 | {
|
---|
627 | struct var **vpp;
|
---|
628 | struct var *vp;
|
---|
629 | const char *p;
|
---|
630 |
|
---|
631 | static struct var **list; /* static in case we are interrupted */
|
---|
632 | static int list_len;
|
---|
633 | int count = 0;
|
---|
634 |
|
---|
635 | if (!list) {
|
---|
636 | list_len = 32;
|
---|
637 | list = ckmalloc(psh, list_len * sizeof(*list));
|
---|
638 | }
|
---|
639 |
|
---|
640 | for (vpp = psh->vartab ; vpp < psh->vartab + VTABSIZE ; vpp++) {
|
---|
641 | for (vp = *vpp ; vp ; vp = vp->next) {
|
---|
642 | if (flag && !(vp->flags & flag))
|
---|
643 | continue;
|
---|
644 | if (vp->flags & VUNSET && !(show_value & 2))
|
---|
645 | continue;
|
---|
646 | if (count >= list_len) {
|
---|
647 | list = ckrealloc(psh, list,
|
---|
648 | (list_len << 1) * sizeof(*list));
|
---|
649 | list_len <<= 1;
|
---|
650 | }
|
---|
651 | list[count++] = vp;
|
---|
652 | }
|
---|
653 | }
|
---|
654 |
|
---|
655 | qsort(list, count, sizeof(*list), sort_var);
|
---|
656 |
|
---|
657 | for (vpp = list; count--; vpp++) {
|
---|
658 | vp = *vpp;
|
---|
659 | if (name)
|
---|
660 | out1fmt(psh, "%s ", name);
|
---|
661 | for (p = vp->text ; *p != '=' ; p++)
|
---|
662 | out1c(psh, *p);
|
---|
663 | if (!(vp->flags & VUNSET) && show_value) {
|
---|
664 | out1fmt(psh, "=");
|
---|
665 | print_quoted(psh, ++p);
|
---|
666 | }
|
---|
667 | out1c(psh, '\n');
|
---|
668 | }
|
---|
669 | return 0;
|
---|
670 | }
|
---|
671 |
|
---|
672 |
|
---|
673 |
|
---|
674 | /*
|
---|
675 | * The export and readonly commands.
|
---|
676 | */
|
---|
677 |
|
---|
678 | int
|
---|
679 | exportcmd(shinstance *psh, int argc, char **argv)
|
---|
680 | {
|
---|
681 | struct var *vp;
|
---|
682 | char *name;
|
---|
683 | const char *p;
|
---|
684 | int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
|
---|
685 | int pflag;
|
---|
686 |
|
---|
687 | pflag = nextopt(psh, "p") == 'p' ? 3 : 0;
|
---|
688 | if (argc <= 1 || pflag) {
|
---|
689 | showvars(psh, pflag ? argv[0] : 0, flag, pflag );
|
---|
690 | return 0;
|
---|
691 | }
|
---|
692 |
|
---|
693 | while ((name = *psh->argptr++) != NULL) {
|
---|
694 | if ((p = strchr(name, '=')) != NULL) {
|
---|
695 | p++;
|
---|
696 | } else {
|
---|
697 | vp = find_var(psh, name, NULL, NULL);
|
---|
698 | if (vp != NULL) {
|
---|
699 | vp->flags |= flag;
|
---|
700 | continue;
|
---|
701 | }
|
---|
702 | }
|
---|
703 | setvar(psh, name, p, flag);
|
---|
704 | }
|
---|
705 | return 0;
|
---|
706 | }
|
---|
707 |
|
---|
708 |
|
---|
709 | /*
|
---|
710 | * The "local" command.
|
---|
711 | */
|
---|
712 |
|
---|
713 | int
|
---|
714 | localcmd(shinstance *psh, int argc, char **argv)
|
---|
715 | {
|
---|
716 | char *name;
|
---|
717 |
|
---|
718 | if (! in_function(psh))
|
---|
719 | error(psh, "Not in a function");
|
---|
720 | while ((name = *psh->argptr++) != NULL) {
|
---|
721 | mklocal(psh, name, 0);
|
---|
722 | }
|
---|
723 | return 0;
|
---|
724 | }
|
---|
725 |
|
---|
726 |
|
---|
727 | /*
|
---|
728 | * Make a variable a local variable. When a variable is made local, it's
|
---|
729 | * value and flags are saved in a localvar structure. The saved values
|
---|
730 | * will be restored when the shell function returns. We handle the name
|
---|
731 | * "-" as a special case.
|
---|
732 | */
|
---|
733 |
|
---|
734 | void
|
---|
735 | mklocal(shinstance *psh, const char *name, int flags)
|
---|
736 | {
|
---|
737 | struct localvar *lvp;
|
---|
738 | struct var **vpp;
|
---|
739 | struct var *vp;
|
---|
740 |
|
---|
741 | INTOFF;
|
---|
742 | lvp = ckmalloc(psh, sizeof (struct localvar));
|
---|
743 | if (name[0] == '-' && name[1] == '\0') {
|
---|
744 | char *p;
|
---|
745 | p = ckmalloc(psh, sizeof_optlist);
|
---|
746 | lvp->text = memcpy(p, psh->optlist, sizeof_optlist);
|
---|
747 | vp = NULL;
|
---|
748 | } else {
|
---|
749 | vp = find_var(psh, name, &vpp, NULL);
|
---|
750 | if (vp == NULL) {
|
---|
751 | if (strchr(name, '='))
|
---|
752 | setvareq(psh, savestr(psh, name), VSTRFIXED|flags);
|
---|
753 | else
|
---|
754 | setvar(psh, name, NULL, VSTRFIXED|flags);
|
---|
755 | vp = *vpp; /* the new variable */
|
---|
756 | lvp->text = NULL;
|
---|
757 | lvp->flags = VUNSET;
|
---|
758 | } else {
|
---|
759 | lvp->text = vp->text;
|
---|
760 | lvp->flags = vp->flags;
|
---|
761 | vp->flags |= VSTRFIXED|VTEXTFIXED;
|
---|
762 | if (name[vp->name_len] == '=')
|
---|
763 | setvareq(psh, savestr(psh, name), flags);
|
---|
764 | }
|
---|
765 | }
|
---|
766 | lvp->vp = vp;
|
---|
767 | lvp->next = psh->localvars;
|
---|
768 | psh->localvars = lvp;
|
---|
769 | INTON;
|
---|
770 | }
|
---|
771 |
|
---|
772 |
|
---|
773 | /*
|
---|
774 | * Called after a function returns.
|
---|
775 | */
|
---|
776 |
|
---|
777 | void
|
---|
778 | poplocalvars(shinstance *psh)
|
---|
779 | {
|
---|
780 | struct localvar *lvp;
|
---|
781 | struct var *vp;
|
---|
782 |
|
---|
783 | while ((lvp = psh->localvars) != NULL) {
|
---|
784 | psh->localvars = lvp->next;
|
---|
785 | vp = lvp->vp;
|
---|
786 | TRACE((psh, "poplocalvar %s", vp ? vp->text : "-"));
|
---|
787 | if (vp == NULL) { /* $- saved */
|
---|
788 | memcpy(psh->optlist, lvp->text, sizeof_optlist);
|
---|
789 | ckfree(psh, lvp->text);
|
---|
790 | } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
|
---|
791 | (void)unsetvar(psh, vp->text, 0);
|
---|
792 | } else {
|
---|
793 | if (vp->func && (vp->flags & VNOFUNC) == 0)
|
---|
794 | (*vp->func)(psh, lvp->text + vp->name_len + 1);
|
---|
795 | if ((vp->flags & VTEXTFIXED) == 0)
|
---|
796 | ckfree(psh, vp->text);
|
---|
797 | vp->flags = lvp->flags;
|
---|
798 | vp->text = lvp->text;
|
---|
799 | }
|
---|
800 | ckfree(psh, lvp);
|
---|
801 | }
|
---|
802 | }
|
---|
803 |
|
---|
804 |
|
---|
805 | int
|
---|
806 | setvarcmd(shinstance *psh, int argc, char **argv)
|
---|
807 | {
|
---|
808 | if (argc <= 2)
|
---|
809 | return unsetcmd(psh, argc, argv);
|
---|
810 | else if (argc == 3)
|
---|
811 | setvar(psh, argv[1], argv[2], 0);
|
---|
812 | else
|
---|
813 | error(psh, "List assignment not implemented");
|
---|
814 | return 0;
|
---|
815 | }
|
---|
816 |
|
---|
817 |
|
---|
818 | /*
|
---|
819 | * The unset builtin command. We unset the function before we unset the
|
---|
820 | * variable to allow a function to be unset when there is a readonly variable
|
---|
821 | * with the same name.
|
---|
822 | */
|
---|
823 |
|
---|
824 | int
|
---|
825 | unsetcmd(shinstance *psh, int argc, char **argv)
|
---|
826 | {
|
---|
827 | char **ap;
|
---|
828 | int i;
|
---|
829 | int flg_func = 0;
|
---|
830 | int flg_var = 0;
|
---|
831 | int ret = 0;
|
---|
832 |
|
---|
833 | while ((i = nextopt(psh, "evf")) != '\0') {
|
---|
834 | if (i == 'f')
|
---|
835 | flg_func = 1;
|
---|
836 | else
|
---|
837 | flg_var = i;
|
---|
838 | }
|
---|
839 | if (flg_func == 0 && flg_var == 0)
|
---|
840 | flg_var = 1;
|
---|
841 |
|
---|
842 | for (ap = psh->argptr; *ap ; ap++) {
|
---|
843 | if (flg_func)
|
---|
844 | ret |= unsetfunc(psh, *ap);
|
---|
845 | if (flg_var)
|
---|
846 | ret |= unsetvar(psh, *ap, flg_var == 'e');
|
---|
847 | }
|
---|
848 | return ret;
|
---|
849 | }
|
---|
850 |
|
---|
851 |
|
---|
852 | /*
|
---|
853 | * Unset the specified variable.
|
---|
854 | */
|
---|
855 |
|
---|
856 | int
|
---|
857 | unsetvar(shinstance *psh, const char *s, int unexport)
|
---|
858 | {
|
---|
859 | struct var **vpp;
|
---|
860 | struct var *vp;
|
---|
861 |
|
---|
862 | vp = find_var(psh, s, &vpp, NULL);
|
---|
863 | if (vp == NULL)
|
---|
864 | return 1;
|
---|
865 |
|
---|
866 | if (vp->flags & VREADONLY)
|
---|
867 | return (1);
|
---|
868 |
|
---|
869 | INTOFF;
|
---|
870 | if (unexport) {
|
---|
871 | vp->flags &= ~VEXPORT;
|
---|
872 | } else {
|
---|
873 | if (vp->text[vp->name_len + 1] != '\0')
|
---|
874 | setvar(psh, s, nullstr, 0);
|
---|
875 | vp->flags &= ~VEXPORT;
|
---|
876 | vp->flags |= VUNSET;
|
---|
877 | if ((vp->flags & VSTRFIXED) == 0) {
|
---|
878 | if ((vp->flags & VTEXTFIXED) == 0)
|
---|
879 | ckfree(psh, vp->text);
|
---|
880 | *vpp = vp->next;
|
---|
881 | ckfree(psh, vp);
|
---|
882 | }
|
---|
883 | }
|
---|
884 | INTON;
|
---|
885 | return 0;
|
---|
886 | }
|
---|
887 |
|
---|
888 |
|
---|
889 | /*
|
---|
890 | * Returns true if the two strings specify the same varable. The first
|
---|
891 | * variable name is terminated by '='; the second may be terminated by
|
---|
892 | * either '=' or '\0'.
|
---|
893 | */
|
---|
894 |
|
---|
895 | STATIC int
|
---|
896 | strequal(const char *p, const char *q)
|
---|
897 | {
|
---|
898 | while (*p == *q++) {
|
---|
899 | if (*p++ == '=')
|
---|
900 | return 1;
|
---|
901 | }
|
---|
902 | if (*p == '=' && *(q - 1) == '\0')
|
---|
903 | return 1;
|
---|
904 | return 0;
|
---|
905 | }
|
---|
906 |
|
---|
907 | /*
|
---|
908 | * Search for a variable.
|
---|
909 | * 'name' may be terminated by '=' or a NUL.
|
---|
910 | * vppp is set to the pointer to vp, or the list head if vp isn't found
|
---|
911 | * lenp is set to the number of charactets in 'name'
|
---|
912 | */
|
---|
913 |
|
---|
914 | STATIC struct var *
|
---|
915 | find_var(shinstance *psh, const char *name, struct var ***vppp, int *lenp)
|
---|
916 | {
|
---|
917 | unsigned int hashval;
|
---|
918 | int len;
|
---|
919 | struct var *vp, **vpp;
|
---|
920 | const char *p = name;
|
---|
921 |
|
---|
922 | hashval = 0;
|
---|
923 | while (*p && *p != '=')
|
---|
924 | hashval = 2 * hashval + (unsigned char)*p++;
|
---|
925 | len = (int)(p - name);
|
---|
926 |
|
---|
927 | if (lenp)
|
---|
928 | *lenp = len;
|
---|
929 | vpp = &psh->vartab[hashval % VTABSIZE];
|
---|
930 | if (vppp)
|
---|
931 | *vppp = vpp;
|
---|
932 |
|
---|
933 | for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
|
---|
934 | if (vp->name_len != len)
|
---|
935 | continue;
|
---|
936 | if (memcmp(vp->text, name, len) != 0)
|
---|
937 | continue;
|
---|
938 | if (vppp)
|
---|
939 | *vppp = vpp;
|
---|
940 | return vp;
|
---|
941 | }
|
---|
942 | return NULL;
|
---|
943 | }
|
---|