Home | History | Annotate | Line # | Download | only in csh
csh.c revision 1.1
      1  1.1  cgd /*-
      2  1.1  cgd  * Copyright (c) 1980, 1991 The Regents of the University of California.
      3  1.1  cgd  * All rights reserved.
      4  1.1  cgd  *
      5  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1  cgd  * modification, are permitted provided that the following conditions
      7  1.1  cgd  * are met:
      8  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1  cgd  *    must display the following acknowledgement:
     15  1.1  cgd  *	This product includes software developed by the University of
     16  1.1  cgd  *	California, Berkeley and its contributors.
     17  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1  cgd  *    may be used to endorse or promote products derived from this software
     19  1.1  cgd  *    without specific prior written permission.
     20  1.1  cgd  *
     21  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1  cgd  * SUCH DAMAGE.
     32  1.1  cgd  */
     33  1.1  cgd 
     34  1.1  cgd #ifndef lint
     35  1.1  cgd char copyright[] =
     36  1.1  cgd "@(#) Copyright (c) 1991 The Regents of the University of California.\n\
     37  1.1  cgd  All rights reserved.\n";
     38  1.1  cgd #endif /* not lint */
     39  1.1  cgd 
     40  1.1  cgd #ifndef lint
     41  1.1  cgd static char sccsid[] = "@(#)csh.c	5.26 (Berkeley) 6/8/91";
     42  1.1  cgd #endif /* not lint */
     43  1.1  cgd 
     44  1.1  cgd #include <sys/types.h>
     45  1.1  cgd #include <sys/ioctl.h>
     46  1.1  cgd #include <sys/stat.h>
     47  1.1  cgd #include <fcntl.h>
     48  1.1  cgd #include <errno.h>
     49  1.1  cgd #include <pwd.h>
     50  1.1  cgd #include <stdlib.h>
     51  1.1  cgd #include <string.h>
     52  1.1  cgd #include <locale.h>
     53  1.1  cgd #include <unistd.h>
     54  1.1  cgd #if __STDC__
     55  1.1  cgd # include <stdarg.h>
     56  1.1  cgd #else
     57  1.1  cgd # include <varargs.h>
     58  1.1  cgd #endif
     59  1.1  cgd 
     60  1.1  cgd #include "csh.h"
     61  1.1  cgd #include "extern.h"
     62  1.1  cgd #include "pathnames.h"
     63  1.1  cgd 
     64  1.1  cgd extern bool MapsAreInited;
     65  1.1  cgd extern bool NLSMapsAreInited;
     66  1.1  cgd 
     67  1.1  cgd /*
     68  1.1  cgd  * C Shell
     69  1.1  cgd  *
     70  1.1  cgd  * Bill Joy, UC Berkeley, California, USA
     71  1.1  cgd  * October 1978, May 1980
     72  1.1  cgd  *
     73  1.1  cgd  * Jim Kulp, IIASA, Laxenburg, Austria
     74  1.1  cgd  * April 1980
     75  1.1  cgd  *
     76  1.1  cgd  * Christos Zoulas, Cornell University
     77  1.1  cgd  * June, 1991
     78  1.1  cgd  */
     79  1.1  cgd 
     80  1.1  cgd Char   *dumphist[] = {STRhistory, STRmh, 0, 0};
     81  1.1  cgd Char   *loadhist[] = {STRsource, STRmh, STRhistfile, 0};
     82  1.1  cgd 
     83  1.1  cgd int     nofile = 0;
     84  1.1  cgd bool    reenter = 0;
     85  1.1  cgd bool    nverbose = 0;
     86  1.1  cgd bool    nexececho = 0;
     87  1.1  cgd bool    quitit = 0;
     88  1.1  cgd bool    fast = 0;
     89  1.1  cgd bool    batch = 0;
     90  1.1  cgd bool    mflag = 0;
     91  1.1  cgd bool    prompt = 1;
     92  1.1  cgd bool    enterhist = 0;
     93  1.1  cgd bool    tellwhat = 0;
     94  1.1  cgd 
     95  1.1  cgd extern char **environ;
     96  1.1  cgd 
     97  1.1  cgd static int	srccat __P((Char *, Char *));
     98  1.1  cgd static int	srcfile __P((char *, bool, bool));
     99  1.1  cgd static void	phup __P((int));
    100  1.1  cgd static void	srcunit __P((int, bool, bool));
    101  1.1  cgd static void	mailchk __P((void));
    102  1.1  cgd static Char   **defaultpath __P((void));
    103  1.1  cgd 
    104  1.1  cgd int
    105  1.1  cgd main(argc, argv)
    106  1.1  cgd     int     argc;
    107  1.1  cgd     char  **argv;
    108  1.1  cgd {
    109  1.1  cgd     register Char *cp;
    110  1.1  cgd     register char *tcp;
    111  1.1  cgd     register int f;
    112  1.1  cgd     register char **tempv;
    113  1.1  cgd     struct sigvec osv;
    114  1.1  cgd 
    115  1.1  cgd 
    116  1.1  cgd     settimes();			/* Immed. estab. timing base */
    117  1.1  cgd 
    118  1.1  cgd     /*
    119  1.1  cgd      * Initialize non constant strings
    120  1.1  cgd      */
    121  1.1  cgd #ifdef _PATH_BSHELL
    122  1.1  cgd     STR_BSHELL = SAVE(_PATH_BSHELL);
    123  1.1  cgd #endif
    124  1.1  cgd #ifdef _PATH_CSHELL
    125  1.1  cgd     STR_SHELLPATH = SAVE(_PATH_CSHELL);
    126  1.1  cgd #endif
    127  1.1  cgd     STR_environ = blk2short(environ);
    128  1.1  cgd     environ = short2blk(STR_environ);	/* So that we can free it */
    129  1.1  cgd     STR_WORD_CHARS = SAVE(WORD_CHARS);
    130  1.1  cgd 
    131  1.1  cgd     HIST = '!';
    132  1.1  cgd     HISTSUB = '^';
    133  1.1  cgd     word_chars = STR_WORD_CHARS;
    134  1.1  cgd 
    135  1.1  cgd     tempv = argv;
    136  1.1  cgd     if (eq(str2short(tempv[0]), STRaout))	/* A.out's are quittable */
    137  1.1  cgd 	quitit = 1;
    138  1.1  cgd     uid = getuid();
    139  1.1  cgd     gid = getgid();
    140  1.1  cgd     /*
    141  1.1  cgd      * We are a login shell if: 1. we were invoked as -<something> and we had
    142  1.1  cgd      * no arguments 2. or we were invoked only with the -l flag
    143  1.1  cgd      */
    144  1.1  cgd     loginsh = (**tempv == '-' && argc == 1) ||
    145  1.1  cgd 	(argc == 2 && tempv[1][0] == '-' && tempv[1][1] == 'l' &&
    146  1.1  cgd 	 tempv[1][2] == '\0');
    147  1.1  cgd 
    148  1.1  cgd     if (loginsh && **tempv != '-') {
    149  1.1  cgd 	/*
    150  1.1  cgd 	 * Mangle the argv space
    151  1.1  cgd 	 */
    152  1.1  cgd 	tempv[1][0] = '\0';
    153  1.1  cgd 	tempv[1][1] = '\0';
    154  1.1  cgd 	tempv[1] = NULL;
    155  1.1  cgd 	for (tcp = *tempv; *tcp++;);
    156  1.1  cgd 	for (tcp--; tcp >= *tempv; tcp--)
    157  1.1  cgd 	    tcp[1] = tcp[0];
    158  1.1  cgd 	*++tcp = '-';
    159  1.1  cgd 	argc--;
    160  1.1  cgd     }
    161  1.1  cgd     if (loginsh)
    162  1.1  cgd 	(void) time(&chktim);
    163  1.1  cgd 
    164  1.1  cgd     AsciiOnly = 1;
    165  1.1  cgd #ifdef NLS
    166  1.1  cgd     (void) setlocale(LC_ALL, "");
    167  1.1  cgd     {
    168  1.1  cgd 	int     k;
    169  1.1  cgd 
    170  1.1  cgd 	for (k = 0200; k <= 0377 && !Isprint(k); k++);
    171  1.1  cgd 	AsciiOnly = k > 0377;
    172  1.1  cgd     }
    173  1.1  cgd #else
    174  1.1  cgd     AsciiOnly = getenv("LANG") == NULL && getenv("LC_CTYPE") == NULL;
    175  1.1  cgd #endif				/* NLS */
    176  1.1  cgd 
    177  1.1  cgd     /*
    178  1.1  cgd      * Move the descriptors to safe places. The variable didfds is 0 while we
    179  1.1  cgd      * have only FSH* to work with. When didfds is true, we have 0,1,2 and
    180  1.1  cgd      * prefer to use these.
    181  1.1  cgd      */
    182  1.1  cgd     initdesc();
    183  1.1  cgd 
    184  1.1  cgd     /*
    185  1.1  cgd      * Initialize the shell variables. ARGV and PROMPT are initialized later.
    186  1.1  cgd      * STATUS is also munged in several places. CHILD is munged when
    187  1.1  cgd      * forking/waiting
    188  1.1  cgd      */
    189  1.1  cgd     set(STRstatus, Strsave(STR0));
    190  1.1  cgd 
    191  1.1  cgd     if ((tcp = getenv("HOME")) != NULL)
    192  1.1  cgd 	cp = SAVE(tcp);
    193  1.1  cgd     else
    194  1.1  cgd 	cp = NULL;
    195  1.1  cgd 
    196  1.1  cgd     if (cp == NULL)
    197  1.1  cgd 	fast = 1;		/* No home -> can't read scripts */
    198  1.1  cgd     else
    199  1.1  cgd 	set(STRhome, cp);
    200  1.1  cgd     dinit(cp);			/* dinit thinks that HOME == cwd in a login
    201  1.1  cgd 				 * shell */
    202  1.1  cgd     /*
    203  1.1  cgd      * Grab other useful things from the environment. Should we grab
    204  1.1  cgd      * everything??
    205  1.1  cgd      */
    206  1.1  cgd     if ((tcp = getenv("LOGNAME")) != NULL ||
    207  1.1  cgd 	(tcp = getenv("USER")) != NULL)
    208  1.1  cgd 	set(STRuser, SAVE(tcp));
    209  1.1  cgd     if ((tcp = getenv("TERM")) != NULL)
    210  1.1  cgd 	set(STRterm, SAVE(tcp));
    211  1.1  cgd 
    212  1.1  cgd     /*
    213  1.1  cgd      * Re-initialize path if set in environment
    214  1.1  cgd      */
    215  1.1  cgd     if ((tcp = getenv("PATH")) == NULL)
    216  1.1  cgd 	set1(STRpath, defaultpath(), &shvhed);
    217  1.1  cgd     else
    218  1.1  cgd 	importpath(SAVE(tcp));
    219  1.1  cgd 
    220  1.1  cgd     set(STRshell, Strsave(STR_SHELLPATH));
    221  1.1  cgd 
    222  1.1  cgd     doldol = putn((int) getpid());	/* For $$ */
    223  1.1  cgd     shtemp = Strspl(STRtmpsh, doldol);	/* For << */
    224  1.1  cgd 
    225  1.1  cgd     /*
    226  1.1  cgd      * Record the interrupt states from the parent process. If the parent is
    227  1.1  cgd      * non-interruptible our hand must be forced or we (and our children) won't
    228  1.1  cgd      * be either. Our children inherit termination from our parent. We catch it
    229  1.1  cgd      * only if we are the login shell.
    230  1.1  cgd      */
    231  1.1  cgd     /* parents interruptibility */
    232  1.1  cgd     (void) sigvec(SIGINT, NULL, &osv);
    233  1.1  cgd     parintr = (void (*) ()) osv.sv_handler;
    234  1.1  cgd     (void) sigvec(SIGTERM, NULL, &osv);
    235  1.1  cgd     parterm = (void (*) ()) osv.sv_handler;
    236  1.1  cgd 
    237  1.1  cgd     if (loginsh) {
    238  1.1  cgd 	(void) signal(SIGHUP, phup);	/* exit processing on HUP */
    239  1.1  cgd 	(void) signal(SIGXCPU, phup);	/* ...and on XCPU */
    240  1.1  cgd 	(void) signal(SIGXFSZ, phup);	/* ...and on XFSZ */
    241  1.1  cgd     }
    242  1.1  cgd 
    243  1.1  cgd     /*
    244  1.1  cgd      * Process the arguments.
    245  1.1  cgd      *
    246  1.1  cgd      * Note that processing of -v/-x is actually delayed till after script
    247  1.1  cgd      * processing.
    248  1.1  cgd      *
    249  1.1  cgd      * We set the first character of our name to be '-' if we are a shell running
    250  1.1  cgd      * interruptible commands.  Many programs which examine ps'es use this to
    251  1.1  cgd      * filter such shells out.
    252  1.1  cgd      */
    253  1.1  cgd     argc--, tempv++;
    254  1.1  cgd     while (argc > 0 && (tcp = tempv[0])[0] == '-' && *++tcp != '\0' && !batch) {
    255  1.1  cgd 	do
    256  1.1  cgd 	    switch (*tcp++) {
    257  1.1  cgd 
    258  1.1  cgd 	    case 0:		/* -	Interruptible, no prompt */
    259  1.1  cgd 		prompt = 0;
    260  1.1  cgd 		setintr = 1;
    261  1.1  cgd 		nofile = 1;
    262  1.1  cgd 		break;
    263  1.1  cgd 
    264  1.1  cgd 	    case 'b':		/* -b	Next arg is input file */
    265  1.1  cgd 		batch = 1;
    266  1.1  cgd 		break;
    267  1.1  cgd 
    268  1.1  cgd 	    case 'c':		/* -c	Command input from arg */
    269  1.1  cgd 		if (argc == 1)
    270  1.1  cgd 		    xexit(0);
    271  1.1  cgd 		argc--, tempv++;
    272  1.1  cgd 		arginp = SAVE(tempv[0]);
    273  1.1  cgd 		prompt = 0;
    274  1.1  cgd 		nofile = 1;
    275  1.1  cgd 		break;
    276  1.1  cgd 
    277  1.1  cgd 	    case 'e':		/* -e	Exit on any error */
    278  1.1  cgd 		exiterr = 1;
    279  1.1  cgd 		break;
    280  1.1  cgd 
    281  1.1  cgd 	    case 'f':		/* -f	Fast start */
    282  1.1  cgd 		fast = 1;
    283  1.1  cgd 		break;
    284  1.1  cgd 
    285  1.1  cgd 	    case 'i':		/* -i	Interactive, even if !intty */
    286  1.1  cgd 		intact = 1;
    287  1.1  cgd 		nofile = 1;
    288  1.1  cgd 		break;
    289  1.1  cgd 
    290  1.1  cgd 	    case 'm':		/* -m	read .cshrc (from su) */
    291  1.1  cgd 		mflag = 1;
    292  1.1  cgd 		break;
    293  1.1  cgd 
    294  1.1  cgd 	    case 'n':		/* -n	Don't execute */
    295  1.1  cgd 		noexec = 1;
    296  1.1  cgd 		break;
    297  1.1  cgd 
    298  1.1  cgd 	    case 'q':		/* -q	(Undoc'd) ... die on quit */
    299  1.1  cgd 		quitit = 1;
    300  1.1  cgd 		break;
    301  1.1  cgd 
    302  1.1  cgd 	    case 's':		/* -s	Read from std input */
    303  1.1  cgd 		nofile = 1;
    304  1.1  cgd 		break;
    305  1.1  cgd 
    306  1.1  cgd 	    case 't':		/* -t	Read one line from input */
    307  1.1  cgd 		onelflg = 2;
    308  1.1  cgd 		prompt = 0;
    309  1.1  cgd 		nofile = 1;
    310  1.1  cgd 		break;
    311  1.1  cgd 
    312  1.1  cgd 	    case 'v':		/* -v	Echo hist expanded input */
    313  1.1  cgd 		nverbose = 1;	/* ... later */
    314  1.1  cgd 		break;
    315  1.1  cgd 
    316  1.1  cgd 	    case 'x':		/* -x	Echo just before execution */
    317  1.1  cgd 		nexececho = 1;	/* ... later */
    318  1.1  cgd 		break;
    319  1.1  cgd 
    320  1.1  cgd 	    case 'V':		/* -V	Echo hist expanded input */
    321  1.1  cgd 		setNS(STRverbose);	/* NOW! */
    322  1.1  cgd 		break;
    323  1.1  cgd 
    324  1.1  cgd 	    case 'X':		/* -X	Echo just before execution */
    325  1.1  cgd 		setNS(STRecho);	/* NOW! */
    326  1.1  cgd 		break;
    327  1.1  cgd 
    328  1.1  cgd 	} while (*tcp);
    329  1.1  cgd 	tempv++, argc--;
    330  1.1  cgd     }
    331  1.1  cgd 
    332  1.1  cgd     if (quitit)			/* With all due haste, for debugging */
    333  1.1  cgd 	(void) signal(SIGQUIT, SIG_DFL);
    334  1.1  cgd 
    335  1.1  cgd     /*
    336  1.1  cgd      * Unless prevented by -, -c, -i, -s, or -t, if there are remaining
    337  1.1  cgd      * arguments the first of them is the name of a shell file from which to
    338  1.1  cgd      * read commands.
    339  1.1  cgd      */
    340  1.1  cgd     if (nofile == 0 && argc > 0) {
    341  1.1  cgd 	nofile = open(tempv[0], O_RDONLY);
    342  1.1  cgd 	if (nofile < 0) {
    343  1.1  cgd 	    child = 1;		/* So this doesn't return */
    344  1.1  cgd 	    stderror(ERR_SYSTEM, tempv[0], strerror(errno));
    345  1.1  cgd 	}
    346  1.1  cgd 	ffile = SAVE(tempv[0]);
    347  1.1  cgd 	/*
    348  1.1  cgd 	 * Replace FSHIN. Handle /dev/std{in,out,err} specially
    349  1.1  cgd 	 * since once they are closed we cannot open them again.
    350  1.1  cgd 	 * In that case we use our own saved descriptors
    351  1.1  cgd 	 */
    352  1.1  cgd 	if ((SHIN = dmove(nofile, FSHIN)) < 0)
    353  1.1  cgd 	    switch(nofile) {
    354  1.1  cgd 	    case 0:
    355  1.1  cgd 		SHIN = FSHIN;
    356  1.1  cgd 		break;
    357  1.1  cgd 	    case 1:
    358  1.1  cgd 		SHIN = FSHOUT;
    359  1.1  cgd 		break;
    360  1.1  cgd 	    case 2:
    361  1.1  cgd 		SHIN = FSHDIAG;
    362  1.1  cgd 		break;
    363  1.1  cgd 	    default:
    364  1.1  cgd 		stderror(ERR_SYSTEM, tempv[0], strerror(errno));
    365  1.1  cgd 		break;
    366  1.1  cgd 	    }
    367  1.1  cgd 	(void) ioctl(SHIN, FIOCLEX, NULL);
    368  1.1  cgd 	prompt = 0;
    369  1.1  cgd 	 /* argc not used any more */ tempv++;
    370  1.1  cgd     }
    371  1.1  cgd     intty = isatty(SHIN);
    372  1.1  cgd     intty |= intact;
    373  1.1  cgd     if (intty || (intact && isatty(SHOUT))) {
    374  1.1  cgd 	if (!batch && (uid != geteuid() || gid != getegid())) {
    375  1.1  cgd 	    errno = EACCES;
    376  1.1  cgd 	    child = 1;		/* So this doesn't return */
    377  1.1  cgd 	    stderror(ERR_SYSTEM, "csh", strerror(errno));
    378  1.1  cgd 	}
    379  1.1  cgd     }
    380  1.1  cgd     /*
    381  1.1  cgd      * Decide whether we should play with signals or not. If we are explicitly
    382  1.1  cgd      * told (via -i, or -) or we are a login shell (arg0 starts with -) or the
    383  1.1  cgd      * input and output are both the ttys("csh", or "csh</dev/ttyx>/dev/ttyx")
    384  1.1  cgd      * Note that in only the login shell is it likely that parent may have set
    385  1.1  cgd      * signals to be ignored
    386  1.1  cgd      */
    387  1.1  cgd     if (loginsh || intact || intty && isatty(SHOUT))
    388  1.1  cgd 	setintr = 1;
    389  1.1  cgd     settell();
    390  1.1  cgd     /*
    391  1.1  cgd      * Save the remaining arguments in argv.
    392  1.1  cgd      */
    393  1.1  cgd     setq(STRargv, blk2short(tempv), &shvhed);
    394  1.1  cgd 
    395  1.1  cgd     /*
    396  1.1  cgd      * Set up the prompt.
    397  1.1  cgd      */
    398  1.1  cgd     if (prompt) {
    399  1.1  cgd 	set(STRprompt, Strsave(uid == 0 ? STRsymhash : STRsymcent));
    400  1.1  cgd 	/* that's a meta-questionmark */
    401  1.1  cgd 	set(STRprompt2, Strsave(STRmquestion));
    402  1.1  cgd     }
    403  1.1  cgd 
    404  1.1  cgd     /*
    405  1.1  cgd      * If we are an interactive shell, then start fiddling with the signals;
    406  1.1  cgd      * this is a tricky game.
    407  1.1  cgd      */
    408  1.1  cgd     shpgrp = getpgrp();
    409  1.1  cgd     opgrp = tpgrp = -1;
    410  1.1  cgd     if (setintr) {
    411  1.1  cgd 	**argv = '-';
    412  1.1  cgd 	if (!quitit)		/* Wary! */
    413  1.1  cgd 	    (void) signal(SIGQUIT, SIG_IGN);
    414  1.1  cgd 	(void) signal(SIGINT, pintr);
    415  1.1  cgd 	(void) sigblock(sigmask(SIGINT));
    416  1.1  cgd 	(void) signal(SIGTERM, SIG_IGN);
    417  1.1  cgd 	if (quitit == 0 && arginp == 0) {
    418  1.1  cgd 	    (void) signal(SIGTSTP, SIG_IGN);
    419  1.1  cgd 	    (void) signal(SIGTTIN, SIG_IGN);
    420  1.1  cgd 	    (void) signal(SIGTTOU, SIG_IGN);
    421  1.1  cgd 	    /*
    422  1.1  cgd 	     * Wait till in foreground, in case someone stupidly runs csh &
    423  1.1  cgd 	     * dont want to try to grab away the tty.
    424  1.1  cgd 	     */
    425  1.1  cgd 	    if (isatty(FSHDIAG))
    426  1.1  cgd 		f = FSHDIAG;
    427  1.1  cgd 	    else if (isatty(FSHOUT))
    428  1.1  cgd 		f = FSHOUT;
    429  1.1  cgd 	    else if (isatty(OLDSTD))
    430  1.1  cgd 		f = OLDSTD;
    431  1.1  cgd 	    else
    432  1.1  cgd 		f = -1;
    433  1.1  cgd     retry:
    434  1.1  cgd 	    if ((tpgrp = tcgetpgrp(f)) != -1) {
    435  1.1  cgd 		if (tpgrp != shpgrp) {
    436  1.1  cgd 		    sig_t old = signal(SIGTTIN, SIG_DFL);
    437  1.1  cgd 		    (void) kill(0, SIGTTIN);
    438  1.1  cgd 		    (void) signal(SIGTTIN, old);
    439  1.1  cgd 		    goto retry;
    440  1.1  cgd 		}
    441  1.1  cgd 		opgrp = shpgrp;
    442  1.1  cgd 		shpgrp = getpid();
    443  1.1  cgd 		tpgrp = shpgrp;
    444  1.1  cgd 		/*
    445  1.1  cgd 		 * Setpgid will fail if we are a session leader and
    446  1.1  cgd 		 * mypid == mypgrp (POSIX 4.3.3)
    447  1.1  cgd 		 */
    448  1.1  cgd 		if (opgrp != shpgrp)
    449  1.1  cgd 		    if (setpgid(0, shpgrp) == -1)
    450  1.1  cgd 			goto notty;
    451  1.1  cgd 		/*
    452  1.1  cgd 		 * We do that after we set our process group, to make sure
    453  1.1  cgd 		 * that the process group belongs to a process in the same
    454  1.1  cgd 		 * session as the tty (our process and our group) (POSIX 7.2.4)
    455  1.1  cgd 		 */
    456  1.1  cgd 		if (tcsetpgrp(f, shpgrp) == -1)
    457  1.1  cgd 		    goto notty;
    458  1.1  cgd 		(void) ioctl(dcopy(f, FSHTTY), FIOCLEX, NULL);
    459  1.1  cgd 	    }
    460  1.1  cgd 	    if (tpgrp == -1) {
    461  1.1  cgd notty:
    462  1.1  cgd 		xprintf("Warning: no access to tty (%s).\n", strerror(errno));
    463  1.1  cgd 		xprintf("Thus no job control in this shell.\n");
    464  1.1  cgd 	    }
    465  1.1  cgd 	}
    466  1.1  cgd     }
    467  1.1  cgd     if ((setintr == 0) && (parintr == SIG_DFL))
    468  1.1  cgd 	setintr = 1;
    469  1.1  cgd     (void) signal(SIGCHLD, pchild);	/* while signals not ready */
    470  1.1  cgd 
    471  1.1  cgd     /*
    472  1.1  cgd      * Set an exit here in case of an interrupt or error reading the shell
    473  1.1  cgd      * start-up scripts.
    474  1.1  cgd      */
    475  1.1  cgd     reenter = setexit();	/* PWP */
    476  1.1  cgd     haderr = 0;			/* In case second time through */
    477  1.1  cgd     if (!fast && reenter == 0) {
    478  1.1  cgd 	/* Will have value(STRhome) here because set fast if don't */
    479  1.1  cgd 	{
    480  1.1  cgd 	    int     osetintr = setintr;
    481  1.1  cgd 	    sigset_t omask = sigblock(sigmask(SIGINT));
    482  1.1  cgd 
    483  1.1  cgd 	    setintr = 0;
    484  1.1  cgd #ifdef _PATH_DOTCSHRC
    485  1.1  cgd 	    (void) srcfile(_PATH_DOTCSHRC, 0, 0);
    486  1.1  cgd #endif
    487  1.1  cgd 	    if (!fast && !arginp && !onelflg)
    488  1.1  cgd 		dohash();
    489  1.1  cgd #ifdef _PATH_DOTLOGIN
    490  1.1  cgd 	    if (loginsh)
    491  1.1  cgd 		(void) srcfile(_PATH_DOTLOGIN, 0, 0);
    492  1.1  cgd #endif
    493  1.1  cgd 	    (void) sigsetmask(omask);
    494  1.1  cgd 	    setintr = osetintr;
    495  1.1  cgd 	}
    496  1.1  cgd 	(void) srccat(value(STRhome), STRsldotcshrc);
    497  1.1  cgd 
    498  1.1  cgd 	if (!fast && !arginp && !onelflg && !havhash)
    499  1.1  cgd 	    dohash();
    500  1.1  cgd         if (loginsh)
    501  1.1  cgd 	      (void) srccat(value(STRhome), STRsldotlogin);
    502  1.1  cgd 	dosource(loadhist);
    503  1.1  cgd     }
    504  1.1  cgd 
    505  1.1  cgd     /*
    506  1.1  cgd      * Now are ready for the -v and -x flags
    507  1.1  cgd      */
    508  1.1  cgd     if (nverbose)
    509  1.1  cgd 	setNS(STRverbose);
    510  1.1  cgd     if (nexececho)
    511  1.1  cgd 	setNS(STRecho);
    512  1.1  cgd 
    513  1.1  cgd     /*
    514  1.1  cgd      * All the rest of the world is inside this call. The argument to process
    515  1.1  cgd      * indicates whether it should catch "error unwinds".  Thus if we are a
    516  1.1  cgd      * interactive shell our call here will never return by being blown past on
    517  1.1  cgd      * an error.
    518  1.1  cgd      */
    519  1.1  cgd     process(setintr);
    520  1.1  cgd 
    521  1.1  cgd     /*
    522  1.1  cgd      * Mop-up.
    523  1.1  cgd      */
    524  1.1  cgd     if (intty) {
    525  1.1  cgd 	if (loginsh) {
    526  1.1  cgd 	    xprintf("logout\n");
    527  1.1  cgd 	    (void) close(SHIN);
    528  1.1  cgd 	    child = 1;
    529  1.1  cgd 	    goodbye();
    530  1.1  cgd 	}
    531  1.1  cgd 	else {
    532  1.1  cgd 	    xprintf("exit\n");
    533  1.1  cgd 	}
    534  1.1  cgd     }
    535  1.1  cgd     rechist();
    536  1.1  cgd     exitstat();
    537  1.1  cgd     return (0);
    538  1.1  cgd }
    539  1.1  cgd 
    540  1.1  cgd void
    541  1.1  cgd untty()
    542  1.1  cgd {
    543  1.1  cgd     if (tpgrp > 0) {
    544  1.1  cgd 	(void) setpgid(0, opgrp);
    545  1.1  cgd 	(void) tcsetpgrp(FSHTTY, opgrp);
    546  1.1  cgd     }
    547  1.1  cgd }
    548  1.1  cgd 
    549  1.1  cgd void
    550  1.1  cgd importpath(cp)
    551  1.1  cgd     Char   *cp;
    552  1.1  cgd {
    553  1.1  cgd     register int i = 0;
    554  1.1  cgd     register Char *dp;
    555  1.1  cgd     register Char **pv;
    556  1.1  cgd     int     c;
    557  1.1  cgd 
    558  1.1  cgd     for (dp = cp; *dp; dp++)
    559  1.1  cgd 	if (*dp == ':')
    560  1.1  cgd 	    i++;
    561  1.1  cgd     /*
    562  1.1  cgd      * i+2 where i is the number of colons in the path. There are i+1
    563  1.1  cgd      * directories in the path plus we need room for a zero terminator.
    564  1.1  cgd      */
    565  1.1  cgd     pv = (Char **) xcalloc((size_t) (i + 2), sizeof(Char **));
    566  1.1  cgd     dp = cp;
    567  1.1  cgd     i = 0;
    568  1.1  cgd     if (*dp)
    569  1.1  cgd 	for (;;) {
    570  1.1  cgd 	    if ((c = *dp) == ':' || c == 0) {
    571  1.1  cgd 		*dp = 0;
    572  1.1  cgd 		pv[i++] = Strsave(*cp ? cp : STRdot);
    573  1.1  cgd 		if (c) {
    574  1.1  cgd 		    cp = dp + 1;
    575  1.1  cgd 		    *dp = ':';
    576  1.1  cgd 		}
    577  1.1  cgd 		else
    578  1.1  cgd 		    break;
    579  1.1  cgd 	    }
    580  1.1  cgd 	    dp++;
    581  1.1  cgd 	}
    582  1.1  cgd     pv[i] = 0;
    583  1.1  cgd     set1(STRpath, pv, &shvhed);
    584  1.1  cgd }
    585  1.1  cgd 
    586  1.1  cgd /*
    587  1.1  cgd  * Source to the file which is the catenation of the argument names.
    588  1.1  cgd  */
    589  1.1  cgd static int
    590  1.1  cgd srccat(cp, dp)
    591  1.1  cgd     Char   *cp, *dp;
    592  1.1  cgd {
    593  1.1  cgd     register Char *ep = Strspl(cp, dp);
    594  1.1  cgd     char   *ptr = short2str(ep);
    595  1.1  cgd 
    596  1.1  cgd     xfree((ptr_t) ep);
    597  1.1  cgd     return srcfile(ptr, mflag ? 0 : 1, 0);
    598  1.1  cgd }
    599  1.1  cgd 
    600  1.1  cgd /*
    601  1.1  cgd  * Source to a file putting the file descriptor in a safe place (> 2).
    602  1.1  cgd  */
    603  1.1  cgd static int
    604  1.1  cgd srcfile(f, onlyown, flag)
    605  1.1  cgd     char   *f;
    606  1.1  cgd     bool    onlyown, flag;
    607  1.1  cgd {
    608  1.1  cgd     register int unit;
    609  1.1  cgd 
    610  1.1  cgd     if ((unit = open(f, O_RDONLY)) == -1)
    611  1.1  cgd 	return 0;
    612  1.1  cgd     unit = dmove(unit, -1);
    613  1.1  cgd 
    614  1.1  cgd     (void) ioctl(unit, FIOCLEX, NULL);
    615  1.1  cgd     srcunit(unit, onlyown, flag);
    616  1.1  cgd     return 1;
    617  1.1  cgd }
    618  1.1  cgd 
    619  1.1  cgd /*
    620  1.1  cgd  * Source to a unit.  If onlyown it must be our file or our group or
    621  1.1  cgd  * we don't chance it.	This occurs on ".cshrc"s and the like.
    622  1.1  cgd  */
    623  1.1  cgd int     insource;
    624  1.1  cgd static void
    625  1.1  cgd srcunit(unit, onlyown, hflg)
    626  1.1  cgd     register int unit;
    627  1.1  cgd     bool    onlyown, hflg;
    628  1.1  cgd {
    629  1.1  cgd     /* We have to push down a lot of state here */
    630  1.1  cgd     /* All this could go into a structure */
    631  1.1  cgd     int     oSHIN = -1, oldintty = intty, oinsource = insource;
    632  1.1  cgd     struct whyle *oldwhyl = whyles;
    633  1.1  cgd     Char   *ogointr = gointr, *oarginp = arginp;
    634  1.1  cgd     Char   *oevalp = evalp, **oevalvec = evalvec;
    635  1.1  cgd     int     oonelflg = onelflg;
    636  1.1  cgd     bool    oenterhist = enterhist;
    637  1.1  cgd     char    OHIST = HIST;
    638  1.1  cgd     bool    otell = cantell;
    639  1.1  cgd 
    640  1.1  cgd     struct Bin saveB;
    641  1.1  cgd     sigset_t omask;
    642  1.1  cgd     jmp_buf oldexit;
    643  1.1  cgd 
    644  1.1  cgd     /* The (few) real local variables */
    645  1.1  cgd     int     my_reenter;
    646  1.1  cgd 
    647  1.1  cgd     if (unit < 0)
    648  1.1  cgd 	return;
    649  1.1  cgd     if (didfds)
    650  1.1  cgd 	donefds();
    651  1.1  cgd     if (onlyown) {
    652  1.1  cgd 	struct stat stb;
    653  1.1  cgd 
    654  1.1  cgd 	if (fstat(unit, &stb) < 0) {
    655  1.1  cgd 	    (void) close(unit);
    656  1.1  cgd 	    return;
    657  1.1  cgd 	}
    658  1.1  cgd     }
    659  1.1  cgd 
    660  1.1  cgd     /*
    661  1.1  cgd      * There is a critical section here while we are pushing down the input
    662  1.1  cgd      * stream since we have stuff in different structures. If we weren't
    663  1.1  cgd      * careful an interrupt could corrupt SHIN's Bin structure and kill the
    664  1.1  cgd      * shell.
    665  1.1  cgd      *
    666  1.1  cgd      * We could avoid the critical region by grouping all the stuff in a single
    667  1.1  cgd      * structure and pointing at it to move it all at once.  This is less
    668  1.1  cgd      * efficient globally on many variable references however.
    669  1.1  cgd      */
    670  1.1  cgd     insource = 1;
    671  1.1  cgd     getexit(oldexit);
    672  1.1  cgd     omask = 0;
    673  1.1  cgd 
    674  1.1  cgd     if (setintr)
    675  1.1  cgd 	omask = sigblock(sigmask(SIGINT));
    676  1.1  cgd     /* Setup the new values of the state stuff saved above */
    677  1.1  cgd     bcopy((char *) &B, (char *) &(saveB), sizeof(B));
    678  1.1  cgd     fbuf = NULL;
    679  1.1  cgd     fseekp = feobp = fblocks = 0;
    680  1.1  cgd     oSHIN = SHIN, SHIN = unit, arginp = 0, onelflg = 0;
    681  1.1  cgd     intty = isatty(SHIN), whyles = 0, gointr = 0;
    682  1.1  cgd     evalvec = 0;
    683  1.1  cgd     evalp = 0;
    684  1.1  cgd     enterhist = hflg;
    685  1.1  cgd     if (enterhist)
    686  1.1  cgd 	HIST = '\0';
    687  1.1  cgd 
    688  1.1  cgd     /*
    689  1.1  cgd      * Now if we are allowing commands to be interrupted, we let ourselves be
    690  1.1  cgd      * interrupted.
    691  1.1  cgd      */
    692  1.1  cgd     if (setintr)
    693  1.1  cgd 	(void) sigsetmask(omask);
    694  1.1  cgd     settell();
    695  1.1  cgd 
    696  1.1  cgd     if ((my_reenter = setexit()) == 0)
    697  1.1  cgd 	process(0);		/* 0 -> blow away on errors */
    698  1.1  cgd 
    699  1.1  cgd     if (setintr)
    700  1.1  cgd 	(void) sigsetmask(omask);
    701  1.1  cgd     if (oSHIN >= 0) {
    702  1.1  cgd 	register int i;
    703  1.1  cgd 
    704  1.1  cgd 	/* We made it to the new state... free up its storage */
    705  1.1  cgd 	/* This code could get run twice but xfree doesn't care */
    706  1.1  cgd 	for (i = 0; i < fblocks; i++)
    707  1.1  cgd 	    xfree((ptr_t) fbuf[i]);
    708  1.1  cgd 	xfree((ptr_t) fbuf);
    709  1.1  cgd 
    710  1.1  cgd 	/* Reset input arena */
    711  1.1  cgd 	bcopy((char *) &(saveB), (char *) &B, sizeof(B));
    712  1.1  cgd 
    713  1.1  cgd 	(void) close(SHIN), SHIN = oSHIN;
    714  1.1  cgd 	arginp = oarginp, onelflg = oonelflg;
    715  1.1  cgd 	evalp = oevalp, evalvec = oevalvec;
    716  1.1  cgd 	intty = oldintty, whyles = oldwhyl, gointr = ogointr;
    717  1.1  cgd 	if (enterhist)
    718  1.1  cgd 	    HIST = OHIST;
    719  1.1  cgd 	enterhist = oenterhist;
    720  1.1  cgd 	cantell = otell;
    721  1.1  cgd     }
    722  1.1  cgd 
    723  1.1  cgd     resexit(oldexit);
    724  1.1  cgd     /*
    725  1.1  cgd      * If process reset() (effectively an unwind) then we must also unwind.
    726  1.1  cgd      */
    727  1.1  cgd     if (my_reenter)
    728  1.1  cgd 	stderror(ERR_SILENT);
    729  1.1  cgd     insource = oinsource;
    730  1.1  cgd }
    731  1.1  cgd 
    732  1.1  cgd void
    733  1.1  cgd rechist()
    734  1.1  cgd {
    735  1.1  cgd     Char    buf[BUFSIZ];
    736  1.1  cgd     int     fp, ftmp, oldidfds;
    737  1.1  cgd 
    738  1.1  cgd     if (!fast) {
    739  1.1  cgd 	if (value(STRsavehist)[0] == '\0')
    740  1.1  cgd 	    return;
    741  1.1  cgd 	(void) Strcpy(buf, value(STRhome));
    742  1.1  cgd 	(void) Strcat(buf, STRsldthist);
    743  1.1  cgd 	fp = creat(short2str(buf), 0600);
    744  1.1  cgd 	if (fp == -1)
    745  1.1  cgd 	    return;
    746  1.1  cgd 	oldidfds = didfds;
    747  1.1  cgd 	didfds = 0;
    748  1.1  cgd 	ftmp = SHOUT;
    749  1.1  cgd 	SHOUT = fp;
    750  1.1  cgd 	(void) Strcpy(buf, value(STRsavehist));
    751  1.1  cgd 	dumphist[2] = buf;
    752  1.1  cgd 	dohist(dumphist);
    753  1.1  cgd 	(void) close(fp);
    754  1.1  cgd 	SHOUT = ftmp;
    755  1.1  cgd 	didfds = oldidfds;
    756  1.1  cgd     }
    757  1.1  cgd }
    758  1.1  cgd 
    759  1.1  cgd void
    760  1.1  cgd goodbye()
    761  1.1  cgd {
    762  1.1  cgd     rechist();
    763  1.1  cgd 
    764  1.1  cgd     if (loginsh) {
    765  1.1  cgd 	(void) signal(SIGQUIT, SIG_IGN);
    766  1.1  cgd 	(void) signal(SIGINT, SIG_IGN);
    767  1.1  cgd 	(void) signal(SIGTERM, SIG_IGN);
    768  1.1  cgd 	setintr = 0;		/* No interrupts after "logout" */
    769  1.1  cgd 	if (!(adrof(STRlogout)))
    770  1.1  cgd 	    set(STRlogout, STRnormal);
    771  1.1  cgd #ifdef _PATH_DOTLOGOUT
    772  1.1  cgd 	(void) srcfile(_PATH_DOTLOGOUT, 0, 0);
    773  1.1  cgd #endif
    774  1.1  cgd 	if (adrof(STRhome))
    775  1.1  cgd 	    (void) srccat(value(STRhome), STRsldtlogout);
    776  1.1  cgd     }
    777  1.1  cgd     exitstat();
    778  1.1  cgd }
    779  1.1  cgd 
    780  1.1  cgd void
    781  1.1  cgd exitstat()
    782  1.1  cgd {
    783  1.1  cgd 
    784  1.1  cgd #ifdef PROF
    785  1.1  cgd     monitor(0);
    786  1.1  cgd #endif
    787  1.1  cgd     /*
    788  1.1  cgd      * Note that if STATUS is corrupted (i.e. getn bombs) then error will exit
    789  1.1  cgd      * directly because we poke child here. Otherwise we might continue
    790  1.1  cgd      * unwarrantedly (sic).
    791  1.1  cgd      */
    792  1.1  cgd     child = 1;
    793  1.1  cgd     xexit(getn(value(STRstatus)));
    794  1.1  cgd }
    795  1.1  cgd 
    796  1.1  cgd /*
    797  1.1  cgd  * in the event of a HUP we want to save the history
    798  1.1  cgd  */
    799  1.1  cgd static void
    800  1.1  cgd phup(sig)
    801  1.1  cgd int sig;
    802  1.1  cgd {
    803  1.1  cgd     rechist();
    804  1.1  cgd     xexit(sig);
    805  1.1  cgd }
    806  1.1  cgd 
    807  1.1  cgd Char   *jobargv[2] = {STRjobs, 0};
    808  1.1  cgd 
    809  1.1  cgd /*
    810  1.1  cgd  * Catch an interrupt, e.g. during lexical input.
    811  1.1  cgd  * If we are an interactive shell, we reset the interrupt catch
    812  1.1  cgd  * immediately.  In any case we drain the shell output,
    813  1.1  cgd  * and finally go through the normal error mechanism, which
    814  1.1  cgd  * gets a chance to make the shell go away.
    815  1.1  cgd  */
    816  1.1  cgd /* ARGSUSED */
    817  1.1  cgd void
    818  1.1  cgd pintr(notused)
    819  1.1  cgd 	int notused;
    820  1.1  cgd {
    821  1.1  cgd     pintr1(1);
    822  1.1  cgd }
    823  1.1  cgd 
    824  1.1  cgd void
    825  1.1  cgd pintr1(wantnl)
    826  1.1  cgd     bool    wantnl;
    827  1.1  cgd {
    828  1.1  cgd     register Char **v;
    829  1.1  cgd     sigset_t omask;
    830  1.1  cgd 
    831  1.1  cgd     omask = sigblock((sigset_t) 0);
    832  1.1  cgd     if (setintr) {
    833  1.1  cgd 	(void) sigsetmask(omask & ~sigmask(SIGINT));
    834  1.1  cgd 	if (pjobs) {
    835  1.1  cgd 	    pjobs = 0;
    836  1.1  cgd 	    xprintf("\n");
    837  1.1  cgd 	    dojobs(jobargv);
    838  1.1  cgd 	    stderror(ERR_NAME | ERR_INTR);
    839  1.1  cgd 	}
    840  1.1  cgd     }
    841  1.1  cgd     (void) sigsetmask(omask & ~sigmask(SIGCHLD));
    842  1.1  cgd     draino();
    843  1.1  cgd     (void) endpwent();
    844  1.1  cgd 
    845  1.1  cgd     /*
    846  1.1  cgd      * If we have an active "onintr" then we search for the label. Note that if
    847  1.1  cgd      * one does "onintr -" then we shan't be interruptible so we needn't worry
    848  1.1  cgd      * about that here.
    849  1.1  cgd      */
    850  1.1  cgd     if (gointr) {
    851  1.1  cgd 	search(T_GOTO, 0, gointr);
    852  1.1  cgd 	timflg = 0;
    853  1.1  cgd 	if (v = pargv)
    854  1.1  cgd 	    pargv = 0, blkfree(v);
    855  1.1  cgd 	if (v = gargv)
    856  1.1  cgd 	    gargv = 0, blkfree(v);
    857  1.1  cgd 	reset();
    858  1.1  cgd     }
    859  1.1  cgd     else if (intty && wantnl) {
    860  1.1  cgd 	(void) putraw('\r');
    861  1.1  cgd 	(void) putraw('\n');
    862  1.1  cgd     }
    863  1.1  cgd     stderror(ERR_SILENT);
    864  1.1  cgd }
    865  1.1  cgd 
    866  1.1  cgd /*
    867  1.1  cgd  * Process is the main driving routine for the shell.
    868  1.1  cgd  * It runs all command processing, except for those within { ... }
    869  1.1  cgd  * in expressions (which is run by a routine evalav in sh.exp.c which
    870  1.1  cgd  * is a stripped down process), and `...` evaluation which is run
    871  1.1  cgd  * also by a subset of this code in sh.glob.c in the routine backeval.
    872  1.1  cgd  *
    873  1.1  cgd  * The code here is a little strange because part of it is interruptible
    874  1.1  cgd  * and hence freeing of structures appears to occur when none is necessary
    875  1.1  cgd  * if this is ignored.
    876  1.1  cgd  *
    877  1.1  cgd  * Note that if catch is not set then we will unwind on any error.
    878  1.1  cgd  * If an end-of-file occurs, we return.
    879  1.1  cgd  */
    880  1.1  cgd void
    881  1.1  cgd process(catch)
    882  1.1  cgd     bool    catch;
    883  1.1  cgd {
    884  1.1  cgd     jmp_buf osetexit;
    885  1.1  cgd     struct command *t;
    886  1.1  cgd 
    887  1.1  cgd     getexit(osetexit);
    888  1.1  cgd     for (;;) {
    889  1.1  cgd 	pendjob();
    890  1.1  cgd 	paraml.next = paraml.prev = &paraml;
    891  1.1  cgd 	paraml.word = STRNULL;
    892  1.1  cgd 	t = 0;
    893  1.1  cgd 	(void) setexit();
    894  1.1  cgd 	justpr = enterhist;	/* execute if not entering history */
    895  1.1  cgd 
    896  1.1  cgd 	/*
    897  1.1  cgd 	 * Interruptible during interactive reads
    898  1.1  cgd 	 */
    899  1.1  cgd 	if (setintr)
    900  1.1  cgd 	    (void) sigsetmask(sigblock((sigset_t) 0) & ~sigmask(SIGINT));
    901  1.1  cgd 
    902  1.1  cgd 	/*
    903  1.1  cgd 	 * For the sake of reset()
    904  1.1  cgd 	 */
    905  1.1  cgd 	freelex(&paraml);
    906  1.1  cgd 	freesyn(t);
    907  1.1  cgd 	t = 0;
    908  1.1  cgd 
    909  1.1  cgd 	if (haderr) {
    910  1.1  cgd 	    if (!catch) {
    911  1.1  cgd 		/* unwind */
    912  1.1  cgd 		doneinp = 0;
    913  1.1  cgd 		resexit(osetexit);
    914  1.1  cgd 		reset();
    915  1.1  cgd 	    }
    916  1.1  cgd 	    haderr = 0;
    917  1.1  cgd 	    /*
    918  1.1  cgd 	     * Every error is eventually caught here or the shell dies.  It is
    919  1.1  cgd 	     * at this point that we clean up any left-over open files, by
    920  1.1  cgd 	     * closing all but a fixed number of pre-defined files.  Thus
    921  1.1  cgd 	     * routines don't have to worry about leaving files open due to
    922  1.1  cgd 	     * deeper errors... they will get closed here.
    923  1.1  cgd 	     */
    924  1.1  cgd 	    closem();
    925  1.1  cgd 	    continue;
    926  1.1  cgd 	}
    927  1.1  cgd 	if (doneinp) {
    928  1.1  cgd 	    doneinp = 0;
    929  1.1  cgd 	    break;
    930  1.1  cgd 	}
    931  1.1  cgd 	if (chkstop)
    932  1.1  cgd 	    chkstop--;
    933  1.1  cgd 	if (neednote)
    934  1.1  cgd 	    pnote();
    935  1.1  cgd 	if (intty && prompt && evalvec == 0) {
    936  1.1  cgd 	    mailchk();
    937  1.1  cgd 	    /*
    938  1.1  cgd 	     * If we are at the end of the input buffer then we are going to
    939  1.1  cgd 	     * read fresh stuff. Otherwise, we are rereading input and don't
    940  1.1  cgd 	     * need or want to prompt.
    941  1.1  cgd 	     */
    942  1.1  cgd 	    if (fseekp == feobp)
    943  1.1  cgd 		printprompt();
    944  1.1  cgd 	    flush();
    945  1.1  cgd 	}
    946  1.1  cgd 	if (seterr) {
    947  1.1  cgd 	    xfree((ptr_t) seterr);
    948  1.1  cgd 	    seterr = NULL;
    949  1.1  cgd 	}
    950  1.1  cgd 
    951  1.1  cgd 	/*
    952  1.1  cgd 	 * Echo not only on VERBOSE, but also with history expansion. If there
    953  1.1  cgd 	 * is a lexical error then we forego history echo.
    954  1.1  cgd 	 */
    955  1.1  cgd 	if (lex(&paraml) && !seterr && intty || adrof(STRverbose)) {
    956  1.1  cgd 	    haderr = 1;
    957  1.1  cgd 	    prlex(&paraml);
    958  1.1  cgd 	    haderr = 0;
    959  1.1  cgd 	}
    960  1.1  cgd 
    961  1.1  cgd 	/*
    962  1.1  cgd 	 * The parser may lose space if interrupted.
    963  1.1  cgd 	 */
    964  1.1  cgd 	if (setintr)
    965  1.1  cgd 	    (void) sigblock(sigmask(SIGINT));
    966  1.1  cgd 
    967  1.1  cgd 	/*
    968  1.1  cgd 	 * Save input text on the history list if reading in old history, or it
    969  1.1  cgd 	 * is from the terminal at the top level and not in a loop.
    970  1.1  cgd 	 *
    971  1.1  cgd 	 * PWP: entry of items in the history list while in a while loop is done
    972  1.1  cgd 	 * elsewhere...
    973  1.1  cgd 	 */
    974  1.1  cgd 	if (enterhist || catch && intty && !whyles)
    975  1.1  cgd 	    savehist(&paraml);
    976  1.1  cgd 
    977  1.1  cgd 	/*
    978  1.1  cgd 	 * Print lexical error messages, except when sourcing history lists.
    979  1.1  cgd 	 */
    980  1.1  cgd 	if (!enterhist && seterr)
    981  1.1  cgd 	    stderror(ERR_OLD);
    982  1.1  cgd 
    983  1.1  cgd 	/*
    984  1.1  cgd 	 * If had a history command :p modifier then this is as far as we
    985  1.1  cgd 	 * should go
    986  1.1  cgd 	 */
    987  1.1  cgd 	if (justpr)
    988  1.1  cgd 	    reset();
    989  1.1  cgd 
    990  1.1  cgd 	alias(&paraml);
    991  1.1  cgd 
    992  1.1  cgd 	/*
    993  1.1  cgd 	 * Parse the words of the input into a parse tree.
    994  1.1  cgd 	 */
    995  1.1  cgd 	t = syntax(paraml.next, &paraml, 0);
    996  1.1  cgd 	if (seterr)
    997  1.1  cgd 	    stderror(ERR_OLD);
    998  1.1  cgd 
    999  1.1  cgd 	execute(t, (tpgrp > 0 ? tpgrp : -1), NULL, NULL);
   1000  1.1  cgd 
   1001  1.1  cgd 	/*
   1002  1.1  cgd 	 * Made it!
   1003  1.1  cgd 	 */
   1004  1.1  cgd 	freelex(&paraml);
   1005  1.1  cgd 	freesyn(t);
   1006  1.1  cgd     }
   1007  1.1  cgd     resexit(osetexit);
   1008  1.1  cgd }
   1009  1.1  cgd 
   1010  1.1  cgd void
   1011  1.1  cgd dosource(t)
   1012  1.1  cgd     register Char **t;
   1013  1.1  cgd {
   1014  1.1  cgd     register Char *f;
   1015  1.1  cgd     bool    hflg = 0;
   1016  1.1  cgd     Char    buf[BUFSIZ];
   1017  1.1  cgd 
   1018  1.1  cgd     t++;
   1019  1.1  cgd     if (*t && eq(*t, STRmh)) {
   1020  1.1  cgd 	if (*++t == NULL)
   1021  1.1  cgd 	    stderror(ERR_NAME | ERR_HFLAG);
   1022  1.1  cgd 	hflg++;
   1023  1.1  cgd     }
   1024  1.1  cgd     (void) Strcpy(buf, *t);
   1025  1.1  cgd     f = globone(buf, G_ERROR);
   1026  1.1  cgd     (void) strcpy((char *) buf, short2str(f));
   1027  1.1  cgd     xfree((ptr_t) f);
   1028  1.1  cgd     if (!srcfile((char *) buf, 0, hflg) && !hflg)
   1029  1.1  cgd 	stderror(ERR_SYSTEM, (char *) buf, strerror(errno));
   1030  1.1  cgd }
   1031  1.1  cgd 
   1032  1.1  cgd /*
   1033  1.1  cgd  * Check for mail.
   1034  1.1  cgd  * If we are a login shell, then we don't want to tell
   1035  1.1  cgd  * about any mail file unless its been modified
   1036  1.1  cgd  * after the time we started.
   1037  1.1  cgd  * This prevents us from telling the user things he already
   1038  1.1  cgd  * knows, since the login program insists on saying
   1039  1.1  cgd  * "You have mail."
   1040  1.1  cgd  */
   1041  1.1  cgd static void
   1042  1.1  cgd mailchk()
   1043  1.1  cgd {
   1044  1.1  cgd     register struct varent *v;
   1045  1.1  cgd     register Char **vp;
   1046  1.1  cgd     time_t  t;
   1047  1.1  cgd     int     intvl, cnt;
   1048  1.1  cgd     struct stat stb;
   1049  1.1  cgd     bool    new;
   1050  1.1  cgd 
   1051  1.1  cgd     v = adrof(STRmail);
   1052  1.1  cgd     if (v == 0)
   1053  1.1  cgd 	return;
   1054  1.1  cgd     (void) time(&t);
   1055  1.1  cgd     vp = v->vec;
   1056  1.1  cgd     cnt = blklen(vp);
   1057  1.1  cgd     intvl = (cnt && number(*vp)) ? (--cnt, getn(*vp++)) : MAILINTVL;
   1058  1.1  cgd     if (intvl < 1)
   1059  1.1  cgd 	intvl = 1;
   1060  1.1  cgd     if (chktim + intvl > t)
   1061  1.1  cgd 	return;
   1062  1.1  cgd     for (; *vp; vp++) {
   1063  1.1  cgd 	if (stat(short2str(*vp), &stb) < 0)
   1064  1.1  cgd 	    continue;
   1065  1.1  cgd 	new = stb.st_mtime > time0.tv_sec;
   1066  1.1  cgd 	if (stb.st_size == 0 || stb.st_atime > stb.st_mtime ||
   1067  1.1  cgd 	    (stb.st_atime < chktim && stb.st_mtime < chktim) ||
   1068  1.1  cgd 	    loginsh && !new)
   1069  1.1  cgd 	    continue;
   1070  1.1  cgd 	if (cnt == 1)
   1071  1.1  cgd 	    xprintf("You have %smail.\n", new ? "new " : "");
   1072  1.1  cgd 	else
   1073  1.1  cgd 	    xprintf("%s in %s.\n", new ? "New mail" : "Mail", short2str(*vp));
   1074  1.1  cgd     }
   1075  1.1  cgd     chktim = t;
   1076  1.1  cgd }
   1077  1.1  cgd 
   1078  1.1  cgd /*
   1079  1.1  cgd  * Extract a home directory from the password file
   1080  1.1  cgd  * The argument points to a buffer where the name of the
   1081  1.1  cgd  * user whose home directory is sought is currently.
   1082  1.1  cgd  * We write the home directory of the user back there.
   1083  1.1  cgd  */
   1084  1.1  cgd int
   1085  1.1  cgd gethdir(home)
   1086  1.1  cgd     Char   *home;
   1087  1.1  cgd {
   1088  1.1  cgd     Char   *h;
   1089  1.1  cgd     struct passwd *pw;
   1090  1.1  cgd 
   1091  1.1  cgd     /*
   1092  1.1  cgd      * Is it us?
   1093  1.1  cgd      */
   1094  1.1  cgd     if (*home == '\0') {
   1095  1.1  cgd 	if (h = value(STRhome)) {
   1096  1.1  cgd 	    (void) Strcpy(home, h);
   1097  1.1  cgd 	    return 0;
   1098  1.1  cgd 	}
   1099  1.1  cgd 	else
   1100  1.1  cgd 	    return 1;
   1101  1.1  cgd     }
   1102  1.1  cgd 
   1103  1.1  cgd     if (pw = getpwnam(short2str(home))) {
   1104  1.1  cgd 	(void) Strcpy(home, str2short(pw->pw_dir));
   1105  1.1  cgd 	return 0;
   1106  1.1  cgd     }
   1107  1.1  cgd     else
   1108  1.1  cgd 	return 1;
   1109  1.1  cgd }
   1110  1.1  cgd 
   1111  1.1  cgd /*
   1112  1.1  cgd  * Move the initial descriptors to their eventual
   1113  1.1  cgd  * resting places, closin all other units.
   1114  1.1  cgd  */
   1115  1.1  cgd void
   1116  1.1  cgd initdesc()
   1117  1.1  cgd {
   1118  1.1  cgd 
   1119  1.1  cgd     didfds = 0;			/* 0, 1, 2 aren't set up */
   1120  1.1  cgd     (void) ioctl(SHIN = dcopy(0, FSHIN), FIOCLEX, NULL);
   1121  1.1  cgd     (void) ioctl(SHOUT = dcopy(1, FSHOUT), FIOCLEX, NULL);
   1122  1.1  cgd     (void) ioctl(SHDIAG = dcopy(2, FSHDIAG), FIOCLEX, NULL);
   1123  1.1  cgd     (void) ioctl(OLDSTD = dcopy(SHIN, FOLDSTD), FIOCLEX, NULL);
   1124  1.1  cgd     closem();
   1125  1.1  cgd }
   1126  1.1  cgd 
   1127  1.1  cgd 
   1128  1.1  cgd void
   1129  1.1  cgd #ifdef PROF
   1130  1.1  cgd done(i)
   1131  1.1  cgd #else
   1132  1.1  cgd xexit(i)
   1133  1.1  cgd #endif
   1134  1.1  cgd     int     i;
   1135  1.1  cgd {
   1136  1.1  cgd     untty();
   1137  1.1  cgd     _exit(i);
   1138  1.1  cgd }
   1139  1.1  cgd 
   1140  1.1  cgd static Char **
   1141  1.1  cgd defaultpath()
   1142  1.1  cgd {
   1143  1.1  cgd     char   *ptr;
   1144  1.1  cgd     Char  **blk, **blkp;
   1145  1.1  cgd     struct stat stb;
   1146  1.1  cgd 
   1147  1.1  cgd     blkp = blk = (Char **) xmalloc((size_t) sizeof(Char *) * 10);
   1148  1.1  cgd 
   1149  1.1  cgd #define DIRAPPEND(a)  \
   1150  1.1  cgd 	if (stat(ptr = a, &stb) == 0 && (stb.st_mode & S_IFMT) == S_IFDIR) \
   1151  1.1  cgd 		*blkp++ = SAVE(ptr)
   1152  1.1  cgd 
   1153  1.1  cgd     DIRAPPEND(_PATH_BIN);
   1154  1.1  cgd     DIRAPPEND(_PATH_USRBIN);
   1155  1.1  cgd 
   1156  1.1  cgd #undef DIRAPPEND
   1157  1.1  cgd 
   1158  1.1  cgd     *blkp++ = Strsave(STRdot);
   1159  1.1  cgd     *blkp = NULL;
   1160  1.1  cgd     return (blk);
   1161  1.1  cgd }
   1162  1.1  cgd 
   1163  1.1  cgd void
   1164  1.1  cgd printprompt()
   1165  1.1  cgd {
   1166  1.1  cgd     register Char *cp;
   1167  1.1  cgd 
   1168  1.1  cgd     if (!whyles) {
   1169  1.1  cgd 	for (cp = value(STRprompt); *cp; cp++)
   1170  1.1  cgd 	    if (*cp == HIST)
   1171  1.1  cgd 		xprintf("%d", eventno + 1);
   1172  1.1  cgd 	    else {
   1173  1.1  cgd 		if (*cp == '\\' && cp[1] == HIST)
   1174  1.1  cgd 		    cp++;
   1175  1.1  cgd 		xputchar(*cp | QUOTE);
   1176  1.1  cgd 	    }
   1177  1.1  cgd     }
   1178  1.1  cgd     else
   1179  1.1  cgd 	/*
   1180  1.1  cgd 	 * Prompt for forward reading loop body content.
   1181  1.1  cgd 	 */
   1182  1.1  cgd 	xprintf("? ");
   1183  1.1  cgd     flush();
   1184  1.1  cgd }
   1185