1 /* $NetBSD: main.c,v 1.17 1995/03/21 09:09:26 cgd 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.1 (Berkeley) 5/31/93"; 48 #else 49 static char rcsid[] = "$NetBSD: main.c,v 1.17 1995/03/21 09:09:26 cgd 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 #include "shell.h" 59 #include "main.h" 60 #include "mail.h" 61 #include "options.h" 62 #include "output.h" 63 #include "parser.h" 64 #include "nodes.h" 65 #include "eval.h" 66 #include "jobs.h" 67 #include "input.h" 68 #include "trap.h" 69 #include "var.h" 70 #include "memalloc.h" 71 #include "error.h" 72 #include "init.h" 73 #include "mystring.h" 74 #include "exec.h" 75 #include "extern.h" 76 77 #define PROFILE 0 78 79 int rootpid; 80 int rootshell; 81 STATIC union node *curcmd; 82 STATIC union node *prevcmd; 83 extern int errno; 84 #if PROFILE 85 short profile_buf[16384]; 86 extern int etext(); 87 #endif 88 89 #ifdef __STDC__ 90 STATIC void read_profile(char *); 91 char *getenv(char *); 92 #else 93 STATIC void read_profile(); 94 char *getenv(); 95 #endif 96 97 98 /* 99 * Main routine. We initialize things, parse the arguments, execute 100 * profiles if we're a login shell, and then call cmdloop to execute 101 * commands. The setjmp call sets up the location to jump to when an 102 * exception occurs. When an exception occurs the variable "state" 103 * is used to figure out how far we had gotten. 104 */ 105 106 int 107 main(argc, argv) 108 int argc; 109 char **argv; 110 { 111 struct jmploc jmploc; 112 struct stackmark smark; 113 volatile int state; 114 char *shinit; 115 116 #if PROFILE 117 monitor(4, etext, profile_buf, sizeof profile_buf, 50); 118 #endif 119 state = 0; 120 if (setjmp(jmploc.loc)) { 121 /* 122 * When a shell procedure is executed, we raise the 123 * exception EXSHELLPROC to clean up before executing 124 * the shell procedure. 125 */ 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 if (argv[0] && argv[0][0] == '-') { 164 state = 1; 165 read_profile("/etc/profile"); 166 state1: 167 state = 2; 168 read_profile(".profile"); 169 } 170 state2: 171 state = 3; 172 if (getuid() == geteuid() && getgid() == getegid()) { 173 if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') { 174 state = 3; 175 read_profile(shinit); 176 } 177 } 178 state3: 179 state = 4; 180 if (minusc) { 181 evalstring(minusc); 182 } 183 if (sflag || minusc == NULL) { 184 state4: /* 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 } 192 193 194 /* 195 * Read and execute commands. "Top" is nonzero for the top level command 196 * loop; it turns on prompting if the shell is interactive. 197 */ 198 199 void 200 cmdloop(top) 201 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); 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 } 238 popstackmark(&smark); /* unnecessary */ 239 } 240 241 242 243 /* 244 * Read /etc/profile or .profile. Return on error. 245 */ 246 247 STATIC void 248 read_profile(name) 249 char *name; 250 { 251 int fd; 252 253 INTOFF; 254 if ((fd = open(name, O_RDONLY)) >= 0) 255 setinputfd(fd, 1); 256 INTON; 257 if (fd < 0) 258 return; 259 cmdloop(0); 260 popfile(); 261 } 262 263 264 265 /* 266 * Read a file containing shell functions. 267 */ 268 269 void 270 readcmdfile(name) 271 char *name; 272 { 273 int fd; 274 275 INTOFF; 276 if ((fd = open(name, O_RDONLY)) >= 0) 277 setinputfd(fd, 1); 278 else 279 error("Can't open %s", name); 280 INTON; 281 cmdloop(0); 282 popfile(); 283 } 284 285 286 287 /* 288 * Take commands from a file. To be compatable we should do a path 289 * search for the file, which is necessary to find sub-commands. 290 */ 291 292 293 static char * 294 find_dot_file(basename) char *basename; { 295 static char localname[FILENAME_MAX+1]; 296 char *fullname; 297 char *path = pathval(); 298 struct stat statb; 299 300 /* don't try this for absolute or relative paths */ 301 if( strchr(basename, '/')) 302 return basename; 303 304 while ((fullname = padvance(&path, basename)) != NULL) { 305 strcpy(localname, fullname); 306 stunalloc(fullname); 307 if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) 308 return localname; 309 } 310 return basename; 311 } 312 313 int 314 dotcmd(argc, argv) 315 int argc; 316 char **argv; 317 { 318 exitstatus = 0; 319 if (argc >= 2) { /* That's what SVR2 does */ 320 char *fullname = find_dot_file(argv[1]); 321 setinputfile(fullname, 1); 322 commandname = fullname; 323 cmdloop(0); 324 popfile(); 325 } 326 return exitstatus; 327 } 328 329 330 int 331 exitcmd(argc, argv) 332 int argc; 333 char **argv; 334 { 335 if (stoppedjobs()) 336 return; 337 if (argc > 1) 338 exitstatus = number(argv[1]); 339 exitshell(exitstatus); 340 } 341 342 343 #ifdef notdef 344 /* 345 * Should never be called. 346 */ 347 348 void 349 exit(exitstatus) { 350 _exit(exitstatus); 351 } 352 #endif 353