1 /* $Header: /src/pub/tcsh/sh.c,v 3.105 2002/07/05 16:28:16 christos Exp $ */
3 * sh.c: Main shell routines
6 * Copyright (c) 1980, 1991 The Regents of the University of California.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #define EXTERN /* Intern */
38 "@(#) Copyright (c) 1991 The Regents of the University of California.\n\
39 All rights reserved.\n";
42 RCSID("$Id: sh.c,v 3.105 2002/07/05 16:28:16 christos Exp $")
48 extern bool MapsAreInited
;
49 extern bool NLSMapsAreInited
;
50 extern bool NoNLSRebind
;
55 * Bill Joy, UC Berkeley, California, USA
56 * October 1978, May 1980
58 * Jim Kulp, IIASA, Laxenburg, Austria
61 * Filename recognition added:
62 * Ken Greer, Ind. Consultant, Palo Alto CA
65 * Karl Kleinpaste, Computer Consoles, Inc.
66 * Added precmd, periodic/tperiod, prompt changes,
67 * directory stack hack, and login watch.
68 * Sometime March 1983 - Feb 1984.
70 * Added scheduled commands, including the "sched" command,
71 * plus the call to sched_run near the precmd et al
73 * Upgraded scheduled events for running events while
74 * sitting idle at command input.
76 * Paul Placeway, Ohio State
77 * added stuff for running with twenex/inputl 9 Oct 1984.
79 * ported to Apple Unix (TM) (OREO) 26 -- 29 Jun 1987
82 jmp_buf_t reslab INIT_ZERO_STRUCT
;
84 static const char tcshstr
[] = "tcsh";
86 static const char tcshstr_nt
[] = "tcsh.exe";
87 #endif /* WINNT_NATIVE */
89 signalfun_t parintr
= 0; /* Parents interrupt catch */
90 signalfun_t parterm
= 0; /* Parents terminate catch */
97 bool use_fork
= 0; /* use fork() instead of vfork()? */
100 * Magic pointer values. Used to specify other invalid conditions aside
104 Char
*INVPTR
= &INVCHAR
;
105 Char
**INVPPTR
= &INVPTR
;
107 static int nofile
= 0;
108 static bool reenter
= 0;
109 static bool nverbose
= 0;
110 static bool nexececho
= 0;
111 static bool quitit
= 0;
112 static bool rdirs
= 0;
114 static bool batch
= 0;
115 static bool mflag
= 0;
116 static bool prompt
= 1;
117 static int enterhist
= 0;
124 static time_t chktim
; /* Time mail last checked */
127 extern char **environ
;
130 * This preserves the input state of the shell. It is used by
131 * st_save and st_restore to manupulate shell state.
140 struct whyle
*whyles
;
153 /* These keep signal state and setjump state */
161 static int srccat
__P((Char
*, Char
*));
163 static int srcfile
__P((char *, bool, int, Char
**));
165 int srcfile
__P((char *, bool, int, Char
**));
166 #endif /*WINNT_NATIVE*/
167 static sigret_t phup
__P((int));
168 static void srcunit
__P((int, bool, int, Char
**));
169 static void mailchk
__P((void));
170 #ifndef _PATH_DEFPATH
171 static Char
**defaultpath
__P((void));
173 static void record
__P((void));
174 static void st_save
__P((struct saved_state
*, int, int,
176 static void st_restore
__P((struct saved_state
*, Char
**));
178 int main
__P((int, char **));
189 register char *tcp
, *ttyn
;
191 register char **tempv
;
199 #endif /* WINNT_NATIVE */
200 #if defined(NLS_CATALOGS) && defined(LC_MESSAGES)
201 (void) setlocale(LC_MESSAGES
, "");
202 #endif /* NLS_CATALOGS && LC_MESSAGES */
206 (void) setlocale(LC_CTYPE
, ""); /* for iscntrl */
207 # endif /* LC_CTYPE */
213 mal_setstatsfile(fdopen(dup2(open("/tmp/tcsh.trace",
214 O_WRONLY
|O_CREAT
, 0666), 25), "w"));
216 #endif /* MALLOC_TRACE */
218 #if !(defined(BSDTIMES) || defined(_SEQUENT_)) && defined(POSIX)
220 clk_tck
= (clock_t) sysconf(_SC_CLK_TCK
);
221 # else /* ! _SC_CLK_TCK */
224 # else /* !CLK_TCK */
226 # endif /* CLK_TCK */
227 # endif /* _SC_CLK_TCK */
228 #endif /* !BSDTIMES && POSIX */
230 settimes(); /* Immed. estab. timing base */
236 * Make sure we have 0, 1, 2 open
237 * Otherwise `` jobs will not work... (From knaff@poly.polytechnique.fr)
241 if ((f
= open(_PATH_DEVNULL
, O_RDONLY
)) == -1 &&
242 (f
= open("/", O_RDONLY
)) == -1)
248 osinit(); /* Os dependent initialization */
254 t
= strrchr(argv
[0], '/');
257 char *s
= strrchr(argv
[0], '\\');
261 #endif /* WINNT_NATIVE */
262 t
= t
? t
+ 1 : argv
[0];
264 progname
= strsave((t
&& *t
) ? t
: tcshstr
); /* never want a null */
265 tcsh
= strcmp(progname
, tcshstr
) == 0;
269 * Initialize non constant strings
272 STR_BSHELL
= SAVE(_PATH_BSHELL
);
275 STR_SHELLPATH
= SAVE(_PATH_TCSHELL
);
278 STR_SHELLPATH
= SAVE(_PATH_CSHELL
);
281 STR_environ
= blk2short(environ
);
282 environ
= short2blk(STR_environ
); /* So that we can free it */
283 STR_WORD_CHARS
= SAVE(WORD_CHARS
);
289 word_chars
= STR_WORD_CHARS
;
290 bslash_quote
= 0; /* PWP: do tcsh-style backslash quoting? */
292 /* Default history size to 100 */
293 set(STRhistory
, SAVE("100"), VAR_READWRITE
);
296 ffile
= SAVE(tempv
[0]);
298 if (eq(ffile
, STRaout
)) /* A.out's are quittable */
305 * We are a login shell if: 1. we were invoked as -<something> with
306 * optional arguments 2. or we were invoked only with the -l flag
308 loginsh
= (**tempv
== '-') || (argc
== 2 &&
309 tempv
[1][0] == '-' && tempv
[1][1] == 'l' &&
310 tempv
[1][2] == '\0');
312 /* No better way to find if we are a login shell */
314 loginsh
= (argc
== 1 && getppid() == 1);
315 **tempv
= '-'; /* Avoid giving VMS an acidic stomach */
317 #endif /* _VMS_POSIX */
319 if (loginsh
&& **tempv
!= '-') {
321 * Mangle the argv space
326 for (tcp
= *tempv
; *tcp
++;)
328 for (tcp
--; tcp
>= *tempv
; tcp
--)
334 (void) time(&chktim
);
335 set(STRloginsh
, Strsave(STRNULL
), VAR_READWRITE
);
339 NoNLSRebind
= getenv("NOREBIND") != NULL
;
343 # endif /* SETLOCALEBUG */
344 (void) setlocale(LC_ALL
, "");
346 (void) setlocale(LC_COLLATE
, "");
350 # endif /* SETLOCALEBUG */
353 # endif /* STRCOLLBUG */
358 for (k
= 0200; k
<= 0377 && !Isprint(k
); k
++)
360 AsciiOnly
= k
> 0377;
363 AsciiOnly
= getenv("LANG") == NULL
&& getenv("LC_CTYPE") == NULL
;
365 if (MapsAreInited
&& !NLSMapsAreInited
)
370 * Initialize for periodic command intervals. Also, initialize the dummy
371 * tty list for login-watch.
373 (void) time(&t_period
);
376 #endif /* !HAVENOUTMP */
380 * From: Jim Pace <jdp@research.att.com>
381 * tcsh does not work properly on the alliants through an rlogin session.
382 * The shell generally hangs. Also, reference to the controlling terminal
383 * does not work ( ie: echo foo > /dev/tty ).
385 * A security feature was added to rlogind affecting FX/80's Concentrix
386 * from revision 5.5.xx upwards (through 5.7 where this fix was implemented)
387 * This security change also affects the FX/2800 series.
388 * The security change to rlogind requires the process group of an rlogin
389 * session become disassociated with the tty in rlogind.
391 * The changes needed are:
392 * 1. set the process group
393 * 2. reenable the control terminal
395 if (loginsh
&& isatty(SHIN
)) {
396 ttyn
= (char *) ttyname(SHIN
);
398 SHIN
= open(ttyn
, O_RDWR
);
400 (void) ioctl (SHIN
, TIOCSPGRP
, (ioctl_t
) &shpgrp
);
401 (void) setpgid(0, shpgrp
);
406 * Move the descriptors to safe places. The variable didfds is 0 while we
407 * have only FSH* to work with. When didfds is true, we have 0,1,2 and
408 * prefer to use these.
413 * Get and set the tty now
415 if ((ttyn
= ttyname(SHIN
)) != NULL
) {
417 * Could use rindex to get rid of other possible path components, but
418 * hpux preserves the subdirectory /pty/ when storing the tty name in
419 * utmp, so we keep it too.
421 if (strncmp(ttyn
, "/dev/", 5) == 0)
422 set(STRtty
, cp
= SAVE(ttyn
+ 5), VAR_READWRITE
);
424 set(STRtty
, cp
= SAVE(ttyn
), VAR_READWRITE
);
427 set(STRtty
, cp
= SAVE(""), VAR_READWRITE
);
429 * Initialize the shell variables. ARGV and PROMPT are initialized later.
430 * STATUS is also munged in several places. CHILD is munged when
435 * 7-10-87 Paul Placeway autologout should be set ONLY on login shells and
436 * on shells running as root. Out of these, autologout should NOT be set
437 * for any psudo-terminals (this catches most window systems) and not for
438 * any terminal running X windows.
440 * At Ohio State, we have had problems with a user having his X session
441 * drop out from under him (on a Sun) because the shell in his master
442 * xterm timed out and exited.
444 * Really, this should be done with a program external to the shell, that
445 * watches for no activity (and NO running programs, such as dump) on a
446 * terminal for a long peroid of time, and then SIGHUPS the shell on that
449 * bugfix by Rich Salz <rsalz@PINEAPPLE.BBN.COM>: For root rsh things
450 * allways first check to see if loginsh or really root, then do things
453 * Also by Jean-Francois Lamy <lamy%ai.toronto.edu@RELAY.CS.NET>: check the
454 * value of cp before using it! ("root can rsh too")
456 * PWP: keep the nested ifs; the order of the tests matters and a good
457 * (smart) C compiler might re-arange things wrong.
462 /* root always has a 15 minute autologout */
463 set(STRautologout
, Strsave(STRrootdefautologout
), VAR_READWRITE
);
467 /* users get autologout set to 0 */
468 set(STRautologout
, Strsave(STR0
), VAR_READWRITE
);
470 if (loginsh
|| (uid
== 0)) {
472 /* only for login shells or root and we must have a tty */
473 if ((cp2
= Strrchr(cp
, (Char
) '/')) != NULL
) {
478 if (!(((Strncmp(cp2
, STRtty
, 3) == 0) && Isalpha(cp2
[3])) ||
479 ((Strncmp(cp
, STRpts
, 3) == 0) && cp
[3] == '/'))) {
480 if (getenv("DISPLAY") == NULL
) {
481 /* NOT on X window shells */
482 set(STRautologout
, Strsave(STRdefautologout
),
489 #endif /* AUTOLOGOUT */
491 (void) sigset(SIGALRM
, alrmcatch
);
493 set(STRstatus
, Strsave(STR0
), VAR_READWRITE
);
496 * get and set machine specific environment variables
502 * Publish the selected echo style
504 #if ECHO_STYLE != BSD_ECHO
506 # if ECHO_STYLE == NONE_ECHO
507 set(STRecho_style
, Strsave(STRnone
), VAR_READWRITE
);
508 # endif /* ECHO_STYLE == NONE_ECHO */
509 # if ECHO_STYLE == SYSV_ECHO
510 set(STRecho_style
, Strsave(STRsysv
), VAR_READWRITE
);
511 # endif /* ECHO_STYLE == SYSV_ECHO */
512 # if ECHO_STYLE == BOTH_ECHO
513 set(STRecho_style
, Strsave(STRboth
), VAR_READWRITE
);
514 # endif /* ECHO_STYLE == BOTH_ECHO */
516 #endif /* ECHO_STYLE != BSD_ECHO */
517 set(STRecho_style
, Strsave(STRbsd
), VAR_READWRITE
);
520 * increment the shell level.
524 if ((tcp
= getenv("HOME")) != NULL
) {
525 if (strlen(tcp
) >= MAXPATHLEN
) {
527 if ((pw
= getpwuid(getuid())) != NULL
)
528 cp
= quote(SAVE(pw
->pw_dir
));
530 tcp
[MAXPATHLEN
-1] = '\0';
531 cp
= quote(SAVE(tcp
));
534 cp
= quote(SAVE(tcp
));
540 fast
= 1; /* No home -> can't read scripts */
542 set(STRhome
, cp
, VAR_READWRITE
);
544 dinit(cp
); /* dinit thinks that HOME == cwd in a login
547 * Grab other useful things from the environment. Should we grab
551 char *cln
, *cus
, *cgr
;
560 (void) Itoa(oid
, buff
, 0, 0);
561 set(STRoid
, Strsave(buff
), VAR_READWRITE
);
564 (void) Itoa(uid
, buff
, 0, 0);
565 set(STRuid
, Strsave(buff
), VAR_READWRITE
);
567 (void) Itoa(gid
, buff
, 0, 0);
568 set(STRgid
, Strsave(buff
), VAR_READWRITE
);
570 cln
= getenv("LOGNAME");
571 cus
= getenv("USER");
573 set(STRuser
, quote(SAVE(cus
)), VAR_READWRITE
);
574 else if (cln
!= NULL
)
575 set(STRuser
, quote(SAVE(cln
)), VAR_READWRITE
);
576 else if ((pw
= getpwuid(uid
)) == NULL
)
577 set(STRuser
, SAVE("unknown"), VAR_READWRITE
);
579 set(STRuser
, SAVE(pw
->pw_name
), VAR_READWRITE
);
581 tsetenv(STRLOGNAME
, varval(STRuser
));
583 tsetenv(STRKUSER
, varval(STRuser
));
585 cgr
= getenv("GROUP");
587 set(STRgroup
, quote(SAVE(cgr
)), VAR_READWRITE
);
588 else if ((gr
= getgrgid(gid
)) == NULL
)
589 set(STRgroup
, SAVE("unknown"), VAR_READWRITE
);
591 set(STRgroup
, SAVE(gr
->gr_name
), VAR_READWRITE
);
593 tsetenv(STRKGROUP
, varval(STRgroup
));
597 * HOST may be wrong, since rexd transports the entire environment on sun
598 * 3.x Just set it again
601 char cbuff
[MAXHOSTNAMELEN
];
603 if (gethostname(cbuff
, sizeof(cbuff
)) >= 0) {
604 cbuff
[sizeof(cbuff
) - 1] = '\0'; /* just in case */
605 tsetenv(STRHOST
, str2short(cbuff
));
608 tsetenv(STRHOST
, str2short("unknown"));
614 * Try to determine the remote host we were logged in from.
617 #endif /* REMOTEHOST */
620 if ((tcp
= getenv("SYSTYPE")) == NULL
)
622 tsetenv(STRSYSTYPE
, quote(str2short(tcp
)));
626 * set editing on by default, unless running under Emacs as an inferior
628 * We try to do this intelligently. If $TERM is available, then it
629 * should determine if we should edit or not. $TERM is preserved
630 * across rlogin sessions, so we will not get confused if we rlogin
631 * under an emacs shell. Another advantage is that if we run an
632 * xterm under an emacs shell, then the $TERM will be set to
633 * xterm, so we are going to want to edit. Unfortunately emacs
634 * does not restore all the tty modes, so xterm is not very well
635 * set up. But this is not the shell's fault.
636 * Also don't edit if $TERM == wm, for when we're running under an ATK app.
637 * Finally, emacs compiled under terminfo, sets the terminal to dumb,
638 * so disable editing for that too.
640 * Unfortunately, in some cases the initial $TERM setting is "unknown",
641 * "dumb", or "network" which is then changed in the user's startup files.
642 * We fix this by setting noediting here if $TERM is unknown/dumb and
643 * if noediting is set, we switch on editing if $TERM is changed.
645 if ((tcp
= getenv("TERM")) != NULL
) {
646 set(STRterm
, quote(SAVE(tcp
)), VAR_READWRITE
);
647 noediting
= strcmp(tcp
, "unknown") == 0 || strcmp(tcp
, "dumb") == 0 ||
648 strcmp(tcp
, "network") == 0;
649 editing
= strcmp(tcp
, "emacs") != 0 && strcmp(tcp
, "wm") != 0 &&
654 editing
= ((tcp
= getenv("EMACS")) == NULL
|| strcmp(tcp
, "t") != 0);
658 * The 'edit' variable is either set or unset. It doesn't
659 * need a value. Making it 'emacs' might be confusing.
662 set(STRedit
, Strsave(STRNULL
), VAR_READWRITE
);
666 * still more mutability: make the complete routine automatically add the
667 * suffix of file names...
669 set(STRaddsuffix
, Strsave(STRNULL
), VAR_READWRITE
);
672 * Random default kill ring size
674 set(STRkillring
, SAVE("30"), VAR_READWRITE
);
677 * Re-initialize path if set in environment
679 if ((tcp
= getenv("PATH")) == NULL
)
681 importpath(str2short(_PATH_DEFPATH
));
682 #else /* !_PATH_DEFPATH */
683 setq(STRpath
, defaultpath(), &shvhed
, VAR_READWRITE
);
684 #endif /* _PATH_DEFPATH */
686 /* Importpath() allocates memory for the path, and the
687 * returned pointer from SAVE() was discarded, so
688 * this was a memory leak.. (sg)
690 * importpath(SAVE(tcp));
692 importpath(str2short(tcp
));
696 /* If the SHELL environment variable ends with "tcsh", set
697 * STRshell to the same path. This is to facilitate using
698 * the executable in environments where the compiled-in
699 * default isn't appropriate (sg).
704 if ((tcp
= getenv("SHELL")) != NULL
) {
705 sh_len
= strlen(tcp
);
706 if ((sh_len
>= 5 && strcmp(tcp
+ (sh_len
- 5), "/tcsh") == 0) ||
707 (!tcsh
&& sh_len
>= 4 && strcmp(tcp
+ (sh_len
- 4), "/csh") == 0))
708 set(STRshell
, quote(SAVE(tcp
)), VAR_READWRITE
);
713 set(STRshell
, Strsave(STR_SHELLPATH
), VAR_READWRITE
);
717 if ((tcp
= getenv("LS_COLORS")) != NULL
)
718 parseLS_COLORS(str2short(tcp
));
719 #endif /* COLOR_LS_F */
721 doldol
= putn((int) getpid()); /* For $$ */
724 char *strtmp1
, strtmp2
[MAXPATHLEN
];
725 if ((strtmp1
= getenv("TMP")) != NULL
)
726 wsprintf(strtmp2
, "%s/%s", strtmp1
, "sh");
727 shtemp
= Strspl(SAVE(strtmp2
), doldol
); /* For << */
729 #else /* !WINNT_NATIVE */
730 shtemp
= Strspl(STRtmpsh
, doldol
); /* For << */
731 #endif /* WINNT_NATIVE */
734 * Record the interrupt states from the parent process. If the parent is
735 * non-interruptible our hand must be forced or we (and our children) won't
736 * be either. Our children inherit termination from our parent. We catch it
737 * only if we are the login shell.
741 * PURIFY-2 claims that osv does not get
742 * initialized after the sigvec call
744 setzero((char*) &osv
, sizeof(osv
));
745 /* parents interruptibility */
746 (void) mysigvec(SIGINT
, NULL
, &osv
);
747 parintr
= (signalfun_t
) osv
.sv_handler
;
748 (void) mysigvec(SIGTERM
, NULL
, &osv
);
749 parterm
= (signalfun_t
) osv
.sv_handler
;
751 parintr
= signal(SIGINT
, SIG_IGN
); /* parents interruptibility */
752 (void) sigset(SIGINT
, parintr
); /* ... restore */
755 if (loginsh
) /* it seems that SIGTERM is always set to SIG_IGN by */
756 /* init/getty so it should be set to SIG_DFL - there may be */
757 /* a better fix for this. */
760 # else /* !COHERENT */
761 parterm
= signal(SIGTERM
, SIG_IGN
); /* parents terminability */
762 # endif /* COHERENT */
763 (void) sigset(SIGTERM
, parterm
); /* ... restore */
769 /* Enable process migration on ourselves and our progeny */
770 (void) signal(SIGMIGRATE
, SIG_DFL
);
774 * dspkanji/dspmbyte autosetting
776 /* PATCH IDEA FROM Issei.Suzuki VERY THANKS */
777 #if defined(DSPMBYTE)
778 #if defined(NLS) && defined(LC_CTYPE)
779 if (((tcp
= setlocale(LC_CTYPE
, NULL
)) != NULL
|| (tcp
= getenv("LANG")) != NULL
) && !adrof(CHECK_MBYTEVAR
)) {
781 if ((tcp
= getenv("LANG")) != NULL
&& !adrof(CHECK_MBYTEVAR
)) {
783 autoset_dspmbyte(str2short(tcp
));
785 #if defined(WINNT_NATIVE)
786 else if (!adrof(CHECK_MBYTEVAR
))
787 nt_autoset_dspmbyte();
788 #endif /* WINNT_NATIVE */
791 fix_version(); /* publish the shell version */
793 if (argc
> 1 && strcmp(argv
[1], "--version") == 0) {
794 xprintf("%S\n", varval(STRversion
));
798 * Process the arguments.
800 * Note that processing of -v/-x is actually delayed till after script
803 * We set the first character of our name to be '-' if we are a shell
804 * running interruptible commands. Many programs which examine ps'es
805 * use this to filter such shells out.
808 while (argc
> 0 && (tcp
= tempv
[0])[0] == '-' &&
809 *++tcp
!= '\0' && !batch
) {
813 case 0: /* - Interruptible, no prompt */
819 case 'b': /* -b Next arg is input file */
823 case 'c': /* -c Command input from arg */
829 it relies on a 7 bit environment (/bin/sh), so it
830 pass ascii arguments with the 8th bit set */
831 if (!strcmp(argv
[0], "sh"))
835 for (p
= tempv
[0]; *p
; ++p
)
839 arginp
= SAVE(tempv
[0]);
842 * we put the command into a variable
845 set(STRcommand
, quote(Strsave(arginp
)), VAR_READWRITE
);
848 * * Give an error on -c arguments that end in * backslash to
849 * ensure that you don't make * nonportable csh scripts.
854 cp
= arginp
+ Strlen(arginp
);
856 while (cp
> arginp
&& *--cp
== '\\')
858 if ((count
& 1) != 0) {
866 case 'd': /* -d Load directory stack from file */
871 case 'D': /* -D Define environment variable */
876 if (dp
= Strchr(cp
, '=')) {
881 tsetenv(cp
, STRNULL
);
883 *tcp
= '\0'; /* done with this argument */
887 case 'e': /* -e Exit on any error */
891 case 'f': /* -f Fast start */
895 case 'i': /* -i Interactive, even if !intty */
900 case 'm': /* -m read .cshrc (from su) */
904 case 'n': /* -n Don't execute */
908 case 'q': /* -q (Undoc'd) ... die on quit */
912 case 's': /* -s Read from std input */
916 case 't': /* -t Read one line from input */
922 case 'v': /* -v Echo hist expanded input */
923 nverbose
= 1; /* ... later */
926 case 'x': /* -x Echo just before execution */
927 nexececho
= 1; /* ... later */
930 case 'V': /* -V Echo hist expanded input */
931 setNS(STRverbose
); /* NOW! */
934 case 'X': /* -X Echo just before execution */
935 setNS(STRecho
); /* NOW! */
938 case 'F': /* Undocumented flag */
940 * This will cause children to be created using fork instead of
949 * for O/S's that don't do the argument parsing right in
950 * "#!/foo -f " scripts
954 default: /* Unknown command option */
956 stderror(ERR_TCSHUSAGE
, tcp
-1, progname
);
963 if (quitit
) /* With all due haste, for debugging */
964 (void) signal(SIGQUIT
, SIG_DFL
);
967 * Unless prevented by -, -c, -i, -s, or -t, if there are remaining
968 * arguments the first of them is the name of a shell file from which to
971 if (nofile
== 0 && argc
> 0) {
972 nofile
= open(tempv
[0], O_RDONLY
);
974 child
= 1; /* So this ... */
975 /* ... doesn't return */
976 stderror(ERR_SYSTEM
, tempv
[0], strerror(errno
));
979 xfree((ptr_t
) ffile
);
981 ffile
= SAVE(tempv
[0]);
983 * Replace FSHIN. Handle /dev/std{in,out,err} specially
984 * since once they are closed we cannot open them again.
985 * In that case we use our own saved descriptors
987 if ((SHIN
= dmove(nofile
, FSHIN
)) < 0)
999 stderror(ERR_SYSTEM
, tempv
[0], strerror(errno
));
1002 (void) close_on_exec(SHIN
, 1);
1004 /* argc not used any more */ tempv
++;
1008 * Call to closem() used to be part of initdesc(). Now called below where
1009 * the script name argument has become stdin. Kernel may have used a file
1010 * descriptor to hold the name of the script (setuid case) and this name
1011 * mustn't be lost by closing the fd too soon.
1016 * Consider input a tty if it really is or we are interactive. but not for
1017 * editing (christos)
1019 if (!(intty
= isatty(SHIN
))) {
1026 if (intty
|| (intact
&& isatty(SHOUT
))) {
1027 if (!batch
&& (uid
!= euid
|| gid
!= egid
)) {
1029 child
= 1; /* So this ... */
1030 /* ... doesn't return */
1031 stderror(ERR_SYSTEM
, progname
, strerror(errno
));
1035 isoutatty
= isatty(SHOUT
);
1036 isdiagatty
= isatty(SHDIAG
);
1038 * Decide whether we should play with signals or not. If we are explicitly
1039 * told (via -i, or -) or we are a login shell (arg0 starts with -) or the
1040 * input and output are both the ttys("csh", or "csh</dev/ttyx>/dev/ttyx")
1041 * Note that in only the login shell is it likely that parent may have set
1042 * signals to be ignored
1044 if (loginsh
|| intact
|| (intty
&& isatty(SHOUT
)))
1048 * Save the remaining arguments in argv.
1050 setq(STRargv
, blk2short(tempv
), &shvhed
, VAR_READWRITE
);
1053 * Set up the prompt.
1057 set(STRprompt
, Strsave(STRdeftcshprompt
), VAR_READWRITE
);
1059 set(STRprompt
, Strsave(STRdefcshprompt
), VAR_READWRITE
);
1060 /* that's a meta-questionmark */
1061 set(STRprompt2
, Strsave(STRmquestion
), VAR_READWRITE
);
1062 set(STRprompt3
, Strsave(STRKCORRECT
), VAR_READWRITE
);
1066 * If we are an interactive shell, then start fiddling with the signals;
1067 * this is a tricky game.
1069 shpgrp
= mygetpgrp();
1074 if (!quitit
) /* Wary! */
1075 (void) signal(SIGQUIT
, SIG_IGN
);
1076 (void) sigset(SIGINT
, pintr
);
1077 (void) sighold(SIGINT
);
1078 (void) signal(SIGTERM
, SIG_IGN
);
1081 * No reason I can see not to save history on all these events..
1082 * Most usual occurrence is in a window system, where we're not a login
1083 * shell, but might as well be... (sg)
1084 * But there might be races when lots of shells exit together...
1085 * [this is also incompatible].
1086 * We have to be mre careful here. If the parent wants to
1087 * ignore the signals then we leave them untouched...
1088 * We also only setup the handlers for shells that are trully
1091 osig
= signal(SIGHUP
, phup
); /* exit processing on HUP */
1092 if (!loginsh
&& osig
== SIG_IGN
)
1093 (void) signal(SIGHUP
, osig
);
1095 osig
= signal(SIGXCPU
, phup
); /* exit processing on XCPU */
1096 if (!loginsh
&& osig
== SIG_IGN
)
1097 (void) signal(SIGXCPU
, osig
);
1100 osig
= signal(SIGXFSZ
, phup
); /* exit processing on XFSZ */
1101 if (!loginsh
&& osig
== SIG_IGN
)
1102 (void) signal(SIGXFSZ
, osig
);
1105 if (quitit
== 0 && arginp
== 0) {
1107 (void) signal(SIGTSTP
, SIG_IGN
);
1110 (void) signal(SIGTTIN
, SIG_IGN
);
1113 (void) signal(SIGTTOU
, SIG_IGN
);
1116 * Wait till in foreground, in case someone stupidly runs csh &
1117 * dont want to try to grab away the tty.
1119 if (isatty(FSHDIAG
))
1121 else if (isatty(FSHOUT
))
1123 else if (isatty(OLDSTD
))
1129 /* NeXT 2.0 /usr/etc/rlogind, does not set our process group! */
1132 (void) setpgid(0, shpgrp
);
1133 (void) tcsetpgrp(f
, shpgrp
);
1136 #ifdef BSDJOBS /* if we have tty job control */
1138 if ((tpgrp
= tcgetpgrp(f
)) != -1) {
1139 if (tpgrp
!= shpgrp
) {
1140 signalfun_t old
= signal(SIGTTIN
, SIG_DFL
);
1141 (void) kill(0, SIGTTIN
);
1142 (void) signal(SIGTTIN
, old
);
1146 * Thanks to Matt Day for the POSIX references, and to
1147 * Paul Close for the SGI clarification.
1149 if (setdisc(f
) != -1) {
1153 if (tcsetpgrp(f
, shpgrp
) == -1) {
1155 * On hpux 7.03 this fails with EPERM. This happens on
1156 * the 800 when opgrp != shpgrp at this point. (we were
1157 * forked from a non job control shell)
1158 * POSIX 7.2.4, says we failed because the process
1159 * group specified did not belong to a process
1160 * in the same session with the tty. So we set our
1161 * process group and try again.
1163 if (setpgid(0, shpgrp
) == -1) {
1164 xprintf("setpgid:");
1167 if (tcsetpgrp(f
, shpgrp
) == -1) {
1168 xprintf("tcsetpgrp:");
1173 * We check the process group now. If it is the same, then
1174 * we don't need to set it again. On hpux 7.0 on the 300's
1175 * if we set it again it fails with EPERM. This is the
1176 * correct behavior according to POSIX 4.3.3 if the process
1177 * was a session leader .
1179 else if (shpgrp
!= mygetpgrp()) {
1180 if(setpgid(0, shpgrp
) == -1) {
1181 xprintf("setpgid:");
1187 * But on irix 3.3 we need to set it again, even if it is
1188 * the same. We do that to tell the system that we
1189 * need BSD process group compatibility.
1192 (void) setpgid(0, shpgrp
);
1194 (void) close_on_exec(dcopy(f
, FSHTTY
), 1);
1201 xprintf(CGETS(11, 1, "Warning: no access to tty (%s).\n"),
1203 xprintf(CGETS(11, 2, "Thus no job control in this shell.\n"));
1205 * Fix from:Sakari Jalovaara <sja@sirius.hut.fi> if we don't
1206 * have access to tty, disable editing too
1212 #else /* BSDJOBS */ /* don't have job control, so frotz it */
1214 #endif /* BSDJOBS */
1217 if ((setintr
== 0) && (parintr
== SIG_DFL
))
1221 * SVR4 doesn't send a SIGCHLD when a child is stopped or continued if the
1222 * handler is installed with signal(2) or sigset(2). sigaction(2) must
1225 * David Dawes (dawes@physics.su.oz.au) Sept 1991
1230 struct sigaction act
;
1231 act
.sa_handler
=pchild
;
1232 (void) sigemptyset(&(act
.sa_mask
)); /* Don't block any extra sigs
1233 * when the handler is called
1235 act
.sa_flags
=0; /* want behaviour of sigset() without
1239 if ((sigaction(SIGCHLD
,&act
,(struct sigaction
*)NULL
)) == -1)
1240 stderror(ERR_SYSTEM
, "sigaction", strerror(errno
));
1242 #else /* SYSVREL <= 3 */
1243 (void) sigset(SIGCHLD
, pchild
); /* while signals not ready */
1244 #endif /* SYSVREL <= 3 */
1247 if (intty
&& !arginp
)
1248 (void) ed_Setup(editing
);/* Get the tty state, and set defaults */
1249 /* Only alter the tty state if editing */
1252 * Set an exit here in case of an interrupt or error reading the shell
1255 reenter
= setexit(); /* PWP */
1257 haderr
= 0; /* In case second time through */
1258 if (!fast
&& reenter
== 0) {
1259 /* Will have varval(STRhome) here because set fast if don't */
1261 int osetintr
= setintr
;
1262 signalfun_t oparintr
= parintr
;
1265 sigmask_t omask
= sigblock(sigmask(SIGINT
));
1267 (void) sighold(SIGINT
);
1270 parintr
= SIG_IGN
; /* onintr in /etc/ files has no effect */
1272 #ifdef _PATH_DOTLOGIN
1274 (void) srcfile(_PATH_DOTLOGIN
, 0, 0, NULL
);
1278 #ifdef _PATH_DOTCSHRC
1279 (void) srcfile(_PATH_DOTCSHRC
, 0, 0, NULL
);
1281 if (!arginp
&& !onelflg
&& !havhash
)
1284 #ifdef _PATH_DOTLOGIN
1286 (void) srcfile(_PATH_DOTLOGIN
, 0, 0, NULL
);
1290 (void) sigsetmask(omask
);
1292 (void) sigrelse(SIGINT
);
1299 (void) srccat(varval(STRhome
), STRsldotlogin
);
1301 /* upward compat. */
1302 if (!srccat(varval(STRhome
), STRsldottcshrc
))
1303 (void) srccat(varval(STRhome
), STRsldotcshrc
);
1305 if (!fast
&& !arginp
&& !onelflg
&& !havhash
)
1309 * Source history before .login so that it is available in .login
1314 (void) srccat(varval(STRhome
), STRsldotlogin
);
1316 if (!fast
&& (loginsh
|| rdirs
))
1319 /* Initing AFTER .cshrc is the Right Way */
1320 if (intty
&& !arginp
) { /* PWP setup stuff */
1321 ed_Init(); /* init the new line editor */
1323 check_window_size(1); /* mung environment */
1324 #endif /* SIG_WINDOW */
1328 * Now are ready for the -v and -x flags
1336 * All the rest of the world is inside this call. The argument to process
1337 * indicates whether it should catch "error unwinds". Thus if we are a
1338 * interactive shell our call here will never return by being blown past on
1348 xprintf("logout\n");
1354 goodbye(NULL
, NULL
);
1369 if (tpgrp
> 0 && opgrp
!= shpgrp
) {
1370 (void) setpgid(0, opgrp
);
1371 (void) tcsetpgrp(FSHTTY
, opgrp
);
1372 (void) resetdisc(FSHTTY
);
1374 #endif /* BSDJOBS */
1386 for (dp
= cp
; *dp
; dp
++)
1390 * i+2 where i is the number of colons in the path. There are i+1
1391 * directories in the path plus we need room for a zero terminator.
1393 pv
= (Char
**) xcalloc((size_t) (i
+ 2), sizeof(Char
*));
1398 if ((c
= *dp
) == PATHSEP
|| c
== 0) {
1400 pv
[i
++] = Strsave(*cp
? cp
: STRdot
);
1409 else if (*dp
== '\\')
1411 #endif /* WINNT_NATIVE */
1415 setq(STRpath
, pv
, &shvhed
, VAR_READWRITE
);
1419 * Source to the file which is the catenation of the argument names.
1425 if (cp
[0] == '/' && cp
[1] == '\0')
1426 return srcfile(short2str(dp
), (mflag
? 0 : 1), 0, NULL
);
1436 if (ep
[-1] == '/' && dp
[0] == '/') /* silly win95 */
1438 #endif /* WINNT_NATIVE */
1440 ep
= Strspl(cp
, dp
);
1441 ptr
= short2str(ep
);
1443 rv
= srcfile(ptr
, (mflag
? 0 : 1), 0, NULL
);
1450 * Source to a file putting the file descriptor in a safe place (> 2).
1452 #ifndef WINNT_NATIVE
1456 #endif /*WINNT_NATIVE*/
1457 srcfile(f
, onlyown
, flag
, av
)
1465 if ((unit
= open(f
, O_RDONLY
)) == -1)
1467 unit
= dmove(unit
, -1);
1469 (void) close_on_exec(unit
, 1);
1470 srcunit(unit
, onlyown
, flag
, av
);
1476 * Save the shell state, and establish new argument vector, and new input
1480 st_save(st
, unit
, hflg
, al
, av
)
1481 struct saved_state
*st
;
1485 st
->insource
= insource
;
1487 /* Want to preserve the meaning of "source file >output".
1488 * Save old descriptors, move new 0,1,2 to safe places and assign
1489 * them to SH* and let process() redo 0,1,2 from them.
1491 * The macro returns true if d1 and d2 are good and they point to
1492 * different things. If you don't avoid saving duplicate
1493 * descriptors, you really limit the depth of "source" recursion
1494 * you can do because of all the open file descriptors. -IAN!
1496 #define NEED_SAVE_FD(d1,d2) \
1497 (fstat(d1, &s1) != -1 && fstat(d2, &s2) != -1 \
1498 && (s1.st_ino != s2.st_ino || s1.st_dev != s2.st_dev) )
1500 st
->OLDSTD
= st
->SHOUT
= st
->SHDIAG
= -1;/* test later to restore these */
1503 if (NEED_SAVE_FD(0,OLDSTD
))
1504 st
->OLDSTD
= OLDSTD
, OLDSTD
= dmove(0, -1);
1505 if (NEED_SAVE_FD(1,SHOUT
))
1506 st
->SHOUT
= SHOUT
, SHOUT
= dmove(1, -1);
1507 if (NEED_SAVE_FD(2,SHDIAG
))
1508 st
->SHDIAG
= SHDIAG
, SHDIAG
= dmove(2, -1);
1513 st
->whyles
= whyles
;
1514 st
->gointr
= gointr
;
1515 st
->arginp
= arginp
;
1517 st
->evalvec
= evalvec
;
1518 st
->alvecp
= alvecp
;
1520 st
->onelflg
= onelflg
;
1521 st
->enterhist
= enterhist
;
1526 st
->cantell
= cantell
;
1530 * we can now pass arguments to source.
1531 * For compatibility we do that only if arguments were really
1532 * passed, otherwise we keep the old, global $argv like before.
1534 if (av
!= NULL
&& *av
!= NULL
) {
1536 if ((vp
= adrof(STRargv
)) != NULL
&& vp
->vec
!= NULL
)
1537 st
->argv
= saveblk(vp
->vec
);
1540 setq(STRargv
, saveblk(av
), &shvhed
, VAR_READWRITE
);
1545 SHIN
= unit
; /* Do this first */
1547 /* Establish new input arena */
1550 fseekp
= feobp
= fblocks
= 0;
1556 intty
= isatty(SHIN
);
1571 * Restore the shell to a saved state
1575 struct saved_state
*st
;
1581 /* Reset input arena */
1584 register Char
** nfbuf
= fbuf
;
1585 register int nfblocks
= fblocks
;
1589 for (i
= 0; i
< nfblocks
; i
++)
1590 xfree((ptr_t
) nfbuf
[i
]);
1591 xfree((ptr_t
) nfbuf
);
1597 insource
= st
->insource
;
1599 if (st
->OLDSTD
!= -1)
1600 (void)close(OLDSTD
), OLDSTD
= st
->OLDSTD
;
1601 if (st
->SHOUT
!= -1)
1602 (void)close(SHOUT
), SHOUT
= st
->SHOUT
;
1603 if (st
->SHDIAG
!= -1)
1604 (void)close(SHDIAG
), SHDIAG
= st
->SHDIAG
;
1605 arginp
= st
->arginp
;
1606 onelflg
= st
->onelflg
;
1608 evalvec
= st
->evalvec
;
1609 alvecp
= st
->alvecp
;
1612 whyles
= st
->whyles
;
1613 gointr
= st
->gointr
;
1614 if (st
->HIST
!= '\0')
1616 enterhist
= st
->enterhist
;
1617 cantell
= st
->cantell
;
1619 if (st
->argv
!= NULL
)
1620 setq(STRargv
, st
->argv
, &shvhed
, VAR_READWRITE
);
1621 else if (av
!= NULL
&& *av
!= NULL
&& adrof(STRargv
) != NULL
)
1626 * Source to a unit. If onlyown it must be our file or our group or
1627 * we don't chance it. This occurs on ".cshrc"s and the like.
1630 srcunit(unit
, onlyown
, hflg
, av
)
1636 struct saved_state st
;
1637 st
.SHIN
= -1; /* st_restore checks this */
1645 if (fstat(unit
, &stb
) < 0) {
1651 getexit(st
.oldexit
);
1655 st
.mask
= sigblock(sigmask(SIGINT
));
1657 (void) sighold(SIGINT
);
1660 /* Save the current state and move us to a new state */
1661 st_save(&st
, unit
, hflg
, NULL
, av
);
1664 * Now if we are allowing commands to be interrupted, we let ourselves be
1669 (void) sigsetmask(st
.mask
);
1671 (void) sigrelse(SIGINT
);
1675 * Bugfix for running out of memory by: Jak Kirman
1676 * <jak%cs.brown.edu@RELAY.CS.NET>. Solution: pay attention to what
1677 * setexit() is returning because reenter _may_ be in a register, and
1678 * thus restored to 0 on a longjump(). (PWP: insert flames about
1679 * compiler-dependant code here) PWP: THANKS LOTS !!!
1681 * PWP: think of this as like a LISP (unwind-protect ...)
1682 * thanks to Diana Smetters for pointing out how this _should_ be written
1685 st
.reenter
= 1; /* assume non-zero return val */
1686 if (setexit() == 0) {
1687 st
.reenter
= 0; /* Oh well, we were wrong */
1689 if ((st
.reenter
= setexit()) == 0) {
1691 process(0); /* 0 -> blow away on errors */
1696 (void) sigsetmask(st
.mask
);
1698 (void) sigrelse(SIGINT
);
1701 /* Restore the old state */
1702 st_restore(&st
, av
);
1703 resexit(st
.oldexit
);
1705 * If process reset() (effectively an unwind) then we must also unwind.
1708 stderror(ERR_SILENT
);
1722 (void) sigset(SIGQUIT
, SIG_IGN
);
1723 (void) sigset(SIGINT
, SIG_IGN
);
1724 (void) sigset(SIGTERM
, SIG_IGN
);
1725 (void) sigset(SIGHUP
, SIG_IGN
);
1726 setintr
= 0; /* No interrupts after "logout" */
1727 /* Trap errors inside .logout */
1728 reenter
= setexit();
1731 if (!(adrof(STRlogout
)))
1732 set(STRlogout
, Strsave(STRnormal
), VAR_READWRITE
);
1733 #ifdef _PATH_DOTLOGOUT
1734 (void) srcfile(_PATH_DOTLOGOUT
, 0, 0, NULL
);
1737 (void) srccat(varval(STRhome
), STRsldtlogout
);
1752 * Note that if STATUS is corrupted (i.e. getn bombs) then error will exit
1753 * directly because we poke child here. Otherwise we might continue
1754 * unwarrantedly (sic).
1758 xexit(getn(varval(STRstatus
)));
1762 * in the event of a HUP we want to save the history
1769 * There is no return from here,
1770 * so we are not going to release SIGHUP
1775 (void) sigset(snum
, SIG_IGN
);
1778 (void) sigblock(sigmask(SIGHUP
));
1780 (void) sighold(SIGHUP
);
1781 # endif /* BSDSIGS */
1782 #endif /* UNRELSIGS */
1785 set(STRlogout
, Strsave(STRhangup
), VAR_READWRITE
);
1786 #ifdef _PATH_DOTLOGOUT
1787 (void) srcfile(_PATH_DOTLOGOUT
, 0, 0, NULL
);
1790 (void) srccat(varval(STRhome
), STRsldtlogout
);
1797 * We kill the last foreground process group. It then becomes
1798 * responsible to propagate the SIGHUP to its progeny.
1801 struct process
*pp
, *np
;
1803 for (pp
= proclist
.p_next
; pp
; pp
= pp
->p_next
) {
1806 * Find if this job is in the foreground. It could be that
1807 * the process leader has exited and the foreground flag
1808 * is cleared for it.
1812 * If a process is in the foreground we try to kill
1813 * it's process group. If we succeed, then the
1814 * whole job is gone. Otherwise we keep going...
1815 * But avoid sending HUP to the shell again.
1817 if (((np
->p_flags
& PFOREGND
) != 0) && np
->p_jobid
!= shpgrp
) {
1818 np
->p_flags
&= ~PHUP
;
1819 if (killpg(np
->p_jobid
, SIGHUP
) != -1) {
1820 /* In case the job was suspended... */
1822 (void) killpg(np
->p_jobid
, SIGCONT
);
1827 while ((np
= np
->p_friends
) != pp
);
1830 #endif /* POSIXJOBS */
1838 static Char
*jobargv
[2] = {STRjobs
, 0};
1841 * Catch an interrupt, e.g. during lexical input.
1842 * If we are an interactive shell, we reset the interrupt catch
1843 * immediately. In any case we drain the shell output,
1844 * and finally go through the normal error mechanism, which
1845 * gets a chance to make the shell go away.
1847 int just_signaled
; /* bugfix by Michael Bloom (mg@ttidca.TTI.COM) */
1858 (void) sigset(snum
, pintr
);
1859 #endif /* UNRELSIGS */
1877 omask
= sigblock((sigmask_t
) 0);
1881 (void) sigsetmask(omask
& ~sigmask(SIGINT
));
1883 (void) sigrelse(SIGINT
);
1888 dojobs(jobargv
, NULL
);
1889 stderror(ERR_NAME
| ERR_INTR
);
1892 /* MH - handle interrupted completions specially */
1894 extern int InsideCompletion
;
1896 if (InsideCompletion
)
1897 stderror(ERR_SILENT
);
1899 /* JV - Make sure we shut off inputl */
1901 extern Char GettingInput
;
1903 (void) Cookedmode();
1907 (void) sigsetmask(omask
& ~sigmask(SIGCHLD
));
1910 (void) sighold(SIGINT
);
1911 (void) sigrelse(SIGCHLD
);
1914 #if !defined(_VMS_POSIX) && !defined(WINNT_NATIVE)
1916 #endif /* !_VMS_POSIX && !WINNT_NATIVE */
1919 * If we have an active "onintr" then we search for the label. Note that if
1920 * one does "onintr -" then we shan't be interruptible so we needn't worry
1926 if ((v
= pargv
) != 0)
1927 pargv
= 0, blkfree(v
);
1928 if ((v
= gargv
) != 0)
1929 gargv
= 0, blkfree(v
);
1932 else if (intty
&& wantnl
) {
1935 * If we are editing a multi-line input command, and move to
1936 * the beginning of the line, we don't want to trash it when
1944 /* xputchar('\n'); *//* Some like this, others don't */
1945 (void) putraw('\r');
1946 (void) putraw('\n');
1949 stderror(ERR_SILENT
);
1953 * Process is the main driving routine for the shell.
1954 * It runs all command processing, except for those within { ... }
1955 * in expressions (which is run by a routine evalav in sh.exp.c which
1956 * is a stripped down process), and `...` evaluation which is run
1957 * also by a subset of this code in sh.glob.c in the routine backeval.
1959 * The code here is a little strange because part of it is interruptible
1960 * and hence freeing of structures appears to occur when none is necessary
1961 * if this is ignored.
1963 * Note that if catch is not set then we will unwind on any error.
1964 * If an end-of-file occurs, we return.
1966 static struct command
*savet
= NULL
;
1973 /* PWP: This might get nuked my longjmp so don't make it a register var */
1974 struct command
*t
= savet
;
1982 /* This was leaking memory badly, particularly when sourcing
1983 * files, etc.. For whatever reason we were arriving here with
1984 * allocated pointers still active, and the code was simply
1985 * overwriting them. I can't say I fully understand the
1986 * control flow here, but according to Purify this particular
1987 * leak has been plugged, and I haven't noticed any ill
1990 if (paraml
.next
&& paraml
.next
!= ¶ml
)
1993 paraml
.next
= paraml
.prev
= ¶ml
;
1994 paraml
.word
= STRNULL
;
1996 justpr
= enterhist
; /* execute if not entering history */
1999 * Interruptible during interactive reads
2003 (void) sigsetmask(sigblock((sigmask_t
) 0) & ~sigmask(SIGINT
));
2005 (void) sigrelse(SIGINT
);
2010 * For the sake of reset()
2014 freesyn(savet
), savet
= NULL
;
2026 * Every error is eventually caught here or the shell dies. It is
2027 * at this point that we clean up any left-over open files, by
2028 * closing all but a fixed number of pre-defined files. Thus
2029 * routines don't have to worry about leaving files open due to
2030 * deeper errors... they will get closed here.
2043 if (intty
&& prompt
&& evalvec
== 0) {
2047 * Watch for logins/logouts. Next is scheduled commands stored
2048 * previously using "sched." Then execute periodic commands.
2049 * Following that, the prompt precmd is run.
2053 #endif /* !HAVENOUTMP */
2058 * If we are at the end of the input buffer then we are going to
2059 * read fresh stuff. Otherwise, we are rereading input and don't
2060 * need or want to prompt.
2062 if (fseekp
== feobp
&& aret
== TCSH_F_SEEK
)
2063 printprompt(0, NULL
);
2068 xfree((ptr_t
) seterr
);
2073 * Echo not only on VERBOSE, but also with history expansion. If there
2074 * is a lexical error then we forego history echo.
2076 if ((lex(¶ml
) && !seterr
&& intty
&& !tellwhat
&& !Expand
&&
2077 !whyles
) || adrof(STRverbose
)) {
2078 int odidfds
= didfds
;
2086 (void) alarm(0); /* Autologout OFF */
2089 * The parser may lose space if interrupted.
2093 (void) sigblock(sigmask(SIGINT
));
2095 (void) sighold(SIGINT
);
2099 * Save input text on the history list if reading in old history, or it
2100 * is from the terminal at the top level and not in a loop.
2102 * PWP: entry of items in the history list while in a while loop is done
2105 if (enterhist
|| (catch && intty
&& !whyles
&& !tellwhat
&& !arun
))
2106 savehist(¶ml
, enterhist
> 1);
2108 if (Expand
&& seterr
)
2112 * Print lexical error messages, except when sourcing history lists.
2114 if (!enterhist
&& seterr
)
2118 * If had a history command :p modifier then this is as far as we
2125 * If had a tellwhat from twenex() then do
2128 (void) tellmewhat(¶ml
, NULL
);
2136 * If we are interactive, try to continue jobs that we have stopped
2139 continue_jobs(¶ml
);
2140 #endif /* BSDJOBS */
2143 * Check to see if the user typed "rm * .o" or something
2148 * Parse the words of the input into a parse tree.
2150 savet
= syntax(paraml
.next
, ¶ml
, 0);
2156 * Execute the parse tree From: Michael Schroeder
2157 * <mlschroe@immd4.informatik.uni-erlangen.de> was execute(t, tpgrp);
2159 execute(savet
, (tpgrp
> 0 ? tpgrp
: -1), NULL
, NULL
, TRUE
);
2165 freesyn(savet
), savet
= NULL
;
2167 if (windowchg
|| (catch && intty
&& !whyles
&& !tellwhat
)) {
2169 (void) check_window_size(0); /* for window systems */
2171 #endif /* SIG_WINDOW */
2172 set(STR_
, Strsave(InputBuf
), VAR_READWRITE
| VAR_NOGLOB
);
2191 if (*t
&& eq(*t
, STRmh
)) {
2193 stderror(ERR_NAME
| ERR_HFLAG
);
2196 else if (*t
&& eq(*t
, STRmm
)) {
2198 stderror(ERR_NAME
| ERR_MFLAG
);
2202 f
= globone(*t
++, G_ERROR
);
2203 (void) strcpy(buf
, short2str(f
));
2205 if ((!srcfile(buf
, 0, hflg
, t
)) && (!hflg
) && (!bequiet
))
2206 stderror(ERR_SYSTEM
, buf
, strerror(errno
));
2211 * If we are a login shell, then we don't want to tell
2212 * about any mail file unless its been modified
2213 * after the time we started.
2214 * This prevents us from telling the user things he already
2215 * knows, since the login program insists on saying
2221 * This version checks if the file is a directory, and if so,
2222 * tells you the number of files in it, otherwise do the old thang.
2223 * The magic "+1" in the time calculation is to compensate for
2224 * an AFS bug where directory mtimes are set to 1 second in
2231 register struct varent
*v
;
2239 if (v
== NULL
|| v
->vec
== NULL
)
2244 intvl
= (cnt
&& number(*vp
)) ? (--cnt
, getn(*vp
++)) : MAILINTVL
;
2247 if (chktim
+ intvl
> t
)
2250 char *filename
= short2str(*vp
);
2251 char *mboxdir
= filename
;
2253 if (stat(filename
, &stb
) < 0)
2255 #if defined(BSDTIMES) || defined(_SEQUENT_)
2256 new = stb
.st_mtime
> time0
.tv_sec
;
2258 new = stb
.st_mtime
> seconds0
;
2260 if (S_ISDIR(stb
.st_mode
)) {
2263 char tempfilename
[MAXPATHLEN
];
2266 xsnprintf(tempfilename
, MAXPATHLEN
, "%s/new", filename
);
2268 if (stat(tempfilename
, &stc
) != -1 && S_ISDIR(stc
.st_mode
)) {
2270 * "filename/new" exists and is a directory; you are
2274 #if defined(BSDTIMES) || defined(_SEQUENT_)
2275 new = stb
.st_mtime
> time0
.tv_sec
;
2277 new = stb
.st_mtime
> seconds0
;
2279 mboxdir
= tempfilename
;
2282 if (stb
.st_mtime
<= chktim
+ 1 || (loginsh
&& !new))
2285 if ((mailbox
= opendir(mboxdir
)) == NULL
)
2289 if (!readdir(mailbox
) || !readdir(mailbox
))
2292 while (readdir(mailbox
))
2299 xprintf(CGETS(11, 3, "You have %d mail messages.\n"),
2302 xprintf(CGETS(11, 4, "You have %d mail messages in %s.\n"),
2303 mailcount
, filename
);
2306 if (stb
.st_size
== 0 || stb
.st_atime
> stb
.st_mtime
||
2307 (stb
.st_atime
<= chktim
&& stb
.st_mtime
<= chktim
) ||
2311 xprintf(CGETS(11, 5, "You have %smail.\n"),
2312 new ? CGETS(11, 6, "new ") : "");
2314 xprintf(CGETS(11, 7, "You have %smail in %s.\n"),
2315 new ? CGETS(11, 6, "new ") : "", filename
);
2322 * Extract a home directory from the password file
2323 * The argument points to a buffer where the name of the
2324 * user whose home directory is sought is currently.
2325 * We write the home directory of the user back there.
2336 if (*home
== '\0') {
2337 if ((h
= varval(STRhome
)) != STRNULL
) {
2338 (void) Strcpy(home
, h
);
2348 if ((h
= gettilde(home
)) == NULL
)
2351 (void) Strcpy(home
, h
);
2357 * Move the initial descriptors to their eventual
2358 * resting places, closing all other units.
2365 (void)catclose(catd
);
2366 #endif /* NLS_CATALOGS */
2367 #endif /* NLS_BUGS */
2370 didfds
= 0; /* 0, 1, 2 aren't set up */
2371 (void) close_on_exec(SHIN
= dcopy(0, FSHIN
), 1);
2372 (void) close_on_exec(SHOUT
= dcopy(1, FSHOUT
), 1);
2373 (void) close_on_exec(SHDIAG
= dcopy(2, FSHDIAG
), 1);
2374 (void) close_on_exec(OLDSTD
= dcopy(SHIN
, FOLDSTD
), 1);
2375 #ifndef CLOSE_ON_EXEC
2376 didcch
= 0; /* Havent closed for child */
2377 #endif /* CLOSE_ON_EXEC */
2378 isdiagatty
= isatty(SHDIAG
);
2379 isoutatty
= isatty(SHOUT
);
2383 #endif /* NLS_CATALOGS */
2384 #endif /* NLS_BUGS */
2397 if (loginsh
&& do_logout
) {
2398 /* this is to send hangup signal to the develcon */
2399 /* we toggle DTR. clear dtr - sleep 1 - set dtr */
2400 /* ioctl will return ENOTTY for pty's but we ignore it */
2401 /* exitstat will run after disconnect */
2402 /* we sleep for 2 seconds to let things happen in */
2403 /* .logout and rechist() */
2406 (void) ioctl(FSHTTY
, TIOCCDTR
, NULL
);
2408 (void) ioctl(FSHTTY
, TIOCSDTR
, NULL
);
2409 #endif /* TIOCCDTR */
2414 struct process
*pp
, *np
;
2416 /* Kill all processes marked for hup'ing */
2417 for (pp
= proclist
.p_next
; pp
; pp
= pp
->p_next
) {
2420 if ((np
->p_flags
& PHUP
) && np
->p_jobid
!= shpgrp
) {
2421 if (killpg(np
->p_jobid
, SIGHUP
) != -1) {
2422 /* In case the job was suspended... */
2424 (void) killpg(np
->p_jobid
, SIGCONT
);
2429 while ((np
= np
->p_friends
) != pp
);
2435 * We need to call catclose, because SVR4 leaves symlinks behind otherwise
2436 * in the catalog directories. We cannot close on a vforked() child,
2437 * because messages will stop working on the parent too.
2440 (void) catclose(catd
);
2441 #endif /* NLS_CATALOGS */
2444 #endif /* WINNT_NATIVE */
2448 #ifndef _PATH_DEFPATH
2456 blkp
= blk
= (Char
**) xmalloc((size_t) sizeof(Char
*) * 10);
2460 *blkp
++ = Strsave(STRdot
);
2464 #define DIRAPPEND(a) \
2465 if (stat(ptr = a, &stb) == 0 && S_ISDIR(stb.st_mode)) \
2469 DIRAPPEND(_PATH_LOCAL
);
2473 DIRAPPEND(_PATH_USRUCB
);
2477 DIRAPPEND(_PATH_USRBSD
);
2481 DIRAPPEND(_PATH_BIN
);
2485 DIRAPPEND(_PATH_USRBIN
);
2492 *blkp
++ = Strsave(STRdot
);
2504 recdirs(NULL
, adrof(STRsavedirs
) != NULL
);
2505 rechist(NULL
, adrof(STRsavehist
) != NULL
);