]>
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/context_find.h"
10 #include "sbr/readconfig.h"
11 #include "sbr/ambigsw.h"
13 #include "sbr/print_version.h"
14 #include "sbr/print_help.h"
15 #include "sbr/error.h"
21 #include "h/mhparse.h"
22 #include "h/mhcachesbr.h"
25 #include "sbr/m_maildir.h"
26 #include "sbr/m_mktemp.h"
30 #define MHBUILD_SWITCHES \
31 X("auto", 0, AUTOSW) \
32 X("noauto", 0, NAUTOSW) \
33 X("check", 0, CHECKSW) \
34 X("nocheck", 0, NCHECKSW) \
35 X("directives", 0, DIRECTIVES) \
36 X("nodirectives", 0, NDIRECTIVES) \
37 X("headers", 0, HEADSW) \
38 X("noheaders", 0, NHEADSW) \
39 X("list", 0, LISTSW) \
40 X("nolist", 0, NLISTSW) \
41 X("realsize", 0, SIZESW) \
42 X("norealsize", 0, NSIZESW) \
43 X("rfc934mode", 0, RFC934SW) \
44 X("norfc934mode", 0, NRFC934SW) \
45 X("verbose", 0, VERBSW) \
46 X("noverbose", 0, NVERBSW) \
47 X("disposition", 0, DISPOSW) \
48 X("nodisposition", 0, NDISPOSW) \
49 X("rcache policy", 0, RCACHESW) \
50 X("wcache policy", 0, WCACHESW) \
51 X("contentid", 0, CONTENTIDSW) \
52 X("nocontentid", 0, NCONTENTIDSW) \
53 X("headerencoding encoding-algorithm", 0, HEADERENCSW) \
54 X("autoheaderencoding", 0, AUTOHEADERENCSW) \
55 X("maxunencoded", 0, MAXUNENCSW) \
56 X("version", 0, VERSIONSW) \
57 X("help", 0, HELPSW) \
58 X("debug", -5, DEBUGSW) \
59 X("dist", 0, DISTSW) \
61 #define X(sw, minchars, id) id,
62 DEFINE_SWITCH_ENUM(MHBUILD
);
65 #define X(sw, minchars, id) { sw, minchars, id },
66 DEFINE_SWITCH_ARRAY(MHBUILD
, switches
);
69 /* utf-8 is for Email Address Internationalization, using SMTPUTF8. */
70 #define MIMEENCODING_SWITCHES \
71 X("base64", 0, BASE64SW) \
72 X("quoted-printable", 0, QUOTEDPRINTSW) \
73 X("utf-8", 0, UTF8SW) \
75 #define X(sw, minchars, id) id,
76 DEFINE_SWITCH_ENUM(MIMEENCODING
);
79 #define X(sw, minchars, id) { sw, minchars, id },
80 DEFINE_SWITCH_ARRAY(MIMEENCODING
, encodingswitches
);
87 bool contentidsw
= true;
92 static char infile
[BUFSIZ
];
93 static char outfile
[BUFSIZ
];
97 main (int argc
, char **argv
)
101 bool directives
= true;
102 bool autobuild
= false;
104 bool verbosw
= false;
106 size_t maxunencoded
= MAXTEXTPERLN
;
108 char *cp
, buf
[BUFSIZ
];
109 char buffer
[BUFSIZ
], *compfile
= NULL
;
110 char **argp
, **arguments
;
114 int header_encoding
= CE_UNKNOWN
;
117 if (nmh_init(argv
[0], true, false)) { return 1; }
119 arguments
= getarguments (invo_name
, argc
, argv
, 1);
122 while ((cp
= *argp
++)) {
123 if (cp
[0] == '-' && cp
[1] == '\0') {
125 die("cannot specify both standard input and a file");
127 listsw
= false; /* turn off -list if using standard in/out */
128 verbosw
= false; /* turn off -verbose listings */
132 switch (smatch (++cp
, switches
)) {
134 ambigsw (cp
, switches
);
137 die("-%s unknown", cp
);
140 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
141 print_help (buf
, switches
, 1);
144 print_version(invo_name
);
148 /* -auto implies -nodirectives */
154 * We're turning directives back on since this is likely here
155 * to override a profile entry
162 icachesw
= &rcachesw
;
165 icachesw
= &wcachesw
;
167 if (!(cp
= *argp
++) || *cp
== '-')
168 die("missing argument to %s", argp
[-2]);
169 switch (*icachesw
= smatch (cp
, cache_policy
)) {
171 ambigsw (cp
, cache_policy
);
174 die("%s unknown", cp
);
232 if (!(cp
= *argp
++) || *cp
== '-')
233 die("missing argument to %s", argp
[-2]);
234 switch (encoding
= smatch (cp
, encodingswitches
)) {
236 ambigsw (cp
, encodingswitches
);
239 die("%s unknown encoding algorithm", cp
);
241 header_encoding
= CE_BASE64
;
244 header_encoding
= CE_QUOTED
;
247 header_encoding
= CE_8BIT
;
250 die("Internal error: algorithm %s", cp
);
255 case AUTOHEADERENCSW
:
256 header_encoding
= CE_UNKNOWN
;
260 if (!(cp
= *argp
++) || *cp
== '-')
261 die("missing argument to %s", argp
[-2]);
262 if ((maxunencoded
= atoi(cp
)) < 1)
263 die("Invalid argument for %s: %s", argp
[-2], cp
);
264 if (maxunencoded
> 998)
265 die("limit of -maxunencoded is 998");
289 die("only one composition file allowed");
294 * Check if we've specified an additional profile
296 if ((cp
= getenv ("MHBUILD"))) {
297 if ((fp
= fopen (cp
, "r"))) {
298 readconfig(NULL
, fp
, cp
, 0);
301 admonish ("", "unable to read $MHBUILD profile (%s)", cp
);
306 * Read the standard profile setup
308 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
309 readconfig(NULL
, fp
, cp
, 0);
313 /* Check for public cache location */
314 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
317 /* Check for private cache location */
318 if (!(cache_private
= context_find (nmhprivcache
)))
319 cache_private
= ".cache";
320 cache_private
= mh_xstrdup(m_maildir(cache_private
));
322 if (!context_find ("path"))
323 free (path ("./", TFOLDER
));
325 /* Check if we have a file to process */
327 die("need to specify a %s composition file", invo_name
);
330 * Process the composition file from standard input.
332 if (compfile
[0] == '-' && compfile
[1] == '\0') {
333 if ((cp
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
334 die("unable to create temporary file in %s",
337 strncpy (infile
, cp
, sizeof(infile
));
339 /* copy standard input to temporary file */
340 while ((n
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0) {
341 if (fwrite(buffer
, 1, n
, fp
) != n
) {
342 die("error copying to temporary file");
347 /* build the content structures for MIME message */
348 ct
= build_mime (infile
, autobuild
, dist
, directives
, header_encoding
,
349 maxunencoded
, verbosw
);
352 * If ct == NULL, that means that -auto was set and a MIME version
353 * header was already seen. Just use the input file as the output
357 if (! (fp
= fopen(infile
, "r"))) {
358 die("Unable to open %s for reading", infile
);
360 while ((n
= fread(buffer
, 1, sizeof(buffer
), fp
)) > 0) {
361 if (fwrite(buffer
, 1, n
, stdout
) != n
) {
362 die("error copying %s to stdout", infile
);
366 /* output the message */
367 output_message_fp (ct
, stdout
, NULL
);
375 * Process the composition file from a file.
378 /* build the content structures for MIME message */
379 ct
= build_mime (compfile
, autobuild
, dist
, directives
, header_encoding
,
380 maxunencoded
, verbosw
);
383 * If ct == NULL, that means -auto was set and we found a MIME version
384 * header. Simply exit and do nothing.
393 /* output MIME message to this temporary file */
394 if ((cp
= m_mktemp2(compfile
, invo_name
, NULL
, &fp_out
)) == NULL
) {
395 die("unable to create temporary file");
397 strncpy(outfile
, cp
, sizeof(outfile
));
399 /* output the message */
400 output_message_fp (ct
, fp_out
, outfile
);
404 * List the message info
407 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, dispo
);
409 /* Rename composition draft */
410 snprintf (buffer
, sizeof(buffer
), "%s.orig", m_backup (compfile
));
411 if (rename (compfile
, buffer
) == NOTOK
) {
412 adios (compfile
, "unable to rename comp draft %s to", buffer
);
415 /* Rename output file to take its place */
416 if (rename (outfile
, compfile
) == NOTOK
) {
417 advise (outfile
, "unable to rename output %s to", compfile
);
418 rename (buffer
, compfile
);
421 /* Remove from atexit(3) list of files to unlink. */
422 if (!(m_unlink(outfile
) == -1 && errno
== ENOENT
)) {
423 adios(outfile
, "file exists after rename:");