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