]>
diplodocus.org Git - nmh/blob - uip/mhbuild.c
1 /* mhbuild.c -- expand/translate MIME composition files
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
14 #include <h/mhparse.h>
15 #include <h/mhcachesbr.h>
18 #include "sbr/m_maildir.h"
19 #include "sbr/m_mktemp.h"
23 #define MHBUILD_SWITCHES \
24 X("auto", 0, AUTOSW) \
25 X("noauto", 0, NAUTOSW) \
26 X("check", 0, CHECKSW) \
27 X("nocheck", 0, NCHECKSW) \
28 X("directives", 0, DIRECTIVES) \
29 X("nodirectives", 0, NDIRECTIVES) \
30 X("headers", 0, HEADSW) \
31 X("noheaders", 0, NHEADSW) \
32 X("list", 0, LISTSW) \
33 X("nolist", 0, NLISTSW) \
34 X("realsize", 0, SIZESW) \
35 X("norealsize", 0, NSIZESW) \
36 X("rfc934mode", 0, RFC934SW) \
37 X("norfc934mode", 0, NRFC934SW) \
38 X("verbose", 0, VERBSW) \
39 X("noverbose", 0, NVERBSW) \
40 X("disposition", 0, DISPOSW) \
41 X("nodisposition", 0, NDISPOSW) \
42 X("rcache policy", 0, RCACHESW) \
43 X("wcache policy", 0, WCACHESW) \
44 X("contentid", 0, CONTENTIDSW) \
45 X("nocontentid", 0, NCONTENTIDSW) \
46 X("headerencoding encoding-algorithm", 0, HEADERENCSW) \
47 X("autoheaderencoding", 0, AUTOHEADERENCSW) \
48 X("maxunencoded", 0, MAXUNENCSW) \
49 X("version", 0, VERSIONSW) \
50 X("help", 0, HELPSW) \
51 X("debug", -5, DEBUGSW) \
52 X("dist", 0, DISTSW) \
54 #define X(sw, minchars, id) id,
55 DEFINE_SWITCH_ENUM(MHBUILD
);
58 #define X(sw, minchars, id) { sw, minchars, id },
59 DEFINE_SWITCH_ARRAY(MHBUILD
, switches
);
62 /* utf-8 is for Email Address Internationalization, using SMTPUTF8. */
63 #define MIMEENCODING_SWITCHES \
64 X("base64", 0, BASE64SW) \
65 X("quoted-printable", 0, QUOTEDPRINTSW) \
66 X("utf-8", 0, UTF8SW) \
68 #define X(sw, minchars, id) id,
69 DEFINE_SWITCH_ENUM(MIMEENCODING
);
72 #define X(sw, minchars, id) { sw, minchars, id },
73 DEFINE_SWITCH_ARRAY(MIMEENCODING
, encodingswitches
);
80 bool contentidsw
= true;
85 static char infile
[BUFSIZ
];
86 static char outfile
[BUFSIZ
];
90 main (int argc
, char **argv
)
94 bool directives
= true;
95 bool autobuild
= false;
99 size_t maxunencoded
= MAXTEXTPERLN
;
101 char *cp
, buf
[BUFSIZ
];
102 char buffer
[BUFSIZ
], *compfile
= NULL
;
103 char **argp
, **arguments
;
107 int header_encoding
= CE_UNKNOWN
;
110 if (nmh_init(argv
[0], true, false)) { return 1; }
112 arguments
= getarguments (invo_name
, argc
, argv
, 1);
115 while ((cp
= *argp
++)) {
116 if (cp
[0] == '-' && cp
[1] == '\0') {
118 die("cannot specify both standard input and a file");
120 listsw
= false; /* turn off -list if using standard in/out */
121 verbosw
= false; /* turn off -verbose listings */
125 switch (smatch (++cp
, switches
)) {
127 ambigsw (cp
, switches
);
130 die("-%s unknown", cp
);
133 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
134 print_help (buf
, switches
, 1);
137 print_version(invo_name
);
141 /* -auto implies -nodirectives */
147 * We're turning directives back on since this is likely here
148 * to override a profile entry
155 icachesw
= &rcachesw
;
158 icachesw
= &wcachesw
;
160 if (!(cp
= *argp
++) || *cp
== '-')
161 die("missing argument to %s", argp
[-2]);
162 switch (*icachesw
= smatch (cp
, cache_policy
)) {
164 ambigsw (cp
, cache_policy
);
167 die("%s unknown", cp
);
225 if (!(cp
= *argp
++) || *cp
== '-')
226 die("missing argument to %s", argp
[-2]);
227 switch (encoding
= smatch (cp
, encodingswitches
)) {
229 ambigsw (cp
, encodingswitches
);
232 die("%s unknown encoding algorithm", cp
);
234 header_encoding
= CE_BASE64
;
237 header_encoding
= CE_QUOTED
;
240 header_encoding
= CE_8BIT
;
243 die("Internal error: algorithm %s", cp
);
248 case AUTOHEADERENCSW
:
249 header_encoding
= CE_UNKNOWN
;
253 if (!(cp
= *argp
++) || *cp
== '-')
254 die("missing argument to %s", argp
[-2]);
255 if ((maxunencoded
= atoi(cp
)) < 1)
256 die("Invalid argument for %s: %s", argp
[-2], cp
);
257 if (maxunencoded
> 998)
258 die("limit of -maxunencoded is 998");
282 die("only one composition file allowed");
287 * Check if we've specified an additional profile
289 if ((cp
= getenv ("MHBUILD"))) {
290 if ((fp
= fopen (cp
, "r"))) {
291 readconfig(NULL
, fp
, cp
, 0);
294 admonish ("", "unable to read $MHBUILD profile (%s)", cp
);
299 * Read the standard profile setup
301 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
302 readconfig(NULL
, fp
, cp
, 0);
306 /* Check for public cache location */
307 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
310 /* Check for private cache location */
311 if (!(cache_private
= context_find (nmhprivcache
)))
312 cache_private
= ".cache";
313 cache_private
= mh_xstrdup(m_maildir(cache_private
));
315 if (!context_find ("path"))
316 free (path ("./", TFOLDER
));
318 /* Check if we have a file to process */
320 die("need to specify a %s composition file", invo_name
);
323 * Process the composition file from standard input.
325 if (compfile
[0] == '-' && compfile
[1] == '\0') {
326 if ((cp
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
327 die("unable to create temporary file in %s",
330 strncpy (infile
, cp
, sizeof(infile
));
332 /* copy standard input to temporary file */
333 while ((n
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0) {
334 if (fwrite(buffer
, 1, n
, fp
) != n
) {
335 die("error copying to temporary file");
340 /* build the content structures for MIME message */
341 ct
= build_mime (infile
, autobuild
, dist
, directives
, header_encoding
,
342 maxunencoded
, verbosw
);
345 * If ct == NULL, that means that -auto was set and a MIME version
346 * header was already seen. Just use the input file as the output
350 if (! (fp
= fopen(infile
, "r"))) {
351 die("Unable to open %s for reading", infile
);
353 while ((n
= fread(buffer
, 1, sizeof(buffer
), fp
)) > 0) {
354 if (fwrite(buffer
, 1, n
, stdout
) != n
) {
355 die("error copying %s to stdout", infile
);
359 /* output the message */
360 output_message_fp (ct
, stdout
, NULL
);
368 * Process the composition file from a file.
371 /* build the content structures for MIME message */
372 ct
= build_mime (compfile
, autobuild
, dist
, directives
, header_encoding
,
373 maxunencoded
, verbosw
);
376 * If ct == NULL, that means -auto was set and we found a MIME version
377 * header. Simply exit and do nothing.
386 /* output MIME message to this temporary file */
387 if ((cp
= m_mktemp2(compfile
, invo_name
, NULL
, &fp_out
)) == NULL
) {
388 die("unable to create temporary file");
390 strncpy(outfile
, cp
, sizeof(outfile
));
392 /* output the message */
393 output_message_fp (ct
, fp_out
, outfile
);
397 * List the message info
400 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, dispo
);
402 /* Rename composition draft */
403 snprintf (buffer
, sizeof(buffer
), "%s.orig", m_backup (compfile
));
404 if (rename (compfile
, buffer
) == NOTOK
) {
405 adios (compfile
, "unable to rename comp draft %s to", buffer
);
408 /* Rename output file to take its place */
409 if (rename (outfile
, compfile
) == NOTOK
) {
410 advise (outfile
, "unable to rename output %s to", compfile
);
411 rename (buffer
, compfile
);
414 /* Remove from atexit(3) list of files to unlink. */
415 if (!(m_unlink(outfile
) == -1 && errno
== ENOENT
)) {
416 adios(outfile
, "file exists after rename:");