1994-05-26 06:18:55 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1998-05-18 06:44:24 +00:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 5/4/95";
|
|
|
|
#endif
|
1994-05-26 06:18:55 +00:00
|
|
|
#endif /* not lint */
|
2002-06-30 05:15:05 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-26 06:18:55 +00:00
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
2003-05-05 22:49:23 +00:00
|
|
|
#include <paths.h>
|
1996-09-01 10:22:36 +00:00
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
/*
|
|
|
|
* Shell variables.
|
|
|
|
*/
|
|
|
|
|
1996-08-11 22:51:00 +00:00
|
|
|
#include <locale.h>
|
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
#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-12-14 06:20:03 +00:00
|
|
|
#include "parser.h"
|
1996-09-01 10:22:36 +00:00
|
|
|
#ifndef NO_HISTORY
|
|
|
|
#include "myhistedit.h"
|
|
|
|
#endif
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
#define VTABSIZE 39
|
|
|
|
|
|
|
|
|
|
|
|
struct varinit {
|
|
|
|
struct var *var;
|
|
|
|
int flags;
|
|
|
|
char *text;
|
2002-02-02 06:50:57 +00:00
|
|
|
void (*func)(const char *);
|
1994-05-26 06:18:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
#ifndef NO_HISTORY
|
1994-05-26 06:18:55 +00:00
|
|
|
struct var vhistsize;
|
1996-09-01 10:22:36 +00:00
|
|
|
#endif
|
1994-05-26 06:18:55 +00:00
|
|
|
struct var vifs;
|
|
|
|
struct var vmail;
|
|
|
|
struct var vmpath;
|
|
|
|
struct var vpath;
|
2002-06-01 02:14:48 +00:00
|
|
|
struct var vppid;
|
1994-05-26 06:18:55 +00:00
|
|
|
struct var vps1;
|
|
|
|
struct var vps2;
|
2006-06-15 07:00:49 +00:00
|
|
|
struct var vps4;
|
1994-05-26 06:18:55 +00:00
|
|
|
struct var vvers;
|
2003-07-05 15:18:44 +00:00
|
|
|
STATIC struct var voptind;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2003-07-05 15:18:44 +00:00
|
|
|
STATIC const struct varinit varinit[] = {
|
1996-09-01 10:22:36 +00:00
|
|
|
#ifndef NO_HISTORY
|
1996-12-14 06:20:03 +00:00
|
|
|
{ &vhistsize, VSTRFIXED|VTEXTFIXED|VUNSET, "HISTSIZE=",
|
|
|
|
sethistsize },
|
1996-09-01 10:22:36 +00:00
|
|
|
#endif
|
1996-12-14 06:20:03 +00:00
|
|
|
{ &vifs, VSTRFIXED|VTEXTFIXED, "IFS= \t\n",
|
|
|
|
NULL },
|
|
|
|
{ &vmail, VSTRFIXED|VTEXTFIXED|VUNSET, "MAIL=",
|
|
|
|
NULL },
|
|
|
|
{ &vmpath, VSTRFIXED|VTEXTFIXED|VUNSET, "MAILPATH=",
|
|
|
|
NULL },
|
2003-05-05 22:49:23 +00:00
|
|
|
{ &vpath, VSTRFIXED|VTEXTFIXED, "PATH=" _PATH_DEFPATH,
|
1996-12-14 06:20:03 +00:00
|
|
|
changepath },
|
2002-06-01 02:14:48 +00:00
|
|
|
{ &vppid, VSTRFIXED|VTEXTFIXED|VUNSET, "PPID=",
|
|
|
|
NULL },
|
1995-05-30 00:07:29 +00:00
|
|
|
/*
|
1994-05-26 06:18:55 +00:00
|
|
|
* vps1 depends on uid
|
|
|
|
*/
|
1996-12-14 06:20:03 +00:00
|
|
|
{ &vps2, VSTRFIXED|VTEXTFIXED, "PS2=> ",
|
|
|
|
NULL },
|
2006-06-15 07:00:49 +00:00
|
|
|
{ &vps4, VSTRFIXED|VTEXTFIXED, "PS4=+ ",
|
|
|
|
NULL },
|
1996-12-14 06:20:03 +00:00
|
|
|
{ &voptind, VSTRFIXED|VTEXTFIXED, "OPTIND=1",
|
|
|
|
getoptsreset },
|
|
|
|
{ NULL, 0, NULL,
|
|
|
|
NULL }
|
1994-05-26 06:18:55 +00:00
|
|
|
};
|
|
|
|
|
2003-07-05 15:18:44 +00:00
|
|
|
STATIC struct var *vartab[VTABSIZE];
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2002-02-02 06:50:57 +00:00
|
|
|
STATIC struct var **hashvar(char *);
|
|
|
|
STATIC int varequal(char *, char *);
|
|
|
|
STATIC int localevar(char *);
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
/*
|
2006-02-04 14:38:37 +00:00
|
|
|
* Initialize the variable symbol tables and import the environment.
|
1994-05-26 06:18:55 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef mkinit
|
|
|
|
INCLUDE "var.h"
|
|
|
|
INIT {
|
|
|
|
char **envp;
|
|
|
|
extern char **environ;
|
|
|
|
|
|
|
|
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-02-02 06:50:57 +00:00
|
|
|
initvar(void)
|
|
|
|
{
|
2002-06-01 02:14:48 +00:00
|
|
|
char ppid[20];
|
1994-05-26 06:18:55 +00:00
|
|
|
const struct varinit *ip;
|
|
|
|
struct var *vp;
|
|
|
|
struct var **vpp;
|
|
|
|
|
|
|
|
for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
|
|
|
|
if ((vp->flags & VEXPORT) == 0) {
|
|
|
|
vpp = hashvar(ip->text);
|
|
|
|
vp->next = *vpp;
|
|
|
|
*vpp = vp;
|
|
|
|
vp->text = ip->text;
|
|
|
|
vp->flags = ip->flags;
|
1996-12-14 06:20:03 +00:00
|
|
|
vp->func = ip->func;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* PS1 depends on uid
|
|
|
|
*/
|
|
|
|
if ((vps1.flags & VEXPORT) == 0) {
|
|
|
|
vpp = hashvar("PS1=");
|
|
|
|
vps1.next = *vpp;
|
|
|
|
*vpp = &vps1;
|
|
|
|
vps1.text = geteuid() ? "PS1=$ " : "PS1=# ";
|
|
|
|
vps1.flags = VSTRFIXED|VTEXTFIXED;
|
|
|
|
}
|
2002-06-01 02:14:48 +00:00
|
|
|
if ((vppid.flags & VEXPORT) == 0) {
|
|
|
|
fmtstr(ppid, sizeof(ppid), "%d", (int)getppid());
|
|
|
|
setvarsafe("PPID", ppid, 0);
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
1996-12-14 06:20:03 +00:00
|
|
|
/*
|
|
|
|
* Safe version of setvar, returns 1 on success 0 on failure.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2002-02-02 06:50:57 +00:00
|
|
|
setvarsafe(char *name, char *val, int flags)
|
1996-12-14 06:20:03 +00:00
|
|
|
{
|
|
|
|
struct jmploc jmploc;
|
|
|
|
struct jmploc *volatile savehandler = handler;
|
|
|
|
int err = 0;
|
2009-06-01 11:02:09 +00:00
|
|
|
#ifdef __GNUC__
|
1996-12-14 06:20:03 +00:00
|
|
|
/* Avoid longjmp clobbering */
|
|
|
|
(void) &err;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (setjmp(jmploc.loc))
|
|
|
|
err = 1;
|
|
|
|
else {
|
|
|
|
handler = &jmploc;
|
|
|
|
setvar(name, val, flags);
|
|
|
|
}
|
|
|
|
handler = savehandler;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
/*
|
2006-02-04 14:38:37 +00:00
|
|
|
* Set the value of a variable. The flags argument is stored with the
|
1994-05-26 06:18:55 +00:00
|
|
|
* flags of the variable. If val is NULL, the variable is unset.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
setvar(char *name, char *val, int flags)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
char *p, *q;
|
|
|
|
int len;
|
|
|
|
int namelen;
|
|
|
|
char *nameeq;
|
|
|
|
int isbad;
|
|
|
|
|
|
|
|
isbad = 0;
|
|
|
|
p = name;
|
1996-08-11 22:51:00 +00:00
|
|
|
if (! is_name(*p))
|
1994-05-26 06:18:55 +00:00
|
|
|
isbad = 1;
|
1996-08-11 22:51:00 +00:00
|
|
|
p++;
|
1994-05-26 06:18:55 +00:00
|
|
|
for (;;) {
|
|
|
|
if (! is_in_name(*p)) {
|
|
|
|
if (*p == '\0' || *p == '=')
|
|
|
|
break;
|
|
|
|
isbad = 1;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
namelen = p - name;
|
|
|
|
if (isbad)
|
|
|
|
error("%.*s: bad variable name", namelen, name);
|
|
|
|
len = namelen + 2; /* 2 is space for '=' and '\0' */
|
|
|
|
if (val == NULL) {
|
|
|
|
flags |= VUNSET;
|
|
|
|
} else {
|
|
|
|
len += strlen(val);
|
|
|
|
}
|
|
|
|
p = nameeq = ckmalloc(len);
|
|
|
|
q = name;
|
|
|
|
while (--namelen >= 0)
|
|
|
|
*p++ = *q++;
|
|
|
|
*p++ = '=';
|
|
|
|
*p = '\0';
|
|
|
|
if (val)
|
|
|
|
scopy(val, p);
|
|
|
|
setvareq(nameeq, flags);
|
|
|
|
}
|
|
|
|
|
1996-08-11 22:51:00 +00:00
|
|
|
STATIC int
|
2002-02-02 06:50:57 +00:00
|
|
|
localevar(char *s)
|
|
|
|
{
|
1996-08-11 22:51:00 +00:00
|
|
|
static char *lnames[7] = {
|
|
|
|
"ALL", "COLLATE", "CTYPE", "MONETARY",
|
|
|
|
"NUMERIC", "TIME", NULL
|
|
|
|
};
|
|
|
|
char **ss;
|
|
|
|
|
|
|
|
if (*s != 'L')
|
|
|
|
return 0;
|
|
|
|
if (varequal(s + 1, "ANG"))
|
|
|
|
return 1;
|
|
|
|
if (strncmp(s + 1, "C_", 2) != 0)
|
|
|
|
return 0;
|
|
|
|
for (ss = lnames; *ss ; ss++)
|
|
|
|
if (varequal(s + 3, *ss))
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2007-07-06 04:04:58 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Sets/unsets an environment variable from a pointer that may actually be a
|
|
|
|
* pointer into environ where the string should not be manipulated.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
change_env(char *s, int set)
|
|
|
|
{
|
|
|
|
char *eqp;
|
|
|
|
char *ss;
|
|
|
|
|
|
|
|
ss = savestr(s);
|
|
|
|
if ((eqp = strchr(ss, '=')) != NULL)
|
|
|
|
*eqp = '\0';
|
|
|
|
if (set && eqp != NULL)
|
|
|
|
(void) setenv(ss, eqp + 1, 1);
|
|
|
|
else
|
|
|
|
(void) unsetenv(ss);
|
|
|
|
ckfree(ss);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
/*
|
|
|
|
* 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-02-02 06:50:57 +00:00
|
|
|
setvareq(char *s, int flags)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
struct var *vp, **vpp;
|
2002-02-02 07:06:58 +00:00
|
|
|
int len;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
1999-04-03 11:41:46 +00:00
|
|
|
if (aflag)
|
|
|
|
flags |= VEXPORT;
|
1994-05-26 06:18:55 +00:00
|
|
|
vpp = hashvar(s);
|
|
|
|
for (vp = *vpp ; vp ; vp = vp->next) {
|
|
|
|
if (varequal(s, vp->text)) {
|
|
|
|
if (vp->flags & VREADONLY) {
|
2002-02-02 07:06:58 +00:00
|
|
|
len = strchr(s, '=') - s;
|
1994-05-26 06:18:55 +00:00
|
|
|
error("%.*s: is read only", len, s);
|
|
|
|
}
|
|
|
|
INTOFF;
|
1996-12-14 06:20:03 +00:00
|
|
|
|
|
|
|
if (vp->func && (flags & VNOFUNC) == 0)
|
|
|
|
(*vp->func)(strchr(s, '=') + 1);
|
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
|
|
|
|
ckfree(vp->text);
|
1996-12-14 06:20:03 +00:00
|
|
|
|
|
|
|
vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
|
1994-05-26 06:18:55 +00:00
|
|
|
vp->flags |= flags;
|
|
|
|
vp->text = s;
|
1996-12-14 06:20:03 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We could roll this to a function, to handle it as
|
|
|
|
* a regular variable function callback, but why bother?
|
|
|
|
*/
|
1994-05-26 06:18:55 +00:00
|
|
|
if (vp == &vmpath || (vp == &vmail && ! mpathset()))
|
|
|
|
chkmail(1);
|
1996-08-11 22:51:00 +00:00
|
|
|
if ((vp->flags & VEXPORT) && localevar(s)) {
|
2007-07-06 04:04:58 +00:00
|
|
|
change_env(s, 1);
|
1996-08-11 22:51:00 +00:00
|
|
|
(void) setlocale(LC_ALL, "");
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
INTON;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* not found */
|
|
|
|
vp = ckmalloc(sizeof (*vp));
|
|
|
|
vp->flags = flags;
|
|
|
|
vp->text = s;
|
|
|
|
vp->next = *vpp;
|
1996-12-14 06:20:03 +00:00
|
|
|
vp->func = NULL;
|
1996-08-11 22:51:00 +00:00
|
|
|
INTOFF;
|
1994-05-26 06:18:55 +00:00
|
|
|
*vpp = vp;
|
1996-08-11 22:51:00 +00:00
|
|
|
if ((vp->flags & VEXPORT) && localevar(s)) {
|
2007-07-06 04:04:58 +00:00
|
|
|
change_env(s, 1);
|
1996-08-11 22:51:00 +00:00
|
|
|
(void) setlocale(LC_ALL, "");
|
|
|
|
}
|
|
|
|
INTON;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process a linked list of variable assignments.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
listsetvar(struct strlist *list)
|
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
struct strlist *lp;
|
|
|
|
|
|
|
|
INTOFF;
|
|
|
|
for (lp = list ; lp ; lp = lp->next) {
|
|
|
|
setvareq(savestr(lp->text), 0);
|
|
|
|
}
|
|
|
|
INTON;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the value of a variable. Returns NULL if not set.
|
|
|
|
*/
|
|
|
|
|
|
|
|
char *
|
2002-02-02 06:50:57 +00:00
|
|
|
lookupvar(char *name)
|
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
struct var *v;
|
|
|
|
|
|
|
|
for (v = *hashvar(name) ; v ; v = v->next) {
|
|
|
|
if (varequal(v->text, name)) {
|
|
|
|
if (v->flags & VUNSET)
|
|
|
|
return NULL;
|
|
|
|
return strchr(v->text, '=') + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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-02-02 06:50:57 +00:00
|
|
|
bltinlookup(char *name, int doall)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
struct strlist *sp;
|
|
|
|
struct var *v;
|
|
|
|
|
|
|
|
for (sp = cmdenviron ; sp ; sp = sp->next) {
|
|
|
|
if (varequal(sp->text, name))
|
|
|
|
return strchr(sp->text, '=') + 1;
|
|
|
|
}
|
|
|
|
for (v = *hashvar(name) ; v ; v = v->next) {
|
|
|
|
if (varequal(v->text, name)) {
|
1996-09-01 10:22:36 +00:00
|
|
|
if ((v->flags & VUNSET)
|
|
|
|
|| (!doall && (v->flags & VEXPORT) == 0))
|
1994-05-26 06:18:55 +00:00
|
|
|
return NULL;
|
|
|
|
return strchr(v->text, '=') + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generate a list of exported variables. This routine is used to construct
|
|
|
|
* the third argument to execve when executing a program.
|
|
|
|
*/
|
|
|
|
|
|
|
|
char **
|
2002-02-02 06:50:57 +00:00
|
|
|
environment(void)
|
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
int nenv;
|
|
|
|
struct var **vpp;
|
|
|
|
struct var *vp;
|
|
|
|
char **env, **ep;
|
|
|
|
|
|
|
|
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
|
2005-08-13 08:12:18 +00:00
|
|
|
MKINIT void shprocvar(void);
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
SHELLPROC {
|
|
|
|
shprocvar();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
shprocvar(void)
|
|
|
|
{
|
1994-05-26 06:18:55 +00: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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-04-29 12:57:53 +00:00
|
|
|
static int
|
|
|
|
var_compare(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const char *const *sa, *const *sb;
|
|
|
|
|
|
|
|
sa = a;
|
|
|
|
sb = b;
|
|
|
|
/*
|
|
|
|
* This compares two var=value strings which creates a different
|
|
|
|
* order from what you would probably expect. POSIX is somewhat
|
|
|
|
* ambiguous on what should be sorted exactly.
|
|
|
|
*/
|
|
|
|
return strcoll(*sa, *sb);
|
|
|
|
}
|
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2002-02-02 06:50:57 +00:00
|
|
|
showvarscmd(int argc __unused, char **argv __unused)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
struct var **vpp;
|
|
|
|
struct var *vp;
|
2002-06-06 04:02:50 +00:00
|
|
|
const char *s;
|
2006-04-29 12:57:53 +00:00
|
|
|
const char **vars;
|
|
|
|
int i, n;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2006-04-29 12:57:53 +00:00
|
|
|
/*
|
|
|
|
* POSIX requires us to sort the variables.
|
|
|
|
*/
|
|
|
|
n = 0;
|
|
|
|
for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
|
|
|
|
for (vp = *vpp; vp; vp = vp->next) {
|
|
|
|
if (!(vp->flags & VUNSET))
|
|
|
|
n++;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
2006-04-29 12:57:53 +00:00
|
|
|
|
|
|
|
INTON;
|
|
|
|
vars = ckmalloc(n * sizeof(*vars));
|
|
|
|
i = 0;
|
|
|
|
for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
|
|
|
|
for (vp = *vpp; vp; vp = vp->next) {
|
|
|
|
if (!(vp->flags & VUNSET))
|
|
|
|
vars[i++] = vp->text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
qsort(vars, n, sizeof(*vars), var_compare);
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
for (s = vars[i]; *s != '='; s++)
|
|
|
|
out1c(*s);
|
|
|
|
out1c('=');
|
|
|
|
out1qstr(s + 1);
|
|
|
|
out1c('\n');
|
|
|
|
}
|
|
|
|
ckfree(vars);
|
|
|
|
INTOFF;
|
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The export and readonly commands.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2002-02-02 06:50:57 +00:00
|
|
|
exportcmd(int argc, char **argv)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
struct var **vpp;
|
|
|
|
struct var *vp;
|
|
|
|
char *name;
|
|
|
|
char *p;
|
2002-06-06 03:57:22 +00:00
|
|
|
char *cmdname;
|
|
|
|
int ch, values;
|
1994-05-26 06:18:55 +00:00
|
|
|
int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
|
|
|
|
|
2002-06-06 03:57:22 +00:00
|
|
|
cmdname = argv[0];
|
|
|
|
optreset = optind = 1;
|
2002-07-25 10:47:38 +00:00
|
|
|
opterr = 0;
|
2002-06-06 03:57:22 +00:00
|
|
|
values = 0;
|
|
|
|
while ((ch = getopt(argc, argv, "p")) != -1) {
|
|
|
|
switch (ch) {
|
|
|
|
case 'p':
|
|
|
|
values = 1;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
error("unknown option: -%c", optopt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2005-09-09 21:03:34 +00:00
|
|
|
if (values && argc != 0)
|
|
|
|
error("-p requires no arguments");
|
1994-05-26 06:18:55 +00:00
|
|
|
listsetvar(cmdenviron);
|
2002-06-06 03:57:22 +00:00
|
|
|
if (argc != 0) {
|
2005-09-09 21:03:34 +00:00
|
|
|
while ((name = *argv++) != NULL) {
|
1994-05-26 06:18:55 +00:00
|
|
|
if ((p = strchr(name, '=')) != NULL) {
|
|
|
|
p++;
|
|
|
|
} else {
|
|
|
|
vpp = hashvar(name);
|
|
|
|
for (vp = *vpp ; vp ; vp = vp->next) {
|
|
|
|
if (varequal(vp->text, name)) {
|
2002-02-02 06:50:57 +00:00
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
vp->flags |= flag;
|
1996-08-11 22:51:00 +00:00
|
|
|
if ((vp->flags & VEXPORT) && localevar(vp->text)) {
|
2007-07-06 04:04:58 +00:00
|
|
|
change_env(vp->text, 1);
|
1996-08-11 22:51:00 +00:00
|
|
|
(void) setlocale(LC_ALL, "");
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setvar(name, p, flag);
|
|
|
|
found:;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
|
|
|
|
for (vp = *vpp ; vp ; vp = vp->next) {
|
|
|
|
if (vp->flags & flag) {
|
2002-06-06 03:57:22 +00:00
|
|
|
if (values) {
|
|
|
|
out1str(cmdname);
|
|
|
|
out1c(' ');
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
for (p = vp->text ; *p != '=' ; p++)
|
|
|
|
out1c(*p);
|
2002-06-06 03:57:22 +00:00
|
|
|
if (values && !(vp->flags & VUNSET)) {
|
|
|
|
out1c('=');
|
|
|
|
out1qstr(p + 1);
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
out1c('\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The "local" command.
|
|
|
|
*/
|
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
int
|
2002-02-02 06:50:57 +00:00
|
|
|
localcmd(int argc __unused, char **argv __unused)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
char *name;
|
|
|
|
|
|
|
|
if (! in_function())
|
|
|
|
error("Not in a function");
|
|
|
|
while ((name = *argptr++) != NULL) {
|
|
|
|
mklocal(name);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make a variable a local variable. When a variable is made local, it's
|
|
|
|
* 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
|
2002-02-02 06:50:57 +00:00
|
|
|
mklocal(char *name)
|
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
struct localvar *lvp;
|
|
|
|
struct var **vpp;
|
|
|
|
struct var *vp;
|
|
|
|
|
|
|
|
INTOFF;
|
|
|
|
lvp = ckmalloc(sizeof (struct localvar));
|
|
|
|
if (name[0] == '-' && name[1] == '\0') {
|
|
|
|
lvp->text = ckmalloc(sizeof optlist);
|
1996-09-01 10:22:36 +00:00
|
|
|
memcpy(lvp->text, optlist, sizeof optlist);
|
1994-05-26 06:18:55 +00:00
|
|
|
vp = NULL;
|
|
|
|
} else {
|
|
|
|
vpp = hashvar(name);
|
|
|
|
for (vp = *vpp ; vp && ! varequal(vp->text, name) ; vp = vp->next);
|
|
|
|
if (vp == NULL) {
|
|
|
|
if (strchr(name, '='))
|
|
|
|
setvareq(savestr(name), VSTRFIXED);
|
|
|
|
else
|
|
|
|
setvar(name, NULL, VSTRFIXED);
|
|
|
|
vp = *vpp; /* the new variable */
|
|
|
|
lvp->text = NULL;
|
|
|
|
lvp->flags = VUNSET;
|
|
|
|
} else {
|
|
|
|
lvp->text = vp->text;
|
|
|
|
lvp->flags = vp->flags;
|
|
|
|
vp->flags |= VSTRFIXED|VTEXTFIXED;
|
|
|
|
if (strchr(name, '='))
|
|
|
|
setvareq(savestr(name), 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lvp->vp = vp;
|
|
|
|
lvp->next = localvars;
|
|
|
|
localvars = lvp;
|
|
|
|
INTON;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called after a function returns.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
poplocalvars(void)
|
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
struct localvar *lvp;
|
|
|
|
struct var *vp;
|
|
|
|
|
|
|
|
while ((lvp = localvars) != NULL) {
|
|
|
|
localvars = lvp->next;
|
|
|
|
vp = lvp->vp;
|
|
|
|
if (vp == NULL) { /* $- saved */
|
1996-09-01 10:22:36 +00:00
|
|
|
memcpy(optlist, lvp->text, sizeof optlist);
|
1994-05-26 06:18:55 +00:00
|
|
|
ckfree(lvp->text);
|
|
|
|
} else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
|
|
|
|
(void)unsetvar(vp->text);
|
|
|
|
} else {
|
|
|
|
if ((vp->flags & VTEXTFIXED) == 0)
|
|
|
|
ckfree(vp->text);
|
|
|
|
vp->flags = lvp->flags;
|
|
|
|
vp->text = lvp->text;
|
|
|
|
}
|
|
|
|
ckfree(lvp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
int
|
2002-02-02 06:50:57 +00:00
|
|
|
setvarcmd(int argc, char **argv)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00: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.
|
|
|
|
*/
|
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
int
|
2002-02-02 06:50:57 +00:00
|
|
|
unsetcmd(int argc __unused, char **argv __unused)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
char **ap;
|
|
|
|
int i;
|
|
|
|
int flg_func = 0;
|
|
|
|
int flg_var = 0;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
while ((i = nextopt("vf")) != '\0') {
|
|
|
|
if (i == 'f')
|
|
|
|
flg_func = 1;
|
|
|
|
else
|
|
|
|
flg_var = 1;
|
|
|
|
}
|
|
|
|
if (flg_func == 0 && flg_var == 0)
|
|
|
|
flg_var = 1;
|
1995-05-30 00:07:29 +00:00
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
for (ap = argptr; *ap ; ap++) {
|
|
|
|
if (flg_func)
|
|
|
|
ret |= unsetfunc(*ap);
|
|
|
|
if (flg_var)
|
|
|
|
ret |= unsetvar(*ap);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unset the specified variable.
|
|
|
|
*/
|
|
|
|
|
1996-12-14 06:20:03 +00:00
|
|
|
int
|
2002-02-02 06:50:57 +00:00
|
|
|
unsetvar(char *s)
|
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
struct var **vpp;
|
|
|
|
struct var *vp;
|
|
|
|
|
|
|
|
vpp = hashvar(s);
|
|
|
|
for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
|
|
|
|
if (varequal(vp->text, s)) {
|
|
|
|
if (vp->flags & VREADONLY)
|
|
|
|
return (1);
|
|
|
|
INTOFF;
|
|
|
|
if (*(strchr(vp->text, '=') + 1) != '\0')
|
|
|
|
setvar(s, nullstr, 0);
|
1996-08-11 22:51:00 +00:00
|
|
|
if ((vp->flags & VEXPORT) && localevar(vp->text)) {
|
2007-07-06 04:04:58 +00:00
|
|
|
change_env(s, 0);
|
1996-08-11 22:51:00 +00:00
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
}
|
1996-12-14 06:20:03 +00:00
|
|
|
vp->flags &= ~VEXPORT;
|
1994-05-26 06:18:55 +00:00
|
|
|
vp->flags |= VUNSET;
|
|
|
|
if ((vp->flags & VSTRFIXED) == 0) {
|
|
|
|
if ((vp->flags & VTEXTFIXED) == 0)
|
|
|
|
ckfree(vp->text);
|
|
|
|
*vpp = vp->next;
|
|
|
|
ckfree(vp);
|
|
|
|
}
|
|
|
|
INTON;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-27 18:43:18 +00:00
|
|
|
return (0);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the appropriate entry in the hash table from the name.
|
|
|
|
*/
|
|
|
|
|
|
|
|
STATIC struct var **
|
2002-02-02 06:50:57 +00:00
|
|
|
hashvar(char *p)
|
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
unsigned int hashval;
|
|
|
|
|
1996-12-14 06:20:03 +00:00
|
|
|
hashval = ((unsigned char) *p) << 4;
|
1994-05-26 06:18:55 +00:00
|
|
|
while (*p && *p != '=')
|
1996-12-14 06:20:03 +00:00
|
|
|
hashval += (unsigned char) *p++;
|
1994-05-26 06:18:55 +00:00
|
|
|
return &vartab[hashval % VTABSIZE];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
2002-02-02 06:50:57 +00:00
|
|
|
varequal(char *p, char *q)
|
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
while (*p == *q++) {
|
|
|
|
if (*p++ == '=')
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (*p == '=' && *(q - 1) == '\0')
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|