]>
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/getarguments.h"
10 #include "sbr/smatch.h"
11 #include "sbr/m_backup.h"
12 #include "sbr/context_find.h"
13 #include "sbr/readconfig.h"
14 #include "sbr/ambigsw.h"
16 #include "sbr/print_version.h"
17 #include "sbr/print_help.h"
18 #include "sbr/error.h"
24 #include "h/mhparse.h"
25 #include "h/mhcachesbr.h"
28 #include "sbr/m_maildir.h"
29 #include "sbr/m_mktemp.h"
33 #define MHBUILD_SWITCHES \
34 X("auto", 0, AUTOSW) \
35 X("noauto", 0, NAUTOSW) \
36 X("check", 0, CHECKSW) \
37 X("nocheck", 0, NCHECKSW) \
38 X("directives", 0, DIRECTIVES) \
39 X("nodirectives", 0, NDIRECTIVES) \
40 X("headers", 0, HEADSW) \
41 X("noheaders", 0, NHEADSW) \
42 X("list", 0, LISTSW) \
43 X("nolist", 0, NLISTSW) \
44 X("realsize", 0, SIZESW) \
45 X("norealsize", 0, NSIZESW) \
46 X("rfc934mode", 0, RFC934SW) \
47 X("norfc934mode", 0, NRFC934SW) \
48 X("verbose", 0, VERBSW) \
49 X("noverbose", 0, NVERBSW) \
50 X("disposition", 0, DISPOSW) \
51 X("nodisposition", 0, NDISPOSW) \
52 X("rcache policy", 0, RCACHESW) \
53 X("wcache policy", 0, WCACHESW) \
54 X("contentid", 0, CONTENTIDSW) \
55 X("nocontentid", 0, NCONTENTIDSW) \
56 X("headerencoding encoding-algorithm", 0, HEADERENCSW) \
57 X("autoheaderencoding", 0, AUTOHEADERENCSW) \
58 X("maxunencoded", 0, MAXUNENCSW) \
59 X("version", 0, VERSIONSW) \
60 X("help", 0, HELPSW) \
61 X("debug", -5, DEBUGSW) \
62 X("dist", 0, DISTSW) \
64 #define X(sw, minchars, id) id,
65 DEFINE_SWITCH_ENUM(MHBUILD
);
68 #define X(sw, minchars, id) { sw, minchars, id },
69 DEFINE_SWITCH_ARRAY(MHBUILD
, switches
);
72 /* utf-8 is for Email Address Internationalization, using SMTPUTF8. */
73 #define MIMEENCODING_SWITCHES \
74 X("base64", 0, BASE64SW) \
75 X("quoted-printable", 0, QUOTEDPRINTSW) \
76 X("utf-8", 0, UTF8SW) \
78 #define X(sw, minchars, id) id,
79 DEFINE_SWITCH_ENUM(MIMEENCODING
);
82 #define X(sw, minchars, id) { sw, minchars, id },
83 DEFINE_SWITCH_ARRAY(MIMEENCODING
, encodingswitches
);
90 bool contentidsw
= true;
95 static char infile
[BUFSIZ
];
96 static char outfile
[BUFSIZ
];
100 main (int argc
, char **argv
)
104 bool directives
= true;
105 bool autobuild
= false;
107 bool verbosw
= false;
109 size_t maxunencoded
= MAXTEXTPERLN
;
111 char *cp
, buf
[BUFSIZ
];
112 char buffer
[BUFSIZ
], *compfile
= NULL
;
113 char **argp
, **arguments
;
117 int header_encoding
= CE_UNKNOWN
;
120 if (nmh_init(argv
[0], true, false)) { return 1; }
122 arguments
= getarguments (invo_name
, argc
, argv
, 1);
125 while ((cp
= *argp
++)) {
126 if (cp
[0] == '-' && cp
[1] == '\0') {
128 die("cannot specify both standard input and a file");
130 listsw
= false; /* turn off -list if using standard in/out */
131 verbosw
= false; /* turn off -verbose listings */
135 switch (smatch (++cp
, switches
)) {
137 ambigsw (cp
, switches
);
140 die("-%s unknown", cp
);
143 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
144 print_help (buf
, switches
, 1);
147 print_version(invo_name
);
151 /* -auto implies -nodirectives */
157 * We're turning directives back on since this is likely here
158 * to override a profile entry
165 icachesw
= &rcachesw
;
168 icachesw
= &wcachesw
;
170 if (!(cp
= *argp
++) || *cp
== '-')
171 die("missing argument to %s", argp
[-2]);
172 switch (*icachesw
= smatch (cp
, cache_policy
)) {
174 ambigsw (cp
, cache_policy
);
177 die("%s unknown", cp
);
235 if (!(cp
= *argp
++) || *cp
== '-')
236 die("missing argument to %s", argp
[-2]);
237 switch (encoding
= smatch (cp
, encodingswitches
)) {
239 ambigsw (cp
, encodingswitches
);
242 die("%s unknown encoding algorithm", cp
);
244 header_encoding
= CE_BASE64
;
247 header_encoding
= CE_QUOTED
;
250 header_encoding
= CE_8BIT
;
253 die("Internal error: algorithm %s", cp
);
258 case AUTOHEADERENCSW
:
259 header_encoding
= CE_UNKNOWN
;
263 if (!(cp
= *argp
++) || *cp
== '-')
264 die("missing argument to %s", argp
[-2]);
265 if ((maxunencoded
= atoi(cp
)) < 1)
266 die("Invalid argument for %s: %s", argp
[-2], cp
);
267 if (maxunencoded
> 998)
268 die("limit of -maxunencoded is 998");
292 die("only one composition file allowed");
297 * Check if we've specified an additional profile
299 if ((cp
= getenv ("MHBUILD"))) {
300 if ((fp
= fopen (cp
, "r"))) {
301 readconfig(NULL
, fp
, cp
, 0);
304 admonish ("", "unable to read $MHBUILD profile (%s)", cp
);
309 * Read the standard profile setup
311 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
312 readconfig(NULL
, fp
, cp
, 0);
316 /* Check for public cache location */
317 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
320 /* Check for private cache location */
321 if (!(cache_private
= context_find (nmhprivcache
)))
322 cache_private
= ".cache";
323 cache_private
= mh_xstrdup(m_maildir(cache_private
));
325 if (!context_find ("path"))
326 free (path ("./", TFOLDER
));
328 /* Check if we have a file to process */
330 die("need to specify a %s composition file", invo_name
);
333 * Process the composition file from standard input.
335 if (compfile
[0] == '-' && compfile
[1] == '\0') {
336 if ((cp
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
337 die("unable to create temporary file in %s",
340 strncpy (infile
, cp
, sizeof(infile
));
342 /* copy standard input to temporary file */
343 while ((n
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0) {
344 if (fwrite(buffer
, 1, n
, fp
) != n
) {
345 die("error copying to temporary file");
350 /* build the content structures for MIME message */
351 ct
= build_mime (infile
, autobuild
, dist
, directives
, header_encoding
,
352 maxunencoded
, verbosw
);
355 * If ct == NULL, that means that -auto was set and a MIME version
356 * header was already seen. Just use the input file as the output
360 if (! (fp
= fopen(infile
, "r"))) {
361 die("Unable to open %s for reading", infile
);
363 while ((n
= fread(buffer
, 1, sizeof(buffer
), fp
)) > 0) {
364 if (fwrite(buffer
, 1, n
, stdout
) != n
) {
365 die("error copying %s to stdout", infile
);
369 /* output the message */
370 output_message_fp (ct
, stdout
, NULL
);
378 * Process the composition file from a file.
381 /* build the content structures for MIME message */
382 ct
= build_mime (compfile
, autobuild
, dist
, directives
, header_encoding
,
383 maxunencoded
, verbosw
);
386 * If ct == NULL, that means -auto was set and we found a MIME version
387 * header. Simply exit and do nothing.
396 /* output MIME message to this temporary file */
397 if ((cp
= m_mktemp2(compfile
, invo_name
, NULL
, &fp_out
)) == NULL
) {
398 die("unable to create temporary file");
400 strncpy(outfile
, cp
, sizeof(outfile
));
402 /* output the message */
403 output_message_fp (ct
, fp_out
, outfile
);
407 * List the message info
410 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, dispo
);
412 /* Rename composition draft */
413 snprintf (buffer
, sizeof(buffer
), "%s.orig", m_backup (compfile
));
414 if (rename (compfile
, buffer
) == NOTOK
) {
415 adios (compfile
, "unable to rename comp draft %s to", buffer
);
418 /* Rename output file to take its place */
419 if (rename (outfile
, compfile
) == NOTOK
) {
420 advise (outfile
, "unable to rename output %s to", compfile
);
421 rename (buffer
, compfile
);
424 /* Remove from atexit(3) list of files to unlink. */
425 if (!(m_unlink(outfile
) == -1 && errno
== ENOENT
)) {
426 adios(outfile
, "file exists after rename:");