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