/*- * Copyright (c) 1991, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Kenneth Almquist. * * %sccs.include.redist.c% */ #ifndef lint static char copyright[] = "@(#) Copyright (c) 1991, 1993\n\ The Regents of the University of California. All rights reserved.\n"; #endif /* not lint */ #ifndef lint static char sccsid[] = "@(#)main.c 8.5 (Berkeley) 05/19/95"; #endif /* not lint */ #include #include #include #include #include #include "shell.h" #include "main.h" #include "mail.h" #include "options.h" #include "output.h" #include "parser.h" #include "nodes.h" #include "eval.h" #include "jobs.h" #include "input.h" #include "trap.h" #include "var.h" #include "show.h" #include "memalloc.h" #include "error.h" #include "init.h" #include "mystring.h" #include "exec.h" #define PROFILE 0 int rootpid; int rootshell; STATIC union node *curcmd; STATIC union node *prevcmd; extern int errno; #if PROFILE short profile_buf[16384]; extern int etext(); #endif STATIC void read_profile __P((char *)); STATIC char *find_dot_file __P((char *)); /* * Main routine. We initialize things, parse the arguments, execute * profiles if we're a login shell, and then call cmdloop to execute * commands. The setjmp call sets up the location to jump to when an * exception occurs. When an exception occurs the variable "state" * is used to figure out how far we had gotten. */ int main(argc, argv) int argc; char **argv; { struct jmploc jmploc; struct stackmark smark; volatile int state; char *shinit; #if PROFILE monitor(4, etext, profile_buf, sizeof profile_buf, 50); #endif state = 0; if (setjmp(jmploc.loc)) { /* * When a shell procedure is executed, we raise the * exception EXSHELLPROC to clean up before executing * the shell procedure. */ if (exception == EXERROR) exitstatus = 2; if (exception == EXSHELLPROC) { rootpid = getpid(); rootshell = 1; minusc = NULL; state = 3; } else if (state == 0 || iflag == 0 || ! rootshell) exitshell(2); reset(); if (exception == EXINT #if ATTY && (! attyset() || equal(termval(), "emacs")) #endif ) { out2c('\n'); flushout(&errout); } popstackmark(&smark); FORCEINTON; /* enable interrupts */ if (state == 1) goto state1; else if (state == 2) goto state2; else if (state == 3) goto state3; else goto state4; } handler = &jmploc; #ifdef DEBUG opentrace(); trputs("Shell args: "); trargs(argv); #endif rootpid = getpid(); rootshell = 1; init(); setstackmark(&smark); procargs(argc, argv); if (argv[0] && argv[0][0] == '-') { state = 1; read_profile("/etc/profile"); state1: state = 2; read_profile(".profile"); } state2: state = 3; if (getuid() == geteuid() && getgid() == getegid()) { if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') { state = 3; read_profile(shinit); } } state3: state = 4; if (minusc) { evalstring(minusc); } if (sflag || minusc == NULL) { state4: /* XXX ??? - why isn't this before the "if" statement */ cmdloop(1); } #if PROFILE monitor(0); #endif exitshell(exitstatus); /*NOTREACHED*/ return 0; } /* * Read and execute commands. "Top" is nonzero for the top level command * loop; it turns on prompting if the shell is interactive. */ void cmdloop(top) int top; { union node *n; struct stackmark smark; int inter; int numeof = 0; TRACE(("cmdloop(%d) called\n", top)); setstackmark(&smark); for (;;) { if (pendingsigs) dotrap(); inter = 0; if (iflag && top) { inter++; showjobs(1); chkmail(0); flushout(&output); } n = parsecmd(inter); /* showtree(n); DEBUG */ if (n == NEOF) { if (!top || numeof >= 50) break; if (!stoppedjobs()) { if (!Iflag) break; out2str("\nUse \"exit\" to leave shell.\n"); } numeof++; } else if (n != NULL && nflag == 0) { job_warning = (job_warning == 2) ? 1 : 0; numeof = 0; evaltree(n, 0); } popstackmark(&smark); } popstackmark(&smark); /* unnecessary */ } /* * Read /etc/profile or .profile. Return on error. */ STATIC void read_profile(name) char *name; { int fd; INTOFF; if ((fd = open(name, O_RDONLY)) >= 0) setinputfd(fd, 1); INTON; if (fd < 0) return; cmdloop(0); popfile(); } /* * Read a file containing shell functions. */ void readcmdfile(name) char *name; { int fd; INTOFF; if ((fd = open(name, O_RDONLY)) >= 0) setinputfd(fd, 1); else error("Can't open %s", name); INTON; cmdloop(0); popfile(); } /* * Take commands from a file. To be compatable we should do a path * search for the file, which is necessary to find sub-commands. */ STATIC char * find_dot_file(basename) char *basename; { static char localname[FILENAME_MAX+1]; char *fullname; char *path = pathval(); struct stat statb; /* don't try this for absolute or relative paths */ if( strchr(basename, '/')) return basename; while ((fullname = padvance(&path, basename)) != NULL) { strcpy(localname, fullname); stunalloc(fullname); if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) return localname; } return basename; } int dotcmd(argc, argv) int argc; char **argv; { exitstatus = 0; if (argc >= 2) { /* That's what SVR2 does */ char *fullname = find_dot_file(argv[1]); setinputfile(fullname, 1); commandname = fullname; cmdloop(0); popfile(); } return exitstatus; } int exitcmd(argc, argv) int argc; char **argv; { if (stoppedjobs()) return 0; if (argc > 1) exitstatus = number(argv[1]); exitshell(exitstatus); /*NOTREACHED*/ return 0; } #ifdef notdef /* * Should never be called. */ void exit(exitstatus) { _exit(exitstatus); } #endif