| 1 | /*      $NetBSD: main.c,v 1.48 2003/09/14 12:09:29 jmmv 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. 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 | #include <sys/cdefs.h>
 | 
|---|
| 36 | #ifndef lint
 | 
|---|
| 37 | __COPYRIGHT("@(#) 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 | #if 0
 | 
|---|
| 43 | static char sccsid[] = "@(#)main.c      8.7 (Berkeley) 7/19/95";
 | 
|---|
| 44 | #else
 | 
|---|
| 45 | __RCSID("$NetBSD: main.c,v 1.48 2003/09/14 12:09:29 jmmv Exp $");
 | 
|---|
| 46 | #endif
 | 
|---|
| 47 | #endif /* not lint */
 | 
|---|
| 48 | 
 | 
|---|
| 49 | #include <errno.h>
 | 
|---|
| 50 | #include <stdio.h>
 | 
|---|
| 51 | #include <signal.h>
 | 
|---|
| 52 | #include <sys/stat.h>
 | 
|---|
| 53 | #include <unistd.h>
 | 
|---|
| 54 | #include <locale.h>
 | 
|---|
| 55 | #include <fcntl.h>
 | 
|---|
| 56 | 
 | 
|---|
| 57 | 
 | 
|---|
| 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 "expand.h"
 | 
|---|
| 66 | #include "eval.h"
 | 
|---|
| 67 | #include "jobs.h"
 | 
|---|
| 68 | #include "input.h"
 | 
|---|
| 69 | #include "trap.h"
 | 
|---|
| 70 | #include "var.h"
 | 
|---|
| 71 | #include "show.h"
 | 
|---|
| 72 | #include "memalloc.h"
 | 
|---|
| 73 | #include "error.h"
 | 
|---|
| 74 | #include "init.h"
 | 
|---|
| 75 | #include "mystring.h"
 | 
|---|
| 76 | #include "exec.h"
 | 
|---|
| 77 | #include "cd.h"
 | 
|---|
| 78 | 
 | 
|---|
| 79 | #define PROFILE 0
 | 
|---|
| 80 | 
 | 
|---|
| 81 | int rootpid;
 | 
|---|
| 82 | int rootshell;
 | 
|---|
| 83 | STATIC union node *curcmd;
 | 
|---|
| 84 | STATIC union node *prevcmd;
 | 
|---|
| 85 | #if PROFILE
 | 
|---|
| 86 | short profile_buf[16384];
 | 
|---|
| 87 | extern int etext();
 | 
|---|
| 88 | #endif
 | 
|---|
| 89 | 
 | 
|---|
| 90 | STATIC void read_profile(const char *);
 | 
|---|
| 91 | STATIC char *find_dot_file(char *);
 | 
|---|
| 92 | int main(int, char **);
 | 
|---|
| 93 | 
 | 
|---|
| 94 | /*
 | 
|---|
| 95 |  * Main routine.  We initialize things, parse the arguments, execute
 | 
|---|
| 96 |  * profiles if we're a login shell, and then call cmdloop to execute
 | 
|---|
| 97 |  * commands.  The setjmp call sets up the location to jump to when an
 | 
|---|
| 98 |  * exception occurs.  When an exception occurs the variable "state"
 | 
|---|
| 99 |  * is used to figure out how far we had gotten.
 | 
|---|
| 100 |  */
 | 
|---|
| 101 | 
 | 
|---|
| 102 | int
 | 
|---|
| 103 | main(int argc, char **argv)
 | 
|---|
| 104 | {
 | 
|---|
| 105 |         struct jmploc jmploc;
 | 
|---|
| 106 |         struct stackmark smark;
 | 
|---|
| 107 |         volatile int state;
 | 
|---|
| 108 |         char *shinit;
 | 
|---|
| 109 | 
 | 
|---|
| 110 |         setlocale(LC_ALL, "");
 | 
|---|
| 111 | 
 | 
|---|
| 112 | #if PROFILE
 | 
|---|
| 113 |         monitor(4, etext, profile_buf, sizeof profile_buf, 50);
 | 
|---|
| 114 | #endif
 | 
|---|
| 115 |         state = 0;
 | 
|---|
| 116 |         if (setjmp(jmploc.loc)) {
 | 
|---|
| 117 |                 /*
 | 
|---|
| 118 |                  * When a shell procedure is executed, we raise the
 | 
|---|
| 119 |                  * exception EXSHELLPROC to clean up before executing
 | 
|---|
| 120 |                  * the shell procedure.
 | 
|---|
| 121 |                  */
 | 
|---|
| 122 |                 switch (exception) {
 | 
|---|
| 123 |                 case EXSHELLPROC:
 | 
|---|
| 124 |                         rootpid = getpid();
 | 
|---|
| 125 |                         rootshell = 1;
 | 
|---|
| 126 |                         minusc = NULL;
 | 
|---|
| 127 |                         state = 3;
 | 
|---|
| 128 |                         break;
 | 
|---|
| 129 | 
 | 
|---|
| 130 |                 case EXEXEC:
 | 
|---|
| 131 |                         exitstatus = exerrno;
 | 
|---|
| 132 |                         break;
 | 
|---|
| 133 | 
 | 
|---|
| 134 |                 case EXERROR:
 | 
|---|
| 135 |                         exitstatus = 2;
 | 
|---|
| 136 |                         break;
 | 
|---|
| 137 | 
 | 
|---|
| 138 |                 default:
 | 
|---|
| 139 |                         break;
 | 
|---|
| 140 |                 }
 | 
|---|
| 141 | 
 | 
|---|
| 142 |                 if (exception != EXSHELLPROC) {
 | 
|---|
| 143 |                         if (state == 0 || iflag == 0 || ! rootshell)
 | 
|---|
| 144 |                                 exitshell(exitstatus);
 | 
|---|
| 145 |                 }
 | 
|---|
| 146 |                 reset();
 | 
|---|
| 147 |                 if (exception == EXINT
 | 
|---|
| 148 | #if ATTY
 | 
|---|
| 149 |                  && (! attyset() || equal(termval(), "emacs"))
 | 
|---|
| 150 | #endif
 | 
|---|
| 151 |                  ) {
 | 
|---|
| 152 |                         out2c('\n');
 | 
|---|
| 153 |                         flushout(&errout);
 | 
|---|
| 154 |                 }
 | 
|---|
| 155 |                 popstackmark(&smark);
 | 
|---|
| 156 |                 FORCEINTON;                             /* enable interrupts */
 | 
|---|
| 157 |                 if (state == 1)
 | 
|---|
| 158 |                         goto state1;
 | 
|---|
| 159 |                 else if (state == 2)
 | 
|---|
| 160 |                         goto state2;
 | 
|---|
| 161 |                 else if (state == 3)
 | 
|---|
| 162 |                         goto state3;
 | 
|---|
| 163 |                 else
 | 
|---|
| 164 |                         goto state4;
 | 
|---|
| 165 |         }
 | 
|---|
| 166 |         handler = &jmploc;
 | 
|---|
| 167 | #ifdef DEBUG
 | 
|---|
| 168 | #if DEBUG == 2
 | 
|---|
| 169 |         debug = 1;
 | 
|---|
| 170 | #endif
 | 
|---|
| 171 |         opentrace();
 | 
|---|
| 172 |         trputs("Shell args:  ");  trargs(argv);
 | 
|---|
| 173 | #endif
 | 
|---|
| 174 |         rootpid = getpid();
 | 
|---|
| 175 |         rootshell = 1;
 | 
|---|
| 176 |         init();
 | 
|---|
| 177 |         setstackmark(&smark);
 | 
|---|
| 178 |         procargs(argc, argv);
 | 
|---|
| 179 |         if (argv[0] && argv[0][0] == '-') {
 | 
|---|
| 180 |                 state = 1;
 | 
|---|
| 181 |                 read_profile("/etc/profile");
 | 
|---|
| 182 | state1:
 | 
|---|
| 183 |                 state = 2;
 | 
|---|
| 184 |                 read_profile(".profile");
 | 
|---|
| 185 |         }
 | 
|---|
| 186 | state2:
 | 
|---|
| 187 |         state = 3;
 | 
|---|
| 188 |         if (getuid() == geteuid() && getgid() == getegid()) {
 | 
|---|
| 189 |                 if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
 | 
|---|
| 190 |                         state = 3;
 | 
|---|
| 191 |                         read_profile(shinit);
 | 
|---|
| 192 |                 }
 | 
|---|
| 193 |         }
 | 
|---|
| 194 | state3:
 | 
|---|
| 195 |         state = 4;
 | 
|---|
| 196 |         if (sflag == 0 || minusc) {
 | 
|---|
| 197 |                 static int sigs[] =  {
 | 
|---|
| 198 |                     SIGINT, SIGQUIT, SIGHUP, 
 | 
|---|
| 199 | #ifdef SIGTSTP
 | 
|---|
| 200 |                     SIGTSTP,
 | 
|---|
| 201 | #endif
 | 
|---|
| 202 |                     SIGPIPE
 | 
|---|
| 203 |                 };
 | 
|---|
| 204 | #define SIGSSIZE (sizeof(sigs)/sizeof(sigs[0]))
 | 
|---|
| 205 |                 int i;
 | 
|---|
| 206 | 
 | 
|---|
| 207 |                 for (i = 0; i < SIGSSIZE; i++)
 | 
|---|
| 208 |                     setsignal(sigs[i], 0);
 | 
|---|
| 209 |         }
 | 
|---|
| 210 | 
 | 
|---|
| 211 |         if (minusc)
 | 
|---|
| 212 |                 evalstring(minusc, 0);
 | 
|---|
| 213 | 
 | 
|---|
| 214 |         if (sflag || minusc == NULL) {
 | 
|---|
| 215 | state4: /* XXX ??? - why isn't this before the "if" statement */
 | 
|---|
| 216 |                 cmdloop(1);
 | 
|---|
| 217 |         }
 | 
|---|
| 218 | #if PROFILE
 | 
|---|
| 219 |         monitor(0);
 | 
|---|
| 220 | #endif
 | 
|---|
| 221 |         exitshell(exitstatus);
 | 
|---|
| 222 |         /* NOTREACHED */
 | 
|---|
| 223 | }
 | 
|---|
| 224 | 
 | 
|---|
| 225 | 
 | 
|---|
| 226 | /*
 | 
|---|
| 227 |  * Read and execute commands.  "Top" is nonzero for the top level command
 | 
|---|
| 228 |  * loop; it turns on prompting if the shell is interactive.
 | 
|---|
| 229 |  */
 | 
|---|
| 230 | 
 | 
|---|
| 231 | void
 | 
|---|
| 232 | cmdloop(int top)
 | 
|---|
| 233 | {
 | 
|---|
| 234 |         union node *n;
 | 
|---|
| 235 |         struct stackmark smark;
 | 
|---|
| 236 |         int inter;
 | 
|---|
| 237 |         int numeof = 0;
 | 
|---|
| 238 | 
 | 
|---|
| 239 |         TRACE(("cmdloop(%d) called\n", top));
 | 
|---|
| 240 |         setstackmark(&smark);
 | 
|---|
| 241 |         for (;;) {
 | 
|---|
| 242 |                 if (pendingsigs)
 | 
|---|
| 243 |                         dotrap();
 | 
|---|
| 244 |                 inter = 0;
 | 
|---|
| 245 |                 if (iflag && top) {
 | 
|---|
| 246 |                         inter = 1;
 | 
|---|
| 247 |                         showjobs(out2, SHOW_CHANGED);
 | 
|---|
| 248 |                         chkmail(0);
 | 
|---|
| 249 |                         flushout(&errout);
 | 
|---|
| 250 |                 }
 | 
|---|
| 251 |                 n = parsecmd(inter);
 | 
|---|
| 252 |                 /* showtree(n); DEBUG */
 | 
|---|
| 253 |                 if (n == NEOF) {
 | 
|---|
| 254 |                         if (!top || numeof >= 50)
 | 
|---|
| 255 |                                 break;
 | 
|---|
| 256 |                         if (!stoppedjobs()) {
 | 
|---|
| 257 |                                 if (!Iflag)
 | 
|---|
| 258 |                                         break;
 | 
|---|
| 259 |                                 out2str("\nUse \"exit\" to leave shell.\n");
 | 
|---|
| 260 |                         }
 | 
|---|
| 261 |                         numeof++;
 | 
|---|
| 262 |                 } else if (n != NULL && nflag == 0) {
 | 
|---|
| 263 |                         job_warning = (job_warning == 2) ? 1 : 0;
 | 
|---|
| 264 |                         numeof = 0;
 | 
|---|
| 265 |                         evaltree(n, 0);
 | 
|---|
| 266 |                 }
 | 
|---|
| 267 |                 popstackmark(&smark);
 | 
|---|
| 268 |                 setstackmark(&smark);
 | 
|---|
| 269 |                 if (evalskip == SKIPFILE) {
 | 
|---|
| 270 |                         evalskip = 0;
 | 
|---|
| 271 |                         break;
 | 
|---|
| 272 |                 }
 | 
|---|
| 273 |         }
 | 
|---|
| 274 |         popstackmark(&smark);
 | 
|---|
| 275 | }
 | 
|---|
| 276 | 
 | 
|---|
| 277 | 
 | 
|---|
| 278 | 
 | 
|---|
| 279 | /*
 | 
|---|
| 280 |  * Read /etc/profile or .profile.  Return on error.
 | 
|---|
| 281 |  */
 | 
|---|
| 282 | 
 | 
|---|
| 283 | STATIC void
 | 
|---|
| 284 | read_profile(const char *name)
 | 
|---|
| 285 | {
 | 
|---|
| 286 |         int fd;
 | 
|---|
| 287 |         int xflag_set = 0;
 | 
|---|
| 288 |         int vflag_set = 0;
 | 
|---|
| 289 | 
 | 
|---|
| 290 |         INTOFF;
 | 
|---|
| 291 |         if ((fd = open(name, O_RDONLY)) >= 0)
 | 
|---|
| 292 |                 setinputfd(fd, 1);
 | 
|---|
| 293 |         INTON;
 | 
|---|
| 294 |         if (fd < 0)
 | 
|---|
| 295 |                 return;
 | 
|---|
| 296 |         /* -q turns off -x and -v just when executing init files */
 | 
|---|
| 297 |         if (qflag)  {
 | 
|---|
| 298 |             if (xflag)
 | 
|---|
| 299 |                     xflag = 0, xflag_set = 1;
 | 
|---|
| 300 |             if (vflag)
 | 
|---|
| 301 |                     vflag = 0, vflag_set = 1;
 | 
|---|
| 302 |         }
 | 
|---|
| 303 |         cmdloop(0);
 | 
|---|
| 304 |         if (qflag)  {
 | 
|---|
| 305 |             if (xflag_set)
 | 
|---|
| 306 |                     xflag = 1;
 | 
|---|
| 307 |             if (vflag_set)
 | 
|---|
| 308 |                     vflag = 1;
 | 
|---|
| 309 |         }
 | 
|---|
| 310 |         popfile();
 | 
|---|
| 311 | }
 | 
|---|
| 312 | 
 | 
|---|
| 313 | 
 | 
|---|
| 314 | 
 | 
|---|
| 315 | /*
 | 
|---|
| 316 |  * Read a file containing shell functions.
 | 
|---|
| 317 |  */
 | 
|---|
| 318 | 
 | 
|---|
| 319 | void
 | 
|---|
| 320 | readcmdfile(char *name)
 | 
|---|
| 321 | {
 | 
|---|
| 322 |         int fd;
 | 
|---|
| 323 | 
 | 
|---|
| 324 |         INTOFF;
 | 
|---|
| 325 |         if ((fd = open(name, O_RDONLY)) >= 0)
 | 
|---|
| 326 |                 setinputfd(fd, 1);
 | 
|---|
| 327 |         else
 | 
|---|
| 328 |                 error("Can't open %s", name);
 | 
|---|
| 329 |         INTON;
 | 
|---|
| 330 |         cmdloop(0);
 | 
|---|
| 331 |         popfile();
 | 
|---|
| 332 | }
 | 
|---|
| 333 | 
 | 
|---|
| 334 | 
 | 
|---|
| 335 | 
 | 
|---|
| 336 | /*
 | 
|---|
| 337 |  * Take commands from a file.  To be compatible we should do a path
 | 
|---|
| 338 |  * search for the file, which is necessary to find sub-commands.
 | 
|---|
| 339 |  */
 | 
|---|
| 340 | 
 | 
|---|
| 341 | 
 | 
|---|
| 342 | STATIC char *
 | 
|---|
| 343 | find_dot_file(char *basename)
 | 
|---|
| 344 | {
 | 
|---|
| 345 |         char *fullname;
 | 
|---|
| 346 |         const char *path = pathval();
 | 
|---|
| 347 |         struct stat statb;
 | 
|---|
| 348 | 
 | 
|---|
| 349 |         /* don't try this for absolute or relative paths */
 | 
|---|
| 350 |         if (strchr(basename, '/'))
 | 
|---|
| 351 |                 return basename;
 | 
|---|
| 352 | 
 | 
|---|
| 353 |         while ((fullname = padvance(&path, basename)) != NULL) {
 | 
|---|
| 354 |                 if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) {
 | 
|---|
| 355 |                         /*
 | 
|---|
| 356 |                          * Don't bother freeing here, since it will
 | 
|---|
| 357 |                          * be freed by the caller.
 | 
|---|
| 358 |                          */
 | 
|---|
| 359 |                         return fullname;
 | 
|---|
| 360 |                 }
 | 
|---|
| 361 |                 stunalloc(fullname);
 | 
|---|
| 362 |         }
 | 
|---|
| 363 | 
 | 
|---|
| 364 |         /* not found in the PATH */
 | 
|---|
| 365 |         error("%s: not found", basename);
 | 
|---|
| 366 |         /* NOTREACHED */
 | 
|---|
| 367 | }
 | 
|---|
| 368 | 
 | 
|---|
| 369 | int
 | 
|---|
| 370 | dotcmd(int argc, char **argv)
 | 
|---|
| 371 | {
 | 
|---|
| 372 |         exitstatus = 0;
 | 
|---|
| 373 | 
 | 
|---|
| 374 |         if (argc >= 2) {                /* That's what SVR2 does */
 | 
|---|
| 375 |                 char *fullname;
 | 
|---|
| 376 |                 struct stackmark smark;
 | 
|---|
| 377 | 
 | 
|---|
| 378 |                 setstackmark(&smark);
 | 
|---|
| 379 |                 fullname = find_dot_file(argv[1]);
 | 
|---|
| 380 |                 setinputfile(fullname, 1);
 | 
|---|
| 381 |                 commandname = fullname;
 | 
|---|
| 382 |                 cmdloop(0);
 | 
|---|
| 383 |                 popfile();
 | 
|---|
| 384 |                 popstackmark(&smark);
 | 
|---|
| 385 |         }
 | 
|---|
| 386 |         return exitstatus;
 | 
|---|
| 387 | }
 | 
|---|
| 388 | 
 | 
|---|
| 389 | 
 | 
|---|
| 390 | int
 | 
|---|
| 391 | exitcmd(int argc, char **argv)
 | 
|---|
| 392 | {
 | 
|---|
| 393 |         if (stoppedjobs())
 | 
|---|
| 394 |                 return 0;
 | 
|---|
| 395 |         if (argc > 1)
 | 
|---|
| 396 |                 exitstatus = number(argv[1]);
 | 
|---|
| 397 |         exitshell(exitstatus);
 | 
|---|
| 398 |         /* NOTREACHED */
 | 
|---|
| 399 | }
 | 
|---|