]> diplodocus.org Git - nmh/blob - uip/mhbuild.c
change mhlist to use decimal math when abbreviating sizes
[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("disposition", 0, DISPOSW) \
39 X("nodisposition", 0, NDISPOSW) \
40 X("rcache policy", 0, RCACHESW) \
41 X("wcache policy", 0, WCACHESW) \
42 X("contentid", 0, CONTENTIDSW) \
43 X("nocontentid", 0, NCONTENTIDSW) \
44 X("headerencoding encoding-algorithm", 0, HEADERENCSW) \
45 X("autoheaderencoding", 0, AUTOHEADERENCSW) \
46 X("maxunencoded", 0, MAXUNENCSW) \
47 X("version", 0, VERSIONSW) \
48 X("help", 0, HELPSW) \
49 X("debug", -5, DEBUGSW) \
50 X("dist", -4, DISTSW) \
51
52 #define X(sw, minchars, id) id,
53 DEFINE_SWITCH_ENUM(MHBUILD);
54 #undef X
55
56 #define X(sw, minchars, id) { sw, minchars, id },
57 DEFINE_SWITCH_ARRAY(MHBUILD, switches);
58 #undef X
59
60 #define MIMEENCODING_SWITCHES \
61 X("base64", 0, BASE64SW) \
62 X("quoted-printable", 0, QUOTEDPRINTSW) \
63
64 #define X(sw, minchars, id) id,
65 DEFINE_SWITCH_ENUM(MIMEENCODING);
66 #undef X
67
68 #define X(sw, minchars, id) { sw, minchars, id },
69 DEFINE_SWITCH_ARRAY(MIMEENCODING, encodingswitches);
70 #undef X
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
80 int listsw = 0;
81 int rfc934sw = 0;
82 int contentidsw = 1;
83
84 /*
85 * Temporary files
86 */
87 static char infile[BUFSIZ];
88 static int unlink_infile = 0;
89
90 static char outfile[BUFSIZ];
91 static int unlink_outfile = 0;
92
93 static void unlink_done (int) NORETURN;
94
95 /* mhbuildsbr.c */
96 int output_message (CT, char *);
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], 1)) { 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 default:
249 adios (NULL, "Internal error: algorithm %s", cp);
250 }
251 continue;
252 }
253
254 case AUTOHEADERENCSW:
255 header_encoding = CE_UNKNOWN;
256 continue;
257
258 case MAXUNENCSW:
259 if (!(cp = *argp++) || *cp == '-')
260 adios (NULL, "missing argument to %s", argp[-2]);
261 if ((maxunencoded = atoi(cp)) < 1)
262 adios (NULL, "Invalid argument for %s: %s", argp[-2], cp);
263 if (maxunencoded > 998)
264 adios (NULL, "limit of -maxunencoded is 998");
265 continue;
266
267 case VERBSW:
268 verbosw++;
269 continue;
270 case NVERBSW:
271 verbosw = 0;
272 continue;
273 case DISPOSW:
274 dispo = 1;
275 continue;
276 case NDISPOSW:
277 dispo = 0;
278 continue;
279 case DEBUGSW:
280 debugsw = 1;
281 continue;
282 case DISTSW:
283 dist = 1;
284 continue;
285 }
286 }
287 if (compfile)
288 adios (NULL, "only one composition file allowed");
289 else
290 compfile = cp;
291 }
292
293 /*
294 * Check if we've specified an additional profile
295 */
296 if ((cp = getenv ("MHBUILD"))) {
297 if ((fp = fopen (cp, "r"))) {
298 readconfig ((struct node **) 0, fp, cp, 0);
299 fclose (fp);
300 } else {
301 admonish ("", "unable to read $MHBUILD profile (%s)", cp);
302 }
303 }
304
305 /*
306 * Read the standard profile setup
307 */
308 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
309 readconfig ((struct node **) 0, fp, cp, 0);
310 fclose (fp);
311 }
312
313 /* Check for public cache location */
314 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
315 cache_public = NULL;
316
317 /* Check for private cache location */
318 if (!(cache_private = context_find (nmhprivcache)))
319 cache_private = ".cache";
320 cache_private = getcpy (m_maildir (cache_private));
321
322 if (!context_find ("path"))
323 free (path ("./", TFOLDER));
324
325 /* Check if we have a file to process */
326 if (!compfile)
327 adios (NULL, "need to specify a %s composition file", invo_name);
328
329 /*
330 * Process the composition file from standard input.
331 */
332 if (compfile[0] == '-' && compfile[1] == '\0') {
333 if ((cp = m_mktemp2(NULL, invo_name, NULL, &fp)) == NULL) {
334 adios(NULL, "unable to create temporary file in %s",
335 get_temp_dir());
336 }
337
338 /* save a copy of the name for later removal */
339 strncpy (infile, cp, sizeof(infile));
340 unlink_infile = 1;
341
342 /* copy standard input to temporary file */
343 while ((n = fread(buffer, 1, sizeof(buffer), stdin)) > 0) {
344 if (fwrite(buffer, 1, n, fp) != n) {
345 adios (NULL, "error copying to temporary file");
346 }
347 }
348 fclose (fp);
349
350 /* build the content structures for MIME message */
351 ct = build_mime (infile, autobuild, dist, directives, header_encoding,
352 maxunencoded, verbosw);
353
354 /*
355 * If ct == NULL, that means that -auto was set and a MIME version
356 * header was already seen. Just use the input file as the output
357 */
358
359 if (!ct) {
360 if (! (fp = fopen(infile, "r"))) {
361 adios(NULL, "Unable to open %s for reading", infile);
362 }
363 while ((n = fread(buffer, 1, sizeof(buffer), fp)) > 0) {
364 if (fwrite(buffer, 1, n, stdout) != n) {
365 adios(NULL, "error copying %s to stdout", infile);
366 }
367 }
368 } else {
369 /* output the message */
370 output_message_fp (ct, stdout, NULL);
371 free_content (ct);
372 }
373
374 done (0);
375 }
376
377 /*
378 * Process the composition file from a file.
379 */
380
381 /* build the content structures for MIME message */
382 ct = build_mime (compfile, autobuild, dist, directives, header_encoding,
383 maxunencoded, verbosw);
384
385 /*
386 * If ct == NULL, that means -auto was set and we found a MIME version
387 * header. Simply exit and do nothing.
388 */
389
390 if (! ct)
391 done(0);
392
393 cts[0] = ct;
394 cts[1] = NULL;
395
396 /* output MIME message to this temporary file */
397 if ((cp = m_mktemp2(compfile, invo_name, NULL, &fp_out)) == NULL) {
398 adios(NULL, "unable to create temporary file in %s", get_temp_dir());
399 }
400 strncpy(outfile, cp, sizeof(outfile));
401 unlink_outfile = 1;
402
403 /* output the message */
404 output_message_fp (ct, fp_out, outfile);
405 fclose(fp_out);
406
407 /*
408 * List the message info
409 */
410 if (listsw)
411 list_all_messages (cts, headsw, sizesw, verbosw, debugsw, dispo);
412
413 /* Rename composition draft */
414 snprintf (buffer, sizeof(buffer), "%s.orig", m_backup (compfile));
415 if (rename (compfile, buffer) == NOTOK) {
416 adios (compfile, "unable to rename comp draft %s to", buffer);
417 }
418
419 /* Rename output file to take its place */
420 if (rename (outfile, compfile) == NOTOK) {
421 advise (outfile, "unable to rename output %s to", compfile);
422 rename (buffer, compfile);
423 done (1);
424 }
425 unlink_outfile = 0;
426
427 free_content (ct);
428 done (0);
429 return 1;
430 }
431
432
433 static void
434 unlink_done (int status)
435 {
436 /*
437 * Check if we need to remove stray
438 * temporary files.
439 */
440 if (unlink_infile)
441 (void) m_unlink (infile);
442 if (unlink_outfile)
443 (void) m_unlink (outfile);
444
445 exit (status);
446 }