]>
diplodocus.org Git - nmh/blob - sbr/context_read.c
acee3949bdb3dc4d94a84754d9e6db44b0bfafed
1 /* context_read.c -- find and read profile and context files
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.
7 * This function must be called early on in any nmh utility, and
8 * may only be called once. It does the following:
10 * o Sets the global variable "mypath" to the home directory path.
12 * o Sets the global variable "defpath" to the absolute path of
15 * o Reads in the profile file. Bails out if it can't.
17 * o Makes sure that the mail directory exists, prompting for
18 * creation if it doesn't.
20 * o Reads the context file either as set by the MHCONTEXT
21 * environment variable or by the profile.
27 #include "lock_file.h"
28 #include "m_maildir.h"
36 char buf
[BUFSIZ
]; /* path name buffer */
37 char *cp
; /* miscellaneous pointer */
38 char *nd
; /* nmh directory pointer */
39 struct stat st
; /* stat() results */
40 struct passwd
*pw
; /* getpwuid() results */
41 FILE *ib
; /* profile and context file pointer */
42 int failed_to_lock
= 0;
45 * If this routine _is_ called again (despite the warnings in the
46 * comments above), return immediately.
52 * Find user's home directory. Try the HOME environment variable first,
53 * the home directory field in the password file if that's not found.
56 if ((mypath
= getenv("HOME")) == NULL
) {
57 if ((pw
= getpwuid(getuid())) == NULL
|| *pw
->pw_dir
== '\0')
58 die("cannot determine your home directory");
63 * Find and read user's profile. Check for the existence of an MH environment
64 * variable first with non-empty contents. Convert any relative path name
65 * found there to an absolute one. Look for the profile in the user's home
66 * directory if the MH environment variable isn't set.
69 if ((cp
= getenv("MH")) && *cp
!= '\0') {
70 defpath
= path(cp
, TFILE
);
72 /* defpath is an absolute path; make sure that always MH is, too. */
73 setenv("MH", defpath
, 1);
74 if (stat(defpath
, &st
) != -1 && (st
.st_mode
& S_IFREG
) == 0)
75 die("`%s' specified by your MH environment variable is not a normal file", cp
);
77 if ((ib
= fopen(defpath
, "r")) == NULL
)
78 die("unable to read the `%s' profile specified by your MH environment variable", defpath
);
81 defpath
= concat(mypath
, "/", mh_profile
, NULL
);
83 if ((ib
= fopen(defpath
, "r")) == NULL
)
84 die("Doesn't look like nmh is installed. Run install-mh to do so.");
89 readconfig (&m_defs
, ib
, cp
, 0);
93 * Find the user's nmh directory, which is specified by the "path" profile component.
94 * Convert a relative path name to an absolute one rooted in the home directory.
97 if ((cp
= context_find ("path")) == NULL
)
98 die("Your %s file does not contain a path entry.", defpath
);
101 die("Your `%s' profile file does not contain a valid path entry.", defpath
);
104 (void)snprintf (nd
= buf
, sizeof(buf
), "%s/%s", mypath
, cp
);
108 if (stat(nd
, &st
) == -1) {
110 adios (nd
, "error opening");
112 cp
= concat ("Your MH-directory \"", nd
, "\" doesn't exist; Create it? ", NULL
);
114 if (!read_yes_or_no_if_tty(cp
))
115 die("unable to access MH-directory \"%s\"", nd
);
120 die("unable to create %s", nd
);
123 else if ((st
.st_mode
& S_IFDIR
) == 0)
124 die("`%s' is not a directory", nd
);
127 * Open and read user's context file. The name of the context file comes from the
128 * profile unless overridden by the MHCONTEXT environment variable.
131 if ((cp
= getenv ("MHCONTEXT")) == NULL
|| *cp
== '\0')
134 /* context is NULL if context_foil() was called to disable use of context
135 * We also support users setting explicitly setting MHCONTEXT to /dev/null.
136 * (if this wasn't special-cased then the locking would be liable to fail)
138 if (!cp
|| (strcmp(cp
,"/dev/null") == 0)) {
143 ctxpath
= mh_xstrdup(m_maildir(cp
));
145 if ((ib
= lkfopendata (ctxpath
, "r", &failed_to_lock
))) {
146 readconfig(NULL
, ib
, cp
, 1);
147 lkfclosedata (ib
, ctxpath
);