var.c revision 1.55.2.4 1 1.55.2.4 martin /* $NetBSD: var.c,v 1.55.2.4 2018/12/07 13:23:49 martin 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.33 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.19 christos #include <sys/cdefs.h>
36 1.1 cgd #ifndef lint
37 1.12 cgd #if 0
38 1.13 christos static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 5/4/95";
39 1.12 cgd #else
40 1.55.2.4 martin __RCSID("$NetBSD: var.c,v 1.55.2.4 2018/12/07 13:23:49 martin Exp $");
41 1.12 cgd #endif
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.55.2.1 snj #include <stdio.h>
45 1.13 christos #include <unistd.h>
46 1.13 christos #include <stdlib.h>
47 1.42 christos #include <string.h>
48 1.21 fair #include <paths.h>
49 1.41 christos #include <limits.h>
50 1.55.2.1 snj #include <time.h>
51 1.55.2.1 snj #include <pwd.h>
52 1.55.2.1 snj #include <fcntl.h>
53 1.55.2.3 martin #include <inttypes.h>
54 1.13 christos
55 1.1 cgd /*
56 1.1 cgd * Shell variables.
57 1.1 cgd */
58 1.1 cgd
59 1.1 cgd #include "shell.h"
60 1.1 cgd #include "output.h"
61 1.1 cgd #include "expand.h"
62 1.1 cgd #include "nodes.h" /* for other headers */
63 1.1 cgd #include "eval.h" /* defines cmdenviron */
64 1.1 cgd #include "exec.h"
65 1.1 cgd #include "syntax.h"
66 1.1 cgd #include "options.h"
67 1.40 christos #include "builtins.h"
68 1.1 cgd #include "mail.h"
69 1.1 cgd #include "var.h"
70 1.1 cgd #include "memalloc.h"
71 1.1 cgd #include "error.h"
72 1.1 cgd #include "mystring.h"
73 1.15 christos #include "parser.h"
74 1.32 dsl #include "show.h"
75 1.55.2.1 snj #include "machdep.h"
76 1.17 christos #ifndef SMALL
77 1.13 christos #include "myhistedit.h"
78 1.13 christos #endif
79 1.1 cgd
80 1.35 dsl #ifdef SMALL
81 1.1 cgd #define VTABSIZE 39
82 1.35 dsl #else
83 1.35 dsl #define VTABSIZE 517
84 1.35 dsl #endif
85 1.1 cgd
86 1.1 cgd
87 1.1 cgd struct varinit {
88 1.1 cgd struct var *var;
89 1.1 cgd int flags;
90 1.23 christos const char *text;
91 1.55.2.1 snj union var_func_union v_u;
92 1.1 cgd };
93 1.55.2.1 snj #define func v_u.set_func
94 1.55.2.1 snj #define rfunc v_u.ref_func
95 1.55.2.1 snj
96 1.55.2.1 snj char *get_lineno(struct var *);
97 1.55.2.1 snj
98 1.55.2.1 snj #ifndef SMALL
99 1.55.2.1 snj char *get_tod(struct var *);
100 1.55.2.1 snj char *get_hostname(struct var *);
101 1.55.2.1 snj char *get_seconds(struct var *);
102 1.55.2.1 snj char *get_euser(struct var *);
103 1.55.2.1 snj char *get_random(struct var *);
104 1.55.2.1 snj #endif
105 1.1 cgd
106 1.39 dholland struct localvar *localvars;
107 1.1 cgd
108 1.17 christos #ifndef SMALL
109 1.5 jtc struct var vhistsize;
110 1.18 christos struct var vterm;
111 1.55.2.1 snj struct var editrc;
112 1.55.2.1 snj struct var ps_lit;
113 1.7 cgd #endif
114 1.1 cgd struct var vifs;
115 1.1 cgd struct var vmail;
116 1.1 cgd struct var vmpath;
117 1.1 cgd struct var vpath;
118 1.1 cgd struct var vps1;
119 1.1 cgd struct var vps2;
120 1.32 dsl struct var vps4;
121 1.49 christos struct var vvers;
122 1.14 christos struct var voptind;
123 1.55.2.1 snj struct var line_num;
124 1.55.2.1 snj #ifndef SMALL
125 1.55.2.1 snj struct var tod;
126 1.55.2.1 snj struct var host_name;
127 1.55.2.1 snj struct var seconds;
128 1.55.2.1 snj struct var euname;
129 1.55.2.1 snj struct var random_num;
130 1.55.2.1 snj
131 1.55.2.1 snj intmax_t sh_start_time;
132 1.55.2.1 snj #endif
133 1.55.2.1 snj
134 1.55.2.1 snj struct var line_num;
135 1.55.2.1 snj int line_number;
136 1.55.2.1 snj int funclinebase = 0;
137 1.55.2.1 snj int funclineabs = 0;
138 1.1 cgd
139 1.48 christos char ifs_default[] = " \t\n";
140 1.48 christos
141 1.1 cgd const struct varinit varinit[] = {
142 1.17 christos #ifndef SMALL
143 1.14 christos { &vhistsize, VSTRFIXED|VTEXTFIXED|VUNSET, "HISTSIZE=",
144 1.55.2.1 snj { .set_func= sethistsize } },
145 1.7 cgd #endif
146 1.14 christos { &vifs, VSTRFIXED|VTEXTFIXED, "IFS= \t\n",
147 1.55.2.1 snj { NULL } },
148 1.14 christos { &vmail, VSTRFIXED|VTEXTFIXED|VUNSET, "MAIL=",
149 1.55.2.1 snj { NULL } },
150 1.14 christos { &vmpath, VSTRFIXED|VTEXTFIXED|VUNSET, "MAILPATH=",
151 1.55.2.1 snj { NULL } },
152 1.49 christos { &vvers, VSTRFIXED|VTEXTFIXED|VNOEXPORT, "NETBSD_SHELL=",
153 1.55.2.1 snj { NULL } },
154 1.26 cgd { &vpath, VSTRFIXED|VTEXTFIXED, "PATH=" _PATH_DEFPATH,
155 1.55.2.1 snj { .set_func= changepath } },
156 1.15 christos /*
157 1.1 cgd * vps1 depends on uid
158 1.1 cgd */
159 1.14 christos { &vps2, VSTRFIXED|VTEXTFIXED, "PS2=> ",
160 1.55.2.1 snj { NULL } },
161 1.32 dsl { &vps4, VSTRFIXED|VTEXTFIXED, "PS4=+ ",
162 1.55.2.1 snj { NULL } },
163 1.18 christos #ifndef SMALL
164 1.14 christos { &vterm, VSTRFIXED|VTEXTFIXED|VUNSET, "TERM=",
165 1.55.2.1 snj { .set_func= setterm } },
166 1.55.2.1 snj { &editrc, VSTRFIXED|VTEXTFIXED|VUNSET, "EDITRC=",
167 1.55.2.1 snj { .set_func= set_editrc } },
168 1.55.2.1 snj { &ps_lit, VSTRFIXED|VTEXTFIXED|VUNSET, "PSlit=",
169 1.55.2.1 snj { .set_func= set_prompt_lit } },
170 1.1 cgd #endif
171 1.32 dsl { &voptind, VSTRFIXED|VTEXTFIXED|VNOFUNC, "OPTIND=1",
172 1.55.2.1 snj { .set_func= getoptsreset } },
173 1.55.2.4 martin { &line_num, VSTRFIXED|VTEXTFIXED|VFUNCREF|VSPECIAL, "LINENO=1",
174 1.55.2.1 snj { .ref_func= get_lineno } },
175 1.55.2.1 snj #ifndef SMALL
176 1.55.2.1 snj { &tod, VSTRFIXED|VTEXTFIXED|VFUNCREF, "ToD=",
177 1.55.2.1 snj { .ref_func= get_tod } },
178 1.55.2.1 snj { &host_name, VSTRFIXED|VTEXTFIXED|VFUNCREF, "HOSTNAME=",
179 1.55.2.1 snj { .ref_func= get_hostname } },
180 1.55.2.1 snj { &seconds, VSTRFIXED|VTEXTFIXED|VFUNCREF, "SECONDS=",
181 1.55.2.1 snj { .ref_func= get_seconds } },
182 1.55.2.1 snj { &euname, VSTRFIXED|VTEXTFIXED|VFUNCREF, "EUSER=",
183 1.55.2.1 snj { .ref_func= get_euser } },
184 1.55.2.4 martin { &random_num, VSTRFIXED|VTEXTFIXED|VFUNCREF|VSPECIAL, "RANDOM=",
185 1.55.2.1 snj { .ref_func= get_random } },
186 1.55.2.1 snj #endif
187 1.14 christos { NULL, 0, NULL,
188 1.55.2.1 snj { NULL } }
189 1.1 cgd };
190 1.1 cgd
191 1.1 cgd struct var *vartab[VTABSIZE];
192 1.1 cgd
193 1.35 dsl STATIC int strequal(const char *, const char *);
194 1.35 dsl STATIC struct var *find_var(const char *, struct var ***, int *);
195 1.1 cgd
196 1.1 cgd /*
197 1.1 cgd * Initialize the varable symbol tables and import the environment
198 1.1 cgd */
199 1.1 cgd
200 1.1 cgd #ifdef mkinit
201 1.47 christos INCLUDE <stdio.h>
202 1.47 christos INCLUDE <unistd.h>
203 1.55.2.1 snj INCLUDE <time.h>
204 1.1 cgd INCLUDE "var.h"
205 1.49 christos INCLUDE "version.h"
206 1.27 christos MKINIT char **environ;
207 1.1 cgd INIT {
208 1.1 cgd char **envp;
209 1.47 christos char buf[64];
210 1.1 cgd
211 1.55.2.1 snj #ifndef SMALL
212 1.55.2.1 snj sh_start_time = (intmax_t)time((time_t *)0);
213 1.55.2.1 snj #endif
214 1.55.2.1 snj /*
215 1.55.2.1 snj * Set up our default variables and their values.
216 1.55.2.1 snj */
217 1.1 cgd initvar();
218 1.55.2.1 snj
219 1.55.2.1 snj /*
220 1.55.2.1 snj * Import variables from the environment, which will
221 1.55.2.1 snj * override anything initialised just previously.
222 1.55.2.1 snj */
223 1.1 cgd for (envp = environ ; *envp ; envp++) {
224 1.1 cgd if (strchr(*envp, '=')) {
225 1.1 cgd setvareq(*envp, VEXPORT|VTEXTFIXED);
226 1.1 cgd }
227 1.1 cgd }
228 1.47 christos
229 1.47 christos /*
230 1.53 kre * Set variables which override anything read from environment.
231 1.53 kre *
232 1.47 christos * PPID is readonly
233 1.53 kre * Always default IFS
234 1.55.2.3 martin * POSIX: "Whenever the shell is invoked, OPTIND shall
235 1.55.2.3 martin * be initialized to 1."
236 1.55.2.1 snj * PSc indicates the root/non-root status of this shell.
237 1.55.2.1 snj * START_TIME belongs only to this shell.
238 1.55.2.3 martin * NETBSD_SHELL is a constant (readonly), and is never exported
239 1.55.2.1 snj * LINENO is simply magic...
240 1.47 christos */
241 1.47 christos snprintf(buf, sizeof(buf), "%d", (int)getppid());
242 1.47 christos setvar("PPID", buf, VREADONLY);
243 1.48 christos setvar("IFS", ifs_default, VTEXTFIXED);
244 1.55.2.3 martin setvar("OPTIND", "1", VTEXTFIXED);
245 1.55.2.1 snj setvar("PSc", (geteuid() == 0 ? "#" : "$"), VTEXTFIXED);
246 1.55.2.1 snj
247 1.55.2.1 snj #ifndef SMALL
248 1.55.2.1 snj snprintf(buf, sizeof(buf), "%jd", sh_start_time);
249 1.55.2.1 snj setvar("START_TIME", buf, VTEXTFIXED);
250 1.55.2.1 snj #endif
251 1.53 kre
252 1.53 kre setvar("NETBSD_SHELL", NETBSD_SHELL
253 1.53 kre #ifdef BUILD_DATE
254 1.53 kre " BUILD:" BUILD_DATE
255 1.53 kre #endif
256 1.53 kre #ifdef DEBUG
257 1.53 kre " DEBUG"
258 1.53 kre #endif
259 1.53 kre #if !defined(JOBS) || JOBS == 0
260 1.53 kre " -JOBS"
261 1.53 kre #endif
262 1.53 kre #ifndef DO_SHAREDVFORK
263 1.53 kre " -VFORK"
264 1.53 kre #endif
265 1.53 kre #ifdef SMALL
266 1.53 kre " SMALL"
267 1.53 kre #endif
268 1.53 kre #ifdef TINY
269 1.53 kre " TINY"
270 1.53 kre #endif
271 1.53 kre #ifdef OLD_TTY_DRIVER
272 1.53 kre " OLD_TTY"
273 1.53 kre #endif
274 1.53 kre #ifdef SYSV
275 1.53 kre " SYSV"
276 1.53 kre #endif
277 1.53 kre #ifndef BSD
278 1.53 kre " -BSD"
279 1.53 kre #endif
280 1.55 kre #ifdef BOGUS_NOT_COMMAND
281 1.55 kre " BOGUS_NOT"
282 1.55 kre #endif
283 1.53 kre , VTEXTFIXED|VREADONLY|VNOEXPORT);
284 1.55.2.1 snj
285 1.55.2.1 snj setvar("LINENO", "1", VTEXTFIXED);
286 1.1 cgd }
287 1.1 cgd #endif
288 1.1 cgd
289 1.1 cgd
290 1.1 cgd /*
291 1.1 cgd * This routine initializes the builtin variables. It is called when the
292 1.1 cgd * shell is initialized and again when a shell procedure is spawned.
293 1.1 cgd */
294 1.1 cgd
295 1.1 cgd void
296 1.30 christos initvar(void)
297 1.30 christos {
298 1.1 cgd const struct varinit *ip;
299 1.1 cgd struct var *vp;
300 1.1 cgd struct var **vpp;
301 1.1 cgd
302 1.1 cgd for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
303 1.35 dsl if (find_var(ip->text, &vpp, &vp->name_len) != NULL)
304 1.35 dsl continue;
305 1.35 dsl vp->next = *vpp;
306 1.35 dsl *vpp = vp;
307 1.35 dsl vp->text = strdup(ip->text);
308 1.55.2.1 snj vp->flags = (ip->flags & ~VTEXTFIXED) | VSTRFIXED;
309 1.55.2.1 snj vp->v_u = ip->v_u;
310 1.1 cgd }
311 1.1 cgd /*
312 1.1 cgd * PS1 depends on uid
313 1.1 cgd */
314 1.35 dsl if (find_var("PS1", &vpp, &vps1.name_len) == NULL) {
315 1.1 cgd vps1.next = *vpp;
316 1.1 cgd *vpp = &vps1;
317 1.55.2.1 snj vps1.flags = VSTRFIXED;
318 1.44 christos vps1.text = NULL;
319 1.44 christos choose_ps1();
320 1.1 cgd }
321 1.1 cgd }
322 1.1 cgd
323 1.44 christos void
324 1.44 christos choose_ps1(void)
325 1.44 christos {
326 1.55.2.1 snj if ((vps1.flags & (VTEXTFIXED|VSTACK)) == 0)
327 1.55.2.1 snj free(vps1.text);
328 1.44 christos vps1.text = strdup(geteuid() ? "PS1=$ " : "PS1=# ");
329 1.55.2.1 snj vps1.flags &= ~(VTEXTFIXED|VSTACK);
330 1.55.2.1 snj
331 1.55.2.1 snj /*
332 1.55.2.1 snj * Update PSc whenever we feel the need to update PS1
333 1.55.2.1 snj */
334 1.55.2.1 snj setvarsafe("PSc", (geteuid() == 0 ? "#" : "$"), 0);
335 1.44 christos }
336 1.44 christos
337 1.1 cgd /*
338 1.14 christos * Safe version of setvar, returns 1 on success 0 on failure.
339 1.14 christos */
340 1.14 christos
341 1.14 christos int
342 1.30 christos setvarsafe(const char *name, const char *val, int flags)
343 1.14 christos {
344 1.14 christos struct jmploc jmploc;
345 1.55.2.1 snj struct jmploc * const savehandler = handler;
346 1.38 christos int volatile err = 0;
347 1.14 christos
348 1.14 christos if (setjmp(jmploc.loc))
349 1.14 christos err = 1;
350 1.14 christos else {
351 1.14 christos handler = &jmploc;
352 1.14 christos setvar(name, val, flags);
353 1.14 christos }
354 1.14 christos handler = savehandler;
355 1.14 christos return err;
356 1.14 christos }
357 1.14 christos
358 1.14 christos /*
359 1.1 cgd * Set the value of a variable. The flags argument is ored with the
360 1.1 cgd * flags of the variable. If val is NULL, the variable is unset.
361 1.55.2.1 snj *
362 1.55.2.1 snj * This always copies name and val when setting a variable, so
363 1.55.2.1 snj * the source strings can be from anywhere, and are no longer needed
364 1.55.2.1 snj * after this function returns. The VTEXTFIXED and VSTACK flags should
365 1.55.2.1 snj * not be used (but just in case they were, clear them.)
366 1.1 cgd */
367 1.1 cgd
368 1.1 cgd void
369 1.30 christos setvar(const char *name, const char *val, int flags)
370 1.10 cgd {
371 1.23 christos const char *p;
372 1.23 christos const char *q;
373 1.23 christos char *d;
374 1.1 cgd int len;
375 1.1 cgd int namelen;
376 1.1 cgd char *nameeq;
377 1.1 cgd int isbad;
378 1.1 cgd
379 1.1 cgd isbad = 0;
380 1.1 cgd p = name;
381 1.15 christos if (! is_name(*p))
382 1.1 cgd isbad = 1;
383 1.15 christos p++;
384 1.1 cgd for (;;) {
385 1.1 cgd if (! is_in_name(*p)) {
386 1.1 cgd if (*p == '\0' || *p == '=')
387 1.1 cgd break;
388 1.1 cgd isbad = 1;
389 1.1 cgd }
390 1.1 cgd p++;
391 1.1 cgd }
392 1.1 cgd namelen = p - name;
393 1.1 cgd if (isbad)
394 1.5 jtc error("%.*s: bad variable name", namelen, name);
395 1.1 cgd len = namelen + 2; /* 2 is space for '=' and '\0' */
396 1.1 cgd if (val == NULL) {
397 1.1 cgd flags |= VUNSET;
398 1.1 cgd } else {
399 1.1 cgd len += strlen(val);
400 1.1 cgd }
401 1.23 christos d = nameeq = ckmalloc(len);
402 1.1 cgd q = name;
403 1.1 cgd while (--namelen >= 0)
404 1.23 christos *d++ = *q++;
405 1.23 christos *d++ = '=';
406 1.23 christos *d = '\0';
407 1.1 cgd if (val)
408 1.23 christos scopy(val, d);
409 1.55.2.1 snj setvareq(nameeq, flags & ~(VTEXTFIXED | VSTACK));
410 1.1 cgd }
411 1.1 cgd
412 1.1 cgd
413 1.1 cgd
414 1.1 cgd /*
415 1.1 cgd * Same as setvar except that the variable and value are passed in
416 1.1 cgd * the first argument as name=value. Since the first argument will
417 1.1 cgd * be actually stored in the table, it should not be a string that
418 1.55.2.1 snj * will go away. The flags (VTEXTFIXED or VSTACK) can be used to
419 1.55.2.1 snj * indicate the source of the string (if neither is set, the string will
420 1.55.2.1 snj * eventually be free()d when a replacement value is assigned.)
421 1.1 cgd */
422 1.1 cgd
423 1.1 cgd void
424 1.30 christos setvareq(char *s, int flags)
425 1.10 cgd {
426 1.1 cgd struct var *vp, **vpp;
427 1.35 dsl int nlen;
428 1.1 cgd
429 1.49 christos if (aflag && !(flags & VNOEXPORT))
430 1.28 bjh21 flags |= VEXPORT;
431 1.35 dsl vp = find_var(s, &vpp, &nlen);
432 1.35 dsl if (vp != NULL) {
433 1.55.2.1 snj if (vp->flags & VREADONLY) {
434 1.55.2.1 snj if ((flags & (VTEXTFIXED|VSTACK)) == 0)
435 1.55.2.1 snj ckfree(s);
436 1.55.2.1 snj if (flags & VNOERROR)
437 1.55.2.1 snj return;
438 1.55.2.1 snj error("%.*s: is read only", vp->name_len, vp->text);
439 1.55.2.1 snj }
440 1.55.2.1 snj if (flags & VNOSET) {
441 1.55.2.1 snj if ((flags & (VTEXTFIXED|VSTACK)) == 0)
442 1.55.2.1 snj ckfree(s);
443 1.35 dsl return;
444 1.55.2.1 snj }
445 1.55.2.1 snj
446 1.35 dsl INTOFF;
447 1.14 christos
448 1.55.2.1 snj if (vp->func && !(vp->flags & VFUNCREF) && !(flags & VNOFUNC))
449 1.35 dsl (*vp->func)(s + vp->name_len + 1);
450 1.14 christos
451 1.35 dsl if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
452 1.35 dsl ckfree(vp->text);
453 1.14 christos
454 1.55.2.4 martin /*
455 1.55.2.4 martin * if we set a magic var, the magic dissipates,
456 1.55.2.4 martin * unless it is very special indeed.
457 1.55.2.4 martin */
458 1.55.2.4 martin if (vp->rfunc && (vp->flags & (VFUNCREF|VSPECIAL)) == VFUNCREF)
459 1.55.2.4 martin vp->rfunc = NULL;
460 1.55.2.4 martin
461 1.35 dsl vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
462 1.49 christos if (flags & VNOEXPORT)
463 1.49 christos vp->flags &= ~VEXPORT;
464 1.54 kre if (vp->flags & VNOEXPORT)
465 1.54 kre flags &= ~VEXPORT;
466 1.35 dsl vp->flags |= flags & ~VNOFUNC;
467 1.35 dsl vp->text = s;
468 1.35 dsl
469 1.35 dsl /*
470 1.35 dsl * We could roll this to a function, to handle it as
471 1.35 dsl * a regular variable function callback, but why bother?
472 1.35 dsl */
473 1.35 dsl if (vp == &vmpath || (vp == &vmail && ! mpathset()))
474 1.35 dsl chkmail(1);
475 1.55.2.1 snj
476 1.35 dsl INTON;
477 1.35 dsl return;
478 1.1 cgd }
479 1.1 cgd /* not found */
480 1.55.2.1 snj if (flags & VNOSET) {
481 1.55.2.1 snj if ((flags & (VTEXTFIXED|VSTACK)) == 0)
482 1.55.2.1 snj ckfree(s);
483 1.30 christos return;
484 1.55.2.1 snj }
485 1.1 cgd vp = ckmalloc(sizeof (*vp));
486 1.55.2.2 martin vp->flags = flags & ~(VNOFUNC|VFUNCREF);
487 1.1 cgd vp->text = s;
488 1.35 dsl vp->name_len = nlen;
489 1.1 cgd vp->next = *vpp;
490 1.14 christos vp->func = NULL;
491 1.1 cgd *vpp = vp;
492 1.1 cgd }
493 1.1 cgd
494 1.1 cgd
495 1.1 cgd
496 1.1 cgd /*
497 1.1 cgd * Process a linked list of variable assignments.
498 1.1 cgd */
499 1.1 cgd
500 1.1 cgd void
501 1.30 christos listsetvar(struct strlist *list, int flags)
502 1.30 christos {
503 1.1 cgd struct strlist *lp;
504 1.1 cgd
505 1.1 cgd INTOFF;
506 1.1 cgd for (lp = list ; lp ; lp = lp->next) {
507 1.30 christos setvareq(savestr(lp->text), flags);
508 1.1 cgd }
509 1.1 cgd INTON;
510 1.1 cgd }
511 1.1 cgd
512 1.32 dsl void
513 1.32 dsl listmklocal(struct strlist *list, int flags)
514 1.32 dsl {
515 1.32 dsl struct strlist *lp;
516 1.32 dsl
517 1.32 dsl for (lp = list ; lp ; lp = lp->next)
518 1.32 dsl mklocal(lp->text, flags);
519 1.32 dsl }
520 1.1 cgd
521 1.1 cgd
522 1.1 cgd /*
523 1.1 cgd * Find the value of a variable. Returns NULL if not set.
524 1.1 cgd */
525 1.1 cgd
526 1.1 cgd char *
527 1.30 christos lookupvar(const char *name)
528 1.30 christos {
529 1.1 cgd struct var *v;
530 1.1 cgd
531 1.35 dsl v = find_var(name, NULL, NULL);
532 1.35 dsl if (v == NULL || v->flags & VUNSET)
533 1.35 dsl return NULL;
534 1.55.2.1 snj if (v->rfunc && (v->flags & VFUNCREF) != 0)
535 1.55.2.1 snj return (*v->rfunc)(v) + v->name_len + 1;
536 1.35 dsl return v->text + v->name_len + 1;
537 1.1 cgd }
538 1.1 cgd
539 1.1 cgd
540 1.1 cgd
541 1.1 cgd /*
542 1.1 cgd * Search the environment of a builtin command. If the second argument
543 1.1 cgd * is nonzero, return the value of a variable even if it hasn't been
544 1.1 cgd * exported.
545 1.1 cgd */
546 1.1 cgd
547 1.1 cgd char *
548 1.30 christos bltinlookup(const char *name, int doall)
549 1.10 cgd {
550 1.1 cgd struct strlist *sp;
551 1.1 cgd struct var *v;
552 1.1 cgd
553 1.1 cgd for (sp = cmdenviron ; sp ; sp = sp->next) {
554 1.35 dsl if (strequal(sp->text, name))
555 1.1 cgd return strchr(sp->text, '=') + 1;
556 1.1 cgd }
557 1.35 dsl
558 1.35 dsl v = find_var(name, NULL, NULL);
559 1.35 dsl
560 1.35 dsl if (v == NULL || v->flags & VUNSET || (!doall && !(v->flags & VEXPORT)))
561 1.35 dsl return NULL;
562 1.55.2.1 snj if (v->rfunc && (v->flags & VFUNCREF) != 0)
563 1.55.2.1 snj return (*v->rfunc)(v) + v->name_len + 1;
564 1.35 dsl return v->text + v->name_len + 1;
565 1.1 cgd }
566 1.1 cgd
567 1.1 cgd
568 1.1 cgd
569 1.1 cgd /*
570 1.1 cgd * Generate a list of exported variables. This routine is used to construct
571 1.1 cgd * the third argument to execve when executing a program.
572 1.1 cgd */
573 1.1 cgd
574 1.1 cgd char **
575 1.30 christos environment(void)
576 1.30 christos {
577 1.1 cgd int nenv;
578 1.1 cgd struct var **vpp;
579 1.1 cgd struct var *vp;
580 1.23 christos char **env;
581 1.23 christos char **ep;
582 1.1 cgd
583 1.1 cgd nenv = 0;
584 1.1 cgd for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
585 1.1 cgd for (vp = *vpp ; vp ; vp = vp->next)
586 1.51 kre if ((vp->flags & (VEXPORT|VUNSET)) == VEXPORT)
587 1.1 cgd nenv++;
588 1.1 cgd }
589 1.55.2.1 snj CTRACE(DBG_VARS, ("environment: %d vars to export\n", nenv));
590 1.1 cgd ep = env = stalloc((nenv + 1) * sizeof *env);
591 1.1 cgd for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
592 1.1 cgd for (vp = *vpp ; vp ; vp = vp->next)
593 1.55.2.1 snj if ((vp->flags & (VEXPORT|VUNSET)) == VEXPORT) {
594 1.55.2.1 snj if (vp->rfunc && (vp->flags & VFUNCREF))
595 1.55.2.1 snj *ep++ = (*vp->rfunc)(vp);
596 1.55.2.1 snj else
597 1.55.2.1 snj *ep++ = vp->text;
598 1.55.2.1 snj VTRACE(DBG_VARS, ("environment: %s\n", ep[-1]));
599 1.55.2.1 snj }
600 1.1 cgd }
601 1.1 cgd *ep = NULL;
602 1.1 cgd return env;
603 1.1 cgd }
604 1.1 cgd
605 1.1 cgd
606 1.1 cgd /*
607 1.1 cgd * Called when a shell procedure is invoked to clear out nonexported
608 1.1 cgd * variables. It is also necessary to reallocate variables of with
609 1.1 cgd * VSTACK set since these are currently allocated on the stack.
610 1.1 cgd */
611 1.1 cgd
612 1.1 cgd #ifdef mkinit
613 1.30 christos void shprocvar(void);
614 1.1 cgd
615 1.1 cgd SHELLPROC {
616 1.1 cgd shprocvar();
617 1.1 cgd }
618 1.1 cgd #endif
619 1.1 cgd
620 1.1 cgd void
621 1.30 christos shprocvar(void)
622 1.30 christos {
623 1.1 cgd struct var **vpp;
624 1.1 cgd struct var *vp, **prev;
625 1.1 cgd
626 1.1 cgd for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
627 1.1 cgd for (prev = vpp ; (vp = *prev) != NULL ; ) {
628 1.1 cgd if ((vp->flags & VEXPORT) == 0) {
629 1.1 cgd *prev = vp->next;
630 1.1 cgd if ((vp->flags & VTEXTFIXED) == 0)
631 1.1 cgd ckfree(vp->text);
632 1.1 cgd if ((vp->flags & VSTRFIXED) == 0)
633 1.1 cgd ckfree(vp);
634 1.1 cgd } else {
635 1.1 cgd if (vp->flags & VSTACK) {
636 1.1 cgd vp->text = savestr(vp->text);
637 1.1 cgd vp->flags &=~ VSTACK;
638 1.1 cgd }
639 1.1 cgd prev = &vp->next;
640 1.1 cgd }
641 1.1 cgd }
642 1.1 cgd }
643 1.1 cgd initvar();
644 1.1 cgd }
645 1.1 cgd
646 1.1 cgd
647 1.1 cgd
648 1.1 cgd /*
649 1.1 cgd * Command to list all variables which are set. Currently this command
650 1.1 cgd * is invoked from the set command when the set command is called without
651 1.1 cgd * any variables.
652 1.1 cgd */
653 1.1 cgd
654 1.30 christos void
655 1.30 christos print_quoted(const char *p)
656 1.30 christos {
657 1.30 christos const char *q;
658 1.30 christos
659 1.50 kre if (p[0] == '\0') {
660 1.50 kre out1fmt("''");
661 1.50 kre return;
662 1.50 kre }
663 1.30 christos if (strcspn(p, "|&;<>()$`\\\"' \t\n*?[]#~=%") == strlen(p)) {
664 1.30 christos out1fmt("%s", p);
665 1.30 christos return;
666 1.30 christos }
667 1.30 christos while (*p) {
668 1.30 christos if (*p == '\'') {
669 1.30 christos out1fmt("\\'");
670 1.30 christos p++;
671 1.30 christos continue;
672 1.30 christos }
673 1.42 christos q = strchr(p, '\'');
674 1.30 christos if (!q) {
675 1.30 christos out1fmt("'%s'", p );
676 1.30 christos return;
677 1.30 christos }
678 1.31 agc out1fmt("'%.*s'", (int)(q - p), p );
679 1.30 christos p = q;
680 1.30 christos }
681 1.30 christos }
682 1.30 christos
683 1.30 christos static int
684 1.30 christos sort_var(const void *v_v1, const void *v_v2)
685 1.30 christos {
686 1.30 christos const struct var * const *v1 = v_v1;
687 1.30 christos const struct var * const *v2 = v_v2;
688 1.52 kre char *t1 = (*v1)->text, *t2 = (*v2)->text;
689 1.30 christos
690 1.52 kre if (*t1 == *t2) {
691 1.52 kre char *p, *s;
692 1.52 kre
693 1.52 kre STARTSTACKSTR(p);
694 1.52 kre
695 1.52 kre /*
696 1.52 kre * note: if lengths are equal, strings must be different
697 1.52 kre * so we don't care which string we pick for the \0 in
698 1.52 kre * that case.
699 1.52 kre */
700 1.52 kre if ((strchr(t1, '=') - t1) <= (strchr(t2, '=') - t2)) {
701 1.52 kre s = t1;
702 1.52 kre t1 = p;
703 1.52 kre } else {
704 1.52 kre s = t2;
705 1.52 kre t2 = p;
706 1.52 kre }
707 1.52 kre
708 1.52 kre while (*s && *s != '=') {
709 1.52 kre STPUTC(*s, p);
710 1.52 kre s++;
711 1.52 kre }
712 1.52 kre STPUTC('\0', p);
713 1.52 kre }
714 1.52 kre
715 1.52 kre return strcoll(t1, t2);
716 1.30 christos }
717 1.30 christos
718 1.30 christos /*
719 1.30 christos * POSIX requires that 'set' (but not export or readonly) output the
720 1.30 christos * variables in lexicographic order - by the locale's collating order (sigh).
721 1.30 christos * Maybe we could keep them in an ordered balanced binary tree
722 1.30 christos * instead of hashed lists.
723 1.30 christos * For now just roll 'em through qsort for printing...
724 1.30 christos */
725 1.30 christos
726 1.1 cgd int
727 1.49 christos showvars(const char *name, int flag, int show_value, const char *xtra)
728 1.10 cgd {
729 1.1 cgd struct var **vpp;
730 1.1 cgd struct var *vp;
731 1.30 christos const char *p;
732 1.30 christos
733 1.30 christos static struct var **list; /* static in case we are interrupted */
734 1.30 christos static int list_len;
735 1.30 christos int count = 0;
736 1.30 christos
737 1.30 christos if (!list) {
738 1.30 christos list_len = 32;
739 1.30 christos list = ckmalloc(list_len * sizeof *list);
740 1.30 christos }
741 1.1 cgd
742 1.1 cgd for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
743 1.1 cgd for (vp = *vpp ; vp ; vp = vp->next) {
744 1.30 christos if (flag && !(vp->flags & flag))
745 1.30 christos continue;
746 1.30 christos if (vp->flags & VUNSET && !(show_value & 2))
747 1.30 christos continue;
748 1.30 christos if (count >= list_len) {
749 1.30 christos list = ckrealloc(list,
750 1.30 christos (list_len << 1) * sizeof *list);
751 1.30 christos list_len <<= 1;
752 1.30 christos }
753 1.30 christos list[count++] = vp;
754 1.1 cgd }
755 1.1 cgd }
756 1.30 christos
757 1.30 christos qsort(list, count, sizeof *list, sort_var);
758 1.30 christos
759 1.30 christos for (vpp = list; count--; vpp++) {
760 1.30 christos vp = *vpp;
761 1.30 christos if (name)
762 1.30 christos out1fmt("%s ", name);
763 1.49 christos if (xtra)
764 1.49 christos out1fmt("%s ", xtra);
765 1.55.2.1 snj p = vp->text;
766 1.55.2.1 snj if (vp->rfunc && (vp->flags & VFUNCREF) != 0) {
767 1.55.2.1 snj p = (*vp->rfunc)(vp);
768 1.55.2.1 snj if (p == NULL)
769 1.55.2.1 snj p = vp->text;
770 1.55.2.1 snj }
771 1.55.2.1 snj for ( ; *p != '=' ; p++)
772 1.30 christos out1c(*p);
773 1.30 christos if (!(vp->flags & VUNSET) && show_value) {
774 1.30 christos out1fmt("=");
775 1.30 christos print_quoted(++p);
776 1.30 christos }
777 1.30 christos out1c('\n');
778 1.30 christos }
779 1.1 cgd return 0;
780 1.1 cgd }
781 1.1 cgd
782 1.1 cgd
783 1.1 cgd
784 1.1 cgd /*
785 1.1 cgd * The export and readonly commands.
786 1.1 cgd */
787 1.1 cgd
788 1.1 cgd int
789 1.30 christos exportcmd(int argc, char **argv)
790 1.10 cgd {
791 1.1 cgd struct var *vp;
792 1.1 cgd char *name;
793 1.23 christos const char *p;
794 1.1 cgd int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
795 1.49 christos int pflg = 0;
796 1.49 christos int nflg = 0;
797 1.49 christos int xflg = 0;
798 1.49 christos int res;
799 1.49 christos int c;
800 1.51 kre int f;
801 1.49 christos
802 1.49 christos while ((c = nextopt("npx")) != '\0') {
803 1.49 christos switch (c) {
804 1.49 christos case 'p':
805 1.49 christos if (nflg)
806 1.49 christos return 1;
807 1.49 christos pflg = 3;
808 1.49 christos break;
809 1.49 christos case 'n':
810 1.49 christos if (pflg || xflg || flag == VREADONLY)
811 1.49 christos return 1;
812 1.49 christos nflg = 1;
813 1.49 christos break;
814 1.49 christos case 'x':
815 1.49 christos if (nflg || flag == VREADONLY)
816 1.49 christos return 1;
817 1.49 christos flag = VNOEXPORT;
818 1.49 christos xflg = 1;
819 1.49 christos break;
820 1.49 christos default:
821 1.49 christos return 1;
822 1.49 christos }
823 1.49 christos }
824 1.1 cgd
825 1.49 christos if (nflg && *argptr == NULL)
826 1.49 christos return 1;
827 1.49 christos
828 1.49 christos if (pflg || *argptr == NULL) {
829 1.49 christos showvars( pflg ? argv[0] : 0, flag, pflg,
830 1.49 christos pflg && xflg ? "-x" : NULL );
831 1.30 christos return 0;
832 1.30 christos }
833 1.30 christos
834 1.49 christos res = 0;
835 1.30 christos while ((name = *argptr++) != NULL) {
836 1.51 kre f = flag;
837 1.30 christos if ((p = strchr(name, '=')) != NULL) {
838 1.30 christos p++;
839 1.30 christos } else {
840 1.35 dsl vp = find_var(name, NULL, NULL);
841 1.35 dsl if (vp != NULL) {
842 1.49 christos if (nflg)
843 1.49 christos vp->flags &= ~flag;
844 1.49 christos else if (flag&VEXPORT && vp->flags&VNOEXPORT)
845 1.49 christos res = 1;
846 1.49 christos else {
847 1.49 christos vp->flags |= flag;
848 1.49 christos if (flag == VNOEXPORT)
849 1.49 christos vp->flags &= ~VEXPORT;
850 1.49 christos }
851 1.36 enami continue;
852 1.51 kre } else
853 1.51 kre f |= VUNSET;
854 1.1 cgd }
855 1.49 christos if (!nflg)
856 1.51 kre setvar(name, p, f);
857 1.1 cgd }
858 1.49 christos return res;
859 1.1 cgd }
860 1.1 cgd
861 1.1 cgd
862 1.1 cgd /*
863 1.1 cgd * The "local" command.
864 1.1 cgd */
865 1.1 cgd
866 1.10 cgd int
867 1.30 christos localcmd(int argc, char **argv)
868 1.10 cgd {
869 1.1 cgd char *name;
870 1.54 kre int c;
871 1.54 kre int flags = 0; /*XXX perhaps VUNSET from a -o option value */
872 1.1 cgd
873 1.1 cgd if (! in_function())
874 1.1 cgd error("Not in a function");
875 1.54 kre
876 1.54 kre /* upper case options, as bash stole all the good ones ... */
877 1.54 kre while ((c = nextopt("INx")) != '\0')
878 1.54 kre switch (c) {
879 1.54 kre case 'I': flags &= ~VUNSET; break;
880 1.54 kre case 'N': flags |= VUNSET; break;
881 1.54 kre case 'x': flags |= VEXPORT; break;
882 1.54 kre }
883 1.54 kre
884 1.1 cgd while ((name = *argptr++) != NULL) {
885 1.54 kre mklocal(name, flags);
886 1.1 cgd }
887 1.1 cgd return 0;
888 1.1 cgd }
889 1.1 cgd
890 1.1 cgd
891 1.1 cgd /*
892 1.37 snj * Make a variable a local variable. When a variable is made local, its
893 1.1 cgd * value and flags are saved in a localvar structure. The saved values
894 1.1 cgd * will be restored when the shell function returns. We handle the name
895 1.1 cgd * "-" as a special case.
896 1.1 cgd */
897 1.1 cgd
898 1.1 cgd void
899 1.32 dsl mklocal(const char *name, int flags)
900 1.30 christos {
901 1.1 cgd struct localvar *lvp;
902 1.1 cgd struct var **vpp;
903 1.1 cgd struct var *vp;
904 1.1 cgd
905 1.1 cgd INTOFF;
906 1.1 cgd lvp = ckmalloc(sizeof (struct localvar));
907 1.1 cgd if (name[0] == '-' && name[1] == '\0') {
908 1.23 christos char *p;
909 1.30 christos p = ckmalloc(sizeof_optlist);
910 1.30 christos lvp->text = memcpy(p, optlist, sizeof_optlist);
911 1.55.2.4 martin lvp->rfunc = NULL;
912 1.1 cgd vp = NULL;
913 1.1 cgd } else {
914 1.35 dsl vp = find_var(name, &vpp, NULL);
915 1.1 cgd if (vp == NULL) {
916 1.54 kre flags &= ~VNOEXPORT;
917 1.1 cgd if (strchr(name, '='))
918 1.54 kre setvareq(savestr(name),
919 1.54 kre VSTRFIXED | (flags & ~VUNSET));
920 1.1 cgd else
921 1.29 christos setvar(name, NULL, VSTRFIXED|flags);
922 1.1 cgd vp = *vpp; /* the new variable */
923 1.1 cgd lvp->text = NULL;
924 1.1 cgd lvp->flags = VUNSET;
925 1.55.2.4 martin lvp->rfunc = NULL;
926 1.1 cgd } else {
927 1.1 cgd lvp->text = vp->text;
928 1.1 cgd lvp->flags = vp->flags;
929 1.55.2.4 martin lvp->v_u = vp->v_u;
930 1.1 cgd vp->flags |= VSTRFIXED|VTEXTFIXED;
931 1.54 kre if (vp->flags & VNOEXPORT)
932 1.54 kre flags &= ~VEXPORT;
933 1.54 kre if (flags & (VNOEXPORT | VUNSET))
934 1.54 kre vp->flags &= ~VEXPORT;
935 1.54 kre flags &= ~VNOEXPORT;
936 1.35 dsl if (name[vp->name_len] == '=')
937 1.54 kre setvareq(savestr(name), flags & ~VUNSET);
938 1.54 kre else if (flags & VUNSET)
939 1.54 kre unsetvar(name, 0);
940 1.54 kre else
941 1.54 kre vp->flags |= flags & (VUNSET|VEXPORT);
942 1.55.2.1 snj
943 1.55.2.1 snj if (vp == &line_num) {
944 1.55.2.1 snj if (name[vp->name_len] == '=')
945 1.55.2.1 snj funclinebase = funclineabs -1;
946 1.55.2.1 snj else
947 1.55.2.1 snj funclinebase = 0;
948 1.55.2.1 snj }
949 1.1 cgd }
950 1.1 cgd }
951 1.1 cgd lvp->vp = vp;
952 1.1 cgd lvp->next = localvars;
953 1.1 cgd localvars = lvp;
954 1.1 cgd INTON;
955 1.1 cgd }
956 1.1 cgd
957 1.1 cgd
958 1.1 cgd /*
959 1.1 cgd * Called after a function returns.
960 1.1 cgd */
961 1.1 cgd
962 1.1 cgd void
963 1.30 christos poplocalvars(void)
964 1.30 christos {
965 1.1 cgd struct localvar *lvp;
966 1.1 cgd struct var *vp;
967 1.1 cgd
968 1.1 cgd while ((lvp = localvars) != NULL) {
969 1.1 cgd localvars = lvp->next;
970 1.1 cgd vp = lvp->vp;
971 1.55.2.1 snj VTRACE(DBG_VARS, ("poplocalvar %s", vp ? vp->text : "-"));
972 1.1 cgd if (vp == NULL) { /* $- saved */
973 1.30 christos memcpy(optlist, lvp->text, sizeof_optlist);
974 1.1 cgd ckfree(lvp->text);
975 1.55.2.1 snj optschanged();
976 1.1 cgd } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
977 1.30 christos (void)unsetvar(vp->text, 0);
978 1.1 cgd } else {
979 1.55.2.4 martin if (lvp->func && (lvp->flags & (VNOFUNC|VFUNCREF)) == 0)
980 1.55.2.4 martin (*lvp->func)(lvp->text + vp->name_len + 1);
981 1.1 cgd if ((vp->flags & VTEXTFIXED) == 0)
982 1.1 cgd ckfree(vp->text);
983 1.1 cgd vp->flags = lvp->flags;
984 1.1 cgd vp->text = lvp->text;
985 1.55.2.4 martin vp->v_u = lvp->v_u;
986 1.1 cgd }
987 1.1 cgd ckfree(lvp);
988 1.1 cgd }
989 1.1 cgd }
990 1.1 cgd
991 1.1 cgd
992 1.10 cgd int
993 1.30 christos setvarcmd(int argc, char **argv)
994 1.10 cgd {
995 1.1 cgd if (argc <= 2)
996 1.1 cgd return unsetcmd(argc, argv);
997 1.1 cgd else if (argc == 3)
998 1.1 cgd setvar(argv[1], argv[2], 0);
999 1.1 cgd else
1000 1.1 cgd error("List assignment not implemented");
1001 1.1 cgd return 0;
1002 1.1 cgd }
1003 1.1 cgd
1004 1.1 cgd
1005 1.1 cgd /*
1006 1.1 cgd * The unset builtin command. We unset the function before we unset the
1007 1.1 cgd * variable to allow a function to be unset when there is a readonly variable
1008 1.1 cgd * with the same name.
1009 1.1 cgd */
1010 1.1 cgd
1011 1.10 cgd int
1012 1.30 christos unsetcmd(int argc, char **argv)
1013 1.10 cgd {
1014 1.1 cgd char **ap;
1015 1.5 jtc int i;
1016 1.5 jtc int flg_func = 0;
1017 1.5 jtc int flg_var = 0;
1018 1.54 kre int flg_x = 0;
1019 1.5 jtc int ret = 0;
1020 1.5 jtc
1021 1.54 kre while ((i = nextopt("efvx")) != '\0') {
1022 1.54 kre switch (i) {
1023 1.54 kre case 'f':
1024 1.5 jtc flg_func = 1;
1025 1.54 kre break;
1026 1.54 kre case 'e':
1027 1.54 kre case 'x':
1028 1.54 kre flg_x = (2 >> (i == 'e'));
1029 1.54 kre /* FALLTHROUGH */
1030 1.54 kre case 'v':
1031 1.54 kre flg_var = 1;
1032 1.54 kre break;
1033 1.54 kre }
1034 1.5 jtc }
1035 1.54 kre
1036 1.5 jtc if (flg_func == 0 && flg_var == 0)
1037 1.5 jtc flg_var = 1;
1038 1.15 christos
1039 1.5 jtc for (ap = argptr; *ap ; ap++) {
1040 1.5 jtc if (flg_func)
1041 1.5 jtc ret |= unsetfunc(*ap);
1042 1.5 jtc if (flg_var)
1043 1.54 kre ret |= unsetvar(*ap, flg_x);
1044 1.1 cgd }
1045 1.5 jtc return ret;
1046 1.1 cgd }
1047 1.1 cgd
1048 1.1 cgd
1049 1.1 cgd /*
1050 1.1 cgd * Unset the specified variable.
1051 1.1 cgd */
1052 1.1 cgd
1053 1.14 christos int
1054 1.30 christos unsetvar(const char *s, int unexport)
1055 1.30 christos {
1056 1.1 cgd struct var **vpp;
1057 1.1 cgd struct var *vp;
1058 1.1 cgd
1059 1.35 dsl vp = find_var(s, &vpp, NULL);
1060 1.35 dsl if (vp == NULL)
1061 1.43 christos return 0;
1062 1.35 dsl
1063 1.54 kre if (vp->flags & VREADONLY && !(unexport & 1))
1064 1.43 christos return 1;
1065 1.35 dsl
1066 1.35 dsl INTOFF;
1067 1.54 kre if (unexport & 1) {
1068 1.35 dsl vp->flags &= ~VEXPORT;
1069 1.35 dsl } else {
1070 1.35 dsl if (vp->text[vp->name_len + 1] != '\0')
1071 1.35 dsl setvar(s, nullstr, 0);
1072 1.54 kre if (!(unexport & 2))
1073 1.54 kre vp->flags &= ~VEXPORT;
1074 1.35 dsl vp->flags |= VUNSET;
1075 1.54 kre if ((vp->flags&(VEXPORT|VSTRFIXED|VREADONLY|VNOEXPORT)) == 0) {
1076 1.35 dsl if ((vp->flags & VTEXTFIXED) == 0)
1077 1.35 dsl ckfree(vp->text);
1078 1.35 dsl *vpp = vp->next;
1079 1.35 dsl ckfree(vp);
1080 1.1 cgd }
1081 1.1 cgd }
1082 1.35 dsl INTON;
1083 1.35 dsl return 0;
1084 1.1 cgd }
1085 1.1 cgd
1086 1.1 cgd
1087 1.1 cgd /*
1088 1.1 cgd * Returns true if the two strings specify the same varable. The first
1089 1.1 cgd * variable name is terminated by '='; the second may be terminated by
1090 1.1 cgd * either '=' or '\0'.
1091 1.1 cgd */
1092 1.1 cgd
1093 1.1 cgd STATIC int
1094 1.35 dsl strequal(const char *p, const char *q)
1095 1.30 christos {
1096 1.1 cgd while (*p == *q++) {
1097 1.1 cgd if (*p++ == '=')
1098 1.1 cgd return 1;
1099 1.1 cgd }
1100 1.1 cgd if (*p == '=' && *(q - 1) == '\0')
1101 1.1 cgd return 1;
1102 1.1 cgd return 0;
1103 1.1 cgd }
1104 1.35 dsl
1105 1.35 dsl /*
1106 1.35 dsl * Search for a variable.
1107 1.35 dsl * 'name' may be terminated by '=' or a NUL.
1108 1.35 dsl * vppp is set to the pointer to vp, or the list head if vp isn't found
1109 1.35 dsl * lenp is set to the number of charactets in 'name'
1110 1.35 dsl */
1111 1.35 dsl
1112 1.35 dsl STATIC struct var *
1113 1.35 dsl find_var(const char *name, struct var ***vppp, int *lenp)
1114 1.35 dsl {
1115 1.35 dsl unsigned int hashval;
1116 1.35 dsl int len;
1117 1.35 dsl struct var *vp, **vpp;
1118 1.35 dsl const char *p = name;
1119 1.35 dsl
1120 1.35 dsl hashval = 0;
1121 1.35 dsl while (*p && *p != '=')
1122 1.35 dsl hashval = 2 * hashval + (unsigned char)*p++;
1123 1.35 dsl len = p - name;
1124 1.35 dsl
1125 1.35 dsl if (lenp)
1126 1.35 dsl *lenp = len;
1127 1.35 dsl vpp = &vartab[hashval % VTABSIZE];
1128 1.35 dsl if (vppp)
1129 1.35 dsl *vppp = vpp;
1130 1.35 dsl
1131 1.35 dsl for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
1132 1.35 dsl if (vp->name_len != len)
1133 1.35 dsl continue;
1134 1.35 dsl if (memcmp(vp->text, name, len) != 0)
1135 1.35 dsl continue;
1136 1.35 dsl if (vppp)
1137 1.35 dsl *vppp = vpp;
1138 1.35 dsl return vp;
1139 1.35 dsl }
1140 1.35 dsl return NULL;
1141 1.35 dsl }
1142 1.55.2.1 snj
1143 1.55.2.1 snj /*
1144 1.55.2.1 snj * The following are the functions that create the values for
1145 1.55.2.1 snj * shell variables that are dynamically produced when needed.
1146 1.55.2.1 snj *
1147 1.55.2.1 snj * The output strings cannot be malloc'd as there is nothing to
1148 1.55.2.1 snj * free them - callers assume these are ordinary variables where
1149 1.55.2.1 snj * the value returned is vp->text
1150 1.55.2.1 snj *
1151 1.55.2.1 snj * Each function needs its own storage space, as the results are
1152 1.55.2.1 snj * used to create processes' environment, and (if exported) all
1153 1.55.2.1 snj * the values will (might) be needed simultaneously.
1154 1.55.2.1 snj *
1155 1.55.2.1 snj * It is not a problem if a var is updated while nominally in use
1156 1.55.2.1 snj * somewhere, all these are intended to be dynamic, the value they
1157 1.55.2.1 snj * return is not guaranteed, an updated vaue is just as good.
1158 1.55.2.1 snj *
1159 1.55.2.1 snj * So, malloc a single buffer for the result of each function,
1160 1.55.2.1 snj * grow, and even shrink, it as needed, but once we have one that
1161 1.55.2.1 snj * is a suitable size for the actual usage, simply hold it forever.
1162 1.55.2.1 snj *
1163 1.55.2.1 snj * For a SMALL shell we implement only LINENO, none of the others,
1164 1.55.2.1 snj * and give it just a fixed length static buffer for its result.
1165 1.55.2.1 snj */
1166 1.55.2.1 snj
1167 1.55.2.1 snj #ifndef SMALL
1168 1.55.2.1 snj
1169 1.55.2.1 snj struct space_reserved { /* record of space allocated for results */
1170 1.55.2.1 snj char *b;
1171 1.55.2.1 snj int len;
1172 1.55.2.1 snj };
1173 1.55.2.1 snj
1174 1.55.2.1 snj /* rough (over-)estimate of the number of bytes needed to hold a number */
1175 1.55.2.1 snj static int
1176 1.55.2.1 snj digits_in(intmax_t number)
1177 1.55.2.1 snj {
1178 1.55.2.1 snj int res = 0;
1179 1.55.2.1 snj
1180 1.55.2.1 snj if (number & ~((1LL << 62) - 1))
1181 1.55.2.1 snj res = 64; /* enough for 2^200 and a bit more */
1182 1.55.2.1 snj else if (number & ~((1LL << 32) - 1))
1183 1.55.2.1 snj res = 20; /* enough for 2^64 */
1184 1.55.2.1 snj else if (number & ~((1 << 23) - 1))
1185 1.55.2.1 snj res = 10; /* enough for 2^32 */
1186 1.55.2.1 snj else
1187 1.55.2.1 snj res = 8; /* enough for 2^23 or smaller */
1188 1.55.2.1 snj
1189 1.55.2.1 snj return res;
1190 1.55.2.1 snj }
1191 1.55.2.1 snj
1192 1.55.2.1 snj static int
1193 1.55.2.1 snj make_space(struct space_reserved *m, int bytes)
1194 1.55.2.1 snj {
1195 1.55.2.1 snj void *p;
1196 1.55.2.1 snj
1197 1.55.2.1 snj if (m->len >= bytes && m->len <= (bytes<<2))
1198 1.55.2.1 snj return 1;
1199 1.55.2.1 snj
1200 1.55.2.1 snj bytes = SHELL_ALIGN(bytes);
1201 1.55.2.1 snj /* not ckrealloc() - we want failure, not error() here */
1202 1.55.2.1 snj p = realloc(m->b, bytes);
1203 1.55.2.1 snj if (p == NULL) /* what we had should still be there */
1204 1.55.2.1 snj return 0;
1205 1.55.2.1 snj
1206 1.55.2.1 snj m->b = p;
1207 1.55.2.1 snj m->len = bytes;
1208 1.55.2.1 snj m->b[bytes - 1] = '\0';
1209 1.55.2.1 snj
1210 1.55.2.1 snj return 1;
1211 1.55.2.1 snj }
1212 1.55.2.1 snj #endif
1213 1.55.2.1 snj
1214 1.55.2.1 snj char *
1215 1.55.2.1 snj get_lineno(struct var *vp)
1216 1.55.2.1 snj {
1217 1.55.2.1 snj #ifdef SMALL
1218 1.55.2.1 snj #define length (8 + 10) /* 10 digits is enough for a 32 bit line num */
1219 1.55.2.1 snj static char result[length];
1220 1.55.2.1 snj #else
1221 1.55.2.1 snj static struct space_reserved buf;
1222 1.55.2.1 snj #define result buf.b
1223 1.55.2.1 snj #define length buf.len
1224 1.55.2.1 snj #endif
1225 1.55.2.1 snj int ln = line_number;
1226 1.55.2.1 snj
1227 1.55.2.1 snj if (vp->flags & VUNSET)
1228 1.55.2.1 snj return NULL;
1229 1.55.2.1 snj
1230 1.55.2.1 snj ln -= funclinebase;
1231 1.55.2.1 snj
1232 1.55.2.1 snj #ifndef SMALL
1233 1.55.2.1 snj if (!make_space(&buf, vp->name_len + 2 + digits_in(ln)))
1234 1.55.2.1 snj return vp->text;
1235 1.55.2.1 snj #endif
1236 1.55.2.1 snj
1237 1.55.2.1 snj snprintf(result, length - 1, "%.*s=%d", vp->name_len, vp->text, ln);
1238 1.55.2.1 snj return result;
1239 1.55.2.1 snj }
1240 1.55.2.1 snj #undef result
1241 1.55.2.1 snj #undef length
1242 1.55.2.1 snj
1243 1.55.2.1 snj #ifndef SMALL
1244 1.55.2.1 snj
1245 1.55.2.1 snj char *
1246 1.55.2.1 snj get_hostname(struct var *vp)
1247 1.55.2.1 snj {
1248 1.55.2.1 snj static struct space_reserved buf;
1249 1.55.2.1 snj
1250 1.55.2.1 snj if (vp->flags & VUNSET)
1251 1.55.2.1 snj return NULL;
1252 1.55.2.1 snj
1253 1.55.2.1 snj if (!make_space(&buf, vp->name_len + 2 + 256))
1254 1.55.2.1 snj return vp->text;
1255 1.55.2.1 snj
1256 1.55.2.1 snj memcpy(buf.b, vp->text, vp->name_len + 1); /* include '=' */
1257 1.55.2.1 snj (void)gethostname(buf.b + vp->name_len + 1,
1258 1.55.2.1 snj buf.len - vp->name_len - 3);
1259 1.55.2.1 snj return buf.b;
1260 1.55.2.1 snj }
1261 1.55.2.1 snj
1262 1.55.2.1 snj char *
1263 1.55.2.1 snj get_tod(struct var *vp)
1264 1.55.2.1 snj {
1265 1.55.2.1 snj static struct space_reserved buf; /* space for answers */
1266 1.55.2.1 snj static struct space_reserved tzs; /* remember TZ last used */
1267 1.55.2.1 snj static timezone_t last_zone; /* timezone data for tzs zone */
1268 1.55.2.1 snj const char *fmt;
1269 1.55.2.1 snj char *tz;
1270 1.55.2.1 snj time_t now;
1271 1.55.2.1 snj struct tm tm_now, *tmp;
1272 1.55.2.1 snj timezone_t zone = NULL;
1273 1.55.2.1 snj static char t_err[] = "time error";
1274 1.55.2.1 snj int len;
1275 1.55.2.1 snj
1276 1.55.2.1 snj if (vp->flags & VUNSET)
1277 1.55.2.1 snj return NULL;
1278 1.55.2.1 snj
1279 1.55.2.1 snj fmt = lookupvar("ToD_FORMAT");
1280 1.55.2.1 snj if (fmt == NULL)
1281 1.55.2.1 snj fmt="%T";
1282 1.55.2.1 snj tz = lookupvar("TZ");
1283 1.55.2.1 snj (void)time(&now);
1284 1.55.2.1 snj
1285 1.55.2.1 snj if (tz != NULL) {
1286 1.55.2.1 snj if (tzs.b == NULL || strcmp(tzs.b, tz) != 0) {
1287 1.55.2.1 snj if (make_space(&tzs, strlen(tz) + 1)) {
1288 1.55.2.1 snj INTOFF;
1289 1.55.2.1 snj strcpy(tzs.b, tz);
1290 1.55.2.1 snj if (last_zone)
1291 1.55.2.1 snj tzfree(last_zone);
1292 1.55.2.1 snj last_zone = zone = tzalloc(tz);
1293 1.55.2.1 snj INTON;
1294 1.55.2.1 snj } else
1295 1.55.2.1 snj zone = tzalloc(tz);
1296 1.55.2.1 snj } else
1297 1.55.2.1 snj zone = last_zone;
1298 1.55.2.1 snj
1299 1.55.2.1 snj tmp = localtime_rz(zone, &now, &tm_now);
1300 1.55.2.1 snj } else
1301 1.55.2.1 snj tmp = localtime_r(&now, &tm_now);
1302 1.55.2.1 snj
1303 1.55.2.1 snj len = (strlen(fmt) * 4) + vp->name_len + 2;
1304 1.55.2.1 snj while (make_space(&buf, len)) {
1305 1.55.2.1 snj memcpy(buf.b, vp->text, vp->name_len+1);
1306 1.55.2.1 snj if (tmp == NULL) {
1307 1.55.2.1 snj if (buf.len >= vp->name_len+2+(int)(sizeof t_err - 1)) {
1308 1.55.2.1 snj strcpy(buf.b + vp->name_len + 1, t_err);
1309 1.55.2.1 snj if (zone && zone != last_zone)
1310 1.55.2.1 snj tzfree(zone);
1311 1.55.2.1 snj return buf.b;
1312 1.55.2.1 snj }
1313 1.55.2.1 snj len = vp->name_len + 4 + sizeof t_err - 1;
1314 1.55.2.1 snj continue;
1315 1.55.2.1 snj }
1316 1.55.2.1 snj if (strftime_z(zone, buf.b + vp->name_len + 1,
1317 1.55.2.1 snj buf.len - vp->name_len - 2, fmt, tmp)) {
1318 1.55.2.1 snj if (zone && zone != last_zone)
1319 1.55.2.1 snj tzfree(zone);
1320 1.55.2.1 snj return buf.b;
1321 1.55.2.1 snj }
1322 1.55.2.1 snj if (len >= 4096) /* Let's be reasonable */
1323 1.55.2.1 snj break;
1324 1.55.2.1 snj len <<= 1;
1325 1.55.2.1 snj }
1326 1.55.2.1 snj if (zone && zone != last_zone)
1327 1.55.2.1 snj tzfree(zone);
1328 1.55.2.1 snj return vp->text;
1329 1.55.2.1 snj }
1330 1.55.2.1 snj
1331 1.55.2.1 snj char *
1332 1.55.2.1 snj get_seconds(struct var *vp)
1333 1.55.2.1 snj {
1334 1.55.2.1 snj static struct space_reserved buf;
1335 1.55.2.1 snj intmax_t secs;
1336 1.55.2.1 snj
1337 1.55.2.1 snj if (vp->flags & VUNSET)
1338 1.55.2.1 snj return NULL;
1339 1.55.2.1 snj
1340 1.55.2.1 snj secs = (intmax_t)time((time_t *)0) - sh_start_time;
1341 1.55.2.1 snj if (!make_space(&buf, vp->name_len + 2 + digits_in(secs)))
1342 1.55.2.1 snj return vp->text;
1343 1.55.2.1 snj
1344 1.55.2.1 snj snprintf(buf.b, buf.len-1, "%.*s=%jd", vp->name_len, vp->text, secs);
1345 1.55.2.1 snj return buf.b;
1346 1.55.2.1 snj }
1347 1.55.2.1 snj
1348 1.55.2.1 snj char *
1349 1.55.2.1 snj get_euser(struct var *vp)
1350 1.55.2.1 snj {
1351 1.55.2.1 snj static struct space_reserved buf;
1352 1.55.2.1 snj static uid_t lastuid = 0;
1353 1.55.2.1 snj uid_t euid;
1354 1.55.2.1 snj struct passwd *pw;
1355 1.55.2.1 snj
1356 1.55.2.1 snj if (vp->flags & VUNSET)
1357 1.55.2.1 snj return NULL;
1358 1.55.2.1 snj
1359 1.55.2.1 snj euid = geteuid();
1360 1.55.2.1 snj if (buf.b != NULL && lastuid == euid)
1361 1.55.2.1 snj return buf.b;
1362 1.55.2.1 snj
1363 1.55.2.1 snj pw = getpwuid(euid);
1364 1.55.2.1 snj if (pw == NULL)
1365 1.55.2.1 snj return vp->text;
1366 1.55.2.1 snj
1367 1.55.2.1 snj if (make_space(&buf, vp->name_len + 2 + strlen(pw->pw_name))) {
1368 1.55.2.1 snj lastuid = euid;
1369 1.55.2.1 snj snprintf(buf.b, buf.len, "%.*s=%s", vp->name_len, vp->text,
1370 1.55.2.1 snj pw->pw_name);
1371 1.55.2.1 snj return buf.b;
1372 1.55.2.1 snj }
1373 1.55.2.1 snj
1374 1.55.2.1 snj return vp->text;
1375 1.55.2.1 snj }
1376 1.55.2.1 snj
1377 1.55.2.1 snj char *
1378 1.55.2.1 snj get_random(struct var *vp)
1379 1.55.2.1 snj {
1380 1.55.2.1 snj static struct space_reserved buf;
1381 1.55.2.1 snj static intmax_t random_val = 0;
1382 1.55.2.1 snj
1383 1.55.2.1 snj #ifdef USE_LRAND48
1384 1.55.2.1 snj #define random lrand48
1385 1.55.2.1 snj #define srandom srand48
1386 1.55.2.1 snj #endif
1387 1.55.2.1 snj
1388 1.55.2.1 snj if (vp->flags & VUNSET)
1389 1.55.2.1 snj return NULL;
1390 1.55.2.1 snj
1391 1.55.2.1 snj if (vp->text != buf.b) {
1392 1.55.2.1 snj /*
1393 1.55.2.1 snj * Either initialisation, or a new seed has been set
1394 1.55.2.1 snj */
1395 1.55.2.1 snj if (vp->text[vp->name_len + 1] == '\0') {
1396 1.55.2.1 snj int fd;
1397 1.55.2.1 snj
1398 1.55.2.1 snj /*
1399 1.55.2.1 snj * initialisation (without pre-seeding),
1400 1.55.2.1 snj * or explictly requesting a truly random seed.
1401 1.55.2.1 snj */
1402 1.55.2.1 snj fd = open("/dev/urandom", 0);
1403 1.55.2.1 snj if (fd == -1) {
1404 1.55.2.1 snj out2str("RANDOM initialisation failed\n");
1405 1.55.2.1 snj random_val = (getpid()<<3) ^ time((time_t *)0);
1406 1.55.2.1 snj } else {
1407 1.55.2.1 snj int n;
1408 1.55.2.1 snj
1409 1.55.2.1 snj do {
1410 1.55.2.1 snj n = read(fd,&random_val,sizeof random_val);
1411 1.55.2.1 snj } while (n != sizeof random_val);
1412 1.55.2.1 snj close(fd);
1413 1.55.2.1 snj }
1414 1.55.2.1 snj } else
1415 1.55.2.1 snj /* good enough for today */
1416 1.55.2.3 martin random_val = strtoimax(vp->text+vp->name_len+1,NULL,0);
1417 1.55.2.1 snj
1418 1.55.2.1 snj srandom((long)random_val);
1419 1.55.2.1 snj }
1420 1.55.2.1 snj
1421 1.55.2.1 snj #if 0
1422 1.55.2.1 snj random_val = (random_val + 1) & 0x7FFF; /* 15 bit "random" numbers */
1423 1.55.2.1 snj #else
1424 1.55.2.1 snj random_val = (random() >> 5) & 0x7FFF;
1425 1.55.2.1 snj #endif
1426 1.55.2.1 snj
1427 1.55.2.1 snj if (!make_space(&buf, vp->name_len + 2 + digits_in(random_val)))
1428 1.55.2.1 snj return vp->text;
1429 1.55.2.1 snj
1430 1.55.2.1 snj snprintf(buf.b, buf.len-1, "%.*s=%jd", vp->name_len, vp->text,
1431 1.55.2.1 snj random_val);
1432 1.55.2.1 snj
1433 1.55.2.1 snj if (buf.b != vp->text && (vp->flags & (VTEXTFIXED|VSTACK)) == 0)
1434 1.55.2.1 snj free(vp->text);
1435 1.55.2.1 snj vp->flags |= VTEXTFIXED;
1436 1.55.2.1 snj vp->text = buf.b;
1437 1.55.2.1 snj
1438 1.55.2.1 snj return vp->text;
1439 1.55.2.1 snj #undef random
1440 1.55.2.1 snj #undef srandom
1441 1.55.2.1 snj }
1442 1.55.2.1 snj
1443 1.55.2.1 snj #endif /* SMALL */
1444