2006-04-24 23:00:29 +04:00
|
|
|
/* $NetBSD: var.c,v 1.37 2006/04/24 19:00:30 snj Exp $ */
|
1995-03-21 12:01:59 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*-
|
1994-05-11 21:09:42 +04:00
|
|
|
* Copyright (c) 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Kenneth Almquist.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 13:05:01 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-07-05 01:01:48 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1995-03-21 12:01:59 +03:00
|
|
|
#if 0
|
1995-05-12 01:28:33 +04:00
|
|
|
static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 5/4/95";
|
1995-03-21 12:01:59 +03:00
|
|
|
#else
|
2006-04-24 23:00:29 +04:00
|
|
|
__RCSID("$NetBSD: var.c,v 1.37 2006/04/24 19:00:30 snj Exp $");
|
1995-03-21 12:01:59 +03:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
1995-05-12 01:28:33 +04:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
2003-08-26 22:14:24 +04:00
|
|
|
#include <strings.h>
|
1998-04-08 01:15:33 +04:00
|
|
|
#include <paths.h>
|
1995-05-12 01:28:33 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* Shell variables.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "shell.h"
|
|
|
|
#include "output.h"
|
|
|
|
#include "expand.h"
|
|
|
|
#include "nodes.h" /* for other headers */
|
|
|
|
#include "eval.h" /* defines cmdenviron */
|
|
|
|
#include "exec.h"
|
|
|
|
#include "syntax.h"
|
|
|
|
#include "options.h"
|
|
|
|
#include "mail.h"
|
|
|
|
#include "var.h"
|
|
|
|
#include "memalloc.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "mystring.h"
|
1996-10-16 19:24:31 +04:00
|
|
|
#include "parser.h"
|
2003-01-22 23:36:03 +03:00
|
|
|
#include "show.h"
|
1997-03-14 04:42:18 +03:00
|
|
|
#ifndef SMALL
|
1995-05-12 01:28:33 +04:00
|
|
|
#include "myhistedit.h"
|
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2004-10-02 16:16:53 +04:00
|
|
|
#ifdef SMALL
|
1993-03-21 12:45:37 +03:00
|
|
|
#define VTABSIZE 39
|
2004-10-02 16:16:53 +04:00
|
|
|
#else
|
|
|
|
#define VTABSIZE 517
|
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
|
|
|
|
struct varinit {
|
|
|
|
struct var *var;
|
|
|
|
int flags;
|
1999-07-09 07:05:49 +04:00
|
|
|
const char *text;
|
2002-11-25 01:35:38 +03:00
|
|
|
void (*func)(const char *);
|
1993-03-21 12:45:37 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#if ATTY
|
|
|
|
struct var vatty;
|
|
|
|
#endif
|
1997-03-14 04:42:18 +03:00
|
|
|
#ifndef SMALL
|
1994-05-11 21:09:42 +04:00
|
|
|
struct var vhistsize;
|
1997-04-12 02:45:38 +04:00
|
|
|
struct var vterm;
|
1994-05-15 02:20:04 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
struct var vifs;
|
|
|
|
struct var vmail;
|
|
|
|
struct var vmpath;
|
|
|
|
struct var vpath;
|
|
|
|
struct var vps1;
|
|
|
|
struct var vps2;
|
2003-01-22 23:36:03 +03:00
|
|
|
struct var vps4;
|
1993-03-21 12:45:37 +03:00
|
|
|
struct var vvers;
|
1996-06-25 20:49:05 +04:00
|
|
|
struct var voptind;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
const struct varinit varinit[] = {
|
|
|
|
#if ATTY
|
1996-06-25 20:49:05 +04:00
|
|
|
{ &vatty, VSTRFIXED|VTEXTFIXED|VUNSET, "ATTY=",
|
|
|
|
NULL },
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif
|
1997-03-14 04:42:18 +03:00
|
|
|
#ifndef SMALL
|
1996-06-25 20:49:05 +04:00
|
|
|
{ &vhistsize, VSTRFIXED|VTEXTFIXED|VUNSET, "HISTSIZE=",
|
|
|
|
sethistsize },
|
1994-05-15 02:20:04 +04:00
|
|
|
#endif
|
1996-06-25 20:49:05 +04:00
|
|
|
{ &vifs, VSTRFIXED|VTEXTFIXED, "IFS= \t\n",
|
|
|
|
NULL },
|
|
|
|
{ &vmail, VSTRFIXED|VTEXTFIXED|VUNSET, "MAIL=",
|
|
|
|
NULL },
|
|
|
|
{ &vmpath, VSTRFIXED|VTEXTFIXED|VUNSET, "MAILPATH=",
|
|
|
|
NULL },
|
2000-12-20 03:15:10 +03:00
|
|
|
{ &vpath, VSTRFIXED|VTEXTFIXED, "PATH=" _PATH_DEFPATH,
|
1996-06-25 20:49:05 +04:00
|
|
|
changepath },
|
1996-10-16 19:24:31 +04:00
|
|
|
/*
|
1993-03-21 12:45:37 +03:00
|
|
|
* vps1 depends on uid
|
|
|
|
*/
|
1996-06-25 20:49:05 +04:00
|
|
|
{ &vps2, VSTRFIXED|VTEXTFIXED, "PS2=> ",
|
|
|
|
NULL },
|
2003-01-22 23:36:03 +03:00
|
|
|
{ &vps4, VSTRFIXED|VTEXTFIXED, "PS4=+ ",
|
|
|
|
NULL },
|
1997-04-12 02:45:38 +04:00
|
|
|
#ifndef SMALL
|
1996-06-25 20:49:05 +04:00
|
|
|
{ &vterm, VSTRFIXED|VTEXTFIXED|VUNSET, "TERM=",
|
1997-04-12 02:45:38 +04:00
|
|
|
setterm },
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif
|
2003-01-22 23:36:03 +03:00
|
|
|
{ &voptind, VSTRFIXED|VTEXTFIXED|VNOFUNC, "OPTIND=1",
|
1996-06-25 20:49:05 +04:00
|
|
|
getoptsreset },
|
|
|
|
{ NULL, 0, NULL,
|
|
|
|
NULL }
|
1993-03-21 12:45:37 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
struct var *vartab[VTABSIZE];
|
|
|
|
|
2004-10-02 16:16:53 +04:00
|
|
|
STATIC int strequal(const char *, const char *);
|
|
|
|
STATIC struct var *find_var(const char *, struct var ***, int *);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize the varable symbol tables and import the environment
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef mkinit
|
|
|
|
INCLUDE "var.h"
|
2001-02-04 22:52:06 +03:00
|
|
|
MKINIT char **environ;
|
1993-03-21 12:45:37 +03:00
|
|
|
INIT {
|
|
|
|
char **envp;
|
|
|
|
|
|
|
|
initvar();
|
|
|
|
for (envp = environ ; *envp ; envp++) {
|
|
|
|
if (strchr(*envp, '=')) {
|
|
|
|
setvareq(*envp, VEXPORT|VTEXTFIXED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This routine initializes the builtin variables. It is called when the
|
|
|
|
* shell is initialized and again when a shell procedure is spawned.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2002-11-25 01:35:38 +03:00
|
|
|
initvar(void)
|
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
const struct varinit *ip;
|
|
|
|
struct var *vp;
|
|
|
|
struct var **vpp;
|
|
|
|
|
|
|
|
for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
|
2004-10-02 16:16:53 +04:00
|
|
|
if (find_var(ip->text, &vpp, &vp->name_len) != NULL)
|
|
|
|
continue;
|
|
|
|
vp->next = *vpp;
|
|
|
|
*vpp = vp;
|
|
|
|
vp->text = strdup(ip->text);
|
|
|
|
vp->flags = ip->flags;
|
|
|
|
vp->func = ip->func;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* PS1 depends on uid
|
|
|
|
*/
|
2004-10-02 16:16:53 +04:00
|
|
|
if (find_var("PS1", &vpp, &vps1.name_len) == NULL) {
|
1993-03-21 12:45:37 +03:00
|
|
|
vps1.next = *vpp;
|
|
|
|
*vpp = &vps1;
|
1999-07-09 07:05:49 +04:00
|
|
|
vps1.text = strdup(geteuid() ? "PS1=$ " : "PS1=# ");
|
1993-03-21 12:45:37 +03:00
|
|
|
vps1.flags = VSTRFIXED|VTEXTFIXED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-06-25 20:49:05 +04:00
|
|
|
/*
|
|
|
|
* Safe version of setvar, returns 1 on success 0 on failure.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2002-11-25 01:35:38 +03:00
|
|
|
setvarsafe(const char *name, const char *val, int flags)
|
1996-06-25 20:49:05 +04:00
|
|
|
{
|
|
|
|
struct jmploc jmploc;
|
|
|
|
struct jmploc *volatile savehandler = handler;
|
|
|
|
int err = 0;
|
1997-07-05 01:01:48 +04:00
|
|
|
#ifdef __GNUC__
|
|
|
|
(void) &err;
|
|
|
|
#endif
|
1996-06-25 20:49:05 +04:00
|
|
|
|
|
|
|
if (setjmp(jmploc.loc))
|
|
|
|
err = 1;
|
|
|
|
else {
|
|
|
|
handler = &jmploc;
|
|
|
|
setvar(name, val, flags);
|
|
|
|
}
|
|
|
|
handler = savehandler;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* Set the value of a variable. The flags argument is ored with the
|
|
|
|
* flags of the variable. If val is NULL, the variable is unset.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2002-11-25 01:35:38 +03:00
|
|
|
setvar(const char *name, const char *val, int flags)
|
1994-12-05 22:07:32 +03:00
|
|
|
{
|
1999-07-09 07:05:49 +04:00
|
|
|
const char *p;
|
|
|
|
const char *q;
|
|
|
|
char *d;
|
1993-03-21 12:45:37 +03:00
|
|
|
int len;
|
|
|
|
int namelen;
|
|
|
|
char *nameeq;
|
|
|
|
int isbad;
|
|
|
|
|
|
|
|
isbad = 0;
|
|
|
|
p = name;
|
1996-10-16 19:24:31 +04:00
|
|
|
if (! is_name(*p))
|
1993-03-21 12:45:37 +03:00
|
|
|
isbad = 1;
|
1996-10-16 19:24:31 +04:00
|
|
|
p++;
|
1993-03-21 12:45:37 +03:00
|
|
|
for (;;) {
|
|
|
|
if (! is_in_name(*p)) {
|
|
|
|
if (*p == '\0' || *p == '=')
|
|
|
|
break;
|
|
|
|
isbad = 1;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
namelen = p - name;
|
|
|
|
if (isbad)
|
1994-05-11 21:09:42 +04:00
|
|
|
error("%.*s: bad variable name", namelen, name);
|
1993-03-21 12:45:37 +03:00
|
|
|
len = namelen + 2; /* 2 is space for '=' and '\0' */
|
|
|
|
if (val == NULL) {
|
|
|
|
flags |= VUNSET;
|
|
|
|
} else {
|
|
|
|
len += strlen(val);
|
|
|
|
}
|
1999-07-09 07:05:49 +04:00
|
|
|
d = nameeq = ckmalloc(len);
|
1993-03-21 12:45:37 +03:00
|
|
|
q = name;
|
|
|
|
while (--namelen >= 0)
|
1999-07-09 07:05:49 +04:00
|
|
|
*d++ = *q++;
|
|
|
|
*d++ = '=';
|
|
|
|
*d = '\0';
|
1993-03-21 12:45:37 +03:00
|
|
|
if (val)
|
1999-07-09 07:05:49 +04:00
|
|
|
scopy(val, d);
|
1993-03-21 12:45:37 +03:00
|
|
|
setvareq(nameeq, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Same as setvar except that the variable and value are passed in
|
|
|
|
* the first argument as name=value. Since the first argument will
|
|
|
|
* be actually stored in the table, it should not be a string that
|
|
|
|
* will go away.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2002-11-25 01:35:38 +03:00
|
|
|
setvareq(char *s, int flags)
|
1994-12-05 22:07:32 +03:00
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
struct var *vp, **vpp;
|
2004-10-02 16:16:53 +04:00
|
|
|
int nlen;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2002-05-15 23:43:29 +04:00
|
|
|
if (aflag)
|
|
|
|
flags |= VEXPORT;
|
2004-10-02 16:16:53 +04:00
|
|
|
vp = find_var(s, &vpp, &nlen);
|
|
|
|
if (vp != NULL) {
|
|
|
|
if (vp->flags & VREADONLY)
|
|
|
|
error("%.*s: is read only", vp->name_len, s);
|
|
|
|
if (flags & VNOSET)
|
|
|
|
return;
|
|
|
|
INTOFF;
|
1996-06-25 20:49:05 +04:00
|
|
|
|
2004-10-02 16:16:53 +04:00
|
|
|
if (vp->func && (flags & VNOFUNC) == 0)
|
|
|
|
(*vp->func)(s + vp->name_len + 1);
|
1996-06-25 20:49:05 +04:00
|
|
|
|
2004-10-02 16:16:53 +04:00
|
|
|
if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
|
|
|
|
ckfree(vp->text);
|
1996-06-25 20:49:05 +04:00
|
|
|
|
2004-10-02 16:16:53 +04:00
|
|
|
vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
|
|
|
|
vp->flags |= flags & ~VNOFUNC;
|
|
|
|
vp->text = s;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We could roll this to a function, to handle it as
|
|
|
|
* a regular variable function callback, but why bother?
|
|
|
|
*/
|
|
|
|
if (vp == &vmpath || (vp == &vmail && ! mpathset()))
|
|
|
|
chkmail(1);
|
|
|
|
INTON;
|
|
|
|
return;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
/* not found */
|
2002-11-25 01:35:38 +03:00
|
|
|
if (flags & VNOSET)
|
|
|
|
return;
|
1993-03-21 12:45:37 +03:00
|
|
|
vp = ckmalloc(sizeof (*vp));
|
2003-01-22 23:36:03 +03:00
|
|
|
vp->flags = flags & ~VNOFUNC;
|
1993-03-21 12:45:37 +03:00
|
|
|
vp->text = s;
|
2004-10-02 16:16:53 +04:00
|
|
|
vp->name_len = nlen;
|
1993-03-21 12:45:37 +03:00
|
|
|
vp->next = *vpp;
|
1996-06-25 20:49:05 +04:00
|
|
|
vp->func = NULL;
|
1993-03-21 12:45:37 +03:00
|
|
|
*vpp = vp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process a linked list of variable assignments.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2002-11-25 01:35:38 +03:00
|
|
|
listsetvar(struct strlist *list, int flags)
|
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
struct strlist *lp;
|
|
|
|
|
|
|
|
INTOFF;
|
|
|
|
for (lp = list ; lp ; lp = lp->next) {
|
2002-11-25 01:35:38 +03:00
|
|
|
setvareq(savestr(lp->text), flags);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
INTON;
|
|
|
|
}
|
|
|
|
|
2003-01-22 23:36:03 +03:00
|
|
|
void
|
|
|
|
listmklocal(struct strlist *list, int flags)
|
|
|
|
{
|
|
|
|
struct strlist *lp;
|
|
|
|
|
|
|
|
for (lp = list ; lp ; lp = lp->next)
|
|
|
|
mklocal(lp->text, flags);
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the value of a variable. Returns NULL if not set.
|
|
|
|
*/
|
|
|
|
|
|
|
|
char *
|
2002-11-25 01:35:38 +03:00
|
|
|
lookupvar(const char *name)
|
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
struct var *v;
|
|
|
|
|
2004-10-02 16:16:53 +04:00
|
|
|
v = find_var(name, NULL, NULL);
|
|
|
|
if (v == NULL || v->flags & VUNSET)
|
|
|
|
return NULL;
|
|
|
|
return v->text + v->name_len + 1;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Search the environment of a builtin command. If the second argument
|
|
|
|
* is nonzero, return the value of a variable even if it hasn't been
|
|
|
|
* exported.
|
|
|
|
*/
|
|
|
|
|
|
|
|
char *
|
2002-11-25 01:35:38 +03:00
|
|
|
bltinlookup(const char *name, int doall)
|
1994-12-05 22:07:32 +03:00
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
struct strlist *sp;
|
|
|
|
struct var *v;
|
|
|
|
|
|
|
|
for (sp = cmdenviron ; sp ; sp = sp->next) {
|
2004-10-02 16:16:53 +04:00
|
|
|
if (strequal(sp->text, name))
|
1993-03-21 12:45:37 +03:00
|
|
|
return strchr(sp->text, '=') + 1;
|
|
|
|
}
|
2004-10-02 16:16:53 +04:00
|
|
|
|
|
|
|
v = find_var(name, NULL, NULL);
|
|
|
|
|
|
|
|
if (v == NULL || v->flags & VUNSET || (!doall && !(v->flags & VEXPORT)))
|
|
|
|
return NULL;
|
|
|
|
return v->text + v->name_len + 1;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generate a list of exported variables. This routine is used to construct
|
|
|
|
* the third argument to execve when executing a program.
|
|
|
|
*/
|
|
|
|
|
|
|
|
char **
|
2002-11-25 01:35:38 +03:00
|
|
|
environment(void)
|
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
int nenv;
|
|
|
|
struct var **vpp;
|
|
|
|
struct var *vp;
|
1999-07-09 07:05:49 +04:00
|
|
|
char **env;
|
|
|
|
char **ep;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
nenv = 0;
|
|
|
|
for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
|
|
|
|
for (vp = *vpp ; vp ; vp = vp->next)
|
|
|
|
if (vp->flags & VEXPORT)
|
|
|
|
nenv++;
|
|
|
|
}
|
|
|
|
ep = env = stalloc((nenv + 1) * sizeof *env);
|
|
|
|
for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
|
|
|
|
for (vp = *vpp ; vp ; vp = vp->next)
|
|
|
|
if (vp->flags & VEXPORT)
|
|
|
|
*ep++ = vp->text;
|
|
|
|
}
|
|
|
|
*ep = NULL;
|
|
|
|
return env;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called when a shell procedure is invoked to clear out nonexported
|
|
|
|
* variables. It is also necessary to reallocate variables of with
|
|
|
|
* VSTACK set since these are currently allocated on the stack.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef mkinit
|
2002-11-25 01:35:38 +03:00
|
|
|
void shprocvar(void);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
SHELLPROC {
|
|
|
|
shprocvar();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
2002-11-25 01:35:38 +03:00
|
|
|
shprocvar(void)
|
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
struct var **vpp;
|
|
|
|
struct var *vp, **prev;
|
|
|
|
|
|
|
|
for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
|
|
|
|
for (prev = vpp ; (vp = *prev) != NULL ; ) {
|
|
|
|
if ((vp->flags & VEXPORT) == 0) {
|
|
|
|
*prev = vp->next;
|
|
|
|
if ((vp->flags & VTEXTFIXED) == 0)
|
|
|
|
ckfree(vp->text);
|
|
|
|
if ((vp->flags & VSTRFIXED) == 0)
|
|
|
|
ckfree(vp);
|
|
|
|
} else {
|
|
|
|
if (vp->flags & VSTACK) {
|
|
|
|
vp->text = savestr(vp->text);
|
|
|
|
vp->flags &=~ VSTACK;
|
|
|
|
}
|
|
|
|
prev = &vp->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
initvar();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Command to list all variables which are set. Currently this command
|
|
|
|
* is invoked from the set command when the set command is called without
|
|
|
|
* any variables.
|
|
|
|
*/
|
|
|
|
|
2002-11-25 01:35:38 +03:00
|
|
|
void
|
|
|
|
print_quoted(const char *p)
|
|
|
|
{
|
|
|
|
const char *q;
|
|
|
|
|
|
|
|
if (strcspn(p, "|&;<>()$`\\\"' \t\n*?[]#~=%") == strlen(p)) {
|
|
|
|
out1fmt("%s", p);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while (*p) {
|
|
|
|
if (*p == '\'') {
|
|
|
|
out1fmt("\\'");
|
|
|
|
p++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
q = index(p, '\'');
|
|
|
|
if (!q) {
|
|
|
|
out1fmt("'%s'", p );
|
|
|
|
return;
|
|
|
|
}
|
2002-11-25 15:13:03 +03:00
|
|
|
out1fmt("'%.*s'", (int)(q - p), p );
|
2002-11-25 01:35:38 +03:00
|
|
|
p = q;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
sort_var(const void *v_v1, const void *v_v2)
|
|
|
|
{
|
|
|
|
const struct var * const *v1 = v_v1;
|
|
|
|
const struct var * const *v2 = v_v2;
|
|
|
|
|
|
|
|
/* XXX Will anyone notice we include the '=' of the shorter name? */
|
|
|
|
return strcoll((*v1)->text, (*v2)->text);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* POSIX requires that 'set' (but not export or readonly) output the
|
|
|
|
* variables in lexicographic order - by the locale's collating order (sigh).
|
|
|
|
* Maybe we could keep them in an ordered balanced binary tree
|
|
|
|
* instead of hashed lists.
|
|
|
|
* For now just roll 'em through qsort for printing...
|
|
|
|
*/
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
int
|
2002-11-25 01:35:38 +03:00
|
|
|
showvars(const char *name, int flag, int show_value)
|
1994-12-05 22:07:32 +03:00
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
struct var **vpp;
|
|
|
|
struct var *vp;
|
2002-11-25 01:35:38 +03:00
|
|
|
const char *p;
|
|
|
|
|
|
|
|
static struct var **list; /* static in case we are interrupted */
|
|
|
|
static int list_len;
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
if (!list) {
|
|
|
|
list_len = 32;
|
|
|
|
list = ckmalloc(list_len * sizeof *list);
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
|
|
|
|
for (vp = *vpp ; vp ; vp = vp->next) {
|
2002-11-25 01:35:38 +03:00
|
|
|
if (flag && !(vp->flags & flag))
|
|
|
|
continue;
|
|
|
|
if (vp->flags & VUNSET && !(show_value & 2))
|
|
|
|
continue;
|
|
|
|
if (count >= list_len) {
|
|
|
|
list = ckrealloc(list,
|
|
|
|
(list_len << 1) * sizeof *list);
|
|
|
|
list_len <<= 1;
|
|
|
|
}
|
|
|
|
list[count++] = vp;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
2002-11-25 01:35:38 +03:00
|
|
|
|
|
|
|
qsort(list, count, sizeof *list, sort_var);
|
|
|
|
|
|
|
|
for (vpp = list; count--; vpp++) {
|
|
|
|
vp = *vpp;
|
|
|
|
if (name)
|
|
|
|
out1fmt("%s ", name);
|
|
|
|
for (p = vp->text ; *p != '=' ; p++)
|
|
|
|
out1c(*p);
|
|
|
|
if (!(vp->flags & VUNSET) && show_value) {
|
|
|
|
out1fmt("=");
|
|
|
|
print_quoted(++p);
|
|
|
|
}
|
|
|
|
out1c('\n');
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The export and readonly commands.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2002-11-25 01:35:38 +03:00
|
|
|
exportcmd(int argc, char **argv)
|
1994-12-05 22:07:32 +03:00
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
struct var *vp;
|
|
|
|
char *name;
|
1999-07-09 07:05:49 +04:00
|
|
|
const char *p;
|
1993-03-21 12:45:37 +03:00
|
|
|
int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
|
1999-01-28 21:11:50 +03:00
|
|
|
int pflag;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2002-11-25 01:35:38 +03:00
|
|
|
pflag = nextopt("p") == 'p' ? 3 : 0;
|
|
|
|
if (argc <= 1 || pflag) {
|
|
|
|
showvars( pflag ? argv[0] : 0, flag, pflag );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((name = *argptr++) != NULL) {
|
|
|
|
if ((p = strchr(name, '=')) != NULL) {
|
|
|
|
p++;
|
|
|
|
} else {
|
2004-10-02 16:16:53 +04:00
|
|
|
vp = find_var(name, NULL, NULL);
|
|
|
|
if (vp != NULL) {
|
|
|
|
vp->flags |= flag;
|
2004-10-06 14:23:43 +04:00
|
|
|
continue;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
2002-11-25 01:35:38 +03:00
|
|
|
setvar(name, p, flag);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The "local" command.
|
|
|
|
*/
|
|
|
|
|
1994-12-05 22:07:32 +03:00
|
|
|
int
|
2002-11-25 01:35:38 +03:00
|
|
|
localcmd(int argc, char **argv)
|
1994-12-05 22:07:32 +03:00
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
char *name;
|
|
|
|
|
|
|
|
if (! in_function())
|
|
|
|
error("Not in a function");
|
|
|
|
while ((name = *argptr++) != NULL) {
|
2002-09-27 22:56:50 +04:00
|
|
|
mklocal(name, 0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2006-04-24 23:00:29 +04:00
|
|
|
* Make a variable a local variable. When a variable is made local, its
|
1993-03-21 12:45:37 +03:00
|
|
|
* value and flags are saved in a localvar structure. The saved values
|
|
|
|
* will be restored when the shell function returns. We handle the name
|
|
|
|
* "-" as a special case.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2003-01-22 23:36:03 +03:00
|
|
|
mklocal(const char *name, int flags)
|
2002-11-25 01:35:38 +03:00
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
struct localvar *lvp;
|
|
|
|
struct var **vpp;
|
|
|
|
struct var *vp;
|
|
|
|
|
|
|
|
INTOFF;
|
|
|
|
lvp = ckmalloc(sizeof (struct localvar));
|
|
|
|
if (name[0] == '-' && name[1] == '\0') {
|
1999-07-09 07:05:49 +04:00
|
|
|
char *p;
|
2002-11-25 01:35:38 +03:00
|
|
|
p = ckmalloc(sizeof_optlist);
|
|
|
|
lvp->text = memcpy(p, optlist, sizeof_optlist);
|
1993-03-21 12:45:37 +03:00
|
|
|
vp = NULL;
|
|
|
|
} else {
|
2004-10-02 16:16:53 +04:00
|
|
|
vp = find_var(name, &vpp, NULL);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (vp == NULL) {
|
|
|
|
if (strchr(name, '='))
|
2002-09-27 22:56:50 +04:00
|
|
|
setvareq(savestr(name), VSTRFIXED|flags);
|
1993-03-21 12:45:37 +03:00
|
|
|
else
|
2002-09-27 22:56:50 +04:00
|
|
|
setvar(name, NULL, VSTRFIXED|flags);
|
1993-03-21 12:45:37 +03:00
|
|
|
vp = *vpp; /* the new variable */
|
|
|
|
lvp->text = NULL;
|
|
|
|
lvp->flags = VUNSET;
|
|
|
|
} else {
|
|
|
|
lvp->text = vp->text;
|
|
|
|
lvp->flags = vp->flags;
|
|
|
|
vp->flags |= VSTRFIXED|VTEXTFIXED;
|
2004-10-02 16:16:53 +04:00
|
|
|
if (name[vp->name_len] == '=')
|
2002-09-27 22:56:50 +04:00
|
|
|
setvareq(savestr(name), flags);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
lvp->vp = vp;
|
|
|
|
lvp->next = localvars;
|
|
|
|
localvars = lvp;
|
|
|
|
INTON;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called after a function returns.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2002-11-25 01:35:38 +03:00
|
|
|
poplocalvars(void)
|
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
struct localvar *lvp;
|
|
|
|
struct var *vp;
|
|
|
|
|
|
|
|
while ((lvp = localvars) != NULL) {
|
|
|
|
localvars = lvp->next;
|
|
|
|
vp = lvp->vp;
|
2003-01-22 23:36:03 +03:00
|
|
|
TRACE(("poplocalvar %s", vp ? vp->text : "-"));
|
1993-03-21 12:45:37 +03:00
|
|
|
if (vp == NULL) { /* $- saved */
|
2002-11-25 01:35:38 +03:00
|
|
|
memcpy(optlist, lvp->text, sizeof_optlist);
|
1993-03-21 12:45:37 +03:00
|
|
|
ckfree(lvp->text);
|
|
|
|
} else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
|
2002-11-25 01:35:38 +03:00
|
|
|
(void)unsetvar(vp->text, 0);
|
1993-03-21 12:45:37 +03:00
|
|
|
} else {
|
2003-01-22 23:36:03 +03:00
|
|
|
if (vp->func && (vp->flags & VNOFUNC) == 0)
|
2004-10-02 16:16:53 +04:00
|
|
|
(*vp->func)(lvp->text + vp->name_len + 1);
|
1993-03-21 12:45:37 +03:00
|
|
|
if ((vp->flags & VTEXTFIXED) == 0)
|
|
|
|
ckfree(vp->text);
|
|
|
|
vp->flags = lvp->flags;
|
|
|
|
vp->text = lvp->text;
|
|
|
|
}
|
|
|
|
ckfree(lvp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-12-05 22:07:32 +03:00
|
|
|
int
|
2002-11-25 01:35:38 +03:00
|
|
|
setvarcmd(int argc, char **argv)
|
1994-12-05 22:07:32 +03:00
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
if (argc <= 2)
|
|
|
|
return unsetcmd(argc, argv);
|
|
|
|
else if (argc == 3)
|
|
|
|
setvar(argv[1], argv[2], 0);
|
|
|
|
else
|
|
|
|
error("List assignment not implemented");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The unset builtin command. We unset the function before we unset the
|
|
|
|
* variable to allow a function to be unset when there is a readonly variable
|
|
|
|
* with the same name.
|
|
|
|
*/
|
|
|
|
|
1994-12-05 22:07:32 +03:00
|
|
|
int
|
2002-11-25 01:35:38 +03:00
|
|
|
unsetcmd(int argc, char **argv)
|
1994-12-05 22:07:32 +03:00
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
char **ap;
|
1994-05-11 21:09:42 +04:00
|
|
|
int i;
|
|
|
|
int flg_func = 0;
|
|
|
|
int flg_var = 0;
|
|
|
|
int ret = 0;
|
|
|
|
|
2002-11-25 01:35:38 +03:00
|
|
|
while ((i = nextopt("evf")) != '\0') {
|
1994-05-11 21:09:42 +04:00
|
|
|
if (i == 'f')
|
|
|
|
flg_func = 1;
|
|
|
|
else
|
2002-11-25 01:35:38 +03:00
|
|
|
flg_var = i;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1994-05-11 21:09:42 +04:00
|
|
|
if (flg_func == 0 && flg_var == 0)
|
|
|
|
flg_var = 1;
|
1996-10-16 19:24:31 +04:00
|
|
|
|
1994-05-11 21:09:42 +04:00
|
|
|
for (ap = argptr; *ap ; ap++) {
|
|
|
|
if (flg_func)
|
|
|
|
ret |= unsetfunc(*ap);
|
|
|
|
if (flg_var)
|
2002-11-25 01:35:38 +03:00
|
|
|
ret |= unsetvar(*ap, flg_var == 'e');
|
1994-05-11 21:09:42 +04:00
|
|
|
}
|
|
|
|
return ret;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unset the specified variable.
|
|
|
|
*/
|
|
|
|
|
1996-06-25 20:49:05 +04:00
|
|
|
int
|
2002-11-25 01:35:38 +03:00
|
|
|
unsetvar(const char *s, int unexport)
|
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
struct var **vpp;
|
|
|
|
struct var *vp;
|
|
|
|
|
2004-10-02 16:16:53 +04:00
|
|
|
vp = find_var(s, &vpp, NULL);
|
|
|
|
if (vp == NULL)
|
|
|
|
return 1;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2004-10-02 16:16:53 +04:00
|
|
|
if (vp->flags & VREADONLY)
|
|
|
|
return (1);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2004-10-02 16:16:53 +04:00
|
|
|
INTOFF;
|
|
|
|
if (unexport) {
|
|
|
|
vp->flags &= ~VEXPORT;
|
|
|
|
} else {
|
|
|
|
if (vp->text[vp->name_len + 1] != '\0')
|
|
|
|
setvar(s, nullstr, 0);
|
|
|
|
vp->flags &= ~VEXPORT;
|
|
|
|
vp->flags |= VUNSET;
|
|
|
|
if ((vp->flags & VSTRFIXED) == 0) {
|
|
|
|
if ((vp->flags & VTEXTFIXED) == 0)
|
|
|
|
ckfree(vp->text);
|
|
|
|
*vpp = vp->next;
|
|
|
|
ckfree(vp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
INTON;
|
|
|
|
return 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns true if the two strings specify the same varable. The first
|
|
|
|
* variable name is terminated by '='; the second may be terminated by
|
|
|
|
* either '=' or '\0'.
|
|
|
|
*/
|
|
|
|
|
|
|
|
STATIC int
|
2004-10-02 16:16:53 +04:00
|
|
|
strequal(const char *p, const char *q)
|
2002-11-25 01:35:38 +03:00
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
while (*p == *q++) {
|
|
|
|
if (*p++ == '=')
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (*p == '=' && *(q - 1) == '\0')
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2004-10-02 16:16:53 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Search for a variable.
|
|
|
|
* 'name' may be terminated by '=' or a NUL.
|
|
|
|
* vppp is set to the pointer to vp, or the list head if vp isn't found
|
|
|
|
* lenp is set to the number of charactets in 'name'
|
|
|
|
*/
|
|
|
|
|
|
|
|
STATIC struct var *
|
|
|
|
find_var(const char *name, struct var ***vppp, int *lenp)
|
|
|
|
{
|
|
|
|
unsigned int hashval;
|
|
|
|
int len;
|
|
|
|
struct var *vp, **vpp;
|
|
|
|
const char *p = name;
|
|
|
|
|
|
|
|
hashval = 0;
|
|
|
|
while (*p && *p != '=')
|
|
|
|
hashval = 2 * hashval + (unsigned char)*p++;
|
|
|
|
len = p - name;
|
|
|
|
|
|
|
|
if (lenp)
|
|
|
|
*lenp = len;
|
|
|
|
vpp = &vartab[hashval % VTABSIZE];
|
|
|
|
if (vppp)
|
|
|
|
*vppp = vpp;
|
|
|
|
|
|
|
|
for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
|
|
|
|
if (vp->name_len != len)
|
|
|
|
continue;
|
|
|
|
if (memcmp(vp->text, name, len) != 0)
|
|
|
|
continue;
|
|
|
|
if (vppp)
|
|
|
|
*vppp = vpp;
|
|
|
|
return vp;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|