xref: /netbsd/bin/csh/csh.c (revision 84e62ad1)
1*84e62ad1Smartin /* $NetBSD: csh.c,v 1.56 2022/09/15 11:35:06 martin Exp $ */
249f0ad86Scgd 
361f28255Scgd /*-
4cee2bad8Smycroft  * Copyright (c) 1980, 1991, 1993
5cee2bad8Smycroft  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
15b5b29542Sagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
328ea378c6Schristos #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
342fe2731dSlukem __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993\
352fe2731dSlukem  The Regents of the University of California.  All rights reserved.");
3661f28255Scgd #endif /* not lint */
3761f28255Scgd 
3861f28255Scgd #ifndef lint
3949f0ad86Scgd #if 0
4049f0ad86Scgd static char sccsid[] = "@(#)csh.c	8.2 (Berkeley) 10/12/93";
4149f0ad86Scgd #else
42*84e62ad1Smartin __RCSID("$NetBSD: csh.c,v 1.56 2022/09/15 11:35:06 martin Exp $");
4349f0ad86Scgd #endif
4461f28255Scgd #endif /* not lint */
4561f28255Scgd 
4661f28255Scgd #include <sys/types.h>
4761f28255Scgd #include <sys/ioctl.h>
4861f28255Scgd #include <sys/stat.h>
49b771e65bSwiz 
5061f28255Scgd #include <errno.h>
51b771e65bSwiz #include <fcntl.h>
52b771e65bSwiz #include <locale.h>
53b771e65bSwiz #include <paths.h>	/* should this be included in pathnames.h instead? */
5461f28255Scgd #include <pwd.h>
5518158540Swiz #include <stdarg.h>
5661f28255Scgd #include <stdlib.h>
5761f28255Scgd #include <string.h>
5867e49b4bSkleink #include <time.h>
5961f28255Scgd #include <unistd.h>
60cee2bad8Smycroft #include <vis.h>
61b771e65bSwiz 
6261f28255Scgd #include "csh.h"
6361f28255Scgd #include "extern.h"
6461f28255Scgd #include "pathnames.h"
65b771e65bSwiz #include "proc.h"
6661f28255Scgd 
6761f28255Scgd /*
6861f28255Scgd  * C Shell
6961f28255Scgd  *
7061f28255Scgd  * Bill Joy, UC Berkeley, California, USA
7161f28255Scgd  * October 1978, May 1980
7261f28255Scgd  *
7361f28255Scgd  * Jim Kulp, IIASA, Laxenburg, Austria
7461f28255Scgd  * April 1980
7561f28255Scgd  *
7661f28255Scgd  * Christos Zoulas, Cornell University
7761f28255Scgd  * June, 1991
7861f28255Scgd  */
7961f28255Scgd 
80ae682804Sjoerg FILE *cshin, *cshout, *csherr;
81ae682804Sjoerg struct timespec time0;
82ae682804Sjoerg struct rusage ru0;
83ae682804Sjoerg struct varent shvhed, aliases;
84ae682804Sjoerg Char HISTSUB;
85ae682804Sjoerg int editing;
86ae682804Sjoerg 
87ae682804Sjoerg int child;
88ae682804Sjoerg int chkstop;
89ae682804Sjoerg int didfds;
90ae682804Sjoerg int doneinp;
91ae682804Sjoerg int exiterr;
92ae682804Sjoerg int haderr;
93ae682804Sjoerg int havhash;
94ae682804Sjoerg int intact;
95ae682804Sjoerg int intty;
96ae682804Sjoerg int justpr;
97ae682804Sjoerg int loginsh;
98ae682804Sjoerg int neednote;
99ae682804Sjoerg int noexec;
100ae682804Sjoerg int pjobs;
101ae682804Sjoerg int setintr;
102ae682804Sjoerg int timflg;
103ae682804Sjoerg 
104ae682804Sjoerg Char *arginp;
105ae682804Sjoerg Char *ffile;
106ae682804Sjoerg int onelflg;
107ae682804Sjoerg Char *shtemp;
108ae682804Sjoerg 
109ae682804Sjoerg time_t chktim;
110ae682804Sjoerg Char *doldol;
111ae682804Sjoerg pid_t backpid;
112ae682804Sjoerg gid_t egid, gid;
113ae682804Sjoerg uid_t euid, uid;
114ae682804Sjoerg int shpgrp;
115ae682804Sjoerg int tpgrp;
116ae682804Sjoerg 
117ae682804Sjoerg int opgrp;
118ae682804Sjoerg 
119ae682804Sjoerg int SHIN;
120ae682804Sjoerg int SHOUT;
121ae682804Sjoerg int SHERR;
122ae682804Sjoerg int OLDSTD;
123ae682804Sjoerg 
124ae682804Sjoerg jmp_buf reslab;
125ae682804Sjoerg 
126ae682804Sjoerg Char *gointr;
127ae682804Sjoerg 
128ae682804Sjoerg sig_t parintr;
129ae682804Sjoerg sig_t parterm;
130ae682804Sjoerg 
131ae682804Sjoerg struct Bin B;
132ae682804Sjoerg 
133ae682804Sjoerg struct Ain lineloc;
134ae682804Sjoerg int cantell;
135ae682804Sjoerg Char *lap;
136ae682804Sjoerg struct whyle *whyles;
137ae682804Sjoerg 
138ae682804Sjoerg struct wordent *alhistp,*alhistt;
139ae682804Sjoerg 
140ae682804Sjoerg int AsciiOnly;
141ae682804Sjoerg int gflag;
142ae682804Sjoerg long pnleft;
143ae682804Sjoerg Char *pargs;
144ae682804Sjoerg Char *pargcp;
145ae682804Sjoerg struct Hist Histlist;
146ae682804Sjoerg struct wordent paraml;
147ae682804Sjoerg int eventno;
148ae682804Sjoerg int lastev;
149ae682804Sjoerg Char HIST;
150ae682804Sjoerg Char HISTSUB;
151ae682804Sjoerg const char *bname;
152ae682804Sjoerg Char *Vsav;
153ae682804Sjoerg Char *Vdp;
154ae682804Sjoerg Char *Vexpath;
155ae682804Sjoerg char **Vt;
156ae682804Sjoerg Char **evalvec;
157ae682804Sjoerg Char *evalp;
158ae682804Sjoerg Char *word_chars;
159ae682804Sjoerg Char *STR_SHELLPATH;
160ae682804Sjoerg #ifdef _PATH_BSHELL
161ae682804Sjoerg Char *STR_BSHELL;
162ae682804Sjoerg #endif
163ae682804Sjoerg Char *STR_WORD_CHARS;
164ae682804Sjoerg Char **STR_environ;
165ae682804Sjoerg #ifdef EDIT
166ae682804Sjoerg EditLine *el;
167ae682804Sjoerg History *hi;
168ae682804Sjoerg #endif
169ae682804Sjoerg int editing;
170ae682804Sjoerg 
17161f28255Scgd Char *dumphist[] = {STRhistory, STRmh, 0, 0};
1728b3d9c1bSchristos Char *tildehist[] = {STRsource, STRmh, STRtildothist, 0};
17361f28255Scgd 
17461f28255Scgd int nofile = 0;
175b79c2ef2Schristos int batch = 0;
176b79c2ef2Schristos int enterhist = 0;
177b79c2ef2Schristos int fast = 0;
178b79c2ef2Schristos int mflag = 0;
179b79c2ef2Schristos int nexececho = 0;
180b79c2ef2Schristos int nverbose = 0;
181b79c2ef2Schristos int prompt = 1;
182b79c2ef2Schristos int quitit = 0;
183b79c2ef2Schristos int reenter = 0;
18461f28255Scgd 
18561f28255Scgd extern char **environ;
18661f28255Scgd 
187c24abf82Schristos static ssize_t readf(void *, void *, size_t);
18816ed1862Schristos static off_t seekf(void *, off_t, int);
189c24abf82Schristos static ssize_t writef(void *, const void *, size_t);
190b771e65bSwiz static int closef(void *);
191b771e65bSwiz static int srccat(Char *, Char *);
192b79c2ef2Schristos static int srcfile(const char *, int, int);
193007a7cf0Sjoerg __dead static void phup(int);
194b79c2ef2Schristos static void srcunit(int, int, int);
195b771e65bSwiz static void mailchk(void);
1960f668275Sfair #ifndef _PATH_DEFPATH
197b771e65bSwiz static Char **defaultpath(void);
1980f668275Sfair #endif
1998ea378c6Schristos 
20061f28255Scgd int
main(int argc,char * argv[])201b771e65bSwiz main(int argc, char *argv[])
20261f28255Scgd {
2037b38403cSmycroft     struct sigaction oact;
204b771e65bSwiz     Char *cp;
205b771e65bSwiz     char *tcp, **tempv;
206b771e65bSwiz     const char *ecp;
207b3df6303Skleink     sigset_t nsigset;
208b771e65bSwiz     int f;
20961f28255Scgd 
210cee2bad8Smycroft     cshin = stdin;
211cee2bad8Smycroft     cshout = stdout;
212cee2bad8Smycroft     csherr = stderr;
21361f28255Scgd 
2148e36d79bSwiz     setprogname(argv[0]);
21561f28255Scgd     settimes();			/* Immed. estab. timing base */
21661f28255Scgd 
21761f28255Scgd     /*
21861f28255Scgd      * Initialize non constant strings
21961f28255Scgd      */
22061f28255Scgd #ifdef _PATH_BSHELL
22161f28255Scgd     STR_BSHELL = SAVE(_PATH_BSHELL);
22261f28255Scgd #endif
22361f28255Scgd #ifdef _PATH_CSHELL
22461f28255Scgd     STR_SHELLPATH = SAVE(_PATH_CSHELL);
22561f28255Scgd #endif
22661f28255Scgd     STR_environ = blk2short(environ);
22761f28255Scgd     environ = short2blk(STR_environ);	/* So that we can free it */
22861f28255Scgd     STR_WORD_CHARS = SAVE(WORD_CHARS);
22961f28255Scgd 
23061f28255Scgd     HIST = '!';
23161f28255Scgd     HISTSUB = '^';
23261f28255Scgd     word_chars = STR_WORD_CHARS;
23361f28255Scgd 
23461f28255Scgd     tempv = argv;
23561f28255Scgd     if (eq(str2short(tempv[0]), STRaout))	/* A.out's are quittable */
23661f28255Scgd 	quitit = 1;
23761f28255Scgd     uid = getuid();
23861f28255Scgd     gid = getgid();
239cee2bad8Smycroft     euid = geteuid();
240cee2bad8Smycroft     egid = getegid();
24161f28255Scgd     /*
24261f28255Scgd      * We are a login shell if: 1. we were invoked as -<something> and we had
24361f28255Scgd      * no arguments 2. or we were invoked only with the -l flag
24461f28255Scgd      */
24561f28255Scgd     loginsh = (**tempv == '-' && argc == 1) ||
24661f28255Scgd 	(argc == 2 && tempv[1][0] == '-' && tempv[1][1] == 'l' &&
24761f28255Scgd 	 tempv[1][2] == '\0');
24861f28255Scgd 
24961f28255Scgd     if (loginsh && **tempv != '-') {
25061f28255Scgd 	/*
25161f28255Scgd 	 * Mangle the argv space
25261f28255Scgd 	 */
25361f28255Scgd 	tempv[1][0] = '\0';
25461f28255Scgd 	tempv[1][1] = '\0';
25561f28255Scgd 	tempv[1] = NULL;
256cee2bad8Smycroft 	for (tcp = *tempv; *tcp++;)
257cee2bad8Smycroft 	    continue;
25861f28255Scgd 	for (tcp--; tcp >= *tempv; tcp--)
25961f28255Scgd 	    tcp[1] = tcp[0];
26061f28255Scgd 	*++tcp = '-';
26161f28255Scgd 	argc--;
26261f28255Scgd     }
26361f28255Scgd     if (loginsh)
26461f28255Scgd 	(void)time(&chktim);
26561f28255Scgd 
26661f28255Scgd     AsciiOnly = 1;
26761f28255Scgd #ifdef NLS
26861f28255Scgd     (void)setlocale(LC_ALL, "");
26961f28255Scgd     {
27061f28255Scgd 	int k;
27161f28255Scgd 
272cee2bad8Smycroft 	for (k = 0200; k <= 0377 && !Isprint(k); k++)
273cee2bad8Smycroft 	    continue;
27461f28255Scgd 	AsciiOnly = k > 0377;
27561f28255Scgd     }
27661f28255Scgd #else
27761f28255Scgd     AsciiOnly = getenv("LANG") == NULL && getenv("LC_CTYPE") == NULL;
27861f28255Scgd #endif				/* NLS */
27961f28255Scgd 
28061f28255Scgd     /*
28161f28255Scgd      * Move the descriptors to safe places. The variable didfds is 0 while we
28261f28255Scgd      * have only FSH* to work with. When didfds is true, we have 0,1,2 and
28361f28255Scgd      * prefer to use these.
28461f28255Scgd      */
28561f28255Scgd     initdesc();
286cee2bad8Smycroft     /*
287cee2bad8Smycroft      * XXX: This is to keep programs that use stdio happy.
288cee2bad8Smycroft      *	    what we really want is freunopen() ....
289cee2bad8Smycroft      *	    Closing cshin cshout and csherr (which are really stdin stdout
290cee2bad8Smycroft      *	    and stderr at this point and then reopening them in the same order
291cee2bad8Smycroft      *	    gives us again stdin == cshin stdout == cshout and stderr == csherr.
292cee2bad8Smycroft      *	    If that was not the case builtins like printf that use stdio
293cee2bad8Smycroft      *	    would break. But in any case we could fix that with memcpy and
294cee2bad8Smycroft      *	    a bit of pointer manipulation...
295cee2bad8Smycroft      *	    Fortunately this is not needed under the current implementation
296cee2bad8Smycroft      *	    of stdio.
297cee2bad8Smycroft      */
298cee2bad8Smycroft     (void)fclose(cshin);
299cee2bad8Smycroft     (void)fclose(cshout);
300cee2bad8Smycroft     (void)fclose(csherr);
301c24abf82Schristos     if (!(cshin  = funopen2((void *) &SHIN,  readf, writef, seekf, NULL,
302c24abf82Schristos 	closef)))
303cee2bad8Smycroft 	exit(1);
304c24abf82Schristos     if (!(cshout = funopen2((void *) &SHOUT, readf, writef, seekf, NULL,
305c24abf82Schristos 	closef)))
306cee2bad8Smycroft 	exit(1);
307c24abf82Schristos     if (!(csherr = funopen2((void *) &SHERR, readf, writef, seekf, NULL,
308c24abf82Schristos 	closef)))
309cee2bad8Smycroft 	exit(1);
310cee2bad8Smycroft     (void)setvbuf(cshin,  NULL, _IOLBF, 0);
311cee2bad8Smycroft     (void)setvbuf(cshout, NULL, _IOLBF, 0);
312cee2bad8Smycroft     (void)setvbuf(csherr, NULL, _IOLBF, 0);
31361f28255Scgd 
31461f28255Scgd     /*
31561f28255Scgd      * Initialize the shell variables. ARGV and PROMPT are initialized later.
31661f28255Scgd      * STATUS is also munged in several places. CHILD is munged when
31761f28255Scgd      * forking/waiting
31861f28255Scgd      */
31961f28255Scgd     set(STRstatus, Strsave(STR0));
32061f28255Scgd 
3214d669802Smycroft     if ((ecp = getenv("HOME")) != NULL)
3224d669802Smycroft 	cp = quote(SAVE(ecp));
32361f28255Scgd     else
32461f28255Scgd 	cp = NULL;
32561f28255Scgd 
32661f28255Scgd     if (cp == NULL)
32761f28255Scgd 	fast = 1;		/* No home -> can't read scripts */
32861f28255Scgd     else
32961f28255Scgd 	set(STRhome, cp);
33061f28255Scgd     dinit(cp);			/* dinit thinks that HOME == cwd in a login
33161f28255Scgd 				 * shell */
33261f28255Scgd     /*
33361f28255Scgd      * Grab other useful things from the environment. Should we grab
33461f28255Scgd      * everything??
33561f28255Scgd      */
3364d669802Smycroft     if ((ecp = getenv("LOGNAME")) != NULL ||
3374d669802Smycroft 	(ecp = getenv("USER")) != NULL)
3384d669802Smycroft 	set(STRuser, quote(SAVE(ecp)));
3394d669802Smycroft     if ((ecp = getenv("TERM")) != NULL)
3404d669802Smycroft 	set(STRterm, quote(SAVE(ecp)));
34161f28255Scgd 
34261f28255Scgd     /*
34361f28255Scgd      * Re-initialize path if set in environment
34461f28255Scgd      */
3454d669802Smycroft     if ((ecp = getenv("PATH")) == NULL) {
3460f668275Sfair #ifdef _PATH_DEFPATH
3473d0237efSchristos 	importpath(str2short(_PATH_DEFPATH));
3480f668275Sfair #else
3498af89705Schristos 	setq(STRpath, defaultpath(), &shvhed);
3500f668275Sfair #endif
3510f668275Sfair     } else {
3523d0237efSchristos 	importpath(str2short(ecp));
3530f668275Sfair     }
35461f28255Scgd 
35561f28255Scgd     set(STRshell, Strsave(STR_SHELLPATH));
35661f28255Scgd 
35761f28255Scgd     doldol = putn((int) getpid());	/* For $$ */
35861f28255Scgd     shtemp = Strspl(STRtmpsh, doldol);	/* For << */
35961f28255Scgd 
36061f28255Scgd     /*
36161f28255Scgd      * Record the interrupt states from the parent process. If the parent is
36261f28255Scgd      * non-interruptible our hand must be forced or we (and our children) won't
36361f28255Scgd      * be either. Our children inherit termination from our parent. We catch it
36461f28255Scgd      * only if we are the login shell.
36561f28255Scgd      */
36661f28255Scgd     /* parents interruptibility */
3677b38403cSmycroft     (void)sigaction(SIGINT, NULL, &oact);
3687b38403cSmycroft     parintr = oact.sa_handler;
3697b38403cSmycroft     (void)sigaction(SIGTERM, NULL, &oact);
3707b38403cSmycroft     parterm = oact.sa_handler;
37161f28255Scgd 
3722ee028a2Scgd     /* catch these all, login shell or not */
37361f28255Scgd     (void)signal(SIGHUP, phup);	/* exit processing on HUP */
37461f28255Scgd     (void)signal(SIGXCPU, phup);	/* ...and on XCPU */
37561f28255Scgd     (void)signal(SIGXFSZ, phup);	/* ...and on XFSZ */
37661f28255Scgd 
37761f28255Scgd     /*
37861f28255Scgd      * Process the arguments.
37961f28255Scgd      *
38061f28255Scgd      * Note that processing of -v/-x is actually delayed till after script
38161f28255Scgd      * processing.
38261f28255Scgd      *
383cee2bad8Smycroft      * We set the first character of our name to be '-' if we are a shell
384cee2bad8Smycroft      * running interruptible commands.  Many programs which examine ps'es
385cee2bad8Smycroft      * use this to filter such shells out.
38661f28255Scgd      */
38761f28255Scgd     argc--, tempv++;
38861f28255Scgd     while (argc > 0 && (tcp = tempv[0])[0] == '-' && *++tcp != '\0' && !batch) {
38961f28255Scgd 	do
39061f28255Scgd 	    switch (*tcp++) {
39161f28255Scgd 	    case 0:		/* -	Interruptible, no prompt */
39261f28255Scgd 		prompt = 0;
39361f28255Scgd 		setintr = 1;
39461f28255Scgd 		nofile = 1;
39561f28255Scgd 		break;
39661f28255Scgd 	    case 'b':		/* -b	Next arg is input file */
39761f28255Scgd 		batch = 1;
39861f28255Scgd 		break;
39961f28255Scgd 	    case 'c':		/* -c	Command input from arg */
400ee9e50eaSmycroft 		if (argc == 1)
40161f28255Scgd 		    xexit(0);
40261f28255Scgd 		argc--, tempv++;
40361f28255Scgd 		arginp = SAVE(tempv[0]);
40461f28255Scgd 		prompt = 0;
40561f28255Scgd 		nofile = 1;
40661f28255Scgd 		break;
40761f28255Scgd 	    case 'e':		/* -e	Exit on any error */
40861f28255Scgd 		exiterr = 1;
40961f28255Scgd 		break;
41061f28255Scgd 	    case 'f':		/* -f	Fast start */
41161f28255Scgd 		fast = 1;
41261f28255Scgd 		break;
41361f28255Scgd 	    case 'i':		/* -i	Interactive, even if !intty */
41461f28255Scgd 		intact = 1;
41561f28255Scgd 		nofile = 1;
41661f28255Scgd 		break;
41761f28255Scgd 	    case 'm':		/* -m	read .cshrc (from su) */
41861f28255Scgd 		mflag = 1;
41961f28255Scgd 		break;
42061f28255Scgd 	    case 'n':		/* -n	Don't execute */
42161f28255Scgd 		noexec = 1;
42261f28255Scgd 		break;
42361f28255Scgd 	    case 'q':		/* -q	(Undoc'd) ... die on quit */
42461f28255Scgd 		quitit = 1;
42561f28255Scgd 		break;
42661f28255Scgd 	    case 's':		/* -s	Read from std input */
42761f28255Scgd 		nofile = 1;
42861f28255Scgd 		break;
42961f28255Scgd 	    case 't':		/* -t	Read one line from input */
43061f28255Scgd 		onelflg = 2;
43161f28255Scgd 		prompt = 0;
43261f28255Scgd 		nofile = 1;
43361f28255Scgd 		break;
43461f28255Scgd 	    case 'v':		/* -v	Echo hist expanded input */
43561f28255Scgd 		nverbose = 1;	/* ... later */
43661f28255Scgd 		break;
43761f28255Scgd 	    case 'x':		/* -x	Echo just before execution */
43861f28255Scgd 		nexececho = 1;	/* ... later */
43961f28255Scgd 		break;
44061f28255Scgd 	    case 'V':		/* -V	Echo hist expanded input */
44161f28255Scgd 		setNS(STRverbose);	/* NOW! */
44261f28255Scgd 		break;
44361f28255Scgd 	    case 'X':		/* -X	Echo just before execution */
44461f28255Scgd 		setNS(STRecho);	/* NOW! */
44561f28255Scgd 		break;
44661f28255Scgd 
44761f28255Scgd 	} while (*tcp);
44861f28255Scgd 	tempv++, argc--;
44961f28255Scgd     }
45061f28255Scgd 
45161f28255Scgd     if (quitit)			/* With all due haste, for debugging */
45261f28255Scgd 	(void)signal(SIGQUIT, SIG_DFL);
45361f28255Scgd 
45461f28255Scgd     /*
45561f28255Scgd      * Unless prevented by -, -c, -i, -s, or -t, if there are remaining
45661f28255Scgd      * arguments the first of them is the name of a shell file from which to
45761f28255Scgd      * read commands.
45861f28255Scgd      */
45961f28255Scgd     if (nofile == 0 && argc > 0) {
46061f28255Scgd 	nofile = open(tempv[0], O_RDONLY);
46161f28255Scgd 	if (nofile < 0) {
46261f28255Scgd 	    child = 1;		/* So this doesn't return */
46361f28255Scgd 	    stderror(ERR_SYSTEM, tempv[0], strerror(errno));
46461f28255Scgd 	}
46561f28255Scgd 	ffile = SAVE(tempv[0]);
46661f28255Scgd 	/*
46761f28255Scgd 	 * Replace FSHIN. Handle /dev/std{in,out,err} specially
46861f28255Scgd 	 * since once they are closed we cannot open them again.
46961f28255Scgd 	 * In that case we use our own saved descriptors
47061f28255Scgd 	 */
47161f28255Scgd 	if ((SHIN = dmove(nofile, FSHIN)) < 0)
47261f28255Scgd 	    switch(nofile) {
47361f28255Scgd 	    case 0:
47461f28255Scgd 		SHIN = FSHIN;
47561f28255Scgd 		break;
47661f28255Scgd 	    case 1:
47761f28255Scgd 		SHIN = FSHOUT;
47861f28255Scgd 		break;
47961f28255Scgd 	    case 2:
480cee2bad8Smycroft 		SHIN = FSHERR;
48161f28255Scgd 		break;
48261f28255Scgd 	    default:
48361f28255Scgd 		stderror(ERR_SYSTEM, tempv[0], strerror(errno));
484cdbd74daSmycroft 		/* NOTREACHED */
48561f28255Scgd 	    }
48661f28255Scgd 	(void)ioctl(SHIN, FIOCLEX, NULL);
48761f28255Scgd 	prompt = 0;
48861f28255Scgd 	 /* argc not used any more */ tempv++;
48961f28255Scgd     }
490cee2bad8Smycroft 
49161f28255Scgd     intty = isatty(SHIN);
49261f28255Scgd     intty |= intact;
49361f28255Scgd     if (intty || (intact && isatty(SHOUT))) {
494cee2bad8Smycroft 	if (!batch && (uid != euid || gid != egid)) {
49561f28255Scgd 	    errno = EACCES;
49661f28255Scgd 	    child = 1;		/* So this doesn't return */
49761f28255Scgd 	    stderror(ERR_SYSTEM, "csh", strerror(errno));
49861f28255Scgd 	}
49961f28255Scgd     }
50061f28255Scgd     /*
50161f28255Scgd      * Decide whether we should play with signals or not. If we are explicitly
50261f28255Scgd      * told (via -i, or -) or we are a login shell (arg0 starts with -) or the
50361f28255Scgd      * input and output are both the ttys("csh", or "csh</dev/ttyx>/dev/ttyx")
50461f28255Scgd      * Note that in only the login shell is it likely that parent may have set
50561f28255Scgd      * signals to be ignored
50661f28255Scgd      */
507cee2bad8Smycroft     if (loginsh || intact || (intty && isatty(SHOUT)))
50861f28255Scgd 	setintr = 1;
50961f28255Scgd     settell();
51061f28255Scgd     /*
51161f28255Scgd      * Save the remaining arguments in argv.
51261f28255Scgd      */
51361f28255Scgd     setq(STRargv, blk2short(tempv), &shvhed);
51461f28255Scgd 
51561f28255Scgd     /*
51661f28255Scgd      * Set up the prompt.
51761f28255Scgd      */
51861f28255Scgd     if (prompt) {
51961f28255Scgd 	set(STRprompt, Strsave(uid == 0 ? STRsymhash : STRsymcent));
52061f28255Scgd 	/* that's a meta-questionmark */
52161f28255Scgd 	set(STRprompt2, Strsave(STRmquestion));
52261f28255Scgd     }
52361f28255Scgd 
52461f28255Scgd     /*
52561f28255Scgd      * If we are an interactive shell, then start fiddling with the signals;
52661f28255Scgd      * this is a tricky game.
52761f28255Scgd      */
52861f28255Scgd     shpgrp = getpgrp();
52961f28255Scgd     opgrp = tpgrp = -1;
53061f28255Scgd     if (setintr) {
53161f28255Scgd 	**argv = '-';
53261f28255Scgd 	if (!quitit)		/* Wary! */
53361f28255Scgd 	    (void)signal(SIGQUIT, SIG_IGN);
53461f28255Scgd 	(void)signal(SIGINT, pintr);
535b3df6303Skleink 	sigemptyset(&nsigset);
536b3df6303Skleink 	(void)sigaddset(&nsigset, SIGINT);
537b3df6303Skleink 	(void)sigprocmask(SIG_BLOCK, &nsigset, NULL);
53861f28255Scgd 	(void)signal(SIGTERM, SIG_IGN);
53961f28255Scgd 	if (quitit == 0 && arginp == 0) {
54061f28255Scgd 	    (void)signal(SIGTSTP, SIG_IGN);
54161f28255Scgd 	    (void)signal(SIGTTIN, SIG_IGN);
54261f28255Scgd 	    (void)signal(SIGTTOU, SIG_IGN);
54361f28255Scgd 	    /*
54461f28255Scgd 	     * Wait till in foreground, in case someone stupidly runs csh &
54561f28255Scgd 	     * dont want to try to grab away the tty.
54661f28255Scgd 	     */
547cee2bad8Smycroft 	    if (isatty(FSHERR))
548cee2bad8Smycroft 		f = FSHERR;
54961f28255Scgd 	    else if (isatty(FSHOUT))
55061f28255Scgd 		f = FSHOUT;
55161f28255Scgd 	    else if (isatty(OLDSTD))
55261f28255Scgd 		f = OLDSTD;
55361f28255Scgd 	    else
55461f28255Scgd 		f = -1;
55561f28255Scgd     retry:
55661f28255Scgd 	    if ((tpgrp = tcgetpgrp(f)) != -1) {
55761f28255Scgd 		if (tpgrp != shpgrp) {
55861f28255Scgd 		    sig_t old = signal(SIGTTIN, SIG_DFL);
55961f28255Scgd 		    (void)kill(0, SIGTTIN);
56061f28255Scgd 		    (void)signal(SIGTTIN, old);
56161f28255Scgd 		    goto retry;
56261f28255Scgd 		}
56361f28255Scgd 		opgrp = shpgrp;
56461f28255Scgd 		shpgrp = getpid();
56561f28255Scgd 		tpgrp = shpgrp;
56661f28255Scgd 		/*
56761f28255Scgd 		 * Setpgid will fail if we are a session leader and
56861f28255Scgd 		 * mypid == mypgrp (POSIX 4.3.3)
56961f28255Scgd 		 */
57061f28255Scgd 		if (opgrp != shpgrp)
57161f28255Scgd 		    if (setpgid(0, shpgrp) == -1)
57261f28255Scgd 			goto notty;
57361f28255Scgd 		/*
57461f28255Scgd 		 * We do that after we set our process group, to make sure
57561f28255Scgd 		 * that the process group belongs to a process in the same
57661f28255Scgd 		 * session as the tty (our process and our group) (POSIX 7.2.4)
57761f28255Scgd 		 */
57861f28255Scgd 		if (tcsetpgrp(f, shpgrp) == -1)
57961f28255Scgd 		    goto notty;
58061f28255Scgd 		(void)ioctl(dcopy(f, FSHTTY), FIOCLEX, NULL);
58161f28255Scgd 	    }
58261f28255Scgd 	    if (tpgrp == -1) {
58361f28255Scgd notty:
584cee2bad8Smycroft 		(void)fprintf(csherr, "Warning: no access to tty (%s).\n",
585cee2bad8Smycroft 			       strerror(errno));
586cee2bad8Smycroft 		(void)fprintf(csherr, "Thus no job control in this shell.\n");
58761f28255Scgd 	    }
58861f28255Scgd 	}
58961f28255Scgd     }
59061f28255Scgd     if ((setintr == 0) && (parintr == SIG_DFL))
59161f28255Scgd 	setintr = 1;
59261f28255Scgd     (void)signal(SIGCHLD, pchild);	/* while signals not ready */
59361f28255Scgd 
59461f28255Scgd     /*
59561f28255Scgd      * Set an exit here in case of an interrupt or error reading the shell
59661f28255Scgd      * start-up scripts.
59761f28255Scgd      */
59861f28255Scgd     reenter = setexit();	/* PWP */
59961f28255Scgd     haderr = 0;			/* In case second time through */
60061f28255Scgd     if (!fast && reenter == 0) {
60161f28255Scgd 	/* Will have value(STRhome) here because set fast if don't */
60261f28255Scgd 	{
603b771e65bSwiz 	    sig_t oparintr;
6047b38403cSmycroft 	    sigset_t osigset;
605b771e65bSwiz 	    int osetintr;
6067b38403cSmycroft 
607b771e65bSwiz 	    oparintr = parintr;
608b771e65bSwiz 	    osetintr = setintr;
609b3df6303Skleink 	    sigemptyset(&nsigset);
610b3df6303Skleink 	    (void)sigaddset(&nsigset, SIGINT);
611b3df6303Skleink 	    (void)sigprocmask(SIG_BLOCK, &nsigset, &osigset);
61261f28255Scgd 
61361f28255Scgd 	    setintr = 0;
614cee2bad8Smycroft 	    parintr = SIG_IGN;	/* Disable onintr */
61561f28255Scgd #ifdef _PATH_DOTCSHRC
61661f28255Scgd 	    (void)srcfile(_PATH_DOTCSHRC, 0, 0);
61761f28255Scgd #endif
61861f28255Scgd 	    if (!fast && !arginp && !onelflg)
619cee2bad8Smycroft 		dohash(NULL, NULL);
62061f28255Scgd #ifdef _PATH_DOTLOGIN
62161f28255Scgd 	    if (loginsh)
62261f28255Scgd 		(void)srcfile(_PATH_DOTLOGIN, 0, 0);
62361f28255Scgd #endif
6245924694dSmycroft 	    (void)sigprocmask(SIG_SETMASK, &osigset, NULL);
62561f28255Scgd 	    setintr = osetintr;
626cee2bad8Smycroft 	    parintr = oparintr;
62761f28255Scgd 	}
62861f28255Scgd 	(void)srccat(value(STRhome), STRsldotcshrc);
62961f28255Scgd 
63061f28255Scgd 	if (!fast && !arginp && !onelflg && !havhash)
631cee2bad8Smycroft 	    dohash(NULL, NULL);
632cee2bad8Smycroft 	/*
633cee2bad8Smycroft 	 * Source history before .login so that it is available in .login
634cee2bad8Smycroft 	 */
635cee2bad8Smycroft 	if ((cp = value(STRhistfile)) != STRNULL)
6368b3d9c1bSchristos 	    tildehist[2] = cp;
6378b3d9c1bSchristos 	dosource(tildehist, NULL);
63861f28255Scgd         if (loginsh)
63961f28255Scgd 	      (void)srccat(value(STRhome), STRsldotlogin);
64061f28255Scgd     }
64161f28255Scgd 
64261f28255Scgd     /*
64361f28255Scgd      * Now are ready for the -v and -x flags
64461f28255Scgd      */
64561f28255Scgd     if (nverbose)
64661f28255Scgd 	setNS(STRverbose);
64761f28255Scgd     if (nexececho)
64861f28255Scgd 	setNS(STRecho);
64961f28255Scgd 
65061f28255Scgd     /*
65161f28255Scgd      * All the rest of the world is inside this call. The argument to process
65261f28255Scgd      * indicates whether it should catch "error unwinds".  Thus if we are a
65361f28255Scgd      * interactive shell our call here will never return by being blown past on
65461f28255Scgd      * an error.
65561f28255Scgd      */
65661f28255Scgd     process(setintr);
65761f28255Scgd 
65861f28255Scgd     /*
65961f28255Scgd      * Mop-up.
66061f28255Scgd      */
66161f28255Scgd     if (intty) {
66261f28255Scgd 	if (loginsh) {
663cee2bad8Smycroft 	    (void)fprintf(cshout, "logout\n");
66461f28255Scgd 	    (void)close(SHIN);
66561f28255Scgd 	    child = 1;
66661f28255Scgd 	    goodbye();
66761f28255Scgd 	}
66861f28255Scgd 	else {
669cee2bad8Smycroft 	    (void)fprintf(cshout, "exit\n");
67061f28255Scgd 	}
67161f28255Scgd     }
67261f28255Scgd     rechist();
67361f28255Scgd     exitstat();
674cdbd74daSmycroft     /* NOTREACHED */
67561f28255Scgd }
67661f28255Scgd 
67761f28255Scgd void
untty(void)678b771e65bSwiz untty(void)
67961f28255Scgd {
68061f28255Scgd     if (tpgrp > 0) {
68161f28255Scgd 	(void)setpgid(0, opgrp);
68261f28255Scgd 	(void)tcsetpgrp(FSHTTY, opgrp);
68361f28255Scgd     }
68461f28255Scgd }
68561f28255Scgd 
68661f28255Scgd void
importpath(Char * cp)687b771e65bSwiz importpath(Char *cp)
68861f28255Scgd {
689b771e65bSwiz     Char *dp, **pv;
690b771e65bSwiz     int c, i;
69161f28255Scgd 
692b771e65bSwiz     i = 0;
69361f28255Scgd     for (dp = cp; *dp; dp++)
69461f28255Scgd 	if (*dp == ':')
69561f28255Scgd 	    i++;
69661f28255Scgd     /*
69761f28255Scgd      * i+2 where i is the number of colons in the path. There are i+1
69861f28255Scgd      * directories in the path plus we need room for a zero terminator.
69961f28255Scgd      */
700dd74a0aaSdholland     pv = xcalloc((size_t) (i + 2), sizeof(*pv));
70161f28255Scgd     dp = cp;
70261f28255Scgd     i = 0;
70361f28255Scgd     if (*dp)
70461f28255Scgd 	for (;;) {
70561f28255Scgd 	    if ((c = *dp) == ':' || c == 0) {
70661f28255Scgd 		*dp = 0;
70761f28255Scgd 		pv[i++] = Strsave(*cp ? cp : STRdot);
70861f28255Scgd 		if (c) {
70961f28255Scgd 		    cp = dp + 1;
71061f28255Scgd 		    *dp = ':';
71161f28255Scgd 		}
71261f28255Scgd 		else
71361f28255Scgd 		    break;
71461f28255Scgd 	    }
71561f28255Scgd 	    dp++;
71661f28255Scgd 	}
71761f28255Scgd     pv[i] = 0;
7183d0237efSchristos     setq(STRpath, pv, &shvhed);
71961f28255Scgd }
72061f28255Scgd 
72161f28255Scgd /*
72261f28255Scgd  * Source to the file which is the catenation of the argument names.
72361f28255Scgd  */
72461f28255Scgd static int
srccat(Char * cp,Char * dp)725b771e65bSwiz srccat(Char *cp, Char *dp)
72661f28255Scgd {
727b771e65bSwiz     Char *ep;
728b771e65bSwiz     char *ptr;
72961f28255Scgd 
730b771e65bSwiz     ep = Strspl(cp, dp);
731b771e65bSwiz     ptr = short2str(ep);
7327405c8c3Schristos     free(ep);
73361f28255Scgd     return srcfile(ptr, mflag ? 0 : 1, 0);
73461f28255Scgd }
73561f28255Scgd 
73661f28255Scgd /*
73761f28255Scgd  * Source to a file putting the file descriptor in a safe place (> 2).
73861f28255Scgd  */
73961f28255Scgd static int
srcfile(const char * f,int onlyown,int flag)740b79c2ef2Schristos srcfile(const char *f, int onlyown, int flag)
74161f28255Scgd {
74276adbe2bStls     int unit;
74361f28255Scgd 
74461f28255Scgd     if ((unit = open(f, O_RDONLY)) == -1)
74561f28255Scgd 	return 0;
74661f28255Scgd     unit = dmove(unit, -1);
74761f28255Scgd 
74861f28255Scgd     (void) ioctl(unit, FIOCLEX, NULL);
74961f28255Scgd     srcunit(unit, onlyown, flag);
75061f28255Scgd     return 1;
75161f28255Scgd }
75261f28255Scgd 
75361f28255Scgd /*
75461f28255Scgd  * Source to a unit.  If onlyown it must be our file or our group or
75561f28255Scgd  * we don't chance it.	This occurs on ".cshrc"s and the like.
75661f28255Scgd  */
75761f28255Scgd int insource;
758b771e65bSwiz 
75961f28255Scgd static void
srcunit(int unit,int onlyown,int hflg)760b79c2ef2Schristos srcunit(int unit, int onlyown, int hflg)
76161f28255Scgd {
76261f28255Scgd     /* We have to push down a lot of state here */
76361f28255Scgd     /* All this could go into a structure */
764b771e65bSwiz     struct whyle *oldwhyl;
76561f28255Scgd     struct Bin saveB;
766b3df6303Skleink     sigset_t nsigset, osigset;
76761f28255Scgd     jmp_buf oldexit;
768b771e65bSwiz     Char *oarginp, *oevalp, **oevalvec, *ogointr;
769c24abf82Schristos     Char OHIST;
770b771e65bSwiz     int oSHIN, oinsource, oldintty, oonelflg;
771b79c2ef2Schristos     int oenterhist, otell;
77261f28255Scgd     /* The (few) real local variables */
77361f28255Scgd     int my_reenter;
77461f28255Scgd 
775b771e65bSwiz     oSHIN = -1;
776b771e65bSwiz     oldintty = intty;
777b771e65bSwiz     oinsource = insource;
778b771e65bSwiz     oldwhyl = whyles;
779b771e65bSwiz     ogointr = gointr;
780b771e65bSwiz     oarginp = arginp;
781b771e65bSwiz     oevalp = evalp;
782b771e65bSwiz     oevalvec = evalvec;
783b771e65bSwiz     oonelflg = onelflg;
784b771e65bSwiz     oenterhist = enterhist;
785b771e65bSwiz     OHIST = HIST;
786b771e65bSwiz     otell = cantell;
787b771e65bSwiz 
78861f28255Scgd     if (unit < 0)
78961f28255Scgd 	return;
79061f28255Scgd     if (didfds)
79161f28255Scgd 	donefds();
79261f28255Scgd     if (onlyown) {
79361f28255Scgd 	struct stat stb;
79461f28255Scgd 
79561f28255Scgd 	if (fstat(unit, &stb) < 0) {
79661f28255Scgd 	    (void)close(unit);
79761f28255Scgd 	    return;
79861f28255Scgd 	}
79961f28255Scgd     }
80061f28255Scgd 
80161f28255Scgd     /*
80261f28255Scgd      * There is a critical section here while we are pushing down the input
80361f28255Scgd      * stream since we have stuff in different structures. If we weren't
80461f28255Scgd      * careful an interrupt could corrupt SHIN's Bin structure and kill the
80561f28255Scgd      * shell.
80661f28255Scgd      *
80761f28255Scgd      * We could avoid the critical region by grouping all the stuff in a single
80861f28255Scgd      * structure and pointing at it to move it all at once.  This is less
80961f28255Scgd      * efficient globally on many variable references however.
81061f28255Scgd      */
81161f28255Scgd     insource = 1;
81261f28255Scgd     getexit(oldexit);
81361f28255Scgd 
8147b38403cSmycroft     if (setintr) {
815b3df6303Skleink 	sigemptyset(&nsigset);
816b3df6303Skleink 	(void)sigaddset(&nsigset, SIGINT);
817b3df6303Skleink 	(void)sigprocmask(SIG_BLOCK, &nsigset, &osigset);
8187b38403cSmycroft     }
81961f28255Scgd     /* Setup the new values of the state stuff saved above */
8205924694dSmycroft     (void)memcpy(&saveB, &B, sizeof(B));
82161f28255Scgd     fbuf = NULL;
82261f28255Scgd     fseekp = feobp = fblocks = 0;
82361f28255Scgd     oSHIN = SHIN, SHIN = unit, arginp = 0, onelflg = 0;
82461f28255Scgd     intty = isatty(SHIN), whyles = 0, gointr = 0;
82561f28255Scgd     evalvec = 0;
82661f28255Scgd     evalp = 0;
82761f28255Scgd     enterhist = hflg;
82861f28255Scgd     if (enterhist)
82961f28255Scgd 	HIST = '\0';
83061f28255Scgd 
83161f28255Scgd     /*
83261f28255Scgd      * Now if we are allowing commands to be interrupted, we let ourselves be
83361f28255Scgd      * interrupted.
83461f28255Scgd      */
83561f28255Scgd     if (setintr)
8365924694dSmycroft 	(void)sigprocmask(SIG_SETMASK, &osigset, NULL);
83761f28255Scgd     settell();
83861f28255Scgd 
83961f28255Scgd     if ((my_reenter = setexit()) == 0)
84061f28255Scgd 	process(0);				/* 0 -> blow away on errors */
84161f28255Scgd 
84261f28255Scgd     if (setintr)
8435924694dSmycroft 	(void)sigprocmask(SIG_SETMASK, &osigset, NULL);
84461f28255Scgd     if (oSHIN >= 0) {
84576adbe2bStls 	int i;
84661f28255Scgd 
84761f28255Scgd 	/* We made it to the new state... free up its storage */
84861f28255Scgd 	for (i = 0; i < fblocks; i++)
8497405c8c3Schristos 	    free(fbuf[i]);
8507405c8c3Schristos 	free(fbuf);
85161f28255Scgd 
85261f28255Scgd 	/* Reset input arena */
853e64868e3Sdholland 	/* (note that this clears fbuf and fblocks) */
8545924694dSmycroft 	(void)memcpy(&B, &saveB, sizeof(B));
85561f28255Scgd 
85661f28255Scgd 	(void)close(SHIN), SHIN = oSHIN;
85761f28255Scgd 	arginp = oarginp, onelflg = oonelflg;
85861f28255Scgd 	evalp = oevalp, evalvec = oevalvec;
85961f28255Scgd 	intty = oldintty, whyles = oldwhyl, gointr = ogointr;
86061f28255Scgd 	if (enterhist)
86161f28255Scgd 	    HIST = OHIST;
86261f28255Scgd 	enterhist = oenterhist;
86361f28255Scgd 	cantell = otell;
86461f28255Scgd     }
86561f28255Scgd 
86661f28255Scgd     resexit(oldexit);
86761f28255Scgd     /*
86861f28255Scgd      * If process reset() (effectively an unwind) then we must also unwind.
86961f28255Scgd      */
870ee9e50eaSmycroft     if (my_reenter)
87161f28255Scgd 	stderror(ERR_SILENT);
87261f28255Scgd     insource = oinsource;
87361f28255Scgd }
87461f28255Scgd 
87561f28255Scgd void
rechist(void)876b771e65bSwiz rechist(void)
87761f28255Scgd {
8780bf6fd0cSchristos     Char buf[BUFSIZE], hbuf[BUFSIZE], *hfile;
87961f28255Scgd     int fp, ftmp, oldidfds;
880cee2bad8Smycroft     struct varent *shist;
88161f28255Scgd 
88261f28255Scgd     if (!fast) {
883cee2bad8Smycroft 	/*
884cee2bad8Smycroft 	 * If $savehist is just set, we use the value of $history
885cee2bad8Smycroft 	 * else we use the value in $savehist
886cee2bad8Smycroft 	 */
887cee2bad8Smycroft 	if ((shist = adrof(STRsavehist)) != NULL) {
888cee2bad8Smycroft 	    if (shist->vec[0][0] != '\0')
889cee2bad8Smycroft 		(void)Strcpy(hbuf, shist->vec[0]);
890cee2bad8Smycroft 	    else if ((shist = adrof(STRhistory)) && shist->vec[0][0] != '\0')
891cee2bad8Smycroft 		(void)Strcpy(hbuf, shist->vec[0]);
892cee2bad8Smycroft 	    else
89361f28255Scgd 		return;
894cee2bad8Smycroft 	}
895cee2bad8Smycroft 	else
896cee2bad8Smycroft   	    return;
897cee2bad8Smycroft 
898cee2bad8Smycroft   	if ((hfile = value(STRhistfile)) == STRNULL) {
899cee2bad8Smycroft   	    hfile = Strcpy(buf, value(STRhome));
90061f28255Scgd   	    (void) Strcat(buf, STRsldthist);
901cee2bad8Smycroft   	}
902cee2bad8Smycroft 
903baccf0fbSmycroft   	if ((fp = open(short2str(hfile), O_WRONLY | O_CREAT | O_TRUNC,
904baccf0fbSmycroft 	    0600)) == -1)
90561f28255Scgd   	    return;
906cee2bad8Smycroft 
90761f28255Scgd 	oldidfds = didfds;
90861f28255Scgd 	didfds = 0;
90961f28255Scgd 	ftmp = SHOUT;
91061f28255Scgd 	SHOUT = fp;
911cee2bad8Smycroft 	dumphist[2] = hbuf;
912cee2bad8Smycroft 	dohist(dumphist, NULL);
91361f28255Scgd 	SHOUT = ftmp;
914cee2bad8Smycroft 	(void)close(fp);
91561f28255Scgd 	didfds = oldidfds;
91661f28255Scgd     }
91761f28255Scgd }
91861f28255Scgd 
91961f28255Scgd void
goodbye(void)920b771e65bSwiz goodbye(void)
92161f28255Scgd {
92261f28255Scgd     rechist();
92361f28255Scgd 
92461f28255Scgd     if (loginsh) {
92561f28255Scgd 	(void)signal(SIGQUIT, SIG_IGN);
92661f28255Scgd 	(void)signal(SIGINT, SIG_IGN);
92761f28255Scgd 	(void)signal(SIGTERM, SIG_IGN);
92861f28255Scgd 	setintr = 0;		/* No interrupts after "logout" */
92961f28255Scgd 	if (!(adrof(STRlogout)))
93061f28255Scgd 	    set(STRlogout, STRnormal);
93161f28255Scgd #ifdef _PATH_DOTLOGOUT
93261f28255Scgd 	(void)srcfile(_PATH_DOTLOGOUT, 0, 0);
93361f28255Scgd #endif
93461f28255Scgd 	if (adrof(STRhome))
93561f28255Scgd 	    (void)srccat(value(STRhome), STRsldtlogout);
93661f28255Scgd     }
93761f28255Scgd     exitstat();
938cdbd74daSmycroft     /* NOTREACHED */
93961f28255Scgd }
94061f28255Scgd 
941cdbd74daSmycroft __dead void
exitstat(void)942b771e65bSwiz exitstat(void)
94361f28255Scgd {
944cee2bad8Smycroft     Char *s;
94561f28255Scgd #ifdef PROF
94661f28255Scgd     monitor(0);
94761f28255Scgd #endif
94861f28255Scgd     /*
94961f28255Scgd      * Note that if STATUS is corrupted (i.e. getn bombs) then error will exit
95061f28255Scgd      * directly because we poke child here. Otherwise we might continue
95161f28255Scgd      * unwarrantedly (sic).
95261f28255Scgd      */
95361f28255Scgd     child = 1;
954cee2bad8Smycroft     s = value(STRstatus);
955cee2bad8Smycroft     xexit(s ? getn(s) : 0);
956cdbd74daSmycroft     /* NOTREACHED */
95761f28255Scgd }
95861f28255Scgd 
95961f28255Scgd /*
96061f28255Scgd  * in the event of a HUP we want to save the history
96161f28255Scgd  */
96261f28255Scgd static void
phup(int sig)963b771e65bSwiz phup(int sig)
96461f28255Scgd {
96561f28255Scgd     rechist();
9662ee028a2Scgd 
9672ee028a2Scgd     /*
9682ee028a2Scgd      * We kill the last foreground process group. It then becomes
9692ee028a2Scgd      * responsible to propagate the SIGHUP to its progeny.
9702ee028a2Scgd      */
971cee2bad8Smycroft     {
972cee2bad8Smycroft 	struct process *pp, *np;
973cee2bad8Smycroft 
9742ee028a2Scgd 	for (pp = proclist.p_next; pp; pp = pp->p_next) {
9752ee028a2Scgd 	    np = pp;
9762ee028a2Scgd 	    /*
9772ee028a2Scgd 	     * Find if this job is in the foreground. It could be that
9782ee028a2Scgd 	     * the process leader has exited and the foreground flag
9792ee028a2Scgd 	     * is cleared for it.
9802ee028a2Scgd 	     */
981cee2bad8Smycroft 	    do
9822ee028a2Scgd 		/*
9832ee028a2Scgd 		 * If a process is in the foreground; we try to kill
984a640fe8cSsnj 		 * its process group. If we succeed, then the
9852ee028a2Scgd 		 * whole job is gone. Otherwise we keep going...
9862ee028a2Scgd 		 * But avoid sending HUP to the shell again.
9872ee028a2Scgd 		 */
988cee2bad8Smycroft 		if ((np->p_flags & PFOREGND) != 0 && np->p_jobid != shpgrp &&
989556d212cSmycroft 		    kill(-np->p_jobid, SIGHUP) != -1) {
9902ee028a2Scgd 		    /* In case the job was suspended... */
991556d212cSmycroft 		    (void)kill(-np->p_jobid, SIGCONT);
9922ee028a2Scgd 		    break;
9932ee028a2Scgd 		}
994cee2bad8Smycroft 	    while ((np = np->p_friends) != pp);
9952ee028a2Scgd 	}
996cee2bad8Smycroft     }
99761f28255Scgd     xexit(sig);
998cdbd74daSmycroft     /* NOTREACHED */
99961f28255Scgd }
100061f28255Scgd 
100161f28255Scgd Char *jobargv[2] = {STRjobs, 0};
100261f28255Scgd 
100361f28255Scgd /*
100461f28255Scgd  * Catch an interrupt, e.g. during lexical input.
100561f28255Scgd  * If we are an interactive shell, we reset the interrupt catch
100661f28255Scgd  * immediately.  In any case we drain the shell output,
100761f28255Scgd  * and finally go through the normal error mechanism, which
100861f28255Scgd  * gets a chance to make the shell go away.
100961f28255Scgd  */
101061f28255Scgd /* ARGSUSED */
101161f28255Scgd void
pintr(int notused)1012b771e65bSwiz pintr(int notused)
101361f28255Scgd {
101461f28255Scgd     pintr1(1);
1015ee9e50eaSmycroft     /* NOTREACHED */
101661f28255Scgd }
101761f28255Scgd 
101861f28255Scgd void
pintr1(int wantnl)1019b79c2ef2Schristos pintr1(int wantnl)
102061f28255Scgd {
1021cee2bad8Smycroft     Char **v;
1022b3df6303Skleink     sigset_t nsigset, osigset;
102361f28255Scgd 
1024b3df6303Skleink     sigemptyset(&nsigset);
1025b3df6303Skleink     (void)sigprocmask(SIG_BLOCK, &nsigset, &osigset);
102661f28255Scgd     if (setintr) {
1027b3df6303Skleink 	nsigset = osigset;
1028b3df6303Skleink 	(void)sigdelset(&nsigset, SIGINT);
1029b3df6303Skleink 	(void)sigprocmask(SIG_SETMASK, &nsigset, NULL);
103061f28255Scgd 	if (pjobs) {
103161f28255Scgd 	    pjobs = 0;
1032cee2bad8Smycroft 	    (void)fprintf(cshout, "\n");
1033cee2bad8Smycroft 	    dojobs(jobargv, NULL);
103461f28255Scgd 	    stderror(ERR_NAME | ERR_INTR);
103561f28255Scgd 	}
103661f28255Scgd     }
10375924694dSmycroft     (void)sigdelset(&osigset, SIGCHLD);
10385924694dSmycroft     (void)sigprocmask(SIG_SETMASK, &osigset, NULL);
1039cee2bad8Smycroft     (void)fpurge(cshout);
104061f28255Scgd     (void)endpwent();
104161f28255Scgd 
104261f28255Scgd     /*
104361f28255Scgd      * If we have an active "onintr" then we search for the label. Note that if
104461f28255Scgd      * one does "onintr -" then we shan't be interruptible so we needn't worry
104561f28255Scgd      * about that here.
104661f28255Scgd      */
104761f28255Scgd     if (gointr) {
1048cee2bad8Smycroft 	gotolab(gointr);
104961f28255Scgd 	timflg = 0;
1050cee2bad8Smycroft 	if ((v = pargv) != NULL)
105161f28255Scgd 	    pargv = 0, blkfree(v);
1052cee2bad8Smycroft 	if ((v = gargv) != NULL)
105361f28255Scgd 	    gargv = 0, blkfree(v);
105461f28255Scgd 	reset();
105561f28255Scgd     }
105661f28255Scgd     else if (intty && wantnl) {
1057cee2bad8Smycroft 	(void)fputc('\r', cshout);
1058cee2bad8Smycroft 	(void)fputc('\n', cshout);
105961f28255Scgd     }
106061f28255Scgd     stderror(ERR_SILENT);
1061cdbd74daSmycroft     /* NOTREACHED */
106261f28255Scgd }
106361f28255Scgd 
106461f28255Scgd /*
106561f28255Scgd  * Process is the main driving routine for the shell.
106661f28255Scgd  * It runs all command processing, except for those within { ... }
106761f28255Scgd  * in expressions (which is run by a routine evalav in sh.exp.c which
106861f28255Scgd  * is a stripped down process), and `...` evaluation which is run
106961f28255Scgd  * also by a subset of this code in sh.glob.c in the routine backeval.
107061f28255Scgd  *
107161f28255Scgd  * The code here is a little strange because part of it is interruptible
107261f28255Scgd  * and hence freeing of structures appears to occur when none is necessary
107361f28255Scgd  * if this is ignored.
107461f28255Scgd  *
107561f28255Scgd  * Note that if catch is not set then we will unwind on any error.
107661f28255Scgd  * If an end-of-file occurs, we return.
107761f28255Scgd  */
1078cee2bad8Smycroft static struct command *savet = NULL;
1079b771e65bSwiz 
108061f28255Scgd void
process(int catch)1081b79c2ef2Schristos process(int catch)
108261f28255Scgd {
1083b771e65bSwiz     struct command *t;
108461f28255Scgd     jmp_buf osetexit;
1085b3df6303Skleink     sigset_t nsigset;
108661f28255Scgd 
1087b771e65bSwiz     t = savet;
1088cee2bad8Smycroft     savet = NULL;
108961f28255Scgd     getexit(osetexit);
109061f28255Scgd     for (;;) {
109161f28255Scgd 	pendjob();
109261f28255Scgd 	paraml.next = paraml.prev = &paraml;
109361f28255Scgd 	paraml.word = STRNULL;
109461f28255Scgd 	(void)setexit();
109561f28255Scgd 	justpr = enterhist;	/* execute if not entering history */
109661f28255Scgd 
109761f28255Scgd 	/*
109861f28255Scgd 	 * Interruptible during interactive reads
109961f28255Scgd 	 */
11007b38403cSmycroft 	if (setintr) {
1101b3df6303Skleink 	    sigemptyset(&nsigset);
1102b3df6303Skleink 	    (void)sigaddset(&nsigset, SIGINT);
1103b3df6303Skleink 	    (void)sigprocmask(SIG_UNBLOCK, &nsigset, NULL);
11047b38403cSmycroft 	}
110561f28255Scgd 
110661f28255Scgd 	/*
110761f28255Scgd 	 * For the sake of reset()
110861f28255Scgd 	 */
110961f28255Scgd 	freelex(&paraml);
1110cee2bad8Smycroft 	if (savet)
1111cee2bad8Smycroft 	    freesyn(savet), savet = NULL;
111261f28255Scgd 
111361f28255Scgd 	if (haderr) {
111461f28255Scgd 	    if (!catch) {
111561f28255Scgd 		/* unwind */
111661f28255Scgd 		doneinp = 0;
111761f28255Scgd 		resexit(osetexit);
1118cee2bad8Smycroft 		savet = t;
111961f28255Scgd 		reset();
112061f28255Scgd 	    }
112161f28255Scgd 	    haderr = 0;
112261f28255Scgd 	    /*
112361f28255Scgd 	     * Every error is eventually caught here or the shell dies.  It is
112461f28255Scgd 	     * at this point that we clean up any left-over open files, by
112561f28255Scgd 	     * closing all but a fixed number of pre-defined files.  Thus
112661f28255Scgd 	     * routines don't have to worry about leaving files open due to
112761f28255Scgd 	     * deeper errors... they will get closed here.
112861f28255Scgd 	     */
112961f28255Scgd 	    closem();
113061f28255Scgd 	    continue;
113161f28255Scgd 	}
113261f28255Scgd 	if (doneinp) {
113361f28255Scgd 	    doneinp = 0;
113461f28255Scgd 	    break;
113561f28255Scgd 	}
113661f28255Scgd 	if (chkstop)
113761f28255Scgd 	    chkstop--;
113861f28255Scgd 	if (neednote)
113961f28255Scgd 	    pnote();
114061f28255Scgd 	if (intty && prompt && evalvec == 0) {
114161f28255Scgd 	    mailchk();
1142*84e62ad1Smartin #ifdef EDIT
114329c28744Schristos 	    updateediting();
1144*84e62ad1Smartin #endif
114561f28255Scgd 	    /*
114661f28255Scgd 	     * If we are at the end of the input buffer then we are going to
114761f28255Scgd 	     * read fresh stuff. Otherwise, we are rereading input and don't
114861f28255Scgd 	     * need or want to prompt.
114961f28255Scgd 	     */
1150cee2bad8Smycroft 	    if (aret == F_SEEK && fseekp == feobp)
115161f28255Scgd 		printprompt();
1152cee2bad8Smycroft 	    (void)fflush(cshout);
115361f28255Scgd 	}
115461f28255Scgd 	if (seterr) {
11557405c8c3Schristos 	    free(seterr);
115661f28255Scgd 	    seterr = NULL;
115761f28255Scgd 	}
115861f28255Scgd 
11592795a372Schristos 
116061f28255Scgd 	/*
116161f28255Scgd 	 * Echo not only on VERBOSE, but also with history expansion. If there
116261f28255Scgd 	 * is a lexical error then we forego history echo.
116361f28255Scgd 	 */
1164cee2bad8Smycroft 	if ((lex(&paraml) && !seterr && intty) || adrof(STRverbose)) {
1165971f3382Schristos 	    int odidfds = didfds;
1166971f3382Schristos 	    fflush(csherr);
1167971f3382Schristos 	    didfds = 0;
1168cee2bad8Smycroft 	    prlex(csherr, &paraml);
1169971f3382Schristos 	    fflush(csherr);
1170971f3382Schristos 	    didfds = odidfds;
117161f28255Scgd 	}
117261f28255Scgd 
117361f28255Scgd 	/*
117461f28255Scgd 	 * The parser may lose space if interrupted.
117561f28255Scgd 	 */
117661f28255Scgd 	if (setintr)
1177b3df6303Skleink 	    (void)sigprocmask(SIG_BLOCK, &nsigset, NULL);
117861f28255Scgd 
117961f28255Scgd 	/*
118061f28255Scgd 	 * Save input text on the history list if reading in old history, or it
118161f28255Scgd 	 * is from the terminal at the top level and not in a loop.
118261f28255Scgd 	 *
118361f28255Scgd 	 * PWP: entry of items in the history list while in a while loop is done
118461f28255Scgd 	 * elsewhere...
118561f28255Scgd 	 */
1186cee2bad8Smycroft 	if (enterhist || (catch && intty && !whyles))
118761f28255Scgd 	    savehist(&paraml);
118861f28255Scgd 
118961f28255Scgd 	/*
119061f28255Scgd 	 * Print lexical error messages, except when sourcing history lists.
119161f28255Scgd 	 */
1192ee9e50eaSmycroft 	if (!enterhist && seterr)
119361f28255Scgd 	    stderror(ERR_OLD);
119461f28255Scgd 
119561f28255Scgd 	/*
119661f28255Scgd 	 * If had a history command :p modifier then this is as far as we
119761f28255Scgd 	 * should go
119861f28255Scgd 	 */
119961f28255Scgd 	if (justpr)
120061f28255Scgd 	    reset();
120161f28255Scgd 
120261f28255Scgd 	alias(&paraml);
120361f28255Scgd 
120461f28255Scgd 	/*
120561f28255Scgd 	 * Parse the words of the input into a parse tree.
120661f28255Scgd 	 */
1207cee2bad8Smycroft 	savet = syntax(paraml.next, &paraml, 0);
1208ee9e50eaSmycroft 	if (seterr)
120961f28255Scgd 	    stderror(ERR_OLD);
121061f28255Scgd 
1211cee2bad8Smycroft 	execute(savet, (tpgrp > 0 ? tpgrp : -1), NULL, NULL);
121261f28255Scgd 
121361f28255Scgd 	/*
121461f28255Scgd 	 * Made it!
121561f28255Scgd 	 */
121661f28255Scgd 	freelex(&paraml);
121784eb3246Sdholland 	freesyn(savet), savet = NULL;
121861f28255Scgd     }
121961f28255Scgd     resexit(osetexit);
1220cee2bad8Smycroft     savet = t;
122161f28255Scgd }
122261f28255Scgd 
122361f28255Scgd void
1224cee2bad8Smycroft /*ARGSUSED*/
dosource(Char ** v,struct command * t)1225b771e65bSwiz dosource(Char **v, struct command *t)
122661f28255Scgd {
1227b771e65bSwiz     Char buf[BUFSIZE], *f;
1228b79c2ef2Schristos     int hflg;
122961f28255Scgd 
1230b771e65bSwiz     hflg = 0;
1231cee2bad8Smycroft     v++;
1232cee2bad8Smycroft     if (*v && eq(*v, STRmh)) {
1233ee9e50eaSmycroft 	if (*++v == NULL)
123461f28255Scgd 	    stderror(ERR_NAME | ERR_HFLAG);
123561f28255Scgd 	hflg++;
123661f28255Scgd     }
1237cee2bad8Smycroft     (void)Strcpy(buf, *v);
123861f28255Scgd     f = globone(buf, G_ERROR);
123961f28255Scgd     (void)strcpy((char *)buf, short2str(f));
12407405c8c3Schristos     free(f);
1241ee9e50eaSmycroft     if (!srcfile((char *)buf, 0, hflg) && !hflg)
124261f28255Scgd 	stderror(ERR_SYSTEM, (char *)buf, strerror(errno));
124361f28255Scgd }
124461f28255Scgd 
124561f28255Scgd /*
124661f28255Scgd  * Check for mail.
124761f28255Scgd  * If we are a login shell, then we don't want to tell
124861f28255Scgd  * about any mail file unless its been modified
124961f28255Scgd  * after the time we started.
125061f28255Scgd  * This prevents us from telling the user things he already
125161f28255Scgd  * knows, since the login program insists on saying
125261f28255Scgd  * "You have mail."
125361f28255Scgd  */
125461f28255Scgd static void
mailchk(void)1255b771e65bSwiz mailchk(void)
125661f28255Scgd {
1257b771e65bSwiz     struct stat stb;
125876adbe2bStls     struct varent *v;
125976adbe2bStls     Char **vp;
126061f28255Scgd     time_t t;
1261b771e65bSwiz     int cnt, intvl;
1262b79c2ef2Schristos     int new;
126361f28255Scgd 
126461f28255Scgd     v = adrof(STRmail);
126561f28255Scgd     if (v == 0)
126661f28255Scgd 	return;
126761f28255Scgd     (void)time(&t);
126861f28255Scgd     vp = v->vec;
126961f28255Scgd     cnt = blklen(vp);
127061f28255Scgd     intvl = (cnt && number(*vp)) ? (--cnt, getn(*vp++)) : MAILINTVL;
127161f28255Scgd     if (intvl < 1)
127261f28255Scgd 	intvl = 1;
127361f28255Scgd     if (chktim + intvl > t)
127461f28255Scgd 	return;
127561f28255Scgd     for (; *vp; vp++) {
127661f28255Scgd 	if (stat(short2str(*vp), &stb) < 0)
127761f28255Scgd 	    continue;
127861f28255Scgd 	new = stb.st_mtime > time0.tv_sec;
127961f28255Scgd 	if (stb.st_size == 0 || stb.st_atime > stb.st_mtime ||
128061f28255Scgd 	    (stb.st_atime < chktim && stb.st_mtime < chktim) ||
1281cee2bad8Smycroft 	    (loginsh && !new))
128261f28255Scgd 	    continue;
128361f28255Scgd 	if (cnt == 1)
1284cee2bad8Smycroft 	    (void)fprintf(cshout, "You have %smail.\n", new ? "new " : "");
128561f28255Scgd 	else
1286cee2bad8Smycroft 	    (void)fprintf(cshout, "%s in %s.\n", new ? "New mail" : "Mail",
1287cee2bad8Smycroft 			   vis_str(*vp));
128861f28255Scgd     }
128961f28255Scgd     chktim = t;
129061f28255Scgd }
129161f28255Scgd 
129261f28255Scgd /*
129361f28255Scgd  * Extract a home directory from the password file
129461f28255Scgd  * The argument points to a buffer where the name of the
129561f28255Scgd  * user whose home directory is sought is currently.
129661f28255Scgd  * We write the home directory of the user back there.
129761f28255Scgd  */
129861f28255Scgd int
gethdir(Char * home)1299b771e65bSwiz gethdir(Char *home)
130061f28255Scgd {
130161f28255Scgd     struct passwd *pw;
1302b771e65bSwiz     Char *h;
130361f28255Scgd 
130461f28255Scgd     /*
130561f28255Scgd      * Is it us?
130661f28255Scgd      */
130761f28255Scgd     if (*home == '\0') {
1308cee2bad8Smycroft 	if ((h = value(STRhome)) != NULL) {
130961f28255Scgd 	    (void)Strcpy(home, h);
131061f28255Scgd 	    return 0;
131161f28255Scgd 	}
131261f28255Scgd 	else
131361f28255Scgd 	    return 1;
131461f28255Scgd     }
131561f28255Scgd 
1316cee2bad8Smycroft     if ((pw = getpwnam(short2str(home))) != NULL) {
131761f28255Scgd 	(void)Strcpy(home, str2short(pw->pw_dir));
131861f28255Scgd 	return 0;
131961f28255Scgd     }
132061f28255Scgd     else
132161f28255Scgd 	return 1;
132261f28255Scgd }
132361f28255Scgd 
132461f28255Scgd /*
1325cee2bad8Smycroft  * When didfds is set, we do I/O from 0, 1, 2 otherwise from 15, 16, 17
1326a2278794Swiz  * We also check if the shell has already changed the descriptor to point to
1327cee2bad8Smycroft  * 0, 1, 2 when didfds is set.
1328cee2bad8Smycroft  */
1329cee2bad8Smycroft #define DESC(a) (*((int *) (a)) - (didfds && *((int *) a) >= FSHIN ? FSHIN : 0))
1330cee2bad8Smycroft 
1331c24abf82Schristos static ssize_t
readf(void * oreo,void * buf,size_t siz)1332c24abf82Schristos readf(void *oreo, void *buf, size_t siz)
1333cee2bad8Smycroft {
1334cee2bad8Smycroft     return read(DESC(oreo), buf, siz);
1335cee2bad8Smycroft }
1336cee2bad8Smycroft 
1337cee2bad8Smycroft 
1338c24abf82Schristos static ssize_t
writef(void * oreo,const void * buf,size_t siz)1339c24abf82Schristos writef(void *oreo, const void *buf, size_t siz)
1340cee2bad8Smycroft {
1341cee2bad8Smycroft     return write(DESC(oreo), buf, siz);
1342cee2bad8Smycroft }
1343cee2bad8Smycroft 
134416ed1862Schristos static off_t
seekf(void * oreo,off_t off,int whence)134516ed1862Schristos seekf(void *oreo, off_t off, int whence)
1346cee2bad8Smycroft {
1347cee2bad8Smycroft     return lseek(DESC(oreo), off, whence);
1348cee2bad8Smycroft }
1349cee2bad8Smycroft 
1350cee2bad8Smycroft 
1351cee2bad8Smycroft static int
closef(void * oreo)1352b771e65bSwiz closef(void *oreo)
1353cee2bad8Smycroft {
1354cee2bad8Smycroft     return close(DESC(oreo));
1355cee2bad8Smycroft }
1356cee2bad8Smycroft 
1357cee2bad8Smycroft 
1358cee2bad8Smycroft /*
1359cee2bad8Smycroft  * Print the visible version of a string.
1360cee2bad8Smycroft  */
1361cee2bad8Smycroft int
vis_fputc(int ch,FILE * fp)1362b771e65bSwiz vis_fputc(int ch, FILE *fp)
1363cee2bad8Smycroft {
1364cee2bad8Smycroft     char uenc[5];	/* 4 + NULL */
1365cee2bad8Smycroft 
1366cee2bad8Smycroft     if (ch & QUOTE)
1367cee2bad8Smycroft 	return fputc(ch & TRIM, fp);
1368cee2bad8Smycroft     /*
1369cee2bad8Smycroft      * XXX: When we are in AsciiOnly we want all characters >= 0200 to
1370cee2bad8Smycroft      * be encoded, but currently there is no way in vis to do that.
1371cee2bad8Smycroft      */
1372cee2bad8Smycroft     (void)vis(uenc, ch & TRIM, VIS_NOSLASH, 0);
1373b771e65bSwiz     return (fputs(uenc, fp));
1374cee2bad8Smycroft }
1375cee2bad8Smycroft 
1376cee2bad8Smycroft /*
137761f28255Scgd  * Move the initial descriptors to their eventual
137898c072dbSwiz  * resting places, closing all other units.
137961f28255Scgd  */
138061f28255Scgd void
initdesc(void)1381b771e65bSwiz initdesc(void)
138261f28255Scgd {
138361f28255Scgd     didfds = 0;			/* 0, 1, 2 aren't set up */
138461f28255Scgd     (void)ioctl(SHIN = dcopy(0, FSHIN), FIOCLEX, NULL);
138561f28255Scgd     (void)ioctl(SHOUT = dcopy(1, FSHOUT), FIOCLEX, NULL);
1386cee2bad8Smycroft     (void)ioctl(SHERR = dcopy(2, FSHERR), FIOCLEX, NULL);
138761f28255Scgd     (void)ioctl(OLDSTD = dcopy(SHIN, FOLDSTD), FIOCLEX, NULL);
138861f28255Scgd     closem();
138961f28255Scgd }
139061f28255Scgd 
139161f28255Scgd 
1392cdbd74daSmycroft __dead void
139361f28255Scgd #ifdef PROF
done(int i)1394b771e65bSwiz done(int i)
139561f28255Scgd #else
1396b771e65bSwiz xexit(int i)
139761f28255Scgd #endif
139861f28255Scgd {
139961f28255Scgd     untty();
140061f28255Scgd     _exit(i);
1401cdbd74daSmycroft     /* NOTREACHED */
140261f28255Scgd }
140361f28255Scgd 
14040f668275Sfair #ifndef _PATH_DEFPATH
140561f28255Scgd static Char **
defaultpath(void)1406b771e65bSwiz defaultpath(void)
140761f28255Scgd {
140861f28255Scgd     struct stat stb;
1409b771e65bSwiz     Char **blk, **blkp;
1410b771e65bSwiz     char *ptr;
141161f28255Scgd 
1412c24abf82Schristos     blkp = blk = xmalloc((size_t) sizeof(Char *) * 10);
141361f28255Scgd 
141461f28255Scgd #define DIRAPPEND(a)  \
1415f5ad44b6Smycroft 	if (stat(ptr = a, &stb) == 0 && S_ISDIR(stb.st_mode)) \
141661f28255Scgd 		*blkp++ = SAVE(ptr)
1417287d684bSchristos #ifdef RESCUEDIR
1418287d684bSchristos     DIRAPPEND(RESCUEDIR);
1419287d684bSchristos #endif
142061f28255Scgd     DIRAPPEND(_PATH_BIN);
142161f28255Scgd     DIRAPPEND(_PATH_USRBIN);
142261f28255Scgd 
142361f28255Scgd #undef DIRAPPEND
142461f28255Scgd 
14254d643bf2Smycroft #if 0
1426cee2bad8Smycroft     if (euid != 0 && uid != 0)
142761f28255Scgd 	*blkp++ = Strsave(STRdot);
14284d643bf2Smycroft #endif
14294d643bf2Smycroft 
143061f28255Scgd     *blkp = NULL;
143161f28255Scgd     return (blk);
143261f28255Scgd }
14330f668275Sfair #endif /* _PATH_DEFPATH */
143461f28255Scgd 
143561f28255Scgd void
printprompt(void)1436b771e65bSwiz printprompt(void)
143761f28255Scgd {
143876adbe2bStls     Char *cp;
143961f28255Scgd 
14409fabbb70Schristos     if (editing)
14419fabbb70Schristos 	return;
14429fabbb70Schristos 
144361f28255Scgd     if (!whyles) {
144461f28255Scgd 	for (cp = value(STRprompt); *cp; cp++)
144561f28255Scgd 	    if (*cp == HIST)
1446cee2bad8Smycroft 		(void)fprintf(cshout, "%d", eventno + 1);
144761f28255Scgd 	    else {
144861f28255Scgd 		if (*cp == '\\' && cp[1] == HIST)
144961f28255Scgd 		    cp++;
1450cee2bad8Smycroft 		(void)vis_fputc(*cp | QUOTE, cshout);
145161f28255Scgd 	    }
145261f28255Scgd     }
145361f28255Scgd     else
145461f28255Scgd 	/*
145561f28255Scgd 	 * Prompt for forward reading loop body content.
145661f28255Scgd 	 */
1457cee2bad8Smycroft 	(void)fprintf(cshout, "? ");
1458cee2bad8Smycroft     (void)fflush(cshout);
145961f28255Scgd }
14609fabbb70Schristos 
14619fabbb70Schristos #ifdef EDIT
14629fabbb70Schristos char *
printpromptstr(EditLine * elx)14639fabbb70Schristos printpromptstr(EditLine *elx) {
14649fabbb70Schristos     static char pbuf[1024];
14659fabbb70Schristos     static char qspace[] = "? ";
14669fabbb70Schristos     Char *cp;
14679fabbb70Schristos     size_t i;
14689fabbb70Schristos 
14699fabbb70Schristos     if (whyles)
14709fabbb70Schristos 	return qspace;
14719fabbb70Schristos 
14729fabbb70Schristos     i = 0;
14739fabbb70Schristos     for (cp = value(STRprompt); *cp; cp++) {
14749fabbb70Schristos 	if (i >= sizeof(pbuf))
14759fabbb70Schristos 	    break;
1476c24abf82Schristos 	if (*cp == HIST) {
1477c24abf82Schristos 	    int r;
1478c24abf82Schristos 	    r = snprintf(pbuf + i, sizeof(pbuf) - i, "%d", eventno + 1);
1479c24abf82Schristos 	    if (r > 0)
1480c24abf82Schristos 		i += (size_t)r;
1481c24abf82Schristos 	} else
1482c24abf82Schristos 	    pbuf[i++] = (char)*cp;
14839fabbb70Schristos     }
14849fabbb70Schristos     if (i >= sizeof(pbuf))
14859fabbb70Schristos 	i = sizeof(pbuf) - 1;
14869fabbb70Schristos     pbuf[i] = '\0';
14879fabbb70Schristos     return pbuf;
14889fabbb70Schristos }
14899fabbb70Schristos #endif
1490