main.c revision 99110
150397Sobrien/*- 2132718Skan * Copyright (c) 1991, 1993 350397Sobrien * The Regents of the University of California. All rights reserved. 450397Sobrien * 590075Sobrien * This code is derived from software contributed to Berkeley by 650397Sobrien * Kenneth Almquist. 790075Sobrien * 890075Sobrien * Redistribution and use in source and binary forms, with or without 990075Sobrien * modification, are permitted provided that the following conditions 1090075Sobrien * are met: 1150397Sobrien * 1. Redistributions of source code must retain the above copyright 1290075Sobrien * notice, this list of conditions and the following disclaimer. 1390075Sobrien * 2. Redistributions in binary form must reproduce the above copyright 1490075Sobrien * notice, this list of conditions and the following disclaimer in the 1590075Sobrien * documentation and/or other materials provided with the distribution. 1650397Sobrien * 3. All advertising materials mentioning features or use of this software 1790075Sobrien * must display the following acknowledgement: 1890075Sobrien * This product includes software developed by the University of 19169689Skan * California, Berkeley and its contributors. 20169689Skan * 4. Neither the name of the University nor the names of its contributors 2150397Sobrien * may be used to endorse or promote products derived from this software 2250397Sobrien * without specific prior written permission. 2350397Sobrien * 2450397Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2550397Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2650397Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2750397Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2850397Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2950397Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3050397Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3150397Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3250397Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3350397Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3450397Sobrien * SUCH DAMAGE. 3550397Sobrien */ 3650397Sobrien 3750397Sobrien#ifndef lint 3850397Sobrienstatic char const copyright[] = 3950397Sobrien"@(#) Copyright (c) 1991, 1993\n\ 4050397Sobrien The Regents of the University of California. All rights reserved.\n"; 4150397Sobrien#endif /* not lint */ 4250397Sobrien 4350397Sobrien#ifndef lint 4450397Sobrien#if 0 4550397Sobrienstatic char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/28/95"; 4650397Sobrien#endif 4750397Sobrien#endif /* not lint */ 4850397Sobrien#include <sys/cdefs.h> 4950397Sobrien__FBSDID("$FreeBSD: head/bin/sh/main.c 99110 2002-06-30 05:15:05Z obrien $"); 5050397Sobrien 5150397Sobrien#include <stdio.h> 5250397Sobrien#include <signal.h> 5350397Sobrien#include <sys/stat.h> 5450397Sobrien#include <unistd.h> 5550397Sobrien#include <fcntl.h> 5650397Sobrien#include <locale.h> 5750397Sobrien#include <errno.h> 5850397Sobrien 5950397Sobrien#include "shell.h" 6050397Sobrien#include "main.h" 6150397Sobrien#include "mail.h" 6250397Sobrien#include "options.h" 6350397Sobrien#include "output.h" 6450397Sobrien#include "parser.h" 6550397Sobrien#include "nodes.h" 6650397Sobrien#include "expand.h" 6750397Sobrien#include "eval.h" 6850397Sobrien#include "jobs.h" 6950397Sobrien#include "input.h" 7050397Sobrien#include "trap.h" 7150397Sobrien#include "var.h" 7250397Sobrien#include "show.h" 7350397Sobrien#include "memalloc.h" 7450397Sobrien#include "error.h" 7550397Sobrien#include "init.h" 7650397Sobrien#include "mystring.h" 7750397Sobrien#include "exec.h" 7850397Sobrien#include "cd.h" 7950397Sobrien 8050397Sobrien#define PROFILE 0 8150397Sobrien 8250397Sobrienint rootpid; 8350397Sobrienint rootshell; 8450397Sobrien#if PROFILE 8550397Sobrienshort profile_buf[16384]; 86132718Skanextern int etext(); 8750397Sobrien#endif 8850397Sobrien 8950397SobrienSTATIC void read_profile(char *); 9050397SobrienSTATIC char *find_dot_file(char *); 9150397Sobrien 9250397Sobrien/* 9350397Sobrien * Main routine. We initialize things, parse the arguments, execute 9450397Sobrien * profiles if we're a login shell, and then call cmdloop to execute 9550397Sobrien * commands. The setjmp call sets up the location to jump to when an 9650397Sobrien * exception occurs. When an exception occurs the variable "state" 9750397Sobrien * is used to figure out how far we had gotten. 9850397Sobrien */ 9950397Sobrien 10050397Sobrienint 10150397Sobrienmain(int argc, char *argv[]) 10250397Sobrien{ 10350397Sobrien struct jmploc jmploc; 10450397Sobrien struct stackmark smark; 10550397Sobrien volatile int state; 10650397Sobrien char *shinit; 10790075Sobrien 10890075Sobrien#if PROFILE 10990075Sobrien monitor(4, etext, profile_buf, sizeof profile_buf, 50); 11090075Sobrien#endif 11190075Sobrien (void) setlocale(LC_ALL, ""); 11290075Sobrien state = 0; 11390075Sobrien if (setjmp(jmploc.loc)) { 11450397Sobrien /* 11550397Sobrien * When a shell procedure is executed, we raise the 11650397Sobrien * exception EXSHELLPROC to clean up before executing 11750397Sobrien * the shell procedure. 11850397Sobrien */ 11950397Sobrien switch (exception) { 12050397Sobrien case EXSHELLPROC: 12150397Sobrien rootpid = getpid(); 12250397Sobrien rootshell = 1; 12350397Sobrien minusc = NULL; 12450397Sobrien state = 3; 12550397Sobrien break; 12650397Sobrien 12750397Sobrien case EXEXEC: 12850397Sobrien exitstatus = exerrno; 12950397Sobrien break; 13050397Sobrien 13150397Sobrien case EXERROR: 13250397Sobrien exitstatus = 2; 13350397Sobrien break; 13450397Sobrien 13550397Sobrien default: 13650397Sobrien break; 13750397Sobrien } 13850397Sobrien 13950397Sobrien if (exception != EXSHELLPROC) { 14050397Sobrien if (state == 0 || iflag == 0 || ! rootshell) 14150397Sobrien exitshell(exitstatus); 14250397Sobrien } 14350397Sobrien reset(); 14450397Sobrien if (exception == EXINT 14550397Sobrien#if ATTY 14650397Sobrien && (! attyset() || equal(termval(), "emacs")) 14750397Sobrien#endif 14850397Sobrien ) { 14950397Sobrien out2c('\n'); 15050397Sobrien flushout(&errout); 15150397Sobrien } 15250397Sobrien popstackmark(&smark); 15390075Sobrien FORCEINTON; /* enable interrupts */ 15490075Sobrien if (state == 1) 15590075Sobrien goto state1; 15690075Sobrien else if (state == 2) 15790075Sobrien goto state2; 15890075Sobrien else if (state == 3) 15990075Sobrien goto state3; 16050397Sobrien else 16150397Sobrien goto state4; 16250397Sobrien } 16350397Sobrien handler = &jmploc; 16450397Sobrien#ifdef DEBUG 16550397Sobrien opentrace(); 16650397Sobrien trputs("Shell args: "); trargs(argv); 16750397Sobrien#endif 16850397Sobrien rootpid = getpid(); 16950397Sobrien rootshell = 1; 17050397Sobrien init(); 17150397Sobrien setstackmark(&smark); 17250397Sobrien procargs(argc, argv); 17350397Sobrien if (getpwd() == NULL && iflag) 17450397Sobrien out2str("sh: cannot determine working directory\n"); 17550397Sobrien if (argv[0] && argv[0][0] == '-') { 17650397Sobrien state = 1; 17750397Sobrien read_profile("/etc/profile"); 17850397Sobrienstate1: 17950397Sobrien state = 2; 18050397Sobrien if (privileged == 0) 18150397Sobrien read_profile(".profile"); 18250397Sobrien else 18350397Sobrien read_profile("/etc/suid_profile"); 18450397Sobrien } 18550397Sobrienstate2: 18650397Sobrien state = 3; 18750397Sobrien if (!privileged && iflag) { 18850397Sobrien if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') { 18950397Sobrien state = 3; 19050397Sobrien read_profile(shinit); 19150397Sobrien } 19250397Sobrien } 19350397Sobrienstate3: 19450397Sobrien state = 4; 19550397Sobrien if (minusc) { 19650397Sobrien evalstring(minusc); 19750397Sobrien } 19850397Sobrien if (sflag || minusc == NULL) { 19990075Sobrienstate4: /* XXX ??? - why isn't this before the "if" statement */ 20090075Sobrien cmdloop(1); 20190075Sobrien } 20290075Sobrien#if PROFILE 20390075Sobrien monitor(0); 20490075Sobrien#endif 20590075Sobrien exitshell(exitstatus); 20650397Sobrien /*NOTREACHED*/ 20750397Sobrien return 0; 20850397Sobrien} 20950397Sobrien 21050397Sobrien 21150397Sobrien/* 21250397Sobrien * Read and execute commands. "Top" is nonzero for the top level command 21350397Sobrien * loop; it turns on prompting if the shell is interactive. 21450397Sobrien */ 21550397Sobrien 21650397Sobrienvoid 21750397Sobriencmdloop(int top) 21850397Sobrien{ 21950397Sobrien union node *n; 22050397Sobrien struct stackmark smark; 22150397Sobrien int inter; 22250397Sobrien int numeof = 0; 22350397Sobrien 22450397Sobrien TRACE(("cmdloop(%d) called\n", top)); 22550397Sobrien setstackmark(&smark); 22650397Sobrien for (;;) { 22750397Sobrien if (pendingsigs) 22850397Sobrien dotrap(); 22950397Sobrien inter = 0; 23050397Sobrien if (iflag && top) { 23150397Sobrien inter++; 23250397Sobrien showjobs(1, 0, 0); 23350397Sobrien chkmail(0); 23450397Sobrien flushout(&output); 23550397Sobrien } 23650397Sobrien n = parsecmd(inter); 23750397Sobrien /* showtree(n); DEBUG */ 23850397Sobrien if (n == NEOF) { 23950397Sobrien if (!top || numeof >= 50) 24050397Sobrien break; 24150397Sobrien if (!stoppedjobs()) { 24250397Sobrien if (!Iflag) 24350397Sobrien break; 24450397Sobrien out2str("\nUse \"exit\" to leave shell.\n"); 24550397Sobrien } 24650397Sobrien numeof++; 24750397Sobrien } else if (n != NULL && nflag == 0) { 24850397Sobrien job_warning = (job_warning == 2) ? 1 : 0; 24950397Sobrien numeof = 0; 25050397Sobrien evaltree(n, 0); 25150397Sobrien } 25250397Sobrien popstackmark(&smark); 253 setstackmark(&smark); 254 if (evalskip == SKIPFILE) { 255 evalskip = 0; 256 break; 257 } 258 } 259 popstackmark(&smark); 260} 261 262 263 264/* 265 * Read /etc/profile or .profile. Return on error. 266 */ 267 268STATIC void 269read_profile(char *name) 270{ 271 int fd; 272 273 INTOFF; 274 if ((fd = open(name, O_RDONLY)) >= 0) 275 setinputfd(fd, 1); 276 INTON; 277 if (fd < 0) 278 return; 279 cmdloop(0); 280 popfile(); 281} 282 283 284 285/* 286 * Read a file containing shell functions. 287 */ 288 289void 290readcmdfile(char *name) 291{ 292 int fd; 293 294 INTOFF; 295 if ((fd = open(name, O_RDONLY)) >= 0) 296 setinputfd(fd, 1); 297 else 298 error("Can't open %s: %s", name, strerror(errno)); 299 INTON; 300 cmdloop(0); 301 popfile(); 302} 303 304 305 306/* 307 * Take commands from a file. To be compatible we should do a path 308 * search for the file, which is necessary to find sub-commands. 309 */ 310 311 312STATIC char * 313find_dot_file(char *basename) 314{ 315 static char localname[FILENAME_MAX+1]; 316 char *fullname; 317 char *path = pathval(); 318 struct stat statb; 319 320 /* don't try this for absolute or relative paths */ 321 if( strchr(basename, '/')) 322 return basename; 323 324 while ((fullname = padvance(&path, basename)) != NULL) { 325 strcpy(localname, fullname); 326 stunalloc(fullname); 327 if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) 328 return localname; 329 } 330 return basename; 331} 332 333int 334dotcmd(int argc, char **argv) 335{ 336 struct strlist *sp; 337 exitstatus = 0; 338 339 for (sp = cmdenviron; sp ; sp = sp->next) 340 setvareq(savestr(sp->text), VSTRFIXED|VTEXTFIXED); 341 342 if (argc >= 2) { /* That's what SVR2 does */ 343 char *fullname = find_dot_file(argv[1]); 344 345 setinputfile(fullname, 1); 346 commandname = fullname; 347 cmdloop(0); 348 popfile(); 349 } 350 return exitstatus; 351} 352 353 354int 355exitcmd(int argc, char **argv) 356{ 357 extern int oexitstatus; 358 359 if (stoppedjobs()) 360 return 0; 361 if (argc > 1) 362 exitstatus = number(argv[1]); 363 else 364 exitstatus = oexitstatus; 365 exitshell(exitstatus); 366 /*NOTREACHED*/ 367 return 0; 368} 369