]>
diplodocus.org Git - nmh/blob - uip/mhbuild.c
b6f26eb9a2e9f08e5f0adb306bd2a2c5bd56ca5c
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
);
85 static char infile
[BUFSIZ
];
86 static char outfile
[BUFSIZ
];
90 main (int argc
, char **argv
)
92 int sizesw
= 1, headsw
= 1, directives
= 1, autobuild
= 0, dist
= 0;
93 int verbosw
= 0, dispo
= 0;
94 size_t maxunencoded
= MAXTEXTPERLN
;
96 char *cp
, buf
[BUFSIZ
];
97 char buffer
[BUFSIZ
], *compfile
= NULL
;
98 char **argp
, **arguments
;
102 int header_encoding
= CE_UNKNOWN
;
105 if (nmh_init(argv
[0], 2)) { return 1; }
107 arguments
= getarguments (invo_name
, argc
, argv
, 1);
110 while ((cp
= *argp
++)) {
111 if (cp
[0] == '-' && cp
[1] == '\0') {
113 adios (NULL
, "cannot specify both standard input and a file");
115 listsw
= 0; /* turn off -list if using standard in/out */
116 verbosw
= 0; /* turn off -verbose listings */
120 switch (smatch (++cp
, switches
)) {
122 ambigsw (cp
, switches
);
125 adios (NULL
, "-%s unknown", cp
);
128 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
129 print_help (buf
, switches
, 1);
132 print_version(invo_name
);
136 /* -auto implies -nodirectives */
142 * We're turning directives back on since this is likely here
143 * to override a profile entry
150 icachesw
= &rcachesw
;
153 icachesw
= &wcachesw
;
155 if (!(cp
= *argp
++) || *cp
== '-')
156 adios (NULL
, "missing argument to %s", argp
[-2]);
157 switch (*icachesw
= smatch (cp
, cache_policy
)) {
159 ambigsw (cp
, cache_policy
);
162 adios (NULL
, "%s unknown", cp
);
220 if (!(cp
= *argp
++) || *cp
== '-')
221 adios (NULL
, "missing argument to %s", argp
[-2]);
222 switch (encoding
= smatch (cp
, encodingswitches
)) {
224 ambigsw (cp
, encodingswitches
);
227 adios (NULL
, "%s unknown encoding algorithm", cp
);
229 header_encoding
= CE_BASE64
;
232 header_encoding
= CE_QUOTED
;
235 header_encoding
= CE_8BIT
;
238 adios (NULL
, "Internal error: algorithm %s", cp
);
243 case AUTOHEADERENCSW
:
244 header_encoding
= CE_UNKNOWN
;
248 if (!(cp
= *argp
++) || *cp
== '-')
249 adios (NULL
, "missing argument to %s", argp
[-2]);
250 if ((maxunencoded
= atoi(cp
)) < 1)
251 adios (NULL
, "Invalid argument for %s: %s", argp
[-2], cp
);
252 if (maxunencoded
> 998)
253 adios (NULL
, "limit of -maxunencoded is 998");
277 adios (NULL
, "only one composition file allowed");
282 * Check if we've specified an additional profile
284 if ((cp
= getenv ("MHBUILD"))) {
285 if ((fp
= fopen (cp
, "r"))) {
286 readconfig(NULL
, fp
, cp
, 0);
289 admonish ("", "unable to read $MHBUILD profile (%s)", cp
);
294 * Read the standard profile setup
296 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
297 readconfig(NULL
, fp
, cp
, 0);
301 /* Check for public cache location */
302 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
305 /* Check for private cache location */
306 if (!(cache_private
= context_find (nmhprivcache
)))
307 cache_private
= ".cache";
308 cache_private
= getcpy (m_maildir (cache_private
));
310 if (!context_find ("path"))
311 free (path ("./", TFOLDER
));
313 /* Check if we have a file to process */
315 adios (NULL
, "need to specify a %s composition file", invo_name
);
318 * Process the composition file from standard input.
320 if (compfile
[0] == '-' && compfile
[1] == '\0') {
321 if ((cp
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
322 adios(NULL
, "unable to create temporary file in %s",
325 strncpy (infile
, cp
, sizeof(infile
));
327 /* copy standard input to temporary file */
328 while ((n
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0) {
329 if (fwrite(buffer
, 1, n
, fp
) != n
) {
330 adios (NULL
, "error copying to temporary file");
335 /* build the content structures for MIME message */
336 ct
= build_mime (infile
, autobuild
, dist
, directives
, header_encoding
,
337 maxunencoded
, verbosw
);
340 * If ct == NULL, that means that -auto was set and a MIME version
341 * header was already seen. Just use the input file as the output
345 if (! (fp
= fopen(infile
, "r"))) {
346 adios(NULL
, "Unable to open %s for reading", infile
);
348 while ((n
= fread(buffer
, 1, sizeof(buffer
), fp
)) > 0) {
349 if (fwrite(buffer
, 1, n
, stdout
) != n
) {
350 adios(NULL
, "error copying %s to stdout", infile
);
354 /* output the message */
355 output_message_fp (ct
, stdout
, NULL
);
363 * Process the composition file from a file.
366 /* build the content structures for MIME message */
367 ct
= build_mime (compfile
, autobuild
, dist
, directives
, header_encoding
,
368 maxunencoded
, verbosw
);
371 * If ct == NULL, that means -auto was set and we found a MIME version
372 * header. Simply exit and do nothing.
381 /* output MIME message to this temporary file */
382 if ((cp
= m_mktemp2(compfile
, invo_name
, NULL
, &fp_out
)) == NULL
) {
383 adios(NULL
, "unable to create temporary file");
385 strncpy(outfile
, cp
, sizeof(outfile
));
387 /* output the message */
388 output_message_fp (ct
, fp_out
, outfile
);
392 * List the message info
395 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, dispo
);
397 /* Rename composition draft */
398 snprintf (buffer
, sizeof(buffer
), "%s.orig", m_backup (compfile
));
399 if (rename (compfile
, buffer
) == NOTOK
) {
400 adios (compfile
, "unable to rename comp draft %s to", buffer
);
403 /* Rename output file to take its place */
404 if (rename (outfile
, compfile
) == NOTOK
) {
405 advise (outfile
, "unable to rename output %s to", compfile
);
406 rename (buffer
, compfile
);
409 /* Remove from atexit(3) list of files to unlink. */
410 if (!(m_unlink(outfile
) == -1 && errno
== ENOENT
)) {
411 adios(outfile
, "file exists after rename:");