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