]> diplodocus.org Git - nmh/blob - uip/install-mh.c
Just reworded the bit about '%s' being safe not to quote (it's only safe not to
[nmh] / uip / install-mh.c
1
2 /*
3 * install-mh.c -- initialize the nmh environment of a new user
4 *
5 * $Id$
6 */
7
8 #include <h/mh.h>
9 #include <pwd.h>
10
11 static struct swit switches[] = {
12 #define AUTOSW 0
13 { "auto", 0 },
14 #define VERSIONSW 1
15 { "version", 0 },
16 #define HELPSW 2
17 { "help", 0 },
18 { NULL, 0 }
19 };
20
21 static char *message[] = {
22 "Prior to using nmh, it is necessary to have a file in your login",
23 "directory (%s) named %s which contains information",
24 "to direct certain nmh operations. The only item which is required",
25 "is the path to use for all nmh folder operations. The suggested nmh",
26 "path for you is %s/Mail...",
27 NULL
28 };
29
30 /*
31 * static prototypes
32 */
33 static char *geta(void);
34
35
36 int
37 main (int argc, char **argv)
38 {
39 int i, autof = 0;
40 char *cp, *path, buf[BUFSIZ];
41 char *dp, **arguments, **argp;
42 struct node *np;
43 struct passwd *pw;
44 struct stat st;
45 FILE *in, *out;
46
47 #ifdef LOCALE
48 setlocale(LC_ALL, "");
49 #endif
50 invo_name = r1bindex (argv[0], '/');
51 arguments = getarguments (invo_name, argc, argv, 0);
52 argp = arguments;
53
54 while ((dp = *argp++)) {
55 if (*dp == '-') {
56 switch (smatch (++dp, switches)) {
57 case AMBIGSW:
58 ambigsw (dp, switches);
59 done (1);
60 case UNKWNSW:
61 adios (NULL, "-%s unknown\n", dp);
62
63 case HELPSW:
64 snprintf (buf, sizeof(buf), "%s [switches]", invo_name);
65 print_help (buf, switches, 0);
66 done (1);
67 case VERSIONSW:
68 print_version(invo_name);
69 done (1);
70
71 case AUTOSW:
72 autof++;
73 continue;
74 }
75 } else {
76 adios (NULL, "%s is invalid argument", dp);
77 }
78 }
79
80 /* straight from context_read ... */
81 if (mypath == NULL) {
82 if ((mypath = getenv ("HOME"))) {
83 mypath = getcpy (mypath);
84 } else {
85 if ((pw = getpwuid (getuid ())) == NULL
86 || pw->pw_dir == NULL
87 || *pw->pw_dir == 0)
88 adios (NULL, "no HOME envariable");
89 else
90 mypath = getcpy (pw->pw_dir);
91 }
92 if ((cp = mypath + strlen (mypath) - 1) > mypath && *cp == '/')
93 *cp = 0;
94 }
95 defpath = concat (mypath, "/", mh_profile, NULL);
96
97 if (stat (defpath, &st) != NOTOK) {
98 if (autof)
99 adios (NULL, "invocation error");
100 else
101 adios (NULL,
102 "You already have an nmh profile, use an editor to modify it");
103 }
104
105 if (!autof && gans ("Do you want help? ", anoyes)) {
106 putchar ('\n');
107 for (i = 0; message[i]; i++) {
108 printf (message[i], mypath, mh_profile);
109 putchar ('\n');
110 }
111 putchar ('\n');
112 }
113
114 cp = concat (mypath, "/", "Mail", NULL);
115 if (stat (cp, &st) != NOTOK) {
116 if (S_ISDIR(st.st_mode)) {
117 cp = concat ("You already have the standard nmh directory \"",
118 cp, "\".\nDo you want to use it for nmh? ", NULL);
119 if (gans (cp, anoyes))
120 path = "Mail";
121 else
122 goto query;
123 } else {
124 goto query;
125 }
126 } else {
127 if (autof)
128 printf ("I'm going to create the standard nmh path for you.\n");
129 else
130 cp = concat ("Do you want the standard nmh path \"",
131 mypath, "/", "Mail\"? ", NULL);
132 if (autof || gans (cp, anoyes))
133 path = "Mail";
134 else {
135 query:
136 if (gans ("Do you want a path below your login directory? ",
137 anoyes)) {
138 printf ("What is the path? %s/", mypath);
139 path = geta ();
140 } else {
141 printf ("What is the whole path? /");
142 path = concat ("/", geta (), NULL);
143 }
144 }
145 }
146
147 chdir (mypath);
148 if (chdir (path) == NOTOK) {
149 cp = concat ("\"", path, "\" doesn't exist; Create it? ", NULL);
150 if (autof || gans (cp, anoyes))
151 if (makedir (path) == 0)
152 adios (NULL, "unable to create %s", path);
153 } else {
154 printf ("[Using existing directory]\n");
155 }
156
157 /*
158 * Add some initial elements to the profile/context list
159 */
160 if (!(m_defs = (struct node *) malloc (sizeof *np)))
161 adios (NULL, "unable to allocate profile storage");
162 np = m_defs;
163 np->n_name = getcpy ("Path");
164 np->n_field = getcpy (path);
165 np->n_context = 0;
166 np->n_next = NULL;
167
168 /*
169 * If there is a default profile file in the
170 * nmh `etc' directory, then read it also.
171 */
172 if ((in = fopen (mh_defaults, "r"))) {
173 readconfig (&np->n_next, in, mh_defaults, 0);
174 fclose (in);
175 }
176
177 ctxpath = getcpy (m_maildir (context = "context"));
178
179 /* Initialize current folder to default */
180 context_replace (pfolder, defaultfolder);
181 context_save ();
182
183 /*
184 * Now write out the initial .mh_profile
185 */
186 if ((out = fopen (defpath, "w")) == NULL)
187 adios (defpath, "unable to write");
188 for (np = m_defs; np; np = np->n_next) {
189 if (!np->n_context)
190 fprintf (out, "%s: %s\n", np->n_name, np->n_field);
191 }
192 fclose (out);
193 return done (0);
194 }
195
196
197 static char *
198 geta (void)
199 {
200 char *cp;
201 static char line[BUFSIZ];
202
203 fflush(stdout);
204 if (fgets(line, sizeof(line), stdin) == NULL)
205 done (1);
206 if ((cp = strchr(line, '\n')))
207 *cp = 0;
208 return line;
209 }