]>
diplodocus.org Git - nmh/blob - uip/mhbuild.c
3 * mhbuild.c -- expand/translate MIME composition files
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
16 #include <h/mhparse.h>
17 #include <h/mhcachesbr.h>
20 #define MHBUILD_SWITCHES \
21 X("auto", 0, AUTOSW) \
22 X("noauto", 0, NAUTOSW) \
23 X("check", 0, CHECKSW) \
24 X("nocheck", 0, NCHECKSW) \
25 X("directives", 0, DIRECTIVES) \
26 X("nodirectives", 0, NDIRECTIVES) \
27 X("headers", 0, HEADSW) \
28 X("noheaders", 0, NHEADSW) \
29 X("list", 0, LISTSW) \
30 X("nolist", 0, NLISTSW) \
31 X("realsize", 0, SIZESW) \
32 X("norealsize", 0, NSIZESW) \
33 X("rfc934mode", 0, RFC934SW) \
34 X("norfc934mode", 0, NRFC934SW) \
35 X("verbose", 0, VERBSW) \
36 X("noverbose", 0, NVERBSW) \
37 X("disposition", 0, DISPOSW) \
38 X("nodisposition", 0, NDISPOSW) \
39 X("rcache policy", 0, RCACHESW) \
40 X("wcache policy", 0, WCACHESW) \
41 X("contentid", 0, CONTENTIDSW) \
42 X("nocontentid", 0, NCONTENTIDSW) \
43 X("headerencoding encoding-algorithm", 0, HEADERENCSW) \
44 X("autoheaderencoding", 0, AUTOHEADERENCSW) \
45 X("maxunencoded", 0, MAXUNENCSW) \
46 X("version", 0, VERSIONSW) \
47 X("help", 0, HELPSW) \
48 X("debug", -5, DEBUGSW) \
49 X("dist", 0, DISTSW) \
51 #define X(sw, minchars, id) id,
52 DEFINE_SWITCH_ENUM(MHBUILD
);
55 #define X(sw, minchars, id) { sw, minchars, id },
56 DEFINE_SWITCH_ARRAY(MHBUILD
, switches
);
59 /* utf-8 is for Email Address Internationalization, using SMTPUTF8. */
60 #define MIMEENCODING_SWITCHES \
61 X("base64", 0, BASE64SW) \
62 X("quoted-printable", 0, QUOTEDPRINTSW) \
63 X("utf-8", 0, UTF8SW) \
65 #define X(sw, minchars, id) id,
66 DEFINE_SWITCH_ENUM(MIMEENCODING
);
69 #define X(sw, minchars, id) { sw, minchars, id },
70 DEFINE_SWITCH_ARRAY(MIMEENCODING
, encodingswitches
);
76 extern char *cache_public
;
77 extern char *cache_private
;
88 static char infile
[BUFSIZ
];
89 static int unlink_infile
= 0;
91 static char outfile
[BUFSIZ
];
92 static int unlink_outfile
= 0;
94 static void unlink_done (int) NORETURN
;
97 int output_message (CT
, char *);
98 int output_message_fp (CT
, FILE *, char*);
102 main (int argc
, char **argv
)
104 int sizesw
= 1, headsw
= 1, directives
= 1, autobuild
= 0, dist
= 0;
105 int verbosw
= 0, dispo
= 0;
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], 2)) { return 1; }
121 arguments
= getarguments (invo_name
, argc
, argv
, 1);
124 while ((cp
= *argp
++)) {
125 if (cp
[0] == '-' && cp
[1] == '\0') {
127 adios (NULL
, "cannot specify both standard input and a file");
130 listsw
= 0; /* turn off -list if using standard in/out */
131 verbosw
= 0; /* turn off -verbose listings */
135 switch (smatch (++cp
, switches
)) {
137 ambigsw (cp
, switches
);
140 adios (NULL
, "-%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 adios (NULL
, "missing argument to %s", argp
[-2]);
172 switch (*icachesw
= smatch (cp
, caches
)) {
174 ambigsw (cp
, caches
);
177 adios (NULL
, "%s unknown", cp
);
235 if (!(cp
= *argp
++) || *cp
== '-')
236 adios (NULL
, "missing argument to %s", argp
[-2]);
237 switch (encoding
= smatch (cp
, encodingswitches
)) {
239 ambigsw (cp
, encodingswitches
);
242 adios (NULL
, "%s unknown encoding algorithm", cp
);
244 header_encoding
= CE_BASE64
;
247 header_encoding
= CE_QUOTED
;
250 header_encoding
= CE_8BIT
;
253 adios (NULL
, "Internal error: algorithm %s", cp
);
258 case AUTOHEADERENCSW
:
259 header_encoding
= CE_UNKNOWN
;
263 if (!(cp
= *argp
++) || *cp
== '-')
264 adios (NULL
, "missing argument to %s", argp
[-2]);
265 if ((maxunencoded
= atoi(cp
)) < 1)
266 adios (NULL
, "Invalid argument for %s: %s", argp
[-2], cp
);
267 if (maxunencoded
> 998)
268 adios (NULL
, "limit of -maxunencoded is 998");
292 adios (NULL
, "only one composition file allowed");
298 * Check if we've specified an additional profile
300 if ((cp
= getenv ("MHBUILD"))) {
301 if ((fp
= fopen (cp
, "r"))) {
302 readconfig ((struct node
**) 0, fp
, cp
, 0);
305 admonish ("", "unable to read $MHBUILD profile (%s)", cp
);
310 * Read the standard profile setup
312 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
313 readconfig ((struct node
**) 0, fp
, cp
, 0);
317 /* Check for public cache location */
318 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
321 /* Check for private cache location */
322 if (!(cache_private
= context_find (nmhprivcache
)))
323 cache_private
= ".cache";
324 cache_private
= getcpy (m_maildir (cache_private
));
326 if (!context_find ("path"))
327 free (path ("./", TFOLDER
));
329 /* Check if we have a file to process */
331 adios (NULL
, "need to specify a %s composition file", invo_name
);
334 * Process the composition file from standard input.
336 if (compfile
[0] == '-' && compfile
[1] == '\0') {
337 if ((cp
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
338 adios(NULL
, "unable to create temporary file in %s",
342 /* save a copy of the name for later removal */
343 strncpy (infile
, cp
, sizeof(infile
));
346 /* copy standard input to temporary file */
347 while ((n
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0) {
348 if (fwrite(buffer
, 1, n
, fp
) != n
) {
349 adios (NULL
, "error copying to temporary file");
354 /* build the content structures for MIME message */
355 ct
= build_mime (infile
, autobuild
, dist
, directives
, header_encoding
,
356 maxunencoded
, verbosw
);
359 * If ct == NULL, that means that -auto was set and a MIME version
360 * header was already seen. Just use the input file as the output
364 if (! (fp
= fopen(infile
, "r"))) {
365 adios(NULL
, "Unable to open %s for reading", infile
);
367 while ((n
= fread(buffer
, 1, sizeof(buffer
), fp
)) > 0) {
368 if (fwrite(buffer
, 1, n
, stdout
) != n
) {
369 adios(NULL
, "error copying %s to stdout", infile
);
373 /* output the message */
374 output_message_fp (ct
, stdout
, NULL
);
382 * Process the composition file from a file.
385 /* build the content structures for MIME message */
386 ct
= build_mime (compfile
, autobuild
, dist
, directives
, header_encoding
,
387 maxunencoded
, verbosw
);
390 * If ct == NULL, that means -auto was set and we found a MIME version
391 * header. Simply exit and do nothing.
400 /* output MIME message to this temporary file */
401 if ((cp
= m_mktemp2(compfile
, invo_name
, NULL
, &fp_out
)) == NULL
) {
402 adios(NULL
, "unable to create temporary file in %s", get_temp_dir());
404 strncpy(outfile
, cp
, sizeof(outfile
));
407 /* output the message */
408 output_message_fp (ct
, fp_out
, outfile
);
412 * List the message info
415 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, dispo
);
417 /* Rename composition draft */
418 snprintf (buffer
, sizeof(buffer
), "%s.orig", m_backup (compfile
));
419 if (rename (compfile
, buffer
) == NOTOK
) {
420 adios (compfile
, "unable to rename comp draft %s to", buffer
);
423 /* Rename output file to take its place */
424 if (rename (outfile
, compfile
) == NOTOK
) {
425 advise (outfile
, "unable to rename output %s to", compfile
);
426 rename (buffer
, compfile
);
438 unlink_done (int status
)
441 * Check if we need to remove stray
445 (void) m_unlink (infile
);
447 (void) m_unlink (outfile
);