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*47295Smarc static char sccsid[] = "@(#)main.c 5.2 (Berkeley) 03/13/91"; 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 #if ATTY 3547125Sbostic #include "var.h" 3647125Sbostic #endif 3747125Sbostic #include "memalloc.h" 3847125Sbostic #include "error.h" 3947125Sbostic #include "init.h" 4047125Sbostic #include "mystring.h" 4147125Sbostic 4247125Sbostic #define PROFILE 0 4347125Sbostic 4447125Sbostic int rootpid; 4547125Sbostic int rootshell; 4647125Sbostic STATIC union node *curcmd; 4747125Sbostic STATIC union node *prevcmd; 4847125Sbostic extern int errno; 4947125Sbostic #if PROFILE 5047125Sbostic short profile_buf[16384]; 5147125Sbostic extern int etext(); 5247125Sbostic #endif 5347125Sbostic 5447125Sbostic #ifdef __STDC__ 5547125Sbostic STATIC void read_profile(char *); 5647125Sbostic char *getenv(char *); 5747125Sbostic #else 5847125Sbostic STATIC void read_profile(); 5947125Sbostic char *getenv(); 6047125Sbostic #endif 6147125Sbostic 6247125Sbostic 6347125Sbostic /* 6447125Sbostic * Main routine. We initialize things, parse the arguments, execute 6547125Sbostic * profiles if we're a login shell, and then call cmdloop to execute 6647125Sbostic * commands. The setjmp call sets up the location to jump to when an 6747125Sbostic * exception occurs. When an exception occurs the variable "state" 6847125Sbostic * is used to figure out how far we had gotten. 6947125Sbostic */ 7047125Sbostic 7147125Sbostic main(argc, argv) char **argv; { 7247125Sbostic struct jmploc jmploc; 7347125Sbostic struct stackmark smark; 7447125Sbostic volatile int state; 7547125Sbostic char *shinit; 7647125Sbostic 7747125Sbostic #if PROFILE 7847125Sbostic monitor(4, etext, profile_buf, sizeof profile_buf, 50); 7947125Sbostic #endif 8047125Sbostic state = 0; 8147125Sbostic if (setjmp(jmploc.loc)) { 8247125Sbostic /* 8347125Sbostic * When a shell procedure is executed, we raise the 8447125Sbostic * exception EXSHELLPROC to clean up before executing 8547125Sbostic * the shell procedure. 8647125Sbostic */ 8747125Sbostic if (exception == EXSHELLPROC) { 8847125Sbostic rootpid = getpid(); 8947125Sbostic rootshell = 1; 9047125Sbostic minusc = NULL; 9147125Sbostic state = 3; 9247125Sbostic } else if (state == 0 || iflag == 0 || ! rootshell) 9347125Sbostic exitshell(2); 9447125Sbostic reset(); 9547125Sbostic #if ATTY 9647125Sbostic if (exception == EXINT 9747125Sbostic && (! attyset() || equal(termval(), "emacs"))) { 9847125Sbostic #else 9947125Sbostic if (exception == EXINT) { 10047125Sbostic #endif 10147125Sbostic out2c('\n'); 10247125Sbostic flushout(&errout); 10347125Sbostic } 10447125Sbostic popstackmark(&smark); 10547125Sbostic FORCEINTON; /* enable interrupts */ 10647125Sbostic if (state == 1) 10747125Sbostic goto state1; 10847125Sbostic else if (state == 2) 10947125Sbostic goto state2; 11047125Sbostic else 11147125Sbostic goto state3; 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"); 12947125Sbostic } else if ((sflag || minusc) && (shinit = getenv("SHINIT")) != NULL) { 13047125Sbostic state = 2; 13147125Sbostic evalstring(shinit); 13247125Sbostic } 13347125Sbostic state2: 13447125Sbostic state = 3; 13547125Sbostic if (minusc) { 13647125Sbostic evalstring(minusc); 13747125Sbostic } 13847125Sbostic if (sflag || minusc == NULL) { 13947125Sbostic state3: 14047125Sbostic cmdloop(1); 14147125Sbostic } 14247125Sbostic #if PROFILE 14347125Sbostic monitor(0); 14447125Sbostic #endif 14547125Sbostic exitshell(exitstatus); 14647125Sbostic } 14747125Sbostic 14847125Sbostic 14947125Sbostic /* 15047125Sbostic * Read and execute commands. "Top" is nonzero for the top level command 15147125Sbostic * loop; it turns on prompting if the shell is interactive. 15247125Sbostic */ 15347125Sbostic 15447125Sbostic void 15547125Sbostic cmdloop(top) { 15647125Sbostic union node *n; 15747125Sbostic struct stackmark smark; 15847125Sbostic int inter; 15947125Sbostic int numeof; 16047125Sbostic 16147125Sbostic TRACE(("cmdloop(%d) called\n", top)); 16247125Sbostic setstackmark(&smark); 16347125Sbostic numeof = 0; 16447125Sbostic for (;;) { 16547125Sbostic if (pendingsigs) 16647125Sbostic dotrap(); 16747125Sbostic inter = 0; 16847125Sbostic if (iflag && top) { 16947125Sbostic inter++; 17047125Sbostic showjobs(1); 17147125Sbostic chkmail(0); 17247125Sbostic flushout(&output); 17347125Sbostic } 17447125Sbostic n = parsecmd(inter); 17547125Sbostic #ifdef DEBUG 176*47295Smarc /* showtree(n); */ 17747125Sbostic #endif 17847125Sbostic if (n == NEOF) { 17947125Sbostic if (Iflag == 0 || numeof >= 50) 18047125Sbostic break; 18147125Sbostic out2str("\nUse \"exit\" to leave shell.\n"); 18247125Sbostic numeof++; 18347125Sbostic } else if (n != NULL && nflag == 0) { 18447125Sbostic if (inter) { 18547125Sbostic INTOFF; 18647125Sbostic if (prevcmd) 18747125Sbostic freefunc(prevcmd); 18847125Sbostic prevcmd = curcmd; 18947125Sbostic curcmd = copyfunc(n); 19047125Sbostic INTON; 19147125Sbostic } 19247125Sbostic evaltree(n, 0); 19347125Sbostic #ifdef notdef 19447125Sbostic if (exitstatus) /*DEBUG*/ 19547125Sbostic outfmt(&errout, "Exit status 0x%X\n", exitstatus); 19647125Sbostic #endif 19747125Sbostic } 19847125Sbostic popstackmark(&smark); 19947125Sbostic } 20047125Sbostic popstackmark(&smark); /* unnecessary */ 20147125Sbostic } 20247125Sbostic 20347125Sbostic 20447125Sbostic 20547125Sbostic /* 20647125Sbostic * Read /etc/profile or .profile. Return on error. 20747125Sbostic */ 20847125Sbostic 20947125Sbostic STATIC void 21047125Sbostic read_profile(name) 21147125Sbostic char *name; 21247125Sbostic { 21347125Sbostic int fd; 21447125Sbostic 21547125Sbostic INTOFF; 21647125Sbostic if ((fd = open(name, O_RDONLY)) >= 0) 21747125Sbostic setinputfd(fd, 1); 21847125Sbostic INTON; 21947125Sbostic if (fd < 0) 22047125Sbostic return; 22147125Sbostic cmdloop(0); 22247125Sbostic popfile(); 22347125Sbostic } 22447125Sbostic 22547125Sbostic 22647125Sbostic 22747125Sbostic /* 22847125Sbostic * Read a file containing shell functions. 22947125Sbostic */ 23047125Sbostic 23147125Sbostic void 23247125Sbostic readcmdfile(name) 23347125Sbostic char *name; 23447125Sbostic { 23547125Sbostic int fd; 23647125Sbostic 23747125Sbostic INTOFF; 23847125Sbostic if ((fd = open(name, O_RDONLY)) >= 0) 23947125Sbostic setinputfd(fd, 1); 24047125Sbostic else 24147125Sbostic error("Can't open %s", name); 24247125Sbostic INTON; 24347125Sbostic cmdloop(0); 24447125Sbostic popfile(); 24547125Sbostic } 24647125Sbostic 24747125Sbostic 24847125Sbostic 24947125Sbostic /* 25047125Sbostic * Take commands from a file. To be compatable we should do a path 25147125Sbostic * search for the file, but a path search doesn't make any sense. 25247125Sbostic */ 25347125Sbostic 25447125Sbostic dotcmd(argc, argv) char **argv; { 25547125Sbostic exitstatus = 0; 25647125Sbostic if (argc >= 2) { /* That's what SVR2 does */ 25747125Sbostic setinputfile(argv[1], 1); 25847125Sbostic commandname = argv[1]; 25947125Sbostic cmdloop(0); 26047125Sbostic popfile(); 26147125Sbostic } 26247125Sbostic return exitstatus; 26347125Sbostic } 26447125Sbostic 26547125Sbostic 26647125Sbostic exitcmd(argc, argv) char **argv; { 26747125Sbostic if (argc > 1) 26847125Sbostic exitstatus = number(argv[1]); 26947125Sbostic exitshell(exitstatus); 27047125Sbostic } 27147125Sbostic 27247125Sbostic 27347125Sbostic lccmd(argc, argv) char **argv; { 27447125Sbostic if (argc > 1) { 27547125Sbostic defun(argv[1], prevcmd); 27647125Sbostic return 0; 27747125Sbostic } else { 27847125Sbostic INTOFF; 27947125Sbostic freefunc(curcmd); 28047125Sbostic curcmd = prevcmd; 28147125Sbostic prevcmd = NULL; 28247125Sbostic INTON; 28347125Sbostic evaltree(curcmd, 0); 28447125Sbostic return exitstatus; 28547125Sbostic } 28647125Sbostic } 28747125Sbostic 28847125Sbostic 28947125Sbostic 29047125Sbostic #ifdef notdef 29147125Sbostic /* 29247125Sbostic * Should never be called. 29347125Sbostic */ 29447125Sbostic 29547125Sbostic void 29647125Sbostic exit(exitstatus) { 29747125Sbostic _exit(exitstatus); 29847125Sbostic } 29947125Sbostic #endif 300