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 variables to absolute paths:
12 ** - "mypath": home directory (HOME)
13 ** - "mmhpath": mmh directory (MMH)
14 ** - "defpath": profile file (MMHP)
15 ** - "ctxpath": context file (MMHC)
17 ** o Reads in the profile file. Bails out if it can't.
19 ** o Makes sure that the mail directory exists, prompting for
20 ** creation if it doesn't.
22 ** o Reads the context file.
24 ** You might need to adjust uip/mmh.sh if you make changes here.
27 #include <h/mh.h> /* mh internals */
28 #include <errno.h> /* system call errors */
29 #include <pwd.h> /* structure for getpwuid() results */
34 char buf[BUFSIZ]; /* path name buffer */
35 char *cp; /* miscellaneous pointer */
36 char *nd; /* nmh directory pointer */
37 struct stat st; /* stat() results */
38 register struct passwd *pw; /* getpwuid() results */
39 register FILE *ib; /* profile and context file pointer */
42 ** If this routine _is_ called again (despite the wanings in the
43 ** comments above), return immediately.
50 ** Find user's home directory. Try the HOME environment variable first,
51 ** the home directory field in the password file if that's not found.
53 if (!(mypath = getenv("HOME"))) {
54 if (!(pw = getpwuid(getuid())) || !*pw->pw_dir) {
55 adios(NULL, "cannot determine your home directory");
63 if ((cp = getenv("MMH")) && *cp) {
64 mmhpath = getcpy(expanddir(cp)); /* rel to cwd */
65 if (stat(mmhpath, &st) != -1 && (st.st_mode & S_IFDIR) == 0) {
66 adios(NULL, "`%s' specified by your MMH environment variable is not a directory", cp);
69 mmhpath = concat(mypath, "/", mmhdir, NULL);
70 if (stat(mmhpath, &st) == -1 || (st.st_mode & S_IFDIR) == 0) {
71 adios(NULL, "Doesn't look like mmh is set up for your account. Run `install-mh' to do so.");
76 ** Find and read user's profile. Check for the existence of
77 ** a non-empty MMHP environment variable first. Look for the
78 ** profile in the mmh directory otherwise.
80 if ((cp = getenv("MMHP")) && *cp) {
84 defpath = concat(mmhpath, "/", cp, NULL);
86 if (stat(defpath, &st) != -1 && (st.st_mode & S_IFREG) == 0) {
87 adios(NULL, "Your profile `%s', specified by the MMHP environment variable, is not a normal file", cp);
89 if ((ib = fopen(defpath, "r")) == (FILE *)0) {
90 adios(NULL, "Unable to read your profile `%s', specified by the MMHP environment variable", defpath);
93 defpath = concat(mmhpath, "/", profile, NULL);
94 if ((ib = fopen(defpath, "r")) == (FILE *)0) {
95 adios(NULL, "No profile found. Please create `%s' first.", defpath);
99 readconfig(&m_defs, ib, cp, 0);
103 ** Find the user's mail storage directory, which is specified by
104 ** the `Path' profile component. Convert a relative path name
105 ** to an absolute one rooted in the home directory.
107 if ((cp = context_find("path")) == NULL) {
108 adios(NULL, "Your profile `%s' does not contain the required path entry.", defpath);
111 adios(NULL, "The Path entry of your profile `%s' must be non-empty.", defpath);
116 snprintf(nd = buf, sizeof buf, "%s/%s", mypath, cp);
118 if (stat(nd, &st) == -1) {
119 if (errno != ENOENT) {
120 adios(nd, "error opening");
122 cp = concat("Your mail storage directory `", nd, "' doesn't exist; Create it? ", NULL);
123 if (!getanswer(cp)) {
124 adios(NULL, "Unable to access the mail storage directory `%s'", nd);
128 adios(nd, "unable to create");
130 } else if ((st.st_mode & S_IFDIR) == 0) {
131 adios(NULL, "Your mail storage `%s' is not a directory", nd);
134 ** Create the default folder (inbox)
136 cp = toabsdir(defaultfolder);
137 if (stat(cp, &st) == -1) {
139 adios(cp, "Unable to create the default folder");
141 } else if ((st.st_mode & S_IFDIR) == 0) {
142 adios(NULL, "The default folder `%s' is not a directory", cp);
146 ** Open and read user's context file. The name of the context
147 ** file comes from the profile unless overridden by the MMHC
148 ** environment variable.
150 if (!(cp = getenv("MMHC")) || !*cp) {
151 if (!(cp = context_find("context")) || !*cp) {
157 ** context is NULL if context_foil() was called to disable use
158 ** of context. We also support users setting explicitly setting
159 ** MMHC to /dev/null. (If this wasn't special-cased then the
160 ** locking would be liable to fail.)
162 if (!context || (strcmp(cp, "/dev/null") == 0)) {
168 ctxpath = getcpy(cp);
170 ctxpath = concat(mmhpath, "/", cp, NULL);
172 if ((ib = lkfopen(ctxpath, "r"))) {
173 readconfig((struct node **) 0, ib, cp, 1);
174 lkfclose(ib, ctxpath);