]> diplodocus.org Git - nmh/blob - sbr/context_read.c
b207201d5f82c2cc3945020674e587e8b977793e
[nmh] / sbr / context_read.c
1 /* context_read.c -- find and read profile and context files
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 *
7 * This function must be called early on in any nmh utility, and
8 * may only be called once. It does the following:
9 *
10 * o Sets the global variable "mypath" to the home directory path.
11 *
12 * o Sets the global variable "defpath" to the absolute path of
13 * the profile file.
14 *
15 * o Reads in the profile file. Bails out if it can't.
16 *
17 * o Makes sure that the mail directory exists, prompting for
18 * creation if it doesn't.
19 *
20 * o Reads the context file either as set by the MHCONTEXT
21 * environment variable or by the profile.
22 */
23
24 #include "h/mh.h"
25 #include "concat.h"
26 #include "context_read.h"
27 #include "context_find.h"
28 #include "readconfig.h"
29 #include "path.h"
30 #include "error.h"
31 #include "lock_file.h"
32 #include "m_maildir.h"
33 #include "makedir.h"
34 #include <pwd.h>
35 #include "h/utils.h"
36
37 void
38 context_read (void)
39 {
40 char buf[BUFSIZ]; /* path name buffer */
41 char *cp; /* miscellaneous pointer */
42 char *nd; /* nmh directory pointer */
43 struct stat st; /* stat() results */
44 struct passwd *pw; /* getpwuid() results */
45 FILE *ib; /* profile and context file pointer */
46 int failed_to_lock = 0;
47
48 /*
49 * If this routine _is_ called again (despite the warnings in the
50 * comments above), return immediately.
51 */
52 if ( m_defs != 0 )
53 return;
54
55 /*
56 * Find user's home directory. Try the HOME environment variable first,
57 * the home directory field in the password file if that's not found.
58 */
59
60 if ((mypath = getenv("HOME")) == NULL) {
61 if ((pw = getpwuid(getuid())) == NULL || *pw->pw_dir == '\0')
62 die("cannot determine your home directory");
63 mypath = pw->pw_dir;
64 }
65
66 /*
67 * Find and read user's profile. Check for the existence of an MH environment
68 * variable first with non-empty contents. Convert any relative path name
69 * found there to an absolute one. Look for the profile in the user's home
70 * directory if the MH environment variable isn't set.
71 */
72
73 if ((cp = getenv("MH")) && *cp != '\0') {
74 defpath = path(cp, TFILE);
75
76 /* defpath is an absolute path; make sure that always MH is, too. */
77 setenv("MH", defpath, 1);
78 if (stat(defpath, &st) != -1 && (st.st_mode & S_IFREG) == 0)
79 die("`%s' specified by your MH environment variable is not a normal file", cp);
80
81 if ((ib = fopen(defpath, "r")) == NULL)
82 die("unable to read the `%s' profile specified by your MH environment variable", defpath);
83 }
84 else {
85 defpath = concat(mypath, "/", mh_profile, NULL);
86
87 if ((ib = fopen(defpath, "r")) == NULL)
88 die("Doesn't look like nmh is installed. Run install-mh to do so.");
89
90 cp = mh_profile;
91 }
92
93 readconfig (&m_defs, ib, cp, 0);
94 fclose (ib);
95
96 /*
97 * Find the user's nmh directory, which is specified by the "path" profile component.
98 * Convert a relative path name to an absolute one rooted in the home directory.
99 */
100
101 if ((cp = context_find ("path")) == NULL)
102 die("Your %s file does not contain a path entry.", defpath);
103
104 if (*cp == '\0')
105 die("Your `%s' profile file does not contain a valid path entry.", defpath);
106
107 if (*cp != '/')
108 (void)snprintf (nd = buf, sizeof(buf), "%s/%s", mypath, cp);
109 else
110 nd = cp;
111
112 if (stat(nd, &st) == -1) {
113 if (errno != ENOENT)
114 adios (nd, "error opening");
115
116 cp = concat ("Your MH-directory \"", nd, "\" doesn't exist; Create it? ", NULL);
117
118 if (!read_yes_or_no_if_tty(cp))
119 die("unable to access MH-directory \"%s\"", nd);
120
121 free (cp);
122
123 if (!makedir (nd))
124 die("unable to create %s", nd);
125 }
126
127 else if ((st.st_mode & S_IFDIR) == 0)
128 die("`%s' is not a directory", nd);
129
130 /*
131 * Open and read user's context file. The name of the context file comes from the
132 * profile unless overridden by the MHCONTEXT environment variable.
133 */
134
135 if ((cp = getenv ("MHCONTEXT")) == NULL || *cp == '\0')
136 cp = context;
137
138 /* context is NULL if context_foil() was called to disable use of context
139 * We also support users setting explicitly setting MHCONTEXT to /dev/null.
140 * (if this wasn't special-cased then the locking would be liable to fail)
141 */
142 if (!cp || (strcmp(cp,"/dev/null") == 0)) {
143 ctxpath = NULL;
144 return;
145 }
146
147 ctxpath = mh_xstrdup(m_maildir(cp));
148
149 if ((ib = lkfopendata (ctxpath, "r", &failed_to_lock))) {
150 readconfig(NULL, ib, cp, 1);
151 lkfclosedata (ib, ctxpath);
152 }
153 }