main.c revision 19240
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 * $Id: main.c,v 1.8 1996/09/12 12:41:46 adam Exp $ 37 */ 38 39#ifndef lint 40static char copyright[] = 41"@(#) Copyright (c) 1991, 1993\n\ 42 The Regents of the University of California. All rights reserved.\n"; 43#endif /* not lint */ 44 45#ifndef lint 46static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/28/95"; 47#endif /* not lint */ 48 49#include <stdio.h> 50#include <signal.h> 51#include <sys/stat.h> 52#include <unistd.h> 53#include <fcntl.h> 54#include <locale.h> 55 56 57#include "shell.h" 58#include "main.h" 59#include "mail.h" 60#include "options.h" 61#include "output.h" 62#include "parser.h" 63#include "nodes.h" 64#include "expand.h" 65#include "eval.h" 66#include "jobs.h" 67#include "input.h" 68#include "trap.h" 69#include "var.h" 70#include "show.h" 71#include "memalloc.h" 72#include "error.h" 73#include "init.h" 74#include "mystring.h" 75#include "exec.h" 76 77#define PROFILE 0 78 79int rootpid; 80int rootshell; 81STATIC union node *curcmd; 82STATIC union node *prevcmd; 83extern int errno; 84#if PROFILE 85short profile_buf[16384]; 86extern int etext(); 87#endif 88 89STATIC void read_profile __P((char *)); 90STATIC char *find_dot_file __P((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(argc, argv) 102 int argc; 103 char **argv; 104{ 105 struct jmploc jmploc; 106 struct stackmark smark; 107 volatile int state; 108 char *shinit; 109 110#if PROFILE 111 monitor(4, etext, profile_buf, sizeof profile_buf, 50); 112#endif 113 (void) setlocale(LC_ALL, ""); 114 state = 0; 115 if (setjmp(jmploc.loc)) { 116 /* 117 * When a shell procedure is executed, we raise the 118 * exception EXSHELLPROC to clean up before executing 119 * the shell procedure. 120 */ 121 if (exception == EXERROR) 122 exitstatus = 2; 123 if (exception == EXSHELLPROC) { 124 rootpid = getpid(); 125 rootshell = 1; 126 minusc = NULL; 127 state = 3; 128 } else if (state == 0 || iflag == 0 || ! rootshell) 129 exitshell(2); 130 reset(); 131 if (exception == EXINT 132#if ATTY 133 && (! attyset() || equal(termval(), "emacs")) 134#endif 135 ) { 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 (argv[0] && argv[0][0] == '-') { 161 state = 1; 162 read_profile("/etc/profile"); 163state1: 164 state = 2; 165 if (privileged == 0) 166 read_profile(".profile"); 167 else 168 read_profile("/etc/suid_profile"); 169 } 170state2: 171 state = 3; 172 if (privileged == 0) { 173 if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') { 174 state = 3; 175 read_profile(shinit); 176 } 177 } 178state3: 179 state = 4; 180 if (minusc) { 181 evalstring(minusc); 182 } 183 if (sflag || minusc == NULL) { 184state4: /* XXX ??? - why isn't this before the "if" statement */ 185 cmdloop(1); 186 } 187#if PROFILE 188 monitor(0); 189#endif 190 exitshell(exitstatus); 191 /*NOTREACHED*/ 192 return 0; 193} 194 195 196/* 197 * Read and execute commands. "Top" is nonzero for the top level command 198 * loop; it turns on prompting if the shell is interactive. 199 */ 200 201void 202cmdloop(top) 203 int top; 204{ 205 union node *n; 206 struct stackmark smark; 207 int inter; 208 int numeof = 0; 209 210 TRACE(("cmdloop(%d) called\n", top)); 211 setstackmark(&smark); 212 for (;;) { 213 if (pendingsigs) 214 dotrap(); 215 inter = 0; 216 if (iflag && top) { 217 inter++; 218 showjobs(1); 219 chkmail(0); 220 flushout(&output); 221 } 222 n = parsecmd(inter); 223 /* showtree(n); DEBUG */ 224 if (n == NEOF) { 225 if (!top || numeof >= 50) 226 break; 227 if (!stoppedjobs()) { 228 if (!Iflag) 229 break; 230 out2str("\nUse \"exit\" to leave shell.\n"); 231 } 232 numeof++; 233 } else if (n != NULL && nflag == 0) { 234 job_warning = (job_warning == 2) ? 1 : 0; 235 numeof = 0; 236 evaltree(n, 0); 237 } 238 popstackmark(&smark); 239 } 240 popstackmark(&smark); /* unnecessary */ 241} 242 243 244 245/* 246 * Read /etc/profile or .profile. Return on error. 247 */ 248 249STATIC void 250read_profile(name) 251 char *name; 252 { 253 int fd; 254 255 INTOFF; 256 if ((fd = open(name, O_RDONLY)) >= 0) 257 setinputfd(fd, 1); 258 INTON; 259 if (fd < 0) 260 return; 261 cmdloop(0); 262 popfile(); 263} 264 265 266 267/* 268 * Read a file containing shell functions. 269 */ 270 271void 272readcmdfile(name) 273 char *name; 274{ 275 int fd; 276 277 INTOFF; 278 if ((fd = open(name, O_RDONLY)) >= 0) 279 setinputfd(fd, 1); 280 else 281 error("Can't open %s", name); 282 INTON; 283 cmdloop(0); 284 popfile(); 285} 286 287 288 289/* 290 * Take commands from a file. To be compatable we should do a path 291 * search for the file, which is necessary to find sub-commands. 292 */ 293 294 295STATIC char * 296find_dot_file(basename) 297 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(argc, argv) 319 int argc; 320 char **argv; 321{ 322 struct strlist *sp; 323 exitstatus = 0; 324 325 for (sp = cmdenviron; sp ; sp = sp->next) 326 setvareq(savestr(sp->text), VSTRFIXED|VTEXTFIXED); 327 328 if (argc >= 2) { /* That's what SVR2 does */ 329 char *fullname = find_dot_file(argv[1]); 330 331 setinputfile(fullname, 1); 332 commandname = fullname; 333 cmdloop(0); 334 popfile(); 335 } 336 return exitstatus; 337} 338 339 340int 341exitcmd(argc, argv) 342 int argc; 343 char **argv; 344{ 345 extern int oexitstatus; 346 347 if (stoppedjobs()) 348 return 0; 349 exitstatus = (argc > 1) ? number(argv[1]) : oexitstatus; 350 exitshell(exitstatus); 351 /*NOTREACHED*/ 352 return 0; 353} 354 355 356#ifdef notdef 357/* 358 * Should never be called. 359 */ 360 361void 362exit(exitstatus) { 363 _exit(exitstatus); 364} 365#endif 366