]>
diplodocus.org Git - nmh/blob - uip/mhbuild.c
7fcc0efd51cd395eba1ad3de36a3d7f535c5bcf8
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/m_backup.h"
10 #include "sbr/context_find.h"
11 #include "sbr/readconfig.h"
12 #include "sbr/ambigsw.h"
14 #include "sbr/print_version.h"
15 #include "sbr/print_help.h"
16 #include "sbr/error.h"
22 #include "h/mhparse.h"
23 #include "h/mhcachesbr.h"
26 #include "sbr/m_maildir.h"
27 #include "sbr/m_mktemp.h"
31 #define MHBUILD_SWITCHES \
32 X("auto", 0, AUTOSW) \
33 X("noauto", 0, NAUTOSW) \
34 X("check", 0, CHECKSW) \
35 X("nocheck", 0, NCHECKSW) \
36 X("directives", 0, DIRECTIVES) \
37 X("nodirectives", 0, NDIRECTIVES) \
38 X("headers", 0, HEADSW) \
39 X("noheaders", 0, NHEADSW) \
40 X("list", 0, LISTSW) \
41 X("nolist", 0, NLISTSW) \
42 X("realsize", 0, SIZESW) \
43 X("norealsize", 0, NSIZESW) \
44 X("rfc934mode", 0, RFC934SW) \
45 X("norfc934mode", 0, NRFC934SW) \
46 X("verbose", 0, VERBSW) \
47 X("noverbose", 0, NVERBSW) \
48 X("disposition", 0, DISPOSW) \
49 X("nodisposition", 0, NDISPOSW) \
50 X("rcache policy", 0, RCACHESW) \
51 X("wcache policy", 0, WCACHESW) \
52 X("contentid", 0, CONTENTIDSW) \
53 X("nocontentid", 0, NCONTENTIDSW) \
54 X("headerencoding encoding-algorithm", 0, HEADERENCSW) \
55 X("autoheaderencoding", 0, AUTOHEADERENCSW) \
56 X("maxunencoded", 0, MAXUNENCSW) \
57 X("version", 0, VERSIONSW) \
58 X("help", 0, HELPSW) \
59 X("debug", -5, DEBUGSW) \
60 X("dist", 0, DISTSW) \
62 #define X(sw, minchars, id) id,
63 DEFINE_SWITCH_ENUM(MHBUILD
);
66 #define X(sw, minchars, id) { sw, minchars, id },
67 DEFINE_SWITCH_ARRAY(MHBUILD
, switches
);
70 /* utf-8 is for Email Address Internationalization, using SMTPUTF8. */
71 #define MIMEENCODING_SWITCHES \
72 X("base64", 0, BASE64SW) \
73 X("quoted-printable", 0, QUOTEDPRINTSW) \
74 X("utf-8", 0, UTF8SW) \
76 #define X(sw, minchars, id) id,
77 DEFINE_SWITCH_ENUM(MIMEENCODING
);
80 #define X(sw, minchars, id) { sw, minchars, id },
81 DEFINE_SWITCH_ARRAY(MIMEENCODING
, encodingswitches
);
88 bool contentidsw
= true;
93 static char infile
[BUFSIZ
];
94 static char outfile
[BUFSIZ
];
98 main (int argc
, char **argv
)
102 bool directives
= true;
103 bool autobuild
= false;
105 bool verbosw
= false;
107 size_t maxunencoded
= MAXTEXTPERLN
;
109 char *cp
, buf
[BUFSIZ
];
110 char buffer
[BUFSIZ
], *compfile
= NULL
;
111 char **argp
, **arguments
;
115 int header_encoding
= CE_UNKNOWN
;
118 if (nmh_init(argv
[0], true, false)) { return 1; }
120 arguments
= getarguments (invo_name
, argc
, argv
, 1);
123 while ((cp
= *argp
++)) {
124 if (cp
[0] == '-' && cp
[1] == '\0') {
126 die("cannot specify both standard input and a file");
128 listsw
= false; /* turn off -list if using standard in/out */
129 verbosw
= false; /* turn off -verbose listings */
133 switch (smatch (++cp
, switches
)) {
135 ambigsw (cp
, switches
);
138 die("-%s unknown", cp
);
141 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
142 print_help (buf
, switches
, 1);
145 print_version(invo_name
);
149 /* -auto implies -nodirectives */
155 * We're turning directives back on since this is likely here
156 * to override a profile entry
163 icachesw
= &rcachesw
;
166 icachesw
= &wcachesw
;
168 if (!(cp
= *argp
++) || *cp
== '-')
169 die("missing argument to %s", argp
[-2]);
170 switch (*icachesw
= smatch (cp
, cache_policy
)) {
172 ambigsw (cp
, cache_policy
);
175 die("%s unknown", cp
);
233 if (!(cp
= *argp
++) || *cp
== '-')
234 die("missing argument to %s", argp
[-2]);
235 switch (encoding
= smatch (cp
, encodingswitches
)) {
237 ambigsw (cp
, encodingswitches
);
240 die("%s unknown encoding algorithm", cp
);
242 header_encoding
= CE_BASE64
;
245 header_encoding
= CE_QUOTED
;
248 header_encoding
= CE_8BIT
;
251 die("Internal error: algorithm %s", cp
);
256 case AUTOHEADERENCSW
:
257 header_encoding
= CE_UNKNOWN
;
261 if (!(cp
= *argp
++) || *cp
== '-')
262 die("missing argument to %s", argp
[-2]);
263 if ((maxunencoded
= atoi(cp
)) < 1)
264 die("Invalid argument for %s: %s", argp
[-2], cp
);
265 if (maxunencoded
> 998)
266 die("limit of -maxunencoded is 998");
290 die("only one composition file allowed");
295 * Check if we've specified an additional profile
297 if ((cp
= getenv ("MHBUILD"))) {
298 if ((fp
= fopen (cp
, "r"))) {
299 readconfig(NULL
, fp
, cp
, 0);
302 admonish ("", "unable to read $MHBUILD profile (%s)", cp
);
307 * Read the standard profile setup
309 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
310 readconfig(NULL
, fp
, cp
, 0);
314 /* Check for public cache location */
315 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
318 /* Check for private cache location */
319 if (!(cache_private
= context_find (nmhprivcache
)))
320 cache_private
= ".cache";
321 cache_private
= mh_xstrdup(m_maildir(cache_private
));
323 if (!context_find ("path"))
324 free (path ("./", TFOLDER
));
326 /* Check if we have a file to process */
328 die("need to specify a %s composition file", invo_name
);
331 * Process the composition file from standard input.
333 if (compfile
[0] == '-' && compfile
[1] == '\0') {
334 if ((cp
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
335 die("unable to create temporary file in %s",
338 strncpy (infile
, cp
, sizeof(infile
));
340 /* copy standard input to temporary file */
341 while ((n
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0) {
342 if (fwrite(buffer
, 1, n
, fp
) != n
) {
343 die("error copying to temporary file");
348 /* build the content structures for MIME message */
349 ct
= build_mime (infile
, autobuild
, dist
, directives
, header_encoding
,
350 maxunencoded
, verbosw
);
353 * If ct == NULL, that means that -auto was set and a MIME version
354 * header was already seen. Just use the input file as the output
358 if (! (fp
= fopen(infile
, "r"))) {
359 die("Unable to open %s for reading", infile
);
361 while ((n
= fread(buffer
, 1, sizeof(buffer
), fp
)) > 0) {
362 if (fwrite(buffer
, 1, n
, stdout
) != n
) {
363 die("error copying %s to stdout", infile
);
367 /* output the message */
368 output_message_fp (ct
, stdout
, NULL
);
376 * Process the composition file from a file.
379 /* build the content structures for MIME message */
380 ct
= build_mime (compfile
, autobuild
, dist
, directives
, header_encoding
,
381 maxunencoded
, verbosw
);
384 * If ct == NULL, that means -auto was set and we found a MIME version
385 * header. Simply exit and do nothing.
394 /* output MIME message to this temporary file */
395 if ((cp
= m_mktemp2(compfile
, invo_name
, NULL
, &fp_out
)) == NULL
) {
396 die("unable to create temporary file");
398 strncpy(outfile
, cp
, sizeof(outfile
));
400 /* output the message */
401 output_message_fp (ct
, fp_out
, outfile
);
405 * List the message info
408 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, dispo
);
410 /* Rename composition draft */
411 snprintf (buffer
, sizeof(buffer
), "%s.orig", m_backup (compfile
));
412 if (rename (compfile
, buffer
) == NOTOK
) {
413 adios (compfile
, "unable to rename comp draft %s to", buffer
);
416 /* Rename output file to take its place */
417 if (rename (outfile
, compfile
) == NOTOK
) {
418 advise (outfile
, "unable to rename output %s to", compfile
);
419 rename (buffer
, compfile
);
422 /* Remove from atexit(3) list of files to unlink. */
423 if (!(m_unlink(outfile
) == -1 && errno
== ENOENT
)) {
424 adios(outfile
, "file exists after rename:");