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>
|
2011-05-06 22:31:27 +00:00
|
|
|
#include <langinfo.h>
|
1996-08-11 22:51:00 +00:00
|
|
|
|
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"
|
2011-06-13 21:03:27 +00:00
|
|
|
#include "builtins.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;
|
2009-12-27 18:32:44 +00:00
|
|
|
const 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;
|
2010-06-02 19:16:58 +00:00
|
|
|
struct var vterm;
|
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;
|
2010-10-13 04:01:01 +00:00
|
|
|
static struct var voptind;
|
2012-02-04 23:12:14 +00:00
|
|
|
struct var vdisvfork;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2011-06-12 23:06:04 +00:00
|
|
|
int forcelocal;
|
|
|
|
|
2010-10-13 04:01:01 +00:00
|
|
|
static const struct varinit varinit[] = {
|
1996-09-01 10:22:36 +00:00
|
|
|
#ifndef NO_HISTORY
|
2009-12-27 18:32:44 +00:00
|
|
|
{ &vhistsize, VUNSET, "HISTSIZE=",
|
1996-12-14 06:20:03 +00:00
|
|
|
sethistsize },
|
1996-09-01 10:22:36 +00:00
|
|
|
#endif
|
2009-12-27 18:32:44 +00:00
|
|
|
{ &vifs, 0, "IFS= \t\n",
|
1996-12-14 06:20:03 +00:00
|
|
|
NULL },
|
2009-12-27 18:32:44 +00:00
|
|
|
{ &vmail, VUNSET, "MAIL=",
|
1996-12-14 06:20:03 +00:00
|
|
|
NULL },
|
2009-12-27 18:32:44 +00:00
|
|
|
{ &vmpath, VUNSET, "MAILPATH=",
|
1996-12-14 06:20:03 +00:00
|
|
|
NULL },
|
2009-12-27 18:32:44 +00:00
|
|
|
{ &vpath, 0, "PATH=" _PATH_DEFPATH,
|
1996-12-14 06:20:03 +00:00
|
|
|
changepath },
|
2009-12-27 18:32:44 +00:00
|
|
|
{ &vppid, VUNSET, "PPID=",
|
2002-06-01 02:14:48 +00:00
|
|
|
NULL },
|
1995-05-30 00:07:29 +00:00
|
|
|
/*
|
1994-05-26 06:18:55 +00:00
|
|
|
* vps1 depends on uid
|
|
|
|
*/
|
2009-12-27 18:32:44 +00:00
|
|
|
{ &vps2, 0, "PS2=> ",
|
1996-12-14 06:20:03 +00:00
|
|
|
NULL },
|
2009-12-27 18:32:44 +00:00
|
|
|
{ &vps4, 0, "PS4=+ ",
|
2006-06-15 07:00:49 +00:00
|
|
|
NULL },
|
2010-06-02 19:16:58 +00:00
|
|
|
#ifndef NO_HISTORY
|
|
|
|
{ &vterm, VUNSET, "TERM=",
|
|
|
|
setterm },
|
|
|
|
#endif
|
2009-12-27 18:32:44 +00:00
|
|
|
{ &voptind, 0, "OPTIND=1",
|
1996-12-14 06:20:03 +00:00
|
|
|
getoptsreset },
|
2012-02-04 23:12:14 +00:00
|
|
|
{ &vdisvfork, VUNSET, "SH_DISABLE_VFORK=",
|
|
|
|
NULL },
|
1996-12-14 06:20:03 +00:00
|
|
|
{ NULL, 0, NULL,
|
|
|
|
NULL }
|
1994-05-26 06:18:55 +00:00
|
|
|
};
|
|
|
|
|
2010-10-13 04:01:01 +00:00
|
|
|
static struct var *vartab[VTABSIZE];
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2010-10-13 04:01:01 +00:00
|
|
|
static const char *const locale_names[7] = {
|
2010-05-05 21:48:40 +00:00
|
|
|
"LC_COLLATE", "LC_CTYPE", "LC_MONETARY",
|
|
|
|
"LC_NUMERIC", "LC_TIME", "LC_MESSAGES", NULL
|
|
|
|
};
|
2010-10-13 04:01:01 +00:00
|
|
|
static const int locale_categories[7] = {
|
2010-05-05 21:48:40 +00:00
|
|
|
LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME, LC_MESSAGES, 0
|
|
|
|
};
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static int varequal(const char *, const char *);
|
2011-05-08 16:15:50 +00:00
|
|
|
static struct var *find_var(const char *, struct var ***, int *);
|
2010-10-13 22:18:03 +00:00
|
|
|
static int localevar(const char *);
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2013-01-20 12:44:50 +00:00
|
|
|
extern char **environ;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
/*
|
2013-01-20 12:44:50 +00:00
|
|
|
* This routine initializes the builtin variables and imports the environment.
|
|
|
|
* It is called when the shell is initialized.
|
1994-05-26 06:18:55 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
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;
|
2013-01-20 12:44:50 +00:00
|
|
|
char **envp;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
|
2011-05-08 16:15:50 +00:00
|
|
|
if (find_var(ip->text, &vpp, &vp->name_len) != NULL)
|
|
|
|
continue;
|
|
|
|
vp->next = *vpp;
|
|
|
|
*vpp = vp;
|
|
|
|
vp->text = __DECONST(char *, ip->text);
|
|
|
|
vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED;
|
|
|
|
vp->func = ip->func;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* PS1 depends on uid
|
|
|
|
*/
|
2011-05-08 16:15:50 +00:00
|
|
|
if (find_var("PS1", &vpp, &vps1.name_len) == NULL) {
|
1994-05-26 06:18:55 +00:00
|
|
|
vps1.next = *vpp;
|
|
|
|
*vpp = &vps1;
|
2009-12-27 18:32:44 +00:00
|
|
|
vps1.text = __DECONST(char *, geteuid() ? "PS1=$ " : "PS1=# ");
|
1994-05-26 06:18:55 +00:00
|
|
|
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);
|
|
|
|
}
|
2013-01-20 12:44:50 +00:00
|
|
|
for (envp = environ ; *envp ; envp++) {
|
|
|
|
if (strchr(*envp, '=')) {
|
|
|
|
setvareq(*envp, VEXPORT|VTEXTFIXED);
|
|
|
|
}
|
|
|
|
}
|
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
|
2009-12-24 18:41:14 +00:00
|
|
|
setvarsafe(const char *name, const char *val, int flags)
|
1996-12-14 06:20:03 +00:00
|
|
|
{
|
|
|
|
struct jmploc jmploc;
|
2009-06-23 20:45:12 +00:00
|
|
|
struct jmploc *const savehandler = handler;
|
1996-12-14 06:20:03 +00:00
|
|
|
int err = 0;
|
2009-11-22 18:23:30 +00:00
|
|
|
int inton;
|
1996-12-14 06:20:03 +00:00
|
|
|
|
2009-11-22 18:23:30 +00:00
|
|
|
inton = is_int_on();
|
1996-12-14 06:20:03 +00:00
|
|
|
if (setjmp(jmploc.loc))
|
|
|
|
err = 1;
|
|
|
|
else {
|
|
|
|
handler = &jmploc;
|
|
|
|
setvar(name, val, flags);
|
|
|
|
}
|
|
|
|
handler = savehandler;
|
2009-11-22 18:23:30 +00:00
|
|
|
SETINTON(inton);
|
1996-12-14 06:20:03 +00:00
|
|
|
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
|
2009-12-24 18:41:14 +00:00
|
|
|
setvar(const char *name, const char *val, int flags)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
2009-12-24 18:41:14 +00:00
|
|
|
const char *p;
|
1994-05-26 06:18:55 +00:00
|
|
|
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);
|
|
|
|
}
|
2009-12-24 18:41:14 +00:00
|
|
|
nameeq = ckmalloc(len);
|
|
|
|
memcpy(nameeq, name, namelen);
|
|
|
|
nameeq[namelen] = '=';
|
1994-05-26 06:18:55 +00:00
|
|
|
if (val)
|
2009-12-24 18:41:14 +00:00
|
|
|
scopy(val, nameeq + namelen + 1);
|
|
|
|
else
|
|
|
|
nameeq[namelen + 1] = '\0';
|
1994-05-26 06:18:55 +00:00
|
|
|
setvareq(nameeq, flags);
|
|
|
|
}
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static int
|
2009-12-24 18:41:14 +00:00
|
|
|
localevar(const char *s)
|
2002-02-02 06:50:57 +00:00
|
|
|
{
|
2010-05-05 21:48:40 +00:00
|
|
|
const char *const *ss;
|
1996-08-11 22:51:00 +00:00
|
|
|
|
|
|
|
if (*s != 'L')
|
|
|
|
return 0;
|
|
|
|
if (varequal(s + 1, "ANG"))
|
|
|
|
return 1;
|
|
|
|
if (strncmp(s + 1, "C_", 2) != 0)
|
|
|
|
return 0;
|
2010-05-05 21:48:40 +00:00
|
|
|
if (varequal(s + 3, "ALL"))
|
|
|
|
return 1;
|
|
|
|
for (ss = locale_names; *ss ; ss++)
|
|
|
|
if (varequal(s + 3, *ss + 3))
|
1996-08-11 22:51:00 +00:00
|
|
|
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.
|
|
|
|
*/
|
2010-10-13 22:18:03 +00:00
|
|
|
static void
|
2009-12-24 18:41:14 +00:00
|
|
|
change_env(const char *s, int set)
|
2007-07-06 04:04:58 +00:00
|
|
|
{
|
|
|
|
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;
|
2011-05-08 16:15:50 +00:00
|
|
|
int nlen;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
1999-04-03 11:41:46 +00:00
|
|
|
if (aflag)
|
|
|
|
flags |= VEXPORT;
|
2011-06-12 23:06:04 +00:00
|
|
|
if (forcelocal && !(flags & (VNOSET | VNOLOCAL)))
|
|
|
|
mklocal(s);
|
2011-05-08 16:15:50 +00: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)
|
1994-05-26 06:18:55 +00:00
|
|
|
return;
|
2011-05-08 16:15:50 +00:00
|
|
|
INTOFF;
|
|
|
|
|
|
|
|
if (vp->func && (flags & VNOFUNC) == 0)
|
|
|
|
(*vp->func)(s + vp->name_len + 1);
|
|
|
|
|
|
|
|
if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
|
|
|
|
ckfree(vp->text);
|
|
|
|
|
|
|
|
vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
|
|
|
|
vp->flags |= flags;
|
|
|
|
vp->text = s;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We could roll this to a function, to handle it as
|
|
|
|
* a regular variable function callback, but why bother?
|
|
|
|
*
|
|
|
|
* Note: this assumes iflag is not set to 1 initially.
|
2013-01-20 12:44:50 +00:00
|
|
|
* As part of initvar(), this is called before arguments
|
2011-05-08 16:15:50 +00:00
|
|
|
* are looked at.
|
|
|
|
*/
|
|
|
|
if ((vp == &vmpath || (vp == &vmail && ! mpathset())) &&
|
|
|
|
iflag == 1)
|
|
|
|
chkmail(1);
|
|
|
|
if ((vp->flags & VEXPORT) && localevar(s)) {
|
|
|
|
change_env(s, 1);
|
|
|
|
(void) setlocale(LC_ALL, "");
|
|
|
|
updatecharset();
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
2011-05-08 16:15:50 +00:00
|
|
|
INTON;
|
|
|
|
return;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
/* not found */
|
2011-01-01 13:26:18 +00:00
|
|
|
if (flags & VNOSET)
|
|
|
|
return;
|
1994-05-26 06:18:55 +00:00
|
|
|
vp = ckmalloc(sizeof (*vp));
|
|
|
|
vp->flags = flags;
|
|
|
|
vp->text = s;
|
2011-05-08 16:15:50 +00:00
|
|
|
vp->name_len = nlen;
|
1994-05-26 06:18:55 +00:00
|
|
|
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, "");
|
2011-05-06 22:31:27 +00:00
|
|
|
updatecharset();
|
1996-08-11 22:51:00 +00:00
|
|
|
}
|
|
|
|
INTON;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process a linked list of variable assignments.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2011-01-01 13:26:18 +00:00
|
|
|
listsetvar(struct strlist *list, int flags)
|
2002-02-02 06:50:57 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
struct strlist *lp;
|
|
|
|
|
|
|
|
INTOFF;
|
|
|
|
for (lp = list ; lp ; lp = lp->next) {
|
2011-01-01 13:26:18 +00:00
|
|
|
setvareq(savestr(lp->text), flags);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
INTON;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the value of a variable. Returns NULL if not set.
|
|
|
|
*/
|
|
|
|
|
|
|
|
char *
|
2009-12-24 18:41:14 +00:00
|
|
|
lookupvar(const char *name)
|
2002-02-02 06:50:57 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
struct var *v;
|
|
|
|
|
2011-05-08 16:15:50 +00:00
|
|
|
v = find_var(name, NULL, NULL);
|
|
|
|
if (v == NULL || v->flags & VUNSET)
|
|
|
|
return NULL;
|
|
|
|
return v->text + v->name_len + 1;
|
1994-05-26 06:18:55 +00: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 *
|
2009-12-24 18:41:14 +00:00
|
|
|
bltinlookup(const 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;
|
2010-09-11 14:15:50 +00:00
|
|
|
char *result;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2010-09-11 14:15:50 +00:00
|
|
|
result = NULL;
|
1994-05-26 06:18:55 +00:00
|
|
|
for (sp = cmdenviron ; sp ; sp = sp->next) {
|
|
|
|
if (varequal(sp->text, name))
|
2010-09-11 14:15:50 +00:00
|
|
|
result = strchr(sp->text, '=') + 1;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
2010-09-11 14:15:50 +00:00
|
|
|
if (result != NULL)
|
|
|
|
return result;
|
2011-05-08 16:15:50 +00:00
|
|
|
|
|
|
|
v = find_var(name, NULL, NULL);
|
|
|
|
if (v == NULL || v->flags & VUNSET ||
|
|
|
|
(!doall && (v->flags & VEXPORT) == 0))
|
|
|
|
return NULL;
|
|
|
|
return v->text + v->name_len + 1;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-05 21:48:40 +00:00
|
|
|
/*
|
|
|
|
* Set up locale for a builtin (LANG/LC_* assignments).
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
bltinsetlocale(void)
|
|
|
|
{
|
|
|
|
struct strlist *lp;
|
|
|
|
int act = 0;
|
|
|
|
char *loc, *locdef;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (lp = cmdenviron ; lp ; lp = lp->next) {
|
|
|
|
if (localevar(lp->text)) {
|
|
|
|
act = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!act)
|
|
|
|
return;
|
|
|
|
loc = bltinlookup("LC_ALL", 0);
|
|
|
|
INTOFF;
|
|
|
|
if (loc != NULL) {
|
|
|
|
setlocale(LC_ALL, loc);
|
|
|
|
INTON;
|
2011-05-06 22:31:27 +00:00
|
|
|
updatecharset();
|
2010-05-05 21:48:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
locdef = bltinlookup("LANG", 0);
|
|
|
|
for (i = 0; locale_names[i] != NULL; i++) {
|
|
|
|
loc = bltinlookup(locale_names[i], 0);
|
|
|
|
if (loc == NULL)
|
|
|
|
loc = locdef;
|
|
|
|
if (loc != NULL)
|
|
|
|
setlocale(locale_categories[i], loc);
|
|
|
|
}
|
|
|
|
INTON;
|
2011-05-06 22:31:27 +00:00
|
|
|
updatecharset();
|
2010-05-05 21:48:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Undo the effect of bltinlocaleset().
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
bltinunsetlocale(void)
|
|
|
|
{
|
|
|
|
struct strlist *lp;
|
|
|
|
|
|
|
|
INTOFF;
|
|
|
|
for (lp = cmdenviron ; lp ; lp = lp->next) {
|
|
|
|
if (localevar(lp->text)) {
|
|
|
|
setlocale(LC_ALL, "");
|
2011-05-06 22:31:27 +00:00
|
|
|
updatecharset();
|
2010-05-05 21:48:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
INTON;
|
|
|
|
}
|
|
|
|
|
2011-05-06 22:31:27 +00:00
|
|
|
/*
|
|
|
|
* Update the localeisutf8 flag.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
updatecharset(void)
|
|
|
|
{
|
|
|
|
char *charset;
|
|
|
|
|
|
|
|
charset = nl_langinfo(CODESET);
|
|
|
|
localeisutf8 = !strcmp(charset, "UTF-8");
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2011-05-08 17:40:10 +00:00
|
|
|
void
|
|
|
|
initcharset(void)
|
|
|
|
{
|
|
|
|
updatecharset();
|
|
|
|
initial_localeisutf8 = localeisutf8;
|
|
|
|
}
|
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static int
|
2006-04-29 12:57:53 +00:00
|
|
|
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
|
|
|
|
|
|
|
/*
|
2011-01-25 20:56:18 +00:00
|
|
|
* Command to list all variables which are set. This is invoked from the
|
|
|
|
* set command when it is called without any options or operands.
|
1994-05-26 06:18:55 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
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
|
|
|
|
2012-02-04 23:29:07 +00:00
|
|
|
INTOFF;
|
2006-04-29 12:57:53 +00:00
|
|
|
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++) {
|
2011-06-17 10:21:24 +00:00
|
|
|
/*
|
|
|
|
* Skip improper variable names so the output remains usable as
|
|
|
|
* shell input.
|
|
|
|
*/
|
|
|
|
if (!isassignment(vars[i]))
|
|
|
|
continue;
|
2010-11-20 14:14:52 +00:00
|
|
|
s = strchr(vars[i], '=');
|
|
|
|
s++;
|
|
|
|
outbin(vars[i], s - vars[i], out1);
|
|
|
|
out1qstr(s);
|
2006-04-29 12:57:53 +00:00
|
|
|
out1c('\n');
|
|
|
|
}
|
|
|
|
ckfree(vars);
|
2012-02-04 23:29:07 +00:00
|
|
|
INTON;
|
2006-04-29 12:57:53 +00:00
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The export and readonly commands.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2012-09-15 21:56:30 +00:00
|
|
|
exportcmd(int argc __unused, char **argv)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
struct var **vpp;
|
|
|
|
struct var *vp;
|
2012-09-15 21:56:30 +00:00
|
|
|
char **ap;
|
1994-05-26 06:18:55 +00:00
|
|
|
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];
|
|
|
|
values = 0;
|
2012-09-15 21:56:30 +00:00
|
|
|
while ((ch = nextopt("p")) != '\0') {
|
2002-06-06 03:57:22 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'p':
|
|
|
|
values = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-15 21:56:30 +00:00
|
|
|
if (values && *argptr != NULL)
|
2005-09-09 21:03:34 +00:00
|
|
|
error("-p requires no arguments");
|
2012-09-15 21:56:30 +00:00
|
|
|
if (*argptr != NULL) {
|
|
|
|
for (ap = argptr; (name = *ap) != NULL; ap++) {
|
1994-05-26 06:18:55 +00:00
|
|
|
if ((p = strchr(name, '=')) != NULL) {
|
|
|
|
p++;
|
|
|
|
} else {
|
2011-05-08 16:15:50 +00:00
|
|
|
vp = find_var(name, NULL, NULL);
|
|
|
|
if (vp != NULL) {
|
|
|
|
vp->flags |= flag;
|
|
|
|
if ((vp->flags & VEXPORT) && localevar(vp->text)) {
|
|
|
|
change_env(vp->text, 1);
|
|
|
|
(void) setlocale(LC_ALL, "");
|
|
|
|
updatecharset();
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
2011-05-08 16:15:50 +00:00
|
|
|
continue;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
setvar(name, p, flag);
|
|
|
|
}
|
|
|
|
} 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) {
|
2011-06-17 10:21:24 +00:00
|
|
|
/*
|
|
|
|
* Skip improper variable names
|
|
|
|
* so the output remains usable
|
|
|
|
* as shell input.
|
|
|
|
*/
|
|
|
|
if (!isassignment(vp->text))
|
|
|
|
continue;
|
2002-06-06 03:57:22 +00:00
|
|
|
out1str(cmdname);
|
|
|
|
out1c(' ');
|
|
|
|
}
|
|
|
|
if (values && !(vp->flags & VUNSET)) {
|
2011-05-15 22:09:27 +00:00
|
|
|
outbin(vp->text,
|
|
|
|
vp->name_len + 1, out1);
|
|
|
|
out1qstr(vp->text +
|
|
|
|
vp->name_len + 1);
|
2010-11-20 14:14:52 +00:00
|
|
|
} else
|
2011-05-15 22:09:27 +00:00
|
|
|
outbin(vp->text, vp->name_len,
|
2010-11-20 14:14:52 +00:00
|
|
|
out1);
|
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 {
|
2011-05-08 16:15:50 +00:00
|
|
|
vp = find_var(name, &vpp, NULL);
|
1994-05-26 06:18:55 +00:00
|
|
|
if (vp == NULL) {
|
|
|
|
if (strchr(name, '='))
|
2011-06-12 23:06:04 +00:00
|
|
|
setvareq(savestr(name), VSTRFIXED | VNOLOCAL);
|
1994-05-26 06:18:55 +00:00
|
|
|
else
|
2011-06-12 23:06:04 +00:00
|
|
|
setvar(name, NULL, VSTRFIXED | VNOLOCAL);
|
1994-05-26 06:18:55 +00: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;
|
2011-05-08 16:15:50 +00:00
|
|
|
if (name[vp->name_len] == '=')
|
2011-06-12 23:06:04 +00:00
|
|
|
setvareq(savestr(name), VNOLOCAL);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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);
|
2010-11-13 22:10:26 +00:00
|
|
|
optschanged();
|
1994-05-26 06:18:55 +00:00
|
|
|
} 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
|
2010-10-29 21:44:43 +00:00
|
|
|
error("too many arguments");
|
1994-05-26 06:18:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2011-01-25 20:56:18 +00:00
|
|
|
* The unset builtin command.
|
1994-05-26 06:18:55 +00:00
|
|
|
*/
|
|
|
|
|
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
|
2009-12-24 18:41:14 +00:00
|
|
|
unsetvar(const char *s)
|
2002-02-02 06:50:57 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
struct var **vpp;
|
|
|
|
struct var *vp;
|
|
|
|
|
2011-05-08 16:15:50 +00:00
|
|
|
vp = find_var(s, &vpp, NULL);
|
|
|
|
if (vp == NULL)
|
|
|
|
return (0);
|
|
|
|
if (vp->flags & VREADONLY)
|
|
|
|
return (1);
|
|
|
|
INTOFF;
|
|
|
|
if (vp->text[vp->name_len + 1] != '\0')
|
|
|
|
setvar(s, nullstr, 0);
|
|
|
|
if ((vp->flags & VEXPORT) && localevar(vp->text)) {
|
|
|
|
change_env(s, 0);
|
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
updatecharset();
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
2011-05-08 16:15:50 +00:00
|
|
|
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;
|
2004-09-27 18:43:18 +00:00
|
|
|
return (0);
|
1994-05-26 06:18:55 +00: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'.
|
|
|
|
*/
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static int
|
2009-12-24 18:41:14 +00:00
|
|
|
varequal(const char *p, const char *q)
|
2002-02-02 06:50:57 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
while (*p == *q++) {
|
|
|
|
if (*p++ == '=')
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (*p == '=' && *(q - 1) == '\0')
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2011-05-08 16:15:50 +00: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;
|
|
|
|
}
|