]> diplodocus.org Git - nmh/blob - uip/mhbuild.c
Document the -dist flag.
[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 <h/mts.h>
15 #include <h/tws.h>
16 #include <h/mime.h>
17 #include <h/mhparse.h>
18 #include <h/mhcachesbr.h>
19 #include <h/utils.h>
20
21 #define MHBUILD_SWITCHES \
22 X("auto", 0, AUTOSW) \
23 X("noauto", 0, NAUTOSW) \
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("headerencoding encoding-algorithm", 0, HEADERENCSW) \
43 X("autoheaderencoding", 0, AUTOHEADERENCSW) \
44 X("version", 0, VERSIONSW) \
45 X("help", 0, HELPSW) \
46 X("debug", -5, DEBUGSW) \
47 X("dist", -4, DISTSW) \
48
49 #define X(sw, minchars, id) id,
50 DEFINE_SWITCH_ENUM(MHBUILD);
51 #undef X
52
53 #define X(sw, minchars, id) { sw, minchars, id },
54 DEFINE_SWITCH_ARRAY(MHBUILD, switches);
55 #undef X
56
57 #define MIMEENCODING_SWITCHES \
58 X("base64", 0, BASE64SW) \
59 X("quoted-printable", 0, QUOTEDPRINTSW) \
60
61 #define X(sw, minchars, id) id,
62 DEFINE_SWITCH_ENUM(MIMEENCODING);
63 #undef X
64
65 #define X(sw, minchars, id) { sw, minchars, id },
66 DEFINE_SWITCH_ARRAY(MIMEENCODING, encodingswitches);
67 #undef X
68
69 /* mhbuildsbr.c */
70 extern char *tmp; /* directory to place temp files */
71
72 /* mhcachesbr.c */
73 extern int rcachesw;
74 extern int wcachesw;
75 extern char *cache_public;
76 extern char *cache_private;
77
78 int debugsw = 0;
79 int verbosw = 0;
80
81 int listsw = 0;
82 int rfc934sw = 0;
83 int contentidsw = 1;
84
85 /*
86 * Temporary files
87 */
88 static char infile[BUFSIZ];
89 static int unlink_infile = 0;
90
91 static char outfile[BUFSIZ];
92 static int unlink_outfile = 0;
93
94 static void unlink_done (int) NORETURN;
95
96 /* mhbuildsbr.c */
97 int output_message (CT, char *);
98 int output_message_fp (CT, FILE *, char*);
99
100 /* mhlistsbr.c */
101 int list_all_messages (CT *, int, int, int, int);
102
103
104 int
105 main (int argc, char **argv)
106 {
107 int sizesw = 1, headsw = 1, directives = 1, autobuild = 0, dist = 0;
108 int *icachesw;
109 char *cp, buf[BUFSIZ];
110 char buffer[BUFSIZ], *compfile = NULL;
111 char **argp, **arguments;
112 CT ct, cts[2];
113 FILE *fp = NULL;
114 FILE *fp_out = NULL;
115 int header_encoding = CE_UNKNOWN;
116
117 done=unlink_done;
118
119 #ifdef LOCALE
120 setlocale(LC_ALL, "");
121 #endif
122 invo_name = r1bindex (argv[0], '/');
123
124 /* read user profile/context */
125 context_read();
126
127 arguments = getarguments (invo_name, argc, argv, 1);
128 argp = arguments;
129
130 while ((cp = *argp++)) {
131 if (cp[0] == '-' && cp[1] == '\0') {
132 if (compfile)
133 adios (NULL, "cannot specify both standard input and a file");
134 else
135 compfile = cp;
136 listsw = 0; /* turn off -list if using standard in/out */
137 verbosw = 0; /* turn off -verbose listings */
138 break;
139 }
140 if (*cp == '-') {
141 switch (smatch (++cp, switches)) {
142 case AMBIGSW:
143 ambigsw (cp, switches);
144 done (1);
145 case UNKWNSW:
146 adios (NULL, "-%s unknown", cp);
147
148 case HELPSW:
149 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
150 print_help (buf, switches, 1);
151 done (0);
152 case VERSIONSW:
153 print_version(invo_name);
154 done (0);
155
156 case AUTOSW:
157 /* -auto implies -nodirectives */
158 autobuild = 1;
159 directives = 0;
160 continue;
161 case NAUTOSW:
162 /*
163 * We're turning directives back on since this is likely here
164 * to override a profile entry
165 */
166 autobuild = 0;
167 directives = 1;
168 continue;
169
170 case RCACHESW:
171 icachesw = &rcachesw;
172 goto do_cache;
173 case WCACHESW:
174 icachesw = &wcachesw;
175 do_cache: ;
176 if (!(cp = *argp++) || *cp == '-')
177 adios (NULL, "missing argument to %s", argp[-2]);
178 switch (*icachesw = smatch (cp, caches)) {
179 case AMBIGSW:
180 ambigsw (cp, caches);
181 done (1);
182 case UNKWNSW:
183 adios (NULL, "%s unknown", cp);
184 default:
185 break;
186 }
187 continue;
188
189 case CHECKSW:
190 checksw++;
191 continue;
192 case NCHECKSW:
193 checksw = 0;
194 continue;
195
196 case HEADSW:
197 headsw++;
198 continue;
199 case NHEADSW:
200 headsw = 0;
201 continue;
202
203 case DIRECTIVES:
204 directives = 1;
205 continue;
206 case NDIRECTIVES:
207 directives = 0;
208 continue;
209
210 case LISTSW:
211 listsw++;
212 continue;
213 case NLISTSW:
214 listsw = 0;
215 continue;
216
217 case RFC934SW:
218 rfc934sw++;
219 continue;
220 case NRFC934SW:
221 rfc934sw = 0;
222 continue;
223
224 case SIZESW:
225 sizesw++;
226 continue;
227 case NSIZESW:
228 sizesw = 0;
229 continue;
230
231 case CONTENTIDSW:
232 contentidsw = 1;
233 continue;
234 case NCONTENTIDSW:
235 contentidsw = 0;
236 continue;
237
238 case HEADERENCSW: {
239 int encoding;
240
241 if (!(cp = *argp++) || *cp == '-')
242 adios (NULL, "missing argument to %s", argp[-2]);
243 switch (encoding = smatch (cp, encodingswitches)) {
244 case AMBIGSW:
245 ambigsw (cp, encodingswitches);
246 done (1);
247 case UNKWNSW:
248 adios (NULL, "%s unknown encoding algorithm", cp);
249 case BASE64SW:
250 header_encoding = CE_BASE64;
251 break;
252 case QUOTEDPRINTSW:
253 header_encoding = CE_QUOTED;
254 break;
255 default:
256 adios (NULL, "Internal error: algorithm %s", cp);
257 }
258 continue;
259 }
260
261 case AUTOHEADERENCSW:
262 header_encoding = CE_UNKNOWN;
263 continue;
264
265 case VERBSW:
266 verbosw++;
267 continue;
268 case NVERBSW:
269 verbosw = 0;
270 continue;
271 case DEBUGSW:
272 debugsw = 1;
273 continue;
274 case DISTSW:
275 dist = 1;
276 continue;
277 }
278 }
279 if (compfile)
280 adios (NULL, "only one composition file allowed");
281 else
282 compfile = cp;
283 }
284
285 /*
286 * Check if we've specified an additional profile
287 */
288 if ((cp = getenv ("MHBUILD"))) {
289 if ((fp = fopen (cp, "r"))) {
290 readconfig ((struct node **) 0, fp, cp, 0);
291 fclose (fp);
292 } else {
293 admonish ("", "unable to read $MHBUILD profile (%s)", cp);
294 }
295 }
296
297 /*
298 * Read the standard profile setup
299 */
300 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
301 readconfig ((struct node **) 0, fp, cp, 0);
302 fclose (fp);
303 }
304
305 /* Check for public cache location */
306 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
307 cache_public = NULL;
308
309 /* Check for private cache location */
310 if (!(cache_private = context_find (nmhprivcache)))
311 cache_private = ".cache";
312 cache_private = getcpy (m_maildir (cache_private));
313
314 /*
315 * Check for storage directory. If defined, we
316 * will store temporary files there. Else we
317 * store them in standard nmh directory.
318 */
319 if ((cp = context_find (nmhstorage)) && *cp)
320 tmp = concat (cp, "/", invo_name, NULL);
321 else
322 tmp = add (m_maildir (invo_name), NULL);
323
324 if (!context_find ("path"))
325 free (path ("./", TFOLDER));
326
327 /* Check if we have a file to process */
328 if (!compfile)
329 adios (NULL, "need to specify a %s composition file", invo_name);
330
331 /*
332 * Process the composition file from standard input.
333 */
334 if (compfile[0] == '-' && compfile[1] == '\0') {
335 /* copy standard input to temporary file */
336 strncpy (infile, m_mktemp(invo_name, NULL, &fp), sizeof(infile));
337 while (fgets (buffer, BUFSIZ, stdin))
338 fputs (buffer, fp);
339 fclose (fp);
340 unlink_infile = 1;
341
342 /* build the content structures for MIME message */
343 ct = build_mime (infile, autobuild, dist, directives, header_encoding);
344
345 /*
346 * If ct == NULL, that means that -auto was set and a MIME version
347 * header was already seen. Just use the input file as the output
348 */
349
350 if (!ct) {
351 if (! (fp = fopen(infile, "r"))) {
352 adios(NULL, "Unable to open %s for reading", infile);
353 }
354 while (fgets(buffer, BUFSIZ, fp))
355 fputs(buffer, stdout);
356 } else {
357 /* output the message */
358 output_message_fp (ct, stdout, NULL);
359 free_content (ct);
360 }
361
362 unlink (infile);
363 unlink_infile = 0;
364
365 done (0);
366 }
367
368 /*
369 * Process the composition file from a file.
370 */
371
372 /* build the content structures for MIME message */
373 ct = build_mime (compfile, autobuild, dist, directives, header_encoding);
374
375 /*
376 * If ct == NULL, that means -auto was set and we found a MIME version
377 * header. Simply exit and do nothing.
378 */
379
380 if (! ct)
381 done(0);
382
383 cts[0] = ct;
384 cts[1] = NULL;
385
386 /* output MIME message to this temporary file */
387 strncpy(outfile, m_mktemp2(compfile, invo_name, NULL, &fp_out),
388 sizeof(outfile));
389 unlink_outfile = 1;
390
391 /* output the message */
392 output_message_fp (ct, fp_out, outfile);
393 fclose(fp_out);
394
395 /*
396 * List the message info
397 */
398 if (listsw)
399 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
400
401 /* Rename composition draft */
402 snprintf (buffer, sizeof(buffer), "%s.orig", m_backup (compfile));
403 if (rename (compfile, buffer) == NOTOK) {
404 adios (compfile, "unable to rename comp draft %s to", buffer);
405 }
406
407 /* Rename output file to take its place */
408 if (rename (outfile, compfile) == NOTOK) {
409 advise (outfile, "unable to rename output %s to", compfile);
410 rename (buffer, compfile);
411 done (1);
412 }
413 unlink_outfile = 0;
414
415 free_content (ct);
416 done (0);
417 return 1;
418 }
419
420
421 static void
422 unlink_done (int status)
423 {
424 /*
425 * Check if we need to remove stray
426 * temporary files.
427 */
428 if (unlink_infile)
429 unlink (infile);
430 if (unlink_outfile)
431 unlink (outfile);
432
433 exit (status);
434 }