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