home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume30 / rc / part06 / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-30  |  2.5 KB  |  116 lines

  1. /* main.c: handles initialization of rc and command line options */
  2.  
  3. #include "rc.h"
  4.  
  5. bool dashdee, dashee, dashvee, dashex, dashell, dasheye,
  6.     dashen, dashpee, interactive;
  7. int rc_pid;
  8.  
  9. static bool dashoh;
  10.  
  11. static void assigndefault(char *,...);
  12. static void checkfd(int, enum redirtype);
  13.  
  14. extern void main(int argc, char *argv[], char *envp[]) {
  15.     char *dashsee[2], *dollarzero, *null[1];
  16.     int c;
  17.     initprint();
  18.     dashsee[0] = dashsee[1] = NULL;
  19.     dollarzero = argv[0];
  20.     rc_pid = getpid();
  21.     dashell = (*argv[0] == '-'); /* Unix tradition */
  22.     while ((c = rc_getopt(argc, argv, "nolpeivdxc:")) != -1)
  23.         switch (c) {
  24.         case 'l':
  25.             dashell = TRUE;
  26.             break;
  27.         case 'e':
  28.             dashee = TRUE;
  29.             break;
  30.         case 'i':
  31.             dasheye = interactive = TRUE;
  32.             break;
  33.         case 'v':
  34.             dashvee = TRUE;
  35.             break;
  36.         case 'x':
  37.             dashex = TRUE;
  38.             break;
  39.         case 'd':
  40.             dashdee = TRUE;
  41.             break;
  42.         case 'c':
  43.             dashsee[0] = rc_optarg;
  44.             goto quitopts;
  45.         case 'n':
  46.             dashen = TRUE;
  47.             break;
  48.         case 'p':
  49.             dashpee = TRUE;
  50.             break;
  51.         case 'o':
  52.             dashoh = TRUE;
  53.             break;
  54.         case '?':
  55.             exit(1);
  56.         }
  57. quitopts:
  58.     argv += rc_optind;
  59.     /* use isatty() iff -i is not set, and iff the input is not from a script or -c flag */
  60.     if (!dasheye && dashsee[0] == NULL && *argv == NULL)
  61.         interactive = isatty(0);
  62.     if (!dashoh) {
  63.         checkfd(0, rFrom);
  64.         checkfd(1, rCreate);
  65.         checkfd(2, rCreate);
  66.     }
  67.     initsignal();
  68.     inithash();
  69.     initparse();
  70.     assigndefault("prompt", "; ", "", (void *)0);
  71. #ifdef DEFAULTPATH
  72.     assigndefault("path", DEFAULTPATH, (void *)0);
  73. #endif
  74.     assigndefault("ifs", " ", "\t", "\n", (void *)0);
  75.     assigndefault("pid", nprint("%d", rc_pid), (void *)0);
  76.     initenv(envp);
  77.     initinput();
  78.     null[0] = NULL;
  79.     starassign(dollarzero, null, FALSE); /* assign $0 to $* */
  80.     inithandler();
  81.     if (dashsee[0] != NULL) {    /* input from the -c flag? */
  82.         if (*argv != NULL)
  83.             starassign(dollarzero, argv, FALSE);
  84.         pushstring(dashsee, TRUE);
  85.     } else if (*argv != NULL) {    /* else from a file? */
  86.         b_dot(--argv);
  87.         rc_exit(getstatus());
  88.     } else {            /* else stdin */
  89.         pushfd(0);
  90.     }
  91.     dasheye = FALSE;
  92.     doit(TRUE);
  93.     rc_exit(getstatus());
  94. }
  95.  
  96. static void assigndefault(char *name,...) {
  97.     va_list ap;
  98.     List *l;
  99.     char *v;
  100.     va_start(ap, name);
  101.     for (l = NULL; (v = va_arg(ap, char *)) != NULL;)
  102.         l = append(l, word(v, NULL));
  103.     varassign(name, l, FALSE);
  104.     if (streq(name, "path"))
  105.         alias(name, l, FALSE);
  106.     va_end(ap);
  107. }
  108.  
  109. /* open an fd on /dev/null if it is inherited closed */
  110.  
  111. static void checkfd(int fd, enum redirtype r) {
  112.     int new = rc_open("/dev/null", r);
  113.     if (new != fd && new != -1)
  114.         close(new);
  115. }
  116.