]>
diplodocus.org Git - nmh/blob - uip/mhbuild.c
711f26ccf917e8eae4b1de6911f1d0a456a866b0
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/ambigsw.h"
11 #include "sbr/print_version.h"
12 #include "sbr/print_help.h"
13 #include "sbr/error.h"
19 #include "h/mhparse.h"
20 #include "h/mhcachesbr.h"
23 #include "sbr/m_maildir.h"
24 #include "sbr/m_mktemp.h"
28 #define MHBUILD_SWITCHES \
29 X("auto", 0, AUTOSW) \
30 X("noauto", 0, NAUTOSW) \
31 X("check", 0, CHECKSW) \
32 X("nocheck", 0, NCHECKSW) \
33 X("directives", 0, DIRECTIVES) \
34 X("nodirectives", 0, NDIRECTIVES) \
35 X("headers", 0, HEADSW) \
36 X("noheaders", 0, NHEADSW) \
37 X("list", 0, LISTSW) \
38 X("nolist", 0, NLISTSW) \
39 X("realsize", 0, SIZESW) \
40 X("norealsize", 0, NSIZESW) \
41 X("rfc934mode", 0, RFC934SW) \
42 X("norfc934mode", 0, NRFC934SW) \
43 X("verbose", 0, VERBSW) \
44 X("noverbose", 0, NVERBSW) \
45 X("disposition", 0, DISPOSW) \
46 X("nodisposition", 0, NDISPOSW) \
47 X("rcache policy", 0, RCACHESW) \
48 X("wcache policy", 0, WCACHESW) \
49 X("contentid", 0, CONTENTIDSW) \
50 X("nocontentid", 0, NCONTENTIDSW) \
51 X("headerencoding encoding-algorithm", 0, HEADERENCSW) \
52 X("autoheaderencoding", 0, AUTOHEADERENCSW) \
53 X("maxunencoded", 0, MAXUNENCSW) \
54 X("version", 0, VERSIONSW) \
55 X("help", 0, HELPSW) \
56 X("debug", -5, DEBUGSW) \
57 X("dist", 0, DISTSW) \
59 #define X(sw, minchars, id) id,
60 DEFINE_SWITCH_ENUM(MHBUILD
);
63 #define X(sw, minchars, id) { sw, minchars, id },
64 DEFINE_SWITCH_ARRAY(MHBUILD
, switches
);
67 /* utf-8 is for Email Address Internationalization, using SMTPUTF8. */
68 #define MIMEENCODING_SWITCHES \
69 X("base64", 0, BASE64SW) \
70 X("quoted-printable", 0, QUOTEDPRINTSW) \
71 X("utf-8", 0, UTF8SW) \
73 #define X(sw, minchars, id) id,
74 DEFINE_SWITCH_ENUM(MIMEENCODING
);
77 #define X(sw, minchars, id) { sw, minchars, id },
78 DEFINE_SWITCH_ARRAY(MIMEENCODING
, encodingswitches
);
85 bool contentidsw
= true;
90 static char infile
[BUFSIZ
];
91 static char outfile
[BUFSIZ
];
95 main (int argc
, char **argv
)
99 bool directives
= true;
100 bool autobuild
= false;
102 bool verbosw
= false;
104 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
160 icachesw
= &rcachesw
;
163 icachesw
= &wcachesw
;
165 if (!(cp
= *argp
++) || *cp
== '-')
166 die("missing argument to %s", argp
[-2]);
167 switch (*icachesw
= smatch (cp
, cache_policy
)) {
169 ambigsw (cp
, cache_policy
);
172 die("%s unknown", cp
);
230 if (!(cp
= *argp
++) || *cp
== '-')
231 die("missing argument to %s", argp
[-2]);
232 switch (encoding
= smatch (cp
, encodingswitches
)) {
234 ambigsw (cp
, encodingswitches
);
237 die("%s unknown encoding algorithm", cp
);
239 header_encoding
= CE_BASE64
;
242 header_encoding
= CE_QUOTED
;
245 header_encoding
= CE_8BIT
;
248 die("Internal error: algorithm %s", cp
);
253 case AUTOHEADERENCSW
:
254 header_encoding
= CE_UNKNOWN
;
258 if (!(cp
= *argp
++) || *cp
== '-')
259 die("missing argument to %s", argp
[-2]);
260 if ((maxunencoded
= atoi(cp
)) < 1)
261 die("Invalid argument for %s: %s", argp
[-2], cp
);
262 if (maxunencoded
> 998)
263 die("limit of -maxunencoded is 998");
287 die("only one composition file allowed");
292 * Check if we've specified an additional profile
294 if ((cp
= getenv ("MHBUILD"))) {
295 if ((fp
= fopen (cp
, "r"))) {
296 readconfig(NULL
, fp
, cp
, 0);
299 admonish ("", "unable to read $MHBUILD profile (%s)", cp
);
304 * Read the standard profile setup
306 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
307 readconfig(NULL
, fp
, cp
, 0);
311 /* Check for public cache location */
312 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
315 /* Check for private cache location */
316 if (!(cache_private
= context_find (nmhprivcache
)))
317 cache_private
= ".cache";
318 cache_private
= mh_xstrdup(m_maildir(cache_private
));
320 if (!context_find ("path"))
321 free (path ("./", TFOLDER
));
323 /* Check if we have a file to process */
325 die("need to specify a %s composition file", invo_name
);
328 * Process the composition file from standard input.
330 if (compfile
[0] == '-' && compfile
[1] == '\0') {
331 if ((cp
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
332 die("unable to create temporary file in %s",
335 strncpy (infile
, cp
, sizeof(infile
));
337 /* copy standard input to temporary file */
338 while ((n
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0) {
339 if (fwrite(buffer
, 1, n
, fp
) != n
) {
340 die("error copying to temporary file");
345 /* build the content structures for MIME message */
346 ct
= build_mime (infile
, autobuild
, dist
, directives
, header_encoding
,
347 maxunencoded
, verbosw
);
350 * If ct == NULL, that means that -auto was set and a MIME version
351 * header was already seen. Just use the input file as the output
355 if (! (fp
= fopen(infile
, "r"))) {
356 die("Unable to open %s for reading", infile
);
358 while ((n
= fread(buffer
, 1, sizeof(buffer
), fp
)) > 0) {
359 if (fwrite(buffer
, 1, n
, stdout
) != n
) {
360 die("error copying %s to stdout", infile
);
364 /* output the message */
365 output_message_fp (ct
, stdout
, NULL
);
373 * Process the composition file from a file.
376 /* build the content structures for MIME message */
377 ct
= build_mime (compfile
, autobuild
, dist
, directives
, header_encoding
,
378 maxunencoded
, verbosw
);
381 * If ct == NULL, that means -auto was set and we found a MIME version
382 * header. Simply exit and do nothing.
391 /* output MIME message to this temporary file */
392 if ((cp
= m_mktemp2(compfile
, invo_name
, NULL
, &fp_out
)) == NULL
) {
393 die("unable to create temporary file");
395 strncpy(outfile
, cp
, sizeof(outfile
));
397 /* output the message */
398 output_message_fp (ct
, fp_out
, outfile
);
402 * List the message info
405 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, dispo
);
407 /* Rename composition draft */
408 snprintf (buffer
, sizeof(buffer
), "%s.orig", m_backup (compfile
));
409 if (rename (compfile
, buffer
) == NOTOK
) {
410 adios (compfile
, "unable to rename comp draft %s to", buffer
);
413 /* Rename output file to take its place */
414 if (rename (outfile
, compfile
) == NOTOK
) {
415 advise (outfile
, "unable to rename output %s to", compfile
);
416 rename (buffer
, compfile
);
419 /* Remove from atexit(3) list of files to unlink. */
420 if (!(m_unlink(outfile
) == -1 && errno
== ENOENT
)) {
421 adios(outfile
, "file exists after rename:");