home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume11 / mush5.7 / part11 / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-09-19  |  10.3 KB  |  384 lines

  1. /* @(#)main.c    (c) copyright 10/18/86 (Dan Heller) */
  2.  
  3. #include "mush.h"
  4.  
  5. static char *usage_str =
  6. #ifdef SUNTOOL 
  7.     "usage: %s [-C] [-i] [-f [folder] ] [-v] [-S] [-t] [-s subject] [users]\n";
  8. #else
  9. #ifdef CURSES
  10.     "usage: %s [-C] [-i] [-f [folder] ] [-v] [-S] [-s subject] [user list]\n";
  11. #else
  12.     "usage: %s [-i] [-f [folder] ] [-v] [-S] [-s subject] [user list]\n";
  13. #endif CURSES
  14. #endif SUNTOOL
  15.  
  16. #if defined(sun) && defined(M_DEBUG)
  17. cpu()
  18. {
  19.     print("CPU time limit exceeded!\n");
  20. }
  21. #endif sun && DEBUG
  22.  
  23. /*ARGSUSED*/   /* we ignore envp */
  24. main(argc, argv)
  25. char **argv;
  26. {
  27.     int         flg = NO_FLG, n;
  28.     char         f_flags[10], buf[256], *Cc = NULL;
  29.     register char   *p;
  30.  
  31.     if (prog_name = rindex(*argv, '/'))
  32.     prog_name++;
  33.     else
  34.     prog_name = *argv;
  35.  
  36.     (void) signal(SIGBUS,  bus_n_seg);
  37.     (void) signal(SIGSEGV, bus_n_seg);
  38.  
  39.     f_flags[0] = 0;
  40.     mailfile = "";
  41.  
  42. #if defined(sun) && defined(M_DEBUG)
  43.     (void) signal(SIGXCPU, cpu);
  44.  
  45.     if (p = getenv("MALLOC_DEBUG"))
  46.     malloc_debug(atoi(p));
  47.     else
  48.     malloc_debug(0);
  49. #endif sun && debug
  50.  
  51.     if (!isatty(0))
  52.     turnon(glob_flags, REDIRECT);
  53.     f_flags[0] = '\0';
  54.  
  55.     n = 0; /* don't ignore no such file or directory */
  56.     p = getpath(COMMAND_HELP, &n);
  57.     if (n) {
  58.     fprintf(stderr, "Warning: can't read %s: %s\n", COMMAND_HELP, p);
  59.     cmd_help = "cmd_help";
  60.     } else
  61.     strdup(cmd_help, p);
  62. #ifdef SUNTOOL
  63.     n = 0;
  64.     p = getpath(TOOL_HELP, &n);
  65.     if (n) {
  66.     fprintf(stderr, "Warning: can't read %s: %s\n", TOOL_HELP, p);
  67.     tool_help = "tool_help";
  68.     } else
  69.     strdup(tool_help, p);
  70.  
  71.     istool = strlen(prog_name) > 3 &&
  72.           !strcmp(prog_name+strlen(prog_name)-4, "tool");
  73.     time_out = 60;
  74. #endif SUNTOOL
  75.  
  76.     /*
  77.      * preparse the command line looking for certain arguments which must
  78.      * be known before sourcing the .mushrc file.  This should definitely
  79.      * be optimized -- see the TODO file accompanying this distibution.
  80.      */
  81.     n = 1;
  82.     if (argc > 1) {
  83.     char **args;
  84.     for (args = argv; *args; args++)
  85. #ifdef SUNTOOL
  86.         if (!strcmp(*args, "-t"))
  87.         istool = 1;
  88.         else
  89. #endif SUNTOOL
  90.         if (!strcmp(*args, "-C") && !hdrs_only)
  91.         /* this will only temporarily be turned on! */
  92.         turnon(glob_flags, PRE_CURSES);
  93.         /* headers only?  get args later, just set to a non-null value */
  94.         else if (!strncmp(*args, "-H", 2))
  95.         hdrs_only = NO_STRING, turnoff(glob_flags, PRE_CURSES);
  96.         else if (!strcmp(*args, "-d"))
  97.         debug = 1;
  98.         else if (!strcmp(*args, "-n"))
  99.         n = 0;
  100.     }
  101.  
  102.     /*
  103.      * read mailrc and so forth. Global variables not set there will have
  104.      * default settings here.  Set them first so user overrides in "source()"
  105.      */
  106.     init(); /* must be done before checking mail since "login" is set here */
  107.  
  108.     /* check to see if we have any mail at all */
  109.     if (argc < 2 && !istool) { /* istool may be set with one arg: "mushtool" */
  110.     struct stat statb;
  111.     if (stat(sprintf(buf, "%s/%s", MAILDIR, login), &statb) ||
  112.         statb.st_size == 0) {
  113.         printf("No mail for %s.\n", login);
  114.         exit(1);
  115.     }
  116.     }
  117.     /* -n flag above */
  118.     if (n)
  119.     (void) source(0, DUBL_NULL);
  120.  
  121. #ifdef SUNTOOL
  122.     if (istool)
  123.     if (ison(glob_flags, REDIRECT))
  124.         puts("You can't redirect input to a tool."), exit(1);
  125.     else
  126.         make_tool(argc, argv), turnon(glob_flags, DO_SHELL);
  127. #endif SUNTOOL
  128.  
  129.     for (argv++; *argv && **argv == '-'; argv++)
  130.     switch (argv[0][1]) {
  131.         case 'e':
  132.         /*
  133.          * don't set tty modes -- e.g. echo and cbreak modes aren't
  134.          * changed.
  135.          */
  136.         turnon(glob_flags, ECHO_FLAG);
  137. #ifdef CURSES
  138.         when 'C':
  139.         /* don't init curses -- don't even set iscurses.   */
  140.         if (istool) {
  141.             puts("-C: You are already running in tool mode");
  142.             turnoff(glob_flags, PRE_CURSES);
  143.         } else if (hdrs_only) {
  144.             puts("headers only: ignoring -C flag");
  145.             turnoff(glob_flags, PRE_CURSES);
  146.         } else
  147.             turnon(glob_flags, PRE_CURSES);
  148. #endif CURSES
  149.         when 'N':
  150.         (void) strcat(f_flags, "-N ");
  151.         when 'r':
  152.         (void) strcat(f_flags, "-r "); /* folder() argument */
  153.         when 'H':
  154.         if (istool) {
  155.             puts("running in tool-mode; -H option ignored.");
  156.             break;
  157.         }
  158.         turnoff(glob_flags, PRE_CURSES);
  159.         if (*(hdrs_only = (*argv)+2) != ':')
  160.             hdrs_only = ":a";
  161.         (void) strcat(f_flags, "-N "); /* tell folder not to do_hdrs */
  162.         when 'i':
  163.         /* force interactive even if !isatty(0) */
  164.         turnoff(glob_flags, REDIRECT);
  165.         /* if no argument for this, use mbox in _current directory_ */
  166.         when 'f':
  167.         if (argv[1])
  168.             p = *++argv;
  169.         else if (!(p = do_set(set_options, "mbox")) || !*p)
  170.             p = DEF_MBOX;
  171.         strdup(mailfile, getpath(p, &n));
  172.         if (n) {
  173.             if (n == -1)
  174.             print("%s: %s\n", p, mailfile);
  175.             else
  176.             print("%s: Is a directory\n", mailfile);
  177.             if (!istool)
  178.             exit(1);
  179.             strdup(mailfile, "");
  180.         }
  181.         when '1':
  182.         if (argv[1])
  183.             strdup(cmd_help, *++argv);
  184.         else
  185.             puts("-1 \"filename\""), cleanup(0);
  186. #ifdef SUNTOOL
  187.         when '2':
  188.         if (argv[1])
  189.             strdup(tool_help, *++argv);
  190.         else
  191.             puts("-2 \"filename\""), cleanup(0);
  192. #endif SUNTOOL
  193.         when 's':
  194.         if (istool)
  195.             puts("bad option when run as a tool");
  196.         else if (argv[1])
  197.             (void) strcpy(buf, *++argv);
  198.         else
  199.             puts("-s \"subject\""), cleanup(0);
  200.         when 'c':
  201.         if (istool)
  202.             puts("bad option when run as a tool");
  203.         else if (argv[1])
  204.             strdup(Cc, *++argv);
  205.         else
  206.             puts("-c \"cc list\""), exit(1);
  207.         when 'S': turnon(glob_flags, DO_SHELL); /* force the shell */
  208. #ifdef VERBOSE_ARG
  209.         when 'v':
  210.         if (istool)
  211.             puts("bad option when run as a tool");
  212.         turnon(flg, VERBOSE);
  213. #endif VERBOSE_ARG
  214. #ifdef SUNTOOL
  215.             when 'T':
  216.         if (!*++argv)
  217.             continue;
  218.         if (istool && (time_out = atoi(*argv)) <= 29)
  219.             time_out = 30;
  220.         else
  221.             printf("-T flag ignored unless run as tool."), exit(1);
  222.         when 't': ;   /* prevent error message */
  223. #endif SUNTOOL
  224.         when 'n': case 'd': ;   /* prevent error messages */
  225.         otherwise:
  226.         print("%s: unknown option: `%c'\n", prog_name,
  227.             argv[0][1]? argv[0][1] : '-');
  228.         print(usage_str, prog_name);
  229.         if (!istool)
  230.             exit(1);
  231.     }
  232.  
  233.     /* now we're ready for I/O */
  234.     if (isoff(glob_flags, REDIRECT)) {
  235.     /* make sure we can always recover from no echo mode */
  236.     (void) signal(SIGINT, catch);
  237.     (void) signal(SIGQUIT, catch);
  238.     if (istool)
  239.         turnon(glob_flags, ECHO_FLAG);
  240.     savetty();
  241. #ifdef TIOCGLTC
  242.     if (isatty(0) && ioctl(0, TIOCGLTC, <chars))
  243.         error("TIOCGLTC");
  244. #endif TIOCGLTC
  245. #ifdef SIGCONT
  246.     (void) signal(SIGTSTP, stop_start); /* this will take care of SIGCONT */
  247. #endif SIGCONT
  248.     /* echo_off() checks to see if echo_flg is set, so don't worry */
  249.     echo_off();
  250.     }
  251.  
  252.     if (*argv) {
  253.     char recipients[BUFSIZ];
  254.     if (*mailfile)
  255.         puts("You can't specify more than one folder"), cleanup(0);
  256.     if (istool)
  257.         puts("You can't mail someone and run a tool."), cleanup(0);
  258.     (void) argv_to_string(recipients, argv);
  259.     /* prompt for subject and Cc list, but not "To: "
  260.      * mail_someone() already takes care of redirection.
  261.      * if -s or -c options are given, they will be passed.
  262.      */
  263.     if (do_set(set_options, "autosign"))
  264.         turnon(flg, SIGN);
  265.     if (do_set(set_options, "autoedit"))
  266.         turnon(flg, EDIT);
  267.     if (do_set(set_options, "verbose"))
  268.         turnon(flg, VERBOSE);
  269.     if (do_set(set_options, "fortune")) {
  270.         p = do_set(set_options, "fortunates");
  271.         if (!p || *p && (chk_two_lists(recipients, p, " \t,")
  272.            || (Cc && *Cc && chk_two_lists(Cc, p, " \t,"))))
  273.         turnon(flg, DO_FORTUNE);
  274.     }
  275.     /* set now in case user is not running shell, but is running debug */
  276.     (void) signal(SIGCHLD, sigchldcatcher);
  277.     (void) mail_someone(recipients, buf, Cc, flg, NULL);
  278.     /* do shell set from above: "mush -S user" perhaps */
  279.     if (isoff(glob_flags, DO_SHELL)) {
  280.         if (isoff(glob_flags, REDIRECT))
  281.         echo_on();
  282.         exit(0);
  283.     }
  284.     }
  285.     if (ison(glob_flags, REDIRECT)) {
  286.     puts("You can't redirect input unless you're sending mail.");
  287.     puts("If you want to run a shell with redirection, use \"-i\"");
  288.     cleanup(0);
  289.     }
  290.     if (!*mailfile) {
  291.     strdup(mailfile, sprintf(buf, "%s/%s", MAILDIR, login));
  292.     if (!mail_size() && isoff(glob_flags, DO_SHELL)) {
  293.         /* we know it's not the spool file here */
  294.         printf("No mail in %s.\n", mailfile);
  295.         echo_on(), exit(0);
  296.     }
  297.     }
  298.     /* At this point, we know we're running a shell, so... */
  299.     if (!hdrs_only) {
  300.     /* catch will test DO_SHELL and try to longjmp if set.  this is a
  301.      * transition state from no-shell to do-shell to ignore sigs to
  302.      * avoid a longjmp botch.  Note setjmp isn't called until do_loop().
  303.      */
  304.     turnon(glob_flags, DO_SHELL);
  305.     turnon(glob_flags, IGN_SIGS);
  306. #ifdef CURSES
  307.     if (ison(glob_flags, PRE_CURSES))
  308.         (void) curses_init(0, DUBL_NULL);
  309.     turnoff(glob_flags, PRE_CURSES);
  310. #endif CURSES
  311.     }
  312.  
  313.     /* find a free tmpfile */
  314.     flg = getpid();
  315.     if (!(p = do_set(set_options, "home")) || !*p)
  316.     p = ALTERNATE_HOME;
  317.     while (!access(sprintf(tempfile, "%s/.%s%d", p, prog_name, flg++), 0))
  318.     ;
  319.     /* just create the file, make sure it's empty.  It'll close later and
  320.      * be reopened for reading only.
  321.      */
  322.     {
  323.     int omask = umask(077);
  324.     if (!(tmpf = fopen(tempfile, "w"))) {
  325.         error("Can't create tempfile %s", tempfile);
  326.         cleanup(0);
  327.     }
  328.     (void) umask(omask);
  329.     }
  330.  
  331.     /* do pseudo-intelligent stuff with certain signals */
  332.     (void) signal(SIGINT,  catch);
  333.     (void) signal(SIGQUIT, catch);
  334.     (void) signal(SIGHUP,  catch);
  335.  
  336.     /* if we're a suntool, then state the version and mark first message
  337.      * as "read" since it will be displayed right away
  338.      */
  339.     if (!hdrs_only && !istool && !do_set(set_options, "quiet"))
  340.     printf("%s: Type '?' for help.\n", VERSION);
  341.  
  342.     (void) sprintf(buf, "folder %s %s", f_flags, mailfile);
  343.     if (argv = make_command(buf, TRPL_NULL, &argc)) {
  344.     (void) folder(argc, argv, NULL);
  345.     free_vec(argv);
  346.     }
  347.  
  348.     if (hdrs_only) {
  349.     (void) sprintf(buf, "headers %s", hdrs_only);
  350.     if (argv = make_command(buf, TRPL_NULL, &argc))
  351.         (void) do_hdrs(argc, argv, NULL);
  352.     cleanup(0);
  353.     }
  354.  
  355.     if (istool && msg_cnt)
  356.     set_isread(current_msg);
  357.  
  358.     sort_commands();
  359.     sort_variables();
  360.  
  361. #ifdef SUNTOOL
  362.     if (istool) {
  363.     turnoff(glob_flags, IGN_SIGS);
  364.     (void) do_hdrs(0, DUBL_NULL, NULL);
  365.     timerclear(&(mail_timer.it_interval));
  366.     timerclear(&(mail_timer.it_value));
  367.     mail_timer.it_value.tv_sec = time_out;
  368.     setitimer(ITIMER_REAL, &mail_timer, NULL);
  369.     (void) signal(SIGALRM, check_new_mail);
  370.     unlock_cursors();
  371.     while (!(tool->tl_flags & TOOL_DONE))
  372.         tool_select(tool, 1);
  373.     cleanup(0);
  374.     }
  375. #endif SUNTOOL
  376.     do_loop();
  377. }
  378.  
  379. do_version()
  380. {
  381.     print("%s\n", VERSION);
  382.     return -1;
  383. }
  384.