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