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