]>
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>
17 #include "sbr/m_maildir.h"
18 #include "sbr/m_mktemp.h"
22 #define MHBUILD_SWITCHES \
23 X("auto", 0, AUTOSW) \
24 X("noauto", 0, NAUTOSW) \
25 X("check", 0, CHECKSW) \
26 X("nocheck", 0, NCHECKSW) \
27 X("directives", 0, DIRECTIVES) \
28 X("nodirectives", 0, NDIRECTIVES) \
29 X("headers", 0, HEADSW) \
30 X("noheaders", 0, NHEADSW) \
31 X("list", 0, LISTSW) \
32 X("nolist", 0, NLISTSW) \
33 X("realsize", 0, SIZESW) \
34 X("norealsize", 0, NSIZESW) \
35 X("rfc934mode", 0, RFC934SW) \
36 X("norfc934mode", 0, NRFC934SW) \
37 X("verbose", 0, VERBSW) \
38 X("noverbose", 0, NVERBSW) \
39 X("disposition", 0, DISPOSW) \
40 X("nodisposition", 0, NDISPOSW) \
41 X("rcache policy", 0, RCACHESW) \
42 X("wcache policy", 0, WCACHESW) \
43 X("contentid", 0, CONTENTIDSW) \
44 X("nocontentid", 0, NCONTENTIDSW) \
45 X("headerencoding encoding-algorithm", 0, HEADERENCSW) \
46 X("autoheaderencoding", 0, AUTOHEADERENCSW) \
47 X("maxunencoded", 0, MAXUNENCSW) \
48 X("version", 0, VERSIONSW) \
49 X("help", 0, HELPSW) \
50 X("debug", -5, DEBUGSW) \
51 X("dist", 0, DISTSW) \
53 #define X(sw, minchars, id) id,
54 DEFINE_SWITCH_ENUM(MHBUILD
);
57 #define X(sw, minchars, id) { sw, minchars, id },
58 DEFINE_SWITCH_ARRAY(MHBUILD
, switches
);
61 /* utf-8 is for Email Address Internationalization, using SMTPUTF8. */
62 #define MIMEENCODING_SWITCHES \
63 X("base64", 0, BASE64SW) \
64 X("quoted-printable", 0, QUOTEDPRINTSW) \
65 X("utf-8", 0, UTF8SW) \
67 #define X(sw, minchars, id) id,
68 DEFINE_SWITCH_ENUM(MIMEENCODING
);
71 #define X(sw, minchars, id) { sw, minchars, id },
72 DEFINE_SWITCH_ARRAY(MIMEENCODING
, encodingswitches
);
84 static char infile
[BUFSIZ
];
85 static int unlink_infile
= 0;
87 static char outfile
[BUFSIZ
];
88 static int unlink_outfile
= 0;
90 static void unlink_done (int) NORETURN
;
94 main (int argc
, char **argv
)
96 int sizesw
= 1, headsw
= 1, directives
= 1, autobuild
= 0, dist
= 0;
97 int verbosw
= 0, dispo
= 0;
98 size_t maxunencoded
= MAXTEXTPERLN
;
100 char *cp
, buf
[BUFSIZ
];
101 char buffer
[BUFSIZ
], *compfile
= NULL
;
102 char **argp
, **arguments
;
106 int header_encoding
= CE_UNKNOWN
;
109 if (nmh_init(argv
[0], 2)) { return 1; }
113 arguments
= getarguments (invo_name
, argc
, argv
, 1);
116 while ((cp
= *argp
++)) {
117 if (cp
[0] == '-' && cp
[1] == '\0') {
119 adios (NULL
, "cannot specify both standard input and a file");
121 listsw
= 0; /* turn off -list if using standard in/out */
122 verbosw
= 0; /* turn off -verbose listings */
126 switch (smatch (++cp
, switches
)) {
128 ambigsw (cp
, switches
);
131 adios (NULL
, "-%s unknown", cp
);
134 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
135 print_help (buf
, switches
, 1);
138 print_version(invo_name
);
142 /* -auto implies -nodirectives */
148 * We're turning directives back on since this is likely here
149 * to override a profile entry
156 icachesw
= &rcachesw
;
159 icachesw
= &wcachesw
;
161 if (!(cp
= *argp
++) || *cp
== '-')
162 adios (NULL
, "missing argument to %s", argp
[-2]);
163 switch (*icachesw
= smatch (cp
, cache_policy
)) {
165 ambigsw (cp
, cache_policy
);
168 adios (NULL
, "%s unknown", cp
);
226 if (!(cp
= *argp
++) || *cp
== '-')
227 adios (NULL
, "missing argument to %s", argp
[-2]);
228 switch (encoding
= smatch (cp
, encodingswitches
)) {
230 ambigsw (cp
, encodingswitches
);
233 adios (NULL
, "%s unknown encoding algorithm", cp
);
235 header_encoding
= CE_BASE64
;
238 header_encoding
= CE_QUOTED
;
241 header_encoding
= CE_8BIT
;
244 adios (NULL
, "Internal error: algorithm %s", cp
);
249 case AUTOHEADERENCSW
:
250 header_encoding
= CE_UNKNOWN
;
254 if (!(cp
= *argp
++) || *cp
== '-')
255 adios (NULL
, "missing argument to %s", argp
[-2]);
256 if ((maxunencoded
= atoi(cp
)) < 1)
257 adios (NULL
, "Invalid argument for %s: %s", argp
[-2], cp
);
258 if (maxunencoded
> 998)
259 adios (NULL
, "limit of -maxunencoded is 998");
283 adios (NULL
, "only one composition file allowed");
288 * Check if we've specified an additional profile
290 if ((cp
= getenv ("MHBUILD"))) {
291 if ((fp
= fopen (cp
, "r"))) {
292 readconfig(NULL
, fp
, cp
, 0);
295 admonish ("", "unable to read $MHBUILD profile (%s)", cp
);
300 * Read the standard profile setup
302 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
303 readconfig(NULL
, fp
, cp
, 0);
307 /* Check for public cache location */
308 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
311 /* Check for private cache location */
312 if (!(cache_private
= context_find (nmhprivcache
)))
313 cache_private
= ".cache";
314 cache_private
= getcpy (m_maildir (cache_private
));
316 if (!context_find ("path"))
317 free (path ("./", TFOLDER
));
319 /* Check if we have a file to process */
321 adios (NULL
, "need to specify a %s composition file", invo_name
);
324 * Process the composition file from standard input.
326 if (compfile
[0] == '-' && compfile
[1] == '\0') {
327 if ((cp
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
328 adios(NULL
, "unable to create temporary file in %s",
332 /* save a copy of the name for later removal */
333 strncpy (infile
, cp
, sizeof(infile
));
336 /* copy standard input to temporary file */
337 while ((n
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0) {
338 if (fwrite(buffer
, 1, n
, fp
) != n
) {
339 adios (NULL
, "error copying to temporary file");
344 /* build the content structures for MIME message */
345 ct
= build_mime (infile
, autobuild
, dist
, directives
, header_encoding
,
346 maxunencoded
, verbosw
);
349 * If ct == NULL, that means that -auto was set and a MIME version
350 * header was already seen. Just use the input file as the output
354 if (! (fp
= fopen(infile
, "r"))) {
355 adios(NULL
, "Unable to open %s for reading", infile
);
357 while ((n
= fread(buffer
, 1, sizeof(buffer
), fp
)) > 0) {
358 if (fwrite(buffer
, 1, n
, stdout
) != n
) {
359 adios(NULL
, "error copying %s to stdout", infile
);
363 /* output the message */
364 output_message_fp (ct
, stdout
, NULL
);
372 * Process the composition file from a file.
375 /* build the content structures for MIME message */
376 ct
= build_mime (compfile
, autobuild
, dist
, directives
, header_encoding
,
377 maxunencoded
, verbosw
);
380 * If ct == NULL, that means -auto was set and we found a MIME version
381 * header. Simply exit and do nothing.
390 /* output MIME message to this temporary file */
391 if ((cp
= m_mktemp2(compfile
, invo_name
, NULL
, &fp_out
)) == NULL
) {
392 adios(NULL
, "unable to create temporary file in %s", get_temp_dir());
394 strncpy(outfile
, cp
, sizeof(outfile
));
397 /* output the message */
398 output_message_fp (ct
, fp_out
, outfile
);
402 * List the message info
405 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, dispo
);
407 /* Rename composition draft */
408 snprintf (buffer
, sizeof(buffer
), "%s.orig", m_backup (compfile
));
409 if (rename (compfile
, buffer
) == NOTOK
) {
410 adios (compfile
, "unable to rename comp draft %s to", buffer
);
413 /* Rename output file to take its place */
414 if (rename (outfile
, compfile
) == NOTOK
) {
415 advise (outfile
, "unable to rename output %s to", compfile
);
416 rename (buffer
, compfile
);
428 unlink_done (int status
)
431 * Check if we need to remove stray
435 (void) m_unlink (infile
);
437 (void) m_unlink (outfile
);