1 /* $NetBSD: main.c,v 1.23 1995/11/19 23:27:42 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.23 1995/11/19 23:27:42 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 if (exception == EXERROR) 125 exitstatus = 2; 126 if (exception == EXSHELLPROC) { 127 rootpid = getpid(); 128 rootshell = 1; 129 minusc = NULL; 130 state = 3; 131 } else if (state == 0 || iflag == 0 || ! rootshell) 132 exitshell(2); 133 reset(); 134 if (exception == EXINT 135 #if ATTY 136 && (! attyset() || equal(termval(), "emacs")) 137 #endif 138 ) { 139 out2c('\n'); 140 flushout(&errout); 141 } 142 popstackmark(&smark); 143 FORCEINTON; /* enable interrupts */ 144 if (state == 1) 145 goto state1; 146 else if (state == 2) 147 goto state2; 148 else if (state == 3) 149 goto state3; 150 else 151 goto state4; 152 } 153 handler = &jmploc; 154 #ifdef DEBUG 155 opentrace(); 156 trputs("Shell args: "); trargs(argv); 157 #endif 158 rootpid = getpid(); 159 rootshell = 1; 160 init(); 161 setstackmark(&smark); 162 procargs(argc, argv); 163 getpwd(); 164 if (argv[0] && argv[0][0] == '-') { 165 state = 1; 166 read_profile("/etc/profile"); 167 state1: 168 state = 2; 169 read_profile(".profile"); 170 } 171 state2: 172 state = 3; 173 if (getuid() == geteuid() && getgid() == getegid()) { 174 if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') { 175 state = 3; 176 read_profile(shinit); 177 } 178 } 179 state3: 180 state = 4; 181 if (minusc) { 182 evalstring(minusc); 183 } 184 if (sflag || minusc == NULL) { 185 state4: /* XXX ??? - why isn't this before the "if" statement */ 186 cmdloop(1); 187 } 188 #if PROFILE 189 monitor(0); 190 #endif 191 exitshell(exitstatus); 192 /*NOTREACHED*/ 193 return 0; 194 } 195 196 197 /* 198 * Read and execute commands. "Top" is nonzero for the top level command 199 * loop; it turns on prompting if the shell is interactive. 200 */ 201 202 void 203 cmdloop(top) 204 int top; 205 { 206 union node *n; 207 struct stackmark smark; 208 int inter; 209 int numeof = 0; 210 211 TRACE(("cmdloop(%d) called\n", top)); 212 setstackmark(&smark); 213 for (;;) { 214 if (pendingsigs) 215 dotrap(); 216 inter = 0; 217 if (iflag && top) { 218 inter++; 219 showjobs(1); 220 chkmail(0); 221 flushout(&output); 222 } 223 n = parsecmd(inter); 224 /* showtree(n); DEBUG */ 225 if (n == NEOF) { 226 if (!top || numeof >= 50) 227 break; 228 if (!stoppedjobs()) { 229 if (!Iflag) 230 break; 231 out2str("\nUse \"exit\" to leave shell.\n"); 232 } 233 numeof++; 234 } else if (n != NULL && nflag == 0) { 235 job_warning = (job_warning == 2) ? 1 : 0; 236 numeof = 0; 237 evaltree(n, 0); 238 } 239 popstackmark(&smark); 240 if (evalskip == SKIPFILE) { 241 evalskip = 0; 242 break; 243 } 244 } 245 popstackmark(&smark); /* unnecessary */ 246 } 247 248 249 250 /* 251 * Read /etc/profile or .profile. Return on error. 252 */ 253 254 STATIC void 255 read_profile(name) 256 char *name; 257 { 258 int fd; 259 260 INTOFF; 261 if ((fd = open(name, O_RDONLY)) >= 0) 262 setinputfd(fd, 1); 263 INTON; 264 if (fd < 0) 265 return; 266 cmdloop(0); 267 popfile(); 268 } 269 270 271 272 /* 273 * Read a file containing shell functions. 274 */ 275 276 void 277 readcmdfile(name) 278 char *name; 279 { 280 int fd; 281 282 INTOFF; 283 if ((fd = open(name, O_RDONLY)) >= 0) 284 setinputfd(fd, 1); 285 else 286 error("Can't open %s", name); 287 INTON; 288 cmdloop(0); 289 popfile(); 290 } 291 292 293 294 /* 295 * Take commands from a file. To be compatable we should do a path 296 * search for the file, which is necessary to find sub-commands. 297 */ 298 299 300 STATIC char * 301 find_dot_file(basename) 302 char *basename; 303 { 304 static char localname[FILENAME_MAX+1]; 305 char *fullname; 306 char *path = pathval(); 307 struct stat statb; 308 309 /* don't try this for absolute or relative paths */ 310 if( strchr(basename, '/')) 311 return basename; 312 313 while ((fullname = padvance(&path, basename)) != NULL) { 314 strcpy(localname, fullname); 315 stunalloc(fullname); 316 if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) 317 return localname; 318 } 319 return basename; 320 } 321 322 int 323 dotcmd(argc, argv) 324 int argc; 325 char **argv; 326 { 327 struct strlist *sp; 328 exitstatus = 0; 329 330 for (sp = cmdenviron; sp ; sp = sp->next) 331 setvareq(savestr(sp->text), VSTRFIXED|VTEXTFIXED); 332 333 if (argc >= 2) { /* That's what SVR2 does */ 334 char *fullname = find_dot_file(argv[1]); 335 336 setinputfile(fullname, 1); 337 commandname = fullname; 338 cmdloop(0); 339 popfile(); 340 } 341 return exitstatus; 342 } 343 344 345 int 346 exitcmd(argc, argv) 347 int argc; 348 char **argv; 349 { 350 extern int oexitstatus; 351 352 if (stoppedjobs()) 353 return 0; 354 if (argc > 1) 355 exitstatus = number(argv[1]); 356 else 357 exitstatus = oexitstatus; 358 exitshell(exitstatus); 359 /*NOTREACHED*/ 360 return 0; 361 } 362 363 364 #ifdef notdef 365 /* 366 * Should never be called. 367 */ 368 369 void 370 exit(exitstatus) { 371 _exit(exitstatus); 372 } 373 #endif 374