home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / duucp-1.17 / AU-117b4-src.lha / src / dmail / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-13  |  6.6 KB  |  329 lines

  1. /*
  2.  * MAIN.C
  3.  *
  4.  *  (C) Copyright 1985-1990 by Matthew Dillon,    All Rights Reserved.
  5.  *
  6.  *  Global Routines:    MAIN()
  7.  *            INIT()
  8.  *            SIG_HANDLE()
  9.  *
  10.  *  Static Routines:    none.
  11.  */
  12.  
  13. #include <pwd.h>
  14. #include <stdio.h>
  15. #include <signal.h>
  16. #include <string.h>
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <config.h>
  20. #include "version.h"
  21.  
  22. #include "dmail.h"
  23.  
  24. IDENT (".17");
  25.  
  26. char
  27.     *dillon_cpr = DCOPYRIGHT;
  28. int
  29.     _bufsiz = 3072;
  30.  
  31. #define MAILHOME   MakeConfigPath (UUMAIL, "")
  32. #define MBOX       MakeConfigPath (UUMAIL, "mbox")
  33. #define ALT_MBOX   MakeConfigPath (UUMAIL, ".mbox")
  34. #define MAILRC       ".dmailrc"
  35. #define VISUAL       GetConfig (EDITOR, "dme")
  36.  
  37. Prototype void init (void);
  38. Prototype int  source_file (char *file);
  39. Prototype void sig_handle (int);
  40. Prototype int  get_inode (char *file);
  41.  
  42. int
  43. main (int argc, char **argv)
  44. {
  45.     int
  46.         i,
  47.         next,
  48.         Retry,
  49.         fop = 0,    /* -f specified */
  50.         oop = 0,    /* -o specified */
  51.         rcload = 1,    /* whether to load dmailrc or not */
  52.         options = 1,
  53.         no_mail_overide = 0,
  54.         nc = 0;
  55.     static int
  56.         nameslist [128];
  57.     char
  58.         *rcname = NULL;
  59.  
  60.     if (push_base ())
  61.         done (30);
  62.  
  63.     init ();
  64.     for (i = 1; i < argc; ++i) {
  65.         next = 0;
  66.         if ((*argv[i] == '-') && options) {
  67.             if (*(argv[i] + 1) == '\0') {
  68.                 options = 0;
  69.                 continue;
  70.             }
  71.             while (*++argv[i]) {
  72.                 switch (*argv[i]) {
  73.                     case 'S':
  74.                         lmessage_overide = 1;
  75.                         break;
  76.                     case 'O':
  77.                         no_mail_overide = 1;
  78.                         break;
  79.                     case 'l':
  80.                         rcload    = 1;
  81.                         if (i + 1 < argc  &&  *argv[i + 1] != '-') {
  82.                             oop = 1;
  83.                             ++i;
  84.                             ++next;
  85.                             rcname = argv[i];
  86.                         }
  87.                         break;
  88.                     case 'L':
  89.                         rcload = 0;
  90.                         break;
  91.                     case 'D':
  92.                         XDebug = 1;
  93.                         break;
  94.                     case 'F':
  95.                         if (++i < argc) {
  96.                             add_extra (argv[i]);
  97.                         }
  98.                         else {
  99.                             puts (" -F Requires Field argument");
  100.                             exit (30);
  101.                         }
  102.                         ++next;
  103.                         break;
  104.                     case 'v':
  105.                         set_var (LEVEL_SET, "verbose", "");
  106.                         break;
  107.                     case 'o':
  108.                         xfree (output_file);
  109.                         if (i + 1 < argc  &&  *argv[i + 1] != '-') {
  110.                             oop = 1;
  111.                             ++i;
  112.                             ++next;
  113.                             output_file = xmalloc (strlen (argv[i]) + 1);
  114.                             strcpy (output_file, argv[i]);
  115.                         }
  116.                         else {
  117.                             oop = -1;
  118.                             output_file = xmalloc (strlen(home_dir) +
  119.                                 strlen(ALT_MBOX) + 2);
  120.  
  121. #ifdef AMIGA
  122.                             strcpy (output_file, ALT_MBOX);
  123. #else
  124.                             sprintf (output_file, "%s/%s", home_dir, ALT_MBOX);
  125. #endif
  126.                         }
  127.                         break;
  128.                     case 'f':
  129.                         if (i + 1 < argc  &&  *argv[i + 1] != '-') {
  130.                             fop = 1;
  131.                             ++i;
  132.                             ++next;
  133.                             mail_file = realloc (mail_file, strlen (argv[i]) + 1);
  134.                             strcpy (mail_file, argv[i]);
  135.                         }
  136.                         else {
  137.                             fop = -1;
  138.                             mail_file = realloc (mail_file,
  139.                                 strlen(home_dir) + strlen(MBOX) + 2);
  140. #ifdef AMIGA
  141.                             strcpy (mail_file, MBOX);
  142. #else
  143.                             sprintf (mail_file, "%s/%s", home_dir, MBOX);
  144. #endif
  145.                         }
  146.                         break;
  147.                     default:
  148.                         fprintf (stderr, "dmail: Bad argument '%s'\n", argv [i]);
  149.                         fprintf (stderr, "dmail -O      then 'help' for help.\n");
  150.                         exit (30);
  151.                 }
  152.                 if (next)
  153.                     break;
  154.             }
  155.         }
  156.         else {
  157.             No_load_mail = 1;
  158.             nameslist[nc++] = i;
  159.         }
  160.     }
  161.     if (oop == -1  &&  fop == -1) {
  162.         mail_file = realloc (mail_file, strlen (output_file) + 1);
  163.         strcpy (mail_file, output_file);
  164.     }
  165. ends:
  166.     initial_load_mail ();
  167.     m_select (Nulav, M_RESET);
  168.     Current = indexof (1);
  169.     if (nc)
  170.         set_var (LEVEL_SET, "comlinemail", "");
  171.     if (rcload) {
  172.         if (rcname == NULL) {
  173. #ifdef AMIGA
  174.             sprintf (Buf, "%s%s" MAILRC, MakeConfigPath (UULIB, ""), user_name);
  175.             if (source_file(Buf) < 0)
  176.                 source_file (MakeConfigPath (UULIB, MAILRC));
  177. #else
  178.             rcname = xmalloc (strlen (home_dir) + strlen (MAILRC) + 2);
  179.             sprintf (rcname, "%s/%s", home_dir, MAILRC);
  180.             source_file (rcname);
  181. #endif
  182.         }
  183.         else {
  184.             source_file (rcname);
  185.         }
  186.     }
  187.  
  188.     if (nc) {
  189.         av [0] = "mail";
  190.         for (i = 0; i < nc; ++i)
  191.             av [i + 1] = argv [nameslist [i]];
  192.         ac = nc + 1;
  193.         do_reply ("", R_MAIL);
  194.         done (0);
  195.     }
  196.  
  197.     if (Entries + no_mail_overide == 0) {
  198.         printf ("\nNO MAIL for %s\n\n", user_name);
  199.         return 5;
  200.     }
  201.  
  202.     printf ("\nRF %-20s   WF %-20s\n", mail_file, output_file);
  203.     do {
  204.         Retry = 20;
  205.         pop_base ();
  206. loop:
  207.         if (push_base ()) {
  208.             pop_base ();
  209.             if (XDebug)
  210.                 printf ("TOP LEVEL INTR, Level: %d\n", Longstack);
  211.             if (--Retry == 0)
  212.                 done (30);
  213.             puts ("");
  214.             goto loop;
  215.         }
  216.         check_new_mail ();
  217.     } while (do_command () > 0);
  218.  
  219.     return 0;
  220. }
  221.  
  222. int
  223. source_file (char *file)
  224. {
  225.     char
  226.         *ptr = xmalloc (strlen (file) + 1);
  227.     int
  228.         result;
  229.  
  230.     strcpy (ptr, file);
  231.     ac = 2;
  232.     av [1] = ptr;
  233.     result = do_source (ptr, 1);
  234.     free (ptr);
  235.     return result;
  236. }
  237.  
  238. void
  239. init (void)
  240. {
  241.     char
  242.         *str;
  243.     struct passwd
  244.         *passwd;
  245.  
  246.     Entry = (struct ENTRY *) xmalloc (sizeof (*Entry));
  247.     Entry->status = Entry->no = Entry->fpos = 0;
  248.  
  249.     passwd = (struct passwd *) getpwuid (getuid ());
  250.     if (passwd == NULL) {
  251.         fprintf (stderr, "DMail, unable to get passwd entry for uid %d\n", getuid ());
  252.         exit (30);
  253.     }
  254.  
  255.     user_name   = xmalloc (strlen (passwd->pw_name) + 1);
  256.     home_dir    = xmalloc (strlen (passwd->pw_dir) + 1);
  257.     visual        = xmalloc (strlen (VISUAL) + 1);
  258.  
  259.     strcpy    (visual   , VISUAL);
  260.     strcpy    (user_name, passwd->pw_name);
  261.     strcpy    (home_dir , passwd->pw_dir);
  262.  
  263. #ifdef AMIGA
  264.     if (str = FindConfig (HOME))
  265.         strcpy ((home_dir = realloc (home_dir, strlen (str) + 1)), str);
  266.     if (str = GetUserName ())
  267.         strcpy ((user_name = realloc (user_name, strlen (str) + 1)), str);
  268.     if (str = FindConfig (EDITOR))
  269.         strcpy ((visual = realloc (visual, strlen (str) + 1)), str);
  270. #else
  271.     if ((str = getenv ("HOME")) != NULL)
  272.         strcpy ((home_dir = realloc (home_dir, strlen (str) + 1)), str);
  273.     if ((str = getenv ("USER")) != NULL)
  274.         strcpy ((user_name = realloc (user_name, strlen (str) + 1)), str);
  275.     if ((str = getenv ("VISUAL")) != NULL)
  276.         strcpy ((visual = realloc (visual, strlen (str) + 1)), str);
  277. #endif
  278.     mail_file   = xmalloc (strlen (MAILHOME) + strlen (user_name) + 1);
  279.     sprintf (mail_file  , "%s%s", MAILHOME, user_name);
  280.     output_file = xmalloc (strlen (home_dir) + 2 + strlen (MBOX) + 1);
  281. #ifdef AMIGA
  282.     strcpy (output_file, MBOX);
  283. #else
  284.     sprintf (output_file, "%s/%s", home_dir, MBOX);
  285. #endif
  286.     fix_globals ();
  287. #ifdef UNIX
  288.     signal (SIGHUP, sig_handle);
  289.     signal (SIGINT, sig_handle);
  290.     signal (SIGPIPE, SIG_IGN);
  291. #endif
  292. #ifdef AMIGA
  293.     signal (SIGINT, sig_handle);
  294. #endif
  295.     set_var (LEVEL_SET, "user", user_name);
  296.  
  297.     return;
  298. }
  299.  
  300. void
  301. sig_handle (int sig)
  302. {
  303. #ifdef UNIX
  304.     int
  305.         mask = sigblock (0);
  306.  
  307.     sigsetmask (mask & ~((1 << SIGHUP) | (1 << SIGINT)));
  308. #endif
  309. #ifdef AMIGA
  310.     signal (SIGINT, sig_handle);    /*  reload signal */
  311. #endif
  312.     if (Longstack  &&  !Breakstack)
  313.         longjmp (env [Longstack], 1);
  314.  
  315.     return;
  316. }
  317.  
  318. int
  319. get_inode (char *file)
  320. {
  321.     struct stat
  322.         stats;
  323.  
  324.     if (stat (file, &stats) < 0)
  325.         return -1;
  326.  
  327.     return stats.st_ino;
  328. }
  329.