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