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