147125Sbostic /*- 247125Sbostic * Copyright (c) 1991 The Regents of the University of California. 347125Sbostic * All rights reserved. 447125Sbostic * 547125Sbostic * This code is derived from software contributed to Berkeley by 647125Sbostic * Kenneth Almquist. 747125Sbostic * 847125Sbostic * %sccs.include.redist.c% 947125Sbostic */ 1047125Sbostic 1147125Sbostic #ifndef lint 1247125Sbostic char copyright[] = 1347125Sbostic "@(#) Copyright (c) 1991 The Regents of the University of California.\n\ 1447125Sbostic All rights reserved.\n"; 1547125Sbostic #endif /* not lint */ 1647125Sbostic 1747125Sbostic #ifndef lint 18*55283Smarc static char sccsid[] = "@(#)main.c 5.7 (Berkeley) 07/16/92"; 1947125Sbostic #endif /* not lint */ 2047125Sbostic 2147125Sbostic #include <signal.h> 2247125Sbostic #include <fcntl.h> 2347125Sbostic #include "shell.h" 2447125Sbostic #include "main.h" 2547125Sbostic #include "mail.h" 2647125Sbostic #include "options.h" 2747125Sbostic #include "output.h" 2847125Sbostic #include "parser.h" 2947125Sbostic #include "nodes.h" 3047125Sbostic #include "eval.h" 3147125Sbostic #include "jobs.h" 3247125Sbostic #include "input.h" 3347125Sbostic #include "trap.h" 3447125Sbostic #include "var.h" 3547125Sbostic #include "memalloc.h" 3647125Sbostic #include "error.h" 3747125Sbostic #include "init.h" 3847125Sbostic #include "mystring.h" 3947125Sbostic 4047125Sbostic #define PROFILE 0 4147125Sbostic 4247125Sbostic int rootpid; 4347125Sbostic int rootshell; 4447125Sbostic STATIC union node *curcmd; 4547125Sbostic STATIC union node *prevcmd; 4647125Sbostic extern int errno; 4747125Sbostic #if PROFILE 4847125Sbostic short profile_buf[16384]; 4947125Sbostic extern int etext(); 5047125Sbostic #endif 5147125Sbostic 5247125Sbostic #ifdef __STDC__ 5347125Sbostic STATIC void read_profile(char *); 5447125Sbostic char *getenv(char *); 5547125Sbostic #else 5647125Sbostic STATIC void read_profile(); 5747125Sbostic char *getenv(); 5847125Sbostic #endif 5947125Sbostic 6047125Sbostic 6147125Sbostic /* 6247125Sbostic * Main routine. We initialize things, parse the arguments, execute 6347125Sbostic * profiles if we're a login shell, and then call cmdloop to execute 6447125Sbostic * commands. The setjmp call sets up the location to jump to when an 6547125Sbostic * exception occurs. When an exception occurs the variable "state" 6647125Sbostic * is used to figure out how far we had gotten. 6747125Sbostic */ 6847125Sbostic 6947125Sbostic main(argc, argv) char **argv; { 7047125Sbostic struct jmploc jmploc; 7147125Sbostic struct stackmark smark; 7247125Sbostic volatile int state; 7347125Sbostic char *shinit; 7447125Sbostic 7547125Sbostic #if PROFILE 7647125Sbostic monitor(4, etext, profile_buf, sizeof profile_buf, 50); 7747125Sbostic #endif 7847125Sbostic state = 0; 7947125Sbostic if (setjmp(jmploc.loc)) { 8047125Sbostic /* 8147125Sbostic * When a shell procedure is executed, we raise the 8247125Sbostic * exception EXSHELLPROC to clean up before executing 8347125Sbostic * the shell procedure. 8447125Sbostic */ 8547125Sbostic if (exception == EXSHELLPROC) { 8647125Sbostic rootpid = getpid(); 8747125Sbostic rootshell = 1; 8847125Sbostic minusc = NULL; 8947125Sbostic state = 3; 9047125Sbostic } else if (state == 0 || iflag == 0 || ! rootshell) 9147125Sbostic exitshell(2); 9247125Sbostic reset(); 9347125Sbostic #if ATTY 9447125Sbostic if (exception == EXINT 9547125Sbostic && (! attyset() || equal(termval(), "emacs"))) { 9647125Sbostic #else 9747125Sbostic if (exception == EXINT) { 9847125Sbostic #endif 9947125Sbostic out2c('\n'); 10047125Sbostic flushout(&errout); 10147125Sbostic } 10247125Sbostic popstackmark(&smark); 10347125Sbostic FORCEINTON; /* enable interrupts */ 10447125Sbostic if (state == 1) 10547125Sbostic goto state1; 10647125Sbostic else if (state == 2) 10747125Sbostic goto state2; 10854316Smarc else if (state == 3) 10954316Smarc goto state3; 11047125Sbostic else 11154316Smarc goto state4; 11247125Sbostic } 11347125Sbostic handler = &jmploc; 11447125Sbostic #ifdef DEBUG 11547125Sbostic opentrace(); 11647125Sbostic trputs("Shell args: "); trargs(argv); 11747125Sbostic #endif 11847125Sbostic rootpid = getpid(); 11947125Sbostic rootshell = 1; 12047125Sbostic init(); 12147125Sbostic setstackmark(&smark); 12247125Sbostic procargs(argc, argv); 12347125Sbostic if (argv[0] && argv[0][0] == '-') { 12447125Sbostic state = 1; 12547125Sbostic read_profile("/etc/profile"); 12647125Sbostic state1: 12747125Sbostic state = 2; 12847125Sbostic read_profile(".profile"); 12954316Smarc } 13047125Sbostic state2: 13147125Sbostic state = 3; 132*55283Smarc if ((shinit = lookupvar("ENV")) != NULL && 13354316Smarc *shinit != '\0') { 13454316Smarc state = 3; 13554316Smarc read_profile(shinit); 13654316Smarc } 13754316Smarc state3: 13854316Smarc state = 4; 13947125Sbostic if (minusc) { 14047125Sbostic evalstring(minusc); 14147125Sbostic } 14247125Sbostic if (sflag || minusc == NULL) { 14354316Smarc state4: /* XXX ??? - why isn't this before the "if" statement */ 14447125Sbostic cmdloop(1); 14547125Sbostic } 14647125Sbostic #if PROFILE 14747125Sbostic monitor(0); 14847125Sbostic #endif 14947125Sbostic exitshell(exitstatus); 15047125Sbostic } 15147125Sbostic 15247125Sbostic 15347125Sbostic /* 15447125Sbostic * Read and execute commands. "Top" is nonzero for the top level command 15547125Sbostic * loop; it turns on prompting if the shell is interactive. 15647125Sbostic */ 15747125Sbostic 15847125Sbostic void 15947125Sbostic cmdloop(top) { 16047125Sbostic union node *n; 16147125Sbostic struct stackmark smark; 16247125Sbostic int inter; 16355278Smarc int numeof = 0; 16447125Sbostic 16547125Sbostic TRACE(("cmdloop(%d) called\n", top)); 16647125Sbostic setstackmark(&smark); 16747125Sbostic for (;;) { 16847125Sbostic if (pendingsigs) 16947125Sbostic dotrap(); 17047125Sbostic inter = 0; 17147125Sbostic if (iflag && top) { 17247125Sbostic inter++; 17347125Sbostic showjobs(1); 17447125Sbostic chkmail(0); 17547125Sbostic flushout(&output); 17647125Sbostic } 17747125Sbostic n = parsecmd(inter); 17855277Smarc /* showtree(n); DEBUG */ 17947125Sbostic if (n == NEOF) { 18055277Smarc if (!top || numeof >= 50) 18147125Sbostic break; 18255277Smarc if (!stoppedjobs()) { 18355277Smarc if (!Iflag) 18455277Smarc break; 18555277Smarc out2str("\nUse \"exit\" to leave shell.\n"); 18655277Smarc } 18747125Sbostic numeof++; 18847125Sbostic } else if (n != NULL && nflag == 0) { 18955277Smarc job_warning = (job_warning == 2) ? 1 : 0; 19055278Smarc numeof = 0; 19147125Sbostic evaltree(n, 0); 19247125Sbostic } 19347125Sbostic popstackmark(&smark); 19447125Sbostic } 19547125Sbostic popstackmark(&smark); /* unnecessary */ 19647125Sbostic } 19747125Sbostic 19847125Sbostic 19947125Sbostic 20047125Sbostic /* 20147125Sbostic * Read /etc/profile or .profile. Return on error. 20247125Sbostic */ 20347125Sbostic 20447125Sbostic STATIC void 20547125Sbostic read_profile(name) 20647125Sbostic char *name; 20747125Sbostic { 20847125Sbostic int fd; 20947125Sbostic 21047125Sbostic INTOFF; 21147125Sbostic if ((fd = open(name, O_RDONLY)) >= 0) 21247125Sbostic setinputfd(fd, 1); 21347125Sbostic INTON; 21447125Sbostic if (fd < 0) 21547125Sbostic return; 21647125Sbostic cmdloop(0); 21747125Sbostic popfile(); 21847125Sbostic } 21947125Sbostic 22047125Sbostic 22147125Sbostic 22247125Sbostic /* 22347125Sbostic * Read a file containing shell functions. 22447125Sbostic */ 22547125Sbostic 22647125Sbostic void 22747125Sbostic readcmdfile(name) 22847125Sbostic char *name; 22947125Sbostic { 23047125Sbostic int fd; 23147125Sbostic 23247125Sbostic INTOFF; 23347125Sbostic if ((fd = open(name, O_RDONLY)) >= 0) 23447125Sbostic setinputfd(fd, 1); 23547125Sbostic else 23647125Sbostic error("Can't open %s", name); 23747125Sbostic INTON; 23847125Sbostic cmdloop(0); 23947125Sbostic popfile(); 24047125Sbostic } 24147125Sbostic 24247125Sbostic 24347125Sbostic 24447125Sbostic /* 24547125Sbostic * Take commands from a file. To be compatable we should do a path 24647125Sbostic * search for the file, but a path search doesn't make any sense. 24747125Sbostic */ 24847125Sbostic 24947125Sbostic dotcmd(argc, argv) char **argv; { 25047125Sbostic exitstatus = 0; 25147125Sbostic if (argc >= 2) { /* That's what SVR2 does */ 25247125Sbostic setinputfile(argv[1], 1); 25347125Sbostic commandname = argv[1]; 25447125Sbostic cmdloop(0); 25547125Sbostic popfile(); 25647125Sbostic } 25747125Sbostic return exitstatus; 25847125Sbostic } 25947125Sbostic 26047125Sbostic 26147125Sbostic exitcmd(argc, argv) char **argv; { 26255277Smarc if (stoppedjobs()) 26355277Smarc return; 26447125Sbostic if (argc > 1) 26547125Sbostic exitstatus = number(argv[1]); 26647125Sbostic exitshell(exitstatus); 26747125Sbostic } 26847125Sbostic 26947125Sbostic 27047125Sbostic #ifdef notdef 27147125Sbostic /* 27247125Sbostic * Should never be called. 27347125Sbostic */ 27447125Sbostic 27547125Sbostic void 27647125Sbostic exit(exitstatus) { 27747125Sbostic _exit(exitstatus); 27847125Sbostic } 27947125Sbostic #endif 280