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