/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright (c) 1980 Regents of the University of California.
* All rights reserved. The Berkeley Software License Agreement
* specifies the terms and conditions for redistribution.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include "sh.h"
#include "sh.tconst.h"
extern int didchdir;
/*
* C Shell
*/
void putn1(int);
void exportpath(tchar **);
void
{
tchar *p;
bool hadsub;
int subscr;
#ifdef TRACE
tprintf("TRACE- doset()\n");
#endif
v++;
p = *v++;
if (p == 0) {
prvars();
return;
}
do {
hadsub = 0;
/*
* check for proper variable syntax
* must be alphanumeric, start with a letter and
* be at most 20 characters
*/
continue;
goto setsyn;
if ((p - vp) > MAX_VAR_LEN)
bferr("Variable name too long");
if (*p == '[') {
hadsub++;
}
if (op = *p) {
*p++ = 0;
if (*p == 0 && *v && **v == '(')
p = *v++;
op = '=', v++;
if (*v)
p = *v++;
}
bferr("Syntax error");
tchar **e = v;
if (hadsub)
goto setsyn;
for (;;) {
if (!*e)
bferr("Missing )");
if (**e == ')')
break;
e++;
}
p = *e;
*e = 0;
*e = p;
v = e + 1;
} else if (hadsub) {
retp = 0;
} else
HIST = *p++;
HISTSUB = *p;
#ifdef FILEC
filec = 1;
#endif
} while (p = *v++);
}
tchar *
{
#ifdef TRACE
tprintf("TRACE- getinx()\n");
#endif
*ip = 0;
*cp++ = 0;
if (*cp++ != ']')
bferr("Subscript error");
return (cp);
}
void
{
#ifdef TRACE
tprintf("TRACE- asx()\n");
#endif
}
struct varent *
{
#ifdef TRACE
tprintf("TRACE- getvx()\n");
#endif
if (v == 0)
bferr("Subscript out of range");
return (v);
}
void
{
tchar *p;
bool hadsub;
int subscr;
v++;
p = *v++;
if (p == 0) {
prvars();
return;
}
do {
hadsub = 0;
continue;
goto letsyn;
if (*p == '[') {
hadsub++;
}
if (*p == 0 && *v)
p = *v++;
if (op = *p)
*p++ = 0;
else
goto letsyn;
if (op == '=') {
c = '=';
p = xset(p, &v);
} else {
c = *p++;
/* if (any(c, "+-")) { */
if (c == '+' || c == '-') {
if (c != op || *p)
goto letsyn;
p = plusplus;
} else {
/* if (any(op, "<>")) { */
if (c != op)
goto letsyn;
c = *p++;
bferr("Syntax error");
}
if (c != '=')
goto letsyn;
p = xset(p, &v);
}
}
if (op == '=')
if (hadsub)
else
else
if (hadsub)
#ifndef V6
/* avoid bug in vax CC */
{
}
#else
#endif
else
}
if (c != '=')
xfree(p);
} while (p = *v++);
}
tchar *
{
#ifdef TRACE
tprintf("TRACE- xset()\n");
#endif
if (*cp) {
--(*vp);
}
}
tchar *
{
int i;
if (op != '=') {
if (*vp)
*v++ = vp;
opr[1] = 0;
*v++ = opr;
*v++ = opr;
}
*v++ = p;
*v++ = 0;
if (*vecp)
bferr("Expression syntax");
return (putn(i));
}
tchar *
putn(int n)
{
#ifdef TRACE
tprintf("TRACE- putn()\n");
#endif
if (n < 0) {
n = -n;
*putp++ = '-';
}
if (sizeof (int) == 2 && n == -32768) {
*putp++ = '3';
n = 2768;
#ifdef pdp11
}
#else
} else if (sizeof (int) == 4 && n == 0x80000000) {
*putp++ = '2';
n = 147483648;
}
#endif
putn1(n);
*putp = 0;
}
void
putn1(int n)
{
#ifdef TRACE
tprintf("TRACE- putn1()\n");
#endif
if (n > 9)
putn1(n / 10);
}
int
{
int n;
int sign;
#ifdef TRACE
tprintf("TRACE- getn()\n");
#endif
sign = 0;
cp++;
if (*cp == '-') {
sign++;
cp++;
goto badnum;
}
n = 0;
if (*cp)
goto badnum;
return (sign ? -n : n);
bferr("Badly formed number");
return (0);
}
tchar *
{
#ifdef TRACE
tprintf("TRACE- value1()\n");
#endif
}
struct varent *
{
#ifdef TRACE
tprintf("TRACE- madrof()\n");
#endif
return vp1;
return vp;
}
return vp;
}
struct varent *
{
int cmp;
#ifdef TRACE
tprintf("TRACE- adrof1()\n");
#endif
v = v->v_left;
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
{
#ifdef TRACE
tprintf("TRACE- set()\n");
#endif
vec[1] = 0;
}
void
{
#ifdef TRACE
tprintf("TRACE- set1()\n");
#endif
gflag = 0;
/*
* then do globbing. But if we are setting the cwd
* becuz of a cd, chdir, pushd, popd, do not do globbing.
*/
{
}
if (gflag) {
if (vec == 0) {
bferr("No match");
return;
}
gargv = 0;
}
}
void
{
struct varent *c;
int f;
#ifdef TRACE
tprintf("TRACE- setq()\n");
#endif
f = 0; /* tree hangs off the header's left link */
while (c = p->v_link[f]) {
goto found;
}
p = c;
f = f > 0;
}
c->v_bal = 0;
c->v_parent = p;
balance(p, f, 0);
}
void
{
#ifdef TRACE
tprintf("TRACE- unset()\n");
#endif
HIST = '!';
HISTSUB = '^';
}
#ifdef FILEC
filec = 0;
#endif
}
void
{
int cnt;
#ifdef TRACE
tprintf("TRACE- unset1()\n");
#endif
while (*++v) {
cnt = 0;
if (cnt == 0)
setname(*v);
}
}
void
{
#ifdef TRACE
tprintf("TRACE- unsetv()\n");
#endif
}
void
{
int f;
#ifdef TRACE
tprintf("TRACE- unsetv1()\n");
#endif
/*
* Free associated memory first to avoid complications.
*/
/*
* 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 right child, copy
* it into p, and move it's left child into it.
*/
if (p->v_right == 0)
c = p->v_left;
else if (p->v_left == 0)
c = p->v_right;
else {
;
p = c;
c = p->v_left;
}
/*
* Move c into where p is.
*/
/*
* Free the deleted node, and rebalance.
*/
xfree(p);
}
void
{
#ifdef TRACE
tprintf("TRACE- setNS()\n");
#endif
}
void
{
#ifdef TRACE
tprintf("TRACE- shift()\n");
#endif
v++;
name = *v;
if (name == 0)
else
if (argv == 0)
bferr("No more words");
}
void
{
#ifdef TRACE
tprintf("TRACE- exportpath()\n");
#endif
exppath[0] = 0;
if (val)
while (*val) {
printf("Warning: ridiculously long PATH truncated\n");
break;
}
break;
}
}
/* macros to do single rotations on node p */
#define rright(p) (\
t = (p)->v_left,\
(p) = t)
#define rleft(p) (\
t = (p)->v_right,\
(p) = t)
/*
* 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.
*/
void
{
struct varent *t; /* used by the rotate macros */
int ff;
#ifdef TRACE
tprintf("TRACE- balance()\n");
#endif
/*
* Ok, from here on, p is the node we're operating on;
* pp is it's parent; f is the branch of p from which we have come;
* ff is the branch of pp which is 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 */
case 1: /* sigle rotate */
p->v_bal = 0;
break;
case 0: /* single rotate */
p->v_bal = -1;
break;
case -1: /* double rotate */
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 */
case -1: /* single rotate */
p->v_bal = 0;
break;
case 0: /* signle rotate */
p->v_bal = 1;
break;
case 1: /* double rotate */
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
{
struct varent *c;
int len;
#ifdef TRACE
tprintf("TRACE- plist()\n");
#endif
if (setintr)
for (;;) {
while (p->v_left)
p = p->v_left;
x:
if (p->v_parent == 0) /* is it the header? */
return;
Putchar('\t');
if (len != 1)
Putchar('(');
if (len != 1)
Putchar(')');
Putchar('\n');
if (p->v_right) {
p = p->v_right;
continue;
}
do {
c = p;
p = p->v_parent;
} while (p->v_right == c);
goto x;
}
}