[9] | 1 | /*-
|
---|
| 2 | * Copyright (c) 1991 The Regents of the University of California.
|
---|
| 3 | * 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 |
|
---|
| 37 | #ifndef lint
|
---|
| 38 | char copyright[] =
|
---|
| 39 | "@(#) Copyright (c) 1991 The Regents of the University of California.\n\
|
---|
| 40 | All rights reserved.\n";
|
---|
| 41 | #endif /* not lint */
|
---|
| 42 |
|
---|
| 43 | #ifndef lint
|
---|
| 44 | static char sccsid[] = "@(#)main.c 5.2 (Berkeley) 3/13/91";
|
---|
| 45 | #endif /* not lint */
|
---|
| 46 |
|
---|
| 47 | #include <sys/types.h>
|
---|
| 48 | #include <signal.h>
|
---|
| 49 | #include <fcntl.h>
|
---|
| 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 "eval.h"
|
---|
| 58 | #include "jobs.h"
|
---|
| 59 | #include "input.h"
|
---|
| 60 | #include "trap.h"
|
---|
| 61 | #if ATTY
|
---|
| 62 | #include "var.h"
|
---|
| 63 | #endif
|
---|
| 64 | #include "memalloc.h"
|
---|
| 65 | #include "error.h"
|
---|
| 66 | #include "init.h"
|
---|
| 67 | #include "mystring.h"
|
---|
| 68 |
|
---|
| 69 | #define PROFILE 0
|
---|
| 70 |
|
---|
| 71 | int rootpid;
|
---|
| 72 | int rootshell;
|
---|
| 73 | STATIC union node *curcmd;
|
---|
| 74 | STATIC union node *prevcmd;
|
---|
| 75 | extern int errno;
|
---|
| 76 | #if PROFILE
|
---|
| 77 | short profile_buf[16384];
|
---|
| 78 | extern int etext();
|
---|
| 79 | #endif
|
---|
| 80 |
|
---|
| 81 | #ifdef __STDC__
|
---|
| 82 | STATIC void read_profile(char *);
|
---|
| 83 | char *getenv(char *);
|
---|
| 84 | #else
|
---|
| 85 | STATIC void read_profile();
|
---|
| 86 | char *getenv();
|
---|
| 87 | #endif
|
---|
| 88 |
|
---|
| 89 |
|
---|
| 90 | /*
|
---|
| 91 | * Main routine. We initialize things, parse the arguments, execute
|
---|
| 92 | * profiles if we're a login shell, and then call cmdloop to execute
|
---|
| 93 | * commands. The setjmp call sets up the location to jump to when an
|
---|
| 94 | * exception occurs. When an exception occurs the variable "state"
|
---|
| 95 | * is used to figure out how far we had gotten.
|
---|
| 96 | */
|
---|
| 97 |
|
---|
| 98 | main(argc, argv) char **argv; {
|
---|
| 99 | struct jmploc jmploc;
|
---|
| 100 | struct stackmark smark;
|
---|
| 101 | volatile int state;
|
---|
| 102 | char *shinit, *home;
|
---|
| 103 | char *profile = NULL, *ashrc = NULL;
|
---|
| 104 |
|
---|
| 105 | #if PROFILE
|
---|
| 106 | monitor(4, etext, profile_buf, sizeof profile_buf, 50);
|
---|
| 107 | #endif
|
---|
| 108 | state = 0;
|
---|
| 109 | if (setjmp(jmploc.loc)) {
|
---|
| 110 | /*
|
---|
| 111 | * When a shell procedure is executed, we raise the
|
---|
| 112 | * exception EXSHELLPROC to clean up before executing
|
---|
| 113 | * the shell procedure.
|
---|
| 114 | */
|
---|
| 115 | if (exception == EXSHELLPROC) {
|
---|
| 116 | rootpid = getpid();
|
---|
| 117 | rootshell = 1;
|
---|
| 118 | minusc = NULL;
|
---|
| 119 | state = 3;
|
---|
| 120 | } else if (state == 0 || iflag == 0 || ! rootshell)
|
---|
| 121 | exitshell(2);
|
---|
| 122 | reset();
|
---|
| 123 | #if ATTY
|
---|
| 124 | if (exception == EXINT
|
---|
| 125 | && (! attyset() || equal(termval(), "emacs"))) {
|
---|
| 126 | #else
|
---|
| 127 | if (exception == EXINT) {
|
---|
| 128 | #endif
|
---|
| 129 | out2c('\n');
|
---|
| 130 | flushout(&errout);
|
---|
| 131 | }
|
---|
| 132 | popstackmark(&smark);
|
---|
| 133 | FORCEINTON; /* enable interrupts */
|
---|
| 134 | if (state == 1)
|
---|
| 135 | goto state1;
|
---|
| 136 | else if (state == 2)
|
---|
| 137 | goto state2;
|
---|
| 138 | else if (state == 3)
|
---|
| 139 | goto state3;
|
---|
| 140 | else
|
---|
| 141 | goto state4;
|
---|
| 142 | }
|
---|
| 143 | handler = &jmploc;
|
---|
| 144 | #if DEBUG
|
---|
| 145 | opentrace();
|
---|
| 146 | trputs("Shell args: "); trargs(argv);
|
---|
| 147 | #endif
|
---|
| 148 | rootpid = getpid();
|
---|
| 149 | rootshell = 1;
|
---|
| 150 | init();
|
---|
| 151 | setstackmark(&smark);
|
---|
| 152 | procargs(argc, argv);
|
---|
| 153 | if (eflag) eflag = 2; /* Truly enable [ex]flag after init. */
|
---|
| 154 | if (xflag) xflag = 2;
|
---|
| 155 | if (argv[0] && argv[0][0] == '-') {
|
---|
| 156 | state = 1;
|
---|
| 157 | read_profile("/etc/profile");
|
---|
| 158 | state1:
|
---|
| 159 | state = 2;
|
---|
| 160 | if ((home = getenv("HOME")) != NULL
|
---|
| 161 | && (profile = (char *) malloc(strlen(home) + 10)) != NULL)
|
---|
| 162 | {
|
---|
| 163 | strcpy(profile, home);
|
---|
| 164 | strcat(profile, "/.profile");
|
---|
| 165 | read_profile(profile);
|
---|
| 166 | } else {
|
---|
| 167 | read_profile(".profile");
|
---|
| 168 | }
|
---|
| 169 | } else if ((sflag || minusc) && (shinit = getenv("SHINIT")) != NULL) {
|
---|
| 170 | state = 2;
|
---|
| 171 | evalstring(shinit);
|
---|
| 172 | }
|
---|
| 173 | state2:
|
---|
| 174 | if (profile != NULL) free(profile);
|
---|
| 175 |
|
---|
| 176 | state = 3;
|
---|
| 177 | if (!argv[0] || argv[0][0] != '-') {
|
---|
| 178 | if ((home = getenv("HOME")) != NULL
|
---|
| 179 | && (ashrc = (char *) malloc(strlen(home) + 8)) != NULL)
|
---|
| 180 | {
|
---|
| 181 | strcpy(ashrc, home);
|
---|
| 182 | strcat(ashrc, "/.ashrc");
|
---|
| 183 | read_profile(ashrc);
|
---|
| 184 | }
|
---|
| 185 | }
|
---|
| 186 | state3:
|
---|
| 187 | if (ashrc != NULL) free(ashrc);
|
---|
| 188 | if (eflag) eflag = 1; /* Init done, enable [ex]flag */
|
---|
| 189 | if (xflag) xflag = 1;
|
---|
| 190 | exitstatus = 0; /* Init shouldn't influence initial $? */
|
---|
| 191 |
|
---|
| 192 | state = 4;
|
---|
| 193 | if (minusc) {
|
---|
| 194 | evalstring(minusc);
|
---|
| 195 | }
|
---|
| 196 | if (sflag || minusc == NULL) {
|
---|
| 197 | state4:
|
---|
| 198 | cmdloop(1);
|
---|
| 199 | }
|
---|
| 200 | #if PROFILE
|
---|
| 201 | monitor(0);
|
---|
| 202 | #endif
|
---|
| 203 | exitshell(exitstatus);
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 |
|
---|
| 207 | /*
|
---|
| 208 | * Read and execute commands. "Top" is nonzero for the top level command
|
---|
| 209 | * loop; it turns on prompting if the shell is interactive.
|
---|
| 210 | */
|
---|
| 211 |
|
---|
| 212 | void
|
---|
| 213 | cmdloop(top) {
|
---|
| 214 | union node *n;
|
---|
| 215 | struct stackmark smark;
|
---|
| 216 | int inter;
|
---|
| 217 | int numeof;
|
---|
| 218 |
|
---|
| 219 | TRACE(("cmdloop(%d) called\n", top));
|
---|
| 220 | setstackmark(&smark);
|
---|
| 221 | numeof = 0;
|
---|
| 222 | for (;;) {
|
---|
| 223 | if (pendingsigs)
|
---|
| 224 | dotrap();
|
---|
| 225 | inter = 0;
|
---|
| 226 | if (iflag && top) {
|
---|
| 227 | inter++;
|
---|
| 228 | showjobs(1);
|
---|
| 229 | chkmail(0);
|
---|
| 230 | flushout(&output);
|
---|
| 231 | }
|
---|
| 232 | n = parsecmd(inter);
|
---|
| 233 | #if DEBUG
|
---|
| 234 | /* showtree(n); */
|
---|
| 235 | #endif
|
---|
| 236 | if (n == NEOF) {
|
---|
| 237 | if (Iflag == 0 || numeof >= 50)
|
---|
| 238 | break;
|
---|
| 239 | out2str("\nUse \"exit\" to leave shell.\n");
|
---|
| 240 | numeof++;
|
---|
| 241 | } else if (n != NULL && nflag == 0) {
|
---|
| 242 | if (inter) {
|
---|
| 243 | INTOFF;
|
---|
| 244 | if (prevcmd)
|
---|
| 245 | freefunc(prevcmd);
|
---|
| 246 | prevcmd = curcmd;
|
---|
| 247 | curcmd = copyfunc(n);
|
---|
| 248 | INTON;
|
---|
| 249 | }
|
---|
| 250 | evaltree(n, 0);
|
---|
| 251 | #ifdef notdef
|
---|
| 252 | if (exitstatus) /*DEBUG*/
|
---|
| 253 | outfmt(&errout, "Exit status 0x%X\n", exitstatus);
|
---|
| 254 | #endif
|
---|
| 255 | }
|
---|
| 256 | popstackmark(&smark);
|
---|
| 257 | }
|
---|
| 258 | popstackmark(&smark); /* unnecessary */
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 |
|
---|
| 262 |
|
---|
| 263 | /*
|
---|
| 264 | * Read /etc/profile or .profile. Return on error.
|
---|
| 265 | */
|
---|
| 266 |
|
---|
| 267 | STATIC void
|
---|
| 268 | read_profile(name)
|
---|
| 269 | char *name;
|
---|
| 270 | {
|
---|
| 271 | int fd;
|
---|
| 272 |
|
---|
| 273 | INTOFF;
|
---|
| 274 | if ((fd = open(name, O_RDONLY)) >= 0)
|
---|
| 275 | setinputfd(fd, 1);
|
---|
| 276 | INTON;
|
---|
| 277 | if (fd < 0)
|
---|
| 278 | return;
|
---|
| 279 | cmdloop(0);
|
---|
| 280 | popfile();
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 |
|
---|
| 284 |
|
---|
| 285 | /*
|
---|
| 286 | * Read a file containing shell functions.
|
---|
| 287 | */
|
---|
| 288 |
|
---|
| 289 | void
|
---|
| 290 | readcmdfile(name)
|
---|
| 291 | char *name;
|
---|
| 292 | {
|
---|
| 293 | int fd;
|
---|
| 294 |
|
---|
| 295 | INTOFF;
|
---|
| 296 | if ((fd = open(name, O_RDONLY)) >= 0)
|
---|
| 297 | setinputfd(fd, 1);
|
---|
| 298 | else
|
---|
| 299 | error("Can't open %s", name);
|
---|
| 300 | INTON;
|
---|
| 301 | cmdloop(0);
|
---|
| 302 | popfile();
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 |
|
---|
| 306 | /*
|
---|
| 307 | * Take commands from a file. To be compatable we should do a path
|
---|
| 308 | * search for the file, but a path search doesn't make any sense.
|
---|
| 309 | */
|
---|
| 310 |
|
---|
| 311 | dotcmd(argc, argv) char **argv; {
|
---|
| 312 | exitstatus = 0;
|
---|
| 313 | if (argc >= 2) { /* That's what SVR2 does */
|
---|
| 314 | setinputfile(argv[1], 1);
|
---|
| 315 | commandname = argv[1];
|
---|
| 316 | cmdloop(0);
|
---|
| 317 | popfile();
|
---|
| 318 | }
|
---|
| 319 | return exitstatus;
|
---|
| 320 | }
|
---|
| 321 |
|
---|
| 322 |
|
---|
| 323 | exitcmd(argc, argv) char **argv; {
|
---|
| 324 | extern int oexitstatus;
|
---|
| 325 | if (argc > 1)
|
---|
| 326 | exitstatus = number(argv[1]);
|
---|
| 327 | else
|
---|
| 328 | exitstatus = oexitstatus;
|
---|
| 329 | exitshell(exitstatus);
|
---|
| 330 | }
|
---|
| 331 |
|
---|
| 332 |
|
---|
| 333 | lccmd(argc, argv) char **argv; {
|
---|
| 334 | if (argc > 1) {
|
---|
| 335 | defun(argv[1], prevcmd);
|
---|
| 336 | return 0;
|
---|
| 337 | } else {
|
---|
| 338 | INTOFF;
|
---|
| 339 | freefunc(curcmd);
|
---|
| 340 | curcmd = prevcmd;
|
---|
| 341 | prevcmd = NULL;
|
---|
| 342 | INTON;
|
---|
| 343 | evaltree(curcmd, 0);
|
---|
| 344 | return exitstatus;
|
---|
| 345 | }
|
---|
| 346 | }
|
---|
| 347 |
|
---|
| 348 |
|
---|
| 349 |
|
---|
| 350 | #ifdef notdef
|
---|
| 351 | /*
|
---|
| 352 | * Should never be called.
|
---|
| 353 | */
|
---|
| 354 |
|
---|
| 355 | void
|
---|
| 356 | exit(exitstatus) {
|
---|
| 357 | _exit(exitstatus);
|
---|
| 358 | }
|
---|
| 359 | #endif
|
---|