]> diplodocus.org Git - nmh/blob - uip/mhbuild.c
Apply David Levine's fix from whomfile() to sendfile() as well.
[nmh] / uip / mhbuild.c
1
2 /*
3 * mhbuild.c -- expand/translate MIME composition files
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <fcntl.h>
12 #include <h/signals.h>
13 #include <h/md5.h>
14 #include <errno.h>
15 #include <signal.h>
16 #include <h/mts.h>
17 #include <h/tws.h>
18 #include <h/mime.h>
19 #include <h/mhparse.h>
20 #include <h/mhcachesbr.h>
21 #include <h/utils.h>
22
23 #define MHBUILD_SWITCHES \
24 X("check", 0, CHECKSW) \
25 X("nocheck", 0, NCHECKSW) \
26 X("directives", 0, DIRECTIVES) \
27 X("nodirectives", 0, NDIRECTIVES) \
28 X("headers", 0, HEADSW) \
29 X("noheaders", 0, NHEADSW) \
30 X("list", 0, LISTSW) \
31 X("nolist", 0, NLISTSW) \
32 X("realsize", 0, SIZESW) \
33 X("norealsize", 0, NSIZESW) \
34 X("rfc934mode", 0, RFC934SW) \
35 X("norfc934mode", 0, NRFC934SW) \
36 X("verbose", 0, VERBSW) \
37 X("noverbose", 0, NVERBSW) \
38 X("rcache policy", 0, RCACHESW) \
39 X("wcache policy", 0, WCACHESW) \
40 X("contentid", 0, CONTENTIDSW) \
41 X("nocontentid", 0, NCONTENTIDSW) \
42 X("version", 0, VERSIONSW) \
43 X("help", 0, HELPSW) \
44 X("debug", -5, DEBUGSW) \
45
46 #define X(sw, minchars, id) id,
47 DEFINE_SWITCH_ENUM(MHBUILD);
48 #undef X
49
50 #define X(sw, minchars, id) { sw, minchars, id },
51 DEFINE_SWITCH_ARRAY(MHBUILD, switches);
52 #undef X
53
54
55 /* mhbuildsbr.c */
56 extern char *tmp; /* directory to place temp files */
57
58 /* mhcachesbr.c */
59 extern int rcachesw;
60 extern int wcachesw;
61 extern char *cache_public;
62 extern char *cache_private;
63
64 int debugsw = 0;
65 int verbosw = 0;
66
67 int listsw = 0;
68 int rfc934sw = 0;
69 int contentidsw = 1;
70
71 /*
72 * Temporary files
73 */
74 static char infile[BUFSIZ];
75 static int unlink_infile = 0;
76
77 static char outfile[BUFSIZ];
78 static int unlink_outfile = 0;
79
80 static void unlink_done (int) NORETURN;
81
82 /* mhbuildsbr.c */
83 CT build_mime (char *, int);
84 int output_message (CT, char *);
85 int output_message_fp (CT, FILE *, char*);
86
87 /* mhlistsbr.c */
88 int list_all_messages (CT *, int, int, int, int);
89
90
91 int
92 main (int argc, char **argv)
93 {
94 int sizesw = 1, headsw = 1, directives = 1;
95 int *icachesw;
96 char *cp, buf[BUFSIZ];
97 char buffer[BUFSIZ], *compfile = NULL;
98 char **argp, **arguments;
99 CT ct, cts[2];
100 FILE *fp = NULL;
101 FILE *fp_out = NULL;
102
103 done=unlink_done;
104
105 #ifdef LOCALE
106 setlocale(LC_ALL, "");
107 #endif
108 invo_name = r1bindex (argv[0], '/');
109
110 /* read user profile/context */
111 context_read();
112
113 arguments = getarguments (invo_name, argc, argv, 1);
114 argp = arguments;
115
116 while ((cp = *argp++)) {
117 if (cp[0] == '-' && cp[1] == '\0') {
118 if (compfile)
119 adios (NULL, "cannot specify both standard input and a file");
120 else
121 compfile = cp;
122 listsw = 0; /* turn off -list if using standard in/out */
123 verbosw = 0; /* turn off -verbose listings */
124 break;
125 }
126 if (*cp == '-') {
127 switch (smatch (++cp, switches)) {
128 case AMBIGSW:
129 ambigsw (cp, switches);
130 done (1);
131 case UNKWNSW:
132 adios (NULL, "-%s unknown", cp);
133
134 case HELPSW:
135 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
136 print_help (buf, switches, 1);
137 done (0);
138 case VERSIONSW:
139 print_version(invo_name);
140 done (0);
141
142 case RCACHESW:
143 icachesw = &rcachesw;
144 goto do_cache;
145 case WCACHESW:
146 icachesw = &wcachesw;
147 do_cache: ;
148 if (!(cp = *argp++) || *cp == '-')
149 adios (NULL, "missing argument to %s", argp[-2]);
150 switch (*icachesw = smatch (cp, caches)) {
151 case AMBIGSW:
152 ambigsw (cp, caches);
153 done (1);
154 case UNKWNSW:
155 adios (NULL, "%s unknown", cp);
156 default:
157 break;
158 }
159 continue;
160
161 case CHECKSW:
162 checksw++;
163 continue;
164 case NCHECKSW:
165 checksw = 0;
166 continue;
167
168 case HEADSW:
169 headsw++;
170 continue;
171 case NHEADSW:
172 headsw = 0;
173 continue;
174
175 case DIRECTIVES:
176 directives = 1;
177 continue;
178 case NDIRECTIVES:
179 directives = 0;
180 continue;
181
182 case LISTSW:
183 listsw++;
184 continue;
185 case NLISTSW:
186 listsw = 0;
187 continue;
188
189 case RFC934SW:
190 rfc934sw++;
191 continue;
192 case NRFC934SW:
193 rfc934sw = 0;
194 continue;
195
196 case SIZESW:
197 sizesw++;
198 continue;
199 case NSIZESW:
200 sizesw = 0;
201 continue;
202
203 case CONTENTIDSW:
204 contentidsw = 1;
205 continue;
206 case NCONTENTIDSW:
207 contentidsw = 0;
208 continue;
209
210 case VERBSW:
211 verbosw++;
212 continue;
213 case NVERBSW:
214 verbosw = 0;
215 continue;
216 case DEBUGSW:
217 debugsw = 1;
218 continue;
219 }
220 }
221 if (compfile)
222 adios (NULL, "only one composition file allowed");
223 else
224 compfile = cp;
225 }
226
227 /*
228 * Check if we've specified an additional profile
229 */
230 if ((cp = getenv ("MHBUILD"))) {
231 if ((fp = fopen (cp, "r"))) {
232 readconfig ((struct node **) 0, fp, cp, 0);
233 fclose (fp);
234 } else {
235 admonish ("", "unable to read $MHBUILD profile (%s)", cp);
236 }
237 }
238
239 /*
240 * Read the standard profile setup
241 */
242 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
243 readconfig ((struct node **) 0, fp, cp, 0);
244 fclose (fp);
245 }
246
247 /* Check for public cache location */
248 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
249 cache_public = NULL;
250
251 /* Check for private cache location */
252 if (!(cache_private = context_find (nmhprivcache)))
253 cache_private = ".cache";
254 cache_private = getcpy (m_maildir (cache_private));
255
256 /*
257 * Check for storage directory. If defined, we
258 * will store temporary files there. Else we
259 * store them in standard nmh directory.
260 */
261 if ((cp = context_find (nmhstorage)) && *cp)
262 tmp = concat (cp, "/", invo_name, NULL);
263 else
264 tmp = add (m_maildir (invo_name), NULL);
265
266 if (!context_find ("path"))
267 free (path ("./", TFOLDER));
268
269 /* Check if we have a file to process */
270 if (!compfile)
271 adios (NULL, "need to specify a %s composition file", invo_name);
272
273 /*
274 * Process the composition file from standard input.
275 */
276 if (compfile[0] == '-' && compfile[1] == '\0') {
277 /* copy standard input to temporary file */
278 strncpy (infile, m_mktemp(invo_name, NULL, &fp), sizeof(infile));
279 while (fgets (buffer, BUFSIZ, stdin))
280 fputs (buffer, fp);
281 fclose (fp);
282 unlink_infile = 1;
283
284 /* build the content structures for MIME message */
285 ct = build_mime (infile, directives);
286 cts[0] = ct;
287 cts[1] = NULL;
288
289 /* output MIME message to this temporary file */
290 strncpy (outfile, m_mktemp(invo_name, NULL, &fp_out), sizeof(outfile));
291 unlink_outfile = 1;
292
293 /* output the message */
294 output_message_fp (ct, fp_out, outfile);
295 fclose(fp_out);
296
297 /* output the temp file to standard output */
298 if ((fp = fopen (outfile, "r")) == NULL)
299 adios (outfile, "unable to open");
300 while (fgets (buffer, BUFSIZ, fp))
301 fputs (buffer, stdout);
302 fclose (fp);
303
304 unlink (infile);
305 unlink_infile = 0;
306
307 unlink (outfile);
308 unlink_outfile = 0;
309
310 free_content (ct);
311 done (0);
312 }
313
314 /*
315 * Process the composition file from a file.
316 */
317
318 /* build the content structures for MIME message */
319 ct = build_mime (compfile, directives);
320 cts[0] = ct;
321 cts[1] = NULL;
322
323 /* output MIME message to this temporary file */
324 strncpy(outfile, m_mktemp2(compfile, invo_name, NULL, &fp_out),
325 sizeof(outfile));
326 unlink_outfile = 1;
327
328 /* output the message */
329 output_message_fp (ct, fp_out, outfile);
330 fclose(fp_out);
331
332 /*
333 * List the message info
334 */
335 if (listsw)
336 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
337
338 /* Rename composition draft */
339 snprintf (buffer, sizeof(buffer), "%s.orig", m_backup (compfile));
340 if (rename (compfile, buffer) == NOTOK) {
341 adios (compfile, "unable to rename comp draft %s to", buffer);
342 }
343
344 /* Rename output file to take its place */
345 if (rename (outfile, compfile) == NOTOK) {
346 advise (outfile, "unable to rename output %s to", compfile);
347 rename (buffer, compfile);
348 done (1);
349 }
350 unlink_outfile = 0;
351
352 free_content (ct);
353 done (0);
354 return 1;
355 }
356
357
358 static void
359 unlink_done (int status)
360 {
361 /*
362 * Check if we need to remove stray
363 * temporary files.
364 */
365 if (unlink_infile)
366 unlink (infile);
367 if (unlink_outfile)
368 unlink (outfile);
369
370 exit (status);
371 }