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