home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume20 / rc / part04 / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-22  |  2.4 KB  |  114 lines

  1. /* main.c: handles initialization of rc and command line options */
  2.  
  3. #include <stdarg.h>
  4. #include "rc.h"
  5. #include "utils.h"
  6. #include "input.h"
  7. #include "nalloc.h"
  8. #include "hash.h"
  9. #include "lex.h"
  10. #include "open.h"
  11. #include "tree.h"
  12. #include "glom.h"
  13. #include "builtins.h"
  14. #include "parse.h"
  15.  
  16. boolean dashdee, dashee, dashvee, dashex, dashell, dasheye, interactive;
  17. int rc_pid;
  18.  
  19. #define REALLYNULL ((void *) 0) /* used to terminate a vararg list with NULL */
  20.  
  21. static void assigndefault(char *,...);
  22.  
  23. void main(int argc, char *argv[], char *envp[]) {
  24.     extern int getopt(int, char **, char *);
  25.     extern int optind;
  26.     extern char *optarg;
  27.     char *dashsee[2], pid[8], *dollarzero, *null[1];
  28.     int c;
  29.  
  30.     dashee = dashell = dashvee = dashex = dashdee = FALSE;
  31.     dashsee[0] = dashsee[1] = NULL;
  32.     dollarzero = argv[0];
  33.  
  34.     dashell = (*argv[0] == '-'); /* Unix tradition */
  35.  
  36.     while ((c = getopt(argc, argv, "leivdxc:")) != -1)
  37.         switch (c) {
  38.         case 'l':
  39.             dashell = TRUE;
  40.             break;
  41.         case 'e':
  42.             dashee = TRUE;
  43.             break;
  44.         case 'i':
  45.             dasheye = interactive = TRUE;
  46.             break;
  47.         case 'v':
  48.             dashvee = TRUE;
  49.             break;
  50.         case 'x':
  51.             dashex = TRUE;
  52.             break;
  53.         case 'd':
  54.             dashdee = TRUE;
  55.             break;
  56.         case 'c':
  57.             dashsee[0] = optarg;
  58.             goto quitopts;
  59.         case '?':
  60.             exit(1);
  61.         }
  62.  
  63. quitopts:
  64.     argv += optind;
  65.  
  66.     /* use isatty() iff -i is not set, and iff the input is not from a script or -c flag */
  67.     if (!dasheye && dashsee[0] == NULL && *argv == NULL)
  68.         interactive = isatty(0);
  69.  
  70.     inithandler();
  71.     inithash();
  72.     initparse();
  73.     assigndefault("prompt", "; ", "", REALLYNULL);
  74.     assigndefault("path", ".", "/bin", "/usr/bin", "/usr/ucb", REALLYNULL);
  75.     assigndefault("ifs", " ", "\t", "\n", REALLYNULL);
  76.     assigndefault("pid", sprint(pid, "%d", rc_pid = getpid()), REALLYNULL);
  77.     initenv(envp);
  78.     initinput();
  79.     null[0] = NULL;
  80.     starassign(dollarzero, null, FALSE); /* assign $0 to $* */
  81.  
  82.     if (dashsee[0] != NULL) {    /* input from the -c flag? */
  83.         if (*argv != NULL)
  84.             starassign(dollarzero, argv, FALSE);
  85.         pushinput(STRING, dashsee);
  86.     } else if (*argv != NULL) {    /* else from a file? */
  87.         b_dot(--argv);
  88.         rc_exit(0);
  89.     } else {            /* else stdin */
  90.         pushinput(FD, 0);
  91.     }
  92.  
  93.     doit(TRUE);
  94.     rc_exit(0);
  95. }
  96.  
  97. static void assigndefault(char *name,...) {
  98.     va_list ap;
  99.     List *l;
  100.     char *v;
  101.  
  102.     va_start(ap, name);
  103.  
  104.     for (l = NULL; (v = va_arg(ap, char *)) != NULL;)
  105.         l = append(l, word(v, NULL));
  106.  
  107.     varassign(name, l, FALSE);
  108.  
  109.     if (streq(name,"path"))
  110.         alias(name, l, FALSE);
  111.  
  112.     va_end(ap);
  113. }
  114.