Rework of Editor/defaulteditor, similar to Pager/defaultpager; new env vars.
[mmh] / sbr / context_read.c
index d2836ba..5733d3f 100644 (file)
-
 /*
- * context_read.c -- find and read profile and context files
- *
- * $Id$
- */
-
-#include <h/mh.h>
-#include <errno.h>
-#include <pwd.h>
+** context_read.c -- find and read profile and context files
+**
+** This code is Copyright (c) 2002, by the authors of nmh.  See the
+** COPYRIGHT file in the root directory of the nmh distribution for
+** complete copyright information.
+**
+** This function must be called early on in any nmh utility, and
+** may only be called once.  It does the following:
+**
+**  o  Sets the global variables to absolute paths:
+**     - "mypath": home directory (HOME)
+**     - "mmhpath": mmh directory (MMH)
+**     - "defpath": profile file (MMHP)
+**     - "ctxpath": context file (MMHC)
+**
+**  o  Reads in the profile file.  Bails out if it can't.
+**
+**  o  Makes sure that the mail directory exists, prompting for
+**     creation if it doesn't.
+**
+**  o  Reads the context file.
+**
+** You might need to adjust uip/mmh.sh if you make changes here.
+*/
 
-extern int errno;
+#include <h/mh.h>    /* mh internals */
+#include <errno.h>   /* system call errors */
+#include <pwd.h>     /* structure for getpwuid() results */
 
 void
-context_read (void)
+context_read(void)
 {
-    pid_t pid;
-    register char *cp, *pp;
-    char buf[BUFSIZ];
-    struct stat st;
-    register struct passwd *pw;
-    register FILE *ib;
+       char buf[BUFSIZ];            /* path name buffer                 */
+       char *cp;                    /* miscellaneous pointer            */
+       char *nd;                    /* nmh directory pointer            */
+       struct stat st;              /* stat() results                   */
+       register struct passwd *pw;  /* getpwuid() results               */
+       register FILE *ib;           /* profile and context file pointer */
+
+       /*
+       ** If this routine _is_ called again (despite the wanings in the
+       ** comments above), return immediately.
+       */
+       if (m_defs) {
+               return;
+       }
 
-    if (defpath)
-       return;
+       /*
+       ** Find user's home directory. Try the HOME environment variable first,
+       ** the home directory field in the password file if that's not found.
+       */
+       if (!(mypath = getenv("HOME"))) {
+               if (!(pw = getpwuid(getuid())) || !*pw->pw_dir) {
+                       adios(NULL, "cannot determine your home directory");
+               }
+               mypath = pw->pw_dir;
+       }
 
-    /*
-     * Find user's home directory
-     */
-    if (!mypath) {
-       if ((mypath = getenv ("HOME")))
-           mypath = getcpy (mypath);
-       else
-           if ((pw = getpwuid (getuid ())) == NULL
-                   || pw->pw_dir == NULL
-                   || *pw->pw_dir == 0)
-               adios (NULL, "no HOME envariable");
-           else
-               mypath = getcpy (pw->pw_dir);
-       if ((cp = mypath + strlen (mypath) - 1) > mypath && *cp == '/')
-           *cp = 0;
-    }
+       /*
+       ** set mmhpath
+       */
+       if ((cp = getenv("MMH")) && *cp) {
+               mmhpath = getcpy(expanddir(cp));  /* rel to cwd */
+               if (stat(mmhpath, &st) != -1 && (st.st_mode & S_IFDIR) == 0) {
+                       adios(NULL, "`%s' specified by your MMH environment variable is not a directory", cp);
+               }
+       } else {
+               mmhpath = concat(mypath, "/", mmhdir, NULL);
+               if (stat(mmhpath, &st) == -1 || (st.st_mode & S_IFDIR) == 0) {
+                       adios(NULL, "Doesn't look like mmh is set up for your account.  Run `install-mh' to do so.");
+               }
+       }
 
-    /*
-     * open and read user's profile
-     */
-    if ((cp = getenv ("MH")) && *cp != '\0') {
-       defpath = path (cp, TFILE);
-       if ((ib = fopen (defpath, "r")) == NULL)
-           adios (defpath, "unable to read");
-       if (*cp != '/')
-           m_putenv ("MH", defpath);
-    } else {
-       defpath = concat (mypath, "/", mh_profile, NULL);
+       /*
+       ** Find and read user's profile.  Check for the existence of
+       ** a non-empty MMHP environment variable first.  Look for the
+       ** profile in the mmh directory otherwise.
+       */
+       if ((cp = getenv("MMHP")) && *cp) {
+               if (*cp == '/') {
+                       defpath = getcpy(cp);
+               } else {
+                       defpath = concat(mmhpath, "/", cp, NULL);
+               }
+               if (stat(defpath, &st) != -1 && (st.st_mode & S_IFREG) == 0) {
+                       adios(NULL, "Your profile `%s', specified by the MMHP environment variable, is not a normal file", cp);
+               }
+               if ((ib = fopen(defpath, "r")) == (FILE *)0) {
+                       adios(NULL, "Unable to read your profile `%s', specified by the MMHP environment variable", defpath);
+               }
+       } else {
+               defpath = concat(mmhpath, "/", profile, NULL);
+               if ((ib = fopen(defpath, "r")) == (FILE *)0) {
+                       adios(NULL, "No profile found. Please create `%s' first.", defpath);
+               }
+               cp = profile;
+       }
+       readconfig(&m_defs, ib, cp, 0);
+       fclose(ib);
 
-       if ((ib = fopen (defpath, "r")) == NULL) {
-           switch (pid = vfork ()) {
-               case -1:
-                   adios ("fork", "unable to");
+       /*
+       ** Find the user's mail storage directory, which is specified by
+       ** the `Path' profile component.  Convert a relative path name
+       ** to an absolute one rooted in the home directory.
+       */
+       if ((cp = context_find("path")) == NULL) {
+               adios(NULL, "Your profile `%s' does not contain the required path entry.", defpath);
+       }
+       if (!*cp) {
+               adios(NULL, "The Path entry of your profile `%s' must be non-empty.", defpath);
+       }
+       if (*cp == '/') {
+               nd = cp;
+       } else {
+               snprintf(nd = buf, sizeof buf, "%s/%s", mypath, cp);
+       }
+       if (stat(nd, &st) == -1) {
+               if (errno != ENOENT) {
+                       adios(nd, "error opening");
+               }
+               cp = concat("Your mail storage directory `", nd, "' doesn't exist; Create it? ", NULL);
+               if (!getanswer(cp)) {
+                       adios(NULL, "Unable to access the mail storage directory `%s'", nd);
+               }
+               free(cp);
+               if (!makedir(nd)) {
+                       adios(nd, "unable to create");
+               }
+       } else if ((st.st_mode & S_IFDIR) == 0) {
+               adios(NULL, "Your mail storage `%s' is not a directory", nd);
+       }
+       /*
+       ** Create the default folder (inbox)
+       */
+       cp = toabsdir(defaultfolder);
+       if (stat(cp, &st) == -1) {
+               if (!makedir(cp)) {
+                       adios(cp, "Unable to create the default folder");
+               }
+       } else if ((st.st_mode & S_IFDIR) == 0) {
+               adios(NULL, "The default folder `%s' is not a directory", cp);
+       }
 
-               case 0:
-                   setgid (getgid ());
-                   setuid (getuid ());
+       /*
+       ** Open and read user's context file.  The name of the context
+       ** file comes from the profile unless overridden by the MMHC
+       ** environment variable.
+       */
+       if (!(cp = getenv("MMHC")) || !*cp) {
+               if (!(cp = context_find("context")) || !*cp) {
+                       cp = context;
+               }
+       }
 
-                   execlp (installproc, "install-mh", "-auto", NULL);
-                   fprintf (stderr, "unable to exec ");
-                   perror (installproc);
-                   _exit (-1);
+       /*
+       ** context is NULL if context_foil() was called to disable use
+       ** of context. We also support users setting explicitly setting
+       ** MMHC to /dev/null.  (If this wasn't special-cased then the
+       ** locking would be liable to fail.)
+       */
+       if (!context || (strcmp(cp, "/dev/null") == 0)) {
+               ctxpath = NULL;
+               return;
+       }
 
-               default:
-                   if (pidwait (pid, 0)
-                           || (ib = fopen (defpath, "r")) == NULL)
-                       adios (NULL, "[install-mh aborted]");
-           }
+       if (*cp == '/') {
+               ctxpath = getcpy(cp);
+       } else {
+               ctxpath = concat(mmhpath, "/", cp, NULL);
+       }
+       if ((ib = lkfopen(ctxpath, "r"))) {
+               readconfig((struct node **) 0, ib, cp, 1);
+               lkfclose(ib, ctxpath);
        }
-    }
-    readconfig (&m_defs, ib, mh_profile, 0);
-    fclose (ib);
 
-    /*
-     * Find user's nmh directory
-     */
-    if ((pp = context_find ("path")) && *pp != '\0') {
-       if (*pp != '/')
-           snprintf (buf, sizeof(buf), "%s/%s", mypath, pp);
-       else
-           strncpy (buf, pp, sizeof(buf));
-       if (stat(buf, &st) == -1) {
-           if (errno != ENOENT)
-               adios (buf, "error opening");
-           cp = concat ("Your MH-directory \"", buf,
-               "\" doesn't exist; Create it? ", NULL);
-           if (!getanswer(cp))
-               adios (NULL, "unable to access MH-directory \"%s\"", buf);
-           free (cp);
-           if (!makedir (buf))
-               adios (NULL, "unable to create", buf);
+       /* Set editor */
+       if (!(cp = getenv("MMHEDITOR")) || !*cp) {
+               if (!(cp = context_find("editor")) || !*cp) {
+                       if (!(cp = getenv("VISUAL")) || !*cp) {
+                               if (!(cp = getenv("EDITOR")) || !*cp) {
+                                       cp = defaulteditor;
+                               }
+                       }
+               }
        }
-    }
+       defaulteditor = cp;
 
-    /*
-     * open and read user's context file
-     */
-    if (!(cp = getenv ("MHCONTEXT")) || *cp == '\0')
-       cp = context;
-    ctxpath = getcpy (m_maildir (cp));
-    if ((ib = fopen (ctxpath, "r"))) {
-       readconfig ((struct node **) 0, ib, cp, 1);
-       fclose (ib);
-    }
+       /* Set pager */
+       if (!(cp = getenv("MMHPAGER")) || !*cp) {
+               if (!(cp = context_find("pager")) || !*cp) {
+                       if (!(cp = getenv("PAGER")) || !*cp) {
+                               cp = defaultpager;
+                       }
+               }
+       }
+       defaultpager = cp;
 }