main.c revision 104255
1/*- 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Kenneth Almquist. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37#ifndef lint 38static char const copyright[] = 39"@(#) Copyright (c) 1991, 1993\n\ 40 The Regents of the University of California. All rights reserved.\n"; 41#endif /* not lint */ 42 43#ifndef lint 44#if 0 45static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/28/95"; 46#endif 47#endif /* not lint */ 48#include <sys/cdefs.h> 49__FBSDID("$FreeBSD: head/bin/sh/main.c 104255 2002-10-01 00:54:14Z tjr $"); 50 51#include <stdio.h> 52#include <signal.h> 53#include <sys/stat.h> 54#include <unistd.h> 55#include <fcntl.h> 56#include <locale.h> 57#include <errno.h> 58 59#include "shell.h" 60#include "main.h" 61#include "mail.h" 62#include "options.h" 63#include "output.h" 64#include "parser.h" 65#include "nodes.h" 66#include "expand.h" 67#include "eval.h" 68#include "jobs.h" 69#include "input.h" 70#include "trap.h" 71#include "var.h" 72#include "show.h" 73#include "memalloc.h" 74#include "error.h" 75#include "init.h" 76#include "mystring.h" 77#include "exec.h" 78#include "cd.h" 79 80#define PROFILE 0 81 82int rootpid; 83int rootshell; 84#if PROFILE 85short profile_buf[16384]; 86extern int etext(); 87#endif 88 89STATIC void read_profile(char *); 90STATIC char *find_dot_file(char *); 91 92/* 93 * Main routine. We initialize things, parse the arguments, execute 94 * profiles if we're a login shell, and then call cmdloop to execute 95 * commands. The setjmp call sets up the location to jump to when an 96 * exception occurs. When an exception occurs the variable "state" 97 * is used to figure out how far we had gotten. 98 */ 99 100int 101main(int argc, char *argv[]) 102{ 103 struct jmploc jmploc; 104 struct stackmark smark; 105 volatile int state; 106 char *shinit; 107 108#if PROFILE 109 monitor(4, etext, profile_buf, sizeof profile_buf, 50); 110#endif 111 (void) setlocale(LC_ALL, ""); 112 state = 0; 113 if (setjmp(jmploc.loc)) { 114 /* 115 * When a shell procedure is executed, we raise the 116 * exception EXSHELLPROC to clean up before executing 117 * the shell procedure. 118 */ 119 switch (exception) { 120 case EXSHELLPROC: 121 rootpid = getpid(); 122 rootshell = 1; 123 minusc = NULL; 124 state = 3; 125 break; 126 127 case EXEXEC: 128 exitstatus = exerrno; 129 break; 130 131 case EXERROR: 132 exitstatus = 2; 133 break; 134 135 default: 136 break; 137 } 138 139 if (exception != EXSHELLPROC) { 140 if (state == 0 || iflag == 0 || ! rootshell) 141 exitshell(exitstatus); 142 } 143 reset(); 144 if (exception == EXINT) { 145 out2c('\n'); 146 flushout(&errout); 147 } 148 popstackmark(&smark); 149 FORCEINTON; /* enable interrupts */ 150 if (state == 1) 151 goto state1; 152 else if (state == 2) 153 goto state2; 154 else if (state == 3) 155 goto state3; 156 else 157 goto state4; 158 } 159 handler = &jmploc; 160#ifdef DEBUG 161 opentrace(); 162 trputs("Shell args: "); trargs(argv); 163#endif 164 rootpid = getpid(); 165 rootshell = 1; 166 init(); 167 setstackmark(&smark); 168 procargs(argc, argv); 169 if (getpwd() == NULL && iflag) 170 out2str("sh: cannot determine working directory\n"); 171 if (argv[0] && argv[0][0] == '-') { 172 state = 1; 173 read_profile("/etc/profile"); 174state1: 175 state = 2; 176 if (privileged == 0) 177 read_profile(".profile"); 178 else 179 read_profile("/etc/suid_profile"); 180 } 181state2: 182 state = 3; 183 if (!privileged && iflag) { 184 if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') { 185 state = 3; 186 read_profile(shinit); 187 } 188 } 189state3: 190 state = 4; 191 if (minusc) { 192 evalstring(minusc); 193 } 194 if (sflag || minusc == NULL) { 195state4: /* XXX ??? - why isn't this before the "if" statement */ 196 cmdloop(1); 197 } 198#if PROFILE 199 monitor(0); 200#endif 201 exitshell(exitstatus); 202 /*NOTREACHED*/ 203 return 0; 204} 205 206 207/* 208 * Read and execute commands. "Top" is nonzero for the top level command 209 * loop; it turns on prompting if the shell is interactive. 210 */ 211 212void 213cmdloop(int top) 214{ 215 union node *n; 216 struct stackmark smark; 217 int inter; 218 int numeof = 0; 219 220 TRACE(("cmdloop(%d) called\n", top)); 221 setstackmark(&smark); 222 for (;;) { 223 if (pendingsigs) 224 dotrap(); 225 inter = 0; 226 if (iflag && top) { 227 inter++; 228 showjobs(1, 0, 0); 229 chkmail(0); 230 flushout(&output); 231 } 232 n = parsecmd(inter); 233 /* showtree(n); DEBUG */ 234 if (n == NEOF) { 235 if (!top || numeof >= 50) 236 break; 237 if (!stoppedjobs()) { 238 if (!Iflag) 239 break; 240 out2str("\nUse \"exit\" to leave shell.\n"); 241 } 242 numeof++; 243 } else if (n != NULL && nflag == 0) { 244 job_warning = (job_warning == 2) ? 1 : 0; 245 numeof = 0; 246 evaltree(n, 0); 247 } 248 popstackmark(&smark); 249 setstackmark(&smark); 250 if (evalskip == SKIPFILE) { 251 evalskip = 0; 252 break; 253 } 254 } 255 popstackmark(&smark); 256} 257 258 259 260/* 261 * Read /etc/profile or .profile. Return on error. 262 */ 263 264STATIC void 265read_profile(char *name) 266{ 267 int fd; 268 269 INTOFF; 270 if ((fd = open(name, O_RDONLY)) >= 0) 271 setinputfd(fd, 1); 272 INTON; 273 if (fd < 0) 274 return; 275 cmdloop(0); 276 popfile(); 277} 278 279 280 281/* 282 * Read a file containing shell functions. 283 */ 284 285void 286readcmdfile(char *name) 287{ 288 int fd; 289 290 INTOFF; 291 if ((fd = open(name, O_RDONLY)) >= 0) 292 setinputfd(fd, 1); 293 else 294 error("Can't open %s: %s", name, strerror(errno)); 295 INTON; 296 cmdloop(0); 297 popfile(); 298} 299 300 301 302/* 303 * Take commands from a file. To be compatible we should do a path 304 * search for the file, which is necessary to find sub-commands. 305 */ 306 307 308STATIC char * 309find_dot_file(char *basename) 310{ 311 static char localname[FILENAME_MAX+1]; 312 char *fullname; 313 char *path = pathval(); 314 struct stat statb; 315 316 /* don't try this for absolute or relative paths */ 317 if( strchr(basename, '/')) 318 return basename; 319 320 while ((fullname = padvance(&path, basename)) != NULL) { 321 strcpy(localname, fullname); 322 stunalloc(fullname); 323 if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) 324 return localname; 325 } 326 return basename; 327} 328 329int 330dotcmd(int argc, char **argv) 331{ 332 struct strlist *sp; 333 exitstatus = 0; 334 335 for (sp = cmdenviron; sp ; sp = sp->next) 336 setvareq(savestr(sp->text), VSTRFIXED|VTEXTFIXED); 337 338 if (argc >= 2) { /* That's what SVR2 does */ 339 char *fullname = find_dot_file(argv[1]); 340 341 setinputfile(fullname, 1); 342 commandname = fullname; 343 cmdloop(0); 344 popfile(); 345 } 346 return exitstatus; 347} 348 349 350int 351exitcmd(int argc, char **argv) 352{ 353 extern int oexitstatus; 354 355 if (stoppedjobs()) 356 return 0; 357 if (argc > 1) 358 exitstatus = number(argv[1]); 359 else 360 exitstatus = oexitstatus; 361 exitshell(exitstatus); 362 /*NOTREACHED*/ 363 return 0; 364} 365