]>
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");
122 listsw
= 0; /* turn off -list if using standard in/out */
123 verbosw
= 0; /* turn off -verbose listings */
127 switch (smatch (++cp
, switches
)) {
129 ambigsw (cp
, switches
);
132 adios (NULL
, "-%s unknown", cp
);
135 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
136 print_help (buf
, switches
, 1);
139 print_version(invo_name
);
143 /* -auto implies -nodirectives */
149 * We're turning directives back on since this is likely here
150 * to override a profile entry
157 icachesw
= &rcachesw
;
160 icachesw
= &wcachesw
;
162 if (!(cp
= *argp
++) || *cp
== '-')
163 adios (NULL
, "missing argument to %s", argp
[-2]);
164 switch (*icachesw
= smatch (cp
, cache_policy
)) {
166 ambigsw (cp
, cache_policy
);
169 adios (NULL
, "%s unknown", cp
);
227 if (!(cp
= *argp
++) || *cp
== '-')
228 adios (NULL
, "missing argument to %s", argp
[-2]);
229 switch (encoding
= smatch (cp
, encodingswitches
)) {
231 ambigsw (cp
, encodingswitches
);
234 adios (NULL
, "%s unknown encoding algorithm", cp
);
236 header_encoding
= CE_BASE64
;
239 header_encoding
= CE_QUOTED
;
242 header_encoding
= CE_8BIT
;
245 adios (NULL
, "Internal error: algorithm %s", cp
);
250 case AUTOHEADERENCSW
:
251 header_encoding
= CE_UNKNOWN
;
255 if (!(cp
= *argp
++) || *cp
== '-')
256 adios (NULL
, "missing argument to %s", argp
[-2]);
257 if ((maxunencoded
= atoi(cp
)) < 1)
258 adios (NULL
, "Invalid argument for %s: %s", argp
[-2], cp
);
259 if (maxunencoded
> 998)
260 adios (NULL
, "limit of -maxunencoded is 998");
284 adios (NULL
, "only one composition file allowed");
290 * Check if we've specified an additional profile
292 if ((cp
= getenv ("MHBUILD"))) {
293 if ((fp
= fopen (cp
, "r"))) {
294 readconfig ((struct node
**) 0, fp
, cp
, 0);
297 admonish ("", "unable to read $MHBUILD profile (%s)", cp
);
302 * Read the standard profile setup
304 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
305 readconfig ((struct node
**) 0, fp
, cp
, 0);
309 /* Check for public cache location */
310 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
313 /* Check for private cache location */
314 if (!(cache_private
= context_find (nmhprivcache
)))
315 cache_private
= ".cache";
316 cache_private
= getcpy (m_maildir (cache_private
));
318 if (!context_find ("path"))
319 free (path ("./", TFOLDER
));
321 /* Check if we have a file to process */
323 adios (NULL
, "need to specify a %s composition file", invo_name
);
326 * Process the composition file from standard input.
328 if (compfile
[0] == '-' && compfile
[1] == '\0') {
329 if ((cp
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
330 adios(NULL
, "unable to create temporary file in %s",
334 /* save a copy of the name for later removal */
335 strncpy (infile
, cp
, sizeof(infile
));
338 /* copy standard input to temporary file */
339 while ((n
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0) {
340 if (fwrite(buffer
, 1, n
, fp
) != n
) {
341 adios (NULL
, "error copying to temporary file");
346 /* build the content structures for MIME message */
347 ct
= build_mime (infile
, autobuild
, dist
, directives
, header_encoding
,
348 maxunencoded
, verbosw
);
351 * If ct == NULL, that means that -auto was set and a MIME version
352 * header was already seen. Just use the input file as the output
356 if (! (fp
= fopen(infile
, "r"))) {
357 adios(NULL
, "Unable to open %s for reading", infile
);
359 while ((n
= fread(buffer
, 1, sizeof(buffer
), fp
)) > 0) {
360 if (fwrite(buffer
, 1, n
, stdout
) != n
) {
361 adios(NULL
, "error copying %s to stdout", infile
);
365 /* output the message */
366 output_message_fp (ct
, stdout
, NULL
);
374 * Process the composition file from a file.
377 /* build the content structures for MIME message */
378 ct
= build_mime (compfile
, autobuild
, dist
, directives
, header_encoding
,
379 maxunencoded
, verbosw
);
382 * If ct == NULL, that means -auto was set and we found a MIME version
383 * header. Simply exit and do nothing.
392 /* output MIME message to this temporary file */
393 if ((cp
= m_mktemp2(compfile
, invo_name
, NULL
, &fp_out
)) == NULL
) {
394 adios(NULL
, "unable to create temporary file in %s", get_temp_dir());
396 strncpy(outfile
, cp
, sizeof(outfile
));
399 /* output the message */
400 output_message_fp (ct
, fp_out
, outfile
);
404 * List the message info
407 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, dispo
);
409 /* Rename composition draft */
410 snprintf (buffer
, sizeof(buffer
), "%s.orig", m_backup (compfile
));
411 if (rename (compfile
, buffer
) == NOTOK
) {
412 adios (compfile
, "unable to rename comp draft %s to", buffer
);
415 /* Rename output file to take its place */
416 if (rename (outfile
, compfile
) == NOTOK
) {
417 advise (outfile
, "unable to rename output %s to", compfile
);
418 rename (buffer
, compfile
);
430 unlink_done (int status
)
433 * Check if we need to remove stray
437 (void) m_unlink (infile
);
439 (void) m_unlink (outfile
);