]>
diplodocus.org Git - nmh/blob - uip/mhbuild.c
34a797e478bfdff625b90a3640edbf0ffa248efb
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"
23 #include "h/mhparse.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", -5, CHECKSW) \
35 X("nocheck", -7, 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("contentid", 0, CONTENTIDSW) \
51 X("nocontentid", 0, NCONTENTIDSW) \
52 X("headerencoding encoding-algorithm", 0, HEADERENCSW) \
53 X("autoheaderencoding", 0, AUTOHEADERENCSW) \
54 X("maxunencoded", 0, MAXUNENCSW) \
55 X("version", 0, VERSIONSW) \
56 X("help", 0, HELPSW) \
57 X("debug", -5, DEBUGSW) \
58 X("dist", 0, DISTSW) \
60 #define X(sw, minchars, id) id,
61 DEFINE_SWITCH_ENUM(MHBUILD
);
64 #define X(sw, minchars, id) { sw, minchars, id },
65 DEFINE_SWITCH_ARRAY(MHBUILD
, switches
);
68 /* utf-8 is for Email Address Internationalization, using SMTPUTF8. */
69 #define MIMEENCODING_SWITCHES \
70 X("base64", 0, BASE64SW) \
71 X("quoted-printable", 0, QUOTEDPRINTSW) \
72 X("utf-8", 0, UTF8SW) \
74 #define X(sw, minchars, id) id,
75 DEFINE_SWITCH_ENUM(MIMEENCODING
);
78 #define X(sw, minchars, id) { sw, minchars, id },
79 DEFINE_SWITCH_ARRAY(MIMEENCODING
, encodingswitches
);
86 bool contentidsw
= true;
91 static char infile
[BUFSIZ
];
92 static char outfile
[BUFSIZ
];
96 main (int argc
, char **argv
)
100 bool directives
= true;
101 bool autobuild
= false;
103 bool verbosw
= false;
105 size_t maxunencoded
= MAXTEXTPERLN
;
106 char *cp
, buf
[BUFSIZ
];
107 char buffer
[BUFSIZ
], *compfile
= NULL
;
108 char **argp
, **arguments
;
112 int header_encoding
= CE_UNKNOWN
;
115 if (nmh_init(argv
[0], true, false)) { return 1; }
117 arguments
= getarguments (invo_name
, argc
, argv
, 1);
120 while ((cp
= *argp
++)) {
121 if (cp
[0] == '-' && cp
[1] == '\0') {
123 die("cannot specify both standard input and a file");
125 listsw
= false; /* turn off -list if using standard in/out */
126 verbosw
= false; /* turn off -verbose listings */
130 switch (smatch (++cp
, switches
)) {
132 ambigsw (cp
, switches
);
135 die("-%s unknown", cp
);
138 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
139 print_help (buf
, switches
, 1);
142 print_version(invo_name
);
146 /* -auto implies -nodirectives */
152 * We're turning directives back on since this is likely here
153 * to override a profile entry
161 /* Currently a NOP */
209 if (!(cp
= *argp
++) || *cp
== '-')
210 die("missing argument to %s", argp
[-2]);
211 switch (encoding
= smatch (cp
, encodingswitches
)) {
213 ambigsw (cp
, encodingswitches
);
216 die("%s unknown encoding algorithm", cp
);
218 header_encoding
= CE_BASE64
;
221 header_encoding
= CE_QUOTED
;
224 header_encoding
= CE_8BIT
;
227 die("Internal error: algorithm %s", cp
);
232 case AUTOHEADERENCSW
:
233 header_encoding
= CE_UNKNOWN
;
237 if (!(cp
= *argp
++) || *cp
== '-')
238 die("missing argument to %s", argp
[-2]);
239 if ((maxunencoded
= atoi(cp
)) < 1)
240 die("Invalid argument for %s: %s", argp
[-2], cp
);
241 if (maxunencoded
> 998)
242 die("limit of -maxunencoded is 998");
266 die("only one composition file allowed");
271 * Check if we've specified an additional profile
273 if ((cp
= getenv ("MHBUILD"))) {
274 if ((fp
= fopen (cp
, "r"))) {
275 readconfig(NULL
, fp
, cp
, 0);
278 admonish ("", "unable to read $MHBUILD profile (%s)", cp
);
283 * Read the standard profile setup
285 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
286 readconfig(NULL
, fp
, cp
, 0);
290 if (!context_find ("path"))
291 free (path ("./", TFOLDER
));
293 /* Check if we have a file to process */
295 die("need to specify a %s composition file", invo_name
);
298 * Process the composition file from standard input.
300 if (compfile
[0] == '-' && compfile
[1] == '\0') {
301 if ((cp
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
302 die("unable to create temporary file in %s",
305 strncpy (infile
, cp
, sizeof(infile
));
307 /* copy standard input to temporary file */
308 while ((n
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0) {
309 if (fwrite(buffer
, 1, n
, fp
) != n
) {
310 die("error copying to temporary file");
315 /* build the content structures for MIME message */
316 ct
= build_mime (infile
, autobuild
, dist
, directives
, header_encoding
,
317 maxunencoded
, verbosw
);
320 * If ct == NULL, that means that -auto was set and a MIME version
321 * header was already seen. Just use the input file as the output
325 if (! (fp
= fopen(infile
, "r"))) {
326 die("Unable to open %s for reading", infile
);
328 while ((n
= fread(buffer
, 1, sizeof(buffer
), fp
)) > 0) {
329 if (fwrite(buffer
, 1, n
, stdout
) != n
) {
330 die("error copying %s to stdout", infile
);
334 /* output the message */
335 output_message_fp (ct
, stdout
, NULL
);
343 * Process the composition file from a file.
346 /* build the content structures for MIME message */
347 ct
= build_mime (compfile
, autobuild
, dist
, directives
, header_encoding
,
348 maxunencoded
, verbosw
);
351 * If ct == NULL, that means -auto was set and we found a MIME version
352 * header. Simply exit and do nothing.
361 /* output MIME message to this temporary file */
362 if ((cp
= m_mktemp2(compfile
, invo_name
, NULL
, &fp_out
)) == NULL
) {
363 die("unable to create temporary file");
365 strncpy(outfile
, cp
, sizeof(outfile
));
367 /* output the message */
368 output_message_fp (ct
, fp_out
, outfile
);
372 * List the message info
375 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, dispo
);
377 /* Rename composition draft */
378 snprintf (buffer
, sizeof(buffer
), "%s.orig", m_backup (compfile
));
379 if (rename (compfile
, buffer
) == NOTOK
) {
380 adios (compfile
, "unable to rename comp draft %s to", buffer
);
383 /* Rename output file to take its place */
384 if (rename (outfile
, compfile
) == NOTOK
) {
385 advise (outfile
, "unable to rename output %s to", compfile
);
386 rename (buffer
, compfile
);
389 /* Remove from atexit(3) list of files to unlink. */
390 if (!(m_unlink(outfile
) == -1 && errno
== ENOENT
)) {
391 adios(outfile
, "file exists after rename:");