1 /* $Header: /p/tcsh/cvsroot/tcsh/sh.c,v 3.136 2007/02/22 21:57:37 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("$tcsh: sh.c,v 3.136 2007/02/22 21:57:37 christos Exp $")
48 extern int MapsAreInited
;
49 extern int NLSMapsAreInited
;
54 * Bill Joy, UC Berkeley, California, USA
55 * October 1978, May 1980
57 * Jim Kulp, IIASA, Laxenburg, Austria
60 * Filename recognition added:
61 * Ken Greer, Ind. Consultant, Palo Alto CA
64 * Karl Kleinpaste, Computer Consoles, Inc.
65 * Added precmd, periodic/tperiod, prompt changes,
66 * directory stack hack, and login watch.
67 * Sometime March 1983 - Feb 1984.
69 * Added scheduled commands, including the "sched" command,
70 * plus the call to sched_run near the precmd et al
72 * Upgraded scheduled events for running events while
73 * sitting idle at command input.
75 * Paul Placeway, Ohio State
76 * added stuff for running with twenex/inputl 9 Oct 1984.
78 * ported to Apple Unix (TM) (OREO) 26 -- 29 Jun 1987
81 jmp_buf_t reslab INIT_ZERO_STRUCT
;
83 static const char tcshstr
[] = "tcsh";
85 struct sigaction parintr
; /* Parents interrupt catch */
86 struct sigaction parterm
; /* Parents terminate catch */
93 int use_fork
= 0; /* use fork() instead of vfork()? */
96 * Magic pointer values. Used to specify other invalid conditions aside
100 Char
*INVPTR
= &INVCHAR
;
101 Char
**INVPPTR
= &INVPTR
;
104 static int mflag
= 0;
105 static int prompt
= 1;
113 static time_t chktim
; /* Time mail last checked */
118 * This preserves the input state of the shell. It is used by
119 * st_save and st_restore to manupulate shell state.
128 struct whyle
*whyles
;
144 static int srccat (Char
*, Char
*);
146 static int srcfile (const char *, int, int, Char
**);
148 int srcfile (const char *, int, int, Char
**);
149 #endif /*WINNT_NATIVE*/
150 static void srcunit (int, int, int, Char
**);
151 static void mailchk (void);
152 #ifndef _PATH_DEFPATH
153 static Char
**defaultpath (void);
155 static void record (void);
156 static void st_save (struct saved_state
*, int, int,
158 static void st_restore (void *);
160 int main (int, char **);
163 main(int argc
, char **argv
)
166 volatile int nexececho
= 0;
168 volatile int nverbose
= 0;
169 volatile int rdirs
= 0;
179 struct sigaction oparintr
;
183 #endif /* WINNT_NATIVE */
184 #if defined(NLS_CATALOGS) && defined(LC_MESSAGES)
185 (void) setlocale(LC_MESSAGES
, "");
186 #endif /* NLS_CATALOGS && LC_MESSAGES */
190 (void) setlocale(LC_CTYPE
, ""); /* for iscntrl */
191 # endif /* LC_CTYPE */
197 mal_setstatsfile(fdopen(dmove(xopen("/tmp/tcsh.trace",
198 O_WRONLY
|O_CREAT
|O_LARGEFILE
, 0666), 25), "w"));
200 #endif /* MALLOC_TRACE */
202 #if !(defined(BSDTIMES) || defined(_SEQUENT_)) && defined(POSIX)
204 clk_tck
= (clock_t) sysconf(_SC_CLK_TCK
);
205 # else /* ! _SC_CLK_TCK */
208 # else /* !CLK_TCK */
210 # endif /* CLK_TCK */
211 # endif /* _SC_CLK_TCK */
212 #endif /* !BSDTIMES && POSIX */
214 settimes(); /* Immed. estab. timing base */
220 * Make sure we have 0, 1, 2 open
221 * Otherwise `` jobs will not work... (From knaff@poly.polytechnique.fr)
225 if ((f
= xopen(_PATH_DEVNULL
, O_RDONLY
|O_LARGEFILE
)) == -1 &&
226 (f
= xopen("/", O_RDONLY
|O_LARGEFILE
)) == -1)
236 osinit(); /* Os dependent initialization */
242 t
= strrchr(argv
[0], '/');
245 char *s
= strrchr(argv
[0], '\\');
249 #endif /* WINNT_NATIVE */
250 t
= t
? t
+ 1 : argv
[0];
252 progname
= strsave((t
&& *t
) ? t
: tcshstr
); /* never want a null */
253 tcsh
= strcmp(progname
, tcshstr
) == 0;
257 * Initialize non constant strings
260 STR_BSHELL
= SAVE(_PATH_BSHELL
);
263 STR_SHELLPATH
= SAVE(_PATH_TCSHELL
);
266 STR_SHELLPATH
= SAVE(_PATH_CSHELL
);
269 STR_environ
= blk2short(environ
);
270 environ
= short2blk(STR_environ
); /* So that we can free it */
271 STR_WORD_CHARS
= SAVE(WORD_CHARS
);
277 word_chars
= STR_WORD_CHARS
;
278 bslash_quote
= 0; /* PWP: do tcsh-style backslash quoting? */
280 /* Default history size to 100 */
281 setcopy(STRhistory
, str2short("100"), VAR_READWRITE
);
284 ffile
= SAVE(tempv
[0]);
286 if (eq(ffile
, STRaout
)) /* A.out's are quittable */
293 * We are a login shell if: 1. we were invoked as -<something> with
294 * optional arguments 2. or we were invoked only with the -l flag
296 loginsh
= (**tempv
== '-') || (argc
== 2 &&
297 tempv
[1][0] == '-' && tempv
[1][1] == 'l' &&
298 tempv
[1][2] == '\0');
300 /* No better way to find if we are a login shell */
302 loginsh
= (argc
== 1 && getppid() == 1);
303 **tempv
= '-'; /* Avoid giving VMS an acidic stomach */
305 #endif /* _VMS_POSIX */
307 if (loginsh
&& **tempv
!= '-') {
311 * Mangle the argv space
316 argv0
= strspl("-", *tempv
);
321 (void) time(&chktim
);
326 NoNLSRebind
= getenv("NOREBIND") != NULL
;
330 # endif /* SETLOCALEBUG */
331 (void) setlocale(LC_ALL
, "");
333 (void) setlocale(LC_COLLATE
, "");
337 # endif /* SETLOCALEBUG */
340 # endif /* STRCOLLBUG */
345 for (k
= 0200; k
<= 0377 && !Isprint(CTL_ESC(k
)); k
++)
347 AsciiOnly
= MB_CUR_MAX
== 1 && k
> 0377;
350 AsciiOnly
= getenv("LANG") == NULL
&& getenv("LC_CTYPE") == NULL
;
352 if (MapsAreInited
&& !NLSMapsAreInited
)
357 * Initialize for periodic command intervals. Also, initialize the dummy
358 * tty list for login-watch.
360 (void) time(&t_period
);
363 #endif /* !HAVENOUTMP */
367 * From: Jim Pace <jdp@research.att.com>
368 * tcsh does not work properly on the alliants through an rlogin session.
369 * The shell generally hangs. Also, reference to the controlling terminal
370 * does not work ( ie: echo foo > /dev/tty ).
372 * A security feature was added to rlogind affecting FX/80's Concentrix
373 * from revision 5.5.xx upwards (through 5.7 where this fix was implemented)
374 * This security change also affects the FX/2800 series.
375 * The security change to rlogind requires the process group of an rlogin
376 * session become disassociated with the tty in rlogind.
378 * The changes needed are:
379 * 1. set the process group
380 * 2. reenable the control terminal
382 if (loginsh
&& isatty(SHIN
)) {
383 ttyn
= ttyname(SHIN
);
385 SHIN
= xopen(ttyn
, O_RDWR
|O_LARGEFILE
);
387 (void) ioctl (SHIN
, TIOCSPGRP
, (ioctl_t
) &shpgrp
);
388 (void) setpgid(0, shpgrp
);
393 * Move the descriptors to safe places. The variable didfds is 0 while we
394 * have only FSH* to work with. When didfds is true, we have 0,1,2 and
395 * prefer to use these.
400 * Get and set the tty now
402 if ((ttyn
= ttyname(SHIN
)) != NULL
) {
404 * Could use rindex to get rid of other possible path components, but
405 * hpux preserves the subdirectory /pty/ when storing the tty name in
406 * utmp, so we keep it too.
408 if (strncmp(ttyn
, "/dev/", 5) == 0)
409 setv(STRtty
, cp
= SAVE(ttyn
+ 5), VAR_READWRITE
);
411 setv(STRtty
, cp
= SAVE(ttyn
), VAR_READWRITE
);
414 setv(STRtty
, cp
= SAVE(""), VAR_READWRITE
);
416 * Initialize the shell variables. ARGV and PROMPT are initialized later.
417 * STATUS is also munged in several places. CHILD is munged when
422 * 7-10-87 Paul Placeway autologout should be set ONLY on login shells and
423 * on shells running as root. Out of these, autologout should NOT be set
424 * for any psudo-terminals (this catches most window systems) and not for
425 * any terminal running X windows.
427 * At Ohio State, we have had problems with a user having his X session
428 * drop out from under him (on a Sun) because the shell in his master
429 * xterm timed out and exited.
431 * Really, this should be done with a program external to the shell, that
432 * watches for no activity (and NO running programs, such as dump) on a
433 * terminal for a long peroid of time, and then SIGHUPS the shell on that
436 * bugfix by Rich Salz <rsalz@PINEAPPLE.BBN.COM>: For root rsh things
437 * allways first check to see if loginsh or really root, then do things
440 * Also by Jean-Francois Lamy <lamy%ai.toronto.edu@RELAY.CS.NET>: check the
441 * value of cp before using it! ("root can rsh too")
443 * PWP: keep the nested ifs; the order of the tests matters and a good
444 * (smart) C compiler might re-arange things wrong.
449 /* root always has a 15 minute autologout */
450 setcopy(STRautologout
, STRrootdefautologout
, VAR_READWRITE
);
453 /* users get autologout set to 0 */
454 setcopy(STRautologout
, STR0
, VAR_READWRITE
);
456 if (loginsh
|| (uid
== 0)) {
458 /* only for login shells or root and we must have a tty */
459 if ((cp2
= Strrchr(cp
, (Char
) '/')) != NULL
) {
464 if (!(((Strncmp(cp2
, STRtty
, 3) == 0) && Isalpha(cp2
[3])) ||
465 ((Strncmp(cp
, STRpts
, 3) == 0) && cp
[3] == '/'))) {
466 if (getenv("DISPLAY") == NULL
) {
467 /* NOT on X window shells */
468 setcopy(STRautologout
, STRdefautologout
, VAR_READWRITE
);
474 #endif /* AUTOLOGOUT */
476 sigset_interrupting(SIGALRM
, queue_alrmcatch
);
478 setcopy(STRstatus
, STR0
, VAR_READWRITE
);
481 * get and set machine specific environment variables
487 * Publish the selected echo style
489 #if ECHO_STYLE != BSD_ECHO
491 # if ECHO_STYLE == NONE_ECHO
492 setcopy(STRecho_style
, STRnone
, VAR_READWRITE
);
493 # endif /* ECHO_STYLE == NONE_ECHO */
494 # if ECHO_STYLE == SYSV_ECHO
495 setcopy(STRecho_style
, STRsysv
, VAR_READWRITE
);
496 # endif /* ECHO_STYLE == SYSV_ECHO */
497 # if ECHO_STYLE == BOTH_ECHO
498 setcopy(STRecho_style
, STRboth
, VAR_READWRITE
);
499 # endif /* ECHO_STYLE == BOTH_ECHO */
501 #endif /* ECHO_STYLE != BSD_ECHO */
502 setcopy(STRecho_style
, STRbsd
, VAR_READWRITE
);
505 * increment the shell level.
509 if ((tcp
= getenv("HOME")) != NULL
)
510 cp
= quote(SAVE(tcp
));
515 fast
= 1; /* No home -> can't read scripts */
517 setv(STRhome
, cp
, VAR_READWRITE
);
519 dinit(cp
); /* dinit thinks that HOME == cwd in a login
522 * Grab other useful things from the environment. Should we grab
526 char *cln
, *cus
, *cgr
;
534 setv(STRoid
, Itoa(oid
, 0, 0), VAR_READWRITE
);
537 setv(STRuid
, Itoa(uid
, 0, 0), VAR_READWRITE
);
539 setv(STRgid
, Itoa(gid
, 0, 0), VAR_READWRITE
);
541 cln
= getenv("LOGNAME");
542 cus
= getenv("USER");
544 setv(STRuser
, quote(SAVE(cus
)), VAR_READWRITE
);
545 else if (cln
!= NULL
)
546 setv(STRuser
, quote(SAVE(cln
)), VAR_READWRITE
);
547 else if ((pw
= xgetpwuid(uid
)) == NULL
)
548 setcopy(STRuser
, str2short("unknown"), VAR_READWRITE
);
550 setcopy(STRuser
, str2short(pw
->pw_name
), VAR_READWRITE
);
552 tsetenv(STRLOGNAME
, varval(STRuser
));
554 tsetenv(STRKUSER
, varval(STRuser
));
556 cgr
= getenv("GROUP");
558 setv(STRgroup
, quote(SAVE(cgr
)), VAR_READWRITE
);
559 else if ((gr
= xgetgrgid(gid
)) == NULL
)
560 setcopy(STRgroup
, str2short("unknown"), VAR_READWRITE
);
562 setcopy(STRgroup
, str2short(gr
->gr_name
), VAR_READWRITE
);
564 tsetenv(STRKGROUP
, varval(STRgroup
));
568 * HOST may be wrong, since rexd transports the entire environment on sun
569 * 3.x Just set it again
572 char cbuff
[MAXHOSTNAMELEN
];
574 if (gethostname(cbuff
, sizeof(cbuff
)) >= 0) {
575 cbuff
[sizeof(cbuff
) - 1] = '\0'; /* just in case */
576 tsetenv(STRHOST
, str2short(cbuff
));
579 tsetenv(STRHOST
, str2short("unknown"));
585 * Try to determine the remote host we were logged in from.
588 #endif /* REMOTEHOST */
591 if ((tcp
= getenv("SYSTYPE")) == NULL
)
593 tsetenv(STRSYSTYPE
, quote(str2short(tcp
)));
597 * set editing on by default, unless running under Emacs as an inferior
599 * We try to do this intelligently. If $TERM is available, then it
600 * should determine if we should edit or not. $TERM is preserved
601 * across rlogin sessions, so we will not get confused if we rlogin
602 * under an emacs shell. Another advantage is that if we run an
603 * xterm under an emacs shell, then the $TERM will be set to
604 * xterm, so we are going to want to edit. Unfortunately emacs
605 * does not restore all the tty modes, so xterm is not very well
606 * set up. But this is not the shell's fault.
607 * Also don't edit if $TERM == wm, for when we're running under an ATK app.
608 * Finally, emacs compiled under terminfo, sets the terminal to dumb,
609 * so disable editing for that too.
611 * Unfortunately, in some cases the initial $TERM setting is "unknown",
612 * "dumb", or "network" which is then changed in the user's startup files.
613 * We fix this by setting noediting here if $TERM is unknown/dumb and
614 * if noediting is set, we switch on editing if $TERM is changed.
616 if ((tcp
= getenv("TERM")) != NULL
) {
617 setv(STRterm
, quote(SAVE(tcp
)), VAR_READWRITE
);
618 noediting
= strcmp(tcp
, "unknown") == 0 || strcmp(tcp
, "dumb") == 0 ||
619 strcmp(tcp
, "network") == 0;
620 editing
= strcmp(tcp
, "emacs") != 0 && strcmp(tcp
, "wm") != 0 &&
625 editing
= ((tcp
= getenv("EMACS")) == NULL
|| strcmp(tcp
, "t") != 0);
629 * The 'edit' variable is either set or unset. It doesn't
630 * need a value. Making it 'emacs' might be confusing.
637 * still more mutability: make the complete routine automatically add the
638 * suffix of file names...
643 * Compatibility with tcsh >= 6.12 by default
645 setNS(STRcsubstnonl
);
648 * Random default kill ring size
650 setcopy(STRkillring
, str2short("30"), VAR_READWRITE
);
653 * Re-initialize path if set in environment
655 if ((tcp
= getenv("PATH")) == NULL
)
657 importpath(str2short(_PATH_DEFPATH
));
658 #else /* !_PATH_DEFPATH */
659 setq(STRpath
, defaultpath(), &shvhed
, VAR_READWRITE
);
660 #endif /* _PATH_DEFPATH */
662 /* Importpath() allocates memory for the path, and the
663 * returned pointer from SAVE() was discarded, so
664 * this was a memory leak.. (sg)
666 * importpath(SAVE(tcp));
668 importpath(str2short(tcp
));
672 /* If the SHELL environment variable ends with "tcsh", set
673 * STRshell to the same path. This is to facilitate using
674 * the executable in environments where the compiled-in
675 * default isn't appropriate (sg).
680 if ((tcp
= getenv("SHELL")) != NULL
) {
681 sh_len
= strlen(tcp
);
682 if ((sh_len
>= 5 && strcmp(tcp
+ (sh_len
- 5), "/tcsh") == 0) ||
683 (!tcsh
&& sh_len
>= 4 && strcmp(tcp
+ (sh_len
- 4), "/csh") == 0))
684 setv(STRshell
, quote(SAVE(tcp
)), VAR_READWRITE
);
689 setcopy(STRshell
, STR_SHELLPATH
, VAR_READWRITE
);
692 #ifdef _OSD_POSIX /* BS2000 needs this variable set to "SHELL" */
693 if ((tcp
= getenv("PROGRAM_ENVIRONMENT")) == NULL
)
695 tsetenv(STRPROGRAM_ENVIRONMENT
, quote(str2short(tcp
)));
696 #endif /* _OSD_POSIX */
699 if ((tcp
= getenv("LS_COLORS")) != NULL
)
700 parseLS_COLORS(str2short(tcp
));
701 #endif /* COLOR_LS_F */
703 doldol
= putn((int) getpid()); /* For $$ */
708 if ((tmp
= getenv("TMP")) != NULL
) {
709 tmp
= xasprintf("%s/%s", tmp
, "sh");
716 shtemp
= Strspl(tmp2
, doldol
); /* For << */
719 #else /* !WINNT_NATIVE */
720 shtemp
= Strspl(STRtmpsh
, doldol
); /* For << */
721 #endif /* WINNT_NATIVE */
724 * Record the interrupt states from the parent process. If the parent is
725 * non-interruptible our hand must be forced or we (and our children) won't
726 * be either. Our children inherit termination from our parent. We catch it
727 * only if we are the login shell.
729 sigaction(SIGINT
, NULL
, &parintr
);
730 sigaction(SIGTERM
, NULL
, &parterm
);
734 /* Enable process migration on ourselves and our progeny */
735 (void) signal(SIGMIGRATE
, SIG_DFL
);
739 * dspkanji/dspmbyte autosetting
741 /* PATCH IDEA FROM Issei.Suzuki VERY THANKS */
742 #if defined(DSPMBYTE)
743 #if defined(NLS) && defined(LC_CTYPE)
744 if (((tcp
= setlocale(LC_CTYPE
, NULL
)) != NULL
|| (tcp
= getenv("LANG")) != NULL
) && !adrof(CHECK_MBYTEVAR
)) {
746 if ((tcp
= getenv("LANG")) != NULL
&& !adrof(CHECK_MBYTEVAR
)) {
748 autoset_dspmbyte(str2short(tcp
));
750 #if defined(WINNT_NATIVE)
751 else if (!adrof(CHECK_MBYTEVAR
))
752 nt_autoset_dspmbyte();
753 #endif /* WINNT_NATIVE */
756 fix_version(); /* publish the shell version */
758 if (argc
> 1 && strcmp(argv
[1], "--version") == 0) {
759 xprintf("%S\n", varval(STRversion
));
762 if (argc
> 1 && strcmp(argv
[1], "--help") == 0) {
763 xprintf("%S\n\n", varval(STRversion
));
764 xprintf(CGETS(11, 8, HELP_STRING
));
768 * Process the arguments.
770 * Note that processing of -v/-x is actually delayed till after script
773 * We set the first character of our name to be '-' if we are a shell
774 * running interruptible commands. Many programs which examine ps'es
775 * use this to filter such shells out.
778 while (argc
> 0 && (tcp
= tempv
[0])[0] == '-' &&
779 *++tcp
!= '\0' && !batch
) {
783 case 0: /* - Interruptible, no prompt */
789 case 'b': /* -b Next arg is input file */
793 case 'c': /* -c Command input from arg */
799 it relies on a 7 bit environment (/bin/sh), so it
800 pass ascii arguments with the 8th bit set */
801 if (!strcmp(argv
[0], "sh"))
805 for (p
= tempv
[0]; *p
; ++p
)
809 arginp
= SAVE(tempv
[0]);
812 * we put the command into a variable
815 setv(STRcommand
, quote(Strsave(arginp
)), VAR_READWRITE
);
818 * * Give an error on -c arguments that end in * backslash to
819 * ensure that you don't make * nonportable csh scripts.
826 while (cp
> arginp
&& *--cp
== '\\')
828 if ((count
& 1) != 0) {
836 case 'd': /* -d Load directory stack from file */
841 case 'D': /* -D Define environment variable */
846 if (dp
= Strchr(cp
, '=')) {
851 tsetenv(cp
, STRNULL
);
853 *tcp
= '\0'; /* done with this argument */
857 case 'e': /* -e Exit on any error */
861 case 'f': /* -f Fast start */
865 case 'i': /* -i Interactive, even if !intty */
870 case 'm': /* -m read .cshrc (from su) */
874 case 'n': /* -n Don't execute */
878 case 'q': /* -q (Undoc'd) ... die on quit */
882 case 's': /* -s Read from std input */
886 case 't': /* -t Read one line from input */
892 case 'v': /* -v Echo hist expanded input */
893 nverbose
= 1; /* ... later */
896 case 'x': /* -x Echo just before execution */
897 nexececho
= 1; /* ... later */
900 case 'V': /* -V Echo hist expanded input */
901 setNS(STRverbose
); /* NOW! */
904 case 'X': /* -X Echo just before execution */
905 setNS(STRecho
); /* NOW! */
910 * This will cause children to be created using fork instead of
919 * for O/S's that don't do the argument parsing right in
920 * "#!/foo -f " scripts
924 default: /* Unknown command option */
926 stderror(ERR_TCSHUSAGE
, tcp
-1, progname
);
933 if (quitit
) /* With all due haste, for debugging */
934 (void) signal(SIGQUIT
, SIG_DFL
);
937 * Unless prevented by -, -c, -i, -s, or -t, if there are remaining
938 * arguments the first of them is the name of a shell file from which to
941 if (nofile
== 0 && argc
> 0) {
942 nofile
= xopen(tempv
[0], O_RDONLY
|O_LARGEFILE
);
944 child
= 1; /* So this ... */
945 /* ... doesn't return */
946 stderror(ERR_SYSTEM
, tempv
[0], strerror(errno
));
949 setmode(nofile
, O_TEXT
);
953 ffile
= SAVE(tempv
[0]);
955 * Replace FSHIN. Handle /dev/std{in,out,err} specially
956 * since once they are closed we cannot open them again.
957 * In that case we use our own saved descriptors
959 if ((SHIN
= dmove(nofile
, FSHIN
)) < 0)
971 stderror(ERR_SYSTEM
, tempv
[0], strerror(errno
));
974 (void) close_on_exec(SHIN
, 1);
976 /* argc not used any more */ tempv
++;
980 * Call to closem() used to be part of initdesc(). Now called below where
981 * the script name argument has become stdin. Kernel may have used a file
982 * descriptor to hold the name of the script (setuid case) and this name
983 * mustn't be lost by closing the fd too soon.
988 * Consider input a tty if it really is or we are interactive. but not for
991 if (!(intty
= isatty(SHIN
))) {
998 if (intty
|| (intact
&& isatty(SHOUT
))) {
999 if (!batch
&& (uid
!= euid
|| gid
!= egid
)) {
1001 child
= 1; /* So this ... */
1002 /* ... doesn't return */
1003 stderror(ERR_SYSTEM
, progname
, strerror(errno
));
1007 isoutatty
= isatty(SHOUT
);
1008 isdiagatty
= isatty(SHDIAG
);
1010 * Decide whether we should play with signals or not. If we are explicitly
1011 * told (via -i, or -) or we are a login shell (arg0 starts with -) or the
1012 * input and output are both the ttys("csh", or "csh</dev/ttyx>/dev/ttyx")
1013 * Note that in only the login shell is it likely that parent may have set
1014 * signals to be ignored
1016 if (loginsh
|| intact
|| (intty
&& isatty(SHOUT
)))
1020 * Save the remaining arguments in argv.
1022 setq(STRargv
, blk2short(tempv
), &shvhed
, VAR_READWRITE
);
1025 * Set up the prompt.
1029 setcopy(STRprompt
, STRdeftcshprompt
, VAR_READWRITE
);
1031 setcopy(STRprompt
, STRdefcshprompt
, VAR_READWRITE
);
1032 /* that's a meta-questionmark */
1033 setcopy(STRprompt2
, STRmquestion
, VAR_READWRITE
);
1034 setcopy(STRprompt3
, STRKCORRECT
, VAR_READWRITE
);
1038 * If we are an interactive shell, then start fiddling with the signals;
1039 * this is a tricky game.
1041 shpgrp
= mygetpgrp();
1044 struct sigaction osig
;
1047 if (!quitit
) /* Wary! */
1048 (void) signal(SIGQUIT
, SIG_IGN
);
1050 sigset_interrupting(SIGINT
, queue_pintr
);
1051 (void) signal(SIGTERM
, SIG_IGN
);
1054 * No reason I can see not to save history on all these events..
1055 * Most usual occurrence is in a window system, where we're not a login
1056 * shell, but might as well be... (sg)
1057 * But there might be races when lots of shells exit together...
1058 * [this is also incompatible].
1059 * We have to be mre careful here. If the parent wants to
1060 * ignore the signals then we leave them untouched...
1061 * We also only setup the handlers for shells that are trully
1064 sigaction(SIGHUP
, NULL
, &osig
);
1065 if (loginsh
|| osig
.sa_handler
!= SIG_IGN
)
1066 /* exit processing on HUP */
1067 sigset_interrupting(SIGHUP
, queue_phup
);
1069 sigaction(SIGXCPU
, NULL
, &osig
);
1070 if (loginsh
|| osig
.sa_handler
!= SIG_IGN
)
1071 /* exit processing on XCPU */
1072 sigset_interrupting(SIGXCPU
, queue_phup
);
1075 sigaction(SIGXFSZ
, NULL
, &osig
);
1076 if (loginsh
|| osig
.sa_handler
!= SIG_IGN
)
1077 /* exit processing on XFSZ */
1078 sigset_interrupting(SIGXFSZ
, queue_phup
);
1081 if (quitit
== 0 && arginp
== 0) {
1083 (void) signal(SIGTSTP
, SIG_IGN
);
1086 (void) signal(SIGTTIN
, SIG_IGN
);
1089 (void) signal(SIGTTOU
, SIG_IGN
);
1092 * Wait till in foreground, in case someone stupidly runs csh &
1093 * dont want to try to grab away the tty.
1095 if (isatty(FSHDIAG
))
1097 else if (isatty(FSHOUT
))
1099 else if (isatty(OLDSTD
))
1105 /* NeXT 2.0 /usr/etc/rlogind, does not set our process group! */
1108 (void) setpgid(0, shpgrp
);
1109 (void) tcsetpgrp(f
, shpgrp
);
1112 #ifdef BSDJOBS /* if we have tty job control */
1114 if ((tpgrp
= tcgetpgrp(f
)) != -1) {
1115 if (tpgrp
!= shpgrp
) {
1116 struct sigaction old
;
1118 sigaction(SIGTTIN
, NULL
, &old
);
1119 signal(SIGTTIN
, SIG_DFL
);
1120 (void) kill(0, SIGTTIN
);
1121 sigaction(SIGTTIN
, &old
, NULL
);
1125 * Thanks to Matt Day for the POSIX references, and to
1126 * Paul Close for the SGI clarification.
1128 if (setdisc(f
) != -1) {
1132 if (tcsetpgrp(f
, shpgrp
) == -1) {
1134 * On hpux 7.03 this fails with EPERM. This happens on
1135 * the 800 when opgrp != shpgrp at this point. (we were
1136 * forked from a non job control shell)
1137 * POSIX 7.2.4, says we failed because the process
1138 * group specified did not belong to a process
1139 * in the same session with the tty. So we set our
1140 * process group and try again.
1142 if (setpgid(0, shpgrp
) == -1) {
1143 xprintf("setpgid:");
1146 if (tcsetpgrp(f
, shpgrp
) == -1) {
1147 xprintf("tcsetpgrp:");
1152 * We check the process group now. If it is the same, then
1153 * we don't need to set it again. On hpux 7.0 on the 300's
1154 * if we set it again it fails with EPERM. This is the
1155 * correct behavior according to POSIX 4.3.3 if the process
1156 * was a session leader .
1158 else if (shpgrp
!= mygetpgrp()) {
1159 if(setpgid(0, shpgrp
) == -1) {
1160 xprintf("setpgid:");
1166 * But on irix 3.3 we need to set it again, even if it is
1167 * the same. We do that to tell the system that we
1168 * need BSD process group compatibility.
1171 (void) setpgid(0, shpgrp
);
1173 (void) close_on_exec(dcopy(f
, FSHTTY
), 1);
1180 xprintf(CGETS(11, 1, "Warning: no access to tty (%s).\n"),
1182 xprintf(CGETS(11, 2, "Thus no job control in this shell.\n"));
1184 * Fix from:Sakari Jalovaara <sja@sirius.hut.fi> if we don't
1185 * have access to tty, disable editing too
1191 #else /* BSDJOBS */ /* don't have job control, so frotz it */
1193 #endif /* BSDJOBS */
1196 if (setintr
== 0 && parintr
.sa_handler
== SIG_DFL
)
1200 * SVR4 doesn't send a SIGCHLD when a child is stopped or continued if the
1201 * handler is installed with signal(2) or sigset(2). sigaction(2) must
1204 * David Dawes (dawes@physics.su.oz.au) Sept 1991
1206 sigset_interrupting(SIGCHLD
, queue_pchild
);
1208 if (intty
&& !arginp
)
1209 (void) ed_Setup(editing
);/* Get the tty state, and set defaults */
1210 /* Only alter the tty state if editing */
1213 * Set an exit here in case of an interrupt or error reading the shell
1218 (void)cleanup_push_mark(); /* There is no outer handler */
1219 if (setexit() != 0) /* PWP */
1224 haderr
= 0; /* In case second time through */
1225 if (!fast
&& reenter
== 0) {
1226 /* Will have varval(STRhome) here because set fast if don't */
1229 cleanup_push(&pintr_disabled
, disabled_cleanup
);
1230 setintr
= 0;/*FIXRESET:cleanup*/
1231 /* onintr in /etc/ files has no effect */
1232 parintr
.sa_handler
= SIG_IGN
;/*FIXRESET: cleanup*/
1234 #ifdef _PATH_DOTLOGIN
1236 (void) srcfile(_PATH_DOTLOGIN
, 0, 0, NULL
);
1240 #ifdef _PATH_DOTCSHRC
1241 (void) srcfile(_PATH_DOTCSHRC
, 0, 0, NULL
);
1243 if (!arginp
&& !onelflg
&& !havhash
)
1246 #ifdef _PATH_DOTLOGIN
1248 (void) srcfile(_PATH_DOTLOGIN
, 0, 0, NULL
);
1251 cleanup_until(&pintr_disabled
);
1257 (void) srccat(varval(STRhome
), STRsldotlogin
);
1259 /* upward compat. */
1260 if (!srccat(varval(STRhome
), STRsldottcshrc
))
1261 (void) srccat(varval(STRhome
), STRsldotcshrc
);
1263 if (!arginp
&& !onelflg
&& !havhash
)
1267 * Source history before .login so that it is available in .login
1272 (void) srccat(varval(STRhome
), STRsldotlogin
);
1274 if (loginsh
|| rdirs
)
1277 /* Reset interrupt flag */
1282 /* Initing AFTER .cshrc is the Right Way */
1283 if (intty
&& !arginp
) { /* PWP setup stuff */
1284 ed_Init(); /* init the new line editor */
1286 check_window_size(1); /* mung environment */
1287 #endif /* SIG_WINDOW */
1291 * Now are ready for the -v and -x flags
1299 * All the rest of the world is inside this call. The argument to process
1300 * indicates whether it should catch "error unwinds". Thus if we are a
1301 * interactive shell our call here will never return by being blown past on
1311 xprintf("logout\n");
1317 goodbye(NULL
, NULL
);
1332 if (tpgrp
> 0 && opgrp
!= shpgrp
) {
1333 (void) setpgid(0, opgrp
);
1334 (void) tcsetpgrp(FSHTTY
, opgrp
);
1335 (void) resetdisc(FSHTTY
);
1337 #endif /* BSDJOBS */
1341 importpath(Char
*cp
)
1348 for (dp
= cp
; *dp
; dp
++)
1352 * i+2 where i is the number of colons in the path. There are i+1
1353 * directories in the path plus we need room for a zero terminator.
1355 pv
= xcalloc(i
+ 2, sizeof(Char
*));
1360 if ((c
= *dp
) == PATHSEP
|| c
== 0) {
1362 pv
[i
++] = Strsave(*cp
? cp
: STRdot
);
1371 else if (*dp
== '\\')
1373 #endif /* WINNT_NATIVE */
1377 cleanup_push(pv
, blk_cleanup
);
1378 setq(STRpath
, pv
, &shvhed
, VAR_READWRITE
);
1384 * Source to the file which is the catenation of the argument names.
1387 srccat(Char
*cp
, Char
*dp
)
1389 if (cp
[0] == '/' && cp
[1] == '\0')
1390 return srcfile(short2str(dp
), (mflag
? 0 : 1), 0, NULL
);
1398 if (ep
!= cp
&& ep
[-1] == '/' && dp
[0] == '/') /* silly win95 */
1400 #endif /* WINNT_NATIVE */
1402 ep
= Strspl(cp
, dp
);
1403 cleanup_push(ep
, xfree
);
1404 ptr
= short2str(ep
);
1406 rv
= srcfile(ptr
, (mflag
? 0 : 1), 0, NULL
);
1413 * Source to a file putting the file descriptor in a safe place (> 2).
1415 #ifndef WINNT_NATIVE
1419 #endif /*WINNT_NATIVE*/
1420 srcfile(const char *f
, int onlyown
, int flag
, Char
**av
)
1424 if ((unit
= xopen(f
, O_RDONLY
|O_LARGEFILE
)) == -1)
1427 setmode(unit
, O_TEXT
);
1429 cleanup_push(&unit
, open_cleanup
);
1430 unit
= dmove(unit
, -1);
1431 cleanup_ignore(&unit
);
1432 cleanup_until(&unit
);
1434 (void) close_on_exec(unit
, 1);
1435 srcunit(unit
, onlyown
, flag
, av
);
1441 * Save the shell state, and establish new argument vector, and new input
1445 st_save(struct saved_state
*st
, int unit
, int hflg
, Char
**al
, Char
**av
)
1447 st
->insource
= insource
;
1449 /* Want to preserve the meaning of "source file >output".
1450 * Save old descriptors, move new 0,1,2 to safe places and assign
1451 * them to SH* and let process() redo 0,1,2 from them.
1453 * The macro returns true if d1 and d2 are good and they point to
1454 * different things. If you don't avoid saving duplicate
1455 * descriptors, you really limit the depth of "source" recursion
1456 * you can do because of all the open file descriptors. -IAN!
1458 #define NEED_SAVE_FD(d1,d2) \
1459 (fstat(d1, &s1) != -1 && fstat(d2, &s2) != -1 \
1460 && (s1.st_ino != s2.st_ino || s1.st_dev != s2.st_dev) )
1462 st
->OLDSTD
= st
->SHOUT
= st
->SHDIAG
= -1;/* test later to restore these */
1465 if (NEED_SAVE_FD(0,OLDSTD
)) {
1466 st
->OLDSTD
= OLDSTD
;
1467 OLDSTD
= dmove(0, -1);
1468 (void)close_on_exec(OLDSTD
, 1);
1470 if (NEED_SAVE_FD(1,SHOUT
)) {
1472 SHOUT
= dmove(1, -1);
1473 (void)close_on_exec(SHOUT
, 1);
1475 if (NEED_SAVE_FD(2,SHDIAG
)) {
1476 st
->SHDIAG
= SHDIAG
;
1477 SHDIAG
= dmove(2, -1);
1478 (void)close_on_exec(SHDIAG
, 1);
1484 st
->whyles
= whyles
;
1485 st
->gointr
= gointr
;
1486 st
->arginp
= arginp
;
1488 st
->evalvec
= evalvec
;
1489 st
->alvecp
= alvecp
;
1491 st
->onelflg
= onelflg
;
1492 st
->enterhist
= enterhist
;
1497 st
->cantell
= cantell
;
1501 * we can now pass arguments to source.
1502 * For compatibility we do that only if arguments were really
1503 * passed, otherwise we keep the old, global $argv like before.
1505 if (av
!= NULL
&& *av
!= NULL
) {
1507 if ((vp
= adrof(STRargv
)) != NULL
&& vp
->vec
!= NULL
)
1508 st
->argv
= saveblk(vp
->vec
);
1511 setq(STRargv
, saveblk(av
), &shvhed
, VAR_READWRITE
);
1517 SHIN
= unit
; /* Do this first */
1519 /* Establish new input arena */
1522 fseekp
= feobp
= fblocks
= 0;
1528 intty
= isatty(SHIN
);
1543 * Restore the shell to a saved state
1546 st_restore(void *xst
)
1548 struct saved_state
*st
;
1554 /* Reset input arena */
1557 Char
** nfbuf
= fbuf
;
1558 int nfblocks
= fblocks
;
1562 for (i
= 0; i
< nfblocks
; i
++)
1570 insource
= st
->insource
;
1572 if (st
->OLDSTD
!= -1)
1573 xclose(OLDSTD
), OLDSTD
= st
->OLDSTD
;
1574 if (st
->SHOUT
!= -1)
1575 xclose(SHOUT
), SHOUT
= st
->SHOUT
;
1576 if (st
->SHDIAG
!= -1)
1577 xclose(SHDIAG
), SHDIAG
= st
->SHDIAG
;
1578 arginp
= st
->arginp
;
1579 onelflg
= st
->onelflg
;
1581 evalvec
= st
->evalvec
;
1582 alvecp
= st
->alvecp
;
1585 whyles
= st
->whyles
;
1586 gointr
= st
->gointr
;
1587 if (st
->HIST
!= '\0')
1589 enterhist
= st
->enterhist
;
1590 cantell
= st
->cantell
;
1592 if (st
->argv
!= NULL
)
1593 setq(STRargv
, st
->argv
, &shvhed
, VAR_READWRITE
);
1594 else if (st
->av
!= NULL
&& *st
->av
!= NULL
&& adrof(STRargv
) != NULL
)
1599 * Source to a unit. If onlyown it must be our file or our group or
1600 * we don't chance it. This occurs on ".cshrc"s and the like.
1603 srcunit(int unit
, int onlyown
, int hflg
, Char
**av
)
1605 struct saved_state st
;
1607 st
.SHIN
= -1; /* st_restore checks this */
1615 if (fstat(unit
, &stb
) < 0) {
1621 /* Does nothing before st_save() because st.SHIN == -1 */
1622 cleanup_push(&st
, st_restore
);
1625 cleanup_push(&pintr_disabled
, disabled_cleanup
);
1628 /* Save the current state and move us to a new state */
1629 st_save(&st
, unit
, hflg
, NULL
, av
);
1632 * Now if we are allowing commands to be interrupted, we let ourselves be
1636 cleanup_until(&pintr_disabled
);
1638 cleanup_push(&pintr_disabled
, disabled_cleanup
);
1641 process(0); /* 0 -> blow away on errors */
1643 /* Restore the old state */
1650 goodbye(Char
**v
, struct command
*c
)
1660 signal(SIGQUIT
, SIG_IGN
);
1661 sigaddset(&set
, SIGQUIT
);
1662 sigprocmask(SIG_UNBLOCK
, &set
, NULL
);
1663 signal(SIGINT
, SIG_IGN
);
1664 sigaddset(&set
, SIGINT
);
1665 signal(SIGTERM
, SIG_IGN
);
1666 sigaddset(&set
, SIGTERM
);
1667 signal(SIGHUP
, SIG_IGN
);
1668 sigaddset(&set
, SIGHUP
);
1669 sigprocmask(SIG_UNBLOCK
, &set
, NULL
);
1671 setintr
= 0; /* No interrupts after "logout" */
1672 /* Trap errors inside .logout */
1673 omark
= cleanup_push_mark();
1674 if (setexit() == 0) {
1675 if (!(adrof(STRlogout
)))
1676 setcopy(STRlogout
, STRnormal
, VAR_READWRITE
);
1677 #ifdef _PATH_DOTLOGOUT
1678 (void) srcfile(_PATH_DOTLOGOUT
, 0, 0, NULL
);
1681 (void) srccat(varval(STRhome
), STRsldtlogout
);
1686 cleanup_pop_mark(omark
);
1698 * Note that if STATUS is corrupted (i.e. getn bombs) then error will exit
1699 * directly because we poke child here. Otherwise we might continue
1700 * unwarrantedly (sic).
1704 xexit(getn(varval(STRstatus
)));
1708 * in the event of a HUP we want to save the history
1714 setcopy(STRlogout
, STRhangup
, VAR_READWRITE
);
1715 #ifdef _PATH_DOTLOGOUT
1716 (void) srcfile(_PATH_DOTLOGOUT
, 0, 0, NULL
);
1719 (void) srccat(varval(STRhome
), STRsldtlogout
);
1726 * We kill the last foreground process group. It then becomes
1727 * responsible to propagate the SIGHUP to its progeny.
1730 struct process
*pp
, *np
;
1732 for (pp
= proclist
.p_next
; pp
; pp
= pp
->p_next
) {
1735 * Find if this job is in the foreground. It could be that
1736 * the process leader has exited and the foreground flag
1737 * is cleared for it.
1741 * If a process is in the foreground we try to kill
1742 * it's process group. If we succeed, then the
1743 * whole job is gone. Otherwise we keep going...
1744 * But avoid sending HUP to the shell again.
1746 if (((np
->p_flags
& PFOREGND
) != 0) && np
->p_jobid
!= shpgrp
) {
1747 np
->p_flags
&= ~PHUP
;
1748 if (killpg(np
->p_jobid
, SIGHUP
) != -1) {
1749 /* In case the job was suspended... */
1751 (void) killpg(np
->p_jobid
, SIGCONT
);
1756 while ((np
= np
->p_friends
) != pp
);
1759 #endif /* POSIXJOBS */
1764 static Char
*jobargv
[2] = {STRjobs
, 0};
1767 * Catch an interrupt, e.g. during lexical input.
1768 * If we are an interactive shell, we reset the interrupt catch
1769 * immediately. In any case we drain the shell output,
1770 * and finally go through the normal error mechanism, which
1771 * gets a chance to make the shell go away.
1773 int just_signaled
; /* bugfix by Michael Bloom (mg@ttidca.TTI.COM) */
1789 dojobs(jobargv
, NULL
);
1790 stderror(ERR_NAME
| ERR_INTR
);
1793 /* MH - handle interrupted completions specially */
1795 if (InsideCompletion
)
1796 stderror(ERR_SILENT
);
1798 /* JV - Make sure we shut off inputl */
1800 (void) Cookedmode();
1804 #ifdef HAVE_GETPWENT
1809 * If we have an active "onintr" then we search for the label. Note that if
1810 * one does "onintr -" then we shan't be interruptible so we needn't worry
1817 else if (intty
&& wantnl
) {
1820 * If we are editing a multi-line input command, and move to
1821 * the beginning of the line, we don't want to trash it when
1829 /* xputchar('\n'); *//* Some like this, others don't */
1830 (void) putraw('\r');
1831 (void) putraw('\n');
1834 stderror(ERR_SILENT
);
1838 * Process is the main driving routine for the shell.
1839 * It runs all command processing, except for those within { ... }
1840 * in expressions (which is run by a routine evalav in sh.exp.c which
1841 * is a stripped down process), and `...` evaluation which is run
1842 * also by a subset of this code in sh.glob.c in the routine backeval.
1844 * The code here is a little strange because part of it is interruptible
1845 * and hence freeing of structures appears to occur when none is necessary
1846 * if this is ignored.
1848 * Note that if catch is not set then we will unwind on any error.
1849 * If an end-of-file occurs, we return.
1855 /* PWP: This might get nuked my longjmp so don't make it a register var */
1859 omark
= cleanup_push_mark();
1863 int hadhist
, old_pintr_disabled
;
1868 justpr
= enterhist
; /* execute if not entering history */
1874 cleanup_pop_mark(omark
);
1880 * Every error is eventually caught here or the shell dies. It is
1881 * at this point that we clean up any left-over open files, by
1882 * closing all but a fixed number of pre-defined files. Thus
1883 * routines don't have to worry about leaving files open due to
1884 * deeper errors... they will get closed here.
1897 if (intty
&& prompt
&& evalvec
== 0) {
1901 * Watch for logins/logouts. Next is scheduled commands stored
1902 * previously using "sched." Then execute periodic commands.
1903 * Following that, the prompt precmd is run.
1907 #endif /* !HAVENOUTMP */
1912 * If we are at the end of the input buffer then we are going to
1913 * read fresh stuff. Otherwise, we are rereading input and don't
1914 * need or want to prompt.
1916 if (fseekp
== feobp
&& aret
== TCSH_F_SEEK
)
1917 printprompt(0, NULL
);
1927 * Interruptible during interactive reads
1930 pintr_push_enable(&old_pintr_disabled
);
1931 hadhist
= lex(¶ml
);
1933 cleanup_until(&old_pintr_disabled
);
1934 cleanup_push(¶ml
, lex_cleanup
);
1937 * Echo not only on VERBOSE, but also with history expansion. If there
1938 * is a lexical error then we forego history echo.
1940 if ((hadhist
&& !seterr
&& intty
&& !tellwhat
&& !Expand
&& !whyles
) ||
1941 adrof(STRverbose
)) {
1942 int odidfds
= didfds
;
1950 (void) alarm(0); /* Autologout OFF */
1951 alrmcatch_disabled
= 1;
1954 * Save input text on the history list if reading in old history, or it
1955 * is from the terminal at the top level and not in a loop.
1957 * PWP: entry of items in the history list while in a while loop is done
1960 if (enterhist
|| (catch && intty
&& !whyles
&& !tellwhat
&& !arun
))
1961 savehist(¶ml
, enterhist
> 1);
1963 if (Expand
&& seterr
)
1967 * Print lexical error messages, except when sourcing history lists.
1969 if (!enterhist
&& seterr
)
1973 * If had a history command :p modifier then this is as far as we
1980 * If had a tellwhat from twenex() then do
1983 (void) tellmewhat(¶ml
, NULL
);
1991 * If we are interactive, try to continue jobs that we have stopped
1994 continue_jobs(¶ml
);
1995 #endif /* BSDJOBS */
1998 * Check to see if the user typed "rm * .o" or something
2003 * Parse the words of the input into a parse tree.
2005 t
= syntax(paraml
.next
, ¶ml
, 0);
2006 cleanup_push(t
, syntax_cleanup
);
2012 * Execute the parse tree From: Michael Schroeder
2013 * <mlschroe@immd4.informatik.uni-erlangen.de> was execute(t, tpgrp);
2015 execute(t
, (tpgrp
> 0 ? tpgrp
: -1), NULL
, NULL
, TRUE
);
2021 if (windowchg
|| (catch && intty
&& !whyles
&& !tellwhat
)) {
2022 (void) check_window_size(0); /* for window systems */
2024 #endif /* SIG_WINDOW */
2025 setcopy(STR_
, InputBuf
, VAR_READWRITE
| VAR_NOGLOB
);
2027 cleanup_until(¶ml
);
2030 cleanup_pop_mark(omark
);
2036 dosource(Char
**t
, struct command
*c
)
2044 if (*t
&& eq(*t
, STRmh
)) {
2046 stderror(ERR_NAME
| ERR_HFLAG
);
2049 else if (*t
&& eq(*t
, STRmm
)) {
2051 stderror(ERR_NAME
| ERR_MFLAG
);
2055 f
= globone(*t
++, G_ERROR
);
2056 file
= strsave(short2str(f
));
2057 cleanup_push(file
, xfree
);
2059 t
= glob_all_or_error(t
);
2060 if ((!srcfile(file
, 0, hflg
, t
)) && (!hflg
) && (!bequiet
))
2061 stderror(ERR_SYSTEM
, file
, strerror(errno
));
2062 cleanup_until(file
);
2067 * If we are a login shell, then we don't want to tell
2068 * about any mail file unless its been modified
2069 * after the time we started.
2070 * This prevents us from telling the user things he already
2071 * knows, since the login program insists on saying
2077 * This version checks if the file is a directory, and if so,
2078 * tells you the number of files in it, otherwise do the old thang.
2079 * The magic "+1" in the time calculation is to compensate for
2080 * an AFS bug where directory mtimes are set to 1 second in
2095 if (v
== NULL
|| v
->vec
== NULL
)
2100 intvl
= (cnt
&& number(*vp
)) ? (--cnt
, getn(*vp
++)) : MAILINTVL
;
2103 if (chktim
+ intvl
> t
)
2106 char *filename
= short2str(*vp
);
2107 char *mboxdir
= filename
;
2109 if (stat(filename
, &stb
) < 0)
2111 #if defined(BSDTIMES) || defined(_SEQUENT_)
2112 new = stb
.st_mtime
> time0
.tv_sec
;
2114 new = stb
.st_mtime
> seconds0
;
2116 if (S_ISDIR(stb
.st_mode
)) {
2122 tempfilename
= xasprintf("%s/new", filename
);
2124 if (stat(tempfilename
, &stc
) != -1 && S_ISDIR(stc
.st_mode
)) {
2126 * "filename/new" exists and is a directory; you are
2130 #if defined(BSDTIMES) || defined(_SEQUENT_)
2131 new = stb
.st_mtime
> time0
.tv_sec
;
2133 new = stb
.st_mtime
> seconds0
;
2135 mboxdir
= tempfilename
;
2138 if (stb
.st_mtime
<= chktim
+ 1 || (loginsh
&& !new)) {
2139 xfree(tempfilename
);
2143 mailbox
= opendir(mboxdir
);
2144 xfree(tempfilename
);
2145 if (mailbox
== NULL
)
2149 if (!readdir(mailbox
) || !readdir(mailbox
))
2152 while (readdir(mailbox
))
2159 xprintf(CGETS(11, 3, "You have %d mail messages.\n"),
2162 xprintf(CGETS(11, 4, "You have %d mail messages in %s.\n"),
2163 mailcount
, filename
);
2168 if (stb
.st_size
== 0 || stb
.st_atime
> stb
.st_mtime
||
2169 (stb
.st_atime
<= chktim
&& stb
.st_mtime
<= chktim
) ||
2172 type
= strsave(new ? CGETS(11, 6, "new ") : "");
2173 cleanup_push(type
, xfree
);
2175 xprintf(CGETS(11, 5, "You have %smail.\n"), type
);
2177 xprintf(CGETS(11, 7, "You have %smail in %s.\n"), type
, filename
);
2178 cleanup_until(type
);
2185 * Extract a home directory from the password file
2186 * The argument points to a buffer where the name of the
2187 * user whose home directory is sought is currently.
2188 * We return home directory of the user, or NULL.
2191 gethdir(const Char
*home
)
2198 if (*home
== '\0') {
2199 if ((h
= varval(STRhome
)) != STRNULL
)
2208 if ((h
= gettilde(home
)) == NULL
)
2215 * Move the initial descriptors to their eventual
2216 * resting places, closing all other units.
2224 #endif /* NLS_CATALOGS */
2225 #endif /* NLS_BUGS */
2228 didfds
= 0; /* 0, 1, 2 aren't set up */
2229 (void) close_on_exec(SHIN
= dcopy(0, FSHIN
), 1);
2230 (void) close_on_exec(SHOUT
= dcopy(1, FSHOUT
), 1);
2231 (void) close_on_exec(SHDIAG
= dcopy(2, FSHDIAG
), 1);
2232 (void) close_on_exec(OLDSTD
= dcopy(SHIN
, FOLDSTD
), 1);
2233 #ifndef CLOSE_ON_EXEC
2234 didcch
= 0; /* Havent closed for child */
2235 #endif /* CLOSE_ON_EXEC */
2236 isdiagatty
= isatty(SHDIAG
);
2237 isoutatty
= isatty(SHOUT
);
2241 #endif /* NLS_CATALOGS */
2242 #endif /* NLS_BUGS */
2254 if (loginsh
&& do_logout
) {
2255 /* this is to send hangup signal to the develcon */
2256 /* we toggle DTR. clear dtr - sleep 1 - set dtr */
2257 /* ioctl will return ENOTTY for pty's but we ignore it */
2258 /* exitstat will run after disconnect */
2259 /* we sleep for 2 seconds to let things happen in */
2260 /* .logout and rechist() */
2263 (void) ioctl(FSHTTY
, TIOCCDTR
, NULL
);
2265 (void) ioctl(FSHTTY
, TIOCSDTR
, NULL
);
2266 #endif /* TIOCCDTR */
2271 struct process
*pp
, *np
;
2273 /* Kill all processes marked for hup'ing */
2274 for (pp
= proclist
.p_next
; pp
; pp
= pp
->p_next
) {
2277 if ((np
->p_flags
& PHUP
) && np
->p_jobid
!= shpgrp
) {
2278 if (killpg(np
->p_jobid
, SIGHUP
) != -1) {
2279 /* In case the job was suspended... */
2281 (void) killpg(np
->p_jobid
, SIGCONT
);
2286 while ((np
= np
->p_friends
) != pp
);
2292 * We need to call catclose, because SVR4 leaves symlinks behind otherwise
2293 * in the catalog directories. We cannot close on a vforked() child,
2294 * because messages will stop working on the parent too.
2298 #endif /* NLS_CATALOGS */
2301 #endif /* WINNT_NATIVE */
2305 #ifndef _PATH_DEFPATH
2313 blkp
= blk
= xmalloc(sizeof(Char
*) * 10);
2317 *blkp
++ = Strsave(STRdot
);
2321 #define DIRAPPEND(a) \
2322 if (stat(ptr = a, &stb) == 0 && S_ISDIR(stb.st_mode)) \
2326 DIRAPPEND(_PATH_LOCAL
);
2330 DIRAPPEND(_PATH_USRUCB
);
2334 DIRAPPEND(_PATH_USRBSD
);
2338 DIRAPPEND(_PATH_BIN
);
2342 DIRAPPEND(_PATH_USRBIN
);
2349 *blkp
++ = Strsave(STRdot
);
2361 recdirs(NULL
, adrof(STRsavedirs
) != NULL
);
2362 rechist(NULL
, adrof(STRsavehist
) != NULL
);