]>
diplodocus.org Git - nmh/blob - uip/mhbuild.c
4a042e931de35e0fcaa1542e75546df7c9ac143d
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.
15 #include <h/mhparse.h>
16 #include <h/mhcachesbr.h>
19 #include "sbr/m_maildir.h"
20 #include "sbr/m_mktemp.h"
24 #define MHBUILD_SWITCHES \
25 X("auto", 0, AUTOSW) \
26 X("noauto", 0, NAUTOSW) \
27 X("check", 0, CHECKSW) \
28 X("nocheck", 0, NCHECKSW) \
29 X("directives", 0, DIRECTIVES) \
30 X("nodirectives", 0, NDIRECTIVES) \
31 X("headers", 0, HEADSW) \
32 X("noheaders", 0, NHEADSW) \
33 X("list", 0, LISTSW) \
34 X("nolist", 0, NLISTSW) \
35 X("realsize", 0, SIZESW) \
36 X("norealsize", 0, NSIZESW) \
37 X("rfc934mode", 0, RFC934SW) \
38 X("norfc934mode", 0, NRFC934SW) \
39 X("verbose", 0, VERBSW) \
40 X("noverbose", 0, NVERBSW) \
41 X("disposition", 0, DISPOSW) \
42 X("nodisposition", 0, NDISPOSW) \
43 X("rcache policy", 0, RCACHESW) \
44 X("wcache policy", 0, WCACHESW) \
45 X("contentid", 0, CONTENTIDSW) \
46 X("nocontentid", 0, NCONTENTIDSW) \
47 X("headerencoding encoding-algorithm", 0, HEADERENCSW) \
48 X("autoheaderencoding", 0, AUTOHEADERENCSW) \
49 X("maxunencoded", 0, MAXUNENCSW) \
50 X("version", 0, VERSIONSW) \
51 X("help", 0, HELPSW) \
52 X("debug", -5, DEBUGSW) \
53 X("dist", 0, DISTSW) \
55 #define X(sw, minchars, id) id,
56 DEFINE_SWITCH_ENUM(MHBUILD
);
59 #define X(sw, minchars, id) { sw, minchars, id },
60 DEFINE_SWITCH_ARRAY(MHBUILD
, switches
);
63 /* utf-8 is for Email Address Internationalization, using SMTPUTF8. */
64 #define MIMEENCODING_SWITCHES \
65 X("base64", 0, BASE64SW) \
66 X("quoted-printable", 0, QUOTEDPRINTSW) \
67 X("utf-8", 0, UTF8SW) \
69 #define X(sw, minchars, id) id,
70 DEFINE_SWITCH_ENUM(MIMEENCODING
);
73 #define X(sw, minchars, id) { sw, minchars, id },
74 DEFINE_SWITCH_ARRAY(MIMEENCODING
, encodingswitches
);
81 bool contentidsw
= true;
86 static char infile
[BUFSIZ
];
87 static char outfile
[BUFSIZ
];
91 main (int argc
, char **argv
)
95 bool directives
= true;
96 bool autobuild
= false;
100 size_t maxunencoded
= MAXTEXTPERLN
;
102 char *cp
, buf
[BUFSIZ
];
103 char buffer
[BUFSIZ
], *compfile
= NULL
;
104 char **argp
, **arguments
;
108 int header_encoding
= CE_UNKNOWN
;
111 if (nmh_init(argv
[0], true, false)) { return 1; }
113 arguments
= getarguments (invo_name
, argc
, argv
, 1);
116 while ((cp
= *argp
++)) {
117 if (cp
[0] == '-' && cp
[1] == '\0') {
119 die("cannot specify both standard input and a file");
121 listsw
= false; /* turn off -list if using standard in/out */
122 verbosw
= false; /* turn off -verbose listings */
126 switch (smatch (++cp
, switches
)) {
128 ambigsw (cp
, switches
);
131 die("-%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 die("missing argument to %s", argp
[-2]);
163 switch (*icachesw
= smatch (cp
, cache_policy
)) {
165 ambigsw (cp
, cache_policy
);
168 die("%s unknown", cp
);
226 if (!(cp
= *argp
++) || *cp
== '-')
227 die("missing argument to %s", argp
[-2]);
228 switch (encoding
= smatch (cp
, encodingswitches
)) {
230 ambigsw (cp
, encodingswitches
);
233 die("%s unknown encoding algorithm", cp
);
235 header_encoding
= CE_BASE64
;
238 header_encoding
= CE_QUOTED
;
241 header_encoding
= CE_8BIT
;
244 die("Internal error: algorithm %s", cp
);
249 case AUTOHEADERENCSW
:
250 header_encoding
= CE_UNKNOWN
;
254 if (!(cp
= *argp
++) || *cp
== '-')
255 die("missing argument to %s", argp
[-2]);
256 if ((maxunencoded
= atoi(cp
)) < 1)
257 die("Invalid argument for %s: %s", argp
[-2], cp
);
258 if (maxunencoded
> 998)
259 die("limit of -maxunencoded is 998");
283 die("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
= mh_xstrdup(m_maildir(cache_private
));
316 if (!context_find ("path"))
317 free (path ("./", TFOLDER
));
319 /* Check if we have a file to process */
321 die("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 die("unable to create temporary file in %s",
331 strncpy (infile
, cp
, sizeof(infile
));
333 /* copy standard input to temporary file */
334 while ((n
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0) {
335 if (fwrite(buffer
, 1, n
, fp
) != n
) {
336 die("error copying to temporary file");
341 /* build the content structures for MIME message */
342 ct
= build_mime (infile
, autobuild
, dist
, directives
, header_encoding
,
343 maxunencoded
, verbosw
);
346 * If ct == NULL, that means that -auto was set and a MIME version
347 * header was already seen. Just use the input file as the output
351 if (! (fp
= fopen(infile
, "r"))) {
352 die("Unable to open %s for reading", infile
);
354 while ((n
= fread(buffer
, 1, sizeof(buffer
), fp
)) > 0) {
355 if (fwrite(buffer
, 1, n
, stdout
) != n
) {
356 die("error copying %s to stdout", infile
);
360 /* output the message */
361 output_message_fp (ct
, stdout
, NULL
);
369 * Process the composition file from a file.
372 /* build the content structures for MIME message */
373 ct
= build_mime (compfile
, autobuild
, dist
, directives
, header_encoding
,
374 maxunencoded
, verbosw
);
377 * If ct == NULL, that means -auto was set and we found a MIME version
378 * header. Simply exit and do nothing.
387 /* output MIME message to this temporary file */
388 if ((cp
= m_mktemp2(compfile
, invo_name
, NULL
, &fp_out
)) == NULL
) {
389 die("unable to create temporary file");
391 strncpy(outfile
, cp
, sizeof(outfile
));
393 /* output the message */
394 output_message_fp (ct
, fp_out
, outfile
);
398 * List the message info
401 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, dispo
);
403 /* Rename composition draft */
404 snprintf (buffer
, sizeof(buffer
), "%s.orig", m_backup (compfile
));
405 if (rename (compfile
, buffer
) == NOTOK
) {
406 adios (compfile
, "unable to rename comp draft %s to", buffer
);
409 /* Rename output file to take its place */
410 if (rename (outfile
, compfile
) == NOTOK
) {
411 advise (outfile
, "unable to rename output %s to", compfile
);
412 rename (buffer
, compfile
);
415 /* Remove from atexit(3) list of files to unlink. */
416 if (!(m_unlink(outfile
) == -1 && errno
== ENOENT
)) {
417 adios(outfile
, "file exists after rename:");