NetBSD/bin/csh/func.c

1528 lines
30 KiB
C
Raw Normal View History

/* $NetBSD: func.c,v 1.16 1998/07/28 11:41:44 mycroft 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 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.
*/
#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[] = "@(#)func.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: func.c,v 1.16 1998/07/28 11:41:44 mycroft 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>
#include <sys/stat.h>
#include <signal.h>
#include <locale.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#if __STDC__
# include <stdarg.h>
#else
# include <varargs.h>
#endif
#include "csh.h"
#include "extern.h"
#include "pathnames.h"
extern char **environ;
static void islogin __P((void));
static void reexecute __P((struct command *));
static void preread __P((void));
static void doagain __P((void));
1994-09-21 04:10:23 +04:00
static void search __P((int, int, Char *));
1993-03-21 12:45:37 +03:00
static int getword __P((Char *));
static int keyword __P((Char *));
static void toend __P((void));
static void xecho __P((int, Char **));
1994-09-21 04:10:23 +04:00
static void Unsetenv __P((Char *));
1993-03-21 12:45:37 +03:00
struct biltins *
isbfunc(t)
struct command *t;
{
1997-01-13 20:53:15 +03:00
Char *cp = t->t_dcom[0];
struct biltins *bp, *bp1, *bp2;
1993-03-21 12:45:37 +03:00
static struct biltins label = {"", dozip, 0, 0};
static struct biltins foregnd = {"%job", dofg1, 0, 0};
static struct biltins backgnd = {"%job &", dobg1, 0, 0};
if (lastchr(cp) == ':') {
label.bname = short2str(cp);
return (&label);
}
if (*cp == '%') {
if (t->t_dflg & F_AMPERSAND) {
t->t_dflg &= ~F_AMPERSAND;
backgnd.bname = short2str(cp);
return (&backgnd);
}
foregnd.bname = short2str(cp);
return (&foregnd);
}
/*
* Binary search Bp1 is the beginning of the current search range. Bp2 is
* one past the end.
*/
for (bp1 = bfunc, bp2 = bfunc + nbfunc; bp1 < bp2;) {
1997-01-13 20:53:15 +03:00
int i;
1993-03-21 12:45:37 +03:00
bp = bp1 + ((bp2 - bp1) >> 1);
if ((i = *cp - *bp->bname) == 0 &&
(i = Strcmp(cp, str2short(bp->bname))) == 0)
return bp;
if (i < 0)
bp2 = bp;
else
bp1 = bp + 1;
}
return (0);
}
void
func(t, bp)
1997-01-13 20:53:15 +03:00
struct command *t;
struct biltins *bp;
1993-03-21 12:45:37 +03:00
{
int i;
xechoit(t->t_dcom);
setname(bp->bname);
i = blklen(t->t_dcom) - 1;
if (i < bp->minargs)
1993-03-21 12:45:37 +03:00
stderror(ERR_NAME | ERR_TOOFEW);
if (i > bp->maxargs)
1993-03-21 12:45:37 +03:00
stderror(ERR_NAME | ERR_TOOMANY);
(*bp->bfunct) (t->t_dcom, t);
}
void
1994-09-21 04:10:23 +04:00
/*ARGSUSED*/
doonintr(v, t)
Char **v;
struct command *t;
1993-03-21 12:45:37 +03:00
{
1997-01-13 20:53:15 +03:00
Char *cp;
Char *vv = v[1];
1995-03-21 21:35:32 +03:00
sigset_t sigset;
1993-03-21 12:45:37 +03:00
if (parintr == SIG_IGN)
return;
if (setintr && intty)
1993-03-21 12:45:37 +03:00
stderror(ERR_NAME | ERR_TERMINAL);
cp = gointr;
gointr = 0;
xfree((ptr_t) cp);
if (vv == 0) {
1995-03-21 21:35:32 +03:00
if (setintr) {
sigemptyset(&sigset);
1998-07-28 06:47:19 +04:00
(void) sigaddset(&sigset, SIGINT);
(void) sigprocmask(SIG_BLOCK, &sigset, NULL);
1995-03-21 21:35:32 +03:00
} else
1993-03-21 12:45:37 +03:00
(void) signal(SIGINT, SIG_DFL);
gointr = 0;
}
else if (eq((vv = strip(vv)), STRminus)) {
(void) signal(SIGINT, SIG_IGN);
gointr = Strsave(STRminus);
}
else {
gointr = Strsave(vv);
(void) signal(SIGINT, pintr);
}
}
void
1994-09-21 04:10:23 +04:00
/*ARGSUSED*/
donohup(v, t)
Char **v;
struct command *t;
1993-03-21 12:45:37 +03:00
{
if (intty)
1993-03-21 12:45:37 +03:00
stderror(ERR_NAME | ERR_TERMINAL);
if (setintr == 0) {
(void) signal(SIGHUP, SIG_IGN);
}
}
void
1994-09-21 04:10:23 +04:00
/*ARGSUSED*/
dozip(v, t)
Char **v;
struct command *t;
1993-03-21 12:45:37 +03:00
{
;
}
void
prvars()
{
plist(&shvhed);
}
void
1994-09-21 04:10:23 +04:00
/*ARGSUSED*/
doalias(v, t)
Char **v;
struct command *t;
1993-03-21 12:45:37 +03:00
{
1997-01-13 20:53:15 +03:00
struct varent *vp;
Char *p;
1993-03-21 12:45:37 +03:00
v++;
p = *v++;
if (p == 0)
plist(&aliases);
else if (*v == 0) {
vp = adrof1(strip(p), &aliases);
1994-09-21 04:10:23 +04:00
if (vp) {
blkpr(cshout, vp->vec);
1998-07-28 06:47:19 +04:00
(void) fputc('\n', cshout);
1994-09-21 04:10:23 +04:00
}
1993-03-21 12:45:37 +03:00
}
else {
if (eq(p, STRalias) || eq(p, STRunalias)) {
1994-09-21 04:10:23 +04:00
setname(vis_str(p));
1993-03-21 12:45:37 +03:00
stderror(ERR_NAME | ERR_DANGER);
}
set1(strip(p), saveblk(v), &aliases);
}
}
void
1994-09-21 04:10:23 +04:00
/*ARGSUSED*/
unalias(v, t)
Char **v;
struct command *t;
1993-03-21 12:45:37 +03:00
{
unset1(v, &aliases);
}
void
1994-09-21 04:10:23 +04:00
/*ARGSUSED*/
dologout(v, t)
Char **v;
struct command *t;
1993-03-21 12:45:37 +03:00
{
islogin();
goodbye();
}
void
1994-09-21 04:10:23 +04:00
/*ARGSUSED*/
dologin(v, t)
Char **v;
struct command *t;
1993-03-21 12:45:37 +03:00
{
islogin();
rechist();
(void) signal(SIGTERM, parterm);
(void) execl(_PATH_LOGIN, "login", short2str(v[1]), NULL);
untty();
xexit(1);
1998-07-28 06:23:37 +04:00
/* NOTREACHED */
1993-03-21 12:45:37 +03:00
}
static void
islogin()
{
if (chkstop == 0 && setintr)
panystop(0);
if (loginsh)
return;
stderror(ERR_NOTLOGIN);
1998-07-28 06:23:37 +04:00
/* NOTREACHED */
1993-03-21 12:45:37 +03:00
}
void
doif(v, kp)
Char **v;
struct command *kp;
{
1997-01-13 20:53:15 +03:00
int i;
Char **vv;
1993-03-21 12:45:37 +03:00
v++;
1994-09-21 04:10:23 +04:00
i = expr(&v);
1993-03-21 12:45:37 +03:00
vv = v;
if (*vv == NULL)
1993-03-21 12:45:37 +03:00
stderror(ERR_NAME | ERR_EMPTYIF);
if (eq(*vv, STRthen)) {
if (*++vv)
1993-03-21 12:45:37 +03:00
stderror(ERR_NAME | ERR_IMPRTHEN);
1994-09-21 04:10:23 +04:00
setname(vis_str(STRthen));
1993-03-21 12:45:37 +03:00
/*
* If expression was zero, then scan to else, otherwise just fall into
* following code.
*/
if (!i)
search(T_IF, 0, NULL);
return;
}
/*
* Simple command attached to this if. Left shift the node in this tree,
* munging it so we can reexecute it.
*/
if (i) {
lshift(kp->t_dcom, vv - kp->t_dcom);
reexecute(kp);
donefds();
}
}
/*
* Reexecute a command, being careful not
* to redo i/o redirection, which is already set up.
*/
static void
reexecute(kp)
1997-01-13 20:53:15 +03:00
struct command *kp;
1993-03-21 12:45:37 +03:00
{
kp->t_dflg &= F_SAVE;
kp->t_dflg |= F_REPEAT;
/*
* 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.
*/
execute(kp, (tpgrp > 0 ? tpgrp : -1), NULL, NULL);
}
void
1994-09-21 04:10:23 +04:00
/*ARGSUSED*/
doelse(v, t)
Char **v;
struct command *t;
1993-03-21 12:45:37 +03:00
{
search(T_ELSE, 0, NULL);
}
void
1994-09-21 04:10:23 +04:00
/*ARGSUSED*/
dogoto(v, t)
Char **v;
struct command *t;
1993-03-21 12:45:37 +03:00
{
Char *lp;
1994-09-21 04:10:23 +04:00
gotolab(lp = globone(v[1], G_ERROR));
xfree((ptr_t) lp);
}
void
gotolab(lab)
Char *lab;
{
1997-01-13 20:53:15 +03:00
struct whyle *wp;
1993-03-21 12:45:37 +03:00
/*
* 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.
*/
for (wp = whyles; wp; wp = wp->w_next)
1994-09-21 04:10:23 +04:00
if (wp->w_end.type == F_SEEK && wp->w_end.f_seek == 0) {
1993-03-21 12:45:37 +03:00
search(T_BREAK, 0, NULL);
1994-09-21 04:10:23 +04:00
btell(&wp->w_end);
1993-03-21 12:45:37 +03:00
}
else
1994-09-21 04:10:23 +04:00
bseek(&wp->w_end);
search(T_GOTO, 0, lab);
1993-03-21 12:45:37 +03:00
/*
* Eliminate loops which were exited.
*/
wfree();
}
void
1994-09-21 04:10:23 +04:00
/*ARGSUSED*/
doswitch(v, t)
Char **v;
struct command *t;
1993-03-21 12:45:37 +03:00
{
1997-01-13 20:53:15 +03:00
Char *cp, *lp;
1993-03-21 12:45:37 +03:00
v++;
if (!*v || *(*v++) != '(')
1993-03-21 12:45:37 +03:00
stderror(ERR_SYNTAX);
cp = **v == ')' ? STRNULL : *v++;
if (*(*v++) != ')')
v--;
if (*v)
1993-03-21 12:45:37 +03:00
stderror(ERR_SYNTAX);
search(T_SWITCH, 0, lp = globone(cp, G_ERROR));
xfree((ptr_t) lp);
}
void
1994-09-21 04:10:23 +04:00
/*ARGSUSED*/
dobreak(v, t)
Char **v;
struct command *t;
1993-03-21 12:45:37 +03:00
{
if (whyles)
toend();
else
1993-03-21 12:45:37 +03:00
stderror(ERR_NAME | ERR_NOTWHILE);
}
void
1994-09-21 04:10:23 +04:00
/*ARGSUSED*/
doexit(v, t)
Char **v;
struct command *t;
1993-03-21 12:45:37 +03:00
{
if (chkstop == 0 && (intty || intact) && evalvec == 0)
panystop(0);
/*
* Don't DEMAND parentheses here either.
*/
v++;
if (*v) {
1994-09-21 04:10:23 +04:00
set(STRstatus, putn(expr(&v)));
if (*v)
1993-03-21 12:45:37 +03:00
stderror(ERR_NAME | ERR_EXPRESSION);
}
btoeof();
if (intty)
(void) close(SHIN);
}
void
1994-09-21 04:10:23 +04:00
/*ARGSUSED*/
doforeach(v, t)
Char **v;
struct command *t;
1993-03-21 12:45:37 +03:00
{
1997-01-13 20:53:15 +03:00
Char *cp, *sp;
struct whyle *nwp;
1993-03-21 12:45:37 +03:00
v++;
sp = cp = strip(*v);
if (!letter(*sp))
1993-03-21 12:45:37 +03:00
stderror(ERR_NAME | ERR_VARBEGIN);
while (*cp && alnum(*cp))
cp++;
if (*cp)
1993-03-21 12:45:37 +03:00
stderror(ERR_NAME | ERR_VARALNUM);
if ((cp - sp) > MAXVARLEN)
1993-03-21 12:45:37 +03:00
stderror(ERR_NAME | ERR_VARTOOLONG);
cp = *v++;
if (v[0][0] != '(' || v[blklen(v) - 1][0] != ')')
1993-03-21 12:45:37 +03:00
stderror(ERR_NAME | ERR_NOPAREN);
v++;
gflag = 0, tglob(v);
v = globall(v);
if (v == 0)
1993-03-21 12:45:37 +03:00
stderror(ERR_NAME | ERR_NOMATCH);
nwp = (struct whyle *) xcalloc(1, sizeof *nwp);
nwp->w_fe = nwp->w_fe0 = v;
gargv = 0;
1994-09-21 04:10:23 +04:00
btell(&nwp->w_start);
1993-03-21 12:45:37 +03:00
nwp->w_fename = Strsave(cp);
nwp->w_next = whyles;
1994-09-21 04:10:23 +04:00
nwp->w_end.type = F_SEEK;
1993-03-21 12:45:37 +03:00
whyles = nwp;
/*
* Pre-read the loop so as to be more comprehensible to a terminal user.
*/
if (intty)
preread();
doagain();
}
void
1994-09-21 04:10:23 +04:00
/*ARGSUSED*/
dowhile(v, t)
Char **v;
struct command *t;
1993-03-21 12:45:37 +03:00
{
1997-01-13 20:53:15 +03:00
int status;
bool again = whyles != 0 && SEEKEQ(&whyles->w_start, &lineloc) &&
1993-03-21 12:45:37 +03:00
whyles->w_fename == 0;
v++;
/*
* Implement prereading here also, taking care not to evaluate the
* expression before the loop has been read up from a terminal.
*/
if (intty && !again)
status = !exp0(&v, 1);
else
1994-09-21 04:10:23 +04:00
status = !expr(&v);
if (*v)
1993-03-21 12:45:37 +03:00
stderror(ERR_NAME | ERR_EXPRESSION);
if (!again) {
1997-01-13 20:53:15 +03:00
struct whyle *nwp =
1993-03-21 12:45:37 +03:00
(struct whyle *) xcalloc(1, sizeof(*nwp));
nwp->w_start = lineloc;
1994-09-21 04:10:23 +04:00
nwp->w_end.type = F_SEEK;
nwp->w_end.f_seek = 0;
1993-03-21 12:45:37 +03:00
nwp->w_next = whyles;
whyles = nwp;
if (intty) {
/*
* The tty preread
*/
preread();
doagain();
return;
}
}
if (status)
/* We ain't gonna loop no more, no more! */
toend();
}
static void
preread()
{
1995-03-21 21:35:32 +03:00
sigset_t sigset;
1994-09-21 04:10:23 +04:00
whyles->w_end.type = I_SEEK;
1995-03-21 21:35:32 +03:00
if (setintr) {
sigemptyset(&sigset);
1998-07-28 06:47:19 +04:00
(void) sigaddset(&sigset, SIGINT);
(void) sigprocmask(SIG_UNBLOCK, &sigset, NULL);
1995-03-21 21:35:32 +03:00
}
1993-03-21 12:45:37 +03:00
search(T_BREAK, 0, NULL); /* read the expression in */
if (setintr)
1998-07-28 06:47:19 +04:00
(void) sigprocmask(SIG_BLOCK, &sigset, NULL);
1994-09-21 04:10:23 +04:00
btell(&whyles->w_end);
1993-03-21 12:45:37 +03:00
}
void
1994-09-21 04:10:23 +04:00
/*ARGSUSED*/
doend(v, t)
Char **v;
struct command *t;
1993-03-21 12:45:37 +03:00
{
if (!whyles)
1993-03-21 12:45:37 +03:00
stderror(ERR_NAME | ERR_NOTWHILE);
1994-09-21 04:10:23 +04:00
btell(&whyles->w_end);
1993-03-21 12:45:37 +03:00
doagain();
}
void
1994-09-21 04:10:23 +04:00
/*ARGSUSED*/
docontin(v, t)
Char **v;
struct command *t;
1993-03-21 12:45:37 +03:00
{
if (!whyles)
1993-03-21 12:45:37 +03:00
stderror(ERR_NAME | ERR_NOTWHILE);
doagain();
}
static void
doagain()
{
/* Repeating a while is simple */
if (whyles->w_fename == 0) {
1994-09-21 04:10:23 +04:00
bseek(&whyles->w_start);
1993-03-21 12:45:37 +03:00
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.
*/
if (!whyles->w_fe[1]) {
1994-09-21 04:10:23 +04:00
dobreak(NULL, NULL);
1993-03-21 12:45:37 +03:00
return;
}
set(whyles->w_fename, Strsave(*whyles->w_fe++));
1994-09-21 04:10:23 +04:00
bseek(&whyles->w_start);
1993-03-21 12:45:37 +03:00
}
void
dorepeat(v, kp)
Char **v;
struct command *kp;
{
1997-01-13 20:53:15 +03:00
int i;
1995-03-21 21:35:32 +03:00
sigset_t sigset;
1993-03-21 12:45:37 +03:00
i = getn(v[1]);
1995-03-21 21:35:32 +03:00
if (setintr) {
sigemptyset(&sigset);
1998-07-28 06:47:19 +04:00
(void) sigaddset(&sigset, SIGINT);
(void) sigprocmask(SIG_BLOCK, &sigset, NULL);
1995-03-21 21:35:32 +03:00
}
1993-03-21 12:45:37 +03:00
lshift(v, 2);
while (i > 0) {
if (setintr)
1998-07-28 06:47:19 +04:00
(void) sigprocmask(SIG_UNBLOCK, &sigset, NULL);
1993-03-21 12:45:37 +03:00
reexecute(kp);
--i;
}
donefds();
if (setintr)
1998-07-28 06:47:19 +04:00
(void) sigprocmask(SIG_UNBLOCK, &sigset, NULL);
1993-03-21 12:45:37 +03:00
}
void
1994-09-21 04:10:23 +04:00
/*ARGSUSED*/
doswbrk(v, t)
Char **v;
struct command *t;
1993-03-21 12:45:37 +03:00
{
search(T_BRKSW, 0, NULL);
}
int
srchx(cp)
1997-01-13 20:53:15 +03:00
Char *cp;
1993-03-21 12:45:37 +03:00
{
1997-01-13 20:53:15 +03:00
struct srch *sp, *sp1, *sp2;
int i;
1993-03-21 12:45:37 +03:00
/*
* Binary search Sp1 is the beginning of the current search range. Sp2 is
* one past the end.
*/
for (sp1 = srchn, sp2 = srchn + nsrchn; sp1 < sp2;) {
sp = sp1 + ((sp2 - sp1) >> 1);
if ((i = *cp - *sp->s_name) == 0 &&
(i = Strcmp(cp, str2short(sp->s_name))) == 0)
return sp->s_value;
if (i < 0)
sp2 = sp;
else
sp1 = sp + 1;
}
return (-1);
}
static Char Stype;
static Char *Sgoal;
/*VARARGS2*/
1994-09-21 04:10:23 +04:00
static void
1993-03-21 12:45:37 +03:00
search(type, level, goal)
int type;
1997-01-13 20:53:15 +03:00
int level;
1993-03-21 12:45:37 +03:00
Char *goal;
{
Char wordbuf[BUFSIZ];
1997-01-13 20:53:15 +03:00
Char *aword = wordbuf;
Char *cp;
1993-03-21 12:45:37 +03:00
Stype = type;
Sgoal = goal;
1994-09-21 04:10:23 +04:00
if (type == T_GOTO) {
struct Ain a;
a.type = F_SEEK;
a.f_seek = 0;
bseek(&a);
}
1993-03-21 12:45:37 +03:00
do {
1994-09-21 04:10:23 +04:00
if (intty && fseekp == feobp && aret == F_SEEK)
(void) fprintf(cshout, "? "), (void) fflush(cshout);
1993-03-21 12:45:37 +03:00
aword[0] = 0;
(void) getword(aword);
switch (srchx(aword)) {
case T_ELSE:
if (level == 0 && type == T_IF)
return;
break;
case T_IF:
while (getword(aword))
continue;
if ((type == T_IF || type == T_ELSE) &&
eq(aword, STRthen))
level++;
break;
case T_ENDIF:
if (type == T_IF || type == T_ELSE)
level--;
break;
case T_FOREACH:
case T_WHILE:
if (type == T_BREAK)
level++;
break;
case T_END:
if (type == T_BREAK)
level--;
break;
case T_SWITCH:
if (type == T_SWITCH || type == T_BRKSW)
level++;
break;
case T_ENDSW:
if (type == T_SWITCH || type == T_BRKSW)
level--;
break;
case T_LABEL:
if (type == T_GOTO && getword(aword) && eq(aword, goal))
level = -1;
break;
default:
if (type != T_GOTO && (type != T_SWITCH || level != 0))
break;
if (lastchr(aword) != ':')
break;
aword[Strlen(aword) - 1] = 0;
1994-09-21 04:10:23 +04:00
if ((type == T_GOTO && eq(aword, goal)) ||
(type == T_SWITCH && eq(aword, STRdefault)))
1993-03-21 12:45:37 +03:00
level = -1;
break;
case T_CASE:
if (type != T_SWITCH || level != 0)
break;
(void) getword(aword);
if (lastchr(aword) == ':')
aword[Strlen(aword) - 1] = 0;
cp = strip(Dfix1(aword));
if (Gmatch(goal, cp))
level = -1;
xfree((ptr_t) cp);
break;
case T_DEFAULT:
if (type == T_SWITCH && level == 0)
level = -1;
break;
}
(void) getword(NULL);
} while (level >= 0);
}
static int
getword(wp)
1997-01-13 20:53:15 +03:00
Char *wp;
1993-03-21 12:45:37 +03:00
{
1997-01-13 20:53:15 +03:00
int found = 0;
int c, d;
1993-03-21 12:45:37 +03:00
int kwd = 0;
Char *owp = wp;
c = readc(1);
d = 0;
do {
while (c == ' ' || c == '\t')
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);
}
unreadc(c);
found = 1;
do {
c = readc(1);
if (c == '\\' && (c = readc(1)) == '\n')
c = ' ';
if (c == '\'' || c == '"')
if (d == 0)
d = c;
else if (d == c)
d = 0;
if (c < 0)
goto past;
if (wp) {
*wp++ = c;
*wp = 0; /* end the string b4 test */
}
1994-09-21 04:10:23 +04:00
} while ((d || (!(kwd = keyword(owp)) && c != ' '
&& c != '\t')) && c != '\n');
1993-03-21 12:45:37 +03:00
} while (wp == 0);
/*
* if we have read a keyword ( "if", "switch" or "while" ) then we do not
* need to unreadc the look-ahead char
*/
if (!kwd) {
unreadc(c);
if (found)
*--wp = 0;
}
return (found);
past:
switch (Stype) {
case T_IF:
stderror(ERR_NAME | ERR_NOTFOUND, "then/endif");
1998-07-28 06:23:37 +04:00
/* NOTREACHED */
1993-03-21 12:45:37 +03:00
case T_ELSE:
stderror(ERR_NAME | ERR_NOTFOUND, "endif");
1998-07-28 06:23:37 +04:00
/* NOTREACHED */
1993-03-21 12:45:37 +03:00
case T_BRKSW:
case T_SWITCH:
stderror(ERR_NAME | ERR_NOTFOUND, "endsw");
1998-07-28 06:23:37 +04:00
/* NOTREACHED */
1993-03-21 12:45:37 +03:00
case T_BREAK:
stderror(ERR_NAME | ERR_NOTFOUND, "end");
1998-07-28 06:23:37 +04:00
/* NOTREACHED */
1993-03-21 12:45:37 +03:00
case T_GOTO:
1994-09-21 04:10:23 +04:00
setname(vis_str(Sgoal));
1993-03-21 12:45:37 +03:00
stderror(ERR_NAME | ERR_NOTFOUND, "label");
1998-07-28 06:23:37 +04:00
/* NOTREACHED */
1993-03-21 12:45:37 +03:00
}
return (0);
}
/*
* keyword(wp) determines if wp is one of the built-n functions if,
* switch or while. It seems that when an if statement looks like
* "if(" then getword above sucks in the '(' and so the search routine
* never finds what it is scanning for. Rather than rewrite doword, I hack
* in a test to see if the string forms a keyword. Then doword stops
* and returns the word "if" -strike
*/
static int
keyword(wp)
Char *wp;
{
static Char STRif[] = {'i', 'f', '\0'};
static Char STRwhile[] = {'w', 'h', 'i', 'l', 'e', '\0'};
static Char STRswitch[] = {'s', 'w', 'i', 't', 'c', 'h', '\0'};
if (!wp)
return (0);
if ((Strcmp(wp, STRif) == 0) || (Strcmp(wp, STRwhile) == 0)
|| (Strcmp(wp, STRswitch) == 0))
return (1);
return (0);
}
static void
toend()
{
1994-09-21 04:10:23 +04:00
if (whyles->w_end.type == F_SEEK && whyles->w_end.f_seek == 0) {
1993-03-21 12:45:37 +03:00
search(T_BREAK, 0, NULL);
1994-09-21 04:10:23 +04:00
btell(&whyles->w_end);
whyles->w_end.f_seek--;
1993-03-21 12:45:37 +03:00
}
else
1994-09-21 04:10:23 +04:00
bseek(&whyles->w_end);
1993-03-21 12:45:37 +03:00
wfree();
}
void
wfree()
{
1994-09-21 04:10:23 +04:00
struct Ain o;
struct whyle *nwp;
btell(&o);
1993-03-21 12:45:37 +03:00
1994-09-21 04:10:23 +04:00
for (; whyles; whyles = nwp) {
1997-01-13 20:53:15 +03:00
struct whyle *wp = whyles;
1994-09-21 04:10:23 +04:00
nwp = wp->w_next;
/*
* We free loops that have different seek types.
*/
if (wp->w_end.type != I_SEEK && wp->w_start.type == wp->w_end.type &&
wp->w_start.type == o.type) {
if (wp->w_end.type == F_SEEK) {
if (o.f_seek >= wp->w_start.f_seek &&
(wp->w_end.f_seek == 0 || o.f_seek < wp->w_end.f_seek))
break;
}
else {
if (o.a_seek >= wp->w_start.a_seek &&
(wp->w_end.a_seek == 0 || o.a_seek < wp->w_end.a_seek))
break;
}
}
1993-03-21 12:45:37 +03:00
if (wp->w_fe0)
blkfree(wp->w_fe0);
if (wp->w_fename)
xfree((ptr_t) wp->w_fename);
xfree((ptr_t) wp);
}
}
void
1994-09-21 04:10:23 +04:00
/*ARGSUSED*/
doecho(v, t)
Char **v;
struct command *t;
1993-03-21 12:45:37 +03:00
{
xecho(' ', v);
}
void
1994-09-21 04:10:23 +04:00
/*ARGSUSED*/
doglob(v, t)
Char **v;
struct command *t;
1993-03-21 12:45:37 +03:00
{
xecho(0, v);
1994-09-21 04:10:23 +04:00
(void) fflush(cshout);
1993-03-21 12:45:37 +03:00
}
static void
xecho(sep, v)
int sep;
1997-01-13 20:53:15 +03:00
Char **v;
1993-03-21 12:45:37 +03:00
{
1997-01-13 20:53:15 +03:00
Char *cp;
1993-03-21 12:45:37 +03:00
int nonl = 0;
1995-03-21 21:35:32 +03:00
sigset_t sigset;
1993-03-21 12:45:37 +03:00
1995-03-21 21:35:32 +03:00
if (setintr) {
sigemptyset(&sigset);
1998-07-28 06:47:19 +04:00
(void) sigaddset(&sigset, SIGINT);
(void) sigprocmask(SIG_UNBLOCK, &sigset, NULL);
1995-03-21 21:35:32 +03:00
}
1993-03-21 12:45:37 +03:00
v++;
if (*v == 0)
return;
gflag = 0, tglob(v);
if (gflag) {
v = globall(v);
if (v == 0)
1993-03-21 12:45:37 +03:00
stderror(ERR_NAME | ERR_NOMATCH);
}
else {
v = gargv = saveblk(v);
trim(v);
}
if (sep == ' ' && *v && eq(*v, STRmn))
nonl++, v++;
1994-09-21 04:10:23 +04:00
while ((cp = *v++) != NULL) {
1997-01-13 20:53:15 +03:00
int c;
1993-03-21 12:45:37 +03:00
1994-09-21 04:10:23 +04:00
while ((c = *cp++) != '\0')
(void) vis_fputc(c | QUOTE, cshout);
1993-03-21 12:45:37 +03:00
if (*v)
1994-09-21 04:10:23 +04:00
(void) vis_fputc(sep | QUOTE, cshout);
1993-03-21 12:45:37 +03:00
}
if (sep && nonl == 0)
1994-09-21 04:10:23 +04:00
(void) fputc('\n', cshout);
1993-03-21 12:45:37 +03:00
else
1994-09-21 04:10:23 +04:00
(void) fflush(cshout);
1993-03-21 12:45:37 +03:00
if (setintr)
1998-07-28 06:47:19 +04:00
(void) sigprocmask(SIG_BLOCK, &sigset, NULL);
1993-03-21 12:45:37 +03:00
if (gargv)
blkfree(gargv), gargv = 0;
}
void
1994-09-21 04:10:23 +04:00
/*ARGSUSED*/
dosetenv(v, t)
Char **v;
struct command *t;
1993-03-21 12:45:37 +03:00
{
Char *vp, *lp;
1995-03-21 21:35:32 +03:00
sigset_t sigset;
1993-03-21 12:45:37 +03:00
v++;
if ((vp = *v++) == 0) {
1997-01-13 20:53:15 +03:00
Char **ep;
1993-03-21 12:45:37 +03:00
1995-03-21 21:35:32 +03:00
if (setintr) {
sigemptyset(&sigset);
1998-07-28 06:47:19 +04:00
(void) sigaddset(&sigset, SIGINT);
(void) sigprocmask(SIG_UNBLOCK, &sigset, NULL);
1995-03-21 21:35:32 +03:00
}
1993-03-21 12:45:37 +03:00
for (ep = STR_environ; *ep; ep++)
1994-09-21 04:10:23 +04:00
(void) fprintf(cshout, "%s\n", vis_str(*ep));
1993-03-21 12:45:37 +03:00
return;
}
if ((lp = *v++) == 0)
lp = STRNULL;
1994-09-21 04:10:23 +04:00
Setenv(vp, lp = globone(lp, G_APPEND));
1993-03-21 12:45:37 +03:00
if (eq(vp, STRPATH)) {
importpath(lp);
1994-09-21 04:10:23 +04:00
dohash(NULL, NULL);
1993-03-21 12:45:37 +03:00
}
else if (eq(vp, STRLANG) || eq(vp, STRLC_CTYPE)) {
#ifdef NLS
int k;
(void) setlocale(LC_ALL, "");
1994-09-21 04:10:23 +04:00
for (k = 0200; k <= 0377 && !Isprint(k); k++)
continue;
1993-03-21 12:45:37 +03:00
AsciiOnly = k > 0377;
#else
AsciiOnly = 0;
#endif /* NLS */
}
xfree((ptr_t) lp);
}
void
1994-09-21 04:10:23 +04:00
/*ARGSUSED*/
dounsetenv(v, t)
Char **v;
struct command *t;
1993-03-21 12:45:37 +03:00
{
Char **ep, *p, *n;
int i, maxi;
static Char *name = NULL;
if (name)
xfree((ptr_t) name);
/*
* Find the longest environment variable
*/
for (maxi = 0, ep = STR_environ; *ep; ep++) {
1994-09-21 04:10:23 +04:00
for (i = 0, p = *ep; *p && *p != '='; p++, i++)
continue;
1993-03-21 12:45:37 +03:00
if (i > maxi)
maxi = i;
}
name = (Char *) xmalloc((size_t) (maxi + 1) * sizeof(Char));
while (++v && *v)
for (maxi = 1; maxi;)
for (maxi = 0, ep = STR_environ; *ep; ep++) {
1994-09-21 04:10:23 +04:00
for (n = name, p = *ep; *p && *p != '='; *n++ = *p++)
continue;
1993-03-21 12:45:37 +03:00
*n = '\0';
if (!Gmatch(name, *v))
continue;
maxi = 1;
if (eq(name, STRLANG) || eq(name, STRLC_CTYPE)) {
#ifdef NLS
int k;
(void) setlocale(LC_ALL, "");
1994-09-21 04:10:23 +04:00
for (k = 0200; k <= 0377 && !Isprint(k); k++)
continue;
1993-03-21 12:45:37 +03:00
AsciiOnly = k > 0377;
#else
AsciiOnly = getenv("LANG") == NULL &&
getenv("LC_CTYPE") == NULL;
#endif /* NLS */
}
/*
* Delete name, and start again cause the environment changes
*/
Unsetenv(name);
break;
}
1994-09-21 04:10:23 +04:00
xfree((ptr_t) name);
name = NULL;
1993-03-21 12:45:37 +03:00
}
void
Setenv(name, val)
Char *name, *val;
{
1997-01-13 20:53:15 +03:00
Char **ep = STR_environ;
Char *cp, *dp;
1993-03-21 12:45:37 +03:00
Char *blk[2];
Char **oep = ep;
for (; *ep; ep++) {
for (cp = name, dp = *ep; *cp && *cp == *dp; cp++, dp++)
continue;
if (*cp != 0 || *dp != '=')
continue;
cp = Strspl(STRequal, val);
xfree((ptr_t) * ep);
*ep = strip(Strspl(name, cp));
xfree((ptr_t) cp);
blkfree((Char **) environ);
environ = short2blk(STR_environ);
return;
}
cp = Strspl(name, STRequal);
blk[0] = strip(Strspl(cp, val));
xfree((ptr_t) cp);
blk[1] = 0;
STR_environ = blkspl(STR_environ, blk);
blkfree((Char **) environ);
environ = short2blk(STR_environ);
xfree((ptr_t) oep);
}
static void
Unsetenv(name)
Char *name;
{
1997-01-13 20:53:15 +03:00
Char **ep = STR_environ;
Char *cp, *dp;
1993-03-21 12:45:37 +03:00
Char **oep = ep;
for (; *ep; ep++) {
for (cp = name, dp = *ep; *cp && *cp == *dp; cp++, dp++)
continue;
if (*cp != 0 || *dp != '=')
continue;
cp = *ep;
*ep = 0;
STR_environ = blkspl(STR_environ, ep + 1);
environ = short2blk(STR_environ);
*ep = cp;
xfree((ptr_t) cp);
xfree((ptr_t) oep);
return;
}
}
void
1994-09-21 04:10:23 +04:00
/*ARGSUSED*/
doumask(v, t)
Char **v;
struct command *t;
1993-03-21 12:45:37 +03:00
{
1997-01-13 20:53:15 +03:00
Char *cp = v[1];
int i;
1993-03-21 12:45:37 +03:00
if (cp == 0) {
i = umask(0);
(void) umask(i);
1994-09-21 04:10:23 +04:00
(void) fprintf(cshout, "%o\n", i);
1993-03-21 12:45:37 +03:00
return;
}
i = 0;
while (Isdigit(*cp) && *cp != '8' && *cp != '9')
i = i * 8 + *cp++ - '0';
if (*cp || i < 0 || i > 0777)
1993-03-21 12:45:37 +03:00
stderror(ERR_NAME | ERR_MASK);
(void) umask(i);
}
1994-05-05 07:00:45 +04:00
typedef quad_t RLIM_TYPE;
1993-03-21 12:45:37 +03:00
1998-07-28 06:23:37 +04:00
static const struct limits {
1993-03-21 12:45:37 +03:00
int limconst;
1998-07-28 06:23:37 +04:00
const char *limname;
1993-03-21 12:45:37 +03:00
int limdiv;
1998-07-28 06:23:37 +04:00
const char *limscale;
1993-03-21 12:45:37 +03:00
} limits[] = {
1994-09-21 04:10:23 +04:00
{ RLIMIT_CPU, "cputime", 1, "seconds" },
{ RLIMIT_FSIZE, "filesize", 1024, "kbytes" },
{ RLIMIT_DATA, "datasize", 1024, "kbytes" },
{ RLIMIT_STACK, "stacksize", 1024, "kbytes" },
{ RLIMIT_CORE, "coredumpsize", 1024, "kbytes" },
{ RLIMIT_RSS, "memoryuse", 1024, "kbytes" },
{ RLIMIT_MEMLOCK, "memorylocked", 1024, "kbytes" },
{ RLIMIT_NPROC, "maxproc", 1, "" },
{ RLIMIT_NOFILE, "openfiles", 1, "" },
{ -1, NULL, 0, NULL }
1993-03-21 12:45:37 +03:00
};
1998-07-28 06:23:37 +04:00
static const struct limits *findlim __P((Char *));
static RLIM_TYPE getval __P((const struct limits *, Char **));
static void limtail __P((Char *, char *));
1998-07-28 06:23:37 +04:00
static void plim __P((const struct limits *, Char));
static int setlim __P((const struct limits *, Char, RLIM_TYPE));
1993-03-21 12:45:37 +03:00
1998-07-28 06:23:37 +04:00
static const struct limits *
1993-03-21 12:45:37 +03:00
findlim(cp)
Char *cp;
{
1998-07-28 06:23:37 +04:00
const struct limits *lp, *res;
1993-03-21 12:45:37 +03:00
res = (struct limits *) NULL;
for (lp = limits; lp->limconst >= 0; lp++)
if (prefix(cp, str2short(lp->limname))) {
if (res)
1993-03-21 12:45:37 +03:00
stderror(ERR_NAME | ERR_AMBIG);
res = lp;
}
if (res)
return (res);
stderror(ERR_NAME | ERR_LIMIT);
/* NOTREACHED */
}
void
1994-09-21 04:10:23 +04:00
/*ARGSUSED*/
dolimit(v, t)
Char **v;
struct command *t;
1993-03-21 12:45:37 +03:00
{
1998-07-28 06:23:37 +04:00
const struct limits *lp;
1997-01-13 20:53:15 +03:00
RLIM_TYPE limit;
1993-03-21 12:45:37 +03:00
char hard = 0;
v++;
if (*v && eq(*v, STRmh)) {
hard = 1;
v++;
}
if (*v == 0) {
for (lp = limits; lp->limconst >= 0; lp++)
plim(lp, hard);
return;
}
lp = findlim(v[0]);
if (v[1] == 0) {
plim(lp, hard);
return;
}
limit = getval(lp, v + 1);
if (setlim(lp, hard, limit) < 0)
1993-03-21 12:45:37 +03:00
stderror(ERR_SILENT);
}
static RLIM_TYPE
getval(lp, v)
1998-07-28 06:23:37 +04:00
const struct limits *lp;
1993-03-21 12:45:37 +03:00
Char **v;
{
1997-01-13 20:53:15 +03:00
float f;
1993-03-21 12:45:37 +03:00
Char *cp = *v++;
f = atof(short2str(cp));
while (Isdigit(*cp) || *cp == '.' || *cp == 'e' || *cp == 'E')
cp++;
if (*cp == 0) {
if (*v == 0)
return ((RLIM_TYPE) ((f + 0.5) * lp->limdiv));
cp = *v;
}
switch (*cp) {
case ':':
if (lp->limconst != RLIMIT_CPU)
goto badscal;
return ((RLIM_TYPE) (f * 60.0 + atof(short2str(cp + 1))));
case 'h':
if (lp->limconst != RLIMIT_CPU)
goto badscal;
limtail(cp, "hours");
f *= 3600.0;
break;
case 'm':
if (lp->limconst == RLIMIT_CPU) {
limtail(cp, "minutes");
f *= 60.0;
break;
}
*cp = 'm';
limtail(cp, "megabytes");
f *= 1024.0 * 1024.0;
break;
case 's':
if (lp->limconst != RLIMIT_CPU)
goto badscal;
limtail(cp, "seconds");
break;
case 'M':
if (lp->limconst == RLIMIT_CPU)
goto badscal;
*cp = 'm';
limtail(cp, "megabytes");
f *= 1024.0 * 1024.0;
break;
case 'k':
if (lp->limconst == RLIMIT_CPU)
goto badscal;
limtail(cp, "kbytes");
f *= 1024.0;
break;
case 'u':
limtail(cp, "unlimited");
return (RLIM_INFINITY);
default:
1998-07-28 06:23:37 +04:00
badscal:
1993-03-21 12:45:37 +03:00
stderror(ERR_NAME | ERR_SCALEF);
1998-07-28 06:23:37 +04:00
/* NOTREACHED */
1993-03-21 12:45:37 +03:00
}
1994-09-21 04:10:23 +04:00
f += 0.5;
if (f > (float) RLIM_INFINITY)
return RLIM_INFINITY;
else
return ((RLIM_TYPE) f);
1993-03-21 12:45:37 +03:00
}
static void
limtail(cp, str)
Char *cp;
char *str;
{
while (*cp && *cp == *str)
cp++, str++;
if (*cp)
1993-03-21 12:45:37 +03:00
stderror(ERR_BADSCALE, str);
}
/*ARGSUSED*/
static void
plim(lp, hard)
1998-07-28 06:23:37 +04:00
const struct limits *lp;
1993-03-21 12:45:37 +03:00
Char hard;
{
struct rlimit rlim;
RLIM_TYPE limit;
1994-09-21 04:10:23 +04:00
(void) fprintf(cshout, "%s \t", lp->limname);
1993-03-21 12:45:37 +03:00
(void) getrlimit(lp->limconst, &rlim);
limit = hard ? rlim.rlim_max : rlim.rlim_cur;
if (limit == RLIM_INFINITY)
1994-09-21 04:10:23 +04:00
(void) fprintf(cshout, "unlimited");
1993-03-21 12:45:37 +03:00
else if (lp->limconst == RLIMIT_CPU)
psecs((long) limit);
else
1994-09-21 04:10:23 +04:00
(void) fprintf(cshout, "%ld %s", (long) (limit / lp->limdiv),
lp->limscale);
(void) fputc('\n', cshout);
1993-03-21 12:45:37 +03:00
}
void
1994-09-21 04:10:23 +04:00
/*ARGSUSED*/
dounlimit(v, t)
Char **v;
struct command *t;
1993-03-21 12:45:37 +03:00
{
1998-07-28 06:23:37 +04:00
const struct limits *lp;
1993-03-21 12:45:37 +03:00
int lerr = 0;
Char hard = 0;
v++;
if (*v && eq(*v, STRmh)) {
hard = 1;
v++;
}
if (*v == 0) {
for (lp = limits; lp->limconst >= 0; lp++)
if (setlim(lp, hard, (RLIM_TYPE) RLIM_INFINITY) < 0)
lerr++;
if (lerr)
1993-03-21 12:45:37 +03:00
stderror(ERR_SILENT);
return;
}
while (*v) {
lp = findlim(*v++);
if (setlim(lp, hard, (RLIM_TYPE) RLIM_INFINITY) < 0)
1993-03-21 12:45:37 +03:00
stderror(ERR_SILENT);
}
}
static int
setlim(lp, hard, limit)
1998-07-28 06:23:37 +04:00
const struct limits *lp;
1993-03-21 12:45:37 +03:00
Char hard;
RLIM_TYPE limit;
{
struct rlimit rlim;
(void) getrlimit(lp->limconst, &rlim);
if (hard)
rlim.rlim_max = limit;
else if (limit == RLIM_INFINITY && geteuid() != 0)
rlim.rlim_cur = rlim.rlim_max;
else
rlim.rlim_cur = limit;
if (setrlimit(lp->limconst, &rlim) < 0) {
1994-09-21 04:10:23 +04:00
(void) fprintf(csherr, "%s: %s: Can't %s%s limit\n", bname, lp->limname,
limit == RLIM_INFINITY ? "remove" : "set",
hard ? " hard" : "");
1993-03-21 12:45:37 +03:00
return (-1);
}
return (0);
}
void
1994-09-21 04:10:23 +04:00
/*ARGSUSED*/
dosuspend(v, t)
Char **v;
struct command *t;
1993-03-21 12:45:37 +03:00
{
int ctpgrp;
void (*old) __P((int));
1993-03-21 12:45:37 +03:00
if (loginsh)
1993-03-21 12:45:37 +03:00
stderror(ERR_SUSPLOG);
untty();
old = signal(SIGTSTP, SIG_DFL);
(void) kill(0, SIGTSTP);
/* the shell stops here */
(void) signal(SIGTSTP, old);
if (tpgrp != -1) {
retry:
1993-03-21 12:45:37 +03:00
ctpgrp = tcgetpgrp(FSHTTY);
if (ctpgrp != opgrp) {
1993-03-21 12:45:37 +03:00
old = signal(SIGTTIN, SIG_DFL);
(void) kill(0, SIGTTIN);
(void) signal(SIGTTIN, old);
goto retry;
1993-03-21 12:45:37 +03:00
}
(void) setpgid(0, shpgrp);
(void) tcsetpgrp(FSHTTY, shpgrp);
}
}
/* This is the dreaded EVAL built-in.
* If you don't fiddle with file descriptors, and reset didfds,
* this command will either ignore redirection inside or outside
* its aguments, e.g. eval "date >x" vs. eval "date" >x
* The stuff here seems to work, but I did it by trial and error rather
* than really knowing what was going on. If tpgrp is zero, we are
* probably a background eval, e.g. "eval date &", and we want to
* make sure that any processes we start stay in our pgrp.
* This is also the case for "time eval date" -- stay in same pgrp.
* Otherwise, under stty tostop, processes will stop in the wrong
* pgrp, with no way for the shell to get them going again. -IAN!
*/
1994-09-21 04:10:23 +04:00
static Char **gv = NULL;
1993-03-21 12:45:37 +03:00
void
1994-09-21 04:10:23 +04:00
/*ARGSUSED*/
doeval(v, t)
Char **v;
struct command *t;
1993-03-21 12:45:37 +03:00
{
Char **oevalvec;
Char *oevalp;
int odidfds;
jmp_buf osetexit;
int my_reenter;
1994-09-21 04:10:23 +04:00
Char **savegv = gv;
1993-03-21 12:45:37 +03:00
int saveIN;
int saveOUT;
1994-09-21 04:10:23 +04:00
int saveERR;
1993-03-21 12:45:37 +03:00
int oSHIN;
int oSHOUT;
1994-09-21 04:10:23 +04:00
int oSHERR;
UNREGISTER(v);
1993-03-21 12:45:37 +03:00
oevalvec = evalvec;
oevalp = evalp;
odidfds = didfds;
oSHIN = SHIN;
oSHOUT = SHOUT;
1994-09-21 04:10:23 +04:00
oSHERR = SHERR;
1993-03-21 12:45:37 +03:00
v++;
if (*v == 0)
return;
gflag = 0, tglob(v);
if (gflag) {
gv = v = globall(v);
gargv = 0;
if (v == 0)
1993-03-21 12:45:37 +03:00
stderror(ERR_NOMATCH);
v = copyblk(v);
}
else {
gv = NULL;
v = copyblk(v);
trim(v);
}
saveIN = dcopy(SHIN, -1);
saveOUT = dcopy(SHOUT, -1);
1994-09-21 04:10:23 +04:00
saveERR = dcopy(SHERR, -1);
1993-03-21 12:45:37 +03:00
getexit(osetexit);
if ((my_reenter = setexit()) == 0) {
evalvec = v;
evalp = 0;
SHIN = dcopy(0, -1);
SHOUT = dcopy(1, -1);
1994-09-21 04:10:23 +04:00
SHERR = dcopy(2, -1);
1993-03-21 12:45:37 +03:00
didfds = 0;
process(0);
}
evalvec = oevalvec;
evalp = oevalp;
doneinp = 0;
didfds = odidfds;
(void) close(SHIN);
(void) close(SHOUT);
1994-09-21 04:10:23 +04:00
(void) close(SHERR);
1993-03-21 12:45:37 +03:00
SHIN = dmove(saveIN, oSHIN);
SHOUT = dmove(saveOUT, oSHOUT);
1994-09-21 04:10:23 +04:00
SHERR = dmove(saveERR, oSHERR);
1993-03-21 12:45:37 +03:00
if (gv)
1994-09-21 04:10:23 +04:00
blkfree(gv), gv = NULL;
1993-03-21 12:45:37 +03:00
resexit(osetexit);
1994-09-21 04:10:23 +04:00
gv = savegv;
if (my_reenter)
1993-03-21 12:45:37 +03:00
stderror(ERR_SILENT);
}
1994-09-21 04:10:23 +04:00
void
/*ARGSUSED*/
doprintf(v, t)
Char **v;
struct command *t;
{
char **c;
extern int progprintf __P((int, char **));
int ret;
ret = progprintf(blklen(v), c = short2blk(v));
(void) fflush(cshout);
(void) fflush(csherr);
blkfree((Char **) c);
if (ret)
1994-09-21 04:10:23 +04:00
stderror(ERR_SILENT);
}