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 * @(#) Copyright (c) 1991, 1993 The Regents of the University of California. All rights reserved. 37 * @(#)main.c 8.6 (Berkeley) 5/28/95 38 * $FreeBSD: src/bin/sh/main.c,v 1.18.2.3 2002/07/19 04:38:51 tjr Exp $ 39 * $DragonFly: src/bin/sh/main.c,v 1.6 2006/09/28 22:29:44 pavalos Exp $ 40 */ 41 42 #include <stdio.h> 43 #include <signal.h> 44 #include <sys/stat.h> 45 #include <unistd.h> 46 #include <fcntl.h> 47 #include <locale.h> 48 #include <errno.h> 49 50 #include "shell.h" 51 #include "main.h" 52 #include "mail.h" 53 #include "options.h" 54 #include "output.h" 55 #include "parser.h" 56 #include "nodes.h" 57 #include "expand.h" 58 #include "eval.h" 59 #include "jobs.h" 60 #include "input.h" 61 #include "trap.h" 62 #include "var.h" 63 #include "show.h" 64 #include "memalloc.h" 65 #include "error.h" 66 #include "init.h" 67 #include "mystring.h" 68 #include "exec.h" 69 #include "cd.h" 70 71 #define PROFILE 0 72 73 int rootpid; 74 int rootshell; 75 #if PROFILE 76 short profile_buf[16384]; 77 extern int etext(); 78 #endif 79 80 STATIC void read_profile(const char *); 81 STATIC const char *find_dot_file(const char *); 82 83 extern int oexitstatus; 84 85 /* 86 * Main routine. We initialize things, parse the arguments, execute 87 * profiles if we're a login shell, and then call cmdloop to execute 88 * commands. The setjmp call sets up the location to jump to when an 89 * exception occurs. When an exception occurs the variable "state" 90 * is used to figure out how far we had gotten. 91 */ 92 93 int 94 main(int argc, char *argv[]) 95 { 96 struct jmploc jmploc; 97 struct stackmark smark; 98 volatile int state; 99 char *shinit; 100 101 #if PROFILE 102 monitor(4, etext, profile_buf, sizeof profile_buf, 50); 103 #endif 104 setlocale(LC_ALL, ""); 105 state = 0; 106 if (setjmp(jmploc.loc)) { 107 /* 108 * When a shell procedure is executed, we raise the 109 * exception EXSHELLPROC to clean up before executing 110 * the shell procedure. 111 */ 112 switch (exception) { 113 case EXSHELLPROC: 114 rootpid = getpid(); 115 rootshell = 1; 116 minusc = NULL; 117 state = 3; 118 break; 119 120 case EXEXEC: 121 exitstatus = exerrno; 122 break; 123 124 case EXERROR: 125 exitstatus = 2; 126 break; 127 128 default: 129 break; 130 } 131 132 if (exception != EXSHELLPROC) { 133 if (state == 0 || iflag == 0 || ! rootshell) 134 exitshell(exitstatus); 135 } 136 reset(); 137 if (exception == EXINT) { 138 out2c('\n'); 139 flushout(&errout); 140 } 141 popstackmark(&smark); 142 FORCEINTON; /* enable interrupts */ 143 if (state == 1) 144 goto state1; 145 else if (state == 2) 146 goto state2; 147 else if (state == 3) 148 goto state3; 149 else 150 goto state4; 151 } 152 handler = &jmploc; 153 #ifdef DEBUG 154 opentrace(); 155 trputs("Shell args: "); trargs(argv); 156 #endif 157 rootpid = getpid(); 158 rootshell = 1; 159 init(); 160 setstackmark(&smark); 161 procargs(argc, argv); 162 if (getpwd() == NULL && iflag) 163 out2str("sh: cannot determine working directory\n"); 164 if (argv[0] && argv[0][0] == '-') { 165 state = 1; 166 read_profile("/etc/profile"); 167 state1: 168 state = 2; 169 if (privileged == 0) 170 read_profile(".profile"); 171 else 172 read_profile("/etc/suid_profile"); 173 } 174 state2: 175 state = 3; 176 if (!privileged && iflag) { 177 if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') { 178 state = 3; 179 read_profile(shinit); 180 } 181 } 182 state3: 183 state = 4; 184 if (minusc) { 185 evalstring(minusc); 186 } 187 if (sflag || minusc == NULL) { 188 state4: /* XXX ??? - why isn't this before the "if" statement */ 189 cmdloop(1); 190 } 191 #if PROFILE 192 monitor(0); 193 #endif 194 exitshell(exitstatus); 195 /*NOTREACHED*/ 196 return 0; 197 } 198 199 200 /* 201 * Read and execute commands. "Top" is nonzero for the top level command 202 * loop; it turns on prompting if the shell is interactive. 203 */ 204 205 void 206 cmdloop(int top) 207 { 208 union node *n; 209 struct stackmark smark; 210 int inter; 211 int numeof = 0; 212 213 TRACE(("cmdloop(%d) called\n", top)); 214 setstackmark(&smark); 215 for (;;) { 216 if (pendingsigs) 217 dotrap(); 218 inter = 0; 219 if (iflag && top) { 220 inter++; 221 showjobs(1, 0, 0); 222 chkmail(0); 223 flushout(&output); 224 } 225 n = parsecmd(inter); 226 /* showtree(n); DEBUG */ 227 if (n == NEOF) { 228 if (!top || numeof >= 50) 229 break; 230 if (!stoppedjobs()) { 231 if (!Iflag) 232 break; 233 out2str("\nUse \"exit\" to leave shell.\n"); 234 } 235 numeof++; 236 } else if (n != NULL && nflag == 0) { 237 job_warning = (job_warning == 2) ? 1 : 0; 238 numeof = 0; 239 evaltree(n, 0); 240 } 241 popstackmark(&smark); 242 setstackmark(&smark); 243 if (evalskip == SKIPFILE) { 244 evalskip = 0; 245 break; 246 } 247 } 248 popstackmark(&smark); 249 } 250 251 252 253 /* 254 * Read /etc/profile or .profile. Return on error. 255 */ 256 257 STATIC void 258 read_profile(const char *name) 259 { 260 int fd; 261 262 INTOFF; 263 if ((fd = open(name, O_RDONLY)) >= 0) 264 setinputfd(fd, 1); 265 INTON; 266 if (fd < 0) 267 return; 268 cmdloop(0); 269 popfile(); 270 } 271 272 273 274 /* 275 * Read a file containing shell functions. 276 */ 277 278 void 279 readcmdfile(char *name) 280 { 281 int fd; 282 283 INTOFF; 284 if ((fd = open(name, O_RDONLY)) >= 0) 285 setinputfd(fd, 1); 286 else 287 error("Can't open %s: %s", name, strerror(errno)); 288 INTON; 289 cmdloop(0); 290 popfile(); 291 } 292 293 294 295 /* 296 * Take commands from a file. To be compatible we should do a path 297 * search for the file, which is necessary to find sub-commands. 298 */ 299 300 301 STATIC const char * 302 find_dot_file(const char *basename) 303 { 304 static char localname[FILENAME_MAX+1]; 305 char *fullname; 306 const 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(int argc, char **argv) 324 { 325 struct strlist *sp; 326 exitstatus = 0; 327 328 for (sp = cmdenviron; sp ; sp = sp->next) 329 setvareq(savestr(sp->text), VSTRFIXED|VTEXTFIXED); 330 331 if (argc >= 2) { /* That's what SVR2 does */ 332 const char *fullname = find_dot_file(argv[1]); 333 334 setinputfile(fullname, 1); 335 commandname = fullname; 336 cmdloop(0); 337 popfile(); 338 } 339 return exitstatus; 340 } 341 342 343 int 344 exitcmd(int argc, char **argv) 345 { 346 if (stoppedjobs()) 347 return 0; 348 if (argc > 1) 349 exitstatus = number(argv[1]); 350 else 351 exitstatus = oexitstatus; 352 exitshell(exitstatus); 353 /*NOTREACHED*/ 354 return 0; 355 } 356