home *** CD-ROM | disk | FTP | other *** search
/ ftp.freefriends.org / ftp.freefriends.org.tar / ftp.freefriends.org / arnold / Source / mush.rstevens.tar.gz / mush.tar / options.c < prev    next >
C/C++ Source or Header  |  1992-10-30  |  9KB  |  351 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. #if defined(SUNTOOL) || defined(POP3_SUPPORT)
  95.         case 'T' :
  96.             if (args[1])
  97.             args++;
  98. #ifdef POP3_SUPPORT
  99.             break;
  100. #endif /* POP3_SUPPORT */
  101. #ifdef SUNTOOL
  102.         case 't' :
  103.             /* Note: we won't ever get here if started as
  104.              * "mushtool" or "mushview" because istool is true.
  105.              */
  106.             istool = 1;
  107.             parse_tool_opts(argcp, argv);
  108.             turnon(glob_flags, DO_SHELL);
  109.             return TRUE;
  110.             /* break; */
  111. #endif /* SUNTOOL */
  112. #endif /* SUNTOOL || POP3_SUPPORT */
  113.         case 'S' :
  114.             turnon(glob_flags, DO_SHELL);
  115.             n = TRUE;
  116.             break;
  117.         case 'f' :
  118.         case 'F' :
  119.         case 'h' :
  120.         case 'm' :
  121.         case 'u' :
  122.             n = TRUE;
  123.         case 'b' :
  124.         case 'c' :
  125.         case 'I' :
  126.         case 's' :
  127.             if (args[1]) {
  128.             args++;
  129.             next = 0;
  130.             }
  131.             break;
  132.         case 'H' :
  133.             if (args[0][next+1] == ':')
  134.             next = 0;
  135.             break;
  136.         case '\0':
  137.             next = 0;
  138.         default : ;
  139.         }
  140.         if (next) {
  141.         ++next;
  142.         goto DoNext;
  143.         }
  144.     }
  145.     if (*args) {  /* unused args indicates sending mail to someone */
  146.         n = TRUE;
  147.         if (!istool)
  148.         turnon(glob_flags, IS_SENDING);
  149.     }
  150.     }
  151.  
  152.     return n;
  153. }
  154.  
  155. static char *usage_str =
  156. #ifdef SUNTOOL
  157.     "usage: %s [-t] [-C] [-i] [-f [folder] ] [-v] [-S] [-s subject] [users]\n";
  158. #else
  159. #ifdef CURSES
  160.     "usage: %s [-C] [-i] [-f [folder] ] [-v] [-S] [-s subject] [user list]\n";
  161. #else
  162.     "usage: %s [-i] [-f [folder] ] [-v] [-S] [-s subject] [user list]\n";
  163. #endif /* CURSES */
  164. #endif /* SUNTOOL */
  165.  
  166. parse_options(argvp, flags)
  167. register char ***argvp;
  168. struct mush_flags *flags;
  169. {
  170.     char buf[256];
  171.  
  172.     bzero((char *) flags, sizeof (struct mush_flags));
  173.     flags->source_rc = TRUE;
  174.     flags->folder = "";
  175.  
  176.     for (++(*argvp); **argvp && ***argvp == '-'; (*argvp)++) {
  177.     int look_again;
  178. DoLookAgain:
  179.     look_again = TRUE;
  180.     switch ((*argvp)[0][1]) {
  181.         case 'e':
  182.         /*
  183.          * don't set tty modes -- e.g. echo and cbreak modes aren't
  184.          * changed.
  185.          */
  186.         turnon(glob_flags, ECHO_FLAG);
  187. #ifdef CURSES
  188.         when 'C':
  189.         /* don't init curses -- don't even set iscurses.   */
  190.         if (istool) {
  191.             puts("-C: You are already running in tool mode");
  192.             turnoff(glob_flags, PRE_CURSES);
  193.         } else if (hdrs_only)
  194.             puts("headers only: ignoring -C flag");
  195.         else
  196.             turnon(glob_flags, PRE_CURSES);
  197. #endif /* CURSES */
  198.         when 'F':
  199.         flags->src_n_exit = ((*argvp)[0][2] == '!');
  200.         if (!(flags->src_file = *++(*argvp)))
  201.             puts("specify filename to source"), exit(1);
  202.         look_again = FALSE;
  203.         /* fall thru! */
  204.         case 'N':
  205.         (void) strcat(flags->f_flags, "-N ");
  206.         when 'r':
  207.         (void) strcat(flags->f_flags, "-r "); /* folder() argument */
  208.         when 'H':
  209.         if (istool) {
  210.             puts("running in tool-mode; -H option ignored.");
  211.             break;
  212.         }
  213.         turnoff(glob_flags, PRE_CURSES);
  214.         if (*(hdrs_only = (*(*argvp))+2) != ':')
  215.             hdrs_only = ":a";
  216.         else
  217.             look_again = FALSE;
  218.         /* read only cuz no updates */
  219.         (void) strcat(flags->f_flags, "-N -r ");
  220.         when 'i':
  221.         /* force interactive even if !isatty(0) */
  222.         turnoff(glob_flags, REDIRECT);
  223.         when 'u': /* specify a user's mailbox */
  224.         if (*(flags->folder))
  225.             puts("You can't specify more than one mailbox"), exit(1);
  226. #ifdef HOMEMAIL
  227.         {
  228.             char *p;
  229.             int isdir = 1;
  230.             (void) sprintf(buf, "%%%s",
  231.                 (*argvp)[1] ? (*argvp)[1] : "root");
  232.             if ((p = getpath(buf, &isdir)) && !isdir)
  233.             strdup(flags->folder, p);
  234.             else if (isdir < 0)
  235.             puts(p), exit(1);
  236.             else if (isdir)
  237.             (void) printf("\"%s\" is a directory\n", p), exit(1);
  238.         }
  239. #else /* HOMEMAIL */
  240.         strdup(flags->folder, sprintf(buf, "%s/%s",
  241.                    MAILDIR, ((*argvp)[1])? (*argvp)[1] : "root"));
  242. #endif /* HOMEMAIL */
  243.         if ((*argvp)[1])
  244.             ++(*argvp);
  245.         look_again = FALSE;
  246.         when 'h':
  247.         if (istool)
  248.             puts("bad option when run as a tool"), exit(1);
  249.         if ((*argvp)[1])
  250.             flags->draft = *++(*argvp);
  251.         else
  252.             (void) printf("-h: missing file name.\n"), exit(1);
  253.         look_again = FALSE;
  254.         turnon(glob_flags, IS_SENDING);
  255.         when 'U':
  256.         if (istool)
  257.             puts("bad option when run as a tool"), exit(1);
  258.         turnon(flags->flg, SEND_NOW);
  259.         if ((*argvp)[0][2] == '!') {
  260.             turnon(flags->flg, NO_SIGN);
  261.             ++(**argvp);
  262.         }
  263.         when 'm':
  264.         if ((*argvp)[1])
  265.             strdup(spoolfile, *++(*argvp));
  266.         else
  267.             (void) printf("-m: missing mailbox name.\n"), exit(1);
  268.         look_again = FALSE;
  269.         when 'f':
  270.         if (*(flags->folder))
  271.             puts("You can't specify more than one mailbox"), exit(1);
  272.         if ((*argvp)[1]) {
  273.             strdup(flags->folder, *++(*argvp));
  274.             look_again = FALSE;
  275.         } else
  276.             strdup(flags->folder, "&");
  277.         when 's':
  278.         if (istool)
  279.             puts("bad option when run as a tool"), exit(1);
  280.         else if ((*argvp)[1])
  281.             flags->Subj = *++(*argvp);
  282.         else
  283.             puts("-s \"subject\""), exit(1);
  284.         look_again = FALSE;
  285.         when 'b':
  286.         if (istool)
  287.             puts("-b: bad option when run as a tool"), exit(1);
  288.         else if ((*argvp)[1])
  289.             flags->Bcc = *++(*argvp);
  290.         else
  291.             puts("-b \"bcc list\""), exit(1);
  292.         look_again = FALSE;
  293.         when 'c':
  294.         if (istool)
  295.             puts("-c: bad option when run as a tool"), exit(1);
  296.         else if ((*argvp)[1])
  297.             flags->Cc = *++(*argvp);
  298.         else
  299.             puts("-c \"cc list\""), exit(1);
  300.         look_again = FALSE;
  301.         break;
  302. #ifdef VERBOSE_ARG
  303.         case 'v':
  304.         if (istool)
  305.             puts("bad option when run as a tool"), exit(1);
  306.         turnon(flags->flg, VERBOSE);
  307.         break;
  308. #endif /* VERBOSE_ARG */
  309. #if defined(SUNTOOL) || defined(POP3_SUPPORT)
  310.         case 'T':
  311.         if ((time_out = atoi(*(*argvp))) < MIN_TIME_OUT)
  312.             time_out = MIN_TIME_OUT;
  313.         look_again = FALSE;
  314. #ifdef POP3_SUPPORT
  315.         break;
  316. #endif /* POP3_SUPPORT */
  317. #ifdef SUNTOOL
  318.         /* -T implies -t */
  319.         case 't': istool = 1;
  320. #endif /* SUNTOOL */
  321. #endif /* SUNTOOL || POP3_SUPPORT */
  322.         case 'S': turnon(glob_flags, DO_SHELL);
  323.         when 'n':
  324.         if ((*argvp)[0][2] == '!') {
  325.             ++(**argvp);
  326.             flags->source_rc = -1;    /* No init files sourced */
  327.         } else
  328.             flags->source_rc = 0;    /* Only ~/.mushrc sourced */
  329.         when 'I':
  330.         if ((*argvp)[0][2] == '!' && flags->source_rc > 0)
  331.             flags->source_rc = 0;    /* Only ~/.mushrc sourced */
  332.         if (!(flags->init_file = *++(*argvp)))
  333.             puts("specify filename for init"), exit(1);
  334.         look_again = FALSE;
  335.         when 'd': debug = 1;
  336.         when '\0' : look_again = FALSE;
  337.         otherwise:
  338.         print("%s: unknown option: `%c'\n", prog_name,
  339.             (*argvp)[0][1]? (*argvp)[0][1] : '-');
  340.         print(usage_str, prog_name);
  341.     }
  342.     if (look_again && ++(**argvp) != '\0')
  343.         goto DoLookAgain;
  344.     }
  345.  
  346.     if (ison(flags->flg, SEND_NOW) && !flags->draft) {
  347.     print("You must specify a draft file to autosend\n");
  348.     exit(1);
  349.     }
  350. }
  351.