main.c revision 104261
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 104261 2002-10-01 01:30:33Z 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 80int rootpid; 81int rootshell; 82 83STATIC void read_profile(char *); 84STATIC char *find_dot_file(char *); 85 86/* 87 * Main routine. We initialize things, parse the arguments, execute 88 * profiles if we're a login shell, and then call cmdloop to execute 89 * commands. The setjmp call sets up the location to jump to when an 90 * exception occurs. When an exception occurs the variable "state" 91 * is used to figure out how far we had gotten. 92 */ 93 94int 95main(int argc, char *argv[]) 96{ 97 struct jmploc jmploc; 98 struct stackmark smark; 99 volatile int state; 100 char *shinit; 101 102 (void) setlocale(LC_ALL, ""); 103 state = 0; 104 if (setjmp(jmploc.loc)) { 105 /* 106 * When a shell procedure is executed, we raise the 107 * exception EXSHELLPROC to clean up before executing 108 * the shell procedure. 109 */ 110 switch (exception) { 111 case EXSHELLPROC: 112 rootpid = getpid(); 113 rootshell = 1; 114 minusc = NULL; 115 state = 3; 116 break; 117 118 case EXEXEC: 119 exitstatus = exerrno; 120 break; 121 122 case EXERROR: 123 exitstatus = 2; 124 break; 125 126 default: 127 break; 128 } 129 130 if (exception != EXSHELLPROC) { 131 if (state == 0 || iflag == 0 || ! rootshell) 132 exitshell(exitstatus); 133 } 134 reset(); 135 if (exception == EXINT) { 136 out2c('\n'); 137 flushout(&errout); 138 } 139 popstackmark(&smark); 140 FORCEINTON; /* enable interrupts */ 141 if (state == 1) 142 goto state1; 143 else if (state == 2) 144 goto state2; 145 else if (state == 3) 146 goto state3; 147 else 148 goto state4; 149 } 150 handler = &jmploc; 151#ifdef DEBUG 152 opentrace(); 153 trputs("Shell args: "); trargs(argv); 154#endif 155 rootpid = getpid(); 156 rootshell = 1; 157 init(); 158 setstackmark(&smark); 159 procargs(argc, argv); 160 if (getpwd() == NULL && iflag) 161 out2str("sh: cannot determine working directory\n"); 162 if (argv[0] && argv[0][0] == '-') { 163 state = 1; 164 read_profile("/etc/profile"); 165state1: 166 state = 2; 167 if (privileged == 0) 168 read_profile(".profile"); 169 else 170 read_profile("/etc/suid_profile"); 171 } 172state2: 173 state = 3; 174 if (!privileged && iflag) { 175 if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') { 176 state = 3; 177 read_profile(shinit); 178 } 179 } 180state3: 181 state = 4; 182 if (minusc) { 183 evalstring(minusc); 184 } 185 if (sflag || minusc == NULL) { 186state4: /* XXX ??? - why isn't this before the "if" statement */ 187 cmdloop(1); 188 } 189 exitshell(exitstatus); 190 /*NOTREACHED*/ 191 return 0; 192} 193 194 195/* 196 * Read and execute commands. "Top" is nonzero for the top level command 197 * loop; it turns on prompting if the shell is interactive. 198 */ 199 200void 201cmdloop(int top) 202{ 203 union node *n; 204 struct stackmark smark; 205 int inter; 206 int numeof = 0; 207 208 TRACE(("cmdloop(%d) called\n", top)); 209 setstackmark(&smark); 210 for (;;) { 211 if (pendingsigs) 212 dotrap(); 213 inter = 0; 214 if (iflag && top) { 215 inter++; 216 showjobs(1, 0, 0); 217 chkmail(0); 218 flushout(&output); 219 } 220 n = parsecmd(inter); 221 /* showtree(n); DEBUG */ 222 if (n == NEOF) { 223 if (!top || numeof >= 50) 224 break; 225 if (!stoppedjobs()) { 226 if (!Iflag) 227 break; 228 out2str("\nUse \"exit\" to leave shell.\n"); 229 } 230 numeof++; 231 } else if (n != NULL && nflag == 0) { 232 job_warning = (job_warning == 2) ? 1 : 0; 233 numeof = 0; 234 evaltree(n, 0); 235 } 236 popstackmark(&smark); 237 setstackmark(&smark); 238 if (evalskip == SKIPFILE) { 239 evalskip = 0; 240 break; 241 } 242 } 243 popstackmark(&smark); 244} 245 246 247 248/* 249 * Read /etc/profile or .profile. Return on error. 250 */ 251 252STATIC void 253read_profile(char *name) 254{ 255 int fd; 256 257 INTOFF; 258 if ((fd = open(name, O_RDONLY)) >= 0) 259 setinputfd(fd, 1); 260 INTON; 261 if (fd < 0) 262 return; 263 cmdloop(0); 264 popfile(); 265} 266 267 268 269/* 270 * Read a file containing shell functions. 271 */ 272 273void 274readcmdfile(char *name) 275{ 276 int fd; 277 278 INTOFF; 279 if ((fd = open(name, O_RDONLY)) >= 0) 280 setinputfd(fd, 1); 281 else 282 error("Can't open %s: %s", name, strerror(errno)); 283 INTON; 284 cmdloop(0); 285 popfile(); 286} 287 288 289 290/* 291 * Take commands from a file. To be compatible we should do a path 292 * search for the file, which is necessary to find sub-commands. 293 */ 294 295 296STATIC char * 297find_dot_file(char *basename) 298{ 299 static char localname[FILENAME_MAX+1]; 300 char *fullname; 301 char *path = pathval(); 302 struct stat statb; 303 304 /* don't try this for absolute or relative paths */ 305 if( strchr(basename, '/')) 306 return basename; 307 308 while ((fullname = padvance(&path, basename)) != NULL) { 309 strcpy(localname, fullname); 310 stunalloc(fullname); 311 if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) 312 return localname; 313 } 314 return basename; 315} 316 317int 318dotcmd(int argc, char **argv) 319{ 320 struct strlist *sp; 321 exitstatus = 0; 322 323 for (sp = cmdenviron; sp ; sp = sp->next) 324 setvareq(savestr(sp->text), VSTRFIXED|VTEXTFIXED); 325 326 if (argc >= 2) { /* That's what SVR2 does */ 327 char *fullname = find_dot_file(argv[1]); 328 329 setinputfile(fullname, 1); 330 commandname = fullname; 331 cmdloop(0); 332 popfile(); 333 } 334 return exitstatus; 335} 336 337 338int 339exitcmd(int argc, char **argv) 340{ 341 extern int oexitstatus; 342 343 if (stoppedjobs()) 344 return 0; 345 if (argc > 1) 346 exitstatus = number(argv[1]); 347 else 348 exitstatus = oexitstatus; 349 exitshell(exitstatus); 350 /*NOTREACHED*/ 351 return 0; 352} 353