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