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. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#) Copyright (c) 1991, 1993 The Regents of the University of California. All rights reserved. 33 * @(#)main.c 8.6 (Berkeley) 5/28/95 34 * $FreeBSD: head/bin/sh/main.c 253650 2013-07-25 15:08:41Z jilles $ 35 */ 36 37 #include <stdio.h> 38 #include <signal.h> 39 #include <sys/stat.h> 40 #include <unistd.h> 41 #include <fcntl.h> 42 #include <locale.h> 43 #include <errno.h> 44 45 #include "shell.h" 46 #include "main.h" 47 #include "mail.h" 48 #include "options.h" 49 #include "output.h" 50 #include "parser.h" 51 #include "nodes.h" 52 #include "expand.h" 53 #include "eval.h" 54 #include "jobs.h" 55 #include "input.h" 56 #include "trap.h" 57 #include "var.h" 58 #include "show.h" 59 #include "memalloc.h" 60 #include "error.h" 61 #include "mystring.h" 62 #include "exec.h" 63 #include "cd.h" 64 #include "redir.h" 65 #include "builtins.h" 66 67 int rootpid; 68 int rootshell; 69 struct jmploc main_handler; 70 int localeisutf8, initial_localeisutf8; 71 72 static void reset(void); 73 static void cmdloop(int); 74 static void read_profile(const char *); 75 static const char *find_dot_file(const char *); 76 77 /* 78 * Main routine. We initialize things, parse the arguments, execute 79 * profiles if we're a login shell, and then call cmdloop to execute 80 * commands. The setjmp call sets up the location to jump to when an 81 * exception occurs. When an exception occurs the variable "state" 82 * is used to figure out how far we had gotten. 83 */ 84 85 int 86 main(int argc, char *argv[]) 87 { 88 struct stackmark smark, smark2; 89 volatile int state; 90 char *shinit; 91 92 setlocale(LC_ALL, ""); 93 initcharset(); 94 state = 0; 95 if (setjmp(main_handler.loc)) { 96 switch (exception) { 97 case EXEXEC: 98 exitstatus = exerrno; 99 break; 100 101 case EXERROR: 102 exitstatus = 2; 103 break; 104 105 default: 106 break; 107 } 108 109 if (state == 0 || iflag == 0 || ! rootshell || 110 exception == EXEXIT) 111 exitshell(exitstatus); 112 reset(); 113 if (exception == EXINT) 114 out2fmt_flush("\n"); 115 popstackmark(&smark); 116 FORCEINTON; /* enable interrupts */ 117 if (state == 1) 118 goto state1; 119 else if (state == 2) 120 goto state2; 121 else if (state == 3) 122 goto state3; 123 else 124 goto state4; 125 } 126 handler = &main_handler; 127 #ifdef DEBUG 128 opentrace(); 129 trputs("Shell args: "); trargs(argv); 130 #endif 131 rootpid = getpid(); 132 rootshell = 1; 133 initvar(); 134 setstackmark(&smark); 135 setstackmark(&smark2); 136 procargs(argc, argv); 137 pwd_init(iflag); 138 if (iflag) 139 chkmail(1); 140 if (argv[0] && argv[0][0] == '-') { 141 state = 1; 142 read_profile("/etc/profile"); 143 state1: 144 state = 2; 145 if (privileged == 0) 146 read_profile("${HOME-}/.profile"); 147 else 148 read_profile("/etc/suid_profile"); 149 } 150 state2: 151 state = 3; 152 if (!privileged && iflag) { 153 if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') { 154 state = 3; 155 read_profile(shinit); 156 } 157 } 158 state3: 159 state = 4; 160 popstackmark(&smark2); 161 if (minusc) { 162 evalstring(minusc, sflag ? 0 : EV_EXIT); 163 } 164 state4: 165 if (sflag || minusc == NULL) { 166 cmdloop(1); 167 } 168 exitshell(exitstatus); 169 /*NOTREACHED*/ 170 return 0; 171 } 172 173 static void 174 reset(void) 175 { 176 reseteval(); 177 resetinput(); 178 resetparser(); 179 resetredir(); 180 } 181 182 /* 183 * Read and execute commands. "Top" is nonzero for the top level command 184 * loop; it turns on prompting if the shell is interactive. 185 */ 186 187 static void 188 cmdloop(int top) 189 { 190 union node *n; 191 struct stackmark smark; 192 int inter; 193 int numeof = 0; 194 195 TRACE(("cmdloop(%d) called\n", top)); 196 setstackmark(&smark); 197 for (;;) { 198 if (pendingsig) 199 dotrap(); 200 inter = 0; 201 if (iflag && top) { 202 inter++; 203 showjobs(1, SHOWJOBS_DEFAULT); 204 chkmail(0); 205 flushout(&output); 206 } 207 n = parsecmd(inter); 208 /* showtree(n); DEBUG */ 209 if (n == NEOF) { 210 if (!top || numeof >= 50) 211 break; 212 if (!stoppedjobs()) { 213 if (!Iflag) 214 break; 215 out2fmt_flush("\nUse \"exit\" to leave shell.\n"); 216 } 217 numeof++; 218 } else if (n != NULL && nflag == 0) { 219 job_warning = (job_warning == 2) ? 1 : 0; 220 numeof = 0; 221 evaltree(n, 0); 222 } 223 popstackmark(&smark); 224 setstackmark(&smark); 225 if (evalskip != 0) { 226 if (evalskip == SKIPFILE) 227 evalskip = 0; 228 break; 229 } 230 } 231 popstackmark(&smark); 232 } 233 234 235 236 /* 237 * Read /etc/profile or .profile. Return on error. 238 */ 239 240 static void 241 read_profile(const char *name) 242 { 243 int fd; 244 const char *expandedname; 245 246 expandedname = expandstr(__DECONST(char *, name)); 247 if (expandedname == NULL) 248 return; 249 INTOFF; 250 #ifndef O_CLOEXEC 251 if ((fd = open(expandedname, O_RDONLY)) >= 0) 252 #else 253 if ((fd = open(expandedname, O_RDONLY | O_CLOEXEC)) >= 0) 254 #endif 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(const char *name) 271 { 272 setinputfile(name, 1); 273 cmdloop(0); 274 popfile(); 275 } 276 277 278 279 /* 280 * Take commands from a file. To be compatible we should do a path 281 * search for the file, which is necessary to find sub-commands. 282 */ 283 284 285 static const char * 286 find_dot_file(const char *basename) 287 { 288 char *fullname; 289 const char *path = pathval(); 290 struct stat statb; 291 292 /* don't try this for absolute or relative paths */ 293 if( strchr(basename, '/')) 294 return basename; 295 296 while ((fullname = padvance(&path, basename)) != NULL) { 297 if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) { 298 /* 299 * Don't bother freeing here, since it will 300 * be freed by the caller. 301 */ 302 return fullname; 303 } 304 stunalloc(fullname); 305 } 306 return basename; 307 } 308 309 int 310 dotcmd(int argc, char **argv) 311 { 312 const char *fullname; 313 char *filename; 314 315 if (argc < 2) 316 error("missing filename"); 317 318 exitstatus = 0; 319 320 /* 321 * Because we have historically not supported any options, 322 * only treat "--" specially. 323 */ 324 filename = argc > 2 && strcmp(argv[1], "--") == 0 ? argv[2] : argv[1]; 325 326 fullname = find_dot_file(filename); 327 setinputfile(fullname, 1); 328 commandname = fullname; 329 cmdloop(0); 330 popfile(); 331 return exitstatus; 332 } 333 334 335 int 336 exitcmd(int argc, char **argv) 337 { 338 if (stoppedjobs()) 339 return 0; 340 if (argc > 1) 341 exitshell(number(argv[1])); 342 else 343 exitshell_savedstatus(); 344 } 345