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