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