home *** CD-ROM | disk | FTP | other *** search
- /* main.c: handles initialization of rc and command line options */
-
- #include <stdarg.h>
- #include "rc.h"
- #include "utils.h"
- #include "input.h"
- #include "nalloc.h"
- #include "hash.h"
- #include "lex.h"
- #include "open.h"
- #include "tree.h"
- #include "glom.h"
- #include "builtins.h"
- #include "parse.h"
-
- boolean dashdee, dashee, dashvee, dashex, dashell, dasheye, interactive;
- int rc_pid;
-
- #define REALLYNULL ((void *) 0) /* used to terminate a vararg list with NULL */
-
- static void assigndefault(char *,...);
-
- void main(int argc, char *argv[], char *envp[]) {
- extern int getopt(int, char **, char *);
- extern int optind;
- extern char *optarg;
- char *dashsee[2], pid[8], *dollarzero, *null[1];
- int c;
-
- dashee = dashell = dashvee = dashex = dashdee = FALSE;
- dashsee[0] = dashsee[1] = NULL;
- dollarzero = argv[0];
-
- dashell = (*argv[0] == '-'); /* Unix tradition */
-
- while ((c = getopt(argc, argv, "leivdxc:")) != -1)
- switch (c) {
- case 'l':
- dashell = TRUE;
- break;
- case 'e':
- dashee = TRUE;
- break;
- case 'i':
- dasheye = interactive = TRUE;
- break;
- case 'v':
- dashvee = TRUE;
- break;
- case 'x':
- dashex = TRUE;
- break;
- case 'd':
- dashdee = TRUE;
- break;
- case 'c':
- dashsee[0] = optarg;
- goto quitopts;
- case '?':
- exit(1);
- }
-
- quitopts:
- argv += optind;
-
- /* use isatty() iff -i is not set, and iff the input is not from a script or -c flag */
- if (!dasheye && dashsee[0] == NULL && *argv == NULL)
- interactive = isatty(0);
-
- inithandler();
- inithash();
- initparse();
- assigndefault("prompt", "; ", "", REALLYNULL);
- assigndefault("path", ".", "/bin", "/usr/bin", "/usr/ucb", REALLYNULL);
- assigndefault("ifs", " ", "\t", "\n", REALLYNULL);
- assigndefault("pid", sprint(pid, "%d", rc_pid = getpid()), REALLYNULL);
- initenv(envp);
- initinput();
- null[0] = NULL;
- starassign(dollarzero, null, FALSE); /* assign $0 to $* */
-
- if (dashsee[0] != NULL) { /* input from the -c flag? */
- if (*argv != NULL)
- starassign(dollarzero, argv, FALSE);
- pushinput(STRING, dashsee);
- } else if (*argv != NULL) { /* else from a file? */
- b_dot(--argv);
- rc_exit(0);
- } else { /* else stdin */
- pushinput(FD, 0);
- }
-
- doit(TRUE);
- rc_exit(0);
- }
-
- static void assigndefault(char *name,...) {
- va_list ap;
- List *l;
- char *v;
-
- va_start(ap, name);
-
- for (l = NULL; (v = va_arg(ap, char *)) != NULL;)
- l = append(l, word(v, NULL));
-
- varassign(name, l, FALSE);
-
- if (streq(name,"path"))
- alias(name, l, FALSE);
-
- va_end(ap);
- }
-