]>
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.
9 #include "sbr/smatch.h"
10 #include "sbr/m_backup.h"
11 #include "sbr/context_find.h"
12 #include "sbr/readconfig.h"
13 #include "sbr/ambigsw.h"
15 #include "sbr/print_version.h"
16 #include "sbr/print_help.h"
17 #include "sbr/error.h"
23 #include "h/mhparse.h"
24 #include "h/mhcachesbr.h"
27 #include "sbr/m_maildir.h"
28 #include "sbr/m_mktemp.h"
32 #define MHBUILD_SWITCHES \
33 X("auto", 0, AUTOSW) \
34 X("noauto", 0, NAUTOSW) \
35 X("check", 0, CHECKSW) \
36 X("nocheck", 0, NCHECKSW) \
37 X("directives", 0, DIRECTIVES) \
38 X("nodirectives", 0, NDIRECTIVES) \
39 X("headers", 0, HEADSW) \
40 X("noheaders", 0, NHEADSW) \
41 X("list", 0, LISTSW) \
42 X("nolist", 0, NLISTSW) \
43 X("realsize", 0, SIZESW) \
44 X("norealsize", 0, NSIZESW) \
45 X("rfc934mode", 0, RFC934SW) \
46 X("norfc934mode", 0, NRFC934SW) \
47 X("verbose", 0, VERBSW) \
48 X("noverbose", 0, NVERBSW) \
49 X("disposition", 0, DISPOSW) \
50 X("nodisposition", 0, NDISPOSW) \
51 X("rcache policy", 0, RCACHESW) \
52 X("wcache policy", 0, WCACHESW) \
53 X("contentid", 0, CONTENTIDSW) \
54 X("nocontentid", 0, NCONTENTIDSW) \
55 X("headerencoding encoding-algorithm", 0, HEADERENCSW) \
56 X("autoheaderencoding", 0, AUTOHEADERENCSW) \
57 X("maxunencoded", 0, MAXUNENCSW) \
58 X("version", 0, VERSIONSW) \
59 X("help", 0, HELPSW) \
60 X("debug", -5, DEBUGSW) \
61 X("dist", 0, DISTSW) \
63 #define X(sw, minchars, id) id,
64 DEFINE_SWITCH_ENUM(MHBUILD
);
67 #define X(sw, minchars, id) { sw, minchars, id },
68 DEFINE_SWITCH_ARRAY(MHBUILD
, switches
);
71 /* utf-8 is for Email Address Internationalization, using SMTPUTF8. */
72 #define MIMEENCODING_SWITCHES \
73 X("base64", 0, BASE64SW) \
74 X("quoted-printable", 0, QUOTEDPRINTSW) \
75 X("utf-8", 0, UTF8SW) \
77 #define X(sw, minchars, id) id,
78 DEFINE_SWITCH_ENUM(MIMEENCODING
);
81 #define X(sw, minchars, id) { sw, minchars, id },
82 DEFINE_SWITCH_ARRAY(MIMEENCODING
, encodingswitches
);
89 bool contentidsw
= true;
94 static char infile
[BUFSIZ
];
95 static char outfile
[BUFSIZ
];
99 main (int argc
, char **argv
)
103 bool directives
= true;
104 bool autobuild
= false;
106 bool verbosw
= false;
108 size_t maxunencoded
= MAXTEXTPERLN
;
110 char *cp
, buf
[BUFSIZ
];
111 char buffer
[BUFSIZ
], *compfile
= NULL
;
112 char **argp
, **arguments
;
116 int header_encoding
= CE_UNKNOWN
;
119 if (nmh_init(argv
[0], true, false)) { return 1; }
121 arguments
= getarguments (invo_name
, argc
, argv
, 1);
124 while ((cp
= *argp
++)) {
125 if (cp
[0] == '-' && cp
[1] == '\0') {
127 die("cannot specify both standard input and a file");
129 listsw
= false; /* turn off -list if using standard in/out */
130 verbosw
= false; /* turn off -verbose listings */
134 switch (smatch (++cp
, switches
)) {
136 ambigsw (cp
, switches
);
139 die("-%s unknown", cp
);
142 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
143 print_help (buf
, switches
, 1);
146 print_version(invo_name
);
150 /* -auto implies -nodirectives */
156 * We're turning directives back on since this is likely here
157 * to override a profile entry
164 icachesw
= &rcachesw
;
167 icachesw
= &wcachesw
;
169 if (!(cp
= *argp
++) || *cp
== '-')
170 die("missing argument to %s", argp
[-2]);
171 switch (*icachesw
= smatch (cp
, cache_policy
)) {
173 ambigsw (cp
, cache_policy
);
176 die("%s unknown", cp
);
234 if (!(cp
= *argp
++) || *cp
== '-')
235 die("missing argument to %s", argp
[-2]);
236 switch (encoding
= smatch (cp
, encodingswitches
)) {
238 ambigsw (cp
, encodingswitches
);
241 die("%s unknown encoding algorithm", cp
);
243 header_encoding
= CE_BASE64
;
246 header_encoding
= CE_QUOTED
;
249 header_encoding
= CE_8BIT
;
252 die("Internal error: algorithm %s", cp
);
257 case AUTOHEADERENCSW
:
258 header_encoding
= CE_UNKNOWN
;
262 if (!(cp
= *argp
++) || *cp
== '-')
263 die("missing argument to %s", argp
[-2]);
264 if ((maxunencoded
= atoi(cp
)) < 1)
265 die("Invalid argument for %s: %s", argp
[-2], cp
);
266 if (maxunencoded
> 998)
267 die("limit of -maxunencoded is 998");
291 die("only one composition file allowed");
296 * Check if we've specified an additional profile
298 if ((cp
= getenv ("MHBUILD"))) {
299 if ((fp
= fopen (cp
, "r"))) {
300 readconfig(NULL
, fp
, cp
, 0);
303 admonish ("", "unable to read $MHBUILD profile (%s)", cp
);
308 * Read the standard profile setup
310 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
311 readconfig(NULL
, fp
, cp
, 0);
315 /* Check for public cache location */
316 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
319 /* Check for private cache location */
320 if (!(cache_private
= context_find (nmhprivcache
)))
321 cache_private
= ".cache";
322 cache_private
= mh_xstrdup(m_maildir(cache_private
));
324 if (!context_find ("path"))
325 free (path ("./", TFOLDER
));
327 /* Check if we have a file to process */
329 die("need to specify a %s composition file", invo_name
);
332 * Process the composition file from standard input.
334 if (compfile
[0] == '-' && compfile
[1] == '\0') {
335 if ((cp
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
336 die("unable to create temporary file in %s",
339 strncpy (infile
, cp
, sizeof(infile
));
341 /* copy standard input to temporary file */
342 while ((n
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0) {
343 if (fwrite(buffer
, 1, n
, fp
) != n
) {
344 die("error copying to temporary file");
349 /* build the content structures for MIME message */
350 ct
= build_mime (infile
, autobuild
, dist
, directives
, header_encoding
,
351 maxunencoded
, verbosw
);
354 * If ct == NULL, that means that -auto was set and a MIME version
355 * header was already seen. Just use the input file as the output
359 if (! (fp
= fopen(infile
, "r"))) {
360 die("Unable to open %s for reading", infile
);
362 while ((n
= fread(buffer
, 1, sizeof(buffer
), fp
)) > 0) {
363 if (fwrite(buffer
, 1, n
, stdout
) != n
) {
364 die("error copying %s to stdout", infile
);
368 /* output the message */
369 output_message_fp (ct
, stdout
, NULL
);
377 * Process the composition file from a file.
380 /* build the content structures for MIME message */
381 ct
= build_mime (compfile
, autobuild
, dist
, directives
, header_encoding
,
382 maxunencoded
, verbosw
);
385 * If ct == NULL, that means -auto was set and we found a MIME version
386 * header. Simply exit and do nothing.
395 /* output MIME message to this temporary file */
396 if ((cp
= m_mktemp2(compfile
, invo_name
, NULL
, &fp_out
)) == NULL
) {
397 die("unable to create temporary file");
399 strncpy(outfile
, cp
, sizeof(outfile
));
401 /* output the message */
402 output_message_fp (ct
, fp_out
, outfile
);
406 * List the message info
409 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, dispo
);
411 /* Rename composition draft */
412 snprintf (buffer
, sizeof(buffer
), "%s.orig", m_backup (compfile
));
413 if (rename (compfile
, buffer
) == NOTOK
) {
414 adios (compfile
, "unable to rename comp draft %s to", buffer
);
417 /* Rename output file to take its place */
418 if (rename (outfile
, compfile
) == NOTOK
) {
419 advise (outfile
, "unable to rename output %s to", compfile
);
420 rename (buffer
, compfile
);
423 /* Remove from atexit(3) list of files to unlink. */
424 if (!(m_unlink(outfile
) == -1 && errno
== ENOENT
)) {
425 adios(outfile
, "file exists after rename:");