2 ** context_read.c -- find and read profile and context files
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
8 ** This function must be called early on in any nmh utility, and
9 ** may only be called once. It does the following:
11 ** o Sets the global variable "mypath" to the home directory path.
13 ** o Sets the global variable "defpath" to the absolute path of
16 ** o Reads in the profile file. Bails out if it can't.
18 ** o Makes sure that the mail directory exists, prompting for
19 ** creation if it doesn't.
21 ** o Reads the context file either as set by the MHCONTEXT
22 ** environment variable or by the profile.
25 #include <h/mh.h> /* mh internals */
26 #include <errno.h> /* system call errors */
27 #include <pwd.h> /* structure for getpwuid() results */
32 char buf[BUFSIZ]; /* path name buffer */
33 char *cp; /* miscellaneous pointer */
34 char *nd; /* nmh directory pointer */
35 struct stat st; /* stat() results */
36 register struct passwd *pw; /* getpwuid() results */
37 register FILE *ib; /* profile and context file pointer */
40 ** If this routine _is_ called again (despite the wanings in the
41 ** comments above), return immediately.
47 ** Find user's home directory. Try the HOME environment variable first,
48 ** the home directory field in the password file if that's not found.
50 if ((mypath = getenv("HOME")) == (char *)0) {
51 if ((pw = getpwuid(getuid())) == (struct passwd *)0 ||
53 adios(NULL, "cannot determine your home directory");
59 ** Find and read user's profile. Check for the existence of
60 ** an MH environment variable first with non-empty contents.
61 ** Convert any relative path name found there to an absolute one.
62 ** Look for the profile in the user's home directory if the MH
63 ** environment variable isn't set.
66 if ((cp = getenv("MH")) && *cp != '\0') {
67 defpath = path(cp, TFILE);
69 if (stat(defpath, &st) != -1 && (st.st_mode & S_IFREG) == 0)
70 adios((char *)0, "`%s' specified by your MH environment variable is not a normal file", cp);
72 if ((ib = fopen(defpath, "r")) == (FILE *)0)
73 adios((char *)0, "unable to read the `%s' profile specified by your MH environment variable", defpath);
75 defpath = concat(mypath, "/", mh_profile, NULL);
77 if ((ib = fopen(defpath, "r")) == (FILE *)0)
78 adios((char *)0, "Doesn't look like nmh is installed. Run install-mh to do so.");
83 readconfig(&m_defs, ib, cp, 0);
87 ** Find the user's nmh directory, which is specified by the "path"
88 ** profile component. Convert a relative path name to an absolute
89 ** one rooted in the home directory.
92 if ((cp = context_find("path")) == (char *)0)
93 adios(NULL, "Your %s file does not contain a path entry.",
97 adios(NULL, "Your `%s' profile file does not contain a valid path entry.", defpath);
100 (void)snprintf(nd = buf, sizeof(buf), "%s/%s", mypath, cp);
104 if (stat(nd, &st) == -1) {
106 adios(nd, "error opening");
107 cp = concat("Your MH-directory \"", nd, "\" doesn't exist; Create it? ", NULL);
109 adios(NULL, "unable to access MH-directory \"%s\"",
113 adios(NULL, "unable to create %s", nd);
114 } else if ((st.st_mode & S_IFDIR) == 0)
115 adios((char *)0, "`%s' is not a directory", nd);
118 ** Open and read user's context file. The name of the context
119 ** file comes from the profile unless overridden by the MHCONTEXT
120 ** environment variable.
123 if ((cp = getenv("MHCONTEXT")) == (char *)0 || *cp == '\0')
127 ** context is NULL if context_foil() was called to disable use
128 ** of context We also support users setting explicitly setting
129 ** MHCONTEXT to /dev/null. (if this wasn't specialcased then the
130 ** locking would be liable to fail)
132 if (!cp || (strcmp(cp,"/dev/null") == 0)) {
137 ctxpath = getcpy(m_maildir(cp));
139 if ((ib = lkfopen(ctxpath, "r"))) {
140 readconfig((struct node **) 0, ib, cp, 1);
141 lkfclose(ib, ctxpath);