SH_NORETURN_1 void shell_main(shinstance *psh, int argc, char **argv) { struct jmploc jmploc; struct stackmark smark; volatile int state; char *shinit; 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. */ switch (psh->exception) { case EXSHELLPROC: psh->rootpid = /*getpid()*/ psh->pid; psh->rootshell = 1; psh->minusc = NULL; state = 3; break; case EXEXEC: psh->exitstatus = psh->exerrno; break; case EXERROR: psh->exitstatus = 2; break; default: break; } if (psh->exception != EXSHELLPROC) { if (state == 0 || iflag(psh) == 0 || ! psh->rootshell) exitshell(psh, psh->exitstatus); } reset(psh); if (psh->exception == EXINT #if ATTY && (! attyset(psh) || equal(termval(psh), "emacs")) #endif ) { out2c(psh, '\n'); flushout(&psh->errout); } popstackmark(psh, &smark); FORCEINTON; /* enable interrupts */ if (state == 1) goto state1; else if (state == 2) goto state2; else if (state == 3) goto state3; else goto state4; } psh->handler = &jmploc; psh->rootpid = /*getpid()*/ psh->pid; psh->rootshell = 1; #ifdef DEBUG #if DEBUG == 2 debug(psh) = 1; #endif opentrace(psh); trputs(psh, "Shell args: "); trargs(psh, argv); #endif init(psh); setstackmark(psh, &smark); procargs(psh, argc, argv); if (argv[0] && argv[0][0] == '-') { state = 1; read_profile(psh, "/etc/profile"); state1: state = 2; read_profile(psh, ".profile"); } state2: state = 3; if (sh_getuid(psh) == sh_geteuid(psh) && sh_getgid(psh) == sh_getegid(psh)) { if ((shinit = lookupvar(psh, "ENV")) != NULL && *shinit != '\0') { state = 3; read_profile(psh, shinit); } } state3: state = 4; if (sflag(psh) == 0 || psh->minusc) { static int sigs[] = { SIGINT, SIGQUIT, SIGHUP, #ifdef SIGTSTP SIGTSTP, #endif SIGPIPE }; #define SIGSSIZE (sizeof(sigs)/sizeof(sigs[0])) unsigned i; for (i = 0; i < SIGSSIZE; i++) setsignal(psh, sigs[i], 0); } if (psh->minusc) evalstring(psh, psh->minusc, 0); if (sflag(psh) || psh->minusc == NULL) { state4: /* XXX ??? - why isn't this before the "if" statement */ cmdloop(psh, 1); } exitshell(psh, psh->exitstatus); /* NOTREACHED */ }
void getargs(int argc, char *argv[]) { register int i; register char *s; if (argc > 0) myname = argv[0]; for (i = 1; i < argc; ++i) { s = argv[i]; if (*s != '-') break; switch (*++s) { case '\0': input_file = stdin; if (i + 1 < argc) usage(); return; case '-': ++i; goto no_more_options; case 'b': if (*++s) file_prefix = s; else if (++i < argc) file_prefix = argv[i]; else usage(); continue; case 'v': vflag = 1; break; case 's': sflag(); break; default: usage(); } for (;;) { switch (*++s) { case '\0': goto end_of_option; case 'v': vflag = 1; break; case 's': sflag(); break; default: usage(); } } end_of_option:; } no_more_options:; if (i + 1 != argc) usage(); input_file_name = argv[i]; if (file_prefix == 0) { int len; len = strlen(argv[i]); file_prefix = malloc(len + 1); if (file_prefix == 0) no_space(); strcpy(file_prefix, argv[i]); while (len > 0) { len--; if (file_prefix[len] == '.') { file_prefix[len] = 0; break; } } } }
void setsignal(shinstance *psh, int signo, int vforked) { int action; shsig_t sigact = SH_SIG_DFL; char *t, tsig; if ((t = psh->trap[signo]) == NULL) action = S_DFL; else if (*t != '\0') action = S_CATCH; else action = S_IGN; if (psh->rootshell && !vforked && action == S_DFL) { switch (signo) { case SIGINT: if (iflag(psh) || psh->minusc || sflag(psh) == 0) action = S_CATCH; break; case SIGQUIT: #ifdef DEBUG if (debug(psh)) break; #endif /* FALLTHROUGH */ case SIGTERM: if (iflag(psh)) action = S_IGN; break; #if JOBS case SIGTSTP: case SIGTTOU: if (mflag(psh)) action = S_IGN; break; #endif } } t = &psh->sigmode[signo - 1]; tsig = *t; if (tsig == 0) { /* * current setting unknown */ if (!getsigaction(psh, signo, &sigact)) { /* * Pretend it worked; maybe we should give a warning * here, but other shells don't. We don't alter * sigmode, so that we retry every time. */ return; } if (sigact == SH_SIG_IGN) { if (mflag(psh) && (signo == SIGTSTP || signo == SIGTTIN || signo == SIGTTOU)) { tsig = S_IGN; /* don't hard ignore these */ } else tsig = S_HARD_IGN; } else { tsig = S_RESET; /* force to be set */ } } if (tsig == S_HARD_IGN || tsig == action) return; switch (action) { case S_DFL: sigact = SH_SIG_DFL; break; case S_CATCH: sigact = onsig; break; case S_IGN: sigact = SH_SIG_IGN; break; } if (!vforked) *t = action; sh_siginterrupt(psh, signo, 1); sh_signal(psh, signo, sigact); }