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