home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / fish / telecom / uucp_442 / src / dmail / main.c < prev    next >
C/C++ Source or Header  |  1991-01-10  |  6KB  |  298 lines

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