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