2006-05-14 05:05:42 +04:00
|
|
|
/* $NetBSD: set.c,v 1.25 2006/05/14 01:05:42 christos Exp $ */
|
1995-03-21 12:01:59 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*-
|
1994-09-21 04:10:23 +04:00
|
|
|
* Copyright (c) 1980, 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* 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:23:50 +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
|
|
|
|
static char sccsid[] = "@(#)set.c 8.1 (Berkeley) 5/31/93";
|
|
|
|
#else
|
2006-05-14 05:05:42 +04:00
|
|
|
__RCSID("$NetBSD: set.c,v 1.25 2006/05/14 01:05:42 christos Exp $");
|
1995-03-21 12:01:59 +03:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2001-09-14 18:03:59 +04:00
|
|
|
|
2002-05-26 03:29:16 +04:00
|
|
|
#include <stdarg.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <stdlib.h>
|
2001-09-14 18:03:59 +04:00
|
|
|
|
1994-09-21 04:10:23 +04:00
|
|
|
#ifndef SHORT_STRINGS
|
|
|
|
#include <string.h>
|
|
|
|
#endif /* SHORT_STRINGS */
|
2001-09-14 18:03:59 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#include "csh.h"
|
|
|
|
#include "extern.h"
|
|
|
|
|
2001-09-14 18:03:59 +04:00
|
|
|
static Char *getinx(Char *, int *);
|
|
|
|
static void asx(Char *, int, Char *);
|
|
|
|
static struct varent *getvx(Char *, int);
|
|
|
|
static Char *xset(Char *, Char ***);
|
|
|
|
static Char *operate(int, Char *, Char *);
|
|
|
|
static void putn1(int);
|
|
|
|
static struct varent *madrof(Char *, struct varent *);
|
|
|
|
static void unsetv1(struct varent *);
|
|
|
|
static void exportpath(Char **);
|
|
|
|
static void balance(struct varent *, int, int);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* C Shell
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
1994-09-21 04:10:23 +04:00
|
|
|
/*ARGSUSED*/
|
2001-09-14 18:03:59 +04:00
|
|
|
doset(Char **v, struct command *t)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2001-09-14 18:03:59 +04:00
|
|
|
Char op, *p, **vecp, *vp;
|
2006-05-14 05:05:42 +04:00
|
|
|
int subscr = 0; /* XXX: GCC */
|
2001-09-14 18:03:59 +04:00
|
|
|
bool hadsub;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
v++;
|
|
|
|
p = *v++;
|
|
|
|
if (p == 0) {
|
|
|
|
prvars();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
hadsub = 0;
|
|
|
|
vp = p;
|
|
|
|
if (letter(*p))
|
|
|
|
for (; alnum(*p); p++)
|
|
|
|
continue;
|
1998-07-28 15:41:40 +04:00
|
|
|
if (vp == p || !letter(*vp))
|
1993-03-21 12:45:37 +03:00
|
|
|
stderror(ERR_NAME | ERR_VARBEGIN);
|
1998-07-28 15:41:40 +04:00
|
|
|
if ((p - vp) > MAXVARLEN)
|
1993-03-21 12:45:37 +03:00
|
|
|
stderror(ERR_NAME | ERR_VARTOOLONG);
|
|
|
|
if (*p == '[') {
|
|
|
|
hadsub++;
|
|
|
|
p = getinx(p, &subscr);
|
|
|
|
}
|
1994-09-21 04:10:23 +04:00
|
|
|
if ((op = *p) != '\0') {
|
1993-03-21 12:45:37 +03:00
|
|
|
*p++ = 0;
|
|
|
|
if (*p == 0 && *v && **v == '(')
|
|
|
|
p = *v++;
|
|
|
|
}
|
|
|
|
else if (*v && eq(*v, STRequal)) {
|
|
|
|
op = '=', v++;
|
|
|
|
if (*v)
|
|
|
|
p = *v++;
|
|
|
|
}
|
1998-07-28 15:41:40 +04:00
|
|
|
if (op && op != '=')
|
1993-03-21 12:45:37 +03:00
|
|
|
stderror(ERR_NAME | ERR_SYNTAX);
|
|
|
|
if (eq(p, STRLparen)) {
|
1997-01-13 20:53:15 +03:00
|
|
|
Char **e = v;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1998-07-28 15:41:40 +04:00
|
|
|
if (hadsub)
|
1993-03-21 12:45:37 +03:00
|
|
|
stderror(ERR_NAME | ERR_SYNTAX);
|
|
|
|
for (;;) {
|
1998-07-28 15:41:40 +04:00
|
|
|
if (!*e)
|
1993-03-21 12:45:37 +03:00
|
|
|
stderror(ERR_NAME | ERR_MISSING, ')');
|
|
|
|
if (**e == ')')
|
|
|
|
break;
|
|
|
|
e++;
|
|
|
|
}
|
|
|
|
p = *e;
|
|
|
|
*e = 0;
|
|
|
|
vecp = saveblk(v);
|
|
|
|
set1(vp, vecp, &shvhed);
|
|
|
|
*e = p;
|
|
|
|
v = e + 1;
|
|
|
|
}
|
|
|
|
else if (hadsub)
|
|
|
|
asx(vp, subscr, Strsave(p));
|
|
|
|
else
|
|
|
|
set(vp, Strsave(p));
|
|
|
|
if (eq(vp, STRpath)) {
|
2006-03-18 01:38:44 +03:00
|
|
|
struct varent *pt = adrof(STRpath);
|
|
|
|
if (pt == NULL)
|
|
|
|
stderror(ERR_NAME | ERR_UNDVAR);
|
|
|
|
else {
|
|
|
|
exportpath(pt->vec);
|
|
|
|
dohash(NULL, NULL);
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
else if (eq(vp, STRhistchars)) {
|
1997-01-13 20:53:15 +03:00
|
|
|
Char *pn = value(STRhistchars);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
HIST = *pn++;
|
|
|
|
HISTSUB = *pn;
|
|
|
|
}
|
|
|
|
else if (eq(vp, STRuser)) {
|
|
|
|
Setenv(STRUSER, value(vp));
|
|
|
|
Setenv(STRLOGNAME, value(vp));
|
|
|
|
}
|
|
|
|
else if (eq(vp, STRwordchars)) {
|
|
|
|
word_chars = value(vp);
|
|
|
|
}
|
|
|
|
else if (eq(vp, STRterm))
|
|
|
|
Setenv(STRTERM, value(vp));
|
|
|
|
else if (eq(vp, STRhome)) {
|
1997-01-13 20:53:15 +03:00
|
|
|
Char *cp;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
cp = Strsave(value(vp)); /* get the old value back */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* convert to cononical pathname (possibly resolving symlinks)
|
|
|
|
*/
|
|
|
|
cp = dcanon(cp, cp);
|
|
|
|
|
|
|
|
set(vp, Strsave(cp)); /* have to save the new val */
|
|
|
|
|
|
|
|
/* and now mirror home with HOME */
|
|
|
|
Setenv(STRHOME, cp);
|
|
|
|
/* fix directory stack for new tilde home */
|
|
|
|
dtilde();
|
2001-09-14 18:03:59 +04:00
|
|
|
xfree((ptr_t)cp);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
#ifdef FILEC
|
|
|
|
else if (eq(vp, STRfilec))
|
|
|
|
filec = 1;
|
|
|
|
#endif
|
1994-09-21 04:10:23 +04:00
|
|
|
} while ((p = *v++) != NULL);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static Char *
|
2001-09-14 18:03:59 +04:00
|
|
|
getinx(Char *cp, int *ip)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
*ip = 0;
|
|
|
|
*cp++ = 0;
|
|
|
|
while (*cp && Isdigit(*cp))
|
|
|
|
*ip = *ip * 10 + *cp++ - '0';
|
1998-07-28 15:41:40 +04:00
|
|
|
if (*cp++ != ']')
|
1993-03-21 12:45:37 +03:00
|
|
|
stderror(ERR_NAME | ERR_SUBSCRIPT);
|
|
|
|
return (cp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-09-14 18:03:59 +04:00
|
|
|
asx(Char *vp, int subscr, Char *p)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2001-09-14 18:03:59 +04:00
|
|
|
struct varent *v;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2001-09-14 18:03:59 +04:00
|
|
|
v = getvx(vp, subscr);
|
1993-03-21 12:45:37 +03:00
|
|
|
xfree((ptr_t) v->vec[subscr - 1]);
|
|
|
|
v->vec[subscr - 1] = globone(p, G_APPEND);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct varent *
|
2001-09-14 18:03:59 +04:00
|
|
|
getvx(Char *vp, int subscr)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2001-09-14 18:03:59 +04:00
|
|
|
struct varent *v;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2001-09-14 18:03:59 +04:00
|
|
|
v = adrof(vp);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (v == 0)
|
|
|
|
udvar(vp);
|
1998-07-28 15:41:40 +04:00
|
|
|
if (subscr < 1 || subscr > blklen(v->vec))
|
1993-03-21 12:45:37 +03:00
|
|
|
stderror(ERR_NAME | ERR_RANGE);
|
|
|
|
return (v);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1994-09-21 04:10:23 +04:00
|
|
|
/*ARGSUSED*/
|
2001-09-14 18:03:59 +04:00
|
|
|
dolet(Char **v, struct command *t)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2001-09-14 18:03:59 +04:00
|
|
|
Char c, op, *p, *vp;
|
2006-05-14 05:05:42 +04:00
|
|
|
int subscr = 0; /* XXX: GCC */
|
2001-09-14 18:03:59 +04:00
|
|
|
bool hadsub;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
v++;
|
|
|
|
p = *v++;
|
|
|
|
if (p == 0) {
|
|
|
|
prvars();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
hadsub = 0;
|
|
|
|
vp = p;
|
|
|
|
if (letter(*p))
|
|
|
|
for (; alnum(*p); p++)
|
|
|
|
continue;
|
1998-07-28 15:41:40 +04:00
|
|
|
if (vp == p || !letter(*vp))
|
1993-03-21 12:45:37 +03:00
|
|
|
stderror(ERR_NAME | ERR_VARBEGIN);
|
1998-07-28 15:41:40 +04:00
|
|
|
if ((p - vp) > MAXVARLEN)
|
1993-03-21 12:45:37 +03:00
|
|
|
stderror(ERR_NAME | ERR_VARTOOLONG);
|
|
|
|
if (*p == '[') {
|
|
|
|
hadsub++;
|
|
|
|
p = getinx(p, &subscr);
|
|
|
|
}
|
|
|
|
if (*p == 0 && *v)
|
|
|
|
p = *v++;
|
1994-09-21 04:10:23 +04:00
|
|
|
if ((op = *p) != '\0')
|
1993-03-21 12:45:37 +03:00
|
|
|
*p++ = 0;
|
1998-07-28 15:41:40 +04:00
|
|
|
else
|
1993-03-21 12:45:37 +03:00
|
|
|
stderror(ERR_NAME | ERR_ASSIGN);
|
|
|
|
|
1998-07-28 15:41:40 +04:00
|
|
|
if (*p == '\0' && *v == NULL)
|
1993-03-21 12:45:37 +03:00
|
|
|
stderror(ERR_NAME | ERR_ASSIGN);
|
|
|
|
|
|
|
|
vp = Strsave(vp);
|
|
|
|
if (op == '=') {
|
|
|
|
c = '=';
|
|
|
|
p = xset(p, &v);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
c = *p++;
|
|
|
|
if (any("+-", c)) {
|
1998-07-28 15:41:40 +04:00
|
|
|
if (c != op || *p)
|
1993-03-21 12:45:37 +03:00
|
|
|
stderror(ERR_NAME | ERR_UNKNOWNOP);
|
|
|
|
p = Strsave(STR1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (any("<>", op)) {
|
1998-07-28 15:41:40 +04:00
|
|
|
if (c != op)
|
1993-03-21 12:45:37 +03:00
|
|
|
stderror(ERR_NAME | ERR_UNKNOWNOP);
|
|
|
|
c = *p++;
|
|
|
|
stderror(ERR_NAME | ERR_SYNTAX);
|
|
|
|
}
|
|
|
|
if (c != '=')
|
|
|
|
stderror(ERR_NAME | ERR_UNKNOWNOP);
|
|
|
|
p = xset(p, &v);
|
|
|
|
}
|
|
|
|
}
|
1999-03-19 15:58:33 +03:00
|
|
|
if (op == '=') {
|
1993-03-21 12:45:37 +03:00
|
|
|
if (hadsub)
|
|
|
|
asx(vp, subscr, p);
|
|
|
|
else
|
|
|
|
set(vp, p);
|
1999-03-19 15:58:33 +03:00
|
|
|
} else if (hadsub) {
|
1993-03-21 12:45:37 +03:00
|
|
|
struct varent *gv = getvx(vp, subscr);
|
|
|
|
|
|
|
|
asx(vp, subscr, operate(op, gv->vec[subscr - 1], p));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
set(vp, operate(op, value(vp), p));
|
|
|
|
if (eq(vp, STRpath)) {
|
2006-03-18 01:38:44 +03:00
|
|
|
struct varent *pt = adrof(STRpath);
|
|
|
|
if (pt == NULL)
|
|
|
|
stderror(ERR_NAME | ERR_UNDVAR);
|
|
|
|
else {
|
|
|
|
exportpath(pt->vec);
|
|
|
|
dohash(NULL, NULL);
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
xfree((ptr_t) vp);
|
|
|
|
if (c != '=')
|
|
|
|
xfree((ptr_t) p);
|
1994-09-21 04:10:23 +04:00
|
|
|
} while ((p = *v++) != NULL);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static Char *
|
2001-09-14 18:03:59 +04:00
|
|
|
xset(Char *cp, Char ***vp)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-01-13 20:53:15 +03:00
|
|
|
Char *dp;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (*cp) {
|
|
|
|
dp = Strsave(cp);
|
|
|
|
--(*vp);
|
|
|
|
xfree((ptr_t) ** vp);
|
|
|
|
**vp = dp;
|
|
|
|
}
|
1994-09-21 04:10:23 +04:00
|
|
|
return (putn(expr(vp)));
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static Char *
|
2001-09-14 18:03:59 +04:00
|
|
|
operate(int op, Char *vp, Char *p)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2001-09-14 18:03:59 +04:00
|
|
|
Char opr[2], **v, *vec[5], **vecp;
|
1997-01-13 20:53:15 +03:00
|
|
|
int i;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2001-09-14 18:03:59 +04:00
|
|
|
v = vec;
|
|
|
|
vecp = v;
|
1993-03-21 12:45:37 +03:00
|
|
|
if (op != '=') {
|
|
|
|
if (*vp)
|
|
|
|
*v++ = vp;
|
|
|
|
opr[0] = op;
|
|
|
|
opr[1] = 0;
|
|
|
|
*v++ = opr;
|
|
|
|
if (op == '<' || op == '>')
|
|
|
|
*v++ = opr;
|
|
|
|
}
|
|
|
|
*v++ = p;
|
|
|
|
*v++ = 0;
|
1994-09-21 04:10:23 +04:00
|
|
|
i = expr(&vecp);
|
1998-07-28 15:41:40 +04:00
|
|
|
if (*vecp)
|
1993-03-21 12:45:37 +03:00
|
|
|
stderror(ERR_NAME | ERR_EXPRESSION);
|
|
|
|
return (putn(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
static Char *putp;
|
|
|
|
|
2001-09-14 18:03:59 +04:00
|
|
|
Char *
|
|
|
|
putn(int n)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2001-11-03 16:35:39 +03:00
|
|
|
static Char numbers[15];
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2001-11-03 16:35:39 +03:00
|
|
|
putp = numbers;
|
1993-03-21 12:45:37 +03:00
|
|
|
if (n < 0) {
|
|
|
|
n = -n;
|
|
|
|
*putp++ = '-';
|
|
|
|
}
|
2006-03-21 19:46:44 +03:00
|
|
|
if ((unsigned int)n == 0x80000000U) {
|
2006-03-18 01:20:31 +03:00
|
|
|
*putp++ = '2';
|
|
|
|
n = 147483648;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
putn1(n);
|
|
|
|
*putp = 0;
|
2001-11-03 16:35:39 +03:00
|
|
|
return (Strsave(numbers));
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-09-14 18:03:59 +04:00
|
|
|
putn1(int n)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
if (n > 9)
|
|
|
|
putn1(n / 10);
|
|
|
|
*putp++ = n % 10 + '0';
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-09-14 18:03:59 +04:00
|
|
|
getn(Char *cp)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2001-09-14 18:03:59 +04:00
|
|
|
int n, sign;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
sign = 0;
|
|
|
|
if (cp[0] == '+' && cp[1])
|
|
|
|
cp++;
|
|
|
|
if (*cp == '-') {
|
|
|
|
sign++;
|
|
|
|
cp++;
|
1998-07-28 15:41:40 +04:00
|
|
|
if (!Isdigit(*cp))
|
1993-03-21 12:45:37 +03:00
|
|
|
stderror(ERR_NAME | ERR_BADNUM);
|
|
|
|
}
|
|
|
|
n = 0;
|
|
|
|
while (Isdigit(*cp))
|
|
|
|
n = n * 10 + *cp++ - '0';
|
1998-07-28 15:41:40 +04:00
|
|
|
if (*cp)
|
1993-03-21 12:45:37 +03:00
|
|
|
stderror(ERR_NAME | ERR_BADNUM);
|
|
|
|
return (sign ? -n : n);
|
|
|
|
}
|
|
|
|
|
2001-09-14 18:03:59 +04:00
|
|
|
Char *
|
|
|
|
value1(Char *var, struct varent *head)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-01-13 20:53:15 +03:00
|
|
|
struct varent *vp;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
vp = adrof1(var, head);
|
|
|
|
return (vp == 0 || vp->vec[0] == 0 ? STRNULL : vp->vec[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct varent *
|
2001-09-14 18:03:59 +04:00
|
|
|
madrof(Char *pat, struct varent *vp)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-01-13 20:53:15 +03:00
|
|
|
struct varent *vp1;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
for (; vp; vp = vp->v_right) {
|
|
|
|
if (vp->v_left && (vp1 = madrof(pat, vp->v_left)))
|
|
|
|
return vp1;
|
|
|
|
if (Gmatch(vp->v_name, pat))
|
|
|
|
return vp;
|
|
|
|
}
|
|
|
|
return vp;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct varent *
|
2001-09-14 18:03:59 +04:00
|
|
|
adrof1(Char *name, struct varent *v)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-01-13 20:53:15 +03:00
|
|
|
int cmp;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
v = v->v_left;
|
|
|
|
while (v && ((cmp = *name - *v->v_name) ||
|
|
|
|
(cmp = Strcmp(name, v->v_name))))
|
|
|
|
if (cmp < 0)
|
|
|
|
v = v->v_left;
|
|
|
|
else
|
|
|
|
v = v->v_right;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The caller is responsible for putting value in a safe place
|
|
|
|
*/
|
|
|
|
void
|
2001-09-14 18:03:59 +04:00
|
|
|
set(Char *var, Char *val)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2001-09-14 18:03:59 +04:00
|
|
|
Char **vec;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2001-09-14 18:03:59 +04:00
|
|
|
vec = (Char **)xmalloc((size_t)(2 * sizeof(Char **)));
|
1993-03-21 12:45:37 +03:00
|
|
|
vec[0] = val;
|
|
|
|
vec[1] = 0;
|
|
|
|
set1(var, vec, &shvhed);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-09-14 18:03:59 +04:00
|
|
|
set1(Char *var, Char **vec, struct varent *head)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2001-09-14 18:03:59 +04:00
|
|
|
Char **oldv;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2001-09-14 18:03:59 +04:00
|
|
|
oldv = vec;
|
1993-03-21 12:45:37 +03:00
|
|
|
gflag = 0;
|
|
|
|
tglob(oldv);
|
|
|
|
if (gflag) {
|
|
|
|
vec = globall(oldv);
|
|
|
|
if (vec == 0) {
|
|
|
|
blkfree(oldv);
|
|
|
|
stderror(ERR_NAME | ERR_NOMATCH);
|
|
|
|
}
|
|
|
|
blkfree(oldv);
|
|
|
|
gargv = 0;
|
|
|
|
}
|
|
|
|
setq(var, vec, head);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-09-14 18:03:59 +04:00
|
|
|
setq(Char *name, Char **vec, struct varent *p)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-01-13 20:53:15 +03:00
|
|
|
struct varent *c;
|
|
|
|
int f;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
f = 0; /* tree hangs off the header's left link */
|
1994-09-21 04:10:23 +04:00
|
|
|
while ((c = p->v_link[f]) != NULL) {
|
1993-03-21 12:45:37 +03:00
|
|
|
if ((f = *name - *c->v_name) == 0 &&
|
|
|
|
(f = Strcmp(name, c->v_name)) == 0) {
|
|
|
|
blkfree(c->vec);
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
p = c;
|
|
|
|
f = f > 0;
|
|
|
|
}
|
2001-09-14 18:03:59 +04:00
|
|
|
p->v_link[f] = c = (struct varent *)xmalloc((size_t)sizeof(struct varent));
|
1993-03-21 12:45:37 +03:00
|
|
|
c->v_name = Strsave(name);
|
|
|
|
c->v_bal = 0;
|
|
|
|
c->v_left = c->v_right = 0;
|
|
|
|
c->v_parent = p;
|
|
|
|
balance(p, f, 0);
|
|
|
|
found:
|
|
|
|
trim(c->vec = vec);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1994-09-21 04:10:23 +04:00
|
|
|
/*ARGSUSED*/
|
2001-09-14 18:03:59 +04:00
|
|
|
unset(Char **v, struct command *t)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
unset1(v, &shvhed);
|
|
|
|
#ifdef FILEC
|
|
|
|
if (adrof(STRfilec) == 0)
|
|
|
|
filec = 0;
|
|
|
|
#endif
|
|
|
|
if (adrof(STRhistchars) == 0) {
|
|
|
|
HIST = '!';
|
|
|
|
HISTSUB = '^';
|
|
|
|
}
|
|
|
|
if (adrof(STRwordchars) == 0)
|
|
|
|
word_chars = STR_WORD_CHARS;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-09-14 18:03:59 +04:00
|
|
|
unset1(Char *v[], struct varent *head)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-01-13 20:53:15 +03:00
|
|
|
struct varent *vp;
|
|
|
|
int cnt;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
while (*++v) {
|
|
|
|
cnt = 0;
|
1994-09-21 04:10:23 +04:00
|
|
|
while ((vp = madrof(*v, head->v_left)) != NULL)
|
1993-03-21 12:45:37 +03:00
|
|
|
unsetv1(vp), cnt++;
|
|
|
|
if (cnt == 0)
|
1994-09-21 04:10:23 +04:00
|
|
|
setname(vis_str(*v));
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-09-14 18:03:59 +04:00
|
|
|
unsetv(Char *var)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-01-13 20:53:15 +03:00
|
|
|
struct varent *vp;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if ((vp = adrof1(var, &shvhed)) == 0)
|
|
|
|
udvar(var);
|
|
|
|
unsetv1(vp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-09-14 18:03:59 +04:00
|
|
|
unsetv1(struct varent *p)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-01-13 20:53:15 +03:00
|
|
|
struct varent *c, *pp;
|
|
|
|
int f;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Free associated memory first to avoid complications.
|
|
|
|
*/
|
|
|
|
blkfree(p->vec);
|
|
|
|
xfree((ptr_t) p->v_name);
|
|
|
|
/*
|
|
|
|
* If p is missing one child, then we can move the other into where p is.
|
|
|
|
* Otherwise, we find the predecessor of p, which is guaranteed to have no
|
2006-04-24 23:00:29 +04:00
|
|
|
* right child, copy it into p, and move its left child into it.
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
|
|
|
if (p->v_right == 0)
|
|
|
|
c = p->v_left;
|
|
|
|
else if (p->v_left == 0)
|
|
|
|
c = p->v_right;
|
|
|
|
else {
|
1994-09-21 04:10:23 +04:00
|
|
|
for (c = p->v_left; c->v_right; c = c->v_right)
|
|
|
|
continue;
|
1993-03-21 12:45:37 +03:00
|
|
|
p->v_name = c->v_name;
|
|
|
|
p->vec = c->vec;
|
|
|
|
p = c;
|
|
|
|
c = p->v_left;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Move c into where p is.
|
|
|
|
*/
|
|
|
|
pp = p->v_parent;
|
|
|
|
f = pp->v_right == p;
|
1994-09-21 04:10:23 +04:00
|
|
|
if ((pp->v_link[f] = c) != NULL)
|
1993-03-21 12:45:37 +03:00
|
|
|
c->v_parent = pp;
|
|
|
|
/*
|
|
|
|
* Free the deleted node, and rebalance.
|
|
|
|
*/
|
|
|
|
xfree((ptr_t) p);
|
|
|
|
balance(pp, f, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-09-14 18:03:59 +04:00
|
|
|
setNS(Char *cp)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
set(cp, Strsave(STRNULL));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1994-09-21 04:10:23 +04:00
|
|
|
/*ARGSUSED*/
|
2001-09-14 18:03:59 +04:00
|
|
|
shift(Char **v, struct command *t)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-01-13 20:53:15 +03:00
|
|
|
struct varent *argv;
|
|
|
|
Char *name;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
v++;
|
|
|
|
name = *v;
|
|
|
|
if (name == 0)
|
|
|
|
name = STRargv;
|
|
|
|
else
|
|
|
|
(void) strip(name);
|
|
|
|
argv = adrof(name);
|
|
|
|
if (argv == 0)
|
|
|
|
udvar(name);
|
1998-07-28 15:41:40 +04:00
|
|
|
if (argv->vec[0] == 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
stderror(ERR_NAME | ERR_NOMORE);
|
|
|
|
lshift(argv->vec, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-09-14 18:03:59 +04:00
|
|
|
exportpath(Char **val)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2001-09-14 18:03:59 +04:00
|
|
|
Char exppath[BUFSIZE];
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
exppath[0] = 0;
|
|
|
|
if (val)
|
|
|
|
while (*val) {
|
2000-06-01 02:48:44 +04:00
|
|
|
if (Strlen(*val) + Strlen(exppath) + 2 > BUFSIZE) {
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)fprintf(csherr,
|
1994-09-21 04:10:23 +04:00
|
|
|
"Warning: ridiculously long PATH truncated\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
}
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)Strcat(exppath, *val++);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (*val == 0 || eq(*val, STRRparen))
|
|
|
|
break;
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)Strcat(exppath, STRcolon);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
Setenv(STRPATH, exppath);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef lint
|
|
|
|
/*
|
|
|
|
* Lint thinks these have null effect
|
|
|
|
*/
|
|
|
|
/* macros to do single rotations on node p */
|
|
|
|
#define rright(p) (\
|
|
|
|
t = (p)->v_left,\
|
|
|
|
(t)->v_parent = (p)->v_parent,\
|
|
|
|
((p)->v_left = t->v_right) ? (t->v_right->v_parent = (p)) : 0,\
|
|
|
|
(t->v_right = (p))->v_parent = t,\
|
|
|
|
(p) = t)
|
|
|
|
#define rleft(p) (\
|
|
|
|
t = (p)->v_right,\
|
|
|
|
(t)->v_parent = (p)->v_parent,\
|
|
|
|
((p)->v_right = t->v_left) ? (t->v_left->v_parent = (p)) : 0,\
|
|
|
|
(t->v_left = (p))->v_parent = t,\
|
|
|
|
(p) = t)
|
|
|
|
#else
|
|
|
|
struct varent *
|
2001-09-14 18:03:59 +04:00
|
|
|
rleft(struct varent *p)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
struct varent *
|
2001-09-14 18:03:59 +04:00
|
|
|
rright(struct varent *p)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
#endif /* ! lint */
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Rebalance a tree, starting at p and up.
|
|
|
|
* F == 0 means we've come from p's left child.
|
|
|
|
* D == 1 means we've just done a delete, otherwise an insert.
|
|
|
|
*/
|
|
|
|
static void
|
2001-09-14 18:03:59 +04:00
|
|
|
balance(struct varent *p, int f, int d)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-01-13 20:53:15 +03:00
|
|
|
struct varent *pp;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
#ifndef lint
|
1997-01-13 20:53:15 +03:00
|
|
|
struct varent *t; /* used by the rotate macros */
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
#endif
|
1997-01-13 20:53:15 +03:00
|
|
|
int ff;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
2006-04-24 23:00:29 +04:00
|
|
|
* Ok, from here on, p is the node we're operating on; pp is its parent; f
|
1993-03-21 12:45:37 +03:00
|
|
|
* is the branch of p from which we have come; ff is the branch of pp which
|
|
|
|
* is p.
|
|
|
|
*/
|
1994-09-21 04:10:23 +04:00
|
|
|
for (; (pp = p->v_parent) != NULL; p = pp, f = ff) {
|
1993-03-21 12:45:37 +03:00
|
|
|
ff = pp->v_right == p;
|
|
|
|
if (f ^ d) { /* right heavy */
|
|
|
|
switch (p->v_bal) {
|
|
|
|
case -1: /* was left heavy */
|
|
|
|
p->v_bal = 0;
|
|
|
|
break;
|
|
|
|
case 0: /* was balanced */
|
|
|
|
p->v_bal = 1;
|
|
|
|
break;
|
|
|
|
case 1: /* was already right heavy */
|
|
|
|
switch (p->v_right->v_bal) {
|
|
|
|
case 1: /* sigle rotate */
|
|
|
|
pp->v_link[ff] = rleft(p);
|
|
|
|
p->v_left->v_bal = 0;
|
|
|
|
p->v_bal = 0;
|
|
|
|
break;
|
|
|
|
case 0: /* single rotate */
|
|
|
|
pp->v_link[ff] = rleft(p);
|
|
|
|
p->v_left->v_bal = 1;
|
|
|
|
p->v_bal = -1;
|
|
|
|
break;
|
|
|
|
case -1: /* double rotate */
|
|
|
|
(void) rright(p->v_right);
|
|
|
|
pp->v_link[ff] = rleft(p);
|
|
|
|
p->v_left->v_bal =
|
|
|
|
p->v_bal < 1 ? 0 : -1;
|
|
|
|
p->v_right->v_bal =
|
|
|
|
p->v_bal > -1 ? 0 : 1;
|
|
|
|
p->v_bal = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else { /* left heavy */
|
|
|
|
switch (p->v_bal) {
|
|
|
|
case 1: /* was right heavy */
|
|
|
|
p->v_bal = 0;
|
|
|
|
break;
|
|
|
|
case 0: /* was balanced */
|
|
|
|
p->v_bal = -1;
|
|
|
|
break;
|
|
|
|
case -1: /* was already left heavy */
|
|
|
|
switch (p->v_left->v_bal) {
|
|
|
|
case -1: /* single rotate */
|
|
|
|
pp->v_link[ff] = rright(p);
|
|
|
|
p->v_right->v_bal = 0;
|
|
|
|
p->v_bal = 0;
|
|
|
|
break;
|
|
|
|
case 0: /* signle rotate */
|
|
|
|
pp->v_link[ff] = rright(p);
|
|
|
|
p->v_right->v_bal = -1;
|
|
|
|
p->v_bal = 1;
|
|
|
|
break;
|
|
|
|
case 1: /* double rotate */
|
|
|
|
(void) rleft(p->v_left);
|
|
|
|
pp->v_link[ff] = rright(p);
|
|
|
|
p->v_left->v_bal =
|
|
|
|
p->v_bal < 1 ? 0 : -1;
|
|
|
|
p->v_right->v_bal =
|
|
|
|
p->v_bal > -1 ? 0 : 1;
|
|
|
|
p->v_bal = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If from insert, then we terminate when p is balanced. If from
|
|
|
|
* delete, then we terminate when p is unbalanced.
|
|
|
|
*/
|
|
|
|
if ((p->v_bal == 0) ^ d)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-09-14 18:03:59 +04:00
|
|
|
plist(struct varent *p)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-01-13 20:53:15 +03:00
|
|
|
struct varent *c;
|
2003-01-16 12:38:37 +03:00
|
|
|
sigset_t nsigset;
|
2001-09-14 18:03:59 +04:00
|
|
|
int len;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1995-03-21 21:35:32 +03:00
|
|
|
if (setintr) {
|
2003-01-16 12:38:37 +03:00
|
|
|
sigemptyset(&nsigset);
|
|
|
|
(void)sigaddset(&nsigset, SIGINT);
|
|
|
|
(void)sigprocmask(SIG_UNBLOCK, &nsigset, NULL);
|
1995-03-21 21:35:32 +03:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
while (p->v_left)
|
|
|
|
p = p->v_left;
|
|
|
|
x:
|
|
|
|
if (p->v_parent == 0) /* is it the header? */
|
|
|
|
return;
|
|
|
|
len = blklen(p->vec);
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)fprintf(cshout, "%s\t", short2str(p->v_name));
|
1993-03-21 12:45:37 +03:00
|
|
|
if (len != 1)
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)fputc('(', cshout);
|
1994-09-21 04:10:23 +04:00
|
|
|
blkpr(cshout, p->vec);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (len != 1)
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)fputc(')', cshout);
|
|
|
|
(void)fputc('\n', cshout);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (p->v_right) {
|
|
|
|
p = p->v_right;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
c = p;
|
|
|
|
p = p->v_parent;
|
|
|
|
} while (p->v_right == c);
|
|
|
|
goto x;
|
|
|
|
}
|
|
|
|
}
|