xref: /freebsd/contrib/tcsh/sh.func.c (revision 6560ac57ce879857203bc456cdc3849808dc0700)
1c80476e4SDavid E. O'Brien /*
2c80476e4SDavid E. O'Brien  * sh.func.c: csh builtin functions
3c80476e4SDavid E. O'Brien  */
4c80476e4SDavid E. O'Brien /*-
5c80476e4SDavid E. O'Brien  * Copyright (c) 1980, 1991 The Regents of the University of California.
6c80476e4SDavid E. O'Brien  * All rights reserved.
7c80476e4SDavid E. O'Brien  *
8c80476e4SDavid E. O'Brien  * Redistribution and use in source and binary forms, with or without
9c80476e4SDavid E. O'Brien  * modification, are permitted provided that the following conditions
10c80476e4SDavid E. O'Brien  * are met:
11c80476e4SDavid E. O'Brien  * 1. Redistributions of source code must retain the above copyright
12c80476e4SDavid E. O'Brien  *    notice, this list of conditions and the following disclaimer.
13c80476e4SDavid E. O'Brien  * 2. Redistributions in binary form must reproduce the above copyright
14c80476e4SDavid E. O'Brien  *    notice, this list of conditions and the following disclaimer in the
15c80476e4SDavid E. O'Brien  *    documentation and/or other materials provided with the distribution.
1629301572SMark Peek  * 3. Neither the name of the University nor the names of its contributors
17c80476e4SDavid E. O'Brien  *    may be used to endorse or promote products derived from this software
18c80476e4SDavid E. O'Brien  *    without specific prior written permission.
19c80476e4SDavid E. O'Brien  *
20c80476e4SDavid E. O'Brien  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21c80476e4SDavid E. O'Brien  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22c80476e4SDavid E. O'Brien  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23c80476e4SDavid E. O'Brien  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24c80476e4SDavid E. O'Brien  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25c80476e4SDavid E. O'Brien  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26c80476e4SDavid E. O'Brien  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27c80476e4SDavid E. O'Brien  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28c80476e4SDavid E. O'Brien  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29c80476e4SDavid E. O'Brien  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30c80476e4SDavid E. O'Brien  * SUCH DAMAGE.
31c80476e4SDavid E. O'Brien  */
32c80476e4SDavid E. O'Brien #include "sh.h"
33c80476e4SDavid E. O'Brien #include "ed.h"
34c80476e4SDavid E. O'Brien #include "tw.h"
35c80476e4SDavid E. O'Brien #include "tc.h"
363b6eaa7bSAndrey A. Chernov #ifdef WINNT_NATIVE
37c80476e4SDavid E. O'Brien #include "nt.const.h"
383b6eaa7bSAndrey A. Chernov #endif /* WINNT_NATIVE */
39c80476e4SDavid E. O'Brien 
409ccc37e3SMark Peek #if defined (NLS_CATALOGS) && defined(HAVE_ICONV)
4123338178SMark Peek static iconv_t catgets_iconv; /* Or (iconv_t)-1 */
4223338178SMark Peek #endif
4323338178SMark Peek 
44c80476e4SDavid E. O'Brien /*
45c80476e4SDavid E. O'Brien  * C shell
46c80476e4SDavid E. O'Brien  */
47c80476e4SDavid E. O'Brien 
4823338178SMark Peek extern int MapsAreInited;
4923338178SMark Peek extern int NLSMapsAreInited;
5023338178SMark Peek extern int GotTermCaps;
51c80476e4SDavid E. O'Brien 
52c80476e4SDavid E. O'Brien static int zlast = -1;
53c80476e4SDavid E. O'Brien 
5445e5710bSMark Peek static	void	islogin		(void);
5545e5710bSMark Peek static	void	preread		(void);
5645e5710bSMark Peek static	void	doagain		(void);
5745e5710bSMark Peek static  const char *isrchx	(int);
5845e5710bSMark Peek static	void	search		(int, int, Char *);
5945e5710bSMark Peek static	int	getword		(struct Strbuf *);
60a15e6f9aSMark Peek static	struct wordent	*histgetword	(struct wordent *);
6145e5710bSMark Peek static	void	toend		(void);
6245e5710bSMark Peek static	void	xecho		(int, Char **);
6345e5710bSMark Peek static	int	islocale_var	(Char *);
6445e5710bSMark Peek static	void	wpfree		(struct whyle *);
65c80476e4SDavid E. O'Brien 
6645e5710bSMark Peek const struct biltins *
isbfunc(struct command * t)6745e5710bSMark Peek isbfunc(struct command *t)
68c80476e4SDavid E. O'Brien {
6923338178SMark Peek     Char *cp = t->t_dcom[0];
7045e5710bSMark Peek     const struct biltins *bp, *bp1, *bp2;
71c80476e4SDavid E. O'Brien     static struct biltins label = {"", dozip, 0, 0};
72c80476e4SDavid E. O'Brien     static struct biltins foregnd = {"%job", dofg1, 0, 0};
73c80476e4SDavid E. O'Brien     static struct biltins backgnd = {"%job &", dobg1, 0, 0};
74c80476e4SDavid E. O'Brien 
75c80476e4SDavid E. O'Brien     /*
76c80476e4SDavid E. O'Brien      * We never match a builtin that has quoted the first
77c80476e4SDavid E. O'Brien      * character; this has been the traditional way to escape
78c80476e4SDavid E. O'Brien      * builtin commands.
79c80476e4SDavid E. O'Brien      */
80c80476e4SDavid E. O'Brien     if (*cp & QUOTE)
81c80476e4SDavid E. O'Brien 	return NULL;
82c80476e4SDavid E. O'Brien 
83c80476e4SDavid E. O'Brien     if (*cp != ':' && lastchr(cp) == ':') {
84c80476e4SDavid E. O'Brien 	label.bname = short2str(cp);
85c80476e4SDavid E. O'Brien 	return (&label);
86c80476e4SDavid E. O'Brien     }
87c80476e4SDavid E. O'Brien     if (*cp == '%') {
88c80476e4SDavid E. O'Brien 	if (t->t_dflg & F_AMPERSAND) {
89c80476e4SDavid E. O'Brien 	    t->t_dflg &= ~F_AMPERSAND;
90c80476e4SDavid E. O'Brien 	    backgnd.bname = short2str(cp);
91c80476e4SDavid E. O'Brien 	    return (&backgnd);
92c80476e4SDavid E. O'Brien 	}
93c80476e4SDavid E. O'Brien 	foregnd.bname = short2str(cp);
94c80476e4SDavid E. O'Brien 	return (&foregnd);
95c80476e4SDavid E. O'Brien     }
96c80476e4SDavid E. O'Brien #ifdef WARP
97c80476e4SDavid E. O'Brien     /*
98c80476e4SDavid E. O'Brien      * This is a perhaps kludgy way to determine if the warp builtin is to be
99c80476e4SDavid E. O'Brien      * acknowledged or not.  If checkwarp() fails, then we are to assume that
100c80476e4SDavid E. O'Brien      * the warp command is invalid, and carry on as we would handle any other
101c80476e4SDavid E. O'Brien      * non-builtin command.         -- JDK 2/4/88
102c80476e4SDavid E. O'Brien      */
103c80476e4SDavid E. O'Brien     if (eq(STRwarp, cp) && !checkwarp()) {
104c80476e4SDavid E. O'Brien 	return (0);		/* this builtin disabled */
105c80476e4SDavid E. O'Brien     }
106c80476e4SDavid E. O'Brien #endif /* WARP */
107c80476e4SDavid E. O'Brien     /*
108c80476e4SDavid E. O'Brien      * Binary search Bp1 is the beginning of the current search range. Bp2 is
109c80476e4SDavid E. O'Brien      * one past the end.
110c80476e4SDavid E. O'Brien      */
111c80476e4SDavid E. O'Brien     for (bp1 = bfunc, bp2 = bfunc + nbfunc; bp1 < bp2;) {
112c80476e4SDavid E. O'Brien 	int i;
113c80476e4SDavid E. O'Brien 
114c80476e4SDavid E. O'Brien 	bp = bp1 + ((bp2 - bp1) >> 1);
115c80476e4SDavid E. O'Brien 	if ((i = ((char) *cp) - *bp->bname) == 0 &&
116c80476e4SDavid E. O'Brien 	    (i = StrQcmp(cp, str2short(bp->bname))) == 0)
117c80476e4SDavid E. O'Brien 	    return bp;
118c80476e4SDavid E. O'Brien 	if (i < 0)
119c80476e4SDavid E. O'Brien 	    bp2 = bp;
120c80476e4SDavid E. O'Brien 	else
121c80476e4SDavid E. O'Brien 	    bp1 = bp + 1;
122c80476e4SDavid E. O'Brien     }
1233b6eaa7bSAndrey A. Chernov #ifdef WINNT_NATIVE
124c80476e4SDavid E. O'Brien     return nt_check_additional_builtins(cp);
1253b6eaa7bSAndrey A. Chernov #endif /*WINNT_NATIVE*/
126c80476e4SDavid E. O'Brien     return (0);
127c80476e4SDavid E. O'Brien }
128c80476e4SDavid E. O'Brien 
129c80476e4SDavid E. O'Brien void
func(struct command * t,const struct biltins * bp)13045e5710bSMark Peek func(struct command *t, const struct biltins *bp)
131c80476e4SDavid E. O'Brien {
132c80476e4SDavid E. O'Brien     int     i;
133c80476e4SDavid E. O'Brien 
134c80476e4SDavid E. O'Brien     xechoit(t->t_dcom);
135c80476e4SDavid E. O'Brien     setname(bp->bname);
136c80476e4SDavid E. O'Brien     i = blklen(t->t_dcom) - 1;
137c80476e4SDavid E. O'Brien     if (i < bp->minargs)
138c80476e4SDavid E. O'Brien 	stderror(ERR_NAME | ERR_TOOFEW);
139c80476e4SDavid E. O'Brien     if (i > bp->maxargs)
140c80476e4SDavid E. O'Brien 	stderror(ERR_NAME | ERR_TOOMANY);
141c80476e4SDavid E. O'Brien     (*bp->bfunct) (t->t_dcom, t);
142c80476e4SDavid E. O'Brien }
143c80476e4SDavid E. O'Brien 
144c80476e4SDavid E. O'Brien /*ARGSUSED*/
145c80476e4SDavid E. O'Brien void
doonintr(Char ** v,struct command * c)14645e5710bSMark Peek doonintr(Char **v, struct command *c)
147c80476e4SDavid E. O'Brien {
14823338178SMark Peek     Char *cp;
14923338178SMark Peek     Char *vv = v[1];
150c80476e4SDavid E. O'Brien 
151c80476e4SDavid E. O'Brien     USE(c);
15245e5710bSMark Peek     if (parintr.sa_handler == SIG_IGN)
153c80476e4SDavid E. O'Brien 	return;
154c80476e4SDavid E. O'Brien     if (setintr && intty)
155c80476e4SDavid E. O'Brien 	stderror(ERR_NAME | ERR_TERMINAL);
156c80476e4SDavid E. O'Brien     cp = gointr;
157c80476e4SDavid E. O'Brien     gointr = 0;
15845e5710bSMark Peek     xfree(cp);
159c80476e4SDavid E. O'Brien     if (vv == 0) {
16045e5710bSMark Peek 	if (setintr)
16145e5710bSMark Peek 	    sigset_interrupting(SIGINT, queue_pintr);
162c80476e4SDavid E. O'Brien 	else
163c80476e4SDavid E. O'Brien 	    (void) signal(SIGINT, SIG_DFL);
164c80476e4SDavid E. O'Brien 	gointr = 0;
165c80476e4SDavid E. O'Brien     }
166c80476e4SDavid E. O'Brien     else if (eq((vv = strip(vv)), STRminus)) {
167c80476e4SDavid E. O'Brien 	(void) signal(SIGINT, SIG_IGN);
168c80476e4SDavid E. O'Brien 	gointr = Strsave(STRminus);
169c80476e4SDavid E. O'Brien     }
170c80476e4SDavid E. O'Brien     else {
171c80476e4SDavid E. O'Brien 	gointr = Strsave(vv);
17245e5710bSMark Peek 	sigset_interrupting(SIGINT, queue_pintr);
173c80476e4SDavid E. O'Brien     }
174c80476e4SDavid E. O'Brien }
175c80476e4SDavid E. O'Brien 
176c80476e4SDavid E. O'Brien /*ARGSUSED*/
177c80476e4SDavid E. O'Brien void
donohup(Char ** v,struct command * c)17845e5710bSMark Peek donohup(Char **v, struct command *c)
179c80476e4SDavid E. O'Brien {
180c80476e4SDavid E. O'Brien     USE(c);
181c80476e4SDavid E. O'Brien     USE(v);
182c80476e4SDavid E. O'Brien     if (intty)
183c80476e4SDavid E. O'Brien 	stderror(ERR_NAME | ERR_TERMINAL);
184c80476e4SDavid E. O'Brien     if (setintr == 0) {
185c80476e4SDavid E. O'Brien 	(void) signal(SIGHUP, SIG_IGN);
18645e5710bSMark Peek 	phup_disabled = 1;
187c80476e4SDavid E. O'Brien #ifdef CC
188c80476e4SDavid E. O'Brien 	submit(getpid());
189c80476e4SDavid E. O'Brien #endif /* CC */
190c80476e4SDavid E. O'Brien     }
191c80476e4SDavid E. O'Brien }
192c80476e4SDavid E. O'Brien 
193c80476e4SDavid E. O'Brien /*ARGSUSED*/
194c80476e4SDavid E. O'Brien void
dohup(Char ** v,struct command * c)19545e5710bSMark Peek dohup(Char **v, struct command *c)
196c80476e4SDavid E. O'Brien {
197c80476e4SDavid E. O'Brien     USE(c);
198c80476e4SDavid E. O'Brien     USE(v);
199c80476e4SDavid E. O'Brien     if (intty)
200c80476e4SDavid E. O'Brien 	stderror(ERR_NAME | ERR_TERMINAL);
201c80476e4SDavid E. O'Brien     if (setintr == 0)
202cc698b49SBrooks Davis     	sigset_interrupting(SIGHUP, SIG_DFL);
203c80476e4SDavid E. O'Brien }
204c80476e4SDavid E. O'Brien 
205c80476e4SDavid E. O'Brien 
206c80476e4SDavid E. O'Brien /*ARGSUSED*/
207c80476e4SDavid E. O'Brien void
dozip(Char ** v,struct command * c)20845e5710bSMark Peek dozip(Char **v, struct command *c)
209c80476e4SDavid E. O'Brien {
210c80476e4SDavid E. O'Brien     USE(c);
211c80476e4SDavid E. O'Brien     USE(v);
212c80476e4SDavid E. O'Brien }
213c80476e4SDavid E. O'Brien 
214c80476e4SDavid E. O'Brien /*ARGSUSED*/
215c80476e4SDavid E. O'Brien void
dofiletest(Char ** v,struct command * c)21645e5710bSMark Peek dofiletest(Char **v, struct command *c)
217c80476e4SDavid E. O'Brien {
21845e5710bSMark Peek     Char **globbed, **fileptr, *ftest, *res;
219c80476e4SDavid E. O'Brien 
22023338178SMark Peek     USE(c);
221c80476e4SDavid E. O'Brien     if (*(ftest = *++v) != '-')
222c80476e4SDavid E. O'Brien 	stderror(ERR_NAME | ERR_FILEINQ);
223c80476e4SDavid E. O'Brien     ++v;
224c80476e4SDavid E. O'Brien 
22545e5710bSMark Peek     v = glob_all_or_error(v);
22645e5710bSMark Peek     globbed = v;
22745e5710bSMark Peek     cleanup_push(globbed, blk_cleanup);
228c80476e4SDavid E. O'Brien 
229cc698b49SBrooks Davis     while (*(fileptr = v++) != NULL) {
23045e5710bSMark Peek 	res = filetest(ftest, &fileptr, 0);
23145e5710bSMark Peek 	cleanup_push(res, xfree);
23245e5710bSMark Peek 	xprintf("%S", res);
23345e5710bSMark Peek 	cleanup_until(res);
234c80476e4SDavid E. O'Brien 	if (*v)
235c80476e4SDavid E. O'Brien 	    xprintf(" ");
236c80476e4SDavid E. O'Brien     }
237c80476e4SDavid E. O'Brien     xprintf("\n");
238c80476e4SDavid E. O'Brien 
23945e5710bSMark Peek     cleanup_until(globbed);
240c80476e4SDavid E. O'Brien }
241c80476e4SDavid E. O'Brien 
242c80476e4SDavid E. O'Brien void
prvars(void)24345e5710bSMark Peek prvars(void)
244c80476e4SDavid E. O'Brien {
245c80476e4SDavid E. O'Brien     plist(&shvhed, VAR_ALL);
246c80476e4SDavid E. O'Brien }
247c80476e4SDavid E. O'Brien 
248c80476e4SDavid E. O'Brien /*ARGSUSED*/
249c80476e4SDavid E. O'Brien void
doalias(Char ** v,struct command * c)25045e5710bSMark Peek doalias(Char **v, struct command *c)
251c80476e4SDavid E. O'Brien {
25223338178SMark Peek     struct varent *vp;
25323338178SMark Peek     Char *p;
254c80476e4SDavid E. O'Brien 
255c80476e4SDavid E. O'Brien     USE(c);
256c80476e4SDavid E. O'Brien     v++;
257c80476e4SDavid E. O'Brien     p = *v++;
258c80476e4SDavid E. O'Brien     if (p == 0)
259c80476e4SDavid E. O'Brien 	plist(&aliases, VAR_ALL);
260c80476e4SDavid E. O'Brien     else if (*v == 0) {
261c80476e4SDavid E. O'Brien 	vp = adrof1(strip(p), &aliases);
26229301572SMark Peek 	if (vp && vp->vec)
263c80476e4SDavid E. O'Brien 	    blkpr(vp->vec), xputchar('\n');
264c80476e4SDavid E. O'Brien     }
265c80476e4SDavid E. O'Brien     else {
266c80476e4SDavid E. O'Brien 	if (eq(p, STRalias) || eq(p, STRunalias)) {
267c80476e4SDavid E. O'Brien 	    setname(short2str(p));
268c80476e4SDavid E. O'Brien 	    stderror(ERR_NAME | ERR_DANGER);
269c80476e4SDavid E. O'Brien 	}
270c80476e4SDavid E. O'Brien 	set1(strip(p), saveblk(v), &aliases, VAR_READWRITE);
271c80476e4SDavid E. O'Brien 	tw_cmd_free();
272c80476e4SDavid E. O'Brien     }
273c80476e4SDavid E. O'Brien }
274c80476e4SDavid E. O'Brien 
275c80476e4SDavid E. O'Brien /*ARGSUSED*/
276c80476e4SDavid E. O'Brien void
unalias(Char ** v,struct command * c)27745e5710bSMark Peek unalias(Char **v, struct command *c)
278c80476e4SDavid E. O'Brien {
279c80476e4SDavid E. O'Brien     USE(c);
280c80476e4SDavid E. O'Brien     unset1(v, &aliases);
281c80476e4SDavid E. O'Brien     tw_cmd_free();
282c80476e4SDavid E. O'Brien }
283c80476e4SDavid E. O'Brien 
284c80476e4SDavid E. O'Brien /*ARGSUSED*/
285c80476e4SDavid E. O'Brien void
dologout(Char ** v,struct command * c)28645e5710bSMark Peek dologout(Char **v, struct command *c)
287c80476e4SDavid E. O'Brien {
288c80476e4SDavid E. O'Brien     USE(c);
289c80476e4SDavid E. O'Brien     USE(v);
290c80476e4SDavid E. O'Brien     islogin();
291c80476e4SDavid E. O'Brien     goodbye(NULL, NULL);
292c80476e4SDavid E. O'Brien }
293c80476e4SDavid E. O'Brien 
294c80476e4SDavid E. O'Brien /*ARGSUSED*/
295c80476e4SDavid E. O'Brien void
dologin(Char ** v,struct command * c)29645e5710bSMark Peek dologin(Char **v, struct command *c)
297c80476e4SDavid E. O'Brien {
2983b6eaa7bSAndrey A. Chernov #ifdef WINNT_NATIVE
299b2d5d167SMark Peek     USE(c);
300c80476e4SDavid E. O'Brien     USE(v);
3013b6eaa7bSAndrey A. Chernov #else /* !WINNT_NATIVE */
302b2d5d167SMark Peek     char **p = short2blk(v);
30345e5710bSMark Peek 
304b2d5d167SMark Peek     USE(c);
30545e5710bSMark Peek     cleanup_push((Char **)p, blk_cleanup);
306c80476e4SDavid E. O'Brien     islogin();
307c80476e4SDavid E. O'Brien     rechist(NULL, adrof(STRsavehist) != NULL);
30845e5710bSMark Peek     sigaction(SIGTERM, &parterm, NULL);
309b2d5d167SMark Peek     (void) execv(_PATH_BIN_LOGIN, p);
310b2d5d167SMark Peek     (void) execv(_PATH_USRBIN_LOGIN, p);
31145e5710bSMark Peek     cleanup_until((Char **)p);
312c80476e4SDavid E. O'Brien     untty();
313c80476e4SDavid E. O'Brien     xexit(1);
3143b6eaa7bSAndrey A. Chernov #endif /* !WINNT_NATIVE */
315c80476e4SDavid E. O'Brien }
316c80476e4SDavid E. O'Brien 
317c80476e4SDavid E. O'Brien 
318c80476e4SDavid E. O'Brien #ifdef NEWGRP
319c80476e4SDavid E. O'Brien /*ARGSUSED*/
320c80476e4SDavid E. O'Brien void
donewgrp(Char ** v,struct command * c)32145e5710bSMark Peek donewgrp(Char **v, struct command *c)
322c80476e4SDavid E. O'Brien {
323c80476e4SDavid E. O'Brien     char **p;
324c80476e4SDavid E. O'Brien     if (chkstop == 0 && setintr)
325c80476e4SDavid E. O'Brien 	panystop(0);
32645e5710bSMark Peek     sigaction(SIGTERM, &parterm, NULL);
327c80476e4SDavid E. O'Brien     p = short2blk(v);
328c80476e4SDavid E. O'Brien     /*
329c80476e4SDavid E. O'Brien      * From Beto Appleton (beto@aixwiz.austin.ibm.com)
330c80476e4SDavid E. O'Brien      * Newgrp can take 2 arguments...
331c80476e4SDavid E. O'Brien      */
332c80476e4SDavid E. O'Brien     (void) execv(_PATH_BIN_NEWGRP, p);
333c80476e4SDavid E. O'Brien     (void) execv(_PATH_USRBIN_NEWGRP, p);
334c80476e4SDavid E. O'Brien     blkfree((Char **) p);
335c80476e4SDavid E. O'Brien     untty();
336c80476e4SDavid E. O'Brien     xexit(1);
337c80476e4SDavid E. O'Brien }
338c80476e4SDavid E. O'Brien #endif /* NEWGRP */
339c80476e4SDavid E. O'Brien 
340c80476e4SDavid E. O'Brien static void
islogin(void)34145e5710bSMark Peek islogin(void)
342c80476e4SDavid E. O'Brien {
343c80476e4SDavid E. O'Brien     if (chkstop == 0 && setintr)
344c80476e4SDavid E. O'Brien 	panystop(0);
345c80476e4SDavid E. O'Brien     if (loginsh)
346c80476e4SDavid E. O'Brien 	return;
347c80476e4SDavid E. O'Brien     stderror(ERR_NOTLOGIN);
348c80476e4SDavid E. O'Brien }
349c80476e4SDavid E. O'Brien 
350c80476e4SDavid E. O'Brien void
doif(Char ** v,struct command * kp)35145e5710bSMark Peek doif(Char **v, struct command *kp)
352c80476e4SDavid E. O'Brien {
35323338178SMark Peek     int i;
35423338178SMark Peek     Char **vv;
355c80476e4SDavid E. O'Brien 
356c80476e4SDavid E. O'Brien     v++;
35745e5710bSMark Peek     i = noexec ? 1 : expr(&v);
358c80476e4SDavid E. O'Brien     vv = v;
359c80476e4SDavid E. O'Brien     if (*vv == NULL)
360c80476e4SDavid E. O'Brien 	stderror(ERR_NAME | ERR_EMPTYIF);
361c80476e4SDavid E. O'Brien     if (eq(*vv, STRthen)) {
362c80476e4SDavid E. O'Brien 	if (*++vv)
363c80476e4SDavid E. O'Brien 	    stderror(ERR_NAME | ERR_IMPRTHEN);
364c80476e4SDavid E. O'Brien 	setname(short2str(STRthen));
365c80476e4SDavid E. O'Brien 	/*
366c80476e4SDavid E. O'Brien 	 * If expression was zero, then scan to else , otherwise just fall into
367c80476e4SDavid E. O'Brien 	 * following code.
368c80476e4SDavid E. O'Brien 	 */
369c80476e4SDavid E. O'Brien 	if (!i)
370c80476e4SDavid E. O'Brien 	    search(TC_IF, 0, NULL);
371c80476e4SDavid E. O'Brien 	return;
372c80476e4SDavid E. O'Brien     }
373c80476e4SDavid E. O'Brien     /*
374c80476e4SDavid E. O'Brien      * Simple command attached to this if. Left shift the node in this tree,
375c80476e4SDavid E. O'Brien      * munging it so we can reexecute it.
376c80476e4SDavid E. O'Brien      */
377c80476e4SDavid E. O'Brien     if (i) {
378c80476e4SDavid E. O'Brien 	lshift(kp->t_dcom, vv - kp->t_dcom);
379c80476e4SDavid E. O'Brien 	reexecute(kp);
380c80476e4SDavid E. O'Brien 	donefds();
381c80476e4SDavid E. O'Brien     }
382c80476e4SDavid E. O'Brien }
383c80476e4SDavid E. O'Brien 
384c80476e4SDavid E. O'Brien /*
385c80476e4SDavid E. O'Brien  * Reexecute a command, being careful not
386c80476e4SDavid E. O'Brien  * to redo i/o redirection, which is already set up.
387c80476e4SDavid E. O'Brien  */
388c80476e4SDavid E. O'Brien void
reexecute(struct command * kp)38945e5710bSMark Peek reexecute(struct command *kp)
390c80476e4SDavid E. O'Brien {
391c80476e4SDavid E. O'Brien     kp->t_dflg &= F_SAVE;
392c80476e4SDavid E. O'Brien     kp->t_dflg |= F_REPEAT;
393c80476e4SDavid E. O'Brien     /*
394c80476e4SDavid E. O'Brien      * If tty is still ours to arbitrate, arbitrate it; otherwise dont even set
395c80476e4SDavid E. O'Brien      * pgrp's as the jobs would then have no way to get the tty (we can't give
396c80476e4SDavid E. O'Brien      * it to them, and our parent wouldn't know their pgrp, etc.
397c80476e4SDavid E. O'Brien      */
39829301572SMark Peek     execute(kp, (tpgrp > 0 ? tpgrp : -1), NULL, NULL, TRUE);
399c80476e4SDavid E. O'Brien }
400c80476e4SDavid E. O'Brien 
401c80476e4SDavid E. O'Brien /*ARGSUSED*/
402c80476e4SDavid E. O'Brien void
doelse(Char ** v,struct command * c)40345e5710bSMark Peek doelse (Char **v, struct command *c)
404c80476e4SDavid E. O'Brien {
405c80476e4SDavid E. O'Brien     USE(c);
406c80476e4SDavid E. O'Brien     USE(v);
40745e5710bSMark Peek     if (!noexec)
408c80476e4SDavid E. O'Brien 	search(TC_ELSE, 0, NULL);
409c80476e4SDavid E. O'Brien }
410c80476e4SDavid E. O'Brien 
411c80476e4SDavid E. O'Brien /*ARGSUSED*/
412c80476e4SDavid E. O'Brien void
dogoto(Char ** v,struct command * c)41345e5710bSMark Peek dogoto(Char **v, struct command *c)
414c80476e4SDavid E. O'Brien {
415c80476e4SDavid E. O'Brien     Char   *lp;
416c80476e4SDavid E. O'Brien 
417c80476e4SDavid E. O'Brien     USE(c);
41845e5710bSMark Peek     lp = globone(v[1], G_ERROR);
41945e5710bSMark Peek     cleanup_push(lp, xfree);
42045e5710bSMark Peek     if (!noexec)
42145e5710bSMark Peek 	gotolab(lp);
42245e5710bSMark Peek     cleanup_until(lp);
423c80476e4SDavid E. O'Brien }
424c80476e4SDavid E. O'Brien 
425c80476e4SDavid E. O'Brien void
gotolab(Char * lab)42645e5710bSMark Peek gotolab(Char *lab)
427c80476e4SDavid E. O'Brien {
42823338178SMark Peek     struct whyle *wp;
429c80476e4SDavid E. O'Brien     /*
430c80476e4SDavid E. O'Brien      * While we still can, locate any unknown ends of existing loops. This
431c80476e4SDavid E. O'Brien      * obscure code is the WORST result of the fact that we don't really parse.
432c80476e4SDavid E. O'Brien      */
433c80476e4SDavid E. O'Brien     zlast = TC_GOTO;
434c80476e4SDavid E. O'Brien     for (wp = whyles; wp; wp = wp->w_next)
4353b6eaa7bSAndrey A. Chernov 	if (wp->w_end.type == TCSH_F_SEEK && wp->w_end.f_seek == 0) {
436c80476e4SDavid E. O'Brien 	    search(TC_BREAK, 0, NULL);
437c80476e4SDavid E. O'Brien 	    btell(&wp->w_end);
438c80476e4SDavid E. O'Brien 	}
439c80476e4SDavid E. O'Brien 	else {
440c80476e4SDavid E. O'Brien 	    bseek(&wp->w_end);
441c80476e4SDavid E. O'Brien 	}
442c80476e4SDavid E. O'Brien     search(TC_GOTO, 0, lab);
443c80476e4SDavid E. O'Brien     /*
444c80476e4SDavid E. O'Brien      * Eliminate loops which were exited.
445c80476e4SDavid E. O'Brien      */
446c80476e4SDavid E. O'Brien     wfree();
447c80476e4SDavid E. O'Brien }
448c80476e4SDavid E. O'Brien 
449c80476e4SDavid E. O'Brien /*ARGSUSED*/
450c80476e4SDavid E. O'Brien void
doswitch(Char ** v,struct command * c)45145e5710bSMark Peek doswitch(Char **v, struct command *c)
452c80476e4SDavid E. O'Brien {
45323338178SMark Peek     Char *cp, *lp;
454c80476e4SDavid E. O'Brien 
455c80476e4SDavid E. O'Brien     USE(c);
456c80476e4SDavid E. O'Brien     v++;
457c80476e4SDavid E. O'Brien     if (!*v || *(*v++) != '(')
458c80476e4SDavid E. O'Brien 	stderror(ERR_SYNTAX);
459c80476e4SDavid E. O'Brien     cp = **v == ')' ? STRNULL : *v++;
460c80476e4SDavid E. O'Brien     if (*(*v++) != ')')
461c80476e4SDavid E. O'Brien 	v--;
462c80476e4SDavid E. O'Brien     if (*v)
463c80476e4SDavid E. O'Brien 	stderror(ERR_SYNTAX);
46445e5710bSMark Peek     lp = globone(cp, G_ERROR);
46545e5710bSMark Peek     cleanup_push(lp, xfree);
46645e5710bSMark Peek     if (!noexec)
46745e5710bSMark Peek 	search(TC_SWITCH, 0, lp);
46845e5710bSMark Peek     cleanup_until(lp);
469c80476e4SDavid E. O'Brien }
470c80476e4SDavid E. O'Brien 
471c80476e4SDavid E. O'Brien /*ARGSUSED*/
472c80476e4SDavid E. O'Brien void
dobreak(Char ** v,struct command * c)47345e5710bSMark Peek dobreak(Char **v, struct command *c)
474c80476e4SDavid E. O'Brien {
475c80476e4SDavid E. O'Brien     USE(v);
476c80476e4SDavid E. O'Brien     USE(c);
47745e5710bSMark Peek     if (whyles == NULL)
478c80476e4SDavid E. O'Brien 	stderror(ERR_NAME | ERR_NOTWHILE);
47945e5710bSMark Peek     if (!noexec)
48045e5710bSMark Peek 	toend();
481c80476e4SDavid E. O'Brien }
482c80476e4SDavid E. O'Brien 
483c80476e4SDavid E. O'Brien /*ARGSUSED*/
484c80476e4SDavid E. O'Brien void
doexit(Char ** v,struct command * c)48545e5710bSMark Peek doexit(Char **v, struct command *c)
486c80476e4SDavid E. O'Brien {
487c80476e4SDavid E. O'Brien     USE(c);
488c80476e4SDavid E. O'Brien 
489c80476e4SDavid E. O'Brien     if (chkstop == 0 && (intty || intact) && evalvec == 0)
490c80476e4SDavid E. O'Brien 	panystop(0);
491c80476e4SDavid E. O'Brien     /*
492c80476e4SDavid E. O'Brien      * Don't DEMAND parentheses here either.
493c80476e4SDavid E. O'Brien      */
494c80476e4SDavid E. O'Brien     v++;
495c80476e4SDavid E. O'Brien     if (*v) {
49645e5710bSMark Peek 	setv(STRstatus, putn(expr(&v)), VAR_READWRITE);
497c80476e4SDavid E. O'Brien 	if (*v)
498c80476e4SDavid E. O'Brien 	    stderror(ERR_NAME | ERR_EXPRESSION);
499c80476e4SDavid E. O'Brien     }
500c80476e4SDavid E. O'Brien     btoeof();
501c80476e4SDavid E. O'Brien #if 0
502c80476e4SDavid E. O'Brien     if (intty)
503c80476e4SDavid E. O'Brien #endif
504c80476e4SDavid E. O'Brien     /* Always close, why only on ttys? */
50545e5710bSMark Peek 	xclose(SHIN);
506c80476e4SDavid E. O'Brien }
507c80476e4SDavid E. O'Brien 
508c80476e4SDavid E. O'Brien /*ARGSUSED*/
509c80476e4SDavid E. O'Brien void
doforeach(Char ** v,struct command * c)51045e5710bSMark Peek doforeach(Char **v, struct command *c)
511c80476e4SDavid E. O'Brien {
51223338178SMark Peek     Char *cp, *sp;
51323338178SMark Peek     struct whyle *nwp;
51445e5710bSMark Peek     int gflag;
515c80476e4SDavid E. O'Brien 
516c80476e4SDavid E. O'Brien     USE(c);
517c80476e4SDavid E. O'Brien     v++;
5189ccc37e3SMark Peek     cp = sp = strip(*v);
5199ccc37e3SMark Peek     if (!letter(*cp))
520c80476e4SDavid E. O'Brien 	stderror(ERR_NAME | ERR_VARBEGIN);
5219ccc37e3SMark Peek     do {
522c80476e4SDavid E. O'Brien 	cp++;
5239ccc37e3SMark Peek     } while (alnum(*cp));
5249ccc37e3SMark Peek     if (*cp != '\0')
525c80476e4SDavid E. O'Brien 	stderror(ERR_NAME | ERR_VARALNUM);
526c80476e4SDavid E. O'Brien     cp = *v++;
527c80476e4SDavid E. O'Brien     if (v[0][0] != '(' || v[blklen(v) - 1][0] != ')')
528c80476e4SDavid E. O'Brien 	stderror(ERR_NAME | ERR_NOPAREN);
529c80476e4SDavid E. O'Brien     v++;
53045e5710bSMark Peek     gflag = tglob(v);
531c80476e4SDavid E. O'Brien     if (gflag) {
53245e5710bSMark Peek 	v = globall(v, gflag);
53345e5710bSMark Peek 	if (v == 0 && !noexec)
534c80476e4SDavid E. O'Brien 	    stderror(ERR_NAME | ERR_NOMATCH);
535c80476e4SDavid E. O'Brien     }
536c80476e4SDavid E. O'Brien     else {
53745e5710bSMark Peek 	v = saveblk(v);
538c80476e4SDavid E. O'Brien 	trim(v);
539c80476e4SDavid E. O'Brien     }
54045e5710bSMark Peek     nwp = xcalloc(1, sizeof *nwp);
541c80476e4SDavid E. O'Brien     nwp->w_fe = nwp->w_fe0 = v;
542c80476e4SDavid E. O'Brien     btell(&nwp->w_start);
543c80476e4SDavid E. O'Brien     nwp->w_fename = Strsave(cp);
544c80476e4SDavid E. O'Brien     nwp->w_next = whyles;
5453b6eaa7bSAndrey A. Chernov     nwp->w_end.type = TCSH_F_SEEK;
546c80476e4SDavid E. O'Brien     whyles = nwp;
547c80476e4SDavid E. O'Brien     /*
548c80476e4SDavid E. O'Brien      * Pre-read the loop so as to be more comprehensible to a terminal user.
549c80476e4SDavid E. O'Brien      */
550c80476e4SDavid E. O'Brien     zlast = TC_FOREACH;
551c80476e4SDavid E. O'Brien     if (intty)
552c80476e4SDavid E. O'Brien 	preread();
55345e5710bSMark Peek     if (!noexec)
554c80476e4SDavid E. O'Brien 	doagain();
555c80476e4SDavid E. O'Brien }
556c80476e4SDavid E. O'Brien 
557c80476e4SDavid E. O'Brien /*ARGSUSED*/
558c80476e4SDavid E. O'Brien void
dowhile(Char ** v,struct command * c)55945e5710bSMark Peek dowhile(Char **v, struct command *c)
560c80476e4SDavid E. O'Brien {
56123338178SMark Peek     int status;
56223338178SMark Peek     int again = whyles != 0 &&
563c80476e4SDavid E. O'Brien 			  SEEKEQ(&whyles->w_start, &lineloc) &&
564c80476e4SDavid E. O'Brien 			  whyles->w_fename == 0;
565c80476e4SDavid E. O'Brien 
566c80476e4SDavid E. O'Brien     USE(c);
567c80476e4SDavid E. O'Brien     v++;
568c80476e4SDavid E. O'Brien     /*
569c80476e4SDavid E. O'Brien      * Implement prereading here also, taking care not to evaluate the
570c80476e4SDavid E. O'Brien      * expression before the loop has been read up from a terminal.
571c80476e4SDavid E. O'Brien      */
57245e5710bSMark Peek     if (noexec)
57345e5710bSMark Peek 	status = 0;
57445e5710bSMark Peek     else if (intty && !again)
575c80476e4SDavid E. O'Brien 	status = !exp0(&v, 1);
576c80476e4SDavid E. O'Brien     else
577c80476e4SDavid E. O'Brien 	status = !expr(&v);
57845e5710bSMark Peek     if (*v && !noexec)
579c80476e4SDavid E. O'Brien 	stderror(ERR_NAME | ERR_EXPRESSION);
580c80476e4SDavid E. O'Brien     if (!again) {
58145e5710bSMark Peek 	struct whyle *nwp = xcalloc(1, sizeof(*nwp));
582c80476e4SDavid E. O'Brien 
583c80476e4SDavid E. O'Brien 	nwp->w_start = lineloc;
5843b6eaa7bSAndrey A. Chernov 	nwp->w_end.type = TCSH_F_SEEK;
585c80476e4SDavid E. O'Brien 	nwp->w_end.f_seek = 0;
5869ccc37e3SMark Peek 	nwp->w_end.a_seek = 0;
587c80476e4SDavid E. O'Brien 	nwp->w_next = whyles;
588c80476e4SDavid E. O'Brien 	whyles = nwp;
589c80476e4SDavid E. O'Brien 	zlast = TC_WHILE;
590c80476e4SDavid E. O'Brien 	if (intty) {
591c80476e4SDavid E. O'Brien 	    /*
592c80476e4SDavid E. O'Brien 	     * The tty preread
593c80476e4SDavid E. O'Brien 	     */
594c80476e4SDavid E. O'Brien 	    preread();
595c80476e4SDavid E. O'Brien 	    doagain();
596c80476e4SDavid E. O'Brien 	    return;
597c80476e4SDavid E. O'Brien 	}
598c80476e4SDavid E. O'Brien     }
599c80476e4SDavid E. O'Brien     if (status)
600c80476e4SDavid E. O'Brien 	/* We ain't gonna loop no more, no more! */
601c80476e4SDavid E. O'Brien 	toend();
602c80476e4SDavid E. O'Brien }
603c80476e4SDavid E. O'Brien 
604c80476e4SDavid E. O'Brien static void
preread(void)60545e5710bSMark Peek preread(void)
606c80476e4SDavid E. O'Brien {
60745e5710bSMark Peek     int old_pintr_disabled;
60845e5710bSMark Peek 
6093b6eaa7bSAndrey A. Chernov     whyles->w_end.type = TCSH_I_SEEK;
610c80476e4SDavid E. O'Brien     if (setintr)
61145e5710bSMark Peek 	pintr_push_enable(&old_pintr_disabled);
612c80476e4SDavid E. O'Brien     search(TC_BREAK, 0, NULL);		/* read the expression in */
613c80476e4SDavid E. O'Brien     if (setintr)
61445e5710bSMark Peek 	cleanup_until(&old_pintr_disabled);
615c80476e4SDavid E. O'Brien     btell(&whyles->w_end);
616c80476e4SDavid E. O'Brien }
617c80476e4SDavid E. O'Brien 
618c80476e4SDavid E. O'Brien /*ARGSUSED*/
619c80476e4SDavid E. O'Brien void
doend(Char ** v,struct command * c)62045e5710bSMark Peek doend(Char **v, struct command *c)
621c80476e4SDavid E. O'Brien {
622c80476e4SDavid E. O'Brien     USE(v);
623c80476e4SDavid E. O'Brien     USE(c);
624c80476e4SDavid E. O'Brien     if (!whyles)
625c80476e4SDavid E. O'Brien 	stderror(ERR_NAME | ERR_NOTWHILE);
626c80476e4SDavid E. O'Brien     btell(&whyles->w_end);
62745e5710bSMark Peek     if (!noexec)
628c80476e4SDavid E. O'Brien 	doagain();
629c80476e4SDavid E. O'Brien }
630c80476e4SDavid E. O'Brien 
631c80476e4SDavid E. O'Brien /*ARGSUSED*/
632c80476e4SDavid E. O'Brien void
docontin(Char ** v,struct command * c)63345e5710bSMark Peek docontin(Char **v, struct command *c)
634c80476e4SDavid E. O'Brien {
635c80476e4SDavid E. O'Brien     USE(v);
636c80476e4SDavid E. O'Brien     USE(c);
637c80476e4SDavid E. O'Brien     if (!whyles)
638c80476e4SDavid E. O'Brien 	stderror(ERR_NAME | ERR_NOTWHILE);
63945e5710bSMark Peek     if (!noexec)
640c80476e4SDavid E. O'Brien 	doagain();
641c80476e4SDavid E. O'Brien }
642c80476e4SDavid E. O'Brien 
643c80476e4SDavid E. O'Brien static void
doagain(void)64445e5710bSMark Peek doagain(void)
645c80476e4SDavid E. O'Brien {
646c80476e4SDavid E. O'Brien     /* Repeating a while is simple */
647c80476e4SDavid E. O'Brien     if (whyles->w_fename == 0) {
648c80476e4SDavid E. O'Brien 	bseek(&whyles->w_start);
649c80476e4SDavid E. O'Brien 	return;
650c80476e4SDavid E. O'Brien     }
651c80476e4SDavid E. O'Brien     /*
652c80476e4SDavid E. O'Brien      * The foreach variable list actually has a spurious word ")" at the end of
653c80476e4SDavid E. O'Brien      * the w_fe list.  Thus we are at the of the list if one word beyond this
654c80476e4SDavid E. O'Brien      * is 0.
655c80476e4SDavid E. O'Brien      */
656c80476e4SDavid E. O'Brien     if (!whyles->w_fe[1]) {
657c80476e4SDavid E. O'Brien 	dobreak(NULL, NULL);
658c80476e4SDavid E. O'Brien 	return;
659c80476e4SDavid E. O'Brien     }
66045e5710bSMark Peek     setv(whyles->w_fename, quote(Strsave(*whyles->w_fe++)), VAR_READWRITE);
661c80476e4SDavid E. O'Brien     bseek(&whyles->w_start);
662c80476e4SDavid E. O'Brien }
663c80476e4SDavid E. O'Brien 
664c80476e4SDavid E. O'Brien void
dorepeat(Char ** v,struct command * kp)66545e5710bSMark Peek dorepeat(Char **v, struct command *kp)
666c80476e4SDavid E. O'Brien {
66729301572SMark Peek     int i = 1;
668c80476e4SDavid E. O'Brien 
66929301572SMark Peek     do {
67029301572SMark Peek 	i *= getn(v[1]);
67129301572SMark Peek 	lshift(v, 2);
67229301572SMark Peek     } while (v[0] != NULL && Strcmp(v[0], STRrepeat) == 0);
67345e5710bSMark Peek     if (noexec)
67445e5710bSMark Peek 	i = 1;
67529301572SMark Peek 
67645e5710bSMark Peek     if (setintr) {
67745e5710bSMark Peek 	pintr_disabled++;
67845e5710bSMark Peek 	cleanup_push(&pintr_disabled, disabled_cleanup);
67945e5710bSMark Peek     }
680c80476e4SDavid E. O'Brien     while (i > 0) {
68145e5710bSMark Peek 	if (setintr && pintr_disabled == 1) {
68245e5710bSMark Peek 	    cleanup_until(&pintr_disabled);
68345e5710bSMark Peek 	    pintr_disabled++;
68445e5710bSMark Peek 	    cleanup_push(&pintr_disabled, disabled_cleanup);
68545e5710bSMark Peek 	}
686c80476e4SDavid E. O'Brien 	reexecute(kp);
687c80476e4SDavid E. O'Brien 	--i;
688c80476e4SDavid E. O'Brien     }
689a15e6f9aSMark Peek     if (setintr && pintr_disabled == 1)
69045e5710bSMark Peek         cleanup_until(&pintr_disabled);
691c80476e4SDavid E. O'Brien     donefds();
692c80476e4SDavid E. O'Brien }
693c80476e4SDavid E. O'Brien 
694c80476e4SDavid E. O'Brien /*ARGSUSED*/
695c80476e4SDavid E. O'Brien void
doswbrk(Char ** v,struct command * c)69645e5710bSMark Peek doswbrk(Char **v, struct command *c)
697c80476e4SDavid E. O'Brien {
698c80476e4SDavid E. O'Brien     USE(v);
699c80476e4SDavid E. O'Brien     USE(c);
70045e5710bSMark Peek     if (!noexec)
701c80476e4SDavid E. O'Brien 	search(TC_BRKSW, 0, NULL);
702c80476e4SDavid E. O'Brien }
703c80476e4SDavid E. O'Brien 
704c80476e4SDavid E. O'Brien int
srchx(Char * cp)70545e5710bSMark Peek srchx(Char *cp)
706c80476e4SDavid E. O'Brien {
707c80476e4SDavid E. O'Brien     struct srch *sp, *sp1, *sp2;
708c80476e4SDavid E. O'Brien     int i;
709c80476e4SDavid E. O'Brien 
710c80476e4SDavid E. O'Brien     /*
711c80476e4SDavid E. O'Brien      * Ignore keywords inside heredocs
712c80476e4SDavid E. O'Brien      */
713c80476e4SDavid E. O'Brien     if (inheredoc)
714c80476e4SDavid E. O'Brien 	return -1;
715c80476e4SDavid E. O'Brien 
716c80476e4SDavid E. O'Brien     /*
717c80476e4SDavid E. O'Brien      * Binary search Sp1 is the beginning of the current search range. Sp2 is
718c80476e4SDavid E. O'Brien      * one past the end.
719c80476e4SDavid E. O'Brien      */
720c80476e4SDavid E. O'Brien     for (sp1 = srchn, sp2 = srchn + nsrchn; sp1 < sp2;) {
721c80476e4SDavid E. O'Brien 	sp = sp1 + ((sp2 - sp1) >> 1);
722c80476e4SDavid E. O'Brien 	if ((i = *cp - *sp->s_name) == 0 &&
723c80476e4SDavid E. O'Brien 	    (i = Strcmp(cp, str2short(sp->s_name))) == 0)
724c80476e4SDavid E. O'Brien 	    return sp->s_value;
725c80476e4SDavid E. O'Brien 	if (i < 0)
726c80476e4SDavid E. O'Brien 	    sp2 = sp;
727c80476e4SDavid E. O'Brien 	else
728c80476e4SDavid E. O'Brien 	    sp1 = sp + 1;
729c80476e4SDavid E. O'Brien     }
730c80476e4SDavid E. O'Brien     return (-1);
731c80476e4SDavid E. O'Brien }
732c80476e4SDavid E. O'Brien 
73323338178SMark Peek static const char *
isrchx(int n)73445e5710bSMark Peek isrchx(int n)
735c80476e4SDavid E. O'Brien {
73623338178SMark Peek     struct srch *sp, *sp2;
737c80476e4SDavid E. O'Brien 
738c80476e4SDavid E. O'Brien     for (sp = srchn, sp2 = srchn + nsrchn; sp < sp2; sp++)
739c80476e4SDavid E. O'Brien 	if (sp->s_value == n)
740c80476e4SDavid E. O'Brien 	    return (sp->s_name);
741c80476e4SDavid E. O'Brien     return ("");
742c80476e4SDavid E. O'Brien }
743c80476e4SDavid E. O'Brien 
744c80476e4SDavid E. O'Brien 
74545e5710bSMark Peek static int Stype;
746c80476e4SDavid E. O'Brien static Char *Sgoal;
747c80476e4SDavid E. O'Brien 
748c80476e4SDavid E. O'Brien static void
search(int type,int level,Char * goal)74945e5710bSMark Peek search(int type, int level, Char *goal)
750c80476e4SDavid E. O'Brien {
75145e5710bSMark Peek     struct Strbuf word = Strbuf_INIT;
75223338178SMark Peek     Char *cp;
753b2d5d167SMark Peek     struct whyle *wp;
754b2d5d167SMark Peek     int wlevel = 0;
755a15e6f9aSMark Peek     struct wordent *histent = NULL, *ohistent = NULL;
756c80476e4SDavid E. O'Brien 
75745e5710bSMark Peek     Stype = type;
758c80476e4SDavid E. O'Brien     Sgoal = goal;
759c80476e4SDavid E. O'Brien     if (type == TC_GOTO) {
760c80476e4SDavid E. O'Brien 	struct Ain a;
7613b6eaa7bSAndrey A. Chernov 	a.type = TCSH_F_SEEK;
762c80476e4SDavid E. O'Brien 	a.f_seek = 0;
7639ccc37e3SMark Peek 	a.a_seek = 0;
764c80476e4SDavid E. O'Brien 	bseek(&a);
765c80476e4SDavid E. O'Brien     }
76645e5710bSMark Peek     cleanup_push(&word, Strbuf_cleanup);
767c80476e4SDavid E. O'Brien     do {
768a15e6f9aSMark Peek 
769a15e6f9aSMark Peek 	if (intty) {
770a15e6f9aSMark Peek 	    histent = xmalloc(sizeof(*histent));
771a15e6f9aSMark Peek 	    ohistent = xmalloc(sizeof(*histent));
772a15e6f9aSMark Peek 	    ohistent->word = STRNULL;
773a15e6f9aSMark Peek 	    ohistent->next = histent;
774a15e6f9aSMark Peek 	    histent->prev = ohistent;
775a15e6f9aSMark Peek 	}
776a15e6f9aSMark Peek 
7773b6eaa7bSAndrey A. Chernov 	if (intty && fseekp == feobp && aret == TCSH_F_SEEK)
778c80476e4SDavid E. O'Brien 	    printprompt(1, isrchx(type == TC_BREAK ? zlast : type));
779c80476e4SDavid E. O'Brien 	/* xprintf("? "), flush(); */
78045e5710bSMark Peek 	(void) getword(&word);
78145e5710bSMark Peek 	Strbuf_terminate(&word);
782a15e6f9aSMark Peek 
783a15e6f9aSMark Peek 	if (intty && Strlen(word.s) > 0) {
784a15e6f9aSMark Peek 	    histent->word = Strsave(word.s);
785a15e6f9aSMark Peek 	    histent->next = xmalloc(sizeof(*histent));
786a15e6f9aSMark Peek 	    histent->next->prev = histent;
787a15e6f9aSMark Peek 	    histent = histent->next;
788a15e6f9aSMark Peek 	}
789a15e6f9aSMark Peek 
79045e5710bSMark Peek 	switch (srchx(word.s)) {
791c80476e4SDavid E. O'Brien 
792c80476e4SDavid E. O'Brien 	case TC_ELSE:
793c80476e4SDavid E. O'Brien 	    if (level == 0 && type == TC_IF)
79445e5710bSMark Peek 		goto end;
795c80476e4SDavid E. O'Brien 	    break;
796c80476e4SDavid E. O'Brien 
797c80476e4SDavid E. O'Brien 	case TC_IF:
79819d2e3deSDmitry Chagin 	    while (getword(&word)) {
79919d2e3deSDmitry Chagin 		if (intty) {
80019d2e3deSDmitry Chagin 		    histent->word = Strsave(word.s);
80119d2e3deSDmitry Chagin 		    histent->next = xmalloc(sizeof(*histent));
80219d2e3deSDmitry Chagin 		    histent->next->prev = histent;
80319d2e3deSDmitry Chagin 		    histent = histent->next;
80419d2e3deSDmitry Chagin 		}
805c80476e4SDavid E. O'Brien 		continue;
80619d2e3deSDmitry Chagin 	    }
80719d2e3deSDmitry Chagin 
808c80476e4SDavid E. O'Brien 	    if ((type == TC_IF || type == TC_ELSE) &&
80945e5710bSMark Peek 		eq(word.s, STRthen))
810c80476e4SDavid E. O'Brien 		level++;
811c80476e4SDavid E. O'Brien 	    break;
812c80476e4SDavid E. O'Brien 
813c80476e4SDavid E. O'Brien 	case TC_ENDIF:
814c80476e4SDavid E. O'Brien 	    if (type == TC_IF || type == TC_ELSE)
815c80476e4SDavid E. O'Brien 		level--;
816c80476e4SDavid E. O'Brien 	    break;
817c80476e4SDavid E. O'Brien 
818c80476e4SDavid E. O'Brien 	case TC_FOREACH:
819c80476e4SDavid E. O'Brien 	case TC_WHILE:
820b2d5d167SMark Peek 	    wlevel++;
821c80476e4SDavid E. O'Brien 	    if (type == TC_BREAK)
822c80476e4SDavid E. O'Brien 		level++;
823c80476e4SDavid E. O'Brien 	    break;
824c80476e4SDavid E. O'Brien 
825c80476e4SDavid E. O'Brien 	case TC_END:
826b2d5d167SMark Peek 	    if (type == TC_BRKSW) {
827b2d5d167SMark Peek 		if (wlevel == 0) {
828b2d5d167SMark Peek 		    wp = whyles;
829b2d5d167SMark Peek 		    if (wp) {
830b2d5d167SMark Peek 			    whyles = wp->w_next;
831b2d5d167SMark Peek 			    wpfree(wp);
832b2d5d167SMark Peek 		    }
833b2d5d167SMark Peek 		}
834b2d5d167SMark Peek 	    }
835c80476e4SDavid E. O'Brien 	    if (type == TC_BREAK)
836c80476e4SDavid E. O'Brien 		level--;
837b2d5d167SMark Peek 	    wlevel--;
838c80476e4SDavid E. O'Brien 	    break;
839c80476e4SDavid E. O'Brien 
840c80476e4SDavid E. O'Brien 	case TC_SWITCH:
841c80476e4SDavid E. O'Brien 	    if (type == TC_SWITCH || type == TC_BRKSW)
842c80476e4SDavid E. O'Brien 		level++;
843c80476e4SDavid E. O'Brien 	    break;
844c80476e4SDavid E. O'Brien 
845c80476e4SDavid E. O'Brien 	case TC_ENDSW:
846c80476e4SDavid E. O'Brien 	    if (type == TC_SWITCH || type == TC_BRKSW)
847c80476e4SDavid E. O'Brien 		level--;
848c80476e4SDavid E. O'Brien 	    break;
849c80476e4SDavid E. O'Brien 
850c80476e4SDavid E. O'Brien 	case TC_LABEL:
85145e5710bSMark Peek 	    if (type == TC_GOTO && getword(&word) && eq(word.s, goal))
852c80476e4SDavid E. O'Brien 		level = -1;
853c80476e4SDavid E. O'Brien 	    break;
854c80476e4SDavid E. O'Brien 
855c80476e4SDavid E. O'Brien 	default:
856c80476e4SDavid E. O'Brien 	    if (type != TC_GOTO && (type != TC_SWITCH || level != 0))
857c80476e4SDavid E. O'Brien 		break;
85845e5710bSMark Peek 	    if (word.len == 0 || word.s[word.len - 1] != ':')
859c80476e4SDavid E. O'Brien 		break;
86045e5710bSMark Peek 	    word.s[--word.len] = 0;
86145e5710bSMark Peek 	    if ((type == TC_GOTO && eq(word.s, goal)) ||
86245e5710bSMark Peek 		(type == TC_SWITCH && eq(word.s, STRdefault)))
863c80476e4SDavid E. O'Brien 		level = -1;
864c80476e4SDavid E. O'Brien 	    break;
865c80476e4SDavid E. O'Brien 
866c80476e4SDavid E. O'Brien 	case TC_CASE:
867c80476e4SDavid E. O'Brien 	    if (type != TC_SWITCH || level != 0)
868c80476e4SDavid E. O'Brien 		break;
86945e5710bSMark Peek 	    (void) getword(&word);
87045e5710bSMark Peek 	    if (word.len != 0 && word.s[word.len - 1] == ':')
87145e5710bSMark Peek 		word.s[--word.len] = 0;
87245e5710bSMark Peek 	    cp = strip(Dfix1(word.s));
87345e5710bSMark Peek 	    cleanup_push(cp, xfree);
874c80476e4SDavid E. O'Brien 	    if (Gmatch(goal, cp))
875c80476e4SDavid E. O'Brien 		level = -1;
87645e5710bSMark Peek 	    cleanup_until(cp);
877c80476e4SDavid E. O'Brien 	    break;
878c80476e4SDavid E. O'Brien 
879c80476e4SDavid E. O'Brien 	case TC_DEFAULT:
880c80476e4SDavid E. O'Brien 	    if (type == TC_SWITCH && level == 0)
881c80476e4SDavid E. O'Brien 		level = -1;
882c80476e4SDavid E. O'Brien 	    break;
883c80476e4SDavid E. O'Brien 	}
884a15e6f9aSMark Peek 	if (intty) {
885a15e6f9aSMark Peek 	    ohistent->prev = histgetword(histent);
886a15e6f9aSMark Peek 	    ohistent->prev->next = ohistent;
887a15e6f9aSMark Peek 	    savehist(ohistent, 0);
888a15e6f9aSMark Peek 	    freelex(ohistent);
889a15e6f9aSMark Peek 	    xfree(ohistent);
890a15e6f9aSMark Peek 	} else
891c80476e4SDavid E. O'Brien 	    (void) getword(NULL);
892c80476e4SDavid E. O'Brien     } while (level >= 0);
89345e5710bSMark Peek  end:
89445e5710bSMark Peek     cleanup_until(&word);
895c80476e4SDavid E. O'Brien }
896c80476e4SDavid E. O'Brien 
897a15e6f9aSMark Peek static struct wordent *
histgetword(struct wordent * histent)898a15e6f9aSMark Peek histgetword(struct wordent *histent)
899a15e6f9aSMark Peek {
90019d2e3deSDmitry Chagin     int first;
901a15e6f9aSMark Peek     eChar c, d;
902a15e6f9aSMark Peek     int e;
903a15e6f9aSMark Peek     struct Strbuf *tmp;
904a15e6f9aSMark Peek     tmp = xmalloc(sizeof(*tmp));
905a15e6f9aSMark Peek     tmp->size = 0;
906a15e6f9aSMark Peek     tmp->s = NULL;
907a15e6f9aSMark Peek     c = readc(1);
908a15e6f9aSMark Peek     d = 0;
909a15e6f9aSMark Peek     e = 0;
910a15e6f9aSMark Peek     for (;;) {
911a15e6f9aSMark Peek 	tmp->len = 0;
912a15e6f9aSMark Peek 	Strbuf_terminate (tmp);
913a15e6f9aSMark Peek 	while (c == ' ' || c == '\t')
914a15e6f9aSMark Peek 	    c = readc(1);
915a15e6f9aSMark Peek 	if (c == '#')
916a15e6f9aSMark Peek 	    do
917a15e6f9aSMark Peek 		c = readc(1);
918a15e6f9aSMark Peek 	    while (c != CHAR_ERR && c != '\n');
919a15e6f9aSMark Peek 	if (c == CHAR_ERR)
920a15e6f9aSMark Peek 	    goto past;
921a15e6f9aSMark Peek 	if (c == '\n')
922a15e6f9aSMark Peek 	    goto nl;
923a15e6f9aSMark Peek 	unreadc(c);
924a15e6f9aSMark Peek 	first = 1;
925a15e6f9aSMark Peek 	do {
926a15e6f9aSMark Peek 	    e = (c == '\\');
927a15e6f9aSMark Peek 	    c = readc(1);
928a15e6f9aSMark Peek 	    if (c == '\\' && !e) {
929a15e6f9aSMark Peek 		if ((c = readc(1)) == '\n') {
930a15e6f9aSMark Peek 		    e = 1;
931a15e6f9aSMark Peek 		    c = ' ';
932a15e6f9aSMark Peek 		} else {
933a15e6f9aSMark Peek 		    unreadc(c);
934a15e6f9aSMark Peek 		    c = '\\';
935a15e6f9aSMark Peek 		}
936a15e6f9aSMark Peek 	    }
937a15e6f9aSMark Peek 	    if ((c == '\'' || c == '"') && !e) {
938a15e6f9aSMark Peek 		if (d == 0)
939a15e6f9aSMark Peek 		    d = c;
940a15e6f9aSMark Peek 		else if (d == c)
941a15e6f9aSMark Peek 		    d = 0;
942a15e6f9aSMark Peek 	    }
943a15e6f9aSMark Peek 	    if (c == CHAR_ERR)
944a15e6f9aSMark Peek 		goto past;
945a15e6f9aSMark Peek 
946a15e6f9aSMark Peek 	    Strbuf_append1(tmp, (Char) c);
947a15e6f9aSMark Peek 
948a15e6f9aSMark Peek 	    if (!first && !d && c == '(' && !e) {
949a15e6f9aSMark Peek 		break;
950a15e6f9aSMark Peek 	    }
951a15e6f9aSMark Peek 	    first = 0;
952a15e6f9aSMark Peek 	} while (d || e || (c != ' ' && c != '\t' && c != '\n'));
953a15e6f9aSMark Peek 	tmp->len--;
954a15e6f9aSMark Peek 	if (tmp->len) {
955a15e6f9aSMark Peek 	    Strbuf_terminate(tmp);
956a15e6f9aSMark Peek 	    histent->word = Strsave(tmp->s);
957a15e6f9aSMark Peek 	    histent->next = xmalloc(sizeof (*histent));
958a15e6f9aSMark Peek 	    histent->next->prev = histent;
959a15e6f9aSMark Peek 	    histent = histent->next;
960a15e6f9aSMark Peek 	}
961a15e6f9aSMark Peek 	if (c == '\n') {
962a15e6f9aSMark Peek 	nl:
963a15e6f9aSMark Peek 	    tmp->len = 0;
964a15e6f9aSMark Peek 	    Strbuf_append1(tmp, (Char) c);
965a15e6f9aSMark Peek 	    Strbuf_terminate(tmp);
966a15e6f9aSMark Peek 	    histent->word = Strsave(tmp->s);
967a15e6f9aSMark Peek 	    return histent;
968a15e6f9aSMark Peek 	}
969a15e6f9aSMark Peek     }
970a15e6f9aSMark Peek 
971a15e6f9aSMark Peek past:
972a15e6f9aSMark Peek     switch (Stype) {
973a15e6f9aSMark Peek 
974a15e6f9aSMark Peek     case TC_IF:
975a15e6f9aSMark Peek 	stderror(ERR_NAME | ERR_NOTFOUND, "then/endif");
976a15e6f9aSMark Peek 	break;
977a15e6f9aSMark Peek 
978a15e6f9aSMark Peek     case TC_ELSE:
979a15e6f9aSMark Peek 	stderror(ERR_NAME | ERR_NOTFOUND, "endif");
980a15e6f9aSMark Peek 	break;
981a15e6f9aSMark Peek 
982a15e6f9aSMark Peek     case TC_BRKSW:
983a15e6f9aSMark Peek     case TC_SWITCH:
984a15e6f9aSMark Peek 	stderror(ERR_NAME | ERR_NOTFOUND, "endsw");
985a15e6f9aSMark Peek 	break;
986a15e6f9aSMark Peek 
987a15e6f9aSMark Peek     case TC_BREAK:
988a15e6f9aSMark Peek 	stderror(ERR_NAME | ERR_NOTFOUND, "end");
989a15e6f9aSMark Peek 	break;
990a15e6f9aSMark Peek 
991a15e6f9aSMark Peek     case TC_GOTO:
992a15e6f9aSMark Peek 	setname(short2str(Sgoal));
993a15e6f9aSMark Peek 	stderror(ERR_NAME | ERR_NOTFOUND, "label");
994a15e6f9aSMark Peek 	break;
995a15e6f9aSMark Peek 
996a15e6f9aSMark Peek     default:
997a15e6f9aSMark Peek 	break;
998a15e6f9aSMark Peek     }
999a15e6f9aSMark Peek     /* NOTREACHED */
1000a15e6f9aSMark Peek     return NULL;
1001a15e6f9aSMark Peek }
1002a15e6f9aSMark Peek 
1003c80476e4SDavid E. O'Brien static int
getword(struct Strbuf * wp)100445e5710bSMark Peek getword(struct Strbuf *wp)
1005c80476e4SDavid E. O'Brien {
1006c80476e4SDavid E. O'Brien     int found = 0, first;
100723338178SMark Peek     eChar c, d;
1008c80476e4SDavid E. O'Brien 
100945e5710bSMark Peek     if (wp)
101045e5710bSMark Peek 	wp->len = 0;
1011c80476e4SDavid E. O'Brien     c = readc(1);
1012c80476e4SDavid E. O'Brien     d = 0;
1013c80476e4SDavid E. O'Brien     do {
1014c80476e4SDavid E. O'Brien 	while (c == ' ' || c == '\t')
1015c80476e4SDavid E. O'Brien 	    c = readc(1);
1016c80476e4SDavid E. O'Brien 	if (c == '#')
1017c80476e4SDavid E. O'Brien 	    do
1018c80476e4SDavid E. O'Brien 		c = readc(1);
101923338178SMark Peek 	    while (c != CHAR_ERR && c != '\n');
102023338178SMark Peek 	if (c == CHAR_ERR)
1021c80476e4SDavid E. O'Brien 	    goto past;
1022c80476e4SDavid E. O'Brien 	if (c == '\n') {
1023c80476e4SDavid E. O'Brien 	    if (wp)
1024c80476e4SDavid E. O'Brien 		break;
1025c80476e4SDavid E. O'Brien 	    return (0);
1026c80476e4SDavid E. O'Brien 	}
1027c80476e4SDavid E. O'Brien 	unreadc(c);
1028c80476e4SDavid E. O'Brien 	found = 1;
1029c80476e4SDavid E. O'Brien 	first = 1;
1030c80476e4SDavid E. O'Brien 	do {
1031c80476e4SDavid E. O'Brien 	    c = readc(1);
1032c80476e4SDavid E. O'Brien 	    if (c == '\\' && (c = readc(1)) == '\n')
1033c80476e4SDavid E. O'Brien 		c = ' ';
1034c80476e4SDavid E. O'Brien 	    if (c == '\'' || c == '"') {
1035c80476e4SDavid E. O'Brien 		if (d == 0)
1036c80476e4SDavid E. O'Brien 		    d = c;
1037c80476e4SDavid E. O'Brien 		else if (d == c)
1038c80476e4SDavid E. O'Brien 		    d = 0;
1039c80476e4SDavid E. O'Brien 	    }
104023338178SMark Peek 	    if (c == CHAR_ERR)
1041c80476e4SDavid E. O'Brien 		goto past;
104245e5710bSMark Peek 	    if (wp)
104345e5710bSMark Peek 		Strbuf_append1(wp, (Char) c);
104419d2e3deSDmitry Chagin 	    if (!d && c == ')') {
104519d2e3deSDmitry Chagin 		if (!first && wp) {
104619d2e3deSDmitry Chagin 		    goto past_word_end;
104719d2e3deSDmitry Chagin 		} else {
104819d2e3deSDmitry Chagin 		    if (wp) {
104919d2e3deSDmitry Chagin 			wp->len = 1;
105019d2e3deSDmitry Chagin 			Strbuf_terminate(wp);
105119d2e3deSDmitry Chagin 		    }
105219d2e3deSDmitry Chagin 		    return found;
105319d2e3deSDmitry Chagin 		}
105419d2e3deSDmitry Chagin 	    }
1055c80476e4SDavid E. O'Brien 	    if (!first && !d && c == '(') {
105645e5710bSMark Peek 		if (wp)
105745e5710bSMark Peek 		    goto past_word_end;
1058c80476e4SDavid E. O'Brien 		else
1059c80476e4SDavid E. O'Brien 		    break;
1060c80476e4SDavid E. O'Brien 	    }
1061c80476e4SDavid E. O'Brien 	    first = 0;
1062c80476e4SDavid E. O'Brien 	} while ((d || (c != ' ' && c != '\t')) && c != '\n');
1063c80476e4SDavid E. O'Brien     } while (wp == 0);
1064c80476e4SDavid E. O'Brien 
106545e5710bSMark Peek  past_word_end:
1066c80476e4SDavid E. O'Brien     unreadc(c);
106745e5710bSMark Peek     if (found) {
106845e5710bSMark Peek 	wp->len--;
106945e5710bSMark Peek 	Strbuf_terminate(wp);
107045e5710bSMark Peek     }
1071c80476e4SDavid E. O'Brien 
1072c80476e4SDavid E. O'Brien     return (found);
1073c80476e4SDavid E. O'Brien 
1074c80476e4SDavid E. O'Brien past:
1075c80476e4SDavid E. O'Brien     switch (Stype) {
1076c80476e4SDavid E. O'Brien 
1077c80476e4SDavid E. O'Brien     case TC_IF:
1078c80476e4SDavid E. O'Brien 	stderror(ERR_NAME | ERR_NOTFOUND, "then/endif");
1079c80476e4SDavid E. O'Brien 	break;
1080c80476e4SDavid E. O'Brien 
1081c80476e4SDavid E. O'Brien     case TC_ELSE:
1082c80476e4SDavid E. O'Brien 	stderror(ERR_NAME | ERR_NOTFOUND, "endif");
1083c80476e4SDavid E. O'Brien 	break;
1084c80476e4SDavid E. O'Brien 
1085c80476e4SDavid E. O'Brien     case TC_BRKSW:
1086c80476e4SDavid E. O'Brien     case TC_SWITCH:
1087c80476e4SDavid E. O'Brien 	stderror(ERR_NAME | ERR_NOTFOUND, "endsw");
1088c80476e4SDavid E. O'Brien 	break;
1089c80476e4SDavid E. O'Brien 
1090c80476e4SDavid E. O'Brien     case TC_BREAK:
1091c80476e4SDavid E. O'Brien 	stderror(ERR_NAME | ERR_NOTFOUND, "end");
1092c80476e4SDavid E. O'Brien 	break;
1093c80476e4SDavid E. O'Brien 
1094c80476e4SDavid E. O'Brien     case TC_GOTO:
1095c80476e4SDavid E. O'Brien 	setname(short2str(Sgoal));
1096c80476e4SDavid E. O'Brien 	stderror(ERR_NAME | ERR_NOTFOUND, "label");
1097c80476e4SDavid E. O'Brien 	break;
1098c80476e4SDavid E. O'Brien 
1099c80476e4SDavid E. O'Brien     default:
1100c80476e4SDavid E. O'Brien 	break;
1101c80476e4SDavid E. O'Brien     }
1102c80476e4SDavid E. O'Brien     /* NOTREACHED */
1103c80476e4SDavid E. O'Brien     return (0);
1104c80476e4SDavid E. O'Brien }
1105c80476e4SDavid E. O'Brien 
1106c80476e4SDavid E. O'Brien static void
toend(void)110745e5710bSMark Peek toend(void)
1108c80476e4SDavid E. O'Brien {
11093b6eaa7bSAndrey A. Chernov     if (whyles->w_end.type == TCSH_F_SEEK && whyles->w_end.f_seek == 0) {
1110c80476e4SDavid E. O'Brien 	search(TC_BREAK, 0, NULL);
1111c80476e4SDavid E. O'Brien 	btell(&whyles->w_end);
1112c80476e4SDavid E. O'Brien 	whyles->w_end.f_seek--;
1113c80476e4SDavid E. O'Brien     }
1114c80476e4SDavid E. O'Brien     else {
1115c80476e4SDavid E. O'Brien 	bseek(&whyles->w_end);
1116c80476e4SDavid E. O'Brien     }
1117c80476e4SDavid E. O'Brien     wfree();
1118c80476e4SDavid E. O'Brien }
1119c80476e4SDavid E. O'Brien 
1120b2d5d167SMark Peek static void
wpfree(struct whyle * wp)112145e5710bSMark Peek wpfree(struct whyle *wp)
1122b2d5d167SMark Peek {
1123b2d5d167SMark Peek 	if (wp->w_fe0)
1124b2d5d167SMark Peek 	    blkfree(wp->w_fe0);
112545e5710bSMark Peek 	xfree(wp->w_fename);
112645e5710bSMark Peek 	xfree(wp);
1127b2d5d167SMark Peek }
1128b2d5d167SMark Peek 
1129c80476e4SDavid E. O'Brien void
wfree(void)113045e5710bSMark Peek wfree(void)
1131c80476e4SDavid E. O'Brien {
1132c80476e4SDavid E. O'Brien     struct Ain    o;
1133c80476e4SDavid E. O'Brien     struct whyle *nwp;
1134c80476e4SDavid E. O'Brien #ifdef lint
1135c80476e4SDavid E. O'Brien     nwp = NULL;	/* sun lint is dumb! */
1136c80476e4SDavid E. O'Brien #endif
1137c80476e4SDavid E. O'Brien 
1138c80476e4SDavid E. O'Brien #ifdef FDEBUG
113945e5710bSMark Peek     static const char foo[] = "IAFE";
1140c80476e4SDavid E. O'Brien #endif /* FDEBUG */
1141c80476e4SDavid E. O'Brien 
1142c80476e4SDavid E. O'Brien     btell(&o);
1143c80476e4SDavid E. O'Brien 
1144c80476e4SDavid E. O'Brien #ifdef FDEBUG
1145c80476e4SDavid E. O'Brien     xprintf("o->type %c o->a_seek %d o->f_seek %d\n",
1146c80476e4SDavid E. O'Brien 	    foo[o.type + 1], o.a_seek, o.f_seek);
1147c80476e4SDavid E. O'Brien #endif /* FDEBUG */
1148c80476e4SDavid E. O'Brien 
1149c80476e4SDavid E. O'Brien     for (; whyles; whyles = nwp) {
115023338178SMark Peek 	struct whyle *wp = whyles;
1151c80476e4SDavid E. O'Brien 	nwp = wp->w_next;
1152c80476e4SDavid E. O'Brien 
1153c80476e4SDavid E. O'Brien #ifdef FDEBUG
1154c80476e4SDavid E. O'Brien 	xprintf("start->type %c start->a_seek %d start->f_seek %d\n",
1155c80476e4SDavid E. O'Brien 		foo[wp->w_start.type+1],
1156c80476e4SDavid E. O'Brien 		wp->w_start.a_seek, wp->w_start.f_seek);
1157c80476e4SDavid E. O'Brien 	xprintf("end->type %c end->a_seek %d end->f_seek %d\n",
1158c80476e4SDavid E. O'Brien 		foo[wp->w_end.type + 1], wp->w_end.a_seek, wp->w_end.f_seek);
1159c80476e4SDavid E. O'Brien #endif /* FDEBUG */
1160c80476e4SDavid E. O'Brien 
1161c80476e4SDavid E. O'Brien 	/*
1162c80476e4SDavid E. O'Brien 	 * XXX: We free loops that have different seek types.
1163c80476e4SDavid E. O'Brien 	 */
11643b6eaa7bSAndrey A. Chernov 	if (wp->w_end.type != TCSH_I_SEEK && wp->w_start.type == wp->w_end.type &&
1165c80476e4SDavid E. O'Brien 	    wp->w_start.type == o.type) {
11663b6eaa7bSAndrey A. Chernov 	    if (wp->w_end.type == TCSH_F_SEEK) {
1167c80476e4SDavid E. O'Brien 		if (o.f_seek >= wp->w_start.f_seek &&
1168c80476e4SDavid E. O'Brien 		    (wp->w_end.f_seek == 0 || o.f_seek < wp->w_end.f_seek))
1169c80476e4SDavid E. O'Brien 		    break;
1170c80476e4SDavid E. O'Brien 	    }
1171c80476e4SDavid E. O'Brien 	    else {
1172c80476e4SDavid E. O'Brien 		if (o.a_seek >= wp->w_start.a_seek &&
1173c80476e4SDavid E. O'Brien 		    (wp->w_end.a_seek == 0 || o.a_seek < wp->w_end.a_seek))
1174c80476e4SDavid E. O'Brien 		    break;
1175c80476e4SDavid E. O'Brien 	    }
1176c80476e4SDavid E. O'Brien 	}
1177c80476e4SDavid E. O'Brien 
1178b2d5d167SMark Peek 	wpfree(wp);
1179c80476e4SDavid E. O'Brien     }
1180c80476e4SDavid E. O'Brien }
1181c80476e4SDavid E. O'Brien 
1182c80476e4SDavid E. O'Brien /*ARGSUSED*/
1183c80476e4SDavid E. O'Brien void
doecho(Char ** v,struct command * c)118445e5710bSMark Peek doecho(Char **v, struct command *c)
1185c80476e4SDavid E. O'Brien {
1186c80476e4SDavid E. O'Brien     USE(c);
1187c80476e4SDavid E. O'Brien     xecho(' ', v);
1188c80476e4SDavid E. O'Brien }
1189c80476e4SDavid E. O'Brien 
1190c80476e4SDavid E. O'Brien /*ARGSUSED*/
1191c80476e4SDavid E. O'Brien void
doglob(Char ** v,struct command * c)119245e5710bSMark Peek doglob(Char **v, struct command *c)
1193c80476e4SDavid E. O'Brien {
1194c80476e4SDavid E. O'Brien     USE(c);
1195c80476e4SDavid E. O'Brien     xecho(0, v);
1196c80476e4SDavid E. O'Brien     flush();
1197c80476e4SDavid E. O'Brien }
1198c80476e4SDavid E. O'Brien 
1199c80476e4SDavid E. O'Brien static void
xecho(int sep,Char ** v)120045e5710bSMark Peek xecho(int sep, Char **v)
1201c80476e4SDavid E. O'Brien {
120245e5710bSMark Peek     Char *cp, **globbed = NULL;
1203c80476e4SDavid E. O'Brien     int     nonl = 0;
1204c80476e4SDavid E. O'Brien     int	    echo_style = ECHO_STYLE;
1205c80476e4SDavid E. O'Brien     struct varent *vp;
1206c80476e4SDavid E. O'Brien 
1207c80476e4SDavid E. O'Brien     if ((vp = adrof(STRecho_style)) != NULL && vp->vec != NULL &&
1208c80476e4SDavid E. O'Brien 	vp->vec[0] != NULL) {
1209c80476e4SDavid E. O'Brien 	if (Strcmp(vp->vec[0], STRbsd) == 0)
1210c80476e4SDavid E. O'Brien 	    echo_style = BSD_ECHO;
1211c80476e4SDavid E. O'Brien 	else if (Strcmp(vp->vec[0], STRsysv) == 0)
1212c80476e4SDavid E. O'Brien 	    echo_style = SYSV_ECHO;
1213c80476e4SDavid E. O'Brien 	else if (Strcmp(vp->vec[0], STRboth) == 0)
1214c80476e4SDavid E. O'Brien 	    echo_style = BOTH_ECHO;
1215c80476e4SDavid E. O'Brien 	else if (Strcmp(vp->vec[0], STRnone) == 0)
1216c80476e4SDavid E. O'Brien 	    echo_style = NONE_ECHO;
1217c80476e4SDavid E. O'Brien     }
1218c80476e4SDavid E. O'Brien 
1219c80476e4SDavid E. O'Brien     v++;
1220c80476e4SDavid E. O'Brien     if (*v == 0)
12216767bd61SMark Peek 	goto done;
122245e5710bSMark Peek     if (setintr) {
122345e5710bSMark Peek 	int old_pintr_disabled;
122445e5710bSMark Peek 	pintr_push_enable(&old_pintr_disabled);
122545e5710bSMark Peek 	v = glob_all_or_error(v);
122645e5710bSMark Peek 	cleanup_until(&old_pintr_disabled);
122745e5710bSMark Peek     } else {
122845e5710bSMark Peek 	v = glob_all_or_error(v);
1229c80476e4SDavid E. O'Brien     }
123045e5710bSMark Peek     globbed = v;
123145e5710bSMark Peek     if (globbed != NULL)
123245e5710bSMark Peek 	cleanup_push(globbed, blk_cleanup);
1233c80476e4SDavid E. O'Brien 
1234c80476e4SDavid E. O'Brien     if ((echo_style & BSD_ECHO) != 0 && sep == ' ' && *v && eq(*v, STRmn))
1235c80476e4SDavid E. O'Brien 	nonl++, v++;
1236c80476e4SDavid E. O'Brien 
1237c80476e4SDavid E. O'Brien     while ((cp = *v++) != 0) {
123823338178SMark Peek 	Char c;
1239c80476e4SDavid E. O'Brien 
124045e5710bSMark Peek 	if (setintr) {
124145e5710bSMark Peek 	    int old_pintr_disabled;
124245e5710bSMark Peek 
124345e5710bSMark Peek 	    pintr_push_enable(&old_pintr_disabled);
124445e5710bSMark Peek 	    cleanup_until(&old_pintr_disabled);
124545e5710bSMark Peek 	}
1246c80476e4SDavid E. O'Brien 	while ((c = *cp++) != 0) {
1247c80476e4SDavid E. O'Brien 	    if ((echo_style & SYSV_ECHO) != 0 && c == '\\') {
1248c80476e4SDavid E. O'Brien 		switch (c = *cp++) {
1249c80476e4SDavid E. O'Brien 		case 'a':
1250c80476e4SDavid E. O'Brien 		    c = '\a';
1251c80476e4SDavid E. O'Brien 		    break;
1252c80476e4SDavid E. O'Brien 		case 'b':
1253c80476e4SDavid E. O'Brien 		    c = '\b';
1254c80476e4SDavid E. O'Brien 		    break;
1255c80476e4SDavid E. O'Brien 		case 'c':
1256c80476e4SDavid E. O'Brien 		    nonl = 1;
1257c80476e4SDavid E. O'Brien 		    goto done;
1258c80476e4SDavid E. O'Brien 		case 'e':
1259c80476e4SDavid E. O'Brien #if 0			/* Windows does not understand \e */
1260c80476e4SDavid E. O'Brien 		    c = '\e';
1261c80476e4SDavid E. O'Brien #else
126245e5710bSMark Peek 		    c = CTL_ESC('\033');
1263c80476e4SDavid E. O'Brien #endif
1264c80476e4SDavid E. O'Brien 		    break;
1265c80476e4SDavid E. O'Brien 		case 'f':
1266c80476e4SDavid E. O'Brien 		    c = '\f';
1267c80476e4SDavid E. O'Brien 		    break;
1268c80476e4SDavid E. O'Brien 		case 'n':
1269c80476e4SDavid E. O'Brien 		    c = '\n';
1270c80476e4SDavid E. O'Brien 		    break;
1271c80476e4SDavid E. O'Brien 		case 'r':
1272c80476e4SDavid E. O'Brien 		    c = '\r';
1273c80476e4SDavid E. O'Brien 		    break;
1274c80476e4SDavid E. O'Brien 		case 't':
1275c80476e4SDavid E. O'Brien 		    c = '\t';
1276c80476e4SDavid E. O'Brien 		    break;
1277c80476e4SDavid E. O'Brien 		case 'v':
1278c80476e4SDavid E. O'Brien 		    c = '\v';
1279c80476e4SDavid E. O'Brien 		    break;
1280c80476e4SDavid E. O'Brien 		case '\\':
1281c80476e4SDavid E. O'Brien 		    c = '\\';
1282c80476e4SDavid E. O'Brien 		    break;
1283c80476e4SDavid E. O'Brien 		case '0':
1284c80476e4SDavid E. O'Brien 		    c = 0;
1285c80476e4SDavid E. O'Brien 		    if (*cp >= '0' && *cp < '8')
1286c80476e4SDavid E. O'Brien 			c = c * 8 + *cp++ - '0';
1287c80476e4SDavid E. O'Brien 		    if (*cp >= '0' && *cp < '8')
1288c80476e4SDavid E. O'Brien 			c = c * 8 + *cp++ - '0';
1289c80476e4SDavid E. O'Brien 		    if (*cp >= '0' && *cp < '8')
1290c80476e4SDavid E. O'Brien 			c = c * 8 + *cp++ - '0';
1291c80476e4SDavid E. O'Brien 		    break;
1292c80476e4SDavid E. O'Brien 		case '\0':
1293c80476e4SDavid E. O'Brien 		    c = '\\';
1294c80476e4SDavid E. O'Brien 		    cp--;
1295c80476e4SDavid E. O'Brien 		    break;
1296c80476e4SDavid E. O'Brien 		default:
1297c80476e4SDavid E. O'Brien 		    xputchar('\\' | QUOTE);
1298c80476e4SDavid E. O'Brien 		    break;
1299c80476e4SDavid E. O'Brien 		}
1300c80476e4SDavid E. O'Brien 	    }
130123338178SMark Peek 	    xputwchar(c | QUOTE);
1302c80476e4SDavid E. O'Brien 
1303c80476e4SDavid E. O'Brien 	}
1304c80476e4SDavid E. O'Brien 	if (*v)
1305c80476e4SDavid E. O'Brien 	    xputchar(sep | QUOTE);
1306c80476e4SDavid E. O'Brien     }
1307c80476e4SDavid E. O'Brien done:
1308c80476e4SDavid E. O'Brien     if (sep && nonl == 0)
1309c80476e4SDavid E. O'Brien 	xputchar('\n');
1310c80476e4SDavid E. O'Brien     else
1311c80476e4SDavid E. O'Brien 	flush();
131245e5710bSMark Peek     if (globbed != NULL)
131345e5710bSMark Peek 	cleanup_until(globbed);
1314c80476e4SDavid E. O'Brien }
1315c80476e4SDavid E. O'Brien 
1316c80476e4SDavid E. O'Brien /* check whether an environment variable should invoke 'set_locale()' */
131723338178SMark Peek static int
islocale_var(Char * var)131845e5710bSMark Peek islocale_var(Char *var)
1319c80476e4SDavid E. O'Brien {
1320c80476e4SDavid E. O'Brien     static Char *locale_vars[] = {
1321b2d5d167SMark Peek 	STRLANG,	STRLC_ALL, 	STRLC_CTYPE,	STRLC_NUMERIC,
1322b2d5d167SMark Peek 	STRLC_TIME,	STRLC_COLLATE,	STRLC_MESSAGES,	STRLC_MONETARY, 0
1323c80476e4SDavid E. O'Brien     };
132423338178SMark Peek     Char **v;
1325c80476e4SDavid E. O'Brien 
1326c80476e4SDavid E. O'Brien     for (v = locale_vars; *v; ++v)
1327c80476e4SDavid E. O'Brien 	if (eq(var, *v))
1328c80476e4SDavid E. O'Brien 	    return 1;
1329c80476e4SDavid E. O'Brien     return 0;
1330c80476e4SDavid E. O'Brien }
1331c80476e4SDavid E. O'Brien 
133245e5710bSMark Peek static void
xlate_cr_cleanup(void * dummy)133345e5710bSMark Peek xlate_cr_cleanup(void *dummy)
133445e5710bSMark Peek {
133545e5710bSMark Peek     USE(dummy);
133645e5710bSMark Peek     xlate_cr = 0;
133745e5710bSMark Peek }
133845e5710bSMark Peek 
1339c80476e4SDavid E. O'Brien /*ARGSUSED*/
1340c80476e4SDavid E. O'Brien void
doprintenv(Char ** v,struct command * c)134145e5710bSMark Peek doprintenv(Char **v, struct command *c)
1342c80476e4SDavid E. O'Brien {
1343c80476e4SDavid E. O'Brien     Char   *e;
1344c80476e4SDavid E. O'Brien 
1345c80476e4SDavid E. O'Brien     USE(c);
1346c80476e4SDavid E. O'Brien     v++;
1347c80476e4SDavid E. O'Brien     if (*v == 0) {
134823338178SMark Peek 	Char **ep;
1349c80476e4SDavid E. O'Brien 
1350c80476e4SDavid E. O'Brien 	xlate_cr = 1;
135145e5710bSMark Peek 	cleanup_push(&xlate_cr, xlate_cr_cleanup);
135245e5710bSMark Peek 	for (ep = STR_environ; *ep; ep++) {
135345e5710bSMark Peek 	    if (setintr) {
135445e5710bSMark Peek 		int old_pintr_disabled;
135545e5710bSMark Peek 
135645e5710bSMark Peek 		pintr_push_enable(&old_pintr_disabled);
135745e5710bSMark Peek 		cleanup_until(&old_pintr_disabled);
135845e5710bSMark Peek 	    }
1359c80476e4SDavid E. O'Brien 	    xprintf("%S\n", *ep);
136045e5710bSMark Peek 	}
136145e5710bSMark Peek 	cleanup_until(&xlate_cr);
1362c80476e4SDavid E. O'Brien     }
1363c80476e4SDavid E. O'Brien     else if ((e = tgetenv(*v)) != NULL) {
136445e5710bSMark Peek 	int old_output_raw;
136545e5710bSMark Peek 
136645e5710bSMark Peek 	old_output_raw = output_raw;
1367c80476e4SDavid E. O'Brien 	output_raw = 1;
136845e5710bSMark Peek 	cleanup_push(&old_output_raw, output_raw_restore);
1369c80476e4SDavid E. O'Brien 	xprintf("%S\n", e);
137045e5710bSMark Peek 	cleanup_until(&old_output_raw);
1371c80476e4SDavid E. O'Brien     }
1372c80476e4SDavid E. O'Brien     else
137345e5710bSMark Peek 	setcopy(STRstatus, STR1, VAR_READWRITE);
1374c80476e4SDavid E. O'Brien }
1375c80476e4SDavid E. O'Brien 
1376c80476e4SDavid E. O'Brien /* from "Karl Berry." <karl%mote.umb.edu@relay.cs.net> -- for NeXT things
1377c80476e4SDavid E. O'Brien    (and anything else with a modern compiler) */
1378c80476e4SDavid E. O'Brien 
1379c80476e4SDavid E. O'Brien /*ARGSUSED*/
1380c80476e4SDavid E. O'Brien void
dosetenv(Char ** v,struct command * c)138145e5710bSMark Peek dosetenv(Char **v, struct command *c)
1382c80476e4SDavid E. O'Brien {
1383c80476e4SDavid E. O'Brien     Char   *vp, *lp;
1384c80476e4SDavid E. O'Brien 
1385c80476e4SDavid E. O'Brien     USE(c);
1386c80476e4SDavid E. O'Brien     if (*++v == 0) {
1387c80476e4SDavid E. O'Brien 	doprintenv(--v, 0);
1388c80476e4SDavid E. O'Brien 	return;
1389c80476e4SDavid E. O'Brien     }
1390c80476e4SDavid E. O'Brien 
1391c80476e4SDavid E. O'Brien     vp = *v++;
139229301572SMark Peek     lp = vp;
139329301572SMark Peek 
13949ccc37e3SMark Peek     if (!letter(*lp))
13959ccc37e3SMark Peek 	stderror(ERR_NAME | ERR_VARBEGIN);
13969ccc37e3SMark Peek     do {
13979ccc37e3SMark Peek 	lp++;
139819d2e3deSDmitry Chagin     } while (alnum(*lp) || *lp == '.');
13999ccc37e3SMark Peek     if (*lp != '\0')
14009ccc37e3SMark Peek 	stderror(ERR_NAME | ERR_VARALNUM);
14019ccc37e3SMark Peek 
1402c80476e4SDavid E. O'Brien     if ((lp = *v++) == 0)
1403c80476e4SDavid E. O'Brien 	lp = STRNULL;
1404c80476e4SDavid E. O'Brien 
140545e5710bSMark Peek     lp = globone(lp, G_APPEND);
140645e5710bSMark Peek     cleanup_push(lp, xfree);
140745e5710bSMark Peek     tsetenv(vp, lp);
1408c80476e4SDavid E. O'Brien     if (eq(vp, STRKPATH)) {
1409c80476e4SDavid E. O'Brien         importpath(lp);
1410c80476e4SDavid E. O'Brien 	dohash(NULL, NULL);
141145e5710bSMark Peek 	cleanup_until(lp);
1412c80476e4SDavid E. O'Brien 	return;
1413c80476e4SDavid E. O'Brien     }
1414c80476e4SDavid E. O'Brien 
1415c80476e4SDavid E. O'Brien #ifdef apollo
1416c80476e4SDavid E. O'Brien     if (eq(vp, STRSYSTYPE)) {
1417c80476e4SDavid E. O'Brien 	dohash(NULL, NULL);
141845e5710bSMark Peek 	cleanup_until(lp);
1419c80476e4SDavid E. O'Brien 	return;
1420c80476e4SDavid E. O'Brien     }
1421c80476e4SDavid E. O'Brien #endif /* apollo */
1422c80476e4SDavid E. O'Brien 
1423c80476e4SDavid E. O'Brien     /* dspkanji/dspmbyte autosetting */
1424c80476e4SDavid E. O'Brien     /* PATCH IDEA FROM Issei.Suzuki VERY THANKS */
1425c80476e4SDavid E. O'Brien #if defined(DSPMBYTE)
1426c80476e4SDavid E. O'Brien     if (eq(vp, STRLANG) && !adrof(CHECK_MBYTEVAR)) {
1427c80476e4SDavid E. O'Brien 	autoset_dspmbyte(lp);
1428c80476e4SDavid E. O'Brien     }
1429c80476e4SDavid E. O'Brien #endif
1430c80476e4SDavid E. O'Brien 
1431c80476e4SDavid E. O'Brien     if (islocale_var(vp)) {
1432c80476e4SDavid E. O'Brien #ifdef NLS
1433c80476e4SDavid E. O'Brien 	int     k;
1434c80476e4SDavid E. O'Brien 
1435c80476e4SDavid E. O'Brien # ifdef SETLOCALEBUG
1436c80476e4SDavid E. O'Brien 	dont_free = 1;
1437c80476e4SDavid E. O'Brien # endif /* SETLOCALEBUG */
1438c80476e4SDavid E. O'Brien 	(void) setlocale(LC_ALL, "");
1439c80476e4SDavid E. O'Brien # ifdef LC_COLLATE
1440c80476e4SDavid E. O'Brien 	(void) setlocale(LC_COLLATE, "");
1441c80476e4SDavid E. O'Brien # endif
144223338178SMark Peek # ifdef LC_CTYPE
144323338178SMark Peek 	(void) setlocale(LC_CTYPE, ""); /* for iscntrl */
144423338178SMark Peek # endif /* LC_CTYPE */
14459ccc37e3SMark Peek # if defined(AUTOSET_KANJI)
14469ccc37e3SMark Peek         autoset_kanji();
14479ccc37e3SMark Peek # endif /* AUTOSET_KANJI */
1448c80476e4SDavid E. O'Brien # ifdef NLS_CATALOGS
1449c80476e4SDavid E. O'Brien #  ifdef LC_MESSAGES
1450c80476e4SDavid E. O'Brien 	(void) setlocale(LC_MESSAGES, "");
1451c80476e4SDavid E. O'Brien #  endif /* LC_MESSAGES */
145223338178SMark Peek 	nlsclose();
1453c80476e4SDavid E. O'Brien 	nlsinit();
1454c80476e4SDavid E. O'Brien # endif /* NLS_CATALOGS */
1455c80476e4SDavid E. O'Brien # ifdef SETLOCALEBUG
1456c80476e4SDavid E. O'Brien 	dont_free = 0;
1457c80476e4SDavid E. O'Brien # endif /* SETLOCALEBUG */
1458c80476e4SDavid E. O'Brien # ifdef STRCOLLBUG
1459c80476e4SDavid E. O'Brien 	fix_strcoll_bug();
1460c80476e4SDavid E. O'Brien # endif /* STRCOLLBUG */
1461c80476e4SDavid E. O'Brien 	tw_cmd_free();	/* since the collation sequence has changed */
146245e5710bSMark Peek 	for (k = 0200; k <= 0377 && !Isprint(CTL_ESC(k)); k++)
1463c80476e4SDavid E. O'Brien 	    continue;
146423338178SMark Peek 	AsciiOnly = MB_CUR_MAX == 1 && k > 0377;
1465c80476e4SDavid E. O'Brien #else /* !NLS */
1466c80476e4SDavid E. O'Brien 	AsciiOnly = 0;
1467c80476e4SDavid E. O'Brien #endif /* NLS */
1468c80476e4SDavid E. O'Brien 	NLSMapsAreInited = 0;
1469c80476e4SDavid E. O'Brien 	ed_Init();
1470c80476e4SDavid E. O'Brien 	if (MapsAreInited && !NLSMapsAreInited)
1471c80476e4SDavid E. O'Brien 	    ed_InitNLSMaps();
147245e5710bSMark Peek 	cleanup_until(lp);
1473c80476e4SDavid E. O'Brien 	return;
1474c80476e4SDavid E. O'Brien     }
1475c80476e4SDavid E. O'Brien 
147629301572SMark Peek #ifdef NLS_CATALOGS
147729301572SMark Peek     if (eq(vp, STRNLSPATH)) {
147823338178SMark Peek 	nlsclose();
147929301572SMark Peek 	nlsinit();
148029301572SMark Peek     }
148129301572SMark Peek #endif
148229301572SMark Peek 
1483c80476e4SDavid E. O'Brien     if (eq(vp, STRNOREBIND)) {
1484c80476e4SDavid E. O'Brien 	NoNLSRebind = 1;
1485c80476e4SDavid E. O'Brien 	MapsAreInited = 0;
1486c80476e4SDavid E. O'Brien 	NLSMapsAreInited = 0;
1487c80476e4SDavid E. O'Brien 	ed_InitMaps();
148845e5710bSMark Peek 	cleanup_until(lp);
1489c80476e4SDavid E. O'Brien 	return;
1490c80476e4SDavid E. O'Brien     }
14913b6eaa7bSAndrey A. Chernov #ifdef WINNT_NATIVE
1492c80476e4SDavid E. O'Brien     if (eq(vp, STRtcshlang)) {
1493c80476e4SDavid E. O'Brien 	nlsinit();
149445e5710bSMark Peek 	cleanup_until(lp);
1495c80476e4SDavid E. O'Brien 	return;
1496c80476e4SDavid E. O'Brien     }
14973b6eaa7bSAndrey A. Chernov #endif /* WINNT_NATIVE */
1498c80476e4SDavid E. O'Brien     if (eq(vp, STRKTERM)) {
1499c80476e4SDavid E. O'Brien 	char *t;
150045e5710bSMark Peek 
150145e5710bSMark Peek 	setv(STRterm, quote(lp), VAR_READWRITE);	/* lp memory used here */
150245e5710bSMark Peek 	cleanup_ignore(lp);
150345e5710bSMark Peek 	cleanup_until(lp);
1504c80476e4SDavid E. O'Brien 	t = short2str(lp);
1505c80476e4SDavid E. O'Brien 	if (noediting && strcmp(t, "unknown") != 0 && strcmp(t,"dumb") != 0) {
1506c80476e4SDavid E. O'Brien 	    editing = 1;
1507c80476e4SDavid E. O'Brien 	    noediting = 0;
150845e5710bSMark Peek 	    setNS(STRedit);
1509c80476e4SDavid E. O'Brien 	}
1510c80476e4SDavid E. O'Brien 	GotTermCaps = 0;
1511c80476e4SDavid E. O'Brien 	ed_Init();
1512c80476e4SDavid E. O'Brien 	return;
1513c80476e4SDavid E. O'Brien     }
1514c80476e4SDavid E. O'Brien 
1515c80476e4SDavid E. O'Brien     if (eq(vp, STRKHOME)) {
151645e5710bSMark Peek 	Char *canon;
1517c80476e4SDavid E. O'Brien 	/*
1518c80476e4SDavid E. O'Brien 	 * convert to canonical pathname (possibly resolving symlinks)
1519c80476e4SDavid E. O'Brien 	 */
152045e5710bSMark Peek 	cleanup_ignore(lp);
1521*5224c2a3SDmitry Chagin 	canon = dcanon(lp, lp);
152245e5710bSMark Peek 	cleanup_push(canon, xfree);
152345e5710bSMark Peek 	setv(STRhome, quote(canon), VAR_READWRITE); /* lp memory used here */
152445e5710bSMark Peek 	cleanup_ignore(canon);
152545e5710bSMark Peek 	cleanup_until(canon);
1526c80476e4SDavid E. O'Brien 
1527c80476e4SDavid E. O'Brien 	/* fix directory stack for new tilde home */
1528c80476e4SDavid E. O'Brien 	dtilde();
1529c80476e4SDavid E. O'Brien 	return;
1530c80476e4SDavid E. O'Brien     }
1531c80476e4SDavid E. O'Brien 
1532c80476e4SDavid E. O'Brien     if (eq(vp, STRKSHLVL)) {
153345e5710bSMark Peek 	setv(STRshlvl, quote(lp), VAR_READWRITE); /* lp memory used here */
153445e5710bSMark Peek 	cleanup_ignore(lp);
153545e5710bSMark Peek 	cleanup_until(lp);
1536c80476e4SDavid E. O'Brien 	return;
1537c80476e4SDavid E. O'Brien     }
1538c80476e4SDavid E. O'Brien 
1539c80476e4SDavid E. O'Brien     if (eq(vp, STRKUSER)) {
154045e5710bSMark Peek 	setv(STRuser, quote(lp), VAR_READWRITE);	/* lp memory used here */
154145e5710bSMark Peek 	cleanup_ignore(lp);
154245e5710bSMark Peek 	cleanup_until(lp);
1543c80476e4SDavid E. O'Brien 	return;
1544c80476e4SDavid E. O'Brien     }
1545c80476e4SDavid E. O'Brien 
1546c80476e4SDavid E. O'Brien     if (eq(vp, STRKGROUP)) {
154745e5710bSMark Peek 	setv(STRgroup, quote(lp), VAR_READWRITE); /* lp memory used here */
154845e5710bSMark Peek 	cleanup_ignore(lp);
154945e5710bSMark Peek 	cleanup_until(lp);
1550c80476e4SDavid E. O'Brien 	return;
1551c80476e4SDavid E. O'Brien     }
1552c80476e4SDavid E. O'Brien 
1553c80476e4SDavid E. O'Brien #ifdef COLOR_LS_F
1554c80476e4SDavid E. O'Brien     if (eq(vp, STRLS_COLORS)) {
1555c80476e4SDavid E. O'Brien         parseLS_COLORS(lp);
155645e5710bSMark Peek 	cleanup_until(lp);
1557c80476e4SDavid E. O'Brien 	return;
1558c80476e4SDavid E. O'Brien     }
155919d2e3deSDmitry Chagin     if (eq(vp, STRLSCOLORS)) {
156019d2e3deSDmitry Chagin         parseLSCOLORS(lp);
156119d2e3deSDmitry Chagin 	cleanup_until(lp);
156219d2e3deSDmitry Chagin 	return;
156319d2e3deSDmitry Chagin     }
1564c80476e4SDavid E. O'Brien #endif /* COLOR_LS_F */
1565c80476e4SDavid E. O'Brien 
1566c80476e4SDavid E. O'Brien #ifdef SIG_WINDOW
1567c80476e4SDavid E. O'Brien     /*
1568c80476e4SDavid E. O'Brien      * Load/Update $LINES $COLUMNS
1569c80476e4SDavid E. O'Brien      */
1570c80476e4SDavid E. O'Brien     if ((eq(lp, STRNULL) && (eq(vp, STRLINES) || eq(vp, STRCOLUMNS))) ||
1571c80476e4SDavid E. O'Brien 	eq(vp, STRTERMCAP)) {
157245e5710bSMark Peek 	cleanup_until(lp);
1573c80476e4SDavid E. O'Brien 	check_window_size(1);
1574c80476e4SDavid E. O'Brien 	return;
1575c80476e4SDavid E. O'Brien     }
1576c80476e4SDavid E. O'Brien 
1577c80476e4SDavid E. O'Brien     /*
1578c80476e4SDavid E. O'Brien      * Change the size to the one directed by $LINES and $COLUMNS
1579c80476e4SDavid E. O'Brien      */
1580c80476e4SDavid E. O'Brien     if (eq(vp, STRLINES) || eq(vp, STRCOLUMNS)) {
1581c80476e4SDavid E. O'Brien #if 0
1582c80476e4SDavid E. O'Brien 	GotTermCaps = 0;
1583c80476e4SDavid E. O'Brien #endif
158445e5710bSMark Peek 	cleanup_until(lp);
1585c80476e4SDavid E. O'Brien 	ed_Init();
1586c80476e4SDavid E. O'Brien 	return;
1587c80476e4SDavid E. O'Brien     }
1588c80476e4SDavid E. O'Brien #endif /* SIG_WINDOW */
158945e5710bSMark Peek     cleanup_until(lp);
1590c80476e4SDavid E. O'Brien }
1591c80476e4SDavid E. O'Brien 
1592c80476e4SDavid E. O'Brien /*ARGSUSED*/
1593c80476e4SDavid E. O'Brien void
dounsetenv(Char ** v,struct command * c)159445e5710bSMark Peek dounsetenv(Char **v, struct command *c)
1595c80476e4SDavid E. O'Brien {
159645e5710bSMark Peek     Char  **ep, *p, *n, *name;
1597c80476e4SDavid E. O'Brien     int     i, maxi;
1598c80476e4SDavid E. O'Brien 
1599c80476e4SDavid E. O'Brien     USE(c);
1600c80476e4SDavid E. O'Brien     /*
1601c80476e4SDavid E. O'Brien      * Find the longest environment variable
1602c80476e4SDavid E. O'Brien      */
1603c80476e4SDavid E. O'Brien     for (maxi = 0, ep = STR_environ; *ep; ep++) {
1604c80476e4SDavid E. O'Brien 	for (i = 0, p = *ep; *p && *p != '='; p++, i++)
1605c80476e4SDavid E. O'Brien 	    continue;
1606c80476e4SDavid E. O'Brien 	if (i > maxi)
1607c80476e4SDavid E. O'Brien 	    maxi = i;
1608c80476e4SDavid E. O'Brien     }
1609c80476e4SDavid E. O'Brien 
161045e5710bSMark Peek     name = xmalloc((maxi + 1) * sizeof(Char));
161145e5710bSMark Peek     cleanup_push(name, xfree);
1612c80476e4SDavid E. O'Brien 
1613c80476e4SDavid E. O'Brien     while (++v && *v)
1614c80476e4SDavid E. O'Brien 	for (maxi = 1; maxi;)
1615c80476e4SDavid E. O'Brien 	    for (maxi = 0, ep = STR_environ; *ep; ep++) {
1616c80476e4SDavid E. O'Brien 		for (n = name, p = *ep; *p && *p != '='; *n++ = *p++)
1617c80476e4SDavid E. O'Brien 		    continue;
1618c80476e4SDavid E. O'Brien 		*n = '\0';
1619c80476e4SDavid E. O'Brien 		if (!Gmatch(name, *v))
1620c80476e4SDavid E. O'Brien 		    continue;
1621c80476e4SDavid E. O'Brien 		maxi = 1;
1622c80476e4SDavid E. O'Brien 
1623c80476e4SDavid E. O'Brien 		/* Unset the name. This wasn't being done until
1624c80476e4SDavid E. O'Brien 		 * later but most of the stuff following won't
1625c80476e4SDavid E. O'Brien 		 * work (particularly the setlocale() and getenv()
1626c80476e4SDavid E. O'Brien 		 * stuff) as intended until the name is actually
1627c80476e4SDavid E. O'Brien 		 * removed. (sg)
1628c80476e4SDavid E. O'Brien 		 */
1629c80476e4SDavid E. O'Brien 		Unsetenv(name);
1630c80476e4SDavid E. O'Brien 
1631c80476e4SDavid E. O'Brien 		if (eq(name, STRNOREBIND)) {
1632c80476e4SDavid E. O'Brien 		    NoNLSRebind = 0;
1633c80476e4SDavid E. O'Brien 		    MapsAreInited = 0;
1634c80476e4SDavid E. O'Brien 		    NLSMapsAreInited = 0;
1635c80476e4SDavid E. O'Brien 		    ed_InitMaps();
1636c80476e4SDavid E. O'Brien 		}
1637c80476e4SDavid E. O'Brien #ifdef apollo
1638c80476e4SDavid E. O'Brien 		else if (eq(name, STRSYSTYPE))
1639c80476e4SDavid E. O'Brien 		    dohash(NULL, NULL);
1640c80476e4SDavid E. O'Brien #endif /* apollo */
1641c80476e4SDavid E. O'Brien 		else if (islocale_var(name)) {
1642c80476e4SDavid E. O'Brien #ifdef NLS
1643c80476e4SDavid E. O'Brien 		    int     k;
1644c80476e4SDavid E. O'Brien 
1645c80476e4SDavid E. O'Brien # ifdef SETLOCALEBUG
1646c80476e4SDavid E. O'Brien 		    dont_free = 1;
1647c80476e4SDavid E. O'Brien # endif /* SETLOCALEBUG */
1648c80476e4SDavid E. O'Brien 		    (void) setlocale(LC_ALL, "");
1649c80476e4SDavid E. O'Brien # ifdef LC_COLLATE
1650c80476e4SDavid E. O'Brien 		    (void) setlocale(LC_COLLATE, "");
1651c80476e4SDavid E. O'Brien # endif
165223338178SMark Peek # ifdef LC_CTYPE
165323338178SMark Peek 		    (void) setlocale(LC_CTYPE, ""); /* for iscntrl */
165423338178SMark Peek # endif /* LC_CTYPE */
1655c80476e4SDavid E. O'Brien # ifdef NLS_CATALOGS
1656c80476e4SDavid E. O'Brien #  ifdef LC_MESSAGES
1657c80476e4SDavid E. O'Brien 		    (void) setlocale(LC_MESSAGES, "");
1658c80476e4SDavid E. O'Brien #  endif /* LC_MESSAGES */
165923338178SMark Peek 		    nlsclose();
1660c80476e4SDavid E. O'Brien 		    nlsinit();
1661c80476e4SDavid E. O'Brien # endif /* NLS_CATALOGS */
1662c80476e4SDavid E. O'Brien # ifdef SETLOCALEBUG
1663c80476e4SDavid E. O'Brien 		    dont_free = 0;
1664c80476e4SDavid E. O'Brien # endif /* SETLOCALEBUG */
1665c80476e4SDavid E. O'Brien # ifdef STRCOLLBUG
1666c80476e4SDavid E. O'Brien 		    fix_strcoll_bug();
1667c80476e4SDavid E. O'Brien # endif /* STRCOLLBUG */
1668c80476e4SDavid E. O'Brien 		    tw_cmd_free();/* since the collation sequence has changed */
166945e5710bSMark Peek 		    for (k = 0200; k <= 0377 && !Isprint(CTL_ESC(k)); k++)
1670c80476e4SDavid E. O'Brien 			continue;
167123338178SMark Peek 		    AsciiOnly = MB_CUR_MAX == 1 && k > 0377;
1672c80476e4SDavid E. O'Brien #else /* !NLS */
1673c80476e4SDavid E. O'Brien 		    AsciiOnly = getenv("LANG") == NULL &&
1674c80476e4SDavid E. O'Brien 			getenv("LC_CTYPE") == NULL;
1675c80476e4SDavid E. O'Brien #endif /* NLS */
1676c80476e4SDavid E. O'Brien 		    NLSMapsAreInited = 0;
1677c80476e4SDavid E. O'Brien 		    ed_Init();
1678c80476e4SDavid E. O'Brien 		    if (MapsAreInited && !NLSMapsAreInited)
1679c80476e4SDavid E. O'Brien 			ed_InitNLSMaps();
1680c80476e4SDavid E. O'Brien 
1681c80476e4SDavid E. O'Brien 		}
16823b6eaa7bSAndrey A. Chernov #ifdef WINNT_NATIVE
1683c80476e4SDavid E. O'Brien 		else if (eq(name,(STRtcshlang))) {
1684c80476e4SDavid E. O'Brien 		    nls_dll_unload();
1685c80476e4SDavid E. O'Brien 		    nlsinit();
1686c80476e4SDavid E. O'Brien 		}
16873b6eaa7bSAndrey A. Chernov #endif /* WINNT_NATIVE */
1688c80476e4SDavid E. O'Brien #ifdef COLOR_LS_F
1689c80476e4SDavid E. O'Brien 		else if (eq(name, STRLS_COLORS))
1690c80476e4SDavid E. O'Brien 		    parseLS_COLORS(n);
169119d2e3deSDmitry Chagin 		else if (eq(name, STRLSCOLORS))
169219d2e3deSDmitry Chagin 		    parseLSCOLORS(n);
1693c80476e4SDavid E. O'Brien #endif /* COLOR_LS_F */
169429301572SMark Peek #ifdef NLS_CATALOGS
169529301572SMark Peek 		else if (eq(name, STRNLSPATH)) {
169623338178SMark Peek 		    nlsclose();
169729301572SMark Peek 		    nlsinit();
169829301572SMark Peek 		}
169929301572SMark Peek #endif
1700c80476e4SDavid E. O'Brien 		/*
1701c80476e4SDavid E. O'Brien 		 * start again cause the environment changes
1702c80476e4SDavid E. O'Brien 		 */
1703c80476e4SDavid E. O'Brien 		break;
1704c80476e4SDavid E. O'Brien 	    }
170545e5710bSMark Peek     cleanup_until(name);
1706c80476e4SDavid E. O'Brien }
1707c80476e4SDavid E. O'Brien 
1708c80476e4SDavid E. O'Brien void
tsetenv(const Char * name,const Char * val)170945e5710bSMark Peek tsetenv(const Char *name, const Char *val)
1710c80476e4SDavid E. O'Brien {
1711c80476e4SDavid E. O'Brien #ifdef SETENV_IN_LIB
1712c80476e4SDavid E. O'Brien /*
1713c80476e4SDavid E. O'Brien  * XXX: This does not work right, since tcsh cannot track changes to
1714c80476e4SDavid E. O'Brien  * the environment this way. (the builtin setenv without arguments does
1715c80476e4SDavid E. O'Brien  * not print the right stuff neither does unsetenv). This was for Mach,
1716c80476e4SDavid E. O'Brien  * it is not needed anymore.
1717c80476e4SDavid E. O'Brien  */
1718c80476e4SDavid E. O'Brien #undef setenv
171945e5710bSMark Peek     char   *cname;
1720c80476e4SDavid E. O'Brien 
172145e5710bSMark Peek     if (name == NULL)
1722c80476e4SDavid E. O'Brien 	return;
172345e5710bSMark Peek     cname = strsave(short2str(name));
172445e5710bSMark Peek     setenv(cname, short2str(val), 1);
172545e5710bSMark Peek     xfree(cname);
1726c80476e4SDavid E. O'Brien #else /* !SETENV_IN_LIB */
172723338178SMark Peek     Char **ep = STR_environ;
172823338178SMark Peek     const Char *ccp;
172923338178SMark Peek     Char *cp, *dp;
1730c80476e4SDavid E. O'Brien     Char   *blk[2];
1731c80476e4SDavid E. O'Brien     Char  **oep = ep;
1732c80476e4SDavid E. O'Brien 
17333b6eaa7bSAndrey A. Chernov #ifdef WINNT_NATIVE
1734c80476e4SDavid E. O'Brien     nt_set_env(name,val);
17353b6eaa7bSAndrey A. Chernov #endif /* WINNT_NATIVE */
1736c80476e4SDavid E. O'Brien     for (; *ep; ep++) {
17373b6eaa7bSAndrey A. Chernov #ifdef WINNT_NATIVE
173823338178SMark Peek 	for (ccp = name, dp = *ep; *ccp && Tolower(*ccp & TRIM) == Tolower(*dp);
173923338178SMark Peek 				ccp++, dp++)
1740c80476e4SDavid E. O'Brien #else
174123338178SMark Peek 	for (ccp = name, dp = *ep; *ccp && (*ccp & TRIM) == *dp; ccp++, dp++)
17423b6eaa7bSAndrey A. Chernov #endif /* WINNT_NATIVE */
1743c80476e4SDavid E. O'Brien 	    continue;
174423338178SMark Peek 	if (*ccp != 0 || *dp != '=')
1745c80476e4SDavid E. O'Brien 	    continue;
1746c80476e4SDavid E. O'Brien 	cp = Strspl(STRequal, val);
174745e5710bSMark Peek 	xfree(*ep);
1748c80476e4SDavid E. O'Brien 	*ep = strip(Strspl(name, cp));
174945e5710bSMark Peek 	xfree(cp);
1750c80476e4SDavid E. O'Brien 	blkfree((Char **) environ);
1751c80476e4SDavid E. O'Brien 	environ = short2blk(STR_environ);
1752c80476e4SDavid E. O'Brien 	return;
1753c80476e4SDavid E. O'Brien     }
1754c80476e4SDavid E. O'Brien     cp = Strspl(name, STRequal);
1755c80476e4SDavid E. O'Brien     blk[0] = strip(Strspl(cp, val));
175645e5710bSMark Peek     xfree(cp);
1757c80476e4SDavid E. O'Brien     blk[1] = 0;
1758c80476e4SDavid E. O'Brien     STR_environ = blkspl(STR_environ, blk);
1759c80476e4SDavid E. O'Brien     blkfree((Char **) environ);
1760c80476e4SDavid E. O'Brien     environ = short2blk(STR_environ);
176145e5710bSMark Peek     xfree(oep);
1762c80476e4SDavid E. O'Brien #endif /* SETENV_IN_LIB */
1763c80476e4SDavid E. O'Brien }
1764c80476e4SDavid E. O'Brien 
1765c80476e4SDavid E. O'Brien void
Unsetenv(Char * name)176645e5710bSMark Peek Unsetenv(Char *name)
1767c80476e4SDavid E. O'Brien {
176823338178SMark Peek     Char **ep = STR_environ;
176923338178SMark Peek     Char *cp, *dp;
1770c80476e4SDavid E. O'Brien     Char **oep = ep;
1771c80476e4SDavid E. O'Brien 
17723b6eaa7bSAndrey A. Chernov #ifdef WINNT_NATIVE
1773c80476e4SDavid E. O'Brien 	nt_set_env(name,NULL);
17743b6eaa7bSAndrey A. Chernov #endif /*WINNT_NATIVE */
1775c80476e4SDavid E. O'Brien     for (; *ep; ep++) {
1776c80476e4SDavid E. O'Brien 	for (cp = name, dp = *ep; *cp && *cp == *dp; cp++, dp++)
1777c80476e4SDavid E. O'Brien 	    continue;
1778c80476e4SDavid E. O'Brien 	if (*cp != 0 || *dp != '=')
1779c80476e4SDavid E. O'Brien 	    continue;
1780c80476e4SDavid E. O'Brien 	cp = *ep;
1781c80476e4SDavid E. O'Brien 	*ep = 0;
1782c80476e4SDavid E. O'Brien 	STR_environ = blkspl(STR_environ, ep + 1);
1783c80476e4SDavid E. O'Brien 	blkfree((Char **) environ);
1784c80476e4SDavid E. O'Brien 	environ = short2blk(STR_environ);
1785c80476e4SDavid E. O'Brien 	*ep = cp;
178645e5710bSMark Peek 	xfree(cp);
178745e5710bSMark Peek 	xfree(oep);
1788c80476e4SDavid E. O'Brien 	return;
1789c80476e4SDavid E. O'Brien     }
1790c80476e4SDavid E. O'Brien }
1791c80476e4SDavid E. O'Brien 
1792c80476e4SDavid E. O'Brien /*ARGSUSED*/
1793c80476e4SDavid E. O'Brien void
doumask(Char ** v,struct command * c)179445e5710bSMark Peek doumask(Char **v, struct command *c)
1795c80476e4SDavid E. O'Brien {
179623338178SMark Peek     Char *cp = v[1];
179723338178SMark Peek     int i;
1798c80476e4SDavid E. O'Brien 
1799c80476e4SDavid E. O'Brien     USE(c);
1800c80476e4SDavid E. O'Brien     if (cp == 0) {
18018e66bd9eSDavid E. O'Brien 	i = (int)umask(0);
1802c80476e4SDavid E. O'Brien 	(void) umask(i);
1803c80476e4SDavid E. O'Brien 	xprintf("%o\n", i);
1804c80476e4SDavid E. O'Brien 	return;
1805c80476e4SDavid E. O'Brien     }
1806c80476e4SDavid E. O'Brien     i = 0;
1807c80476e4SDavid E. O'Brien     while (Isdigit(*cp) && *cp != '8' && *cp != '9')
1808c80476e4SDavid E. O'Brien 	i = i * 8 + *cp++ - '0';
1809c80476e4SDavid E. O'Brien     if (*cp || i < 0 || i > 0777)
1810c80476e4SDavid E. O'Brien 	stderror(ERR_NAME | ERR_MASK);
1811c80476e4SDavid E. O'Brien     (void) umask(i);
1812c80476e4SDavid E. O'Brien }
1813c80476e4SDavid E. O'Brien 
1814c80476e4SDavid E. O'Brien #ifndef HAVENOLIMIT
1815c80476e4SDavid E. O'Brien # ifndef BSDLIMIT
1816c80476e4SDavid E. O'Brien    typedef long RLIM_TYPE;
181723338178SMark Peek #  ifdef _OSD_POSIX /* BS2000 */
181823338178SMark Peek #   include <ulimit.h>
181923338178SMark Peek #  endif
1820c80476e4SDavid E. O'Brien #  ifndef RLIM_INFINITY
1821c80476e4SDavid E. O'Brien #   if !defined(_MINIX) && !defined(__clipper__) && !defined(_CRAY)
1822c80476e4SDavid E. O'Brien     extern RLIM_TYPE ulimit();
1823c80476e4SDavid E. O'Brien #   endif /* ! _MINIX && !__clipper__ */
1824c80476e4SDavid E. O'Brien #   define RLIM_INFINITY 0x003fffff
1825c80476e4SDavid E. O'Brien #   define RLIMIT_FSIZE 1
1826c80476e4SDavid E. O'Brien #  endif /* RLIM_INFINITY */
1827c80476e4SDavid E. O'Brien #  ifdef aiws
1828c80476e4SDavid E. O'Brien #   define toset(a) (((a) == 3) ? 1004 : (a) + 1)
1829c80476e4SDavid E. O'Brien #   define RLIMIT_DATA	3
1830c80476e4SDavid E. O'Brien #   define RLIMIT_STACK 1005
1831c80476e4SDavid E. O'Brien #  else /* aiws */
1832c80476e4SDavid E. O'Brien #   define toset(a) ((a) + 1)
1833c80476e4SDavid E. O'Brien #  endif /* aiws */
1834c80476e4SDavid E. O'Brien # else /* BSDLIMIT */
183523338178SMark Peek #  if (defined(BSD4_4) || defined(__linux__) || defined(__GNU__) || defined(__GLIBC__) || (HPUXVERSION >= 1100)) && !defined(__386BSD__)
183629301572SMark Peek     typedef rlim_t RLIM_TYPE;
1837c80476e4SDavid E. O'Brien #  else
1838c80476e4SDavid E. O'Brien #   if defined(SOLARIS2) || (defined(sgi) && SYSVREL > 3)
1839c80476e4SDavid E. O'Brien      typedef rlim_t RLIM_TYPE;
1840c80476e4SDavid E. O'Brien #   else
1841c80476e4SDavid E. O'Brien #    if defined(_SX)
1842c80476e4SDavid E. O'Brien       typedef long long RLIM_TYPE;
184329301572SMark Peek #    else /* !_SX */
1844c80476e4SDavid E. O'Brien       typedef unsigned long RLIM_TYPE;
1845c80476e4SDavid E. O'Brien #    endif /* _SX */
1846c80476e4SDavid E. O'Brien #   endif /* SOLARIS2 || (sgi && SYSVREL > 3) */
1847c80476e4SDavid E. O'Brien #  endif /* BSD4_4 && !__386BSD__  */
1848c80476e4SDavid E. O'Brien # endif /* BSDLIMIT */
1849c80476e4SDavid E. O'Brien 
1850b2d5d167SMark Peek # if (HPUXVERSION > 700) && (HPUXVERSION < 1100) && defined(BSDLIMIT)
1851c80476e4SDavid E. O'Brien /* Yes hpux8.0 has limits but <sys/resource.h> does not make them public */
1852c80476e4SDavid E. O'Brien /* Yes, we could have defined _KERNEL, and -I/etc/conf/h, but is that better? */
1853c80476e4SDavid E. O'Brien #  ifndef RLIMIT_CPU
1854c80476e4SDavid E. O'Brien #   define RLIMIT_CPU		0
1855c80476e4SDavid E. O'Brien #   define RLIMIT_FSIZE		1
1856c80476e4SDavid E. O'Brien #   define RLIMIT_DATA		2
1857c80476e4SDavid E. O'Brien #   define RLIMIT_STACK		3
1858c80476e4SDavid E. O'Brien #   define RLIMIT_CORE		4
1859c80476e4SDavid E. O'Brien #   define RLIMIT_RSS		5
1860c80476e4SDavid E. O'Brien #   define RLIMIT_NOFILE	6
1861c80476e4SDavid E. O'Brien #  endif /* RLIMIT_CPU */
1862c80476e4SDavid E. O'Brien #  ifndef RLIM_INFINITY
1863c80476e4SDavid E. O'Brien #   define RLIM_INFINITY	0x7fffffff
1864c80476e4SDavid E. O'Brien #  endif /* RLIM_INFINITY */
1865c80476e4SDavid E. O'Brien    /*
1866c80476e4SDavid E. O'Brien     * old versions of HP/UX counted limits in 512 bytes
1867c80476e4SDavid E. O'Brien     */
1868c80476e4SDavid E. O'Brien #  ifndef SIGRTMIN
1869c80476e4SDavid E. O'Brien #   define FILESIZE512
1870c80476e4SDavid E. O'Brien #  endif /* SIGRTMIN */
1871b2d5d167SMark Peek # endif /* (HPUXVERSION > 700) && (HPUXVERSION < 1100) && BSDLIMIT */
1872c80476e4SDavid E. O'Brien 
1873c80476e4SDavid E. O'Brien # if SYSVREL > 3 && defined(BSDLIMIT) && !defined(_SX)
1874c80476e4SDavid E. O'Brien /* In order to use rusage, we included "/usr/ucbinclude/sys/resource.h" in */
1875c80476e4SDavid E. O'Brien /* sh.h.  However, some SVR4 limits are defined in <sys/resource.h>.  Rather */
1876c80476e4SDavid E. O'Brien /* than include both and get warnings, we define the extra SVR4 limits here. */
18776767bd61SMark Peek /* XXX: I don't understand if RLIMIT_AS is defined, why don't we define */
18786767bd61SMark Peek /* RLIMIT_VMEM based on it? */
1879c80476e4SDavid E. O'Brien #  ifndef RLIMIT_VMEM
1880c80476e4SDavid E. O'Brien #   define RLIMIT_VMEM	6
1881c80476e4SDavid E. O'Brien #  endif
1882c80476e4SDavid E. O'Brien #  ifndef RLIMIT_AS
1883c80476e4SDavid E. O'Brien #   define RLIMIT_AS	RLIMIT_VMEM
1884c80476e4SDavid E. O'Brien #  endif
1885c80476e4SDavid E. O'Brien # endif /* SYSVREL > 3 && BSDLIMIT */
1886c80476e4SDavid E. O'Brien 
18879ccc37e3SMark Peek # if (defined(__linux__) || defined(__GNU__) || defined(__GLIBC__))
18889ccc37e3SMark Peek #  if defined(RLIMIT_AS) && !defined(RLIMIT_VMEM)
18896767bd61SMark Peek #   define RLIMIT_VMEM	RLIMIT_AS
18906767bd61SMark Peek #  endif
18919ccc37e3SMark Peek /*
18929ccc37e3SMark Peek  * Oh well, <asm-generic/resource.h> has it, but <bits/resource.h> does not
18939ccc37e3SMark Peek  * Linux headers: When the left hand does not know what the right hand does.
18949ccc37e3SMark Peek  */
18959ccc37e3SMark Peek #  if defined(RLIMIT_RTPRIO) && !defined(RLIMIT_RTTIME)
18969ccc37e3SMark Peek #   define RLIMIT_RTTIME (RLIMIT_RTPRIO + 1)
18979ccc37e3SMark Peek #  endif
18989ccc37e3SMark Peek # endif
18996767bd61SMark Peek 
1900c80476e4SDavid E. O'Brien struct limits limits[] =
1901c80476e4SDavid E. O'Brien {
1902c80476e4SDavid E. O'Brien # ifdef RLIMIT_CPU
1903c80476e4SDavid E. O'Brien     { RLIMIT_CPU, 	"cputime",	1,	"seconds"	},
1904c80476e4SDavid E. O'Brien # endif /* RLIMIT_CPU */
1905c80476e4SDavid E. O'Brien 
1906c80476e4SDavid E. O'Brien # ifdef RLIMIT_FSIZE
1907c80476e4SDavid E. O'Brien #  ifndef aiws
1908c80476e4SDavid E. O'Brien     { RLIMIT_FSIZE, 	"filesize",	1024,	"kbytes"	},
1909c80476e4SDavid E. O'Brien #  else
1910c80476e4SDavid E. O'Brien     { RLIMIT_FSIZE, 	"filesize",	512,	"blocks"	},
1911c80476e4SDavid E. O'Brien #  endif /* aiws */
1912c80476e4SDavid E. O'Brien # endif /* RLIMIT_FSIZE */
1913c80476e4SDavid E. O'Brien 
1914c80476e4SDavid E. O'Brien # ifdef RLIMIT_DATA
1915c80476e4SDavid E. O'Brien     { RLIMIT_DATA, 	"datasize",	1024,	"kbytes"	},
1916c80476e4SDavid E. O'Brien # endif /* RLIMIT_DATA */
1917c80476e4SDavid E. O'Brien 
1918c80476e4SDavid E. O'Brien # ifdef RLIMIT_STACK
1919c80476e4SDavid E. O'Brien #  ifndef aiws
1920c80476e4SDavid E. O'Brien     { RLIMIT_STACK, 	"stacksize",	1024,	"kbytes"	},
1921c80476e4SDavid E. O'Brien #  else
1922c80476e4SDavid E. O'Brien     { RLIMIT_STACK, 	"stacksize",	1024 * 1024,	"kbytes"},
1923c80476e4SDavid E. O'Brien #  endif /* aiws */
1924c80476e4SDavid E. O'Brien # endif /* RLIMIT_STACK */
1925c80476e4SDavid E. O'Brien 
1926c80476e4SDavid E. O'Brien # ifdef RLIMIT_CORE
1927c80476e4SDavid E. O'Brien     { RLIMIT_CORE, 	"coredumpsize",	1024,	"kbytes"	},
1928c80476e4SDavid E. O'Brien # endif /* RLIMIT_CORE */
1929c80476e4SDavid E. O'Brien 
1930c80476e4SDavid E. O'Brien # ifdef RLIMIT_RSS
1931c80476e4SDavid E. O'Brien     { RLIMIT_RSS, 	"memoryuse",	1024,	"kbytes"	},
1932c80476e4SDavid E. O'Brien # endif /* RLIMIT_RSS */
1933c80476e4SDavid E. O'Brien 
1934c80476e4SDavid E. O'Brien # ifdef RLIMIT_UMEM
1935c80476e4SDavid E. O'Brien     { RLIMIT_UMEM, 	"memoryuse",	1024,	"kbytes"	},
1936c80476e4SDavid E. O'Brien # endif /* RLIMIT_UMEM */
1937c80476e4SDavid E. O'Brien 
1938c80476e4SDavid E. O'Brien # ifdef RLIMIT_VMEM
1939c80476e4SDavid E. O'Brien     { RLIMIT_VMEM, 	"vmemoryuse",	1024,	"kbytes"	},
1940c80476e4SDavid E. O'Brien # endif /* RLIMIT_VMEM */
1941c80476e4SDavid E. O'Brien 
194223338178SMark Peek # if defined(RLIMIT_HEAP) /* found on BS2000/OSD systems */
194323338178SMark Peek     { RLIMIT_HEAP,	"heapsize",	1024,	"kbytes"	},
194423338178SMark Peek # endif /* RLIMIT_HEAP */
194523338178SMark Peek 
1946c80476e4SDavid E. O'Brien # ifdef RLIMIT_NOFILE
1947c80476e4SDavid E. O'Brien     { RLIMIT_NOFILE, 	"descriptors", 1,	""		},
1948c80476e4SDavid E. O'Brien # endif /* RLIMIT_NOFILE */
1949c80476e4SDavid E. O'Brien 
195019d2e3deSDmitry Chagin # ifdef RLIMIT_NPTS
195119d2e3deSDmitry Chagin     { RLIMIT_NPTS,	"pseudoterminals", 1,	""		},
195219d2e3deSDmitry Chagin # endif /* RLIMIT_NPTS */
195319d2e3deSDmitry Chagin 
195419d2e3deSDmitry Chagin # ifdef RLIMIT_KQUEUES
195519d2e3deSDmitry Chagin     { RLIMIT_KQUEUES,	"kqueues",	1,	""		},
195619d2e3deSDmitry Chagin # endif /* RLIMIT_KQUEUES */
195719d2e3deSDmitry Chagin 
1958c80476e4SDavid E. O'Brien # ifdef RLIMIT_CONCUR
1959c80476e4SDavid E. O'Brien     { RLIMIT_CONCUR, 	"concurrency", 1,	"thread(s)"	},
1960c80476e4SDavid E. O'Brien # endif /* RLIMIT_CONCUR */
1961c80476e4SDavid E. O'Brien 
1962c80476e4SDavid E. O'Brien # ifdef RLIMIT_MEMLOCK
1963c80476e4SDavid E. O'Brien     { RLIMIT_MEMLOCK,	"memorylocked",	1024,	"kbytes"	},
1964c80476e4SDavid E. O'Brien # endif /* RLIMIT_MEMLOCK */
1965c80476e4SDavid E. O'Brien 
1966c80476e4SDavid E. O'Brien # ifdef RLIMIT_NPROC
1967c80476e4SDavid E. O'Brien     { RLIMIT_NPROC,	"maxproc",	1,	""		},
1968c80476e4SDavid E. O'Brien # endif /* RLIMIT_NPROC */
1969c80476e4SDavid E. O'Brien 
197019d2e3deSDmitry Chagin # ifdef RLIMIT_NTHR
197119d2e3deSDmitry Chagin     { RLIMIT_NTHR,	"maxthread",	1,	""		},
197219d2e3deSDmitry Chagin # endif /* RLIMIT_NTHR */
197319d2e3deSDmitry Chagin 
197429301572SMark Peek # if defined(RLIMIT_OFILE) && !defined(RLIMIT_NOFILE)
1975c80476e4SDavid E. O'Brien     { RLIMIT_OFILE,	"openfiles",	1,	""		},
197629301572SMark Peek # endif /* RLIMIT_OFILE && !defined(RLIMIT_NOFILE) */
197729301572SMark Peek 
197829301572SMark Peek # ifdef RLIMIT_SBSIZE
197929301572SMark Peek     { RLIMIT_SBSIZE,	"sbsize",	1,	""		},
198029301572SMark Peek # endif /* RLIMIT_SBSIZE */
1981c80476e4SDavid E. O'Brien 
1982c9253e93SKonstantin Belousov # ifdef RLIMIT_SWAP
1983a15e6f9aSMark Peek     { RLIMIT_SWAP,	"swapsize",	1024,	"kbytes"	},
1984c9253e93SKonstantin Belousov # endif /* RLIMIT_SWAP */
1985c9253e93SKonstantin Belousov 
19869ccc37e3SMark Peek # ifdef RLIMIT_LOCKS
19879ccc37e3SMark Peek     { RLIMIT_LOCKS,	"maxlocks",	1,	""		},
19889ccc37e3SMark Peek # endif /* RLIMIT_LOCKS */
19899ccc37e3SMark Peek 
199019d2e3deSDmitry Chagin # ifdef RLIMIT_POSIXLOCKS
199119d2e3deSDmitry Chagin     { RLIMIT_POSIXLOCKS,"posixlocks",	1,	""		},
199219d2e3deSDmitry Chagin # endif /* RLIMIT_POSIXLOCKS */
199319d2e3deSDmitry Chagin 
19949ccc37e3SMark Peek # ifdef RLIMIT_SIGPENDING
19959ccc37e3SMark Peek     { RLIMIT_SIGPENDING,"maxsignal",	1,	""		},
19969ccc37e3SMark Peek # endif /* RLIMIT_SIGPENDING */
19979ccc37e3SMark Peek 
19989ccc37e3SMark Peek # ifdef RLIMIT_MSGQUEUE
19999ccc37e3SMark Peek     { RLIMIT_MSGQUEUE,	"maxmessage",	1,	""		},
20009ccc37e3SMark Peek # endif /* RLIMIT_MSGQUEUE */
20019ccc37e3SMark Peek 
20029ccc37e3SMark Peek # ifdef RLIMIT_NICE
20039ccc37e3SMark Peek     { RLIMIT_NICE,	"maxnice",	1,	""		},
20049ccc37e3SMark Peek # endif /* RLIMIT_NICE */
20059ccc37e3SMark Peek 
20069ccc37e3SMark Peek # ifdef RLIMIT_RTPRIO
20079ccc37e3SMark Peek     { RLIMIT_RTPRIO,	"maxrtprio",	1,	""		},
20089ccc37e3SMark Peek # endif /* RLIMIT_RTPRIO */
20099ccc37e3SMark Peek 
20109ccc37e3SMark Peek # ifdef RLIMIT_RTTIME
20119ccc37e3SMark Peek     { RLIMIT_RTTIME,	"maxrttime",	1,	"usec"		},
20129ccc37e3SMark Peek # endif /* RLIMIT_RTTIME */
20139ccc37e3SMark Peek 
2014c80476e4SDavid E. O'Brien     { -1, 		NULL, 		0, 	NULL		}
2015c80476e4SDavid E. O'Brien };
2016c80476e4SDavid E. O'Brien 
201745e5710bSMark Peek static struct limits *findlim	(Char *);
201845e5710bSMark Peek static RLIM_TYPE getval		(struct limits *, Char **);
20199ccc37e3SMark Peek static int strtail		(Char *, const char *);
202045e5710bSMark Peek static void limtail		(Char *, const char *);
20219ccc37e3SMark Peek static void limtail2		(Char *, const char *, const char *);
202245e5710bSMark Peek static void plim		(struct limits *, int);
202345e5710bSMark Peek static int setlim		(struct limits *, int, RLIM_TYPE);
2024c80476e4SDavid E. O'Brien 
2025c80476e4SDavid E. O'Brien #ifdef convex
2026c80476e4SDavid E. O'Brien static  RLIM_TYPE
restrict_limit(double value)202745e5710bSMark Peek restrict_limit(double value)
2028c80476e4SDavid E. O'Brien {
2029c80476e4SDavid E. O'Brien     /*
2030c80476e4SDavid E. O'Brien      * is f too large to cope with? return the maximum or minimum int
2031c80476e4SDavid E. O'Brien      */
2032c80476e4SDavid E. O'Brien     if (value > (double) INT_MAX)
2033c80476e4SDavid E. O'Brien 	return (RLIM_TYPE) INT_MAX;
2034c80476e4SDavid E. O'Brien     else if (value < (double) INT_MIN)
2035c80476e4SDavid E. O'Brien 	return (RLIM_TYPE) INT_MIN;
2036c80476e4SDavid E. O'Brien     else
2037c80476e4SDavid E. O'Brien 	return (RLIM_TYPE) value;
2038c80476e4SDavid E. O'Brien }
2039c80476e4SDavid E. O'Brien #else /* !convex */
2040c80476e4SDavid E. O'Brien # define restrict_limit(x)	((RLIM_TYPE) (x))
2041c80476e4SDavid E. O'Brien #endif /* convex */
2042c80476e4SDavid E. O'Brien 
2043c80476e4SDavid E. O'Brien 
2044c80476e4SDavid E. O'Brien static struct limits *
findlim(Char * cp)204545e5710bSMark Peek findlim(Char *cp)
2046c80476e4SDavid E. O'Brien {
204723338178SMark Peek     struct limits *lp, *res;
2048c80476e4SDavid E. O'Brien 
204945e5710bSMark Peek     res = NULL;
2050c80476e4SDavid E. O'Brien     for (lp = limits; lp->limconst >= 0; lp++)
2051c80476e4SDavid E. O'Brien 	if (prefix(cp, str2short(lp->limname))) {
2052c80476e4SDavid E. O'Brien 	    if (res)
2053c80476e4SDavid E. O'Brien 		stderror(ERR_NAME | ERR_AMBIG);
2054c80476e4SDavid E. O'Brien 	    res = lp;
2055c80476e4SDavid E. O'Brien 	}
2056c80476e4SDavid E. O'Brien     if (res)
2057c80476e4SDavid E. O'Brien 	return (res);
2058c80476e4SDavid E. O'Brien     stderror(ERR_NAME | ERR_LIMIT);
2059c80476e4SDavid E. O'Brien     /* NOTREACHED */
2060c80476e4SDavid E. O'Brien     return (0);
2061c80476e4SDavid E. O'Brien }
2062c80476e4SDavid E. O'Brien 
2063c80476e4SDavid E. O'Brien /*ARGSUSED*/
2064c80476e4SDavid E. O'Brien void
dolimit(Char ** v,struct command * c)206545e5710bSMark Peek dolimit(Char **v, struct command *c)
2066c80476e4SDavid E. O'Brien {
206723338178SMark Peek     struct limits *lp;
206823338178SMark Peek     RLIM_TYPE limit;
2069c80476e4SDavid E. O'Brien     int    hard = 0;
2070c80476e4SDavid E. O'Brien 
2071c80476e4SDavid E. O'Brien     USE(c);
2072c80476e4SDavid E. O'Brien     v++;
2073c80476e4SDavid E. O'Brien     if (*v && eq(*v, STRmh)) {
2074c80476e4SDavid E. O'Brien 	hard = 1;
2075c80476e4SDavid E. O'Brien 	v++;
2076c80476e4SDavid E. O'Brien     }
2077c80476e4SDavid E. O'Brien     if (*v == 0) {
2078c80476e4SDavid E. O'Brien 	for (lp = limits; lp->limconst >= 0; lp++)
2079c80476e4SDavid E. O'Brien 	    plim(lp, hard);
2080c80476e4SDavid E. O'Brien 	return;
2081c80476e4SDavid E. O'Brien     }
2082c80476e4SDavid E. O'Brien     lp = findlim(v[0]);
2083c80476e4SDavid E. O'Brien     if (v[1] == 0) {
2084c80476e4SDavid E. O'Brien 	plim(lp, hard);
2085c80476e4SDavid E. O'Brien 	return;
2086c80476e4SDavid E. O'Brien     }
2087c80476e4SDavid E. O'Brien     limit = getval(lp, v + 1);
2088c80476e4SDavid E. O'Brien     if (setlim(lp, hard, limit) < 0)
2089c80476e4SDavid E. O'Brien 	stderror(ERR_SILENT);
2090c80476e4SDavid E. O'Brien }
2091c80476e4SDavid E. O'Brien 
2092c80476e4SDavid E. O'Brien static  RLIM_TYPE
getval(struct limits * lp,Char ** v)209345e5710bSMark Peek getval(struct limits *lp, Char **v)
2094c80476e4SDavid E. O'Brien {
209523338178SMark Peek     float f;
2096c80476e4SDavid E. O'Brien     Char   *cp = *v++;
2097c80476e4SDavid E. O'Brien 
2098c80476e4SDavid E. O'Brien     f = atof(short2str(cp));
2099c80476e4SDavid E. O'Brien 
2100c80476e4SDavid E. O'Brien # ifdef convex
2101c80476e4SDavid E. O'Brien     /*
2102c80476e4SDavid E. O'Brien      * is f too large to cope with. limit f to minint, maxint  - X-6768 by
2103c80476e4SDavid E. O'Brien      * strike
2104c80476e4SDavid E. O'Brien      */
2105c80476e4SDavid E. O'Brien     if ((f < (double) INT_MIN) || (f > (double) INT_MAX)) {
2106c80476e4SDavid E. O'Brien 	stderror(ERR_NAME | ERR_TOOLARGE);
2107c80476e4SDavid E. O'Brien     }
2108c80476e4SDavid E. O'Brien # endif /* convex */
2109c80476e4SDavid E. O'Brien 
2110c80476e4SDavid E. O'Brien     while (Isdigit(*cp) || *cp == '.' || *cp == 'e' || *cp == 'E')
2111c80476e4SDavid E. O'Brien 	cp++;
2112c80476e4SDavid E. O'Brien     if (*cp == 0) {
2113c80476e4SDavid E. O'Brien 	if (*v == 0)
21143b6eaa7bSAndrey A. Chernov 	    return restrict_limit((f * lp->limdiv) + 0.5);
2115c80476e4SDavid E. O'Brien 	cp = *v;
2116c80476e4SDavid E. O'Brien     }
2117c80476e4SDavid E. O'Brien     switch (*cp) {
2118c80476e4SDavid E. O'Brien # ifdef RLIMIT_CPU
2119c80476e4SDavid E. O'Brien     case ':':
2120c80476e4SDavid E. O'Brien 	if (lp->limconst != RLIMIT_CPU)
2121c80476e4SDavid E. O'Brien 	    goto badscal;
2122c80476e4SDavid E. O'Brien 	return f == 0.0 ? (RLIM_TYPE) 0 : restrict_limit((f * 60.0 + atof(short2str(cp + 1))));
2123c80476e4SDavid E. O'Brien     case 'h':
2124c80476e4SDavid E. O'Brien 	if (lp->limconst != RLIMIT_CPU)
2125c80476e4SDavid E. O'Brien 	    goto badscal;
2126c80476e4SDavid E. O'Brien 	limtail(cp, "hours");
2127c80476e4SDavid E. O'Brien 	f *= 3600.0;
2128c80476e4SDavid E. O'Brien 	break;
21299ccc37e3SMark Peek # endif /* RLIMIT_CPU */
2130c80476e4SDavid E. O'Brien     case 'm':
21319ccc37e3SMark Peek # ifdef RLIMIT_CPU
2132c80476e4SDavid E. O'Brien 	if (lp->limconst == RLIMIT_CPU) {
2133c80476e4SDavid E. O'Brien 	    limtail(cp, "minutes");
2134c80476e4SDavid E. O'Brien 	    f *= 60.0;
2135c80476e4SDavid E. O'Brien 	    break;
2136c80476e4SDavid E. O'Brien 	}
21379ccc37e3SMark Peek # endif /* RLIMIT_CPU */
21389ccc37e3SMark Peek 	limtail2(cp, "megabytes", "mbytes");
2139c80476e4SDavid E. O'Brien 	f *= 1024.0 * 1024.0;
2140c80476e4SDavid E. O'Brien 	break;
21419ccc37e3SMark Peek # ifdef RLIMIT_CPU
2142c80476e4SDavid E. O'Brien     case 's':
2143c80476e4SDavid E. O'Brien 	if (lp->limconst != RLIMIT_CPU)
2144c80476e4SDavid E. O'Brien 	    goto badscal;
2145c80476e4SDavid E. O'Brien 	limtail(cp, "seconds");
2146c80476e4SDavid E. O'Brien 	break;
2147c80476e4SDavid E. O'Brien # endif /* RLIMIT_CPU */
21489ccc37e3SMark Peek     case 'G':
21499ccc37e3SMark Peek 	*cp = 'g';
21509ccc37e3SMark Peek 	/*FALLTHROUGH*/
21519ccc37e3SMark Peek     case 'g':
21529ccc37e3SMark Peek # ifdef RLIMIT_CPU
21539ccc37e3SMark Peek 	if (lp->limconst == RLIMIT_CPU)
21549ccc37e3SMark Peek 	    goto badscal;
21559ccc37e3SMark Peek # endif /* RLIMIT_CPU */
21569ccc37e3SMark Peek 	limtail2(cp, "gigabytes", "gbytes");
21579ccc37e3SMark Peek 	f *= 1024.0 * 1024.0 * 1024.0;
21589ccc37e3SMark Peek 	break;
2159c80476e4SDavid E. O'Brien     case 'M':
2160c80476e4SDavid E. O'Brien # ifdef RLIMIT_CPU
2161c80476e4SDavid E. O'Brien 	if (lp->limconst == RLIMIT_CPU)
2162c80476e4SDavid E. O'Brien 	    goto badscal;
2163c80476e4SDavid E. O'Brien # endif /* RLIMIT_CPU */
2164c80476e4SDavid E. O'Brien 	*cp = 'm';
21659ccc37e3SMark Peek 	limtail2(cp, "megabytes", "mbytes");
2166c80476e4SDavid E. O'Brien 	f *= 1024.0 * 1024.0;
2167c80476e4SDavid E. O'Brien 	break;
2168c80476e4SDavid E. O'Brien     case 'k':
2169c80476e4SDavid E. O'Brien # ifdef RLIMIT_CPU
2170c80476e4SDavid E. O'Brien 	if (lp->limconst == RLIMIT_CPU)
2171c80476e4SDavid E. O'Brien 	    goto badscal;
2172c80476e4SDavid E. O'Brien # endif /* RLIMIT_CPU */
21739ccc37e3SMark Peek 	limtail2(cp, "kilobytes", "kbytes");
2174c80476e4SDavid E. O'Brien 	f *= 1024.0;
2175c80476e4SDavid E. O'Brien 	break;
2176c80476e4SDavid E. O'Brien     case 'b':
2177c80476e4SDavid E. O'Brien # ifdef RLIMIT_CPU
2178c80476e4SDavid E. O'Brien 	if (lp->limconst == RLIMIT_CPU)
2179c80476e4SDavid E. O'Brien 	    goto badscal;
2180c80476e4SDavid E. O'Brien # endif /* RLIMIT_CPU */
2181c80476e4SDavid E. O'Brien 	limtail(cp, "blocks");
2182c80476e4SDavid E. O'Brien 	f *= 512.0;
2183c80476e4SDavid E. O'Brien 	break;
2184c80476e4SDavid E. O'Brien     case 'u':
2185c80476e4SDavid E. O'Brien 	limtail(cp, "unlimited");
2186c80476e4SDavid E. O'Brien 	return ((RLIM_TYPE) RLIM_INFINITY);
2187c80476e4SDavid E. O'Brien     default:
2188c80476e4SDavid E. O'Brien # ifdef RLIMIT_CPU
2189c80476e4SDavid E. O'Brien badscal:
2190c80476e4SDavid E. O'Brien # endif /* RLIMIT_CPU */
2191c80476e4SDavid E. O'Brien 	stderror(ERR_NAME | ERR_SCALEF);
2192c80476e4SDavid E. O'Brien     }
2193c80476e4SDavid E. O'Brien # ifdef convex
2194c80476e4SDavid E. O'Brien     return f == 0.0 ? (RLIM_TYPE) 0 : restrict_limit((f + 0.5));
2195c80476e4SDavid E. O'Brien # else
2196c80476e4SDavid E. O'Brien     f += 0.5;
21979ccc37e3SMark Peek     if (f > (float) ((RLIM_TYPE) RLIM_INFINITY))
2198c80476e4SDavid E. O'Brien 	return ((RLIM_TYPE) RLIM_INFINITY);
2199c80476e4SDavid E. O'Brien     else
2200c80476e4SDavid E. O'Brien 	return ((RLIM_TYPE) f);
2201c80476e4SDavid E. O'Brien # endif /* convex */
2202c80476e4SDavid E. O'Brien }
2203c80476e4SDavid E. O'Brien 
22049ccc37e3SMark Peek static int
strtail(Char * cp,const char * str)22059ccc37e3SMark Peek strtail(Char *cp, const char *str)
22069ccc37e3SMark Peek {
22079ccc37e3SMark Peek     while (*cp && *cp == (Char)*str)
22089ccc37e3SMark Peek 	cp++, str++;
22099ccc37e3SMark Peek     return (*cp != '\0');
22109ccc37e3SMark Peek }
22119ccc37e3SMark Peek 
2212c80476e4SDavid E. O'Brien static void
limtail(Char * cp,const char * str)221345e5710bSMark Peek limtail(Char *cp, const char *str)
2214c80476e4SDavid E. O'Brien {
22159ccc37e3SMark Peek     if (strtail(cp, str))
22169ccc37e3SMark Peek 	stderror(ERR_BADSCALE, str);
2217c80476e4SDavid E. O'Brien }
2218c80476e4SDavid E. O'Brien 
22199ccc37e3SMark Peek static void
limtail2(Char * cp,const char * str1,const char * str2)22209ccc37e3SMark Peek limtail2(Char *cp, const char *str1, const char *str2)
22219ccc37e3SMark Peek {
22229ccc37e3SMark Peek     if (strtail(cp, str1) && strtail(cp, str2))
22239ccc37e3SMark Peek 	stderror(ERR_BADSCALE, str1);
22249ccc37e3SMark Peek }
2225c80476e4SDavid E. O'Brien 
2226c80476e4SDavid E. O'Brien /*ARGSUSED*/
2227c80476e4SDavid E. O'Brien static void
plim(struct limits * lp,int hard)222845e5710bSMark Peek plim(struct limits *lp, int hard)
2229c80476e4SDavid E. O'Brien {
2230c80476e4SDavid E. O'Brien # ifdef BSDLIMIT
2231c80476e4SDavid E. O'Brien     struct rlimit rlim;
2232c80476e4SDavid E. O'Brien # endif /* BSDLIMIT */
2233c80476e4SDavid E. O'Brien     RLIM_TYPE limit;
223423338178SMark Peek     int     xdiv = lp->limdiv;
2235c80476e4SDavid E. O'Brien 
2236b2d5d167SMark Peek     xprintf("%-13.13s", lp->limname);
2237c80476e4SDavid E. O'Brien 
2238c80476e4SDavid E. O'Brien # ifndef BSDLIMIT
2239c80476e4SDavid E. O'Brien     limit = ulimit(lp->limconst, 0);
2240c80476e4SDavid E. O'Brien #  ifdef aiws
2241c80476e4SDavid E. O'Brien     if (lp->limconst == RLIMIT_DATA)
2242c80476e4SDavid E. O'Brien 	limit -= 0x20000000;
2243c80476e4SDavid E. O'Brien #  endif /* aiws */
2244c80476e4SDavid E. O'Brien # else /* BSDLIMIT */
2245c80476e4SDavid E. O'Brien     (void) getrlimit(lp->limconst, &rlim);
2246c80476e4SDavid E. O'Brien     limit = hard ? rlim.rlim_max : rlim.rlim_cur;
2247c80476e4SDavid E. O'Brien # endif /* BSDLIMIT */
2248c80476e4SDavid E. O'Brien 
2249c80476e4SDavid E. O'Brien # if !defined(BSDLIMIT) || defined(FILESIZE512)
2250c80476e4SDavid E. O'Brien     /*
2251c80476e4SDavid E. O'Brien      * Christos: filesize comes in 512 blocks. we divide by 2 to get 1024
2252c80476e4SDavid E. O'Brien      * blocks. Note we cannot pre-multiply cause we might overflow (A/UX)
2253c80476e4SDavid E. O'Brien      */
2254c80476e4SDavid E. O'Brien     if (lp->limconst == RLIMIT_FSIZE) {
2255c80476e4SDavid E. O'Brien 	if (limit >= (RLIM_INFINITY / 512))
2256c80476e4SDavid E. O'Brien 	    limit = RLIM_INFINITY;
2257c80476e4SDavid E. O'Brien 	else
225823338178SMark Peek 	    xdiv = (xdiv == 1024 ? 2 : 1);
2259c80476e4SDavid E. O'Brien     }
2260c80476e4SDavid E. O'Brien # endif /* !BSDLIMIT || FILESIZE512 */
2261c80476e4SDavid E. O'Brien 
2262c80476e4SDavid E. O'Brien     if (limit == RLIM_INFINITY)
2263c80476e4SDavid E. O'Brien 	xprintf("unlimited");
2264c80476e4SDavid E. O'Brien     else
226523338178SMark Peek # if defined(RLIMIT_CPU) && defined(_OSD_POSIX)
226623338178SMark Peek     if (lp->limconst == RLIMIT_CPU &&
226723338178SMark Peek         (unsigned long)limit >= 0x7ffffffdUL)
226823338178SMark Peek 	xprintf("unlimited");
226923338178SMark Peek     else
227023338178SMark Peek # endif
2271c80476e4SDavid E. O'Brien # ifdef RLIMIT_CPU
2272c80476e4SDavid E. O'Brien     if (lp->limconst == RLIMIT_CPU)
227345e5710bSMark Peek 	psecs(limit);
2274c80476e4SDavid E. O'Brien     else
2275c80476e4SDavid E. O'Brien # endif /* RLIMIT_CPU */
227623338178SMark Peek 	xprintf("%ld %s", (long) (limit / xdiv), lp->limscale);
2277c80476e4SDavid E. O'Brien     xputchar('\n');
2278c80476e4SDavid E. O'Brien }
2279c80476e4SDavid E. O'Brien 
2280c80476e4SDavid E. O'Brien /*ARGSUSED*/
2281c80476e4SDavid E. O'Brien void
dounlimit(Char ** v,struct command * c)228245e5710bSMark Peek dounlimit(Char **v, struct command *c)
2283c80476e4SDavid E. O'Brien {
228423338178SMark Peek     struct limits *lp;
2285c80476e4SDavid E. O'Brien     int    lerr = 0;
2286c80476e4SDavid E. O'Brien     int    hard = 0;
2287c80476e4SDavid E. O'Brien     int	   force = 0;
2288c80476e4SDavid E. O'Brien 
2289c80476e4SDavid E. O'Brien     USE(c);
2290c80476e4SDavid E. O'Brien     while (*++v && **v == '-') {
2291c80476e4SDavid E. O'Brien 	Char   *vp = *v;
2292c80476e4SDavid E. O'Brien 	while (*++vp)
2293c80476e4SDavid E. O'Brien 	    switch (*vp) {
2294c80476e4SDavid E. O'Brien 	    case 'f':
2295c80476e4SDavid E. O'Brien 		force = 1;
2296c80476e4SDavid E. O'Brien 		break;
2297c80476e4SDavid E. O'Brien 	    case 'h':
2298c80476e4SDavid E. O'Brien 		hard = 1;
2299c80476e4SDavid E. O'Brien 		break;
2300c80476e4SDavid E. O'Brien 	    default:
2301c80476e4SDavid E. O'Brien 		stderror(ERR_ULIMUS);
2302c80476e4SDavid E. O'Brien 		break;
2303c80476e4SDavid E. O'Brien 	    }
2304c80476e4SDavid E. O'Brien     }
2305c80476e4SDavid E. O'Brien 
2306c80476e4SDavid E. O'Brien     if (*v == 0) {
2307c80476e4SDavid E. O'Brien 	for (lp = limits; lp->limconst >= 0; lp++)
2308c80476e4SDavid E. O'Brien 	    if (setlim(lp, hard, (RLIM_TYPE) RLIM_INFINITY) < 0)
2309c80476e4SDavid E. O'Brien 		lerr++;
2310c80476e4SDavid E. O'Brien 	if (!force && lerr)
2311c80476e4SDavid E. O'Brien 	    stderror(ERR_SILENT);
2312c80476e4SDavid E. O'Brien 	return;
2313c80476e4SDavid E. O'Brien     }
2314c80476e4SDavid E. O'Brien     while (*v) {
2315c80476e4SDavid E. O'Brien 	lp = findlim(*v++);
2316c80476e4SDavid E. O'Brien 	if (setlim(lp, hard, (RLIM_TYPE) RLIM_INFINITY) < 0 && !force)
2317c80476e4SDavid E. O'Brien 	    stderror(ERR_SILENT);
2318c80476e4SDavid E. O'Brien     }
2319c80476e4SDavid E. O'Brien }
2320c80476e4SDavid E. O'Brien 
2321c80476e4SDavid E. O'Brien static int
setlim(struct limits * lp,int hard,RLIM_TYPE limit)232245e5710bSMark Peek setlim(struct limits *lp, int hard, RLIM_TYPE limit)
2323c80476e4SDavid E. O'Brien {
2324c80476e4SDavid E. O'Brien # ifdef BSDLIMIT
2325c80476e4SDavid E. O'Brien     struct rlimit rlim;
2326c80476e4SDavid E. O'Brien 
2327c80476e4SDavid E. O'Brien     (void) getrlimit(lp->limconst, &rlim);
2328c80476e4SDavid E. O'Brien 
2329c80476e4SDavid E. O'Brien #  ifdef FILESIZE512
2330c80476e4SDavid E. O'Brien     /* Even though hpux has setrlimit(), it expects fsize in 512 byte blocks */
2331c80476e4SDavid E. O'Brien     if (limit != RLIM_INFINITY && lp->limconst == RLIMIT_FSIZE)
2332c80476e4SDavid E. O'Brien 	limit /= 512;
2333c80476e4SDavid E. O'Brien #  endif /* FILESIZE512 */
2334c80476e4SDavid E. O'Brien     if (hard)
2335c80476e4SDavid E. O'Brien 	rlim.rlim_max = limit;
2336c80476e4SDavid E. O'Brien     else if (limit == RLIM_INFINITY && euid != 0)
2337c80476e4SDavid E. O'Brien 	rlim.rlim_cur = rlim.rlim_max;
2338c80476e4SDavid E. O'Brien     else
2339c80476e4SDavid E. O'Brien 	rlim.rlim_cur = limit;
2340c80476e4SDavid E. O'Brien 
234129301572SMark Peek     if (rlim.rlim_cur > rlim.rlim_max)
234229301572SMark Peek 	rlim.rlim_max = rlim.rlim_cur;
234329301572SMark Peek 
2344c80476e4SDavid E. O'Brien     if (setrlimit(lp->limconst, &rlim) < 0) {
2345c80476e4SDavid E. O'Brien # else /* BSDLIMIT */
2346c80476e4SDavid E. O'Brien     if (limit != RLIM_INFINITY && lp->limconst == RLIMIT_FSIZE)
2347c80476e4SDavid E. O'Brien 	limit /= 512;
2348c80476e4SDavid E. O'Brien # ifdef aiws
2349c80476e4SDavid E. O'Brien     if (lp->limconst == RLIMIT_DATA)
2350c80476e4SDavid E. O'Brien 	limit += 0x20000000;
2351c80476e4SDavid E. O'Brien # endif /* aiws */
2352c80476e4SDavid E. O'Brien     if (ulimit(toset(lp->limconst), limit) < 0) {
2353c80476e4SDavid E. O'Brien # endif /* BSDLIMIT */
235423338178SMark Peek         int err;
235523338178SMark Peek         char *op, *type;
235623338178SMark Peek 
235723338178SMark Peek 	err = errno;
235823338178SMark Peek 	op = strsave(limit == RLIM_INFINITY ? CGETS(15, 2, "remove") :
235923338178SMark Peek 		     	CGETS(15, 3, "set"));
236045e5710bSMark Peek 	cleanup_push(op, xfree);
236123338178SMark Peek 	type = strsave(hard ? CGETS(15, 4, " hard") : "");
236245e5710bSMark Peek 	cleanup_push(type, xfree);
236329301572SMark Peek 	xprintf(CGETS(15, 1, "%s: %s: Can't %s%s limit (%s)\n"), bname,
236423338178SMark Peek 	    lp->limname, op, type, strerror(err));
236545e5710bSMark Peek 	cleanup_until(op);
2366c80476e4SDavid E. O'Brien 	return (-1);
2367c80476e4SDavid E. O'Brien     }
2368c80476e4SDavid E. O'Brien     return (0);
2369c80476e4SDavid E. O'Brien }
2370c80476e4SDavid E. O'Brien 
2371c80476e4SDavid E. O'Brien #endif /* !HAVENOLIMIT */
2372c80476e4SDavid E. O'Brien 
2373c80476e4SDavid E. O'Brien /*ARGSUSED*/
2374c80476e4SDavid E. O'Brien void
237545e5710bSMark Peek dosuspend(Char **v, struct command *c)
2376c80476e4SDavid E. O'Brien {
2377c80476e4SDavid E. O'Brien #ifdef BSDJOBS
237845e5710bSMark Peek     struct sigaction old;
2379c80476e4SDavid E. O'Brien #endif /* BSDJOBS */
2380c80476e4SDavid E. O'Brien 
2381c80476e4SDavid E. O'Brien     USE(c);
2382c80476e4SDavid E. O'Brien     USE(v);
2383c80476e4SDavid E. O'Brien 
2384c80476e4SDavid E. O'Brien     if (loginsh)
2385c80476e4SDavid E. O'Brien 	stderror(ERR_SUSPLOG);
2386c80476e4SDavid E. O'Brien     untty();
2387c80476e4SDavid E. O'Brien 
2388c80476e4SDavid E. O'Brien #ifdef BSDJOBS
238945e5710bSMark Peek     sigaction(SIGTSTP, NULL, &old);
239045e5710bSMark Peek     signal(SIGTSTP, SIG_DFL);
2391c80476e4SDavid E. O'Brien     (void) kill(0, SIGTSTP);
2392c80476e4SDavid E. O'Brien     /* the shell stops here */
239345e5710bSMark Peek     sigaction(SIGTSTP, &old, NULL);
2394c80476e4SDavid E. O'Brien #else /* !BSDJOBS */
2395c80476e4SDavid E. O'Brien     stderror(ERR_JOBCONTROL);
2396c80476e4SDavid E. O'Brien #endif /* BSDJOBS */
2397c80476e4SDavid E. O'Brien 
2398c80476e4SDavid E. O'Brien #ifdef BSDJOBS
2399c80476e4SDavid E. O'Brien     if (tpgrp != -1) {
2400a15e6f9aSMark Peek 	if (grabpgrp(FSHTTY, opgrp) == -1)
2401b2d5d167SMark Peek 	    stderror(ERR_SYSTEM, "tcgetpgrp", strerror(errno));
2402c80476e4SDavid E. O'Brien 	(void) setpgid(0, shpgrp);
2403c80476e4SDavid E. O'Brien 	(void) tcsetpgrp(FSHTTY, shpgrp);
2404c80476e4SDavid E. O'Brien     }
2405c80476e4SDavid E. O'Brien #endif /* BSDJOBS */
2406c80476e4SDavid E. O'Brien     (void) setdisc(FSHTTY);
2407c80476e4SDavid E. O'Brien }
2408c80476e4SDavid E. O'Brien 
2409c80476e4SDavid E. O'Brien /* This is the dreaded EVAL built-in.
2410c80476e4SDavid E. O'Brien  *   If you don't fiddle with file descriptors, and reset didfds,
2411c80476e4SDavid E. O'Brien  *   this command will either ignore redirection inside or outside
2412c80476e4SDavid E. O'Brien  *   its arguments, e.g. eval "date >x"  vs.  eval "date" >x
2413c80476e4SDavid E. O'Brien  *   The stuff here seems to work, but I did it by trial and error rather
2414c80476e4SDavid E. O'Brien  *   than really knowing what was going on.  If tpgrp is zero, we are
2415c80476e4SDavid E. O'Brien  *   probably a background eval, e.g. "eval date &", and we want to
2416c80476e4SDavid E. O'Brien  *   make sure that any processes we start stay in our pgrp.
2417c80476e4SDavid E. O'Brien  *   This is also the case for "time eval date" -- stay in same pgrp.
2418c80476e4SDavid E. O'Brien  *   Otherwise, under stty tostop, processes will stop in the wrong
2419c80476e4SDavid E. O'Brien  *   pgrp, with no way for the shell to get them going again.  -IAN!
2420c80476e4SDavid E. O'Brien  */
2421c80476e4SDavid E. O'Brien 
242245e5710bSMark Peek struct doeval_state
242345e5710bSMark Peek {
242445e5710bSMark Peek     Char **evalvec, *evalp;
242545e5710bSMark Peek     int didfds;
242645e5710bSMark Peek #ifndef CLOSE_ON_EXEC
242745e5710bSMark Peek     int didcch;
242845e5710bSMark Peek #endif
242945e5710bSMark Peek     int saveIN, saveOUT, saveDIAG;
243045e5710bSMark Peek     int SHIN, SHOUT, SHDIAG;
243145e5710bSMark Peek };
243245e5710bSMark Peek 
243345e5710bSMark Peek static void
243445e5710bSMark Peek doeval_cleanup(void *xstate)
243545e5710bSMark Peek {
243645e5710bSMark Peek     struct doeval_state *state;
243745e5710bSMark Peek 
243845e5710bSMark Peek     state = xstate;
243945e5710bSMark Peek     evalvec = state->evalvec;
244045e5710bSMark Peek     evalp = state->evalp;
244145e5710bSMark Peek     doneinp = 0;
244245e5710bSMark Peek #ifndef CLOSE_ON_EXEC
244345e5710bSMark Peek     didcch = state->didcch;
244445e5710bSMark Peek #endif /* CLOSE_ON_EXEC */
244545e5710bSMark Peek     didfds = state->didfds;
244619d2e3deSDmitry Chagin     if (state->saveIN != SHIN)
244745e5710bSMark Peek 	xclose(SHIN);
244819d2e3deSDmitry Chagin     if (state->saveOUT != SHOUT)
244945e5710bSMark Peek 	xclose(SHOUT);
245019d2e3deSDmitry Chagin     if (state->saveDIAG != SHDIAG)
245145e5710bSMark Peek 	xclose(SHDIAG);
245245e5710bSMark Peek     close_on_exec(SHIN = dmove(state->saveIN, state->SHIN), 1);
245345e5710bSMark Peek     close_on_exec(SHOUT = dmove(state->saveOUT, state->SHOUT), 1);
245445e5710bSMark Peek     close_on_exec(SHDIAG = dmove(state->saveDIAG, state->SHDIAG), 1);
245519d2e3deSDmitry Chagin     if (didfds) {
245619d2e3deSDmitry Chagin 	close_on_exec(dcopy(SHIN, 0), 1);
245719d2e3deSDmitry Chagin 	close_on_exec(dcopy(SHOUT, 1), 1);
245819d2e3deSDmitry Chagin 	close_on_exec(dcopy(SHDIAG, 2), 1);
245919d2e3deSDmitry Chagin     }
246045e5710bSMark Peek }
2461c80476e4SDavid E. O'Brien 
2462a15e6f9aSMark Peek static Char **Ggv;
2463c80476e4SDavid E. O'Brien /*ARGSUSED*/
2464c80476e4SDavid E. O'Brien void
246545e5710bSMark Peek doeval(Char **v, struct command *c)
2466c80476e4SDavid E. O'Brien {
246745e5710bSMark Peek     struct doeval_state state;
2468a15e6f9aSMark Peek     int gflag, my_reenter;
246945e5710bSMark Peek     Char **gv;
2470a15e6f9aSMark Peek     jmp_buf_t osetexit;
2471c80476e4SDavid E. O'Brien 
2472c80476e4SDavid E. O'Brien     USE(c);
247345e5710bSMark Peek     v++;
247445e5710bSMark Peek     if (*v == 0)
2475c80476e4SDavid E. O'Brien 	return;
247645e5710bSMark Peek     gflag = tglob(v);
2477c80476e4SDavid E. O'Brien     if (gflag) {
247845e5710bSMark Peek 	gv = v = globall(v, gflag);
247945e5710bSMark Peek 	if (v == 0)
2480c80476e4SDavid E. O'Brien 	    stderror(ERR_NOMATCH);
248145e5710bSMark Peek 	cleanup_push(gv, blk_cleanup);
248245e5710bSMark Peek 	v = copyblk(v);
2483c80476e4SDavid E. O'Brien     }
2484c80476e4SDavid E. O'Brien     else {
2485c80476e4SDavid E. O'Brien 	gv = NULL;
248645e5710bSMark Peek 	v = copyblk(v);
248745e5710bSMark Peek 	trim(v);
2488c80476e4SDavid E. O'Brien     }
2489c80476e4SDavid E. O'Brien 
2490a15e6f9aSMark Peek     Ggv = gv;
249145e5710bSMark Peek     state.evalvec = evalvec;
249245e5710bSMark Peek     state.evalp = evalp;
249345e5710bSMark Peek     state.didfds = didfds;
249445e5710bSMark Peek #ifndef CLOSE_ON_EXEC
249545e5710bSMark Peek     state.didcch = didcch;
249645e5710bSMark Peek #endif /* CLOSE_ON_EXEC */
249745e5710bSMark Peek     state.SHIN = SHIN;
249845e5710bSMark Peek     state.SHOUT = SHOUT;
249945e5710bSMark Peek     state.SHDIAG = SHDIAG;
2500c80476e4SDavid E. O'Brien 
250145e5710bSMark Peek     (void)close_on_exec(state.saveIN = dcopy(SHIN, -1), 1);
250245e5710bSMark Peek     (void)close_on_exec(state.saveOUT = dcopy(SHOUT, -1), 1);
250345e5710bSMark Peek     (void)close_on_exec(state.saveDIAG = dcopy(SHDIAG, -1), 1);
2504c80476e4SDavid E. O'Brien 
250545e5710bSMark Peek     cleanup_push(&state, doeval_cleanup);
250645e5710bSMark Peek 
2507a15e6f9aSMark Peek     getexit(osetexit);
2508a15e6f9aSMark Peek 
2509a15e6f9aSMark Peek     /* PWP: setjmp/longjmp bugfix for optimizing compilers */
2510a15e6f9aSMark Peek #ifdef cray
2511a15e6f9aSMark Peek     my_reenter = 1;             /* assume non-zero return val */
2512a15e6f9aSMark Peek     if (setexit() == 0) {
2513a15e6f9aSMark Peek 	my_reenter = 0;         /* Oh well, we were wrong */
2514a15e6f9aSMark Peek #else /* !cray */
2515a15e6f9aSMark Peek     if ((my_reenter = setexit()) == 0) {
2516a15e6f9aSMark Peek #endif /* cray */
251745e5710bSMark Peek 	evalvec = v;
2518c80476e4SDavid E. O'Brien 	evalp = 0;
251923338178SMark Peek 	(void)close_on_exec(SHIN = dcopy(0, -1), 1);
252023338178SMark Peek 	(void)close_on_exec(SHOUT = dcopy(1, -1), 1);
252123338178SMark Peek 	(void)close_on_exec(SHDIAG = dcopy(2, -1), 1);
2522c80476e4SDavid E. O'Brien #ifndef CLOSE_ON_EXEC
2523c80476e4SDavid E. O'Brien 	didcch = 0;
2524c80476e4SDavid E. O'Brien #endif /* CLOSE_ON_EXEC */
2525c80476e4SDavid E. O'Brien 	didfds = 0;
2526a15e6f9aSMark Peek 	gv = Ggv;
2527c80476e4SDavid E. O'Brien 	process(0);
2528a15e6f9aSMark Peek 	Ggv = gv;
2529a15e6f9aSMark Peek     }
2530c80476e4SDavid E. O'Brien 
2531a15e6f9aSMark Peek     if (my_reenter == 0) {
253245e5710bSMark Peek 	cleanup_until(&state);
2533a15e6f9aSMark Peek 	if (Ggv)
2534a15e6f9aSMark Peek 	    cleanup_until(Ggv);
2535a15e6f9aSMark Peek     }
2536c80476e4SDavid E. O'Brien 
2537a15e6f9aSMark Peek     resexit(osetexit);
2538a15e6f9aSMark Peek     if (my_reenter)
2539a15e6f9aSMark Peek 	stderror(ERR_SILENT);
2540c80476e4SDavid E. O'Brien }
2541c80476e4SDavid E. O'Brien 
2542c80476e4SDavid E. O'Brien /*************************************************************************/
2543c80476e4SDavid E. O'Brien /* print list of builtin commands */
2544c80476e4SDavid E. O'Brien 
254545e5710bSMark Peek static void
254645e5710bSMark Peek lbuffed_cleanup (void *dummy)
254745e5710bSMark Peek {
254845e5710bSMark Peek     USE(dummy);
254945e5710bSMark Peek     lbuffed = 1;
255045e5710bSMark Peek }
255145e5710bSMark Peek 
2552c80476e4SDavid E. O'Brien /*ARGSUSED*/
2553c80476e4SDavid E. O'Brien void
255445e5710bSMark Peek dobuiltins(Char **v, struct command *c)
2555c80476e4SDavid E. O'Brien {
2556c80476e4SDavid E. O'Brien     /* would use print_by_column() in tw.parse.c but that assumes
2557c80476e4SDavid E. O'Brien      * we have an array of Char * to pass.. (sg)
2558c80476e4SDavid E. O'Brien      */
255945e5710bSMark Peek     const struct biltins *b;
256023338178SMark Peek     int row, col, columns, rows;
2561c80476e4SDavid E. O'Brien     unsigned int w, maxwidth;
2562c80476e4SDavid E. O'Brien 
2563c80476e4SDavid E. O'Brien     USE(c);
2564c80476e4SDavid E. O'Brien     USE(v);
2565c80476e4SDavid E. O'Brien     lbuffed = 0;		/* turn off line buffering */
256645e5710bSMark Peek     cleanup_push(&lbuffed, lbuffed_cleanup);
2567c80476e4SDavid E. O'Brien 
2568c80476e4SDavid E. O'Brien     /* find widest string */
2569c80476e4SDavid E. O'Brien     for (maxwidth = 0, b = bfunc; b < &bfunc[nbfunc]; ++b)
2570c80476e4SDavid E. O'Brien 	maxwidth = max(maxwidth, strlen(b->bname));
2571c80476e4SDavid E. O'Brien     ++maxwidth;					/* for space */
2572c80476e4SDavid E. O'Brien 
2573c80476e4SDavid E. O'Brien     columns = (TermH + 1) / maxwidth;	/* PWP: terminal size change */
2574c80476e4SDavid E. O'Brien     if (!columns)
2575c80476e4SDavid E. O'Brien 	columns = 1;
2576c80476e4SDavid E. O'Brien     rows = (nbfunc + (columns - 1)) / columns;
2577c80476e4SDavid E. O'Brien 
2578c80476e4SDavid E. O'Brien     for (b = bfunc, row = 0; row < rows; row++) {
2579c80476e4SDavid E. O'Brien 	for (col = 0; col < columns; col++) {
2580c80476e4SDavid E. O'Brien 	    if (b < &bfunc[nbfunc]) {
2581c80476e4SDavid E. O'Brien 		w = strlen(b->bname);
2582c80476e4SDavid E. O'Brien 		xprintf("%s", b->bname);
2583c80476e4SDavid E. O'Brien 		if (col < (columns - 1))	/* Not last column? */
2584c80476e4SDavid E. O'Brien 		    for (; w < maxwidth; w++)
2585c80476e4SDavid E. O'Brien 			xputchar(' ');
2586c80476e4SDavid E. O'Brien 		++b;
2587c80476e4SDavid E. O'Brien 	    }
2588c80476e4SDavid E. O'Brien 	}
2589c80476e4SDavid E. O'Brien 	if (row < (rows - 1)) {
2590c80476e4SDavid E. O'Brien 	    if (Tty_raw_mode)
2591c80476e4SDavid E. O'Brien 		xputchar('\r');
2592c80476e4SDavid E. O'Brien 	    xputchar('\n');
2593c80476e4SDavid E. O'Brien 	}
2594c80476e4SDavid E. O'Brien     }
25953b6eaa7bSAndrey A. Chernov #ifdef WINNT_NATIVE
2596c80476e4SDavid E. O'Brien     nt_print_builtins(maxwidth);
2597c80476e4SDavid E. O'Brien #else
2598c80476e4SDavid E. O'Brien     if (Tty_raw_mode)
2599c80476e4SDavid E. O'Brien 	xputchar('\r');
2600c80476e4SDavid E. O'Brien     xputchar('\n');
26013b6eaa7bSAndrey A. Chernov #endif /* WINNT_NATIVE */
2602c80476e4SDavid E. O'Brien 
260345e5710bSMark Peek     cleanup_until(&lbuffed);		/* turn back on line buffering */
2604c80476e4SDavid E. O'Brien     flush();
2605c80476e4SDavid E. O'Brien }
2606c80476e4SDavid E. O'Brien 
260723338178SMark Peek #ifdef NLS_CATALOGS
260823338178SMark Peek char *
260945e5710bSMark Peek xcatgets(nl_catd ctd, int set_id, int msg_id, const char *s)
261045e5710bSMark Peek {
261145e5710bSMark Peek     char *res;
261245e5710bSMark Peek 
261345e5710bSMark Peek     errno = 0;
261445e5710bSMark Peek     while ((res = catgets(ctd, set_id, msg_id, s)) == s && errno == EINTR) {
261545e5710bSMark Peek 	handle_pending_signals();
261645e5710bSMark Peek 	errno = 0;
261745e5710bSMark Peek     }
261845e5710bSMark Peek     return res;
261945e5710bSMark Peek }
262045e5710bSMark Peek 
262145e5710bSMark Peek 
262245e5710bSMark Peek # if defined(HAVE_ICONV) && defined(HAVE_NL_LANGINFO)
262345e5710bSMark Peek char *
262445e5710bSMark Peek iconv_catgets(nl_catd ctd, int set_id, int msg_id, const char *s)
262523338178SMark Peek {
262623338178SMark Peek     static char *buf = NULL;
262723338178SMark Peek     static size_t buf_size = 0;
262823338178SMark Peek 
262923338178SMark Peek     char *orig, *dest, *p;
263045e5710bSMark Peek     ICONV_CONST char *src;
263123338178SMark Peek     size_t src_size, dest_size;
263223338178SMark Peek 
263345e5710bSMark Peek     orig = xcatgets(ctd, set_id, msg_id, s);
263423338178SMark Peek     if (catgets_iconv == (iconv_t)-1 || orig == s)
263523338178SMark Peek         return orig;
263623338178SMark Peek     src = orig;
263723338178SMark Peek     src_size = strlen(src) + 1;
263823338178SMark Peek     if (buf == NULL && (buf = xmalloc(buf_size = src_size + 32)) == NULL)
263923338178SMark Peek 	return orig;
264023338178SMark Peek     dest = buf;
264123338178SMark Peek     while (src_size != 0) {
264223338178SMark Peek         dest_size = buf + buf_size - dest;
264323338178SMark Peek 	if (iconv(catgets_iconv, &src, &src_size, &dest, &dest_size)
264423338178SMark Peek 	    == (size_t)-1) {
264523338178SMark Peek 	    switch (errno) {
264623338178SMark Peek 	    case E2BIG:
264723338178SMark Peek 		if ((p = xrealloc(buf, buf_size * 2)) == NULL)
264823338178SMark Peek 		    return orig;
264923338178SMark Peek 		buf_size *= 2;
265023338178SMark Peek 		dest = p + (dest - buf);
265123338178SMark Peek 		buf = p;
265223338178SMark Peek 		break;
265323338178SMark Peek 
265423338178SMark Peek 	    case EILSEQ: case EINVAL: default:
265523338178SMark Peek 		return orig;
265623338178SMark Peek 	    }
265723338178SMark Peek 	}
265823338178SMark Peek     }
265923338178SMark Peek     return buf;
266023338178SMark Peek }
266145e5710bSMark Peek # endif /* HAVE_ICONV && HAVE_NL_LANGINFO */
266245e5710bSMark Peek #endif /* NLS_CATALOGS */
266323338178SMark Peek 
2664c80476e4SDavid E. O'Brien void
266545e5710bSMark Peek nlsinit(void)
2666c80476e4SDavid E. O'Brien {
2667c80476e4SDavid E. O'Brien #ifdef NLS_CATALOGS
266845e5710bSMark Peek     static const char default_catalog[] = "tcsh";
266945e5710bSMark Peek 
267045e5710bSMark Peek     char *catalog = (char *)(intptr_t)default_catalog;
26713b6eaa7bSAndrey A. Chernov 
26723b6eaa7bSAndrey A. Chernov     if (adrof(STRcatalog) != NULL)
267345e5710bSMark Peek 	catalog = xasprintf("tcsh.%s", short2str(varval(STRcatalog)));
26749ccc37e3SMark Peek #ifdef NL_CAT_LOCALE /* POSIX-compliant. */
26759ccc37e3SMark Peek     /*
26769ccc37e3SMark Peek      * Check if LC_MESSAGES is set in the environment and use it, if so.
26779ccc37e3SMark Peek      * If not, fall back to the setting of LANG.
26789ccc37e3SMark Peek      */
26799ccc37e3SMark Peek     catd = catopen(catalog, tgetenv(STRLC_MESSAGES) ? NL_CAT_LOCALE : 0);
26809ccc37e3SMark Peek #else /* pre-POSIX */
26819ccc37e3SMark Peek # ifndef MCLoadBySet
26829ccc37e3SMark Peek #  define MCLoadBySet 0
26839ccc37e3SMark Peek #  endif
26843b6eaa7bSAndrey A. Chernov     catd = catopen(catalog, MCLoadBySet);
26859ccc37e3SMark Peek #endif
268645e5710bSMark Peek     if (catalog != default_catalog)
268745e5710bSMark Peek 	xfree(catalog);
268845e5710bSMark Peek #if defined(HAVE_ICONV) && defined(HAVE_NL_LANGINFO)
268945e5710bSMark Peek     /* xcatgets (), not CGETS, the charset name should be in ASCII anyway. */
269023338178SMark Peek     catgets_iconv = iconv_open (nl_langinfo (CODESET),
26919ccc37e3SMark Peek 				xcatgets(catd, 255, 1, "UTF-8"));
269245e5710bSMark Peek #endif /* HAVE_ICONV && HAVE_NL_LANGINFO */
26933b6eaa7bSAndrey A. Chernov #endif /* NLS_CATALOGS */
26943b6eaa7bSAndrey A. Chernov #ifdef WINNT_NATIVE
2695c80476e4SDavid E. O'Brien     nls_dll_init();
26963b6eaa7bSAndrey A. Chernov #endif /* WINNT_NATIVE */
2697c80476e4SDavid E. O'Brien     errinit();		/* init the errorlist in correct locale */
2698c80476e4SDavid E. O'Brien     mesginit();		/* init the messages for signals */
2699c80476e4SDavid E. O'Brien     dateinit();		/* init the messages for dates */
2700c80476e4SDavid E. O'Brien     editinit();		/* init the editor messages */
2701c80476e4SDavid E. O'Brien     terminit();		/* init the termcap messages */
2702c80476e4SDavid E. O'Brien }
270323338178SMark Peek 
270423338178SMark Peek void
270545e5710bSMark Peek nlsclose(void)
270623338178SMark Peek {
270723338178SMark Peek #ifdef NLS_CATALOGS
270845e5710bSMark Peek #if defined(HAVE_ICONV) && defined(HAVE_NL_LANGINFO)
270923338178SMark Peek     if (catgets_iconv != (iconv_t)-1) {
271023338178SMark Peek 	iconv_close(catgets_iconv);
271123338178SMark Peek 	catgets_iconv = (iconv_t)-1;
271223338178SMark Peek     }
271345e5710bSMark Peek #endif /* HAVE_ICONV && HAVE_NL_LANGINFO */
271445e5710bSMark Peek     if (catd != (nl_catd)-1) {
271545e5710bSMark Peek 	/*
271645e5710bSMark Peek 	 * catclose can call other functions which can call longjmp
271745e5710bSMark Peek 	 * making us re-enter this code. Prevent infinite recursion
271845e5710bSMark Peek 	 * by resetting catd. Problem reported and solved by:
271945e5710bSMark Peek 	 * Gerhard Niklasch
272045e5710bSMark Peek 	 */
272145e5710bSMark Peek 	nl_catd oldcatd = catd;
272245e5710bSMark Peek 	catd = (nl_catd)-1;
272345e5710bSMark Peek 	while (catclose(oldcatd) == -1 && errno == EINTR)
272445e5710bSMark Peek 	    handle_pending_signals();
272545e5710bSMark Peek     }
272623338178SMark Peek #endif /* NLS_CATALOGS */
272723338178SMark Peek }
272819d2e3deSDmitry Chagin 
272919d2e3deSDmitry Chagin int
273019d2e3deSDmitry Chagin getYN(const char *prompt)
273119d2e3deSDmitry Chagin {
273255b903e2SDmitry Chagin     int doit;
273355b903e2SDmitry Chagin     char c;
273455b903e2SDmitry Chagin 
273519d2e3deSDmitry Chagin     xprintf("%s", prompt);
273619d2e3deSDmitry Chagin     flush();
273755b903e2SDmitry Chagin     (void) force_read(SHIN, &c, sizeof(c));
273819d2e3deSDmitry Chagin     /*
273919d2e3deSDmitry Chagin      * Perhaps we should use the yesexpr from the
274019d2e3deSDmitry Chagin      * actual locale
274119d2e3deSDmitry Chagin      */
274219d2e3deSDmitry Chagin     doit = (strchr(CGETS(22, 14, "Yy"), c) != NULL);
274355b903e2SDmitry Chagin     while (c != '\n' && force_read(SHIN, &c, sizeof(c)) == sizeof(c))
274419d2e3deSDmitry Chagin 	continue;
274519d2e3deSDmitry Chagin     return doit;
274619d2e3deSDmitry Chagin }
2747