sh.c revision 69408
169408Sache/* $Header: /src/pub/tcsh/sh.c,v 3.92 2000/11/11 23:03:35 christos Exp $ */ 259243Sobrien/* 359243Sobrien * sh.c: Main shell routines 459243Sobrien */ 559243Sobrien/*- 659243Sobrien * Copyright (c) 1980, 1991 The Regents of the University of California. 759243Sobrien * All rights reserved. 859243Sobrien * 959243Sobrien * Redistribution and use in source and binary forms, with or without 1059243Sobrien * modification, are permitted provided that the following conditions 1159243Sobrien * are met: 1259243Sobrien * 1. Redistributions of source code must retain the above copyright 1359243Sobrien * notice, this list of conditions and the following disclaimer. 1459243Sobrien * 2. Redistributions in binary form must reproduce the above copyright 1559243Sobrien * notice, this list of conditions and the following disclaimer in the 1659243Sobrien * documentation and/or other materials provided with the distribution. 1759243Sobrien * 3. All advertising materials mentioning features or use of this software 1859243Sobrien * must display the following acknowledgement: 1959243Sobrien * This product includes software developed by the University of 2059243Sobrien * California, Berkeley and its contributors. 2159243Sobrien * 4. Neither the name of the University nor the names of its contributors 2259243Sobrien * may be used to endorse or promote products derived from this software 2359243Sobrien * without specific prior written permission. 2459243Sobrien * 2559243Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2659243Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2759243Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2859243Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2959243Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3059243Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3159243Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3259243Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3359243Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3459243Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3559243Sobrien * SUCH DAMAGE. 3659243Sobrien */ 3759243Sobrien#define EXTERN /* Intern */ 3859243Sobrien#include "sh.h" 3959243Sobrien 4059243Sobrien#ifndef lint 4159243Sobrienchar copyright[] = 4259243Sobrien"@(#) Copyright (c) 1991 The Regents of the University of California.\n\ 4359243Sobrien All rights reserved.\n"; 4459243Sobrien#endif /* not lint */ 4559243Sobrien 4669408SacheRCSID("$Id: sh.c,v 3.92 2000/11/11 23:03:35 christos Exp $") 4759243Sobrien 4859243Sobrien#include "tc.h" 4959243Sobrien#include "ed.h" 5059243Sobrien#include "tw.h" 5159243Sobrien 5259243Sobrienextern bool MapsAreInited; 5359243Sobrienextern bool NLSMapsAreInited; 5459243Sobrienextern bool NoNLSRebind; 5559243Sobrien 5659243Sobrien/* 5759243Sobrien * C Shell 5859243Sobrien * 5959243Sobrien * Bill Joy, UC Berkeley, California, USA 6059243Sobrien * October 1978, May 1980 6159243Sobrien * 6259243Sobrien * Jim Kulp, IIASA, Laxenburg, Austria 6359243Sobrien * April 1980 6459243Sobrien * 6559243Sobrien * Filename recognition added: 6659243Sobrien * Ken Greer, Ind. Consultant, Palo Alto CA 6759243Sobrien * October 1983. 6859243Sobrien * 6959243Sobrien * Karl Kleinpaste, Computer Consoles, Inc. 7059243Sobrien * Added precmd, periodic/tperiod, prompt changes, 7159243Sobrien * directory stack hack, and login watch. 7259243Sobrien * Sometime March 1983 - Feb 1984. 7359243Sobrien * 7459243Sobrien * Added scheduled commands, including the "sched" command, 7559243Sobrien * plus the call to sched_run near the precmd et al 7659243Sobrien * routines. 7759243Sobrien * Upgraded scheduled events for running events while 7859243Sobrien * sitting idle at command input. 7959243Sobrien * 8059243Sobrien * Paul Placeway, Ohio State 8159243Sobrien * added stuff for running with twenex/inputl 9 Oct 1984. 8259243Sobrien * 8359243Sobrien * ported to Apple Unix (TM) (OREO) 26 -- 29 Jun 1987 8459243Sobrien */ 8559243Sobrien 8659243Sobrienjmp_buf_t reslab INIT_ZERO_STRUCT; 8759243Sobrien 8859243Sobrienstatic const char tcshstr[] = "tcsh"; 8969408Sache#ifdef WINNT_NATIVE 9059243Sobrienstatic const char tcshstr_nt[] = "tcsh.exe"; 9169408Sache#endif /* WINNT_NATIVE */ 9259243Sobrien 9359243Sobriensignalfun_t parintr = 0; /* Parents interrupt catch */ 9459243Sobriensignalfun_t parterm = 0; /* Parents terminate catch */ 9559243Sobrien 9659243Sobrien#ifdef TESLA 9759243Sobrienint do_logout = 0; 9859243Sobrien#endif /* TESLA */ 9959243Sobrien 10059243Sobrien 10159243Sobrienbool use_fork = 0; /* use fork() instead of vfork()? */ 10259243Sobrien 10359243Sobrien/* 10459243Sobrien * Magic pointer values. Used to specify other invalid conditions aside 10559243Sobrien * from null. 10659243Sobrien */ 10759243Sobrienstatic Char INVCHAR; 10859243SobrienChar *INVPTR = &INVCHAR; 10959243SobrienChar **INVPPTR = &INVPTR; 11059243Sobrien 11159243Sobrienstatic int nofile = 0; 11259243Sobrienstatic bool reenter = 0; 11359243Sobrienstatic bool nverbose = 0; 11459243Sobrienstatic bool nexececho = 0; 11559243Sobrienstatic bool quitit = 0; 11659243Sobrienstatic bool rdirs = 0; 11759243Sobrienbool fast = 0; 11859243Sobrienstatic bool batch = 0; 11959243Sobrienstatic bool mflag = 0; 12059243Sobrienstatic bool prompt = 1; 12159243Sobrienstatic int enterhist = 0; 12259243Sobrienbool tellwhat = 0; 12359243Sobrientime_t t_period; 12459243SobrienChar *ffile = NULL; 12559243Sobrienbool dolzero = 0; 12659243Sobrienint insource = 0; 12769408Sacheint exitset = 0; 12859243Sobrienstatic time_t chktim; /* Time mail last checked */ 12959243Sobrienchar *progname; 13059243Sobrienint tcsh; 13159243Sobrienextern char **environ; 13259243Sobrien 13359243Sobrien/* 13459243Sobrien * This preserves the input state of the shell. It is used by 13559243Sobrien * st_save and st_restore to manupulate shell state. 13659243Sobrien */ 13759243Sobrienstruct saved_state { 13859243Sobrien int insource; 13959243Sobrien int SHIN; 14059243Sobrien int intty; 14159243Sobrien struct whyle *whyles; 14259243Sobrien Char *gointr; 14359243Sobrien Char *arginp; 14459243Sobrien Char *evalp; 14559243Sobrien Char **evalvec; 14659243Sobrien Char *alvecp; 14759243Sobrien Char **alvec; 14859243Sobrien int onelflg; 14959243Sobrien bool enterhist; 15059243Sobrien Char **argv; 15159243Sobrien Char HIST; 15259243Sobrien bool cantell; 15359243Sobrien struct Bin B; 15459243Sobrien /* These keep signal state and setjump state */ 15559243Sobrien#ifdef BSDSIGS 15659243Sobrien sigmask_t mask; 15759243Sobrien#endif 15859243Sobrien jmp_buf_t oldexit; 15959243Sobrien int reenter; 16059243Sobrien}; 16159243Sobrien 16259243Sobrienstatic int srccat __P((Char *, Char *)); 16359243Sobrienstatic int srcfile __P((char *, bool, int, Char **)); 16459243Sobrienstatic sigret_t phup __P((int)); 16559243Sobrienstatic void srcunit __P((int, bool, int, Char **)); 16659243Sobrienstatic void mailchk __P((void)); 16759243Sobrien#ifndef _PATH_DEFPATH 16859243Sobrienstatic Char **defaultpath __P((void)); 16959243Sobrien#endif 17059243Sobrienstatic void record __P((void)); 17159243Sobrienstatic void st_save __P((struct saved_state *, int, int, 17259243Sobrien Char **, Char **)); 17359243Sobrienstatic void st_restore __P((struct saved_state *, Char **)); 17459243Sobrien 17559243Sobrien int main __P((int, char **)); 17659243Sobrien 17759243Sobrienint 17859243Sobrienmain(argc, argv) 17959243Sobrien int argc; 18059243Sobrien char **argv; 18159243Sobrien{ 18259243Sobrien register Char *cp; 18359243Sobrien#ifdef AUTOLOGOUT 18459243Sobrien register Char *cp2; 18559243Sobrien#endif 18659243Sobrien register char *tcp, *ttyn; 18759243Sobrien register int f; 18859243Sobrien register char **tempv; 18959243Sobrien 19059243Sobrien#ifdef BSDSIGS 19159243Sobrien sigvec_t osv; 19259243Sobrien#endif /* BSDSIGS */ 19359243Sobrien 19469408Sache#ifdef WINNT_NATIVE 19559243Sobrien nt_init(); 19669408Sache#endif /* WINNT_NATIVE */ 19759243Sobrien#if defined(NLS_CATALOGS) && defined(LC_MESSAGES) 19859243Sobrien (void) setlocale(LC_MESSAGES, ""); 19959243Sobrien#endif /* NLS_CATALOGS && LC_MESSAGES */ 20059243Sobrien 20159243Sobrien#ifdef NLS 20259243Sobrien# ifdef LC_CTYPE 20359243Sobrien (void) setlocale(LC_CTYPE, ""); /* for iscntrl */ 20459243Sobrien# endif /* LC_CTYPE */ 20559243Sobrien#endif /* NLS */ 20659243Sobrien 20759243Sobrien nlsinit(); 20859243Sobrien 20959243Sobrien#ifdef MALLOC_TRACE 21059243Sobrien mal_setstatsfile(fdopen(dup2(open("/tmp/tcsh.trace", 21159243Sobrien O_WRONLY|O_CREAT, 0666), 25), "w")); 21259243Sobrien mal_trace(1); 21359243Sobrien#endif /* MALLOC_TRACE */ 21459243Sobrien 21559243Sobrien#if !(defined(BSDTIMES) || defined(_SEQUENT_)) && defined(POSIX) 21659243Sobrien# ifdef _SC_CLK_TCK 21759243Sobrien clk_tck = (clock_t) sysconf(_SC_CLK_TCK); 21859243Sobrien# else /* ! _SC_CLK_TCK */ 21959243Sobrien# ifdef CLK_TCK 22059243Sobrien clk_tck = CLK_TCK; 22159243Sobrien# else /* !CLK_TCK */ 22259243Sobrien clk_tck = HZ; 22359243Sobrien# endif /* CLK_TCK */ 22459243Sobrien# endif /* _SC_CLK_TCK */ 22559243Sobrien#endif /* !BSDTIMES && POSIX */ 22659243Sobrien 22759243Sobrien settimes(); /* Immed. estab. timing base */ 22859243Sobrien#ifdef TESLA 22959243Sobrien do_logout = 0; 23059243Sobrien#endif /* TESLA */ 23159243Sobrien 23259243Sobrien /* 23359243Sobrien * Make sure we have 0, 1, 2 open 23459243Sobrien * Otherwise `` jobs will not work... (From knaff@poly.polytechnique.fr) 23559243Sobrien */ 23659243Sobrien { 23759243Sobrien do 23859243Sobrien if ((f = open(_PATH_DEVNULL, O_RDONLY)) == -1 && 23959243Sobrien (f = open("/", O_RDONLY)) == -1) 24059243Sobrien exit(1); 24159243Sobrien while (f < 3); 24259243Sobrien (void) close(f); 24359243Sobrien } 24459243Sobrien 24559243Sobrien osinit(); /* Os dependent initialization */ 24659243Sobrien 24759243Sobrien 24859243Sobrien { 24959243Sobrien char *t; 25059243Sobrien 25159243Sobrien t = strrchr(argv[0], '/'); 25269408Sache#ifdef WINNT_NATIVE 25359243Sobrien { 25459243Sobrien char *s = strrchr(argv[0], '\\'); 25559243Sobrien if (s) 25659243Sobrien t = s; 25759243Sobrien } 25869408Sache#endif /* WINNT_NATIVE */ 25959243Sobrien t = t ? t + 1 : argv[0]; 26059243Sobrien if (*t == '-') t++; 26159243Sobrien progname = strsave((t && *t) ? t : tcshstr); /* never want a null */ 26259243Sobrien tcsh = strcmp(progname, tcshstr) == 0; 26359243Sobrien } 26459243Sobrien 26559243Sobrien /* 26659243Sobrien * Initialize non constant strings 26759243Sobrien */ 26859243Sobrien#ifdef _PATH_BSHELL 26959243Sobrien STR_BSHELL = SAVE(_PATH_BSHELL); 27059243Sobrien#endif 27159243Sobrien#ifdef _PATH_TCSHELL 27259243Sobrien STR_SHELLPATH = SAVE(_PATH_TCSHELL); 27359243Sobrien#else 27459243Sobrien# ifdef _PATH_CSHELL 27559243Sobrien STR_SHELLPATH = SAVE(_PATH_CSHELL); 27659243Sobrien# endif 27759243Sobrien#endif 27859243Sobrien STR_environ = blk2short(environ); 27959243Sobrien environ = short2blk(STR_environ); /* So that we can free it */ 28059243Sobrien STR_WORD_CHARS = SAVE(WORD_CHARS); 28159243Sobrien 28259243Sobrien HIST = '!'; 28359243Sobrien HISTSUB = '^'; 28459243Sobrien PRCH = '>'; 28559243Sobrien PRCHROOT = '#'; 28659243Sobrien word_chars = STR_WORD_CHARS; 28759243Sobrien bslash_quote = 0; /* PWP: do tcsh-style backslash quoting? */ 28859243Sobrien 28959243Sobrien /* Default history size to 100 */ 29059243Sobrien set(STRhistory, SAVE("100"), VAR_READWRITE); 29159243Sobrien 29259243Sobrien tempv = argv; 29359243Sobrien ffile = SAVE(tempv[0]); 29459243Sobrien dolzero = 0; 29559243Sobrien if (eq(ffile, STRaout)) /* A.out's are quittable */ 29659243Sobrien quitit = 1; 29759243Sobrien uid = getuid(); 29859243Sobrien gid = getgid(); 29959243Sobrien euid = geteuid(); 30059243Sobrien egid = getegid(); 30159243Sobrien#if defined(OREO) || defined(DT_SUPPORT) 30259243Sobrien /* 30359243Sobrien * We are a login shell if: 1. we were invoked as -<something> with 30459243Sobrien * optional arguments 2. or we were invoked only with the -l flag 30559243Sobrien */ 30659243Sobrien loginsh = (**tempv == '-') || (argc == 2 && 30759243Sobrien tempv[1][0] == '-' && tempv[1][1] == 'l' && 30859243Sobrien tempv[1][2] == '\0'); 30959243Sobrien#else 31059243Sobrien /* 31159243Sobrien * We are a login shell if: 1. we were invoked as -<something> and we had 31259243Sobrien * no arguments 2. or we were invoked only with the -l flag 31359243Sobrien */ 31459243Sobrien loginsh = (**tempv == '-' && argc == 1) || (argc == 2 && 31559243Sobrien tempv[1][0] == '-' && tempv[1][1] == 'l' && 31659243Sobrien tempv[1][2] == '\0'); 31759243Sobrien#endif 31859243Sobrien 31959243Sobrien#ifdef _VMS_POSIX 32059243Sobrien /* No better way to find if we are a login shell */ 32159243Sobrien if (!loginsh) { 32259243Sobrien loginsh = (argc == 1 && getppid() == 1); 32359243Sobrien **tempv = '-'; /* Avoid giving VMS an acidic stomach */ 32459243Sobrien } 32559243Sobrien#endif /* _VMS_POSIX */ 32659243Sobrien 32759243Sobrien if (loginsh && **tempv != '-') { 32859243Sobrien /* 32959243Sobrien * Mangle the argv space 33059243Sobrien */ 33159243Sobrien tempv[1][0] = '\0'; 33259243Sobrien tempv[1][1] = '\0'; 33359243Sobrien tempv[1] = NULL; 33459243Sobrien for (tcp = *tempv; *tcp++;) 33559243Sobrien continue; 33659243Sobrien for (tcp--; tcp >= *tempv; tcp--) 33759243Sobrien tcp[1] = tcp[0]; 33859243Sobrien *++tcp = '-'; 33959243Sobrien argc--; 34059243Sobrien } 34159243Sobrien if (loginsh) { 34259243Sobrien (void) time(&chktim); 34359243Sobrien set(STRloginsh, Strsave(STRNULL), VAR_READWRITE); 34459243Sobrien } 34559243Sobrien 34659243Sobrien AsciiOnly = 1; 34759243Sobrien NoNLSRebind = getenv("NOREBIND") != NULL; 34859243Sobrien#ifdef NLS 34959243Sobrien# ifdef SETLOCALEBUG 35059243Sobrien dont_free = 1; 35159243Sobrien# endif /* SETLOCALEBUG */ 35259243Sobrien (void) setlocale(LC_ALL, ""); 35359243Sobrien# ifdef LC_COLLATE 35459243Sobrien (void) setlocale(LC_COLLATE, ""); 35559243Sobrien# endif 35659243Sobrien# ifdef SETLOCALEBUG 35759243Sobrien dont_free = 0; 35859243Sobrien# endif /* SETLOCALEBUG */ 35959243Sobrien# ifdef STRCOLLBUG 36059243Sobrien fix_strcoll_bug(); 36159243Sobrien# endif /* STRCOLLBUG */ 36259243Sobrien 36359243Sobrien { 36459243Sobrien int k; 36559243Sobrien 36659243Sobrien for (k = 0200; k <= 0377 && !Isprint(k); k++) 36759243Sobrien continue; 36859243Sobrien AsciiOnly = k > 0377; 36959243Sobrien } 37059243Sobrien#else 37159243Sobrien AsciiOnly = getenv("LANG") == NULL && getenv("LC_CTYPE") == NULL; 37259243Sobrien#endif /* NLS */ 37359243Sobrien if (MapsAreInited && !NLSMapsAreInited) 37459243Sobrien ed_InitNLSMaps(); 37559243Sobrien ResetArrowKeys(); 37659243Sobrien 37759243Sobrien /* 37859243Sobrien * Initialize for periodic command intervals. Also, initialize the dummy 37959243Sobrien * tty list for login-watch. 38059243Sobrien */ 38159243Sobrien (void) time(&t_period); 38259243Sobrien#ifndef HAVENOUTMP 38359243Sobrien initwatch(); 38459243Sobrien#endif /* !HAVENOUTMP */ 38559243Sobrien 38659243Sobrien#if defined(alliant) 38759243Sobrien /* 38859243Sobrien * From: Jim Pace <jdp@research.att.com> 38959243Sobrien * tcsh does not work properly on the alliants through an rlogin session. 39059243Sobrien * The shell generally hangs. Also, reference to the controlling terminal 39159243Sobrien * does not work ( ie: echo foo > /dev/tty ). 39259243Sobrien * 39359243Sobrien * A security feature was added to rlogind affecting FX/80's Concentrix 39459243Sobrien * from revision 5.5.xx upwards (through 5.7 where this fix was implemented) 39559243Sobrien * This security change also affects the FX/2800 series. 39659243Sobrien * The security change to rlogind requires the process group of an rlogin 39759243Sobrien * session become disassociated with the tty in rlogind. 39859243Sobrien * 39959243Sobrien * The changes needed are: 40059243Sobrien * 1. set the process group 40159243Sobrien * 2. reenable the control terminal 40259243Sobrien */ 40359243Sobrien if (loginsh && isatty(SHIN)) { 40459243Sobrien ttyn = (char *) ttyname(SHIN); 40559243Sobrien (void) close(SHIN); 40659243Sobrien SHIN = open(ttyn, O_RDWR); 40759243Sobrien shpgrp = getpid(); 40859243Sobrien (void) ioctl (SHIN, TIOCSPGRP, (ioctl_t) &shpgrp); 40959243Sobrien (void) setpgid(0, shpgrp); 41059243Sobrien } 41159243Sobrien#endif /* alliant */ 41259243Sobrien 41359243Sobrien /* 41459243Sobrien * Move the descriptors to safe places. The variable didfds is 0 while we 41559243Sobrien * have only FSH* to work with. When didfds is true, we have 0,1,2 and 41659243Sobrien * prefer to use these. 41759243Sobrien */ 41859243Sobrien initdesc(); 41959243Sobrien 42059243Sobrien /* 42159243Sobrien * Get and set the tty now 42259243Sobrien */ 42359243Sobrien if ((ttyn = ttyname(SHIN)) != NULL) { 42459243Sobrien /* 42559243Sobrien * Could use rindex to get rid of other possible path components, but 42659243Sobrien * hpux preserves the subdirectory /pty/ when storing the tty name in 42759243Sobrien * utmp, so we keep it too. 42859243Sobrien */ 42959243Sobrien if (strncmp(ttyn, "/dev/", 5) == 0) 43059243Sobrien set(STRtty, cp = SAVE(ttyn + 5), VAR_READWRITE); 43159243Sobrien else 43259243Sobrien set(STRtty, cp = SAVE(ttyn), VAR_READWRITE); 43359243Sobrien } 43459243Sobrien else 43559243Sobrien set(STRtty, cp = SAVE(""), VAR_READWRITE); 43659243Sobrien /* 43759243Sobrien * Initialize the shell variables. ARGV and PROMPT are initialized later. 43859243Sobrien * STATUS is also munged in several places. CHILD is munged when 43959243Sobrien * forking/waiting 44059243Sobrien */ 44159243Sobrien 44259243Sobrien /* 44359243Sobrien * 7-10-87 Paul Placeway autologout should be set ONLY on login shells and 44459243Sobrien * on shells running as root. Out of these, autologout should NOT be set 44559243Sobrien * for any psudo-terminals (this catches most window systems) and not for 44659243Sobrien * any terminal running X windows. 44759243Sobrien * 44859243Sobrien * At Ohio State, we have had problems with a user having his X session 44959243Sobrien * drop out from under him (on a Sun) because the shell in his master 45059243Sobrien * xterm timed out and exited. 45159243Sobrien * 45259243Sobrien * Really, this should be done with a program external to the shell, that 45359243Sobrien * watches for no activity (and NO running programs, such as dump) on a 45459243Sobrien * terminal for a long peroid of time, and then SIGHUPS the shell on that 45559243Sobrien * terminal. 45659243Sobrien * 45759243Sobrien * bugfix by Rich Salz <rsalz@PINEAPPLE.BBN.COM>: For root rsh things 45859243Sobrien * allways first check to see if loginsh or really root, then do things 45959243Sobrien * with ttyname() 46059243Sobrien * 46159243Sobrien * Also by Jean-Francois Lamy <lamy%ai.toronto.edu@RELAY.CS.NET>: check the 46259243Sobrien * value of cp before using it! ("root can rsh too") 46359243Sobrien * 46459243Sobrien * PWP: keep the nested ifs; the order of the tests matters and a good 46559243Sobrien * (smart) C compiler might re-arange things wrong. 46659243Sobrien */ 46759243Sobrien#ifdef AUTOLOGOUT 46859243Sobrien# ifdef convex 46959243Sobrien if (uid == 0) { 47059243Sobrien /* root always has a 15 minute autologout */ 47159243Sobrien set(STRautologout, Strsave(STRrootdefautologout), VAR_READWRITE); 47259243Sobrien } 47359243Sobrien else 47459243Sobrien if (loginsh) 47559243Sobrien /* users get autologout set to 0 */ 47659243Sobrien set(STRautologout, Strsave(STR0), VAR_READWRITE); 47759243Sobrien# else /* convex */ 47859243Sobrien if (loginsh || (uid == 0)) { 47959243Sobrien if (*cp) { 48059243Sobrien /* only for login shells or root and we must have a tty */ 48159243Sobrien if ((cp2 = Strrchr(cp, (Char) '/')) != NULL) { 48259243Sobrien cp = cp2 + 1; 48359243Sobrien } 48459243Sobrien else 48559243Sobrien cp2 = cp; 48659243Sobrien if (!(((Strncmp(cp2, STRtty, 3) == 0) && Isalpha(cp2[3])) || 48759243Sobrien ((Strncmp(cp, STRpts, 3) == 0) && cp[3] == '/'))) { 48859243Sobrien if (getenv("DISPLAY") == NULL) { 48959243Sobrien /* NOT on X window shells */ 49059243Sobrien set(STRautologout, Strsave(STRdefautologout), 49159243Sobrien VAR_READWRITE); 49259243Sobrien } 49359243Sobrien } 49459243Sobrien } 49559243Sobrien } 49659243Sobrien# endif /* convex */ 49759243Sobrien#endif /* AUTOLOGOUT */ 49859243Sobrien 49959243Sobrien (void) sigset(SIGALRM, alrmcatch); 50059243Sobrien 50159243Sobrien set(STRstatus, Strsave(STR0), VAR_READWRITE); 50259243Sobrien 50359243Sobrien /* 50459243Sobrien * get and set machine specific envirnment variables 50559243Sobrien */ 50659243Sobrien getmachine(); 50759243Sobrien 50859243Sobrien fix_version(); /* publish the shell version */ 50959243Sobrien 51059243Sobrien /* 51159243Sobrien * Publish the selected echo style 51259243Sobrien */ 51359243Sobrien#if ECHO_STYLE == NONE_ECHO 51459243Sobrien set(STRecho_style, Strsave(STRnone), VAR_READWRITE); 51559243Sobrien#endif /* ECHO_STYLE == NONE_ECHO */ 51659243Sobrien#if ECHO_STYLE == BSD_ECHO 51759243Sobrien set(STRecho_style, Strsave(STRbsd), VAR_READWRITE); 51859243Sobrien#endif /* ECHO_STYLE == BSD_ECHO */ 51959243Sobrien#if ECHO_STYLE == SYSV_ECHO 52059243Sobrien set(STRecho_style, Strsave(STRsysv), VAR_READWRITE); 52159243Sobrien#endif /* ECHO_STYLE == SYSV_ECHO */ 52259243Sobrien#if ECHO_STYLE == BOTH_ECHO 52359243Sobrien set(STRecho_style, Strsave(STRboth), VAR_READWRITE); 52459243Sobrien#endif /* ECHO_STYLE == BOTH_ECHO */ 52559243Sobrien 52659243Sobrien /* 52759243Sobrien * increment the shell level. 52859243Sobrien */ 52959243Sobrien shlvl(1); 53059243Sobrien 53159243Sobrien if ((tcp = getenv("HOME")) != NULL) 53259243Sobrien cp = quote(SAVE(tcp)); 53359243Sobrien else 53459243Sobrien cp = NULL; 53559243Sobrien if (cp == NULL) 53659243Sobrien fast = 1; /* No home -> can't read scripts */ 53759243Sobrien else 53859243Sobrien set(STRhome, cp, VAR_READWRITE); 53959243Sobrien dinit(cp); /* dinit thinks that HOME == cwd in a login 54059243Sobrien * shell */ 54159243Sobrien /* 54259243Sobrien * Grab other useful things from the environment. Should we grab 54359243Sobrien * everything?? 54459243Sobrien */ 54559243Sobrien { 54659243Sobrien char *cln, *cus, *cgr; 54759243Sobrien Char buff[BUFSIZE]; 54859243Sobrien struct passwd *pw; 54959243Sobrien struct group *gr; 55059243Sobrien 55159243Sobrien 55259243Sobrien#ifdef apollo 55359243Sobrien int oid = getoid(); 55459243Sobrien 55559243Sobrien (void) Itoa(oid, buff, 0, 0); 55659243Sobrien set(STRoid, Strsave(buff), VAR_READWRITE); 55759243Sobrien#endif /* apollo */ 55859243Sobrien 55959243Sobrien (void) Itoa(uid, buff, 0, 0); 56059243Sobrien set(STRuid, Strsave(buff), VAR_READWRITE); 56159243Sobrien 56259243Sobrien (void) Itoa(gid, buff, 0, 0); 56359243Sobrien set(STRgid, Strsave(buff), VAR_READWRITE); 56459243Sobrien 56559243Sobrien cln = getenv("LOGNAME"); 56659243Sobrien cus = getenv("USER"); 56759243Sobrien if (cus != NULL) 56859243Sobrien set(STRuser, quote(SAVE(cus)), VAR_READWRITE); 56959243Sobrien else if (cln != NULL) 57059243Sobrien set(STRuser, quote(SAVE(cln)), VAR_READWRITE); 57159243Sobrien else if ((pw = getpwuid(uid)) == NULL) 57259243Sobrien set(STRuser, SAVE("unknown"), VAR_READWRITE); 57359243Sobrien else 57459243Sobrien set(STRuser, SAVE(pw->pw_name), VAR_READWRITE); 57559243Sobrien if (cln == NULL) 57659243Sobrien tsetenv(STRLOGNAME, varval(STRuser)); 57759243Sobrien if (cus == NULL) 57859243Sobrien tsetenv(STRKUSER, varval(STRuser)); 57959243Sobrien 58059243Sobrien cgr = getenv("GROUP"); 58159243Sobrien if (cgr != NULL) 58259243Sobrien set(STRgroup, quote(SAVE(cgr)), VAR_READWRITE); 58359243Sobrien else if ((gr = getgrgid(gid)) == NULL) 58459243Sobrien set(STRgroup, SAVE("unknown"), VAR_READWRITE); 58559243Sobrien else 58659243Sobrien set(STRgroup, SAVE(gr->gr_name), VAR_READWRITE); 58759243Sobrien if (cgr == NULL) 58859243Sobrien tsetenv(STRKGROUP, varval(STRgroup)); 58959243Sobrien } 59059243Sobrien 59159243Sobrien /* 59259243Sobrien * HOST may be wrong, since rexd transports the entire environment on sun 59359243Sobrien * 3.x Just set it again 59459243Sobrien */ 59559243Sobrien { 59659243Sobrien char cbuff[MAXHOSTNAMELEN]; 59759243Sobrien 59859243Sobrien if (gethostname(cbuff, sizeof(cbuff)) >= 0) { 59959243Sobrien cbuff[sizeof(cbuff) - 1] = '\0'; /* just in case */ 60059243Sobrien tsetenv(STRHOST, str2short(cbuff)); 60159243Sobrien } 60259243Sobrien else 60359243Sobrien tsetenv(STRHOST, str2short("unknown")); 60459243Sobrien } 60559243Sobrien 60659243Sobrien 60759243Sobrien#ifdef REMOTEHOST 60859243Sobrien /* 60959243Sobrien * Try to determine the remote host we were logged in from. 61059243Sobrien */ 61159243Sobrien remotehost(); 61259243Sobrien#endif /* REMOTEHOST */ 61359243Sobrien 61459243Sobrien#ifdef apollo 61559243Sobrien if ((tcp = getenv("SYSTYPE")) == NULL) 61659243Sobrien tcp = "bsd4.3"; 61759243Sobrien tsetenv(STRSYSTYPE, quote(str2short(tcp))); 61859243Sobrien#endif /* apollo */ 61959243Sobrien 62059243Sobrien /* 62159243Sobrien * set editing on by default, unless running under Emacs as an inferior 62259243Sobrien * shell. 62359243Sobrien * We try to do this intelligently. If $TERM is available, then it 62459243Sobrien * should determine if we should edit or not. $TERM is preserved 62559243Sobrien * across rlogin sessions, so we will not get confused if we rlogin 62659243Sobrien * under an emacs shell. Another advantage is that if we run an 62759243Sobrien * xterm under an emacs shell, then the $TERM will be set to 62859243Sobrien * xterm, so we are going to want to edit. Unfortunately emacs 62959243Sobrien * does not restore all the tty modes, so xterm is not very well 63059243Sobrien * set up. But this is not the shell's fault. 63159243Sobrien * Also don't edit if $TERM == wm, for when we're running under an ATK app. 63259243Sobrien * Finally, emacs compiled under terminfo, sets the terminal to dumb, 63359243Sobrien * so disable editing for that too. 63459243Sobrien * 63559243Sobrien * Unfortunately, in some cases the initial $TERM setting is "unknown", 63659243Sobrien * "dumb", or "network" which is then changed in the user's startup files. 63759243Sobrien * We fix this by setting noediting here if $TERM is unknown/dumb and 63859243Sobrien * if noediting is set, we switch on editing if $TERM is changed. 63959243Sobrien */ 64059243Sobrien if ((tcp = getenv("TERM")) != NULL) { 64159243Sobrien set(STRterm, quote(SAVE(tcp)), VAR_READWRITE); 64259243Sobrien noediting = strcmp(tcp, "unknown") == 0 || strcmp(tcp, "dumb") == 0 || 64359243Sobrien strcmp(tcp, "network") == 0; 64459243Sobrien editing = strcmp(tcp, "emacs") != 0 && strcmp(tcp, "wm") != 0 && 64559243Sobrien !noediting; 64659243Sobrien } 64759243Sobrien else { 64859243Sobrien noediting = 0; 64959243Sobrien editing = ((tcp = getenv("EMACS")) == NULL || strcmp(tcp, "t") != 0); 65059243Sobrien } 65159243Sobrien 65259243Sobrien /* 65359243Sobrien * The 'edit' variable is either set or unset. It doesn't 65459243Sobrien * need a value. Making it 'emacs' might be confusing. 65559243Sobrien */ 65659243Sobrien if (editing) 65759243Sobrien set(STRedit, Strsave(STRNULL), VAR_READWRITE); 65859243Sobrien 65959243Sobrien 66059243Sobrien /* 66159243Sobrien * still more mutability: make the complete routine automatically add the 66259243Sobrien * suffix of file names... 66359243Sobrien */ 66459243Sobrien set(STRaddsuffix, Strsave(STRNULL), VAR_READWRITE); 66559243Sobrien 66659243Sobrien /* 66759243Sobrien * Re-initialize path if set in environment 66859243Sobrien */ 66959243Sobrien if ((tcp = getenv("PATH")) == NULL) 67059243Sobrien#ifdef _PATH_DEFPATH 67159243Sobrien importpath(str2short(_PATH_DEFPATH)); 67259243Sobrien#else /* !_PATH_DEFPATH */ 67359243Sobrien setq(STRpath, defaultpath(), &shvhed, VAR_READWRITE); 67459243Sobrien#endif /* _PATH_DEFPATH */ 67559243Sobrien else 67659243Sobrien /* Importpath() allocates memory for the path, and the 67759243Sobrien * returned pointer from SAVE() was discarded, so 67859243Sobrien * this was a memory leak.. (sg) 67959243Sobrien * 68059243Sobrien * importpath(SAVE(tcp)); 68159243Sobrien */ 68259243Sobrien importpath(str2short(tcp)); 68359243Sobrien 68459243Sobrien 68559243Sobrien { 68659243Sobrien /* If the SHELL environment variable ends with "tcsh", set 68759243Sobrien * STRshell to the same path. This is to facilitate using 68859243Sobrien * the executable in environments where the compiled-in 68959243Sobrien * default isn't appropriate (sg). 69059243Sobrien */ 69159243Sobrien 69259243Sobrien int sh_len = 0; 69359243Sobrien 69459243Sobrien if ((tcp = getenv("SHELL")) != NULL) { 69559243Sobrien sh_len = strlen(tcp); 69659243Sobrien if ((sh_len >= 5 && strcmp(tcp + (sh_len - 5), "/tcsh") == 0) || 69759243Sobrien (!tcsh && sh_len >= 4 && strcmp(tcp + (sh_len - 4), "/csh") == 0)) 69859243Sobrien set(STRshell, quote(SAVE(tcp)), VAR_READWRITE); 69959243Sobrien else 70059243Sobrien sh_len = 0; 70159243Sobrien } 70259243Sobrien if (sh_len == 0) 70359243Sobrien set(STRshell, Strsave(STR_SHELLPATH), VAR_READWRITE); 70459243Sobrien } 70559243Sobrien 70659243Sobrien#ifdef COLOR_LS_F 70759243Sobrien if ((tcp = getenv("LS_COLORS")) != NULL) 70859243Sobrien parseLS_COLORS(str2short(tcp)); 70959243Sobrien#endif /* COLOR_LS_F */ 71059243Sobrien 71159243Sobrien doldol = putn((int) getpid()); /* For $$ */ 71269408Sache#ifdef WINNT_NATIVE 71359243Sobrien { 71459243Sobrien char *strtmp1, strtmp2[MAXPATHLEN]; 71559243Sobrien if ((strtmp1 = getenv("TMP")) != NULL) 71659243Sobrien wsprintf(strtmp2, "%s/%s", strtmp1, "sh"); 71759243Sobrien shtemp = Strspl(SAVE(strtmp2), doldol); /* For << */ 71859243Sobrien } 71969408Sache#else /* !WINNT_NATIVE */ 72059243Sobrien shtemp = Strspl(STRtmpsh, doldol); /* For << */ 72169408Sache#endif /* WINNT_NATIVE */ 72259243Sobrien 72359243Sobrien /* 72459243Sobrien * Record the interrupt states from the parent process. If the parent is 72559243Sobrien * non-interruptible our hand must be forced or we (and our children) won't 72659243Sobrien * be either. Our children inherit termination from our parent. We catch it 72759243Sobrien * only if we are the login shell. 72859243Sobrien */ 72959243Sobrien#ifdef BSDSIGS 73059243Sobrien /* 73159243Sobrien * PURIFY-2 claims that osv does not get 73259243Sobrien * initialized after the sigvec call 73359243Sobrien */ 73459243Sobrien setzero((char*) &osv, sizeof(osv)); 73559243Sobrien /* parents interruptibility */ 73659243Sobrien (void) mysigvec(SIGINT, NULL, &osv); 73759243Sobrien parintr = (signalfun_t) osv.sv_handler; 73859243Sobrien (void) mysigvec(SIGTERM, NULL, &osv); 73959243Sobrien parterm = (signalfun_t) osv.sv_handler; 74059243Sobrien#else /* BSDSIGS */ 74159243Sobrien parintr = signal(SIGINT, SIG_IGN); /* parents interruptibility */ 74259243Sobrien (void) sigset(SIGINT, parintr); /* ... restore */ 74359243Sobrien 74459243Sobrien# ifdef COHERENT 74559243Sobrien if (loginsh) /* it seems that SIGTERM is always set to SIG_IGN by */ 74659243Sobrien /* init/getty so it should be set to SIG_DFL - there may be */ 74759243Sobrien /* a better fix for this. */ 74859243Sobrien parterm = SIG_DFL; 74959243Sobrien else 75059243Sobrien# else /* !COHERENT */ 75159243Sobrien parterm = signal(SIGTERM, SIG_IGN); /* parents terminability */ 75259243Sobrien# endif /* COHERENT */ 75359243Sobrien (void) sigset(SIGTERM, parterm); /* ... restore */ 75459243Sobrien 75559243Sobrien#endif /* BSDSIGS */ 75659243Sobrien 75759243Sobrien 75859243Sobrien#ifdef TCF 75959243Sobrien /* Enable process migration on ourselves and our progeny */ 76059243Sobrien (void) signal(SIGMIGRATE, SIG_DFL); 76159243Sobrien#endif /* TCF */ 76259243Sobrien 76359243Sobrien /* 76469408Sache * dspkanji/dspmbyte autosetting 76569408Sache */ 76669408Sache /* PATCH IDEA FROM Issei.Suzuki VERY THANKS */ 76769408Sache#if defined(DSPMBYTE) 76869408Sache#if defined(NLS) && defined(LC_CTYPE) 76969408Sache if (((tcp = setlocale(LC_CTYPE, NULL)) != NULL || (tcp = getenv("LANG")) != NULL) && !adrof(CHECK_MBYTEVAR)) { 77069408Sache#else 77169408Sache if ((tcp = getenv("LANG")) != NULL && !adrof(CHECK_MBYTEVAR)) { 77269408Sache#endif 77369408Sache autoset_dspmbyte(str2short(tcp)); 77469408Sache } 77569408Sache#if defined(WINNT_NATIVE) 77669408Sache else if (!adrof(CHECK_MBYTEVAR)) 77769408Sache nt_autoset_dspmbyte(); 77869408Sache#endif /* WINNT_NATIVE */ 77969408Sache#endif 78069408Sache 78169408Sache /* 78259243Sobrien * Process the arguments. 78359243Sobrien * 78459243Sobrien * Note that processing of -v/-x is actually delayed till after script 78559243Sobrien * processing. 78659243Sobrien * 78759243Sobrien * We set the first character of our name to be '-' if we are a shell 78859243Sobrien * running interruptible commands. Many programs which examine ps'es 78959243Sobrien * use this to filter such shells out. 79059243Sobrien */ 79159243Sobrien argc--, tempv++; 79259243Sobrien while (argc > 0 && (tcp = tempv[0])[0] == '-' && 79359243Sobrien *++tcp != '\0' && !batch) { 79459243Sobrien do 79559243Sobrien switch (*tcp++) { 79659243Sobrien 79759243Sobrien case 0: /* - Interruptible, no prompt */ 79859243Sobrien prompt = 0; 79959243Sobrien setintr = 1; 80059243Sobrien nofile = 1; 80159243Sobrien break; 80259243Sobrien 80359243Sobrien case 'b': /* -b Next arg is input file */ 80459243Sobrien batch = 1; 80559243Sobrien break; 80659243Sobrien 80759243Sobrien case 'c': /* -c Command input from arg */ 80859243Sobrien if (argc == 1) 80959243Sobrien xexit(0); 81059243Sobrien argc--, tempv++; 81159243Sobrien#ifdef M_XENIX 81259243Sobrien /* Xenix Vi bug: 81359243Sobrien it relies on a 7 bit environment (/bin/sh), so it 81459243Sobrien pass ascii arguments with the 8th bit set */ 81559243Sobrien if (!strcmp(argv[0], "sh")) 81659243Sobrien { 81759243Sobrien char *p; 81859243Sobrien 81959243Sobrien for (p = tempv[0]; *p; ++p) 82059243Sobrien *p &= ASCII; 82159243Sobrien } 82259243Sobrien#endif 82359243Sobrien arginp = SAVE(tempv[0]); 82459243Sobrien 82559243Sobrien /* 82659243Sobrien * we put the command into a variable 82759243Sobrien */ 82859243Sobrien if (arginp != NULL) 82959243Sobrien set(STRcommand, quote(Strsave(arginp)), VAR_READWRITE); 83059243Sobrien 83159243Sobrien /* 83259243Sobrien * * Give an error on -c arguments that end in * backslash to 83359243Sobrien * ensure that you don't make * nonportable csh scripts. 83459243Sobrien */ 83559243Sobrien { 83659243Sobrien register int count; 83759243Sobrien 83859243Sobrien cp = arginp + Strlen(arginp); 83959243Sobrien count = 0; 84059243Sobrien while (cp > arginp && *--cp == '\\') 84159243Sobrien ++count; 84259243Sobrien if ((count & 1) != 0) { 84359243Sobrien exiterr = 1; 84459243Sobrien stderror(ERR_ARGC); 84559243Sobrien } 84659243Sobrien } 84759243Sobrien prompt = 0; 84859243Sobrien nofile = 1; 84959243Sobrien break; 85059243Sobrien case 'd': /* -d Load directory stack from file */ 85159243Sobrien rdirs = 1; 85259243Sobrien break; 85359243Sobrien 85459243Sobrien#ifdef apollo 85559243Sobrien case 'D': /* -D Define environment variable */ 85659243Sobrien { 85759243Sobrien register Char *dp; 85859243Sobrien 85959243Sobrien cp = str2short(tcp); 86059243Sobrien if (dp = Strchr(cp, '=')) { 86159243Sobrien *dp++ = '\0'; 86259243Sobrien tsetenv(cp, dp); 86359243Sobrien } 86459243Sobrien else 86559243Sobrien tsetenv(cp, STRNULL); 86659243Sobrien } 86759243Sobrien *tcp = '\0'; /* done with this argument */ 86859243Sobrien break; 86959243Sobrien#endif /* apollo */ 87059243Sobrien 87159243Sobrien case 'e': /* -e Exit on any error */ 87259243Sobrien exiterr = 1; 87359243Sobrien break; 87459243Sobrien 87559243Sobrien case 'f': /* -f Fast start */ 87659243Sobrien fast = 1; 87759243Sobrien break; 87859243Sobrien 87959243Sobrien case 'i': /* -i Interactive, even if !intty */ 88059243Sobrien intact = 1; 88159243Sobrien nofile = 1; 88259243Sobrien break; 88359243Sobrien 88459243Sobrien case 'm': /* -m read .cshrc (from su) */ 88559243Sobrien mflag = 1; 88659243Sobrien break; 88759243Sobrien 88859243Sobrien case 'n': /* -n Don't execute */ 88959243Sobrien noexec = 1; 89059243Sobrien break; 89159243Sobrien 89259243Sobrien case 'q': /* -q (Undoc'd) ... die on quit */ 89359243Sobrien quitit = 1; 89459243Sobrien break; 89559243Sobrien 89659243Sobrien case 's': /* -s Read from std input */ 89759243Sobrien nofile = 1; 89859243Sobrien break; 89959243Sobrien 90059243Sobrien case 't': /* -t Read one line from input */ 90159243Sobrien onelflg = 2; 90259243Sobrien prompt = 0; 90359243Sobrien nofile = 1; 90459243Sobrien break; 90559243Sobrien 90659243Sobrien case 'v': /* -v Echo hist expanded input */ 90759243Sobrien nverbose = 1; /* ... later */ 90859243Sobrien break; 90959243Sobrien 91059243Sobrien case 'x': /* -x Echo just before execution */ 91159243Sobrien nexececho = 1; /* ... later */ 91259243Sobrien break; 91359243Sobrien 91459243Sobrien case 'V': /* -V Echo hist expanded input */ 91559243Sobrien setNS(STRverbose); /* NOW! */ 91659243Sobrien break; 91759243Sobrien 91859243Sobrien case 'X': /* -X Echo just before execution */ 91959243Sobrien setNS(STRecho); /* NOW! */ 92059243Sobrien break; 92159243Sobrien 92259243Sobrien case 'F': /* Undocumented flag */ 92359243Sobrien /* 92459243Sobrien * This will cause children to be created using fork instead of 92559243Sobrien * vfork. 92659243Sobrien */ 92759243Sobrien use_fork = 1; 92859243Sobrien break; 92959243Sobrien 93059243Sobrien case ' ': 93159243Sobrien case '\t': 93259243Sobrien /* 93359243Sobrien * for O/S's that don't do the argument parsing right in 93459243Sobrien * "#!/foo -f " scripts 93559243Sobrien */ 93659243Sobrien break; 93759243Sobrien 93859243Sobrien default: /* Unknown command option */ 93959243Sobrien exiterr = 1; 94059243Sobrien stderror(ERR_TCSHUSAGE, tcp-1, progname); 94159243Sobrien break; 94259243Sobrien 94359243Sobrien } while (*tcp); 94459243Sobrien tempv++, argc--; 94559243Sobrien } 94659243Sobrien 94759243Sobrien if (quitit) /* With all due haste, for debugging */ 94859243Sobrien (void) signal(SIGQUIT, SIG_DFL); 94959243Sobrien 95059243Sobrien /* 95159243Sobrien * Unless prevented by -, -c, -i, -s, or -t, if there are remaining 95259243Sobrien * arguments the first of them is the name of a shell file from which to 95359243Sobrien * read commands. 95459243Sobrien */ 95559243Sobrien if (nofile == 0 && argc > 0) { 95659243Sobrien nofile = open(tempv[0], O_RDONLY); 95759243Sobrien if (nofile < 0) { 95859243Sobrien child = 1; /* So this ... */ 95959243Sobrien /* ... doesn't return */ 96059243Sobrien stderror(ERR_SYSTEM, tempv[0], strerror(errno)); 96159243Sobrien } 96259243Sobrien if (ffile != NULL) 96359243Sobrien xfree((ptr_t) ffile); 96459243Sobrien dolzero = 1; 96559243Sobrien ffile = SAVE(tempv[0]); 96659243Sobrien /* 96759243Sobrien * Replace FSHIN. Handle /dev/std{in,out,err} specially 96859243Sobrien * since once they are closed we cannot open them again. 96959243Sobrien * In that case we use our own saved descriptors 97059243Sobrien */ 97159243Sobrien if ((SHIN = dmove(nofile, FSHIN)) < 0) 97259243Sobrien switch(nofile) { 97359243Sobrien case 0: 97459243Sobrien SHIN = FSHIN; 97559243Sobrien break; 97659243Sobrien case 1: 97759243Sobrien SHIN = FSHOUT; 97859243Sobrien break; 97959243Sobrien case 2: 98059243Sobrien SHIN = FSHDIAG; 98159243Sobrien break; 98259243Sobrien default: 98359243Sobrien stderror(ERR_SYSTEM, tempv[0], strerror(errno)); 98459243Sobrien break; 98559243Sobrien } 98659243Sobrien (void) close_on_exec(SHIN, 1); 98759243Sobrien prompt = 0; 98859243Sobrien /* argc not used any more */ tempv++; 98959243Sobrien } 99059243Sobrien 99159243Sobrien 99259243Sobrien /* 99359243Sobrien * Consider input a tty if it really is or we are interactive. but not for 99459243Sobrien * editing (christos) 99559243Sobrien */ 99659243Sobrien if (!(intty = isatty(SHIN))) { 99759243Sobrien if (adrof(STRedit)) 99859243Sobrien unsetv(STRedit); 99959243Sobrien editing = 0; 100059243Sobrien } 100159243Sobrien intty |= intact; 100259243Sobrien#ifndef convex 100359243Sobrien if (intty || (intact && isatty(SHOUT))) { 100459243Sobrien if (!batch && (uid != euid || gid != egid)) { 100559243Sobrien errno = EACCES; 100659243Sobrien child = 1; /* So this ... */ 100759243Sobrien /* ... doesn't return */ 100859243Sobrien stderror(ERR_SYSTEM, progname, strerror(errno)); 100959243Sobrien } 101059243Sobrien } 101159243Sobrien#endif /* convex */ 101259243Sobrien isoutatty = isatty(SHOUT); 101359243Sobrien isdiagatty = isatty(SHDIAG); 101459243Sobrien /* 101559243Sobrien * Decide whether we should play with signals or not. If we are explicitly 101659243Sobrien * told (via -i, or -) or we are a login shell (arg0 starts with -) or the 101759243Sobrien * input and output are both the ttys("csh", or "csh</dev/ttyx>/dev/ttyx") 101859243Sobrien * Note that in only the login shell is it likely that parent may have set 101959243Sobrien * signals to be ignored 102059243Sobrien */ 102159243Sobrien if (loginsh || intact || (intty && isatty(SHOUT))) 102259243Sobrien setintr = 1; 102359243Sobrien settell(); 102459243Sobrien /* 102559243Sobrien * Save the remaining arguments in argv. 102659243Sobrien */ 102759243Sobrien setq(STRargv, blk2short(tempv), &shvhed, VAR_READWRITE); 102859243Sobrien 102959243Sobrien /* 103059243Sobrien * Set up the prompt. 103159243Sobrien */ 103259243Sobrien if (prompt) { 103359243Sobrien if (tcsh) 103459243Sobrien set(STRprompt, Strsave(STRdeftcshprompt), VAR_READWRITE); 103559243Sobrien else 103659243Sobrien set(STRprompt, Strsave(STRdefcshprompt), VAR_READWRITE); 103759243Sobrien /* that's a meta-questionmark */ 103859243Sobrien set(STRprompt2, Strsave(STRmquestion), VAR_READWRITE); 103959243Sobrien set(STRprompt3, Strsave(STRKCORRECT), VAR_READWRITE); 104059243Sobrien } 104159243Sobrien 104259243Sobrien /* 104359243Sobrien * If we are an interactive shell, then start fiddling with the signals; 104459243Sobrien * this is a tricky game. 104559243Sobrien */ 104659243Sobrien shpgrp = mygetpgrp(); 104759243Sobrien opgrp = tpgrp = -1; 104859243Sobrien if (setintr) { 104959243Sobrien signalfun_t osig; 105059243Sobrien **argv = '-'; 105159243Sobrien if (!quitit) /* Wary! */ 105259243Sobrien (void) signal(SIGQUIT, SIG_IGN); 105359243Sobrien (void) sigset(SIGINT, pintr); 105459243Sobrien (void) sighold(SIGINT); 105559243Sobrien (void) signal(SIGTERM, SIG_IGN); 105659243Sobrien 105759243Sobrien /* 105859243Sobrien * No reason I can see not to save history on all these events.. 105959243Sobrien * Most usual occurrence is in a window system, where we're not a login 106059243Sobrien * shell, but might as well be... (sg) 106159243Sobrien * But there might be races when lots of shells exit together... 106259243Sobrien * [this is also incompatible]. 106359243Sobrien * We have to be mre careful here. If the parent wants to 106459243Sobrien * ignore the signals then we leave them untouched... 106559243Sobrien * We also only setup the handlers for shells that are trully 106659243Sobrien * interactive. 106759243Sobrien */ 106859243Sobrien osig = signal(SIGHUP, phup); /* exit processing on HUP */ 106959243Sobrien if (!loginsh && osig == SIG_IGN) 107059243Sobrien (void) signal(SIGHUP, osig); 107159243Sobrien#ifdef SIGXCPU 107259243Sobrien osig = signal(SIGXCPU, phup); /* exit processing on XCPU */ 107359243Sobrien if (!loginsh && osig == SIG_IGN) 107459243Sobrien (void) signal(SIGXCPU, osig); 107559243Sobrien#endif 107659243Sobrien#ifdef SIGXFSZ 107759243Sobrien osig = signal(SIGXFSZ, phup); /* exit processing on XFSZ */ 107859243Sobrien if (!loginsh && osig == SIG_IGN) 107959243Sobrien (void) signal(SIGXFSZ, osig); 108059243Sobrien#endif 108159243Sobrien 108259243Sobrien if (quitit == 0 && arginp == 0) { 108359243Sobrien#ifdef SIGTSTP 108459243Sobrien (void) signal(SIGTSTP, SIG_IGN); 108559243Sobrien#endif 108659243Sobrien#ifdef SIGTTIN 108759243Sobrien (void) signal(SIGTTIN, SIG_IGN); 108859243Sobrien#endif 108959243Sobrien#ifdef SIGTTOU 109059243Sobrien (void) signal(SIGTTOU, SIG_IGN); 109159243Sobrien#endif 109259243Sobrien /* 109359243Sobrien * Wait till in foreground, in case someone stupidly runs csh & 109459243Sobrien * dont want to try to grab away the tty. 109559243Sobrien */ 109659243Sobrien if (isatty(FSHDIAG)) 109759243Sobrien f = FSHDIAG; 109859243Sobrien else if (isatty(FSHOUT)) 109959243Sobrien f = FSHOUT; 110059243Sobrien else if (isatty(OLDSTD)) 110159243Sobrien f = OLDSTD; 110259243Sobrien else 110359243Sobrien f = -1; 110459243Sobrien 110559243Sobrien#ifdef NeXT 110659243Sobrien /* NeXT 2.0 /usr/etc/rlogind, does not set our process group! */ 110759243Sobrien if (shpgrp == 0) { 110859243Sobrien shpgrp = getpid(); 110959243Sobrien (void) setpgid(0, shpgrp); 111059243Sobrien (void) tcsetpgrp(f, shpgrp); 111159243Sobrien } 111259243Sobrien#endif /* NeXT */ 111359243Sobrien#ifdef BSDJOBS /* if we have tty job control */ 111459243Sobrien retry: 111559243Sobrien if ((tpgrp = tcgetpgrp(f)) != -1) { 111659243Sobrien if (tpgrp != shpgrp) { 111759243Sobrien signalfun_t old = signal(SIGTTIN, SIG_DFL); 111859243Sobrien (void) kill(0, SIGTTIN); 111959243Sobrien (void) signal(SIGTTIN, old); 112059243Sobrien goto retry; 112159243Sobrien } 112259243Sobrien /* 112359243Sobrien * Thanks to Matt Day for the POSIX references, and to 112459243Sobrien * Paul Close for the SGI clarification. 112559243Sobrien */ 112659243Sobrien if (setdisc(f) != -1) { 112759243Sobrien opgrp = shpgrp; 112859243Sobrien shpgrp = getpid(); 112959243Sobrien tpgrp = shpgrp; 113059243Sobrien if (tcsetpgrp(f, shpgrp) == -1) { 113159243Sobrien /* 113259243Sobrien * On hpux 7.03 this fails with EPERM. This happens on 113359243Sobrien * the 800 when opgrp != shpgrp at this point. (we were 113459243Sobrien * forked from a non job control shell) 113559243Sobrien * POSIX 7.2.4, says we failed because the process 113659243Sobrien * group specified did not belong to a process 113759243Sobrien * in the same session with the tty. So we set our 113859243Sobrien * process group and try again. 113959243Sobrien */ 114059243Sobrien if (setpgid(0, shpgrp) == -1) { 114159243Sobrien xprintf("setpgid:"); 114259243Sobrien goto notty; 114359243Sobrien } 114459243Sobrien if (tcsetpgrp(f, shpgrp) == -1) { 114559243Sobrien xprintf("tcsetpgrp:"); 114659243Sobrien goto notty; 114759243Sobrien } 114859243Sobrien } 114959243Sobrien /* 115059243Sobrien * We check the process group now. If it is the same, then 115159243Sobrien * we don't need to set it again. On hpux 7.0 on the 300's 115259243Sobrien * if we set it again it fails with EPERM. This is the 115359243Sobrien * correct behavior according to POSIX 4.3.3 if the process 115459243Sobrien * was a session leader . 115559243Sobrien */ 115659243Sobrien else if (shpgrp != mygetpgrp()) { 115759243Sobrien if(setpgid(0, shpgrp) == -1) { 115859243Sobrien xprintf("setpgid:"); 115959243Sobrien goto notty; 116059243Sobrien } 116159243Sobrien } 116259243Sobrien#ifdef IRIS4D 116359243Sobrien /* 116459243Sobrien * But on irix 3.3 we need to set it again, even if it is 116559243Sobrien * the same. We do that to tell the system that we 116659243Sobrien * need BSD process group compatibility. 116759243Sobrien */ 116859243Sobrien else 116959243Sobrien (void) setpgid(0, shpgrp); 117059243Sobrien#endif 117159243Sobrien (void) close_on_exec(dcopy(f, FSHTTY), 1); 117259243Sobrien } 117359243Sobrien else 117459243Sobrien tpgrp = -1; 117559243Sobrien } 117659243Sobrien if (tpgrp == -1) { 117759243Sobrien notty: 117859243Sobrien xprintf(CGETS(11, 1, "Warning: no access to tty (%s).\n"), 117959243Sobrien strerror(errno)); 118059243Sobrien xprintf(CGETS(11, 2, "Thus no job control in this shell.\n")); 118159243Sobrien /* 118259243Sobrien * Fix from:Sakari Jalovaara <sja@sirius.hut.fi> if we don't 118359243Sobrien * have access to tty, disable editing too 118459243Sobrien */ 118559243Sobrien if (adrof(STRedit)) 118659243Sobrien unsetv(STRedit); 118759243Sobrien editing = 0; 118859243Sobrien } 118959243Sobrien#else /* BSDJOBS */ /* don't have job control, so frotz it */ 119059243Sobrien tpgrp = -1; 119159243Sobrien#endif /* BSDJOBS */ 119259243Sobrien } 119359243Sobrien } 119459243Sobrien if ((setintr == 0) && (parintr == SIG_DFL)) 119559243Sobrien setintr = 1; 119659243Sobrien 119759243Sobrien/* 119859243Sobrien * SVR4 doesn't send a SIGCHLD when a child is stopped or continued if the 119959243Sobrien * handler is installed with signal(2) or sigset(2). sigaction(2) must 120059243Sobrien * be used instead. 120159243Sobrien * 120259243Sobrien * David Dawes (dawes@physics.su.oz.au) Sept 1991 120359243Sobrien */ 120459243Sobrien 120559243Sobrien#if SYSVREL > 3 120659243Sobrien { 120759243Sobrien struct sigaction act; 120859243Sobrien act.sa_handler=pchild; 120959243Sobrien (void) sigemptyset(&(act.sa_mask)); /* Don't block any extra sigs 121059243Sobrien * when the handler is called 121159243Sobrien */ 121259243Sobrien act.sa_flags=0; /* want behaviour of sigset() without 121359243Sobrien * SA_NOCLDSTOP 121459243Sobrien */ 121559243Sobrien 121659243Sobrien if ((sigaction(SIGCHLD,&act,(struct sigaction *)NULL)) == -1) 121759243Sobrien stderror(ERR_SYSTEM, "sigaction", strerror(errno)); 121859243Sobrien } 121959243Sobrien#else /* SYSVREL <= 3 */ 122059243Sobrien (void) sigset(SIGCHLD, pchild); /* while signals not ready */ 122159243Sobrien#endif /* SYSVREL <= 3 */ 122259243Sobrien 122359243Sobrien 122459243Sobrien if (intty && !arginp) 122559243Sobrien (void) ed_Setup(editing);/* Get the tty state, and set defaults */ 122659243Sobrien /* Only alter the tty state if editing */ 122759243Sobrien 122859243Sobrien /* 122959243Sobrien * Set an exit here in case of an interrupt or error reading the shell 123059243Sobrien * start-up scripts. 123159243Sobrien */ 123259243Sobrien reenter = setexit(); /* PWP */ 123369408Sache exitset++; 123459243Sobrien haderr = 0; /* In case second time through */ 123559243Sobrien if (!fast && reenter == 0) { 123659243Sobrien /* Will have varval(STRhome) here because set fast if don't */ 123759243Sobrien { 123859243Sobrien int osetintr = setintr; 123959243Sobrien signalfun_t oparintr = parintr; 124059243Sobrien 124159243Sobrien#ifdef BSDSIGS 124259243Sobrien sigmask_t omask = sigblock(sigmask(SIGINT)); 124359243Sobrien#else 124459243Sobrien (void) sighold(SIGINT); 124559243Sobrien#endif 124659243Sobrien setintr = 0; 124759243Sobrien parintr = SIG_IGN; /* onintr in /etc/ files has no effect */ 124859243Sobrien#ifdef LOGINFIRST 124959243Sobrien#ifdef _PATH_DOTLOGIN 125059243Sobrien if (loginsh) 125159243Sobrien (void) srcfile(_PATH_DOTLOGIN, 0, 0, NULL); 125259243Sobrien#endif 125359243Sobrien#endif 125459243Sobrien 125559243Sobrien#ifdef _PATH_DOTCSHRC 125659243Sobrien (void) srcfile(_PATH_DOTCSHRC, 0, 0, NULL); 125759243Sobrien#endif 125859243Sobrien if (!arginp && !onelflg && !havhash) 125959243Sobrien dohash(NULL,NULL); 126059243Sobrien#ifndef LOGINFIRST 126159243Sobrien#ifdef _PATH_DOTLOGIN 126259243Sobrien if (loginsh) 126359243Sobrien (void) srcfile(_PATH_DOTLOGIN, 0, 0, NULL); 126459243Sobrien#endif 126559243Sobrien#endif 126659243Sobrien#ifdef BSDSIGS 126759243Sobrien (void) sigsetmask(omask); 126859243Sobrien#else 126959243Sobrien (void) sigrelse(SIGINT); 127059243Sobrien#endif 127159243Sobrien setintr = osetintr; 127259243Sobrien parintr = oparintr; 127359243Sobrien } 127459243Sobrien#ifdef LOGINFIRST 127559243Sobrien if (loginsh) 127659243Sobrien (void) srccat(varval(STRhome), STRsldotlogin); 127759243Sobrien#endif 127859243Sobrien /* upward compat. */ 127959243Sobrien if (!srccat(varval(STRhome), STRsldottcshrc)) 128059243Sobrien (void) srccat(varval(STRhome), STRsldotcshrc); 128159243Sobrien 128259243Sobrien if (!fast && !arginp && !onelflg && !havhash) 128359243Sobrien dohash(NULL,NULL); 128459243Sobrien 128559243Sobrien /* 128659243Sobrien * Source history before .login so that it is available in .login 128759243Sobrien */ 128859243Sobrien loadhist(NULL, 0); 128959243Sobrien#ifndef LOGINFIRST 129059243Sobrien if (loginsh) 129159243Sobrien (void) srccat(varval(STRhome), STRsldotlogin); 129259243Sobrien#endif 129359243Sobrien if (!fast && (loginsh || rdirs)) 129459243Sobrien loaddirs(NULL); 129559243Sobrien } 129659243Sobrien /* Initing AFTER .cshrc is the Right Way */ 129759243Sobrien if (intty && !arginp) { /* PWP setup stuff */ 129859243Sobrien ed_Init(); /* init the new line editor */ 129959243Sobrien#ifdef SIG_WINDOW 130059243Sobrien check_window_size(1); /* mung environment */ 130159243Sobrien#endif /* SIG_WINDOW */ 130259243Sobrien } 130359243Sobrien 130459243Sobrien /* 130559243Sobrien * Now are ready for the -v and -x flags 130659243Sobrien */ 130759243Sobrien if (nverbose) 130859243Sobrien setNS(STRverbose); 130959243Sobrien if (nexececho) 131059243Sobrien setNS(STRecho); 131159243Sobrien 131259243Sobrien /* 131359243Sobrien * All the rest of the world is inside this call. The argument to process 131459243Sobrien * indicates whether it should catch "error unwinds". Thus if we are a 131559243Sobrien * interactive shell our call here will never return by being blown past on 131659243Sobrien * an error. 131759243Sobrien */ 131859243Sobrien process(setintr); 131959243Sobrien 132059243Sobrien /* 132159243Sobrien * Mop-up. 132259243Sobrien */ 132359243Sobrien if (intty) { 132459243Sobrien if (loginsh) { 132559243Sobrien xprintf("logout\n"); 132659243Sobrien (void) close(SHIN); 132759243Sobrien child = 1; 132859243Sobrien#ifdef TESLA 132959243Sobrien do_logout = 1; 133059243Sobrien#endif /* TESLA */ 133159243Sobrien goodbye(NULL, NULL); 133259243Sobrien } 133359243Sobrien else { 133459243Sobrien xprintf("exit\n"); 133559243Sobrien } 133659243Sobrien } 133759243Sobrien record(); 133859243Sobrien exitstat(); 133959243Sobrien return (0); 134059243Sobrien} 134159243Sobrien 134259243Sobrienvoid 134359243Sobrienuntty() 134459243Sobrien{ 134559243Sobrien#ifdef BSDJOBS 134659243Sobrien if (tpgrp > 0 && opgrp != shpgrp) { 134759243Sobrien (void) setpgid(0, opgrp); 134859243Sobrien (void) tcsetpgrp(FSHTTY, opgrp); 134959243Sobrien (void) resetdisc(FSHTTY); 135059243Sobrien } 135159243Sobrien#endif /* BSDJOBS */ 135259243Sobrien} 135359243Sobrien 135459243Sobrienvoid 135559243Sobrienimportpath(cp) 135659243Sobrien Char *cp; 135759243Sobrien{ 135859243Sobrien register int i = 0; 135959243Sobrien register Char *dp; 136059243Sobrien register Char **pv; 136159243Sobrien int c; 136259243Sobrien 136359243Sobrien for (dp = cp; *dp; dp++) 136459243Sobrien if (*dp == PATHSEP) 136559243Sobrien i++; 136659243Sobrien /* 136759243Sobrien * i+2 where i is the number of colons in the path. There are i+1 136859243Sobrien * directories in the path plus we need room for a zero terminator. 136959243Sobrien */ 137059243Sobrien pv = (Char **) xcalloc((size_t) (i + 2), sizeof(Char *)); 137159243Sobrien dp = cp; 137259243Sobrien i = 0; 137359243Sobrien if (*dp) 137459243Sobrien for (;;) { 137559243Sobrien if ((c = *dp) == PATHSEP || c == 0) { 137659243Sobrien *dp = 0; 137759243Sobrien pv[i++] = Strsave(*cp ? cp : STRdot); 137859243Sobrien if (c) { 137959243Sobrien cp = dp + 1; 138059243Sobrien *dp = PATHSEP; 138159243Sobrien } 138259243Sobrien else 138359243Sobrien break; 138459243Sobrien } 138569408Sache#ifdef WINNT_NATIVE 138659243Sobrien else if (*dp == '\\') 138759243Sobrien *dp = '/'; 138869408Sache#endif /* WINNT_NATIVE */ 138959243Sobrien dp++; 139059243Sobrien } 139159243Sobrien pv[i] = 0; 139259243Sobrien setq(STRpath, pv, &shvhed, VAR_READWRITE); 139359243Sobrien} 139459243Sobrien 139559243Sobrien/* 139659243Sobrien * Source to the file which is the catenation of the argument names. 139759243Sobrien */ 139859243Sobrienstatic int 139959243Sobriensrccat(cp, dp) 140059243Sobrien Char *cp, *dp; 140159243Sobrien{ 140259243Sobrien if (cp[0] == '/' && cp[1] == '\0') 140359243Sobrien return srcfile(short2str(dp), (mflag ? 0 : 1), 0, NULL); 140459243Sobrien else { 140559243Sobrien register Char *ep; 140659243Sobrien char *ptr; 140759243Sobrien int rv; 140859243Sobrien 140969408Sache#ifdef WINNT_NATIVE 141059243Sobrien ep = cp; 141159243Sobrien while(*ep) 141259243Sobrien ep++; 141359243Sobrien if (ep[-1] == '/' && dp[0] == '/') /* silly win95 */ 141459243Sobrien dp++; 141569408Sache#endif /* WINNT_NATIVE */ 141659243Sobrien 141759243Sobrien ep = Strspl(cp, dp); 141859243Sobrien ptr = short2str(ep); 141959243Sobrien 142059243Sobrien rv = srcfile(ptr, (mflag ? 0 : 1), 0, NULL); 142159243Sobrien xfree((ptr_t) ep); 142259243Sobrien return rv; 142359243Sobrien } 142459243Sobrien} 142559243Sobrien 142659243Sobrien/* 142759243Sobrien * Source to a file putting the file descriptor in a safe place (> 2). 142859243Sobrien */ 142959243Sobrienstatic int 143059243Sobriensrcfile(f, onlyown, flag, av) 143159243Sobrien char *f; 143259243Sobrien bool onlyown; 143359243Sobrien int flag; 143459243Sobrien Char **av; 143559243Sobrien{ 143659243Sobrien register int unit; 143759243Sobrien 143859243Sobrien if ((unit = open(f, O_RDONLY)) == -1) 143959243Sobrien return 0; 144059243Sobrien unit = dmove(unit, -1); 144159243Sobrien 144259243Sobrien (void) close_on_exec(unit, 1); 144359243Sobrien srcunit(unit, onlyown, flag, av); 144459243Sobrien return 1; 144559243Sobrien} 144659243Sobrien 144759243Sobrien 144859243Sobrien/* 144959243Sobrien * Save the shell state, and establish new argument vector, and new input 145059243Sobrien * fd. 145159243Sobrien */ 145259243Sobrienstatic void 145359243Sobrienst_save(st, unit, hflg, al, av) 145459243Sobrien struct saved_state *st; 145559243Sobrien int unit, hflg; 145659243Sobrien Char **al, **av; 145759243Sobrien{ 145859243Sobrien st->insource = insource; 145959243Sobrien st->SHIN = SHIN; 146059243Sobrien st->intty = intty; 146159243Sobrien st->whyles = whyles; 146259243Sobrien st->gointr = gointr; 146359243Sobrien st->arginp = arginp; 146459243Sobrien st->evalp = evalp; 146559243Sobrien st->evalvec = evalvec; 146659243Sobrien st->alvecp = alvecp; 146759243Sobrien st->alvec = alvec; 146859243Sobrien st->onelflg = onelflg; 146959243Sobrien st->enterhist = enterhist; 147059243Sobrien if (hflg) 147159243Sobrien st->HIST = HIST; 147259243Sobrien else 147359243Sobrien st->HIST = '\0'; 147459243Sobrien st->cantell = cantell; 147559243Sobrien cpybin(st->B, B); 147659243Sobrien 147759243Sobrien /* 147859243Sobrien * we can now pass arguments to source. 147959243Sobrien * For compatibility we do that only if arguments were really 148059243Sobrien * passed, otherwise we keep the old, global $argv like before. 148159243Sobrien */ 148259243Sobrien if (av != NULL && *av != NULL) { 148359243Sobrien struct varent *vp; 148459243Sobrien if ((vp = adrof(STRargv)) != NULL) 148559243Sobrien st->argv = saveblk(vp->vec); 148659243Sobrien else 148759243Sobrien st->argv = NULL; 148859243Sobrien setq(STRargv, saveblk(av), &shvhed, VAR_READWRITE); 148959243Sobrien } 149059243Sobrien else 149159243Sobrien st->argv = NULL; 149259243Sobrien 149359243Sobrien SHIN = unit; /* Do this first */ 149459243Sobrien 149559243Sobrien /* Establish new input arena */ 149659243Sobrien { 149759243Sobrien fbuf = NULL; 149859243Sobrien fseekp = feobp = fblocks = 0; 149959243Sobrien settell(); 150059243Sobrien } 150159243Sobrien 150259243Sobrien arginp = 0; 150359243Sobrien onelflg = 0; 150459243Sobrien intty = isatty(SHIN); 150559243Sobrien whyles = 0; 150659243Sobrien gointr = 0; 150759243Sobrien evalvec = 0; 150859243Sobrien evalp = 0; 150959243Sobrien alvec = al; 151059243Sobrien alvecp = 0; 151159243Sobrien enterhist = hflg; 151259243Sobrien if (enterhist) 151359243Sobrien HIST = '\0'; 151459243Sobrien insource = 1; 151559243Sobrien} 151659243Sobrien 151759243Sobrien 151859243Sobrien/* 151959243Sobrien * Restore the shell to a saved state 152059243Sobrien */ 152159243Sobrienstatic void 152259243Sobrienst_restore(st, av) 152359243Sobrien struct saved_state *st; 152459243Sobrien Char **av; 152559243Sobrien{ 152659243Sobrien if (st->SHIN == -1) 152759243Sobrien return; 152859243Sobrien 152959243Sobrien /* Reset input arena */ 153059243Sobrien { 153159243Sobrien register int i; 153259243Sobrien register Char** nfbuf = fbuf; 153359243Sobrien register int nfblocks = fblocks; 153459243Sobrien 153559243Sobrien fblocks = 0; 153659243Sobrien fbuf = NULL; 153759243Sobrien for (i = 0; i < nfblocks; i++) 153859243Sobrien xfree((ptr_t) nfbuf[i]); 153959243Sobrien xfree((ptr_t) nfbuf); 154059243Sobrien } 154159243Sobrien cpybin(B, st->B); 154259243Sobrien 154359243Sobrien (void) close(SHIN); 154459243Sobrien 154559243Sobrien insource = st->insource; 154659243Sobrien SHIN = st->SHIN; 154759243Sobrien arginp = st->arginp; 154859243Sobrien onelflg = st->onelflg; 154959243Sobrien evalp = st->evalp; 155059243Sobrien evalvec = st->evalvec; 155159243Sobrien alvecp = st->alvecp; 155259243Sobrien alvec = st->alvec; 155359243Sobrien intty = st->intty; 155459243Sobrien whyles = st->whyles; 155559243Sobrien gointr = st->gointr; 155659243Sobrien if (st->HIST != '\0') 155759243Sobrien HIST = st->HIST; 155859243Sobrien enterhist = st->enterhist; 155959243Sobrien cantell = st->cantell; 156059243Sobrien 156159243Sobrien if (st->argv != NULL) 156259243Sobrien setq(STRargv, st->argv, &shvhed, VAR_READWRITE); 156359243Sobrien else if (av != NULL && *av != NULL && adrof(STRargv) != NULL) 156459243Sobrien unsetv(STRargv); 156559243Sobrien} 156659243Sobrien 156759243Sobrien/* 156859243Sobrien * Source to a unit. If onlyown it must be our file or our group or 156959243Sobrien * we don't chance it. This occurs on ".cshrc"s and the like. 157059243Sobrien */ 157159243Sobrienstatic void 157259243Sobriensrcunit(unit, onlyown, hflg, av) 157359243Sobrien register int unit; 157459243Sobrien bool onlyown; 157559243Sobrien int hflg; 157659243Sobrien Char **av; 157759243Sobrien{ 157859243Sobrien struct saved_state st; 157959243Sobrien st.SHIN = -1; /* st_restore checks this */ 158059243Sobrien 158159243Sobrien if (unit < 0) 158259243Sobrien return; 158359243Sobrien 158459243Sobrien if (didfds) 158559243Sobrien donefds(); 158659243Sobrien 158759243Sobrien if (onlyown) { 158859243Sobrien struct stat stb; 158959243Sobrien 159059243Sobrien if (fstat(unit, &stb) < 0) { 159159243Sobrien (void) close(unit); 159259243Sobrien return; 159359243Sobrien } 159459243Sobrien } 159559243Sobrien 159659243Sobrien getexit(st.oldexit); 159759243Sobrien 159859243Sobrien if (setintr) 159959243Sobrien#ifdef BSDSIGS 160059243Sobrien st.mask = sigblock(sigmask(SIGINT)); 160159243Sobrien#else 160259243Sobrien (void) sighold(SIGINT); 160359243Sobrien#endif 160459243Sobrien 160559243Sobrien /* Save the current state and move us to a new state */ 160659243Sobrien st_save(&st, unit, hflg, NULL, av); 160759243Sobrien 160859243Sobrien /* 160959243Sobrien * Now if we are allowing commands to be interrupted, we let ourselves be 161059243Sobrien * interrupted. 161159243Sobrien */ 161259243Sobrien if (setintr) 161359243Sobrien#ifdef BSDSIGS 161459243Sobrien (void) sigsetmask(st.mask); 161559243Sobrien#else 161659243Sobrien (void) sigrelse(SIGINT); 161759243Sobrien#endif 161859243Sobrien 161959243Sobrien /* 162059243Sobrien * Bugfix for running out of memory by: Jak Kirman 162159243Sobrien * <jak%cs.brown.edu@RELAY.CS.NET>. Solution: pay attention to what 162259243Sobrien * setexit() is returning because reenter _may_ be in a register, and 162359243Sobrien * thus restored to 0 on a longjump(). (PWP: insert flames about 162459243Sobrien * compiler-dependant code here) PWP: THANKS LOTS !!! 162559243Sobrien * 162659243Sobrien * PWP: think of this as like a LISP (unwind-protect ...) 162759243Sobrien * thanks to Diana Smetters for pointing out how this _should_ be written 162859243Sobrien */ 162959243Sobrien#ifdef cray 163059243Sobrien st.reenter = 1; /* assume non-zero return val */ 163159243Sobrien if (setexit() == 0) { 163259243Sobrien st.reenter = 0; /* Oh well, we were wrong */ 163359243Sobrien#else 163459243Sobrien if ((st.reenter = setexit()) == 0) { 163559243Sobrien#endif 163659243Sobrien process(0); /* 0 -> blow away on errors */ 163759243Sobrien } 163859243Sobrien 163959243Sobrien if (setintr) 164059243Sobrien#ifdef BSDSIGS 164159243Sobrien (void) sigsetmask(st.mask); 164259243Sobrien#else 164359243Sobrien (void) sigrelse(SIGINT); 164459243Sobrien#endif 164559243Sobrien 164659243Sobrien /* Restore the old state */ 164759243Sobrien st_restore(&st, av); 164859243Sobrien resexit(st.oldexit); 164959243Sobrien /* 165059243Sobrien * If process reset() (effectively an unwind) then we must also unwind. 165159243Sobrien */ 165259243Sobrien if (st.reenter) 165359243Sobrien stderror(ERR_SILENT); 165459243Sobrien} 165559243Sobrien 165659243Sobrien 165759243Sobrien/*ARGSUSED*/ 165859243Sobrienvoid 165959243Sobriengoodbye(v, c) 166059243Sobrien Char **v; 166159243Sobrien struct command *c; 166259243Sobrien{ 166359243Sobrien USE(c); 166459243Sobrien record(); 166559243Sobrien 166659243Sobrien if (loginsh) { 166759243Sobrien (void) sigset(SIGQUIT, SIG_IGN); 166859243Sobrien (void) sigset(SIGINT, SIG_IGN); 166959243Sobrien (void) sigset(SIGTERM, SIG_IGN); 167059243Sobrien (void) sigset(SIGHUP, SIG_IGN); 167159243Sobrien setintr = 0; /* No interrupts after "logout" */ 167259243Sobrien /* Trap errors inside .logout */ 167359243Sobrien reenter = setexit(); 167459243Sobrien if (reenter != 0) 167559243Sobrien exitstat(); 167659243Sobrien if (!(adrof(STRlogout))) 167759243Sobrien set(STRlogout, Strsave(STRnormal), VAR_READWRITE); 167859243Sobrien#ifdef _PATH_DOTLOGOUT 167959243Sobrien (void) srcfile(_PATH_DOTLOGOUT, 0, 0, NULL); 168059243Sobrien#endif 168159243Sobrien if (adrof(STRhome)) 168259243Sobrien (void) srccat(varval(STRhome), STRsldtlogout); 168359243Sobrien#ifdef TESLA 168459243Sobrien do_logout = 1; 168559243Sobrien#endif /* TESLA */ 168659243Sobrien } 168759243Sobrien exitstat(); 168859243Sobrien} 168959243Sobrien 169059243Sobrienvoid 169159243Sobrienexitstat() 169259243Sobrien{ 169359243Sobrien#ifdef PROF 169459243Sobrien monitor(0); 169559243Sobrien#endif 169659243Sobrien /* 169759243Sobrien * Note that if STATUS is corrupted (i.e. getn bombs) then error will exit 169859243Sobrien * directly because we poke child here. Otherwise we might continue 169959243Sobrien * unwarrantedly (sic). 170059243Sobrien */ 170159243Sobrien child = 1; 170259243Sobrien 170359243Sobrien xexit(getn(varval(STRstatus))); 170459243Sobrien} 170559243Sobrien 170659243Sobrien/* 170759243Sobrien * in the event of a HUP we want to save the history 170859243Sobrien */ 170959243Sobrienstatic sigret_t 171059243Sobrienphup(snum) 171159243Sobrienint snum; 171259243Sobrien{ 171359243Sobrien /* 171459243Sobrien * There is no return from here, 171559243Sobrien * so we are not going to release SIGHUP 171659243Sobrien * anymore 171759243Sobrien */ 171859243Sobrien#ifdef UNRELSIGS 171959243Sobrien if (snum) 172059243Sobrien (void) sigset(snum, SIG_IGN); 172159243Sobrien#else 172259243Sobrien# ifdef BSDSIGS 172359243Sobrien (void) sigblock(sigmask(SIGHUP)); 172459243Sobrien# else 172559243Sobrien (void) sighold(SIGHUP); 172659243Sobrien# endif /* BSDSIGS */ 172759243Sobrien#endif /* UNRELSIGS */ 172859243Sobrien 172959243Sobrien if (loginsh) { 173059243Sobrien set(STRlogout, Strsave(STRhangup), VAR_READWRITE); 173159243Sobrien#ifdef _PATH_DOTLOGOUT 173259243Sobrien (void) srcfile(_PATH_DOTLOGOUT, 0, 0, NULL); 173359243Sobrien#endif 173459243Sobrien if (adrof(STRhome)) 173559243Sobrien (void) srccat(varval(STRhome), STRsldtlogout); 173659243Sobrien } 173759243Sobrien 173859243Sobrien record(); 173959243Sobrien 174059243Sobrien#ifdef POSIXJOBS 174159243Sobrien /* 174259243Sobrien * We kill the last foreground process group. It then becomes 174359243Sobrien * responsible to propagate the SIGHUP to its progeny. 174459243Sobrien */ 174559243Sobrien { 174659243Sobrien struct process *pp, *np; 174759243Sobrien 174859243Sobrien for (pp = proclist.p_next; pp; pp = pp->p_next) { 174959243Sobrien np = pp; 175059243Sobrien /* 175159243Sobrien * Find if this job is in the foreground. It could be that 175259243Sobrien * the process leader has exited and the foreground flag 175359243Sobrien * is cleared for it. 175459243Sobrien */ 175559243Sobrien do 175659243Sobrien /* 175759243Sobrien * If a process is in the foreground we try to kill 175859243Sobrien * it's process group. If we succeed, then the 175959243Sobrien * whole job is gone. Otherwise we keep going... 176059243Sobrien * But avoid sending HUP to the shell again. 176159243Sobrien */ 176259243Sobrien if (((np->p_flags & PFOREGND) != 0) && np->p_jobid != shpgrp) { 176359243Sobrien np->p_flags &= ~PHUP; 176459243Sobrien if (killpg(np->p_jobid, SIGHUP) != -1) { 176559243Sobrien /* In case the job was suspended... */ 176659243Sobrien#ifdef SIGCONT 176759243Sobrien (void) killpg(np->p_jobid, SIGCONT); 176859243Sobrien#endif 176959243Sobrien break; 177059243Sobrien } 177159243Sobrien } 177259243Sobrien while ((np = np->p_friends) != pp); 177359243Sobrien } 177459243Sobrien } 177559243Sobrien#endif /* POSIXJOBS */ 177659243Sobrien 177759243Sobrien xexit(snum); 177859243Sobrien#ifndef SIGVOID 177959243Sobrien return (snum); 178059243Sobrien#endif 178159243Sobrien} 178259243Sobrien 178359243Sobrienstatic Char *jobargv[2] = {STRjobs, 0}; 178459243Sobrien 178559243Sobrien/* 178659243Sobrien * Catch an interrupt, e.g. during lexical input. 178759243Sobrien * If we are an interactive shell, we reset the interrupt catch 178859243Sobrien * immediately. In any case we drain the shell output, 178959243Sobrien * and finally go through the normal error mechanism, which 179059243Sobrien * gets a chance to make the shell go away. 179159243Sobrien */ 179259243Sobrienint just_signaled; /* bugfix by Michael Bloom (mg@ttidca.TTI.COM) */ 179359243Sobrien 179459243Sobrien#ifdef SIGVOID 179559243Sobrien/*ARGSUSED*/ 179659243Sobrien#endif 179759243Sobriensigret_t 179859243Sobrienpintr(snum) 179959243Sobrienint snum; 180059243Sobrien{ 180159243Sobrien#ifdef UNRELSIGS 180259243Sobrien if (snum) 180359243Sobrien (void) sigset(snum, pintr); 180459243Sobrien#endif /* UNRELSIGS */ 180559243Sobrien just_signaled = 1; 180659243Sobrien pintr1(1); 180759243Sobrien#ifndef SIGVOID 180859243Sobrien return (snum); 180959243Sobrien#endif 181059243Sobrien} 181159243Sobrien 181259243Sobrienvoid 181359243Sobrienpintr1(wantnl) 181459243Sobrien bool wantnl; 181559243Sobrien{ 181659243Sobrien register Char **v; 181759243Sobrien#ifdef BSDSIGS 181859243Sobrien sigmask_t omask; 181959243Sobrien#endif 182059243Sobrien 182159243Sobrien#ifdef BSDSIGS 182259243Sobrien omask = sigblock((sigmask_t) 0); 182359243Sobrien#endif 182459243Sobrien if (setintr) { 182559243Sobrien#ifdef BSDSIGS 182659243Sobrien (void) sigsetmask(omask & ~sigmask(SIGINT)); 182759243Sobrien#else 182859243Sobrien (void) sigrelse(SIGINT); 182959243Sobrien#endif 183059243Sobrien if (pjobs) { 183159243Sobrien pjobs = 0; 183259243Sobrien xputchar('\n'); 183359243Sobrien dojobs(jobargv, NULL); 183459243Sobrien stderror(ERR_NAME | ERR_INTR); 183559243Sobrien } 183659243Sobrien } 183759243Sobrien /* MH - handle interrupted completions specially */ 183859243Sobrien { 183959243Sobrien extern int InsideCompletion; 184059243Sobrien 184159243Sobrien if (InsideCompletion) 184259243Sobrien stderror(ERR_SILENT); 184359243Sobrien } 184459243Sobrien /* JV - Make sure we shut off inputl */ 184559243Sobrien { 184659243Sobrien extern Char GettingInput; 184759243Sobrien 184859243Sobrien (void) Cookedmode(); 184959243Sobrien GettingInput = 0; 185059243Sobrien } 185159243Sobrien#ifdef BSDSIGS 185259243Sobrien (void) sigsetmask(omask & ~sigmask(SIGCHLD)); 185359243Sobrien#else 185459243Sobrien if (setintr) 185559243Sobrien (void) sighold(SIGINT); 185659243Sobrien (void) sigrelse(SIGCHLD); 185759243Sobrien#endif 185859243Sobrien drainoline(); 185969408Sache#if !defined(_VMS_POSIX) && !defined(WINNT_NATIVE) 186059243Sobrien (void) endpwent(); 186169408Sache#endif /* !_VMS_POSIX && !WINNT_NATIVE */ 186259243Sobrien 186359243Sobrien /* 186459243Sobrien * If we have an active "onintr" then we search for the label. Note that if 186559243Sobrien * one does "onintr -" then we shan't be interruptible so we needn't worry 186659243Sobrien * about that here. 186759243Sobrien */ 186859243Sobrien if (gointr) { 186959243Sobrien gotolab(gointr); 187059243Sobrien timflg = 0; 187159243Sobrien if ((v = pargv) != 0) 187259243Sobrien pargv = 0, blkfree(v); 187359243Sobrien if ((v = gargv) != 0) 187459243Sobrien gargv = 0, blkfree(v); 187559243Sobrien reset(); 187659243Sobrien } 187759243Sobrien else if (intty && wantnl) { 187859243Sobrien if (editing) { 187959243Sobrien /* 188059243Sobrien * If we are editing a multi-line input command, and move to 188159243Sobrien * the beginning of the line, we don't want to trash it when 188259243Sobrien * we hit ^C 188359243Sobrien */ 188459243Sobrien PastBottom(); 188559243Sobrien ClearLines(); 188659243Sobrien ClearDisp(); 188759243Sobrien } 188859243Sobrien else { 188959243Sobrien /* xputchar('\n'); *//* Some like this, others don't */ 189059243Sobrien (void) putraw('\r'); 189159243Sobrien (void) putraw('\n'); 189259243Sobrien } 189359243Sobrien } 189459243Sobrien stderror(ERR_SILENT); 189559243Sobrien} 189659243Sobrien 189759243Sobrien/* 189859243Sobrien * Process is the main driving routine for the shell. 189959243Sobrien * It runs all command processing, except for those within { ... } 190059243Sobrien * in expressions (which is run by a routine evalav in sh.exp.c which 190159243Sobrien * is a stripped down process), and `...` evaluation which is run 190259243Sobrien * also by a subset of this code in sh.glob.c in the routine backeval. 190359243Sobrien * 190459243Sobrien * The code here is a little strange because part of it is interruptible 190559243Sobrien * and hence freeing of structures appears to occur when none is necessary 190659243Sobrien * if this is ignored. 190759243Sobrien * 190859243Sobrien * Note that if catch is not set then we will unwind on any error. 190959243Sobrien * If an end-of-file occurs, we return. 191059243Sobrien */ 191159243Sobrienstatic struct command *savet = NULL; 191259243Sobrienvoid 191359243Sobrienprocess(catch) 191459243Sobrien bool catch; 191559243Sobrien{ 191659243Sobrien extern char Expand; 191759243Sobrien jmp_buf_t osetexit; 191859243Sobrien /* PWP: This might get nuked my longjmp so don't make it a register var */ 191959243Sobrien struct command *t = savet; 192059243Sobrien 192159243Sobrien savet = NULL; 192259243Sobrien getexit(osetexit); 192359243Sobrien for (;;) { 192459243Sobrien 192559243Sobrien pendjob(); 192659243Sobrien 192759243Sobrien /* This was leaking memory badly, particularly when sourcing 192859243Sobrien * files, etc.. For whatever reason we were arriving here with 192959243Sobrien * allocated pointers still active, and the code was simply 193059243Sobrien * overwriting them. I can't say I fully understand the 193159243Sobrien * control flow here, but according to Purify this particular 193259243Sobrien * leak has been plugged, and I haven't noticed any ill 193359243Sobrien * effects.. (sg) 193459243Sobrien */ 193559243Sobrien if (paraml.next && paraml.next != ¶ml) 193659243Sobrien freelex(¶ml); 193759243Sobrien 193859243Sobrien paraml.next = paraml.prev = ¶ml; 193959243Sobrien paraml.word = STRNULL; 194059243Sobrien (void) setexit(); 194159243Sobrien justpr = enterhist; /* execute if not entering history */ 194259243Sobrien 194359243Sobrien /* 194459243Sobrien * Interruptible during interactive reads 194559243Sobrien */ 194659243Sobrien if (setintr) 194759243Sobrien#ifdef BSDSIGS 194859243Sobrien (void) sigsetmask(sigblock((sigmask_t) 0) & ~sigmask(SIGINT)); 194959243Sobrien#else 195059243Sobrien (void) sigrelse(SIGINT); 195159243Sobrien#endif 195259243Sobrien 195359243Sobrien 195459243Sobrien /* 195559243Sobrien * For the sake of reset() 195659243Sobrien */ 195759243Sobrien freelex(¶ml); 195859243Sobrien if (savet) 195959243Sobrien freesyn(savet), savet = NULL; 196059243Sobrien 196159243Sobrien if (haderr) { 196259243Sobrien if (!catch) { 196359243Sobrien /* unwind */ 196459243Sobrien doneinp = 0; 196559243Sobrien savet = t; 196659243Sobrien resexit(osetexit); 196759243Sobrien reset(); 196859243Sobrien } 196959243Sobrien haderr = 0; 197059243Sobrien /* 197159243Sobrien * Every error is eventually caught here or the shell dies. It is 197259243Sobrien * at this point that we clean up any left-over open files, by 197359243Sobrien * closing all but a fixed number of pre-defined files. Thus 197459243Sobrien * routines don't have to worry about leaving files open due to 197559243Sobrien * deeper errors... they will get closed here. 197659243Sobrien */ 197759243Sobrien closem(); 197859243Sobrien continue; 197959243Sobrien } 198059243Sobrien if (doneinp) { 198159243Sobrien doneinp = 0; 198259243Sobrien break; 198359243Sobrien } 198459243Sobrien if (chkstop) 198559243Sobrien chkstop--; 198659243Sobrien if (neednote) 198759243Sobrien pnote(); 198859243Sobrien if (intty && prompt && evalvec == 0) { 198959243Sobrien just_signaled = 0; 199059243Sobrien mailchk(); 199159243Sobrien /* 199259243Sobrien * Watch for logins/logouts. Next is scheduled commands stored 199359243Sobrien * previously using "sched." Then execute periodic commands. 199459243Sobrien * Following that, the prompt precmd is run. 199559243Sobrien */ 199659243Sobrien#ifndef HAVENOUTMP 199759243Sobrien watch_login(0); 199859243Sobrien#endif /* !HAVENOUTMP */ 199959243Sobrien sched_run(0); 200059243Sobrien period_cmd(); 200159243Sobrien precmd(); 200259243Sobrien /* 200359243Sobrien * If we are at the end of the input buffer then we are going to 200459243Sobrien * read fresh stuff. Otherwise, we are rereading input and don't 200559243Sobrien * need or want to prompt. 200659243Sobrien */ 200769408Sache if (fseekp == feobp && aret == TCSH_F_SEEK) 200859243Sobrien printprompt(0, NULL); 200959243Sobrien flush(); 201059243Sobrien setalarm(1); 201159243Sobrien } 201259243Sobrien if (seterr) { 201359243Sobrien xfree((ptr_t) seterr); 201459243Sobrien seterr = NULL; 201559243Sobrien } 201659243Sobrien 201759243Sobrien /* 201859243Sobrien * Echo not only on VERBOSE, but also with history expansion. If there 201959243Sobrien * is a lexical error then we forego history echo. 202059243Sobrien */ 202159243Sobrien if ((lex(¶ml) && !seterr && intty && !tellwhat && !Expand && 202259243Sobrien !whyles) || adrof(STRverbose)) { 202359243Sobrien haderr = 1; 202459243Sobrien prlex(¶ml); 202559243Sobrien haderr = 0; 202659243Sobrien } 202759243Sobrien (void) alarm(0); /* Autologout OFF */ 202859243Sobrien 202959243Sobrien /* 203059243Sobrien * The parser may lose space if interrupted. 203159243Sobrien */ 203259243Sobrien if (setintr) 203359243Sobrien#ifdef BSDSIGS 203459243Sobrien (void) sigblock(sigmask(SIGINT)); 203559243Sobrien#else 203659243Sobrien (void) sighold(SIGINT); 203759243Sobrien#endif 203859243Sobrien 203959243Sobrien /* 204059243Sobrien * Save input text on the history list if reading in old history, or it 204159243Sobrien * is from the terminal at the top level and not in a loop. 204259243Sobrien * 204359243Sobrien * PWP: entry of items in the history list while in a while loop is done 204459243Sobrien * elsewhere... 204559243Sobrien */ 204659243Sobrien if (enterhist || (catch && intty && !whyles && !tellwhat && !arun)) 204759243Sobrien savehist(¶ml, enterhist > 1); 204859243Sobrien 204959243Sobrien if (Expand && seterr) 205059243Sobrien Expand = 0; 205159243Sobrien 205259243Sobrien /* 205359243Sobrien * Print lexical error messages, except when sourcing history lists. 205459243Sobrien */ 205559243Sobrien if (!enterhist && seterr) 205659243Sobrien stderror(ERR_OLD); 205759243Sobrien 205859243Sobrien /* 205959243Sobrien * If had a history command :p modifier then this is as far as we 206059243Sobrien * should go 206159243Sobrien */ 206259243Sobrien if (justpr) 206359243Sobrien reset(); 206459243Sobrien 206559243Sobrien /* 206659243Sobrien * If had a tellwhat from twenex() then do 206759243Sobrien */ 206859243Sobrien if (tellwhat) { 206959243Sobrien (void) tellmewhat(¶ml, NULL); 207059243Sobrien reset(); 207159243Sobrien } 207259243Sobrien 207359243Sobrien alias(¶ml); 207459243Sobrien 207559243Sobrien#ifdef BSDJOBS 207659243Sobrien /* 207759243Sobrien * If we are interactive, try to continue jobs that we have stopped 207859243Sobrien */ 207959243Sobrien if (prompt) 208059243Sobrien continue_jobs(¶ml); 208159243Sobrien#endif /* BSDJOBS */ 208259243Sobrien 208359243Sobrien /* 208459243Sobrien * Check to see if the user typed "rm * .o" or something 208559243Sobrien */ 208659243Sobrien if (prompt) 208759243Sobrien rmstar(¶ml); 208859243Sobrien /* 208959243Sobrien * Parse the words of the input into a parse tree. 209059243Sobrien */ 209159243Sobrien savet = syntax(paraml.next, ¶ml, 0); 209259243Sobrien if (seterr) 209359243Sobrien stderror(ERR_OLD); 209459243Sobrien 209559243Sobrien postcmd(); 209659243Sobrien /* 209759243Sobrien * Execute the parse tree From: Michael Schroeder 209859243Sobrien * <mlschroe@immd4.informatik.uni-erlangen.de> was execute(t, tpgrp); 209959243Sobrien */ 210059243Sobrien execute(savet, (tpgrp > 0 ? tpgrp : -1), NULL, NULL); 210159243Sobrien 210259243Sobrien /* 210359243Sobrien * Made it! 210459243Sobrien */ 210559243Sobrien freelex(¶ml); 210659243Sobrien freesyn(savet), savet = NULL; 210759243Sobrien#ifdef SIG_WINDOW 210859243Sobrien if (catch && intty && !whyles && !tellwhat) 210959243Sobrien (void) window_change(0); /* for window systems */ 211059243Sobrien#endif /* SIG_WINDOW */ 211159243Sobrien set(STR_, Strsave(InputBuf), VAR_READWRITE | VAR_NOGLOB); 211259243Sobrien } 211359243Sobrien savet = t; 211459243Sobrien resexit(osetexit); 211559243Sobrien} 211659243Sobrien 211759243Sobrien/*ARGSUSED*/ 211859243Sobrienvoid 211959243Sobriendosource(t, c) 212059243Sobrien register Char **t; 212159243Sobrien struct command *c; 212259243Sobrien{ 212359243Sobrien register Char *f; 212459243Sobrien bool hflg = 0; 212559243Sobrien extern int bequiet; 212659243Sobrien char buf[BUFSIZE]; 212759243Sobrien 212859243Sobrien USE(c); 212959243Sobrien t++; 213059243Sobrien if (*t && eq(*t, STRmh)) { 213159243Sobrien if (*++t == NULL) 213259243Sobrien stderror(ERR_NAME | ERR_HFLAG); 213359243Sobrien hflg++; 213459243Sobrien } 213559243Sobrien else if (*t && eq(*t, STRmm)) { 213659243Sobrien if (*++t == NULL) 213759243Sobrien stderror(ERR_NAME | ERR_MFLAG); 213859243Sobrien hflg = 2; 213959243Sobrien } 214059243Sobrien 214159243Sobrien f = globone(*t++, G_ERROR); 214259243Sobrien (void) strcpy(buf, short2str(f)); 214359243Sobrien xfree((ptr_t) f); 214459243Sobrien if ((!srcfile(buf, 0, hflg, t)) && (!hflg) && (!bequiet)) 214559243Sobrien stderror(ERR_SYSTEM, buf, strerror(errno)); 214659243Sobrien} 214759243Sobrien 214859243Sobrien/* 214959243Sobrien * Check for mail. 215059243Sobrien * If we are a login shell, then we don't want to tell 215159243Sobrien * about any mail file unless its been modified 215259243Sobrien * after the time we started. 215359243Sobrien * This prevents us from telling the user things he already 215459243Sobrien * knows, since the login program insists on saying 215559243Sobrien * "You have mail." 215659243Sobrien */ 215759243Sobrien 215859243Sobrien/* 215959243Sobrien * The AMS version. 216059243Sobrien * This version checks if the file is a directory, and if so, 216159243Sobrien * tells you the number of files in it, otherwise do the old thang. 216259243Sobrien * The magic "+1" in the time calculation is to compensate for 216359243Sobrien * an AFS bug where directory mtimes are set to 1 second in 216459243Sobrien * the future. 216559243Sobrien */ 216659243Sobrien 216759243Sobrienstatic void 216859243Sobrienmailchk() 216959243Sobrien{ 217059243Sobrien register struct varent *v; 217159243Sobrien register Char **vp; 217259243Sobrien time_t t; 217359243Sobrien int intvl, cnt; 217459243Sobrien struct stat stb; 217559243Sobrien bool new; 217659243Sobrien 217759243Sobrien v = adrof(STRmail); 217859243Sobrien if (v == 0) 217959243Sobrien return; 218059243Sobrien (void) time(&t); 218159243Sobrien vp = v->vec; 218259243Sobrien cnt = blklen(vp); 218359243Sobrien intvl = (cnt && number(*vp)) ? (--cnt, getn(*vp++)) : MAILINTVL; 218459243Sobrien if (intvl < 1) 218559243Sobrien intvl = 1; 218659243Sobrien if (chktim + intvl > t) 218759243Sobrien return; 218859243Sobrien for (; *vp; vp++) { 218959243Sobrien char *filename = short2str(*vp); 219059243Sobrien char *mboxdir = filename; 219159243Sobrien 219259243Sobrien if (stat(filename, &stb) < 0) 219359243Sobrien continue; 219459243Sobrien#if defined(BSDTIMES) || defined(_SEQUENT_) 219559243Sobrien new = stb.st_mtime > time0.tv_sec; 219659243Sobrien#else 219759415Sobrien new = stb.st_mtime > seconds0; 219859243Sobrien#endif 219959243Sobrien if (S_ISDIR(stb.st_mode)) { 220059243Sobrien DIR *mailbox; 220159243Sobrien int mailcount = 0; 220259243Sobrien char tempfilename[MAXPATHLEN]; 220359243Sobrien struct stat stc; 220459243Sobrien 220559243Sobrien xsnprintf(tempfilename, MAXPATHLEN, "%s/new", filename); 220659243Sobrien 220759243Sobrien if (stat(tempfilename, &stc) != -1 && S_ISDIR(stc.st_mode)) { 220859243Sobrien /* 220959243Sobrien * "filename/new" exists and is a directory; you are 221059243Sobrien * using Qmail. 221159243Sobrien */ 221259243Sobrien stb = stc; 221359243Sobrien#if defined(BSDTIMES) || defined(_SEQUENT_) 221459243Sobrien new = stb.st_mtime > time0.tv_sec; 221559243Sobrien#else 221659415Sobrien new = stb.st_mtime > seconds0; 221759243Sobrien#endif 221859243Sobrien mboxdir = tempfilename; 221959243Sobrien } 222059243Sobrien 222159243Sobrien if (stb.st_mtime <= chktim + 1 || (loginsh && !new)) 222259243Sobrien continue; 222359243Sobrien 222459243Sobrien if ((mailbox = opendir(mboxdir)) == NULL) 222559243Sobrien continue; 222659243Sobrien 222759243Sobrien /* skip . and .. */ 222859243Sobrien if (!readdir(mailbox) || !readdir(mailbox)) 222959243Sobrien continue; 223059243Sobrien 223159243Sobrien while (readdir(mailbox)) 223259243Sobrien mailcount++; 223359243Sobrien 223459243Sobrien if (mailcount == 0) 223559243Sobrien continue; 223659243Sobrien 223759243Sobrien if (cnt == 1) 223859243Sobrien xprintf(CGETS(11, 3, "You have %d mail messages.\n"), 223959243Sobrien mailcount); 224059243Sobrien else 224159243Sobrien xprintf(CGETS(11, 4, "You have %d mail messages in %s.\n"), 224259243Sobrien mailcount, filename); 224359243Sobrien } 224459243Sobrien else { 224559243Sobrien if (stb.st_size == 0 || stb.st_atime > stb.st_mtime || 224659243Sobrien (stb.st_atime <= chktim && stb.st_mtime <= chktim) || 224759243Sobrien (loginsh && !new)) 224859243Sobrien continue; 224959243Sobrien if (cnt == 1) 225059243Sobrien xprintf(CGETS(11, 5, "You have %smail.\n"), 225159243Sobrien new ? CGETS(11, 6, "new ") : ""); 225259243Sobrien else 225359243Sobrien xprintf(CGETS(11, 7, "You have %smail in %s.\n"), 225459243Sobrien new ? CGETS(11, 6, "new ") : "", filename); 225559243Sobrien } 225659243Sobrien } 225759243Sobrien chktim = t; 225859243Sobrien} 225959243Sobrien 226059243Sobrien/* 226159243Sobrien * Extract a home directory from the password file 226259243Sobrien * The argument points to a buffer where the name of the 226359243Sobrien * user whose home directory is sought is currently. 226459243Sobrien * We write the home directory of the user back there. 226559243Sobrien */ 226659243Sobrienint 226759243Sobriengethdir(home) 226859243Sobrien Char *home; 226959243Sobrien{ 227059243Sobrien Char *h; 227159243Sobrien 227259243Sobrien /* 227359243Sobrien * Is it us? 227459243Sobrien */ 227559243Sobrien if (*home == '\0') { 227659243Sobrien if ((h = varval(STRhome)) != STRNULL) { 227759243Sobrien (void) Strcpy(home, h); 227859243Sobrien return 0; 227959243Sobrien } 228059243Sobrien else 228159243Sobrien return 1; 228259243Sobrien } 228359243Sobrien 228459243Sobrien /* 228559243Sobrien * Look in the cache 228659243Sobrien */ 228759243Sobrien if ((h = gettilde(home)) == NULL) 228859243Sobrien return 1; 228959243Sobrien else { 229059243Sobrien (void) Strcpy(home, h); 229159243Sobrien return 0; 229259243Sobrien } 229359243Sobrien} 229459243Sobrien 229559243Sobrien/* 229659243Sobrien * Move the initial descriptors to their eventual 229759243Sobrien * resting places, closing all other units. 229859243Sobrien */ 229959243Sobrienvoid 230059243Sobrieninitdesc() 230159243Sobrien{ 230259243Sobrien#ifdef NLS_BUGS 230359243Sobrien#ifdef NLS_CATALOGS 230459243Sobrien (void)catclose(catd); 230559243Sobrien#endif /* NLS_CATALOGS */ 230659243Sobrien#endif /* NLS_BUGS */ 230759243Sobrien 230859243Sobrien 230959243Sobrien didfds = 0; /* 0, 1, 2 aren't set up */ 231059243Sobrien (void) close_on_exec(SHIN = dcopy(0, FSHIN), 1); 231159243Sobrien (void) close_on_exec(SHOUT = dcopy(1, FSHOUT), 1); 231259243Sobrien (void) close_on_exec(SHDIAG = dcopy(2, FSHDIAG), 1); 231359243Sobrien (void) close_on_exec(OLDSTD = dcopy(SHIN, FOLDSTD), 1); 231459243Sobrien#ifndef CLOSE_ON_EXEC 231559243Sobrien didcch = 0; /* Havent closed for child */ 231659243Sobrien#endif /* CLOSE_ON_EXEC */ 231759243Sobrien isdiagatty = isatty(SHDIAG); 231859243Sobrien isoutatty = isatty(SHOUT); 231959243Sobrien closem(); 232059243Sobrien#ifdef NLS_BUGS 232159243Sobrien#ifdef NLS_CATALOGS 232259243Sobrien nlsinit(); 232359243Sobrien#endif /* NLS_CATALOGS */ 232459243Sobrien#endif /* NLS_BUGS */ 232559243Sobrien} 232659243Sobrien 232759243Sobrien 232859243Sobrienvoid 232959243Sobrien#ifdef PROF 233059243Sobriendone(i) 233159243Sobrien#else 233259243Sobrienxexit(i) 233359243Sobrien#endif 233459243Sobrien int i; 233559243Sobrien{ 233659243Sobrien#ifdef TESLA 233759243Sobrien if (loginsh && do_logout) { 233859243Sobrien /* this is to send hangup signal to the develcon */ 233959243Sobrien /* we toggle DTR. clear dtr - sleep 1 - set dtr */ 234059243Sobrien /* ioctl will return ENOTTY for pty's but we ignore it */ 234159243Sobrien /* exitstat will run after disconnect */ 234259243Sobrien /* we sleep for 2 seconds to let things happen in */ 234359243Sobrien /* .logout and rechist() */ 234459243Sobrien#ifdef TIOCCDTR 234559243Sobrien (void) sleep(2); 234659243Sobrien (void) ioctl(FSHTTY, TIOCCDTR, NULL); 234759243Sobrien (void) sleep(1); 234859243Sobrien (void) ioctl(FSHTTY, TIOCSDTR, NULL); 234959243Sobrien#endif /* TIOCCDTR */ 235059243Sobrien } 235159243Sobrien#endif /* TESLA */ 235259243Sobrien 235359243Sobrien { 235459243Sobrien struct process *pp, *np; 235559243Sobrien 235659243Sobrien /* Kill all processes marked for hup'ing */ 235759243Sobrien for (pp = proclist.p_next; pp; pp = pp->p_next) { 235859243Sobrien np = pp; 235959243Sobrien do 236059243Sobrien if ((np->p_flags & PHUP) && np->p_jobid != shpgrp) { 236159243Sobrien if (killpg(np->p_jobid, SIGHUP) != -1) { 236259243Sobrien /* In case the job was suspended... */ 236359243Sobrien#ifdef SIGCONT 236459243Sobrien (void) killpg(np->p_jobid, SIGCONT); 236559243Sobrien#endif 236659243Sobrien break; 236759243Sobrien } 236859243Sobrien } 236959243Sobrien while ((np = np->p_friends) != pp); 237059243Sobrien } 237159243Sobrien } 237259243Sobrien untty(); 237359243Sobrien#ifdef NLS_CATALOGS 237459243Sobrien /* 237559243Sobrien * We need to call catclose, because SVR4 leaves symlinks behind otherwise 237659243Sobrien * in the catalog directories. We cannot close on a vforked() child, 237759243Sobrien * because messages will stop working on the parent too. 237859243Sobrien */ 237959243Sobrien if (child == 0) 238059243Sobrien (void) catclose(catd); 238159243Sobrien#endif /* NLS_CATALOGS */ 238269408Sache#ifdef WINNT_NATIVE 238359243Sobrien nt_cleanup(); 238469408Sache#endif /* WINNT_NATIVE */ 238559243Sobrien _exit(i); 238659243Sobrien} 238759243Sobrien 238859243Sobrien#ifndef _PATH_DEFPATH 238959243Sobrienstatic Char ** 239059243Sobriendefaultpath() 239159243Sobrien{ 239259243Sobrien char *ptr; 239359243Sobrien Char **blk, **blkp; 239459243Sobrien struct stat stb; 239559243Sobrien 239659243Sobrien blkp = blk = (Char **) xmalloc((size_t) sizeof(Char *) * 10); 239759243Sobrien 239859243Sobrien#ifndef NODOT 239959243Sobrien# ifndef DOTLAST 240059243Sobrien *blkp++ = Strsave(STRdot); 240159243Sobrien# endif 240259243Sobrien#endif 240359243Sobrien 240459243Sobrien#define DIRAPPEND(a) \ 240559243Sobrien if (stat(ptr = a, &stb) == 0 && S_ISDIR(stb.st_mode)) \ 240659243Sobrien *blkp++ = SAVE(ptr) 240759243Sobrien 240859243Sobrien#ifdef _PATH_LOCAL 240959243Sobrien DIRAPPEND(_PATH_LOCAL); 241059243Sobrien#endif 241159243Sobrien 241259243Sobrien#ifdef _PATH_USRUCB 241359243Sobrien DIRAPPEND(_PATH_USRUCB); 241459243Sobrien#endif 241559243Sobrien 241659243Sobrien#ifdef _PATH_USRBSD 241759243Sobrien DIRAPPEND(_PATH_USRBSD); 241859243Sobrien#endif 241959243Sobrien 242059243Sobrien#ifdef _PATH_BIN 242159243Sobrien DIRAPPEND(_PATH_BIN); 242259243Sobrien#endif 242359243Sobrien 242459243Sobrien#ifdef _PATH_USRBIN 242559243Sobrien DIRAPPEND(_PATH_USRBIN); 242659243Sobrien#endif 242759243Sobrien 242859243Sobrien#undef DIRAPPEND 242959243Sobrien 243059243Sobrien#ifndef NODOT 243159243Sobrien# ifdef DOTLAST 243259243Sobrien *blkp++ = Strsave(STRdot); 243359243Sobrien# endif 243459243Sobrien#endif 243559243Sobrien *blkp = NULL; 243659243Sobrien return (blk); 243759243Sobrien} 243859243Sobrien#endif 243959243Sobrien 244059243Sobrienstatic void 244159243Sobrienrecord() 244259243Sobrien{ 244359243Sobrien if (!fast) { 244459243Sobrien recdirs(NULL, adrof(STRsavedirs) != NULL); 244559243Sobrien rechist(NULL, adrof(STRsavehist) != NULL); 244659243Sobrien } 244759243Sobrien} 2448