sh.func.c revision 70a587dd392ff1dbaa2875c6c33921f08ea85273
/*
* Copyright 2006 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 <locale.h> /* For LC_ALL */
#include "sh.tconst.h"
#include <stdlib.h>
/*
* N.B.: Some of the limits change from SunOS 4.x to SunOS 5.0. In
* particular, RLIMIT_RSS is gone and RLIMIT_VMEM is new. Beware of consusing
* the keywords that the command prints for these two. The old one was
* "memoryuse" and the new one is "memorysize". Note also that a given limit
* doesn't necessarily appear in the same position in the two releases.
*/
struct limits {
int limconst;
int limdiv;
} limits[] = {
-1, 0,
};
void islogin(void);
int dolabel(void);
void preread_(void);
void doagain(void);
void toend(void);
void wfree(void);
void search();
#define BUFSZ 1028
/*
* C shell
*/
struct biltins *
{
#ifdef TRACE
tprintf("TRACE- isbfunc()\n");
#endif
return (&label);
}
if (*cp == '%') {
return (&backgnd);
}
return (&foregnd);
}
/*
* Binary search
* Bp1 is the beginning of the current search range.
* Bp2 is one past the end.
*/
int i;
return (bp);
}
if (i < 0) {
} else {
}
}
return (0);
}
void
{
int i;
#ifdef TRACE
tprintf("TRACE- func()\n");
#endif
bferr("Too few arguments");
}
bferr("Too many arguments");
}
}
int
dolabel(void)
{
#ifdef TRACE
tprintf("TRACE- dolabel()\n");
#endif
return (0);
}
void
{
#ifdef TRACE
tprintf("TRACE- doonintr()\n");
#endif
return;
}
bferr("Can't from terminal");
}
if (vv == 0) {
if (setintr) {
} else {
}
gointr = 0;
} else {
}
}
void
donohup(void)
{
#ifdef TRACE
tprintf("TRACE- donohup()\n");
#endif
if (intty) {
bferr("Can't from terminal");
}
if (setintr == 0) {
#ifdef CC
#endif
}
}
void
dozip(void)
{
;
}
void
prvars(void)
{
#ifdef TRACE
tprintf("TRACE- prvars()\n");
#endif
}
void
{
tchar *p;
#ifdef TRACE
tprintf("TRACE- doalias()\n");
#endif
v++;
p = *v++;
if (p == 0) {
} else if (*v == 0) {
if (vp) {
}
} else {
setname(p);
bferr("Too dangerous to alias that");
}
}
}
void
{
#ifdef TRACE
tprintf("TRACE- unalias()\n");
#endif
}
void
dologout(void)
{
#ifdef TRACE
tprintf("TRACE- dologout()\n");
#endif
islogin();
goodbye();
}
void
{
char *v_; /* work */
#ifdef TRACE
tprintf("TRACE- dologin()\n");
#endif
islogin();
rechist();
if (v[1] != NULL) {
} else {
v_ = 0;
}
untty();
exit(1);
}
#ifdef NEWGRP
void
{
char *v_; /* work */
#ifdef TRACE
tprintf("TRACE- donewgrp()\n");
#endif
panystop(0);
}
if (v[1] != NULL) {
} else {
v_ = 0;
}
untty();
exit(1);
}
#endif
void
islogin(void)
{
#ifdef TRACE
tprintf("TRACE- islogin()\n");
#endif
panystop(0);
}
if (loginsh) {
return;
}
error("Not login shell");
}
void
{
int i;
#ifdef TRACE
tprintf("TRACE- doif()\n");
#endif
v++;
i = exp(&v);
vv = v;
bferr("Empty if");
}
if (*++vv) {
bferr("Improper then");
}
/*
* If expression was zero, then scan to else,
* otherwise just fall into following code.
*/
if (!i) {
}
return;
}
/*
* Simple command attached to this if.
* Left shift the node in this tree, munging it
* so we can reexecute it.
*/
if (i) {
donefds();
}
}
/*
* Reexecute a command, being careful not
* to redo i/o redirection, which is already set up.
*/
void
{
#ifdef TRACE
tprintf("TRACE- reexecute()\n");
#endif
/*
* If tty is still ours to arbitrate, arbitrate it;
* otherwise dont even set pgrp's as the jobs would
* then have no way to get the tty (we can't give it
* to them, and our parent wouldn't know their pgrp, etc.
*/
}
void
doelse(void)
{
#ifdef TRACE
tprintf("TRACE- doelse()\n");
#endif
}
void
{
#ifdef TRACE
tprintf("TRACE- dogoto()\n");
#endif
/*
* While we still can, locate any unknown ends of existing loops.
* This obscure code is the WORST result of the fact that we
* don't really parse.
*/
} else {
}
}
/*
* Eliminate loops which were exited.
*/
wfree();
}
void
{
#ifdef TRACE
tprintf("TRACE- doswitch()\n");
#endif
v++;
if (!*v || *(*v++) != '(') {
goto syntax;
}
if (*(*v++) != ')') {
v--;
}
if (*v) {
error("Syntax error");
}
}
void
dobreak(void)
{
#ifdef TRACE
tprintf("TRACE- dobreak()\n");
#endif
if (whyles) {
toend();
} else {
}
}
void
{
#ifdef TRACE
tprintf("TRACE- doexit()\n");
#endif
if (chkstop == 0) {
panystop(0);
}
/*
* Don't DEMAND parentheses here either.
*/
v++;
if (*v) {
if (*v) {
bferr("Expression syntax");
}
}
btoeof();
if (intty) {
}
}
void
{
#ifdef TRACE
tprintf("TRACE- doforeach()\n");
#endif
v++;
cp++;
}
bferr("Invalid variable");
}
cp = *v++;
bferr("Words not ()'ed");
}
v++;
v = glob(v);
if (v == 0) {
bferr("No match");
}
/*
* Pre-read the loop so as to be more
* comprehensible to a terminal user.
*/
if (intty) {
preread_();
}
doagain();
}
void
{
int status;
#ifdef TRACE
tprintf("TRACE- dowhile()\n");
#endif
v++;
/*
* Implement prereading here also, taking care not to
* evaluate the expression before the loop has been read up
* from a terminal.
*/
} else {
}
if (*v) {
bferr("Expression syntax");
}
if (!again) {
if (intty) {
/*
* The tty preread
*/
preread_();
doagain();
return;
}
}
if (status) {
/* We ain't gonna loop no more, no more! */
toend();
}
}
void
preread_(void)
{
#ifdef TRACE
tprintf("TRACE- preread()\n");
#endif
if (setintr) {
}
if (setintr) {
}
}
void
doend(void)
{
#ifdef TRACE
tprintf("TRACE- doend()\n");
#endif
if (!whyles) {
}
doagain();
}
void
docontin(void)
{
#ifdef TRACE
tprintf("TRACE- docontin()\n");
#endif
if (!whyles) {
}
doagain();
}
void
doagain(void)
{
#ifdef TRACE
tprintf("TRACE- doagain()\n");
#endif
/* Repeating a while is simple */
return;
}
/*
* The foreach variable list actually has a spurious word
* ")" at the end of the w_fe list. Thus we are at the
* of the list if one word beyond this is 0.
*/
dobreak();
return;
}
}
void
{
int i, omask;
#ifdef TRACE
tprintf("TRACE- dorepeat()\n");
#endif
i = getn(v[1]);
if (setintr) {
}
lshift(v, 2);
while (i > 0) {
if (setintr) {
(void) sigsetmask(omask);
}
--i;
}
donefds();
if (setintr) {
(void) sigsetmask(omask);
}
}
void
doswbrk(void)
{
#ifdef TRACE
tprintf("TRACE- doswbrk()\n");
#endif
}
int
{
int i;
#ifdef TRACE
tprintf("TRACE- srchx()\n");
#endif
/*
* Binary search
* Sp1 is the beginning of the current search range.
* Sp2 is one past the end.
*/
}
if (i < 0) {
} else {
}
}
return (-1);
}
/*VARARGS2*/
void
{
#ifdef TRACE
tprintf("TRACE- search()\n");
#endif
}
do {
}
aword[0] = 0;
case ZELSE:
return;
}
break;
case ZIF:
continue;
}
level++;
}
break;
case ZENDIF:
level--;
}
break;
case ZFOREACH:
case ZWHILE:
level++;
}
break;
case ZEND:
level--;
}
break;
case ZSWITCH:
level++;
}
break;
case ZENDSW:
level--;
}
break;
case ZLABEL:
level = -1;
}
break;
default:
break;
}
break;
}
level = -1;
}
break;
case ZCASE:
break;
}
}
level = -1;
}
break;
case ZDEFAULT:
level = -1;
}
break;
}
} while (level >= 0);
}
int
{
int found = 0;
int c, d;
#ifdef TRACE
tprintf("TRACE- getword()\n");
#endif
c = readc(1);
d = 0;
do {
while (issp(c)) {
c = readc(1);
}
if (c == '#') {
do {
c = readc(1);
} while (c >= 0 && c != '\n');
}
if (c < 0) {
goto past;
}
if (c == '\n') {
if (wp) {
break;
}
return (0);
}
/* ( and ) form separate words */
if (c == '(' || c == ')') {
return (1);
}
unreadc(c);
found = 1;
do {
c = readc(1);
c = ' ';
}
if (c == '\'' || c == '"') {
if (d == 0) {
d = c;
} else if (d == c) {
d = 0;
}
}
if (c < 0) {
goto past;
}
if (wp) {
*wp++ = c;
}
} while (wp == 0);
unreadc(c);
if (found) {
*--wp = 0;
}
return (found);
past:
switch (Stype) {
case ZIF:
case ZELSE:
bferr("endif not found");
case ZBRKSW:
case ZSWITCH:
bferr("endsw not found");
case ZBREAK:
bferr("end not found");
case ZGOTO:
bferr("label not found");
}
/*NOTREACHED*/
return (0);
}
void
toend(void)
{
#ifdef TRACE
tprintf("TRACE- toend()\n");
#endif
} else {
}
wfree();
}
void
wfree(void)
{
long o = btell();
#ifdef TRACE
tprintf("TRACE- wfree()\n");
#endif
while (whyles) {
break;
}
}
}
}
}
void
{
#ifdef TRACE
tprintf("TRACE- doecho()\n");
#endif
echo(' ', v);
}
void
{
#ifdef TRACE
tprintf("TRACE- doglob()\n");
#endif
echo(0, v);
flush();
}
void
{
int nonl = 0;
#ifdef TRACE
tprintf("TRACE- echo()\n");
#endif
if (setintr) {
}
v++;
if (*v == 0) {
/*
* echo command needs to have newline when there are no
* flags or arguments. glob should have no newline. If
* the separator is a blank, we are doing an echo. If the
* separator is zero, we are globbing.
*/
Putchar('\n');
return;
}
if (gflag) {
v = glob(v);
if (v == 0) {
bferr("No match");
}
}
/* check for -n arg, NOTE: it might be quoted */
(*(*v+2)&TRIM) == 0)) {
nonl++, v++;
}
while (cp = *v++) {
int c;
while (c = *cp++) {
}
if (*v) {
}
}
Putchar('\n');
} else {
flush();
}
if (setintr) {
}
if (gargv) {
}
}
extern char **environ;
/*
* Check if the environment variable vp affects this csh's behavior
* and therefore we should call setlocale() or not.
* This function has two side effects when it returns 1:
* variable islocalevar_catnum is set to the LC_xxx value.
* variable islocalevar_catname is set to the string "LC_xxx"
*/
static int islocalevar_catnum;
static char *islocalevar_catname;
static
bool
{
static struct lcinfo {
} categories_we_care[] = {
NOSTR /* assumption: LC_xxx >= 0 */
};
struct lcinfo *p = categories_we_care;
do {
return (1);
}
return (0);
}
void
{
#ifdef TRACE
tprintf("TRACE- dosetenv()\n");
#endif
v++;
if ((vp = *v++) == 0) {
char **ep;
if (setintr) {
}
}
return;
}
if ((lp = *v++) == 0) {
}
importpath(lp);
} else if (islocalevar(vp)) {
error("Locale could not be set properly");
}
}
}
void
dounsetenv(tchar **v)
{
#ifdef TRACE
tprintf("TRACE- dounsetenv()\n");
#endif
v++;
do {
local_unsetenv(*v);
if (islocalevar(*v++)) {
}
} while (*v);
}
void
{
char *dp;
#ifdef TRACE
/* tprintf("TRACE- local_setenv(%t, %t)\n", name, val); */
/* printf("IN local_setenv args = (%t)\n", val); */
#endif
#ifdef MBCHAR
/*
* This loop compares two chars in different
* representations, EUC (as char *) and wchar_t
* (in tchar), and ends when they are different.
*/
int n;
if (n <= 0) {
break; /* Illegal multibyte. */
}
dp += n; /* Advance to next multibyte char. */
continue;
} else {
break;
}
}
#else /* !MBCHAR */
continue;
}
#endif /* !MBCHAR */
continue;
}
/*
* Trimming is not needed here.
* trim();
*/
return;
}
blk[1] = 0;
}
void
{
char *dp;
char *cp_; /* tmp use */
static int cnt = 0; /* delete counter */
#ifdef TRACE
tprintf("TRACE- local_unsetenv()\n");
#endif
#ifdef MBCHAR
/*
* This loop compares two chars in different
* representations, EUC (as char *) and wchar_t
* (in tchar), and ends when they are different.
*/
int n;
if (n <= 0) {
break; /* Illegal multibyte. */
}
dp += n; /* Advance to next multibyte char. */
continue;
} else {
break;
}
}
#else /* !MBCHAR */
continue;
}
#endif /* !MBCHAR */
continue;
}
*ep = 0;
return;
}
}
void
{
int i;
#ifdef TRACE
tprintf("TRACE- dounmask()\n");
#endif
if (cp == 0) {
i = umask(0);
(void) umask(i);
printf("%o\n", i);
return;
}
i = 0;
}
if (*cp || i < 0 || i > 0777) {
bferr("Improper mask");
}
(void) umask(i);
}
struct limits *
{
#ifdef TRACE
tprintf("TRACE- findlim()\n");
#endif
res = 0;
if (res) {
bferr("Ambiguous");
}
}
}
if (res) {
return (res);
}
bferr("No such limit");
/*NOTREACHED*/
}
void
{
#ifdef TRACE
tprintf("TRACE- dolimit()\n");
#endif
v++;
hard = 1;
v++;
}
if (*v == 0) {
}
return;
}
if (v[1] == 0) {
return;
}
case 0:
error("Value specified for limit is too large");
return;
case (-1):
error("Numeric conversion failed");
return;
default:
}
}
}
static int
{
#ifdef TRACE
tprintf("TRACE- getval()\n");
#endif
errno = 0;
/*
* we must accept zero, but the conversion can fail and give us
* zero as well...try to deal with it as gracefully as possible
* by checking for EINVAL
*/
return (-1);
cp++;
}
if (*cp == 0) {
if (*v == 0) {
/* Check for overflow */
return (1);
} else {
return (0);
}
}
cp = *v;
}
switch (*cp) {
case ':':
goto badscal;
}
return (1);
} else {
return (0);
}
case 'h':
goto badscal;
}
return (0);
}
break;
case 'm':
return (0);
}
break;
}
case 'M':
goto badscal;
}
*cp = 'm';
return (0);
}
break;
case 's':
goto badscal;
}
break;
case 'k':
goto badscal;
}
return (0);
}
break;
case 'u':
*retval = RLIM_INFINITY;
return (1);
default:
bferr("Improper or unknown scale factor");
}
return (1);
}
void
{
#ifdef TRACE
tprintf("TRACE- limtail()\n");
#endif
}
if (*cp) {
}
}
void
{
char *pbuf;
#ifdef TRACE
tprintf("TRACE- plim()\n");
#endif
if (limit == RLIM_INFINITY) {
printf("unlimited");
} else {
}
printf("\n");
}
void
{
int err = 0;
#ifdef TRACE
tprintf("TRACE- dounlimit()\n");
#endif
v++;
hard = 1;
v++;
}
if (*v == 0) {
err++;
}
}
if (err) {
}
return;
}
while (*v) {
}
}
}
int
{
#ifdef TRACE
tprintf("TRACE- setlim()\n");
#endif
if (hard) {
} else {
}
return (-1);
}
return (0);
}
void
{
int ctpgrp;
void (*old)();
#ifdef TRACE
tprintf("TRACE- dosuspend()\n");
#endif
if (loginsh) {
error("Can't suspend a login shell (yet)");
}
error("Can't suspend this shell");
}
untty();
/* the shell stops here */
if (tpgrp != -1) {
goto retry;
}
}
}
void
{
int reenter;
#ifdef TRACE
tprintf("TRACE- doeval()\n");
#endif
v++;
if (*v == 0) {
return;
}
if (gflag) {
gargv = 0;
if (v == 0) {
error("No match");
}
v = copyblk(v);
} else {
trim(v);
}
reenter = 0;
setexit();
reenter++;
if (reenter == 1) {
evalvec = v;
evalp = 0;
process(0);
}
doneinp = 0;
if (gv) {
}
if (reenter >= 2) {
}
}