147823Sbostic /*-
260765Sbostic * Copyright (c) 1980, 1991, 1993
360765Sbostic * The Regents of the University of California. All rights reserved.
447823Sbostic *
547823Sbostic * %sccs.include.redist.c%
621925Sdist */
721925Sdist
817516Sedward #ifndef lint
960765Sbostic static char copyright[] =
1060765Sbostic "@(#) Copyright (c) 1980, 1991, 1993\n\
1160765Sbostic The Regents of the University of California. All rights reserved.\n";
1247823Sbostic #endif /* not lint */
131287Sbill
1447823Sbostic #ifndef lint
15*69126Schristos static char sccsid[] = "@(#)csh.c 8.4 (Berkeley) 04/29/95";
1647823Sbostic #endif /* not lint */
1747823Sbostic
1850028Sbostic #include <sys/types.h>
1950028Sbostic #include <sys/ioctl.h>
2050028Sbostic #include <sys/stat.h>
2150028Sbostic #include <fcntl.h>
2250028Sbostic #include <errno.h>
2350028Sbostic #include <pwd.h>
2450028Sbostic #include <stdlib.h>
2550028Sbostic #include <string.h>
2650028Sbostic #include <locale.h>
2750028Sbostic #include <unistd.h>
2851589Schristos #include <vis.h>
2950033Schristos #if __STDC__
3050033Schristos # include <stdarg.h>
3150033Schristos #else
3250033Schristos # include <varargs.h>
3350033Schristos #endif
3450033Schristos
3550023Sbostic #include "csh.h"
3664711Schristos #include "proc.h"
3750023Sbostic #include "extern.h"
3850028Sbostic #include "pathnames.h"
3936786Sbostic
4049992Sbostic extern bool MapsAreInited;
4149992Sbostic extern bool NLSMapsAreInited;
4249992Sbostic
431287Sbill /*
441287Sbill * C Shell
451287Sbill *
461287Sbill * Bill Joy, UC Berkeley, California, USA
471287Sbill * October 1978, May 1980
481287Sbill *
491287Sbill * Jim Kulp, IIASA, Laxenburg, Austria
501287Sbill * April 1980
5150023Sbostic *
5250023Sbostic * Christos Zoulas, Cornell University
5350023Sbostic * June, 1991
541287Sbill */
551287Sbill
5649992Sbostic Char *dumphist[] = {STRhistory, STRmh, 0, 0};
5752786Schristos Char *loadhist[] = {STRsource, STRmh, STRtildothist, 0};
581287Sbill
5949992Sbostic int nofile = 0;
6049992Sbostic bool reenter = 0;
6149992Sbostic bool nverbose = 0;
6249992Sbostic bool nexececho = 0;
6349992Sbostic bool quitit = 0;
6449992Sbostic bool fast = 0;
6549992Sbostic bool batch = 0;
6649992Sbostic bool mflag = 0;
6749992Sbostic bool prompt = 1;
6849992Sbostic bool enterhist = 0;
6949992Sbostic bool tellwhat = 0;
7026965Slepreau
7149992Sbostic extern char **environ;
7246649Sbostic
7350439Schristos static int readf __P((void *, char *, int));
7450439Schristos static fpos_t seekf __P((void *, fpos_t, int));
7550439Schristos static int writef __P((void *, const char *, int));
7650439Schristos static int closef __P((void *));
7750024Schristos static int srccat __P((Char *, Char *));
7850024Schristos static int srcfile __P((char *, bool, bool));
7950024Schristos static void phup __P((int));
8050024Schristos static void srcunit __P((int, bool, bool));
8150024Schristos static void mailchk __P((void));
8250024Schristos static Char **defaultpath __P((void));
8349992Sbostic
8449992Sbostic int
main(argc,argv)8545559Sbostic main(argc, argv)
8649992Sbostic int argc;
8749992Sbostic char **argv;
881287Sbill {
8949992Sbostic register Char *cp;
9049992Sbostic register char *tcp;
9149992Sbostic register int f;
9249992Sbostic register char **tempv;
9368576Schristos struct sigaction oact;
9468576Schristos sigset_t sigset;
951287Sbill
9650439Schristos cshin = stdin;
9750439Schristos cshout = stdout;
9850439Schristos csherr = stderr;
991287Sbill
10049992Sbostic settimes(); /* Immed. estab. timing base */
1011287Sbill
10249992Sbostic /*
10349992Sbostic * Initialize non constant strings
10449992Sbostic */
10549992Sbostic #ifdef _PATH_BSHELL
10649992Sbostic STR_BSHELL = SAVE(_PATH_BSHELL);
10749992Sbostic #endif
10849992Sbostic #ifdef _PATH_CSHELL
10949992Sbostic STR_SHELLPATH = SAVE(_PATH_CSHELL);
11049992Sbostic #endif
11149992Sbostic STR_environ = blk2short(environ);
11249992Sbostic environ = short2blk(STR_environ); /* So that we can free it */
11349992Sbostic STR_WORD_CHARS = SAVE(WORD_CHARS);
1141287Sbill
11549992Sbostic HIST = '!';
11649992Sbostic HISTSUB = '^';
11749992Sbostic word_chars = STR_WORD_CHARS;
1181287Sbill
11949992Sbostic tempv = argv;
12049992Sbostic if (eq(str2short(tempv[0]), STRaout)) /* A.out's are quittable */
12149992Sbostic quitit = 1;
12249992Sbostic uid = getuid();
12349992Sbostic gid = getgid();
12453387Schristos euid = geteuid();
12553387Schristos egid = getegid();
12649992Sbostic /*
12749992Sbostic * We are a login shell if: 1. we were invoked as -<something> and we had
12849992Sbostic * no arguments 2. or we were invoked only with the -l flag
12949992Sbostic */
13049992Sbostic loginsh = (**tempv == '-' && argc == 1) ||
13149992Sbostic (argc == 2 && tempv[1][0] == '-' && tempv[1][1] == 'l' &&
13249992Sbostic tempv[1][2] == '\0');
1331287Sbill
13449992Sbostic if (loginsh && **tempv != '-') {
1351287Sbill /*
13649992Sbostic * Mangle the argv space
1371287Sbill */
13849992Sbostic tempv[1][0] = '\0';
13949992Sbostic tempv[1][1] = '\0';
14049992Sbostic tempv[1] = NULL;
14151437Sleres for (tcp = *tempv; *tcp++;)
14251437Sleres continue;
14349992Sbostic for (tcp--; tcp >= *tempv; tcp--)
14449992Sbostic tcp[1] = tcp[0];
14549992Sbostic *++tcp = '-';
14649992Sbostic argc--;
14749992Sbostic }
14849992Sbostic if (loginsh)
14949992Sbostic (void) time(&chktim);
15039206Sbostic
15149992Sbostic AsciiOnly = 1;
15249992Sbostic #ifdef NLS
15349992Sbostic (void) setlocale(LC_ALL, "");
15449992Sbostic {
15549992Sbostic int k;
1561287Sbill
15751437Sleres for (k = 0200; k <= 0377 && !Isprint(k); k++)
15851437Sleres continue;
15949992Sbostic AsciiOnly = k > 0377;
16049992Sbostic }
16149992Sbostic #else
16249992Sbostic AsciiOnly = getenv("LANG") == NULL && getenv("LC_CTYPE") == NULL;
16349992Sbostic #endif /* NLS */
1641287Sbill
16549992Sbostic /*
16649992Sbostic * Move the descriptors to safe places. The variable didfds is 0 while we
16749992Sbostic * have only FSH* to work with. When didfds is true, we have 0,1,2 and
16849992Sbostic * prefer to use these.
16949992Sbostic */
17049992Sbostic initdesc();
17155361Smarc /*
17255361Smarc * XXX: This is to keep programs that use stdio happy.
17355361Smarc * what we really want is freunopen() ....
17455361Smarc * Closing cshin cshout and csherr (which are really stdin stdout
17555361Smarc * and stderr at this point and then reopening them in the same order
17655361Smarc * gives us again stdin == cshin stdout == cshout and stderr == csherr.
17755361Smarc * If that was not the case builtins like printf that use stdio
17855361Smarc * would break. But in any case we could fix that with memcpy and
17955361Smarc * a bit of pointer manipulation...
18055361Smarc * Fortunately this is not needed under the current implementation
18155361Smarc * of stdio.
18255361Smarc */
18350439Schristos (void) fclose(cshin);
18450439Schristos (void) fclose(cshout);
18550439Schristos (void) fclose(csherr);
18650439Schristos if (!(cshin = funopen((void *) &SHIN, readf, writef, seekf, closef)))
18750439Schristos exit(1);
18850439Schristos if (!(cshout = funopen((void *) &SHOUT, readf, writef, seekf, closef)))
18950439Schristos exit(1);
19050439Schristos if (!(csherr = funopen((void *) &SHERR, readf, writef, seekf, closef)))
19150439Schristos exit(1);
19250439Schristos (void) setvbuf(cshin, NULL, _IOLBF, 0);
19350439Schristos (void) setvbuf(cshout, NULL, _IOLBF, 0);
19450439Schristos (void) setvbuf(csherr, NULL, _IOLBF, 0);
19517170Sralph
19649992Sbostic /*
19749992Sbostic * Initialize the shell variables. ARGV and PROMPT are initialized later.
19849992Sbostic * STATUS is also munged in several places. CHILD is munged when
19949992Sbostic * forking/waiting
20049992Sbostic */
20149992Sbostic set(STRstatus, Strsave(STR0));
2021287Sbill
20350024Schristos if ((tcp = getenv("HOME")) != NULL)
20449992Sbostic cp = SAVE(tcp);
20549992Sbostic else
20649992Sbostic cp = NULL;
2071287Sbill
20849992Sbostic if (cp == NULL)
20949992Sbostic fast = 1; /* No home -> can't read scripts */
21049992Sbostic else
21149992Sbostic set(STRhome, cp);
21249992Sbostic dinit(cp); /* dinit thinks that HOME == cwd in a login
21349992Sbostic * shell */
21449992Sbostic /*
21549992Sbostic * Grab other useful things from the environment. Should we grab
21649992Sbostic * everything??
21749992Sbostic */
21850024Schristos if ((tcp = getenv("LOGNAME")) != NULL ||
21950024Schristos (tcp = getenv("USER")) != NULL)
22049992Sbostic set(STRuser, SAVE(tcp));
22150024Schristos if ((tcp = getenv("TERM")) != NULL)
22249992Sbostic set(STRterm, SAVE(tcp));
2231287Sbill
22449992Sbostic /*
22549992Sbostic * Re-initialize path if set in environment
22649992Sbostic */
22750024Schristos if ((tcp = getenv("PATH")) == NULL)
22849992Sbostic set1(STRpath, defaultpath(), &shvhed);
22949992Sbostic else
23049992Sbostic importpath(SAVE(tcp));
2311287Sbill
23249992Sbostic set(STRshell, Strsave(STR_SHELLPATH));
23336488Sbostic
23449992Sbostic doldol = putn((int) getpid()); /* For $$ */
23549992Sbostic shtemp = Strspl(STRtmpsh, doldol); /* For << */
2361287Sbill
23749992Sbostic /*
23849992Sbostic * Record the interrupt states from the parent process. If the parent is
23949992Sbostic * non-interruptible our hand must be forced or we (and our children) won't
24049992Sbostic * be either. Our children inherit termination from our parent. We catch it
24149992Sbostic * only if we are the login shell.
24249992Sbostic */
24349992Sbostic /* parents interruptibility */
24468576Schristos (void) sigaction(SIGINT, NULL, &oact);
24568576Schristos parintr = oact.sa_handler;
24668576Schristos (void) sigaction(SIGTERM, NULL, &oact);
24768576Schristos parterm = oact.sa_handler;
2481287Sbill
249*69126Schristos (void) signal(SIGHUP, phup); /* exit processing on HUP */
250*69126Schristos (void) signal(SIGXCPU, phup); /* ...and on XCPU */
251*69126Schristos (void) signal(SIGXFSZ, phup); /* ...and on XFSZ */
2521287Sbill
25349992Sbostic /*
25449992Sbostic * Process the arguments.
25551437Sleres *
25649992Sbostic * Note that processing of -v/-x is actually delayed till after script
25749992Sbostic * processing.
25851437Sleres *
25950439Schristos * We set the first character of our name to be '-' if we are a shell
26051437Sleres * running interruptible commands. Many programs which examine ps'es
26150439Schristos * use this to filter such shells out.
26249992Sbostic */
26349992Sbostic argc--, tempv++;
26449992Sbostic while (argc > 0 && (tcp = tempv[0])[0] == '-' && *++tcp != '\0' && !batch) {
26549992Sbostic do
26649992Sbostic switch (*tcp++) {
2671287Sbill
26849992Sbostic case 0: /* - Interruptible, no prompt */
26949992Sbostic prompt = 0;
27049992Sbostic setintr = 1;
27149992Sbostic nofile = 1;
27249992Sbostic break;
2731287Sbill
27449992Sbostic case 'b': /* -b Next arg is input file */
27549992Sbostic batch = 1;
27649992Sbostic break;
2771287Sbill
27849992Sbostic case 'c': /* -c Command input from arg */
27949992Sbostic if (argc == 1)
28049992Sbostic xexit(0);
28149992Sbostic argc--, tempv++;
28249992Sbostic arginp = SAVE(tempv[0]);
28349992Sbostic prompt = 0;
28449992Sbostic nofile = 1;
28549992Sbostic break;
2861287Sbill
28749992Sbostic case 'e': /* -e Exit on any error */
28849992Sbostic exiterr = 1;
28949992Sbostic break;
2901287Sbill
29149992Sbostic case 'f': /* -f Fast start */
29249992Sbostic fast = 1;
29349992Sbostic break;
2941287Sbill
29549992Sbostic case 'i': /* -i Interactive, even if !intty */
29649992Sbostic intact = 1;
29749992Sbostic nofile = 1;
29849992Sbostic break;
2991287Sbill
30049992Sbostic case 'm': /* -m read .cshrc (from su) */
30149992Sbostic mflag = 1;
30249992Sbostic break;
30349992Sbostic
30449992Sbostic case 'n': /* -n Don't execute */
30549992Sbostic noexec = 1;
30649992Sbostic break;
30749992Sbostic
30849992Sbostic case 'q': /* -q (Undoc'd) ... die on quit */
30949992Sbostic quitit = 1;
31049992Sbostic break;
31149992Sbostic
31249992Sbostic case 's': /* -s Read from std input */
31349992Sbostic nofile = 1;
31449992Sbostic break;
31549992Sbostic
31649992Sbostic case 't': /* -t Read one line from input */
31749992Sbostic onelflg = 2;
3181287Sbill prompt = 0;
31949992Sbostic nofile = 1;
32049992Sbostic break;
32149992Sbostic
32249992Sbostic case 'v': /* -v Echo hist expanded input */
32349992Sbostic nverbose = 1; /* ... later */
32449992Sbostic break;
32549992Sbostic
32649992Sbostic case 'x': /* -x Echo just before execution */
32749992Sbostic nexececho = 1; /* ... later */
32849992Sbostic break;
32949992Sbostic
33049992Sbostic case 'V': /* -V Echo hist expanded input */
33149992Sbostic setNS(STRverbose); /* NOW! */
33249992Sbostic break;
33349992Sbostic
33449992Sbostic case 'X': /* -X Echo just before execution */
33549992Sbostic setNS(STRecho); /* NOW! */
33649992Sbostic break;
33749992Sbostic
33849992Sbostic } while (*tcp);
33949992Sbostic tempv++, argc--;
34049992Sbostic }
34149992Sbostic
34249992Sbostic if (quitit) /* With all due haste, for debugging */
34349992Sbostic (void) signal(SIGQUIT, SIG_DFL);
34449992Sbostic
34549992Sbostic /*
34649992Sbostic * Unless prevented by -, -c, -i, -s, or -t, if there are remaining
34749992Sbostic * arguments the first of them is the name of a shell file from which to
34849992Sbostic * read commands.
34949992Sbostic */
35049992Sbostic if (nofile == 0 && argc > 0) {
35149992Sbostic nofile = open(tempv[0], O_RDONLY);
35249992Sbostic if (nofile < 0) {
35349992Sbostic child = 1; /* So this doesn't return */
35449992Sbostic stderror(ERR_SYSTEM, tempv[0], strerror(errno));
3551287Sbill }
35649992Sbostic ffile = SAVE(tempv[0]);
35751437Sleres /*
35849992Sbostic * Replace FSHIN. Handle /dev/std{in,out,err} specially
35949992Sbostic * since once they are closed we cannot open them again.
36049992Sbostic * In that case we use our own saved descriptors
36149992Sbostic */
36251437Sleres if ((SHIN = dmove(nofile, FSHIN)) < 0)
36349992Sbostic switch(nofile) {
36449992Sbostic case 0:
36549992Sbostic SHIN = FSHIN;
36649992Sbostic break;
36749992Sbostic case 1:
36849992Sbostic SHIN = FSHOUT;
36949992Sbostic break;
37049992Sbostic case 2:
37150439Schristos SHIN = FSHERR;
37249992Sbostic break;
37349992Sbostic default:
37449992Sbostic stderror(ERR_SYSTEM, tempv[0], strerror(errno));
37549992Sbostic break;
37649992Sbostic }
37750024Schristos (void) ioctl(SHIN, FIOCLEX, NULL);
37849992Sbostic prompt = 0;
37949992Sbostic /* argc not used any more */ tempv++;
38049992Sbostic }
38150439Schristos
38249992Sbostic intty = isatty(SHIN);
38349992Sbostic intty |= intact;
38449992Sbostic if (intty || (intact && isatty(SHOUT))) {
38553387Schristos if (!batch && (uid != euid || gid != egid)) {
38649992Sbostic errno = EACCES;
38749992Sbostic child = 1; /* So this doesn't return */
38849992Sbostic stderror(ERR_SYSTEM, "csh", strerror(errno));
38917170Sralph }
39049992Sbostic }
39149992Sbostic /*
39249992Sbostic * Decide whether we should play with signals or not. If we are explicitly
39349992Sbostic * told (via -i, or -) or we are a login shell (arg0 starts with -) or the
39449992Sbostic * input and output are both the ttys("csh", or "csh</dev/ttyx>/dev/ttyx")
39549992Sbostic * Note that in only the login shell is it likely that parent may have set
39649992Sbostic * signals to be ignored
39749992Sbostic */
39860237Schristos if (loginsh || intact || (intty && isatty(SHOUT)))
39949992Sbostic setintr = 1;
40049992Sbostic settell();
40149992Sbostic /*
40249992Sbostic * Save the remaining arguments in argv.
40349992Sbostic */
40449992Sbostic setq(STRargv, blk2short(tempv), &shvhed);
4051287Sbill
40649992Sbostic /*
40749992Sbostic * Set up the prompt.
40849992Sbostic */
40949992Sbostic if (prompt) {
41049992Sbostic set(STRprompt, Strsave(uid == 0 ? STRsymhash : STRsymcent));
41149992Sbostic /* that's a meta-questionmark */
41249992Sbostic set(STRprompt2, Strsave(STRmquestion));
41349992Sbostic }
4141287Sbill
41549992Sbostic /*
41649992Sbostic * If we are an interactive shell, then start fiddling with the signals;
41749992Sbostic * this is a tricky game.
41849992Sbostic */
41949992Sbostic shpgrp = getpgrp();
42049992Sbostic opgrp = tpgrp = -1;
42149992Sbostic if (setintr) {
42249992Sbostic **argv = '-';
42349992Sbostic if (!quitit) /* Wary! */
42449992Sbostic (void) signal(SIGQUIT, SIG_IGN);
42549992Sbostic (void) signal(SIGINT, pintr);
42668576Schristos sigemptyset(&sigset);
42768576Schristos sigaddset(&sigset, SIGINT);
42868576Schristos sigprocmask(SIG_BLOCK, &sigset, NULL);
42949992Sbostic (void) signal(SIGTERM, SIG_IGN);
43049992Sbostic if (quitit == 0 && arginp == 0) {
43149992Sbostic (void) signal(SIGTSTP, SIG_IGN);
43249992Sbostic (void) signal(SIGTTIN, SIG_IGN);
43349992Sbostic (void) signal(SIGTTOU, SIG_IGN);
43449992Sbostic /*
43549992Sbostic * Wait till in foreground, in case someone stupidly runs csh &
43649992Sbostic * dont want to try to grab away the tty.
43749992Sbostic */
43850439Schristos if (isatty(FSHERR))
43950439Schristos f = FSHERR;
44049992Sbostic else if (isatty(FSHOUT))
44149992Sbostic f = FSHOUT;
44249992Sbostic else if (isatty(OLDSTD))
44349992Sbostic f = OLDSTD;
44449992Sbostic else
44549992Sbostic f = -1;
44649992Sbostic retry:
44749992Sbostic if ((tpgrp = tcgetpgrp(f)) != -1) {
44849992Sbostic if (tpgrp != shpgrp) {
44949992Sbostic sig_t old = signal(SIGTTIN, SIG_DFL);
45049992Sbostic (void) kill(0, SIGTTIN);
45149992Sbostic (void) signal(SIGTTIN, old);
45249992Sbostic goto retry;
45349992Sbostic }
45449992Sbostic opgrp = shpgrp;
45549992Sbostic shpgrp = getpid();
45649992Sbostic tpgrp = shpgrp;
45749992Sbostic /*
45851437Sleres * Setpgid will fail if we are a session leader and
45949992Sbostic * mypid == mypgrp (POSIX 4.3.3)
46049992Sbostic */
46149992Sbostic if (opgrp != shpgrp)
46249992Sbostic if (setpgid(0, shpgrp) == -1)
46349992Sbostic goto notty;
46449992Sbostic /*
46549992Sbostic * We do that after we set our process group, to make sure
46649992Sbostic * that the process group belongs to a process in the same
46749992Sbostic * session as the tty (our process and our group) (POSIX 7.2.4)
46849992Sbostic */
46949992Sbostic if (tcsetpgrp(f, shpgrp) == -1)
47049992Sbostic goto notty;
47150024Schristos (void) ioctl(dcopy(f, FSHTTY), FIOCLEX, NULL);
47249992Sbostic }
47349992Sbostic if (tpgrp == -1) {
4741287Sbill notty:
47551437Sleres (void) fprintf(csherr, "Warning: no access to tty (%s).\n",
47650439Schristos strerror(errno));
47750439Schristos (void) fprintf(csherr, "Thus no job control in this shell.\n");
47849992Sbostic }
4791287Sbill }
48049992Sbostic }
48149992Sbostic if ((setintr == 0) && (parintr == SIG_DFL))
48249992Sbostic setintr = 1;
48349992Sbostic (void) signal(SIGCHLD, pchild); /* while signals not ready */
4841287Sbill
48549992Sbostic /*
48649992Sbostic * Set an exit here in case of an interrupt or error reading the shell
48749992Sbostic * start-up scripts.
48849992Sbostic */
48949992Sbostic reenter = setexit(); /* PWP */
49049992Sbostic haderr = 0; /* In case second time through */
49149992Sbostic if (!fast && reenter == 0) {
49249992Sbostic /* Will have value(STRhome) here because set fast if don't */
49349992Sbostic {
49449992Sbostic int osetintr = setintr;
49551523Schristos sig_t oparintr = parintr;
49668576Schristos sigset_t osigset;
49749992Sbostic
49868576Schristos sigemptyset(&sigset);
49968576Schristos sigaddset(&sigset, SIGINT);
50068576Schristos sigprocmask(SIG_BLOCK, &sigset, &osigset);
50168576Schristos
50249992Sbostic setintr = 0;
50351523Schristos parintr = SIG_IGN; /* Disable onintr */
50449992Sbostic #ifdef _PATH_DOTCSHRC
50549992Sbostic (void) srcfile(_PATH_DOTCSHRC, 0, 0);
50649992Sbostic #endif
50749992Sbostic if (!fast && !arginp && !onelflg)
50850439Schristos dohash(NULL, NULL);
50949992Sbostic #ifdef _PATH_DOTLOGIN
51049992Sbostic if (loginsh)
51149992Sbostic (void) srcfile(_PATH_DOTLOGIN, 0, 0);
51249992Sbostic #endif
51368576Schristos sigprocmask(SIG_SETMASK, &osigset, NULL);
51449992Sbostic setintr = osetintr;
51551523Schristos parintr = oparintr;
5161287Sbill }
51749992Sbostic (void) srccat(value(STRhome), STRsldotcshrc);
5181287Sbill
51949992Sbostic if (!fast && !arginp && !onelflg && !havhash)
52050439Schristos dohash(NULL, NULL);
52152786Schristos /*
52252786Schristos * Source history before .login so that it is available in .login
52352786Schristos */
52452786Schristos if ((cp = value(STRhistfile)) != STRNULL)
52552786Schristos loadhist[2] = cp;
52652786Schristos dosource(loadhist, NULL);
52749992Sbostic if (loginsh)
52849992Sbostic (void) srccat(value(STRhome), STRsldotlogin);
52949992Sbostic }
5301287Sbill
53149992Sbostic /*
53249992Sbostic * Now are ready for the -v and -x flags
53349992Sbostic */
53449992Sbostic if (nverbose)
53549992Sbostic setNS(STRverbose);
53649992Sbostic if (nexececho)
53749992Sbostic setNS(STRecho);
5381287Sbill
53949992Sbostic /*
54049992Sbostic * All the rest of the world is inside this call. The argument to process
54149992Sbostic * indicates whether it should catch "error unwinds". Thus if we are a
54249992Sbostic * interactive shell our call here will never return by being blown past on
54349992Sbostic * an error.
54449992Sbostic */
54549992Sbostic process(setintr);
54649992Sbostic
54749992Sbostic /*
54849992Sbostic * Mop-up.
54949992Sbostic */
55049992Sbostic if (intty) {
5511287Sbill if (loginsh) {
55250439Schristos (void) fprintf(cshout, "logout\n");
55349992Sbostic (void) close(SHIN);
55449992Sbostic child = 1;
55549992Sbostic goodbye();
5561287Sbill }
55749992Sbostic else {
55850439Schristos (void) fprintf(cshout, "exit\n");
55949992Sbostic }
56049992Sbostic }
56149992Sbostic rechist();
56249992Sbostic exitstat();
56349992Sbostic return (0);
5641287Sbill }
5651287Sbill
56649992Sbostic void
untty()5671287Sbill untty()
5681287Sbill {
56949992Sbostic if (tpgrp > 0) {
57049992Sbostic (void) setpgid(0, opgrp);
57149992Sbostic (void) tcsetpgrp(FSHTTY, opgrp);
57249992Sbostic }
5731287Sbill }
5741287Sbill
57549992Sbostic void
importpath(cp)5761287Sbill importpath(cp)
57749992Sbostic Char *cp;
5781287Sbill {
57949992Sbostic register int i = 0;
58049992Sbostic register Char *dp;
58149992Sbostic register Char **pv;
58249992Sbostic int c;
5831287Sbill
58449992Sbostic for (dp = cp; *dp; dp++)
58549992Sbostic if (*dp == ':')
58649992Sbostic i++;
58749992Sbostic /*
58849992Sbostic * i+2 where i is the number of colons in the path. There are i+1
58949992Sbostic * directories in the path plus we need room for a zero terminator.
59049992Sbostic */
59149992Sbostic pv = (Char **) xcalloc((size_t) (i + 2), sizeof(Char **));
59249992Sbostic dp = cp;
59349992Sbostic i = 0;
59449992Sbostic if (*dp)
5951287Sbill for (;;) {
59649992Sbostic if ((c = *dp) == ':' || c == 0) {
59749992Sbostic *dp = 0;
59853387Schristos if ((*cp != '/' || *cp == '\0') && (euid == 0 || uid == 0))
59953387Schristos (void) fprintf(csherr,
60053387Schristos "Warning: imported path contains relative components\n");
60149992Sbostic pv[i++] = Strsave(*cp ? cp : STRdot);
60249992Sbostic if (c) {
60349992Sbostic cp = dp + 1;
60449992Sbostic *dp = ':';
6051287Sbill }
60649992Sbostic else
60749992Sbostic break;
60849992Sbostic }
60949992Sbostic dp++;
6101287Sbill }
61149992Sbostic pv[i] = 0;
61249992Sbostic set1(STRpath, pv, &shvhed);
6131287Sbill }
6141287Sbill
6151287Sbill /*
6161287Sbill * Source to the file which is the catenation of the argument names.
6171287Sbill */
61849992Sbostic static int
srccat(cp,dp)6191287Sbill srccat(cp, dp)
62049992Sbostic Char *cp, *dp;
6211287Sbill {
62249992Sbostic register Char *ep = Strspl(cp, dp);
62349992Sbostic char *ptr = short2str(ep);
6241287Sbill
62549992Sbostic xfree((ptr_t) ep);
62649992Sbostic return srcfile(ptr, mflag ? 0 : 1, 0);
6271287Sbill }
6281287Sbill
6291287Sbill /*
63049992Sbostic * Source to a file putting the file descriptor in a safe place (> 2).
6311287Sbill */
63249992Sbostic static int
srcfile(f,onlyown,flag)63349992Sbostic srcfile(f, onlyown, flag)
63449992Sbostic char *f;
63549992Sbostic bool onlyown, flag;
63649992Sbostic {
63749992Sbostic register int unit;
63849992Sbostic
63949992Sbostic if ((unit = open(f, O_RDONLY)) == -1)
64049992Sbostic return 0;
64149992Sbostic unit = dmove(unit, -1);
64249992Sbostic
64350024Schristos (void) ioctl(unit, FIOCLEX, NULL);
64449992Sbostic srcunit(unit, onlyown, flag);
64549992Sbostic return 1;
64649992Sbostic }
64749992Sbostic
64849992Sbostic /*
64949992Sbostic * Source to a unit. If onlyown it must be our file or our group or
65049992Sbostic * we don't chance it. This occurs on ".cshrc"s and the like.
65149992Sbostic */
65249992Sbostic int insource;
65346649Sbostic static void
srcunit(unit,onlyown,hflg)6544946Smckusic srcunit(unit, onlyown, hflg)
65549992Sbostic register int unit;
65649992Sbostic bool onlyown, hflg;
6571287Sbill {
65849992Sbostic /* We have to push down a lot of state here */
65949992Sbostic /* All this could go into a structure */
66049992Sbostic int oSHIN = -1, oldintty = intty, oinsource = insource;
66149992Sbostic struct whyle *oldwhyl = whyles;
66249992Sbostic Char *ogointr = gointr, *oarginp = arginp;
66349992Sbostic Char *oevalp = evalp, **oevalvec = evalvec;
66449992Sbostic int oonelflg = onelflg;
66549992Sbostic bool oenterhist = enterhist;
66649992Sbostic char OHIST = HIST;
66749992Sbostic bool otell = cantell;
6681287Sbill
66949992Sbostic struct Bin saveB;
67068576Schristos sigset_t sigset, osigset;
67149992Sbostic jmp_buf oldexit;
6721287Sbill
67349992Sbostic /* The (few) real local variables */
67449992Sbostic int my_reenter;
6751287Sbill
67649992Sbostic if (unit < 0)
67749992Sbostic return;
67849992Sbostic if (didfds)
67949992Sbostic donefds();
68049992Sbostic if (onlyown) {
68149992Sbostic struct stat stb;
68249992Sbostic
68349992Sbostic if (fstat(unit, &stb) < 0) {
68449992Sbostic (void) close(unit);
68549992Sbostic return;
6861287Sbill }
68749992Sbostic }
6881287Sbill
68949992Sbostic /*
69049992Sbostic * There is a critical section here while we are pushing down the input
69149992Sbostic * stream since we have stuff in different structures. If we weren't
69249992Sbostic * careful an interrupt could corrupt SHIN's Bin structure and kill the
69349992Sbostic * shell.
69451437Sleres *
69549992Sbostic * We could avoid the critical region by grouping all the stuff in a single
69649992Sbostic * structure and pointing at it to move it all at once. This is less
69749992Sbostic * efficient globally on many variable references however.
69849992Sbostic */
69949992Sbostic insource = 1;
70049992Sbostic getexit(oldexit);
70149992Sbostic
70268576Schristos if (setintr) {
70368576Schristos sigemptyset(&sigset);
70468576Schristos sigaddset(&sigset, SIGINT);
70568576Schristos sigprocmask(SIG_BLOCK, &sigset, &osigset);
70668576Schristos }
70749992Sbostic /* Setup the new values of the state stuff saved above */
708*69126Schristos memmove(&saveB, &B, sizeof(B));
70950024Schristos fbuf = NULL;
71049992Sbostic fseekp = feobp = fblocks = 0;
71149992Sbostic oSHIN = SHIN, SHIN = unit, arginp = 0, onelflg = 0;
71249992Sbostic intty = isatty(SHIN), whyles = 0, gointr = 0;
71349992Sbostic evalvec = 0;
71449992Sbostic evalp = 0;
71549992Sbostic enterhist = hflg;
71649992Sbostic if (enterhist)
71749992Sbostic HIST = '\0';
71849992Sbostic
71949992Sbostic /*
72049992Sbostic * Now if we are allowing commands to be interrupted, we let ourselves be
72149992Sbostic * interrupted.
72249992Sbostic */
72349992Sbostic if (setintr)
72468576Schristos sigprocmask(SIG_SETMASK, &osigset, NULL);
72549992Sbostic settell();
7261287Sbill
72749992Sbostic if ((my_reenter = setexit()) == 0)
72849992Sbostic process(0); /* 0 -> blow away on errors */
7291287Sbill
73049992Sbostic if (setintr)
73168576Schristos sigprocmask(SIG_SETMASK, &osigset, NULL);
73249992Sbostic if (oSHIN >= 0) {
73349992Sbostic register int i;
7341287Sbill
73549992Sbostic /* We made it to the new state... free up its storage */
73649992Sbostic /* This code could get run twice but xfree doesn't care */
73749992Sbostic for (i = 0; i < fblocks; i++)
73849992Sbostic xfree((ptr_t) fbuf[i]);
73949992Sbostic xfree((ptr_t) fbuf);
74049992Sbostic
74149992Sbostic /* Reset input arena */
742*69126Schristos memmove(&B, &saveB, sizeof(B));
74349992Sbostic
74449992Sbostic (void) close(SHIN), SHIN = oSHIN;
74549992Sbostic arginp = oarginp, onelflg = oonelflg;
74649992Sbostic evalp = oevalp, evalvec = oevalvec;
74749992Sbostic intty = oldintty, whyles = oldwhyl, gointr = ogointr;
74849992Sbostic if (enterhist)
74949992Sbostic HIST = OHIST;
75049992Sbostic enterhist = oenterhist;
75149992Sbostic cantell = otell;
75249992Sbostic }
7531287Sbill
75449992Sbostic resexit(oldexit);
75549992Sbostic /*
75649992Sbostic * If process reset() (effectively an unwind) then we must also unwind.
75749992Sbostic */
75849992Sbostic if (my_reenter)
75949992Sbostic stderror(ERR_SILENT);
76049992Sbostic insource = oinsource;
7611287Sbill }
7621287Sbill
76349992Sbostic void
rechist()7644946Smckusic rechist()
7651287Sbill {
76652369Schristos Char buf[BUFSIZ], hbuf[BUFSIZ], *hfile;
76749992Sbostic int fp, ftmp, oldidfds;
76852369Schristos struct varent *shist;
7691287Sbill
77049992Sbostic if (!fast) {
77152369Schristos /*
77252369Schristos * If $savehist is just set, we use the value of $history
77352369Schristos * else we use the value in $savehist
77452369Schristos */
77560237Schristos if ((shist = adrof(STRsavehist)) != NULL) {
77652369Schristos if (shist->vec[0][0] != '\0')
77752369Schristos (void) Strcpy(hbuf, shist->vec[0]);
77852369Schristos else if ((shist = adrof(STRhistory)) && shist->vec[0][0] != '\0')
77952369Schristos (void) Strcpy(hbuf, shist->vec[0]);
78052369Schristos else
78152369Schristos return;
78252369Schristos }
78352369Schristos else
78452369Schristos return;
78552369Schristos
78652369Schristos if ((hfile = value(STRhistfile)) == STRNULL) {
78752369Schristos hfile = Strcpy(buf, value(STRhome));
78852369Schristos (void) Strcat(buf, STRsldthist);
78952369Schristos }
79052369Schristos
791*69126Schristos if ((fp = open(short2str(hfile), O_WRONLY | O_CREAT | O_TRUNC,
792*69126Schristos 0600)) == -1)
79352369Schristos return;
79452369Schristos
79549992Sbostic oldidfds = didfds;
79649992Sbostic didfds = 0;
79749992Sbostic ftmp = SHOUT;
79849992Sbostic SHOUT = fp;
79952369Schristos dumphist[2] = hbuf;
80050439Schristos dohist(dumphist, NULL);
80150439Schristos SHOUT = ftmp;
80249992Sbostic (void) close(fp);
80349992Sbostic didfds = oldidfds;
80449992Sbostic }
8054946Smckusic }
8064946Smckusic
80749992Sbostic void
goodbye()8084946Smckusic goodbye()
8094946Smckusic {
81049992Sbostic rechist();
81149992Sbostic
81249992Sbostic if (loginsh) {
81349992Sbostic (void) signal(SIGQUIT, SIG_IGN);
81449992Sbostic (void) signal(SIGINT, SIG_IGN);
81549992Sbostic (void) signal(SIGTERM, SIG_IGN);
81649992Sbostic setintr = 0; /* No interrupts after "logout" */
81749992Sbostic if (!(adrof(STRlogout)))
81849992Sbostic set(STRlogout, STRnormal);
81949992Sbostic #ifdef _PATH_DOTLOGOUT
82049992Sbostic (void) srcfile(_PATH_DOTLOGOUT, 0, 0);
82149992Sbostic #endif
82249992Sbostic if (adrof(STRhome))
82349992Sbostic (void) srccat(value(STRhome), STRsldtlogout);
82449992Sbostic }
82549992Sbostic exitstat();
8261287Sbill }
8271287Sbill
82849992Sbostic void
exitstat()8291287Sbill exitstat()
8301287Sbill {
83150439Schristos Char *s;
83217516Sedward #ifdef PROF
83349992Sbostic monitor(0);
83417516Sedward #endif
83549992Sbostic /*
83649992Sbostic * Note that if STATUS is corrupted (i.e. getn bombs) then error will exit
83749992Sbostic * directly because we poke child here. Otherwise we might continue
83849992Sbostic * unwarrantedly (sic).
83949992Sbostic */
84049992Sbostic child = 1;
84150439Schristos s = value(STRstatus);
84250439Schristos xexit(s ? getn(s) : 0);
8431287Sbill }
8441287Sbill
8454946Smckusic /*
8464946Smckusic * in the event of a HUP we want to save the history
8474946Smckusic */
84849992Sbostic static void
phup(sig)84950024Schristos phup(sig)
85050024Schristos int sig;
8514946Smckusic {
85249992Sbostic rechist();
85364711Schristos
85464711Schristos /*
85564711Schristos * We kill the last foreground process group. It then becomes
85664711Schristos * responsible to propagate the SIGHUP to its progeny.
85764711Schristos */
85864711Schristos {
85964711Schristos struct process *pp, *np;
86064711Schristos
86164711Schristos for (pp = proclist.p_next; pp; pp = pp->p_next) {
86264711Schristos np = pp;
86364711Schristos /*
86464711Schristos * Find if this job is in the foreground. It could be that
86564711Schristos * the process leader has exited and the foreground flag
86664711Schristos * is cleared for it.
86764711Schristos */
86864711Schristos do
86964711Schristos /*
87064711Schristos * If a process is in the foreground; we try to kill
87164711Schristos * it's process group. If we succeed, then the
87264711Schristos * whole job is gone. Otherwise we keep going...
87364711Schristos * But avoid sending HUP to the shell again.
87464711Schristos */
87564711Schristos if ((np->p_flags & PFOREGND) != 0 && np->p_jobid != shpgrp &&
87664711Schristos killpg(np->p_jobid, SIGHUP) != -1) {
87764711Schristos /* In case the job was suspended... */
87864711Schristos (void) killpg(np->p_jobid, SIGCONT);
87964711Schristos break;
88064711Schristos }
88164711Schristos while ((np = np->p_friends) != pp);
88264711Schristos }
88364711Schristos }
884*69126Schristos xexit(sig);
8854946Smckusic }
8864946Smckusic
88749992Sbostic Char *jobargv[2] = {STRjobs, 0};
88849992Sbostic
8891287Sbill /*
8901287Sbill * Catch an interrupt, e.g. during lexical input.
8911287Sbill * If we are an interactive shell, we reset the interrupt catch
8921287Sbill * immediately. In any case we drain the shell output,
8931287Sbill * and finally go through the normal error mechanism, which
8941287Sbill * gets a chance to make the shell go away.
8951287Sbill */
89650023Sbostic /* ARGSUSED */
89746657Sbostic void
pintr(notused)89850023Sbostic pintr(notused)
89950023Sbostic int notused;
9001287Sbill {
90149992Sbostic pintr1(1);
9023213Swnj }
9033213Swnj
90449992Sbostic void
pintr1(wantnl)9053213Swnj pintr1(wantnl)
90649992Sbostic bool wantnl;
9073213Swnj {
90850439Schristos Char **v;
90968576Schristos sigset_t sigset, osigset;
9101287Sbill
91168576Schristos sigemptyset(&sigset);
91268576Schristos sigprocmask(SIG_BLOCK, &sigset, &osigset);
91349992Sbostic if (setintr) {
91468576Schristos sigset = osigset;
91568576Schristos sigdelset(&sigset, SIGINT);
91668576Schristos sigprocmask(SIG_SETMASK, &sigset, NULL);
91749992Sbostic if (pjobs) {
91849992Sbostic pjobs = 0;
91950439Schristos (void) fprintf(cshout, "\n");
92050439Schristos dojobs(jobargv, NULL);
92149992Sbostic stderror(ERR_NAME | ERR_INTR);
9221287Sbill }
92349992Sbostic }
92468576Schristos sigdelset(&osigset, SIGCHLD);
92568576Schristos sigprocmask(SIG_SETMASK, &osigset, NULL);
92650439Schristos (void) fpurge(cshout);
92749992Sbostic (void) endpwent();
9281287Sbill
92949992Sbostic /*
93049992Sbostic * If we have an active "onintr" then we search for the label. Note that if
93149992Sbostic * one does "onintr -" then we shan't be interruptible so we needn't worry
93249992Sbostic * about that here.
93349992Sbostic */
93449992Sbostic if (gointr) {
93551420Schristos gotolab(gointr);
93649992Sbostic timflg = 0;
93760237Schristos if ((v = pargv) != NULL)
93849992Sbostic pargv = 0, blkfree(v);
93960237Schristos if ((v = gargv) != NULL)
94049992Sbostic gargv = 0, blkfree(v);
94149992Sbostic reset();
94249992Sbostic }
94349992Sbostic else if (intty && wantnl) {
94450439Schristos (void) fputc('\r', cshout);
94550439Schristos (void) fputc('\n', cshout);
94649992Sbostic }
94749992Sbostic stderror(ERR_SILENT);
9481287Sbill }
9491287Sbill
9501287Sbill /*
9511287Sbill * Process is the main driving routine for the shell.
9521287Sbill * It runs all command processing, except for those within { ... }
9531287Sbill * in expressions (which is run by a routine evalav in sh.exp.c which
9541287Sbill * is a stripped down process), and `...` evaluation which is run
9551287Sbill * also by a subset of this code in sh.glob.c in the routine backeval.
9561287Sbill *
9571287Sbill * The code here is a little strange because part of it is interruptible
9581287Sbill * and hence freeing of structures appears to occur when none is necessary
9591287Sbill * if this is ignored.
9601287Sbill *
9611287Sbill * Note that if catch is not set then we will unwind on any error.
9621287Sbill * If an end-of-file occurs, we return.
9631287Sbill */
96450439Schristos static struct command *savet = NULL;
96549992Sbostic void
process(catch)9661287Sbill process(catch)
96749992Sbostic bool catch;
9681287Sbill {
96949992Sbostic jmp_buf osetexit;
97050439Schristos struct command *t = savet;
97168576Schristos sigset_t sigset;
9721287Sbill
97350439Schristos savet = NULL;
97449992Sbostic getexit(osetexit);
97549992Sbostic for (;;) {
97649992Sbostic pendjob();
97749992Sbostic paraml.next = paraml.prev = ¶ml;
97849992Sbostic paraml.word = STRNULL;
97949992Sbostic (void) setexit();
98049992Sbostic justpr = enterhist; /* execute if not entering history */
9811287Sbill
98249992Sbostic /*
98349992Sbostic * Interruptible during interactive reads
98449992Sbostic */
98568576Schristos if (setintr) {
98668576Schristos sigemptyset(&sigset);
98768576Schristos sigaddset(&sigset, SIGINT);
98868576Schristos sigprocmask(SIG_UNBLOCK, &sigset, NULL);
98968576Schristos }
9901287Sbill
99149992Sbostic /*
99249992Sbostic * For the sake of reset()
99349992Sbostic */
99449992Sbostic freelex(¶ml);
99550439Schristos if (savet)
99650439Schristos freesyn(savet), savet = NULL;
9971287Sbill
99849992Sbostic if (haderr) {
99949992Sbostic if (!catch) {
100049992Sbostic /* unwind */
100149992Sbostic doneinp = 0;
100249992Sbostic resexit(osetexit);
100350439Schristos savet = t;
100449992Sbostic reset();
100549992Sbostic }
100649992Sbostic haderr = 0;
100749992Sbostic /*
100849992Sbostic * Every error is eventually caught here or the shell dies. It is
100949992Sbostic * at this point that we clean up any left-over open files, by
101049992Sbostic * closing all but a fixed number of pre-defined files. Thus
101149992Sbostic * routines don't have to worry about leaving files open due to
101249992Sbostic * deeper errors... they will get closed here.
101349992Sbostic */
101449992Sbostic closem();
101549992Sbostic continue;
101649992Sbostic }
101749992Sbostic if (doneinp) {
101849992Sbostic doneinp = 0;
101949992Sbostic break;
102049992Sbostic }
102149992Sbostic if (chkstop)
102249992Sbostic chkstop--;
102349992Sbostic if (neednote)
102449992Sbostic pnote();
102549992Sbostic if (intty && prompt && evalvec == 0) {
102649992Sbostic mailchk();
102749992Sbostic /*
102849992Sbostic * If we are at the end of the input buffer then we are going to
102949992Sbostic * read fresh stuff. Otherwise, we are rereading input and don't
103049992Sbostic * need or want to prompt.
103149992Sbostic */
103250944Schristos if (aret == F_SEEK && fseekp == feobp)
103349992Sbostic printprompt();
103450439Schristos (void) fflush(cshout);
103549992Sbostic }
103649992Sbostic if (seterr) {
103749992Sbostic xfree((ptr_t) seterr);
103850024Schristos seterr = NULL;
103949992Sbostic }
10401287Sbill
104149992Sbostic /*
104249992Sbostic * Echo not only on VERBOSE, but also with history expansion. If there
104349992Sbostic * is a lexical error then we forego history echo.
104449992Sbostic */
104560237Schristos if ((lex(¶ml) && !seterr && intty) || adrof(STRverbose)) {
104650439Schristos prlex(csherr, ¶ml);
104749992Sbostic }
10481287Sbill
104949992Sbostic /*
105049992Sbostic * The parser may lose space if interrupted.
105149992Sbostic */
105249992Sbostic if (setintr)
105368576Schristos sigprocmask(SIG_BLOCK, &sigset, NULL);
10541287Sbill
105549992Sbostic /*
105649992Sbostic * Save input text on the history list if reading in old history, or it
105749992Sbostic * is from the terminal at the top level and not in a loop.
105851437Sleres *
105949992Sbostic * PWP: entry of items in the history list while in a while loop is done
106049992Sbostic * elsewhere...
106149992Sbostic */
106260237Schristos if (enterhist || (catch && intty && !whyles))
106349992Sbostic savehist(¶ml);
10641287Sbill
106549992Sbostic /*
106649992Sbostic * Print lexical error messages, except when sourcing history lists.
106749992Sbostic */
106849992Sbostic if (!enterhist && seterr)
106949992Sbostic stderror(ERR_OLD);
10701287Sbill
107149992Sbostic /*
107249992Sbostic * If had a history command :p modifier then this is as far as we
107349992Sbostic * should go
107449992Sbostic */
107549992Sbostic if (justpr)
107649992Sbostic reset();
10771287Sbill
107849992Sbostic alias(¶ml);
10791287Sbill
108049992Sbostic /*
108149992Sbostic * Parse the words of the input into a parse tree.
108249992Sbostic */
108350439Schristos savet = syntax(paraml.next, ¶ml, 0);
108449992Sbostic if (seterr)
108549992Sbostic stderror(ERR_OLD);
10861287Sbill
108751437Sleres execute(savet, (tpgrp > 0 ? tpgrp : -1), NULL, NULL);
10881287Sbill
108949992Sbostic /*
109049992Sbostic * Made it!
109149992Sbostic */
109249992Sbostic freelex(¶ml);
109350439Schristos freesyn((struct command *) savet), savet = NULL;
109449992Sbostic }
109549992Sbostic resexit(osetexit);
109650439Schristos savet = t;
10971287Sbill }
10981287Sbill
109949992Sbostic void
110050439Schristos /*ARGSUSED*/
dosource(v,t)110150439Schristos dosource(v, t)
110250439Schristos Char **v;
110350439Schristos struct command *t;
110450439Schristos
11051287Sbill {
110649992Sbostic register Char *f;
110749992Sbostic bool hflg = 0;
110849992Sbostic Char buf[BUFSIZ];
11091287Sbill
111050439Schristos v++;
111150439Schristos if (*v && eq(*v, STRmh)) {
111250439Schristos if (*++v == NULL)
111349992Sbostic stderror(ERR_NAME | ERR_HFLAG);
111449992Sbostic hflg++;
111549992Sbostic }
111650439Schristos (void) Strcpy(buf, *v);
111749992Sbostic f = globone(buf, G_ERROR);
111849992Sbostic (void) strcpy((char *) buf, short2str(f));
111949992Sbostic xfree((ptr_t) f);
112049992Sbostic if (!srcfile((char *) buf, 0, hflg) && !hflg)
112149992Sbostic stderror(ERR_SYSTEM, (char *) buf, strerror(errno));
11221287Sbill }
11231287Sbill
11241287Sbill /*
11251287Sbill * Check for mail.
11261287Sbill * If we are a login shell, then we don't want to tell
11271287Sbill * about any mail file unless its been modified
11281287Sbill * after the time we started.
11291287Sbill * This prevents us from telling the user things he already
11301287Sbill * knows, since the login program insists on saying
11311287Sbill * "You have mail."
11321287Sbill */
113349992Sbostic static void
mailchk()11341287Sbill mailchk()
11351287Sbill {
113649992Sbostic register struct varent *v;
113749992Sbostic register Char **vp;
113849992Sbostic time_t t;
113949992Sbostic int intvl, cnt;
114049992Sbostic struct stat stb;
114149992Sbostic bool new;
11421287Sbill
114349992Sbostic v = adrof(STRmail);
114449992Sbostic if (v == 0)
114549992Sbostic return;
114649992Sbostic (void) time(&t);
114749992Sbostic vp = v->vec;
114849992Sbostic cnt = blklen(vp);
114949992Sbostic intvl = (cnt && number(*vp)) ? (--cnt, getn(*vp++)) : MAILINTVL;
115049992Sbostic if (intvl < 1)
115149992Sbostic intvl = 1;
115249992Sbostic if (chktim + intvl > t)
115349992Sbostic return;
115449992Sbostic for (; *vp; vp++) {
115549992Sbostic if (stat(short2str(*vp), &stb) < 0)
115649992Sbostic continue;
115749992Sbostic new = stb.st_mtime > time0.tv_sec;
115849992Sbostic if (stb.st_size == 0 || stb.st_atime > stb.st_mtime ||
115949992Sbostic (stb.st_atime < chktim && stb.st_mtime < chktim) ||
116060237Schristos (loginsh && !new))
116149992Sbostic continue;
116249992Sbostic if (cnt == 1)
116350439Schristos (void) fprintf(cshout, "You have %smail.\n", new ? "new " : "");
116449992Sbostic else
116551437Sleres (void) fprintf(cshout, "%s in %s.\n", new ? "New mail" : "Mail",
116651589Schristos vis_str(*vp));
116749992Sbostic }
116849992Sbostic chktim = t;
11691287Sbill }
11701287Sbill
11711287Sbill /*
11721287Sbill * Extract a home directory from the password file
11731287Sbill * The argument points to a buffer where the name of the
11741287Sbill * user whose home directory is sought is currently.
11751287Sbill * We write the home directory of the user back there.
11761287Sbill */
117749992Sbostic int
gethdir(home)11781287Sbill gethdir(home)
117949992Sbostic Char *home;
11801287Sbill {
118149992Sbostic Char *h;
118249992Sbostic struct passwd *pw;
11831287Sbill
118449992Sbostic /*
118549992Sbostic * Is it us?
118649992Sbostic */
118749992Sbostic if (*home == '\0') {
118860237Schristos if ((h = value(STRhome)) != NULL) {
118949992Sbostic (void) Strcpy(home, h);
119049992Sbostic return 0;
119149992Sbostic }
119249992Sbostic else
119349992Sbostic return 1;
119449992Sbostic }
119549992Sbostic
119660237Schristos if ((pw = getpwnam(short2str(home))) != NULL) {
119749992Sbostic (void) Strcpy(home, str2short(pw->pw_dir));
119849992Sbostic return 0;
119949992Sbostic }
120049992Sbostic else
120149992Sbostic return 1;
12021287Sbill }
12031287Sbill
12041287Sbill /*
120550439Schristos * When didfds is set, we do I/O from 0, 1, 2 otherwise from 15, 16, 17
120650460Schristos * We also check if the shell has already changed the decriptor to point to
120750439Schristos * 0, 1, 2 when didfds is set.
120850439Schristos */
120950439Schristos #define DESC(a) (*((int *) (a)) - (didfds && *((int *) a) >= FSHIN ? FSHIN : 0))
121050439Schristos
121150439Schristos static int
readf(oreo,buf,siz)121250439Schristos readf(oreo, buf, siz)
121350439Schristos void *oreo;
121450439Schristos char *buf;
121550439Schristos int siz;
121650439Schristos {
121750439Schristos return read(DESC(oreo), buf, siz);
121850439Schristos }
121950439Schristos
122050439Schristos
122150439Schristos static int
writef(oreo,buf,siz)122250439Schristos writef(oreo, buf, siz)
122350439Schristos void *oreo;
122450439Schristos const char *buf;
122550439Schristos int siz;
122650439Schristos {
122750439Schristos return write(DESC(oreo), buf, siz);
122850439Schristos }
122950439Schristos
123050439Schristos static fpos_t
seekf(oreo,off,whence)123150439Schristos seekf(oreo, off, whence)
123250439Schristos void *oreo;
123350439Schristos fpos_t off;
123450439Schristos int whence;
123550439Schristos {
123650439Schristos return lseek(DESC(oreo), off, whence);
123750439Schristos }
123850439Schristos
123950439Schristos
124050439Schristos static int
closef(oreo)124150439Schristos closef(oreo)
124250439Schristos void *oreo;
124350439Schristos {
124450439Schristos return close(DESC(oreo));
124550439Schristos }
124650439Schristos
124750439Schristos
124850439Schristos /*
124951589Schristos * Print the visible version of a string.
125051589Schristos */
125151589Schristos int
vis_fputc(ch,fp)125251589Schristos vis_fputc(ch, fp)
125351589Schristos int ch;
125451589Schristos FILE *fp;
125551589Schristos {
125651589Schristos char uenc[5]; /* 4 + NULL */
125751589Schristos
125851589Schristos if (ch & QUOTE)
125951589Schristos return fputc(ch & TRIM, fp);
126052369Schristos /*
126152369Schristos * XXX: When we are in AsciiOnly we want all characters >= 0200 to
126252369Schristos * be encoded, but currently there is no way in vis to do that.
126352369Schristos */
126452369Schristos (void) vis(uenc, ch & TRIM, VIS_NOSLASH, 0);
126551589Schristos return fputs(uenc, fp);
126651589Schristos }
126751589Schristos
126851589Schristos /*
12691287Sbill * Move the initial descriptors to their eventual
12701287Sbill * resting places, closin all other units.
12711287Sbill */
127249992Sbostic void
initdesc()12731287Sbill initdesc()
12741287Sbill {
12751287Sbill
127649992Sbostic didfds = 0; /* 0, 1, 2 aren't set up */
127750024Schristos (void) ioctl(SHIN = dcopy(0, FSHIN), FIOCLEX, NULL);
127850024Schristos (void) ioctl(SHOUT = dcopy(1, FSHOUT), FIOCLEX, NULL);
127950439Schristos (void) ioctl(SHERR = dcopy(2, FSHERR), FIOCLEX, NULL);
128050024Schristos (void) ioctl(OLDSTD = dcopy(SHIN, FOLDSTD), FIOCLEX, NULL);
128149992Sbostic closem();
12821287Sbill }
12831287Sbill
128449992Sbostic
128549992Sbostic void
128616679Ssam #ifdef PROF
done(i)128716679Ssam done(i)
128816679Ssam #else
128949992Sbostic xexit(i)
129016679Ssam #endif
129149992Sbostic int i;
12921287Sbill {
129349992Sbostic untty();
129449992Sbostic _exit(i);
129549992Sbostic }
12961287Sbill
129749992Sbostic static Char **
defaultpath()129849992Sbostic defaultpath()
129949992Sbostic {
130049992Sbostic char *ptr;
130149992Sbostic Char **blk, **blkp;
130249992Sbostic struct stat stb;
130349992Sbostic
130449992Sbostic blkp = blk = (Char **) xmalloc((size_t) sizeof(Char *) * 10);
130549992Sbostic
130649992Sbostic #define DIRAPPEND(a) \
130749992Sbostic if (stat(ptr = a, &stb) == 0 && (stb.st_mode & S_IFMT) == S_IFDIR) \
130849992Sbostic *blkp++ = SAVE(ptr)
130949992Sbostic
131049992Sbostic DIRAPPEND(_PATH_BIN);
131149992Sbostic DIRAPPEND(_PATH_USRBIN);
131249992Sbostic
131349992Sbostic #undef DIRAPPEND
131449992Sbostic
131553387Schristos if (euid != 0 && uid != 0)
131653387Schristos *blkp++ = Strsave(STRdot);
131750024Schristos *blkp = NULL;
131849992Sbostic return (blk);
13191287Sbill }
132015373Slayer
132149992Sbostic void
printprompt()132216677Ssam printprompt()
132315373Slayer {
132449992Sbostic register Char *cp;
132516677Ssam
132649992Sbostic if (!whyles) {
132749992Sbostic for (cp = value(STRprompt); *cp; cp++)
132849992Sbostic if (*cp == HIST)
132950439Schristos (void) fprintf(cshout, "%d", eventno + 1);
133049992Sbostic else {
133149992Sbostic if (*cp == '\\' && cp[1] == HIST)
133249992Sbostic cp++;
133351589Schristos (void) vis_fputc(*cp | QUOTE, cshout);
133449992Sbostic }
133549992Sbostic }
133649992Sbostic else
133749992Sbostic /*
133849992Sbostic * Prompt for forward reading loop body content.
133949992Sbostic */
134050439Schristos (void) fprintf(cshout, "? ");
134150439Schristos (void) fflush(cshout);
134215373Slayer }
1343