home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mush-7.1.1 / options.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-02  |  8.6 KB  |  341 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.     { "-draft",        "-h" },
  21.     { "-echo",        "-e" },
  22.     { "-folder",    "-f" },    /* Maybe -file should become -f too? */
  23.     { "-file",        "-F" },    /* Don't really like -file for -F */
  24.     { "-headerfile",    "-h" },
  25.     { "-headers",    "-H" },
  26.     { "-initialize",    "-I" },
  27.     { "-init",        "-I" },
  28.     { "-interactive",    "-i" },
  29.     { "-interact",    "-i" },
  30.     { "-mailbox",    "-m" },
  31.     { "-message",    "-h" },
  32.     { "-noheaders",    "-N" },
  33.     { "-noinit",    "-n" },
  34.     { "-readonly",    "-r" },
  35.     { "-send",        "-U" },
  36.     { "-shell",        "-S" },
  37.     { "-source",    "-F" },    /* This is better for -F */
  38.     { "-subject",    "-s" },
  39.     { "-timeout",    "-T" },
  40.     { "-toolhelp",    "-2" },
  41.     { "-tool",        "-t" },
  42.     { "-user",        "-u" },
  43.     { "-verbose",    "-v" },
  44.     { "-visual",    "-C" },
  45.     { NULL,        NULL }    /* This must be the last entry */
  46. };
  47.  
  48. fix_word_flag(argp)
  49. register char **argp;
  50. {
  51.     int i;
  52.  
  53.     Debug("%s --> ", *argp);
  54.     for (i = 0; word_flags[i][0]; i++) {
  55.     int len = strlen(word_flags[i][0]);
  56.     if (! strncmp(*argp, word_flags[i][0], len)) {
  57.         char buf[BUFSIZ], *p = buf;
  58.         p += Strcpy(buf, word_flags[i][1]);
  59.         (void) strcpy(p, *argp + len);
  60.         (void) strcpy(*argp, buf);
  61.     }
  62.     }
  63.     Debug("%s\n", *argp);
  64. }
  65.  
  66. /*
  67.  * preparse the command line to determine whether or not we're going
  68.  * to bail out after checking that the user has no mail.  Also, check
  69.  * to see if we're going to run a tool because it must be built first.
  70.  */
  71. preparse_opts(argcp, argv)
  72. register int *argcp;    /* Pointer to argument count */
  73. register char **argv;    /* Argument vector */
  74. {
  75.     int n = FALSE;
  76.     char **args;
  77.  
  78. #ifdef SUNTOOL
  79.     /* Note: we are assigning a boolean result to n and istool here */
  80.     if (n = istool = (strlen(prog_name) > 3 &&
  81.          (!strcmp(prog_name+strlen(prog_name)-4, "tool") ||
  82.           !strcmp(prog_name+strlen(prog_name)-4, "view")))) {
  83.     turnon(glob_flags, DO_SHELL);
  84.     parse_tool_opts(argcp, argv);
  85.     }
  86. #endif /* SUNTOOL */
  87.  
  88.     if (!istool && *argcp > 1) {
  89.     for (args = argv+1; *args && args[0][0] == '-'; args++) {
  90.         int next = 1;
  91.         fix_word_flag(&args[0]);
  92. DoNext:
  93.         switch (args[0][next]) {
  94. #ifdef SUNTOOL
  95.         case 'T' :
  96.             if (args[1])
  97.             args++;
  98.         case 't' :
  99.             /* Note: we won't ever get here if started as
  100.              * "mushtool" or "mushview" because istool is true.
  101.              */
  102.             istool = 1;
  103.             parse_tool_opts(argcp, argv);
  104.             turnon(glob_flags, DO_SHELL);
  105.             return TRUE;
  106.             /* break; */
  107. #endif /* SUNTOOL */
  108.         case 'S' :
  109.             turnon(glob_flags, DO_SHELL);
  110.             n = TRUE;
  111.             break;
  112.         case 'f' :
  113.         case 'F' :
  114.         case 'h' :
  115.         case 'm' :
  116.         case 'u' :
  117.             n = TRUE;
  118.         case 'b' :
  119.         case 'c' :
  120.         case 'I' :
  121.         case 's' :
  122.             if (args[1]) {
  123.             args++;
  124.             next = 0;
  125.             }
  126.             break;
  127.         case 'H' :
  128.             if (args[0][next+1] == ':')
  129.             next = 0;
  130.             break;
  131.         case '\0':
  132.             next = 0;
  133.         default : ;
  134.         }
  135.         if (next) {
  136.         ++next;
  137.         goto DoNext;
  138.         }
  139.     }
  140.     if (*args) {  /* unused args indicates sending mail to someone */
  141.         n = TRUE;
  142.         if (!istool)
  143.         turnon(glob_flags, IS_SENDING);
  144.     }
  145.     }
  146.  
  147.     return n;
  148. }
  149.  
  150. static char *usage_str =
  151. #ifdef SUNTOOL
  152.     "usage: %s [-t] [-C] [-i] [-f [folder] ] [-v] [-S] [-s subject] [users]\n";
  153. #else
  154. #ifdef CURSES
  155.     "usage: %s [-C] [-i] [-f [folder] ] [-v] [-S] [-s subject] [user list]\n";
  156. #else
  157.     "usage: %s [-i] [-f [folder] ] [-v] [-S] [-s subject] [user list]\n";
  158. #endif /* CURSES */
  159. #endif /* SUNTOOL */
  160.  
  161. parse_options(argvp, flags)
  162. register char ***argvp;
  163. struct mush_flags *flags;
  164. {
  165.     char buf[256];
  166.  
  167.     bzero((char *) flags, sizeof (struct mush_flags));
  168.     flags->source_rc = TRUE;
  169.     flags->folder = "";
  170.  
  171.     for (++(*argvp); **argvp && ***argvp == '-'; (*argvp)++) {
  172.     int look_again;
  173. DoLookAgain:
  174.     look_again = TRUE;
  175.     switch ((*argvp)[0][1]) {
  176.         case 'e':
  177.         /*
  178.          * don't set tty modes -- e.g. echo and cbreak modes aren't
  179.          * changed.
  180.          */
  181.         turnon(glob_flags, ECHO_FLAG);
  182. #ifdef CURSES
  183.         when 'C':
  184.         /* don't init curses -- don't even set iscurses.   */
  185.         if (istool) {
  186.             puts("-C: You are already running in tool mode");
  187.             turnoff(glob_flags, PRE_CURSES);
  188.         } else if (hdrs_only)
  189.             puts("headers only: ignoring -C flag");
  190.         else
  191.             turnon(glob_flags, PRE_CURSES);
  192. #endif /* CURSES */
  193.         when 'F':
  194.         flags->src_n_exit = ((*argvp)[0][2] == '!');
  195.         if (!(flags->src_file = *++(*argvp)))
  196.             puts("specify filename to source"), exit(1);
  197.         look_again = FALSE;
  198.         /* fall thru! */
  199.         case 'N':
  200.         (void) strcat(flags->f_flags, "-N ");
  201.         when 'r':
  202.         (void) strcat(flags->f_flags, "-r "); /* folder() argument */
  203.         when 'H':
  204.         if (istool) {
  205.             puts("running in tool-mode; -H option ignored.");
  206.             break;
  207.         }
  208.         turnoff(glob_flags, PRE_CURSES);
  209.         if (*(hdrs_only = (*(*argvp))+2) != ':')
  210.             hdrs_only = ":a";
  211.         else
  212.             look_again = FALSE;
  213.         /* read only cuz no updates */
  214.         (void) strcat(flags->f_flags, "-N -r ");
  215.         when 'i':
  216.         /* force interactive even if !isatty(0) */
  217.         turnoff(glob_flags, REDIRECT);
  218.         when 'u': /* specify a user's mailbox */
  219.         if (*(flags->folder))
  220.             puts("You can't specify more than one mailbox"), exit(1);
  221. #ifdef HOMEMAIL
  222.         {
  223.             char *p;
  224.             int isdir = 1;
  225.             (void) sprintf(buf, "%%%s",
  226.                 (*argvp)[1] ? (*argvp)[1] : "root");
  227.             if ((p = getpath(buf, &isdir)) && !isdir)
  228.             strdup(flags->folder, p);
  229.             else if (isdir < 0)
  230.             puts(p), exit(1);
  231.             else if (isdir)
  232.             (void) printf("\"%s\" is a directory\n", p), exit(1);
  233.         }
  234. #else /* HOMEMAIL */
  235.         strdup(flags->folder, sprintf(buf, "%s/%s",
  236.                    MAILDIR, ((*argvp)[1])? (*argvp)[1] : "root"));
  237. #endif /* HOMEMAIL */
  238.         if ((*argvp)[1])
  239.             ++(*argvp);
  240.         look_again = FALSE;
  241.         when 'h':
  242.         if (istool)
  243.             puts("bad option when run as a tool"), exit(1);
  244.         if ((*argvp)[1])
  245.             flags->draft = *++(*argvp);
  246.         else
  247.             (void) printf("-h: missing file name.\n"), exit(1);
  248.         look_again = FALSE;
  249.         turnon(glob_flags, IS_SENDING);
  250.         when 'U':
  251.         if (istool)
  252.             puts("bad option when run as a tool"), exit(1);
  253.         turnon(flags->flg, SEND_NOW);
  254.         if ((*argvp)[0][2] == '!') {
  255.             turnon(flags->flg, NO_SIGN);
  256.             ++(**argvp);
  257.         }
  258.         when 'm':
  259.         if ((*argvp)[1])
  260.             strdup(spoolfile, *++(*argvp));
  261.         else
  262.             (void) printf("-m: missing mailbox name.\n"), exit(1);
  263.         look_again = FALSE;
  264.         when 'f':
  265.         if (*(flags->folder))
  266.             puts("You can't specify more than one mailbox"), exit(1);
  267.         if ((*argvp)[1]) {
  268.             strdup(flags->folder, *++(*argvp));
  269.             look_again = FALSE;
  270.         } else
  271.             strdup(flags->folder, "&");
  272.         when 's':
  273.         if (istool)
  274.             puts("bad option when run as a tool"), exit(1);
  275.         else if ((*argvp)[1])
  276.             flags->Subj = *++(*argvp);
  277.         else
  278.             puts("-s \"subject\""), exit(1);
  279.         look_again = FALSE;
  280.         when 'b':
  281.         if (istool)
  282.             puts("-b: bad option when run as a tool"), exit(1);
  283.         else if ((*argvp)[1])
  284.             flags->Bcc = *++(*argvp);
  285.         else
  286.             puts("-b \"bcc list\""), exit(1);
  287.         look_again = FALSE;
  288.         when 'c':
  289.         if (istool)
  290.             puts("-c: bad option when run as a tool"), exit(1);
  291.         else if ((*argvp)[1])
  292.             flags->Cc = *++(*argvp);
  293.         else
  294.             puts("-c \"cc list\""), exit(1);
  295.         look_again = FALSE;
  296.         break;
  297. #ifdef VERBOSE_ARG
  298.         case 'v':
  299.         if (istool)
  300.             puts("bad option when run as a tool"), exit(1);
  301.         turnon(flags->flg, VERBOSE);
  302.         break;
  303. #endif /* VERBOSE_ARG */
  304. #ifdef SUNTOOL
  305.         case 'T':
  306.         if ((time_out = atoi(*(*argvp))) <= 29)
  307.             time_out = 30;
  308.         look_again = FALSE;
  309.         /* -T implies -t */
  310.         case 't': istool = 1;
  311. #endif /* SUNTOOL */
  312.         case 'S': turnon(glob_flags, DO_SHELL);
  313.         when 'n':
  314.         if ((*argvp)[0][2] == '!') {
  315.             ++(**argvp);
  316.             flags->source_rc = -1;    /* No init files sourced */
  317.         } else
  318.             flags->source_rc = 0;    /* Only ~/.mushrc sourced */
  319.         when 'I':
  320.         if ((*argvp)[0][2] == '!' && flags->source_rc > 0)
  321.             flags->source_rc = 0;    /* Only ~/.mushrc sourced */
  322.         if (!(flags->init_file = *++(*argvp)))
  323.             puts("specify filename for init"), exit(1);
  324.         look_again = FALSE;
  325.         when 'd': debug = 1;
  326.         when '\0' : look_again = FALSE;
  327.         otherwise:
  328.         print("%s: unknown option: `%c'\n", prog_name,
  329.             (*argvp)[0][1]? (*argvp)[0][1] : '-');
  330.         print(usage_str, prog_name);
  331.     }
  332.     if (look_again && ++(**argvp) != '\0')
  333.         goto DoLookAgain;
  334.     }
  335.  
  336.     if (ison(flags->flg, SEND_NOW) && !flags->draft) {
  337.     print("You must specify a draft file to autosend\n");
  338.     exit(1);
  339.     }
  340. }
  341.