corrected termination condition for fill character introduced in last version
[mmh] / sbr / context_read.c
1 /*
2  * context_read.c -- find and read profile and context files
3  *
4  * $Id$
5  *
6  * This code is Copyright (c) 2002, by the authors of nmh.  See the
7  * COPYRIGHT file in the root directory of the nmh distribution for
8  * complete copyright information.
9  *
10  *      This function must be called early on in any nmh utility, and
11  *      may only be called once.  It does the following:
12  *
13  *       o  Sets the global variable "mypath" to the home directory path.
14  *
15  *       o  Sets the global variable "defpath" to the absolute path of
16  *          the profile file.
17  *
18  *       o  Reads in the profile file.  Bails out if it can't.
19  *
20  *       o  Makes sure that the mail directory exists, prompting for
21  *          creation if it doesn't.
22  *
23  *       o  Reads the context file either as set by the MHCONTEXT
24  *          environment variable or by the profile.
25  */
26
27 #include <h/mh.h>                               /* mh internals */
28 #include <errno.h>                              /* system call errors */
29 #include <pwd.h>                                /* structure for getpwuid() results */
30
31 extern  int     errno;                          /* system call error number */
32
33 void
34 context_read (void)
35 {
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     register    struct  passwd  *pw;            /* getpwuid() results */
41     register    FILE            *ib;            /* profile and context file pointer */
42
43     /*
44      *  Find user's home directory.  Try the HOME environment variable first,
45      *  the home directory field in the password file if that's not found.
46      */
47
48     if ((mypath = getenv("HOME")) == (char *)0) {
49         if ((pw = getpwuid(getuid())) == (struct passwd *)0 || *pw->pw_dir == '\0')
50             adios(NULL, "cannot determine your home directory");
51         else
52             mypath = pw->pw_dir;
53     }
54
55     /*
56      *  Find and read user's profile.  Check for the existence of an MH environment
57      *  variable first with non-empty contents.  Convert any relative path name
58      *  found there to an absolute one.  Look for the profile in the user's home
59      *  directory if the MH environment variable isn't set.
60      */
61
62     if ((cp = getenv("MH")) && *cp != '\0') {
63         defpath = path(cp, TFILE);
64
65         if (stat(defpath, &st) != -1 && (st.st_mode & S_IFREG) == 0)
66                 adios((char *)0, "`%s' specified by your MH environment variable is not a normal file", cp);
67
68         if ((ib = fopen(defpath, "r")) == (FILE *)0)
69             adios((char *)0, "unable to read the `%s' profile specified by your MH environment variable", defpath);
70     }
71     else {
72         defpath = concat(mypath, "/", mh_profile, NULL);
73
74         if ((ib = fopen(defpath, "r")) == (FILE *)0)
75             adios((char *)0, "Doesn't look like nmh is installed.  Run install-mh to do so.");
76
77         cp = mh_profile;
78     }
79
80     readconfig (&m_defs, ib, cp, 0);
81     fclose (ib);
82
83     /*
84      *  Find the user's nmh directory, which is specified by the "path" profile component.
85      *  Convert a relative path name to an absolute one rooted in the home directory.
86      */
87
88     if ((cp = context_find ("path")) == (char *)0)
89         adios(NULL, "Your %s file does not contain a path entry.", defpath);
90
91     if (*cp == '\0')
92         adios(NULL, "Your `%s' profile file does not contain a valid path entry.", defpath);
93
94     if (*cp != '/')
95         (void)snprintf (nd = buf, sizeof(buf), "%s/%s", mypath, cp);
96     else
97         nd = cp;
98
99     if (stat(nd, &st) == -1) {
100         if (errno != ENOENT)
101             adios (nd, "error opening");
102
103         cp = concat ("Your MH-directory \"", nd, "\" doesn't exist; Create it? ", NULL);
104
105         if (!getanswer(cp))
106             adios (NULL, "unable to access MH-directory \"%s\"", nd);
107
108         free (cp);
109
110         if (!makedir (nd))
111             adios (NULL, "unable to create", nd);
112     }
113
114     else if ((st.st_mode & S_IFDIR) == 0)
115         adios ((char *)0, "`%s' is not a directory", nd);
116
117     /*
118      *  Open and read user's context file.  The name of the context file comes from the
119      *  profile unless overridden by the MHCONTEXT environment variable.
120      */
121
122     if ((cp = getenv ("MHCONTEXT")) == (char *)0 || *cp == '\0')
123         cp = context;
124
125     ctxpath = getcpy (m_maildir (cp));
126
127     if ((ib = lkfopen (ctxpath, "r"))) {
128         readconfig ((struct node **) 0, ib, cp, 1);
129         lkfclose (ib, ctxpath);
130     }
131
132     return;
133 }