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