home *** CD-ROM | disk | FTP | other *** search
- /*
- * MAIN.C
- *
- * (C) Copyright 1985-1990 by Matthew Dillon, All Rights Reserved.
- *
- * Global Routines: MAIN()
- * INIT()
- * SIG_HANDLE()
- *
- * Static Routines: none.
- */
-
- #include <pwd.h>
- #include <stdio.h>
- #include <signal.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <config.h>
- #include "version.h"
-
- #include "dmail.h"
-
- IDENT (".17");
-
- char
- *dillon_cpr = DCOPYRIGHT;
- int
- _bufsiz = 3072;
-
- #define MAILHOME MakeConfigPath (UUMAIL, "")
- #define MBOX MakeConfigPath (UUMAIL, "mbox")
- #define ALT_MBOX MakeConfigPath (UUMAIL, ".mbox")
- #define MAILRC ".dmailrc"
- #define VISUAL GetConfig (EDITOR, "dme")
-
- Prototype void init (void);
- Prototype int source_file (char *file);
- Prototype void sig_handle (int);
- Prototype int get_inode (char *file);
-
- int
- main (int argc, char **argv)
- {
- int
- i,
- next,
- Retry,
- fop = 0, /* -f specified */
- oop = 0, /* -o specified */
- rcload = 1, /* whether to load dmailrc or not */
- options = 1,
- no_mail_overide = 0,
- nc = 0;
- static int
- nameslist [128];
- char
- *rcname = NULL;
-
- if (push_base ())
- done (30);
-
- init ();
- for (i = 1; i < argc; ++i) {
- next = 0;
- if ((*argv[i] == '-') && options) {
- if (*(argv[i] + 1) == '\0') {
- options = 0;
- continue;
- }
- while (*++argv[i]) {
- switch (*argv[i]) {
- case 'S':
- lmessage_overide = 1;
- break;
- case 'O':
- no_mail_overide = 1;
- break;
- case 'l':
- rcload = 1;
- if (i + 1 < argc && *argv[i + 1] != '-') {
- oop = 1;
- ++i;
- ++next;
- rcname = argv[i];
- }
- break;
- case 'L':
- rcload = 0;
- break;
- case 'D':
- XDebug = 1;
- break;
- case 'F':
- if (++i < argc) {
- add_extra (argv[i]);
- }
- else {
- puts (" -F Requires Field argument");
- exit (30);
- }
- ++next;
- break;
- case 'v':
- set_var (LEVEL_SET, "verbose", "");
- break;
- case 'o':
- xfree (output_file);
- if (i + 1 < argc && *argv[i + 1] != '-') {
- oop = 1;
- ++i;
- ++next;
- output_file = xmalloc (strlen (argv[i]) + 1);
- strcpy (output_file, argv[i]);
- }
- else {
- oop = -1;
- output_file = xmalloc (strlen(home_dir) +
- strlen(ALT_MBOX) + 2);
-
- #ifdef AMIGA
- strcpy (output_file, ALT_MBOX);
- #else
- sprintf (output_file, "%s/%s", home_dir, ALT_MBOX);
- #endif
- }
- break;
- case 'f':
- if (i + 1 < argc && *argv[i + 1] != '-') {
- fop = 1;
- ++i;
- ++next;
- mail_file = realloc (mail_file, strlen (argv[i]) + 1);
- strcpy (mail_file, argv[i]);
- }
- else {
- fop = -1;
- mail_file = realloc (mail_file,
- strlen(home_dir) + strlen(MBOX) + 2);
- #ifdef AMIGA
- strcpy (mail_file, MBOX);
- #else
- sprintf (mail_file, "%s/%s", home_dir, MBOX);
- #endif
- }
- break;
- default:
- fprintf (stderr, "dmail: Bad argument '%s'\n", argv [i]);
- fprintf (stderr, "dmail -O then 'help' for help.\n");
- exit (30);
- }
- if (next)
- break;
- }
- }
- else {
- No_load_mail = 1;
- nameslist[nc++] = i;
- }
- }
- if (oop == -1 && fop == -1) {
- mail_file = realloc (mail_file, strlen (output_file) + 1);
- strcpy (mail_file, output_file);
- }
- ends:
- initial_load_mail ();
- m_select (Nulav, M_RESET);
- Current = indexof (1);
- if (nc)
- set_var (LEVEL_SET, "comlinemail", "");
- if (rcload) {
- if (rcname == NULL) {
- #ifdef AMIGA
- sprintf (Buf, "%s%s" MAILRC, MakeConfigPath (UULIB, ""), user_name);
- if (source_file(Buf) < 0)
- source_file (MakeConfigPath (UULIB, MAILRC));
- #else
- rcname = xmalloc (strlen (home_dir) + strlen (MAILRC) + 2);
- sprintf (rcname, "%s/%s", home_dir, MAILRC);
- source_file (rcname);
- #endif
- }
- else {
- source_file (rcname);
- }
- }
-
- if (nc) {
- av [0] = "mail";
- for (i = 0; i < nc; ++i)
- av [i + 1] = argv [nameslist [i]];
- ac = nc + 1;
- do_reply ("", R_MAIL);
- done (0);
- }
-
- if (Entries + no_mail_overide == 0) {
- printf ("\nNO MAIL for %s\n\n", user_name);
- return 5;
- }
-
- printf ("\nRF %-20s WF %-20s\n", mail_file, output_file);
- do {
- Retry = 20;
- pop_base ();
- loop:
- if (push_base ()) {
- pop_base ();
- if (XDebug)
- printf ("TOP LEVEL INTR, Level: %d\n", Longstack);
- if (--Retry == 0)
- done (30);
- puts ("");
- goto loop;
- }
- check_new_mail ();
- } while (do_command () > 0);
-
- return 0;
- }
-
- int
- source_file (char *file)
- {
- char
- *ptr = xmalloc (strlen (file) + 1);
- int
- result;
-
- strcpy (ptr, file);
- ac = 2;
- av [1] = ptr;
- result = do_source (ptr, 1);
- free (ptr);
- return result;
- }
-
- void
- init (void)
- {
- char
- *str;
- struct passwd
- *passwd;
-
- Entry = (struct ENTRY *) xmalloc (sizeof (*Entry));
- Entry->status = Entry->no = Entry->fpos = 0;
-
- passwd = (struct passwd *) getpwuid (getuid ());
- if (passwd == NULL) {
- fprintf (stderr, "DMail, unable to get passwd entry for uid %d\n", getuid ());
- exit (30);
- }
-
- user_name = xmalloc (strlen (passwd->pw_name) + 1);
- home_dir = xmalloc (strlen (passwd->pw_dir) + 1);
- visual = xmalloc (strlen (VISUAL) + 1);
-
- strcpy (visual , VISUAL);
- strcpy (user_name, passwd->pw_name);
- strcpy (home_dir , passwd->pw_dir);
-
- #ifdef AMIGA
- if (str = FindConfig (HOME))
- strcpy ((home_dir = realloc (home_dir, strlen (str) + 1)), str);
- if (str = GetUserName ())
- strcpy ((user_name = realloc (user_name, strlen (str) + 1)), str);
- if (str = FindConfig (EDITOR))
- strcpy ((visual = realloc (visual, strlen (str) + 1)), str);
- #else
- if ((str = getenv ("HOME")) != NULL)
- strcpy ((home_dir = realloc (home_dir, strlen (str) + 1)), str);
- if ((str = getenv ("USER")) != NULL)
- strcpy ((user_name = realloc (user_name, strlen (str) + 1)), str);
- if ((str = getenv ("VISUAL")) != NULL)
- strcpy ((visual = realloc (visual, strlen (str) + 1)), str);
- #endif
- mail_file = xmalloc (strlen (MAILHOME) + strlen (user_name) + 1);
- sprintf (mail_file , "%s%s", MAILHOME, user_name);
- output_file = xmalloc (strlen (home_dir) + 2 + strlen (MBOX) + 1);
- #ifdef AMIGA
- strcpy (output_file, MBOX);
- #else
- sprintf (output_file, "%s/%s", home_dir, MBOX);
- #endif
- fix_globals ();
- #ifdef UNIX
- signal (SIGHUP, sig_handle);
- signal (SIGINT, sig_handle);
- signal (SIGPIPE, SIG_IGN);
- #endif
- #ifdef AMIGA
- signal (SIGINT, sig_handle);
- #endif
- set_var (LEVEL_SET, "user", user_name);
-
- return;
- }
-
- void
- sig_handle (int sig)
- {
- #ifdef UNIX
- int
- mask = sigblock (0);
-
- sigsetmask (mask & ~((1 << SIGHUP) | (1 << SIGINT)));
- #endif
- #ifdef AMIGA
- signal (SIGINT, sig_handle); /* reload signal */
- #endif
- if (Longstack && !Breakstack)
- longjmp (env [Longstack], 1);
-
- return;
- }
-
- int
- get_inode (char *file)
- {
- struct stat
- stats;
-
- if (stat (file, &stats) < 0)
- return -1;
-
- return stats.st_ino;
- }
-