home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume18 / mush6.4 / part04 / options.c < prev   
Encoding:
C/C++ Source or Header  |  1989-03-12  |  7.6 KB  |  314 lines

  1. /* @(#)options.c    (c) copyright 10/10/88 (Dan Heller, Bart Schaefer) */
  2.  
  3. #include "mush.h"
  4. #include "options.h"
  5.  
  6. /*
  7.  * NOTE:  Any word flag which is a prefix of another word flag must be
  8.  *  listed AFTER the flag it prefixes in the list below
  9.  */
  10.  
  11. char *word_flags[][2] = {
  12.     { "-bcc",        "-b" },
  13.     { "-blindcarbon",    "-b" },
  14.     { "-blind",        "-b" },
  15.     { "-carbon",    "-c" },
  16.     { "-cc",        "-c" },
  17.     { "-copy",        "-c" },
  18.     { "-curses",    "-C" },
  19.     { "-debug",        "-d" },
  20.     { "-echo",        "-e" },
  21.     { "-folder",    "-f" },    /* Maybe -file should become -f too? */
  22.     { "-file",        "-F" },    /* Don't really like -file for -F */
  23.     { "-headers",    "-H" },
  24.     { "-help",        "-1" },
  25.     { "-interact",    "-i" },
  26.     { "-mailbox",    "-m" },
  27.     { "-noheaders",    "-N" },
  28.     { "-noinit",    "-n" },
  29.     { "-readonly",    "-r" },
  30.     { "-shell",        "-S" },
  31.     { "-source",    "-F" },    /* This is better for -F */
  32.     { "-subject",    "-s" },
  33.     { "-sunhelp",    "-2" },
  34.     { "-timeout",    "-T" },
  35.     { "-tool",        "-t" },
  36.     { "-user",        "-u" },
  37.     { "-verbose",    "-v" },
  38.     { "-visual",    "-C" },
  39.     { NULL,        NULL }    /* This must be the last entry */
  40. };
  41.  
  42. fix_word_flags(argv)
  43. register char **argv;
  44. {
  45.     int i;
  46.     Debug(*argv);
  47.     for (++argv; *argv; argv++) {
  48.     for (i = 0; word_flags[i][0]; i++) {
  49.         int len = strlen(word_flags[i][0]);
  50.         if (! strncmp(*argv, word_flags[i][0], len)) {
  51.         char buf[BUFSIZ], *p = buf;
  52.         p += Strcpy(buf, word_flags[i][1]);
  53.         (void) strcpy(p, *argv + len);
  54.         (void) strcpy(*argv, buf);
  55.         }
  56.     }
  57.     Debug(" %s", *argv);
  58.     }
  59.     if (debug)
  60.     putchar('\n');
  61. }
  62.  
  63. /*
  64.  * preparse the command line to determine whether or not we're going
  65.  * to bail out after checking that the user has no mail.  Also, check
  66.  * to see if we're going to run a tool because it must be built first.
  67.  */
  68. preparse_opts(argcp, argv, argt)
  69. register int *argcp;    /* Pointer to argument count */
  70. register char **argv;    /* Argument vector */
  71. register char ***argt;    /* Pointer to tool-mode arg vector */
  72. {
  73.     int n = FALSE;
  74.     char **args;
  75.  
  76. #ifdef SUNTOOL
  77.     if (n = istool = strlen(prog_name) > 3 &&
  78.          !strcmp(prog_name+strlen(prog_name)-4, "tool"))
  79.     turnon(glob_flags, DO_SHELL);
  80. #endif /* SUNTOOL */
  81.  
  82.     fix_word_flags(argv);
  83.  
  84.     if (!istool && *argcp > 1) {
  85.     for (args = argv+1; *args && args[0][0] == '-'; args++) {
  86.         int next = 1;
  87. DoNext:
  88.         switch (args[0][next]) {
  89. #ifdef SUNTOOL
  90.         case 'T' :
  91.             if (args[1])
  92.             args++;
  93.         case 't' :
  94.             istool = 1;
  95.             n = TRUE;
  96.             turnon(glob_flags, DO_SHELL);
  97.             break;
  98. #endif /* SUNTOOL */
  99.         case 'S' :
  100.             turnon(glob_flags, DO_SHELL);
  101.             n = TRUE;
  102.             break;
  103.         case 'f' :
  104.         case 'F' :
  105.         case 'm' :
  106.         case 'u' :
  107.             n = TRUE;
  108.         case 'b' :
  109.         case 'c' :
  110.         case 's' :
  111.         case '1' :
  112.         case '2' :
  113.             if (args[1]) {
  114.             args++;
  115.             next = 0;
  116.             }
  117.             break;
  118.         case '\0':
  119.             next = 0;
  120.         default : ;
  121.         }
  122.         if (next) {
  123.         ++next;
  124.         goto DoNext;
  125.         }
  126.     }
  127.     if (*args) {  /* unused args indicates sending mail to someone */
  128.         n = TRUE;
  129.         if (!istool)
  130.         turnon(glob_flags, IS_SENDING);
  131.     }
  132.     }
  133.  
  134. #ifdef SUNTOOL
  135.     /* even if not running tool mode parse all potential suntools args out */
  136.     tool_parse_all(argcp, argv, argt, prog_name);
  137. #endif /* SUNTOOL */
  138.  
  139.     return n;
  140. }
  141.  
  142. static char *usage_str =
  143. #ifdef SUNTOOL 
  144.     "usage: %s [-C] [-i] [-f [folder] ] [-v] [-S] [-t] [-s subject] [users]\n";
  145. #else
  146. #ifdef CURSES
  147.     "usage: %s [-C] [-i] [-f [folder] ] [-v] [-S] [-s subject] [user list]\n";
  148. #else
  149.     "usage: %s [-i] [-f [folder] ] [-v] [-S] [-s subject] [user list]\n";
  150. #endif /* CURSES */
  151. #endif /* SUNTOOL */
  152.  
  153. parse_options(argvp, flags)
  154. register char ***argvp;
  155. struct mush_flags *flags;
  156. {
  157.     char buf[256];
  158.  
  159.     bzero(flags, sizeof (struct mush_flags));
  160.     flags->source_rc = TRUE;
  161.     mailfile = "";
  162.  
  163.     for (++(*argvp); **argvp && ***argvp == '-'; (*argvp)++) {
  164.     int look_again;
  165. DoLookAgain:
  166.     look_again = TRUE;
  167.     switch ((*argvp)[0][1]) {
  168.         case 'e':
  169.         /*
  170.          * don't set tty modes -- e.g. echo and cbreak modes aren't
  171.          * changed.
  172.          */
  173.         turnon(glob_flags, ECHO_FLAG);
  174. #ifdef CURSES
  175.         when 'C':
  176.         /* don't init curses -- don't even set iscurses.   */
  177.         if (istool) {
  178.             puts("-C: You are already running in tool mode");
  179.             turnoff(glob_flags, PRE_CURSES);
  180.         } else if (hdrs_only)
  181.             puts("headers only: ignoring -C flag");
  182.         else
  183.             turnon(glob_flags, PRE_CURSES);
  184. #endif /* CURSES */
  185.         when 'F':
  186.         flags->src_n_exit = ((*argvp)[0][2] == '!');
  187.         if (!(flags->src_file = *++(*argvp)))
  188.             puts("specify filename to source"), exit(1);
  189.         look_again = FALSE;
  190.         /* fall thru! */
  191.         case 'N':
  192.         (void) strcat(flags->f_flags, "-N ");
  193.         when 'r':
  194.         (void) strcat(flags->f_flags, "-r "); /* folder() argument */
  195.         when 'H':
  196.         if (istool) {
  197.             puts("running in tool-mode; -H option ignored.");
  198.             break;
  199.         }
  200.         turnoff(glob_flags, PRE_CURSES);
  201.         if (*(hdrs_only = (*(*argvp))+2) != ':')
  202.             hdrs_only = ":a";
  203.         else
  204.             look_again = FALSE;
  205.         /* read only cuz no updates */
  206.         (void) strcat(flags->f_flags, "-N -r ");
  207.         when 'i':
  208.         /* force interactive even if !isatty(0) */
  209.         turnoff(glob_flags, REDIRECT);
  210.         when 'u': /* specify a user's mailbox */
  211.         if (*mailfile)
  212.             puts("You can't specify more than one mailbox"), exit(1);
  213. #ifdef HOMEMAIL
  214.         {
  215.             char *p;
  216.             int isdir = 1;
  217.             (void) sprintf(buf, "%%%s",
  218.                 (*argvp)[1] ? (*argvp)[1] : "root");
  219.             if ((p = getpath(buf, &isdir)) && !isdir)
  220.             strdup(mailfile, p);
  221.             else if (isdir < 0)
  222.             puts(p), exit(1);
  223.             else if (isdir)
  224.             printf("\"%s\" is a directory\n", p), exit(1);
  225.         }
  226. #else /* HOMEMAIL */
  227.         strdup(mailfile, sprintf(buf, "%s/%s",
  228.                    MAILDIR, ((*argvp)[1])? (*argvp)[1] : "root"));
  229. #endif /* HOMEMAIL */
  230.         if ((*argvp)[1])
  231.             ++(*argvp);
  232.         look_again = FALSE;
  233.         when 'm':
  234.         if ((*argvp)[1])
  235.             strdup(spoolfile, *++(*argvp));
  236.         else
  237.             printf("-m: missing mailbox name.\n"), exit(1);
  238.         look_again = FALSE;
  239.         when 'f':
  240.         if (*mailfile)
  241.             puts("You can't specify more than one mailbox"), exit(1);
  242.         if ((*argvp)[1]) {
  243.             strdup(mailfile, *++(*argvp));
  244.             look_again = FALSE;
  245.         } else
  246.             strdup(mailfile, "&");
  247.         when '1':
  248.         if ((*argvp)[1])
  249.             strdup(cmd_help, *++(*argvp));
  250.         else
  251.             puts("-1 \"filename\""), exit(1);
  252.         look_again = FALSE;
  253. #ifdef SUNTOOL
  254.         when '2':
  255.         if ((*argvp)[1])
  256.             strdup(tool_help, *++(*argvp));
  257.         else
  258.             puts("-2 \"filename\""), exit(1);
  259.         look_again = FALSE;
  260. #endif /* SUNTOOL */
  261.         when 's':
  262.         if (istool)
  263.             puts("bad option when run as a tool"), exit(1);
  264.         else if ((*argvp)[1])
  265.             flags->Subj = *++(*argvp);
  266.         else
  267.             puts("-s \"subject\""), exit(1);
  268.         look_again = FALSE;
  269.         when 'b':
  270.         if (istool)
  271.             puts("-b: bad option when run as a tool"), exit(1);
  272.         else if ((*argvp)[1])
  273.             flags->Bcc = *++(*argvp);
  274.         else
  275.             puts("-b \"bcc list\""), exit(1);
  276.         look_again = FALSE;
  277.         when 'c':
  278.         if (istool)
  279.             puts("-c: bad option when run as a tool"), exit(1);
  280.         else if ((*argvp)[1])
  281.             flags->Cc = *++(*argvp);
  282.         else
  283.             puts("-c \"cc list\""), exit(1);
  284.         look_again = FALSE;
  285.         break;
  286. #ifdef VERBOSE_ARG
  287.         case 'v':
  288.         if (istool)
  289.             puts("bad option when run as a tool"), exit(1);
  290.         turnon(flags->flg, VERBOSE);
  291.         break;
  292. #endif /* VERBOSE_ARG */
  293. #ifdef SUNTOOL
  294.             case 'T':
  295.         if ((time_out = atoi(*(*argvp))) <= 29)
  296.             time_out = 30;
  297.         look_again = FALSE;
  298.         /* -T implies -t */
  299.         case 't': istool = 1;
  300. #endif /* SUNTOOL */
  301.         case 'S': turnon(glob_flags, DO_SHELL);
  302.         when 'n': flags->source_rc = FALSE;
  303.         when 'd': debug = 1;
  304.         when '\0' : look_again = FALSE;
  305.         otherwise:
  306.         print("%s: unknown option: `%c'\n", prog_name,
  307.             (*argvp)[0][1]? (*argvp)[0][1] : '-');
  308.         print(usage_str, prog_name);
  309.     }
  310.     if (look_again && ++(**argvp) != '\0')
  311.         goto DoLookAgain;
  312.     }
  313. }
  314.