]>
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.
14 #include <h/mhparse.h>
15 #include <h/mhcachesbr.h>
18 #define MHBUILD_SWITCHES \
19 X("auto", 0, AUTOSW) \
20 X("noauto", 0, NAUTOSW) \
21 X("check", 0, CHECKSW) \
22 X("nocheck", 0, NCHECKSW) \
23 X("directives", 0, DIRECTIVES) \
24 X("nodirectives", 0, NDIRECTIVES) \
25 X("headers", 0, HEADSW) \
26 X("noheaders", 0, NHEADSW) \
27 X("list", 0, LISTSW) \
28 X("nolist", 0, NLISTSW) \
29 X("realsize", 0, SIZESW) \
30 X("norealsize", 0, NSIZESW) \
31 X("rfc934mode", 0, RFC934SW) \
32 X("norfc934mode", 0, NRFC934SW) \
33 X("verbose", 0, VERBSW) \
34 X("noverbose", 0, NVERBSW) \
35 X("disposition", 0, DISPOSW) \
36 X("nodisposition", 0, NDISPOSW) \
37 X("rcache policy", 0, RCACHESW) \
38 X("wcache policy", 0, WCACHESW) \
39 X("contentid", 0, CONTENTIDSW) \
40 X("nocontentid", 0, NCONTENTIDSW) \
41 X("headerencoding encoding-algorithm", 0, HEADERENCSW) \
42 X("autoheaderencoding", 0, AUTOHEADERENCSW) \
43 X("maxunencoded", 0, MAXUNENCSW) \
44 X("version", 0, VERSIONSW) \
45 X("help", 0, HELPSW) \
46 X("debug", -5, DEBUGSW) \
47 X("dist", 0, DISTSW) \
49 #define X(sw, minchars, id) id,
50 DEFINE_SWITCH_ENUM(MHBUILD
);
53 #define X(sw, minchars, id) { sw, minchars, id },
54 DEFINE_SWITCH_ARRAY(MHBUILD
, switches
);
57 /* utf-8 is for Email Address Internationalization, using SMTPUTF8. */
58 #define MIMEENCODING_SWITCHES \
59 X("base64", 0, BASE64SW) \
60 X("quoted-printable", 0, QUOTEDPRINTSW) \
61 X("utf-8", 0, UTF8SW) \
63 #define X(sw, minchars, id) id,
64 DEFINE_SWITCH_ENUM(MIMEENCODING
);
67 #define X(sw, minchars, id) { sw, minchars, id },
68 DEFINE_SWITCH_ARRAY(MIMEENCODING
, encodingswitches
);
74 extern char *cache_public
;
75 extern char *cache_private
;
86 static char infile
[BUFSIZ
];
87 static int unlink_infile
= 0;
89 static char outfile
[BUFSIZ
];
90 static int unlink_outfile
= 0;
92 static void unlink_done (int) NORETURN
;
95 int output_message_fp (CT
, FILE *, char*);
99 main (int argc
, char **argv
)
101 int sizesw
= 1, headsw
= 1, directives
= 1, autobuild
= 0, dist
= 0;
102 int verbosw
= 0, dispo
= 0;
103 size_t maxunencoded
= MAXTEXTPERLN
;
105 char *cp
, buf
[BUFSIZ
];
106 char buffer
[BUFSIZ
], *compfile
= NULL
;
107 char **argp
, **arguments
;
111 int header_encoding
= CE_UNKNOWN
;
114 if (nmh_init(argv
[0], 2)) { return 1; }
118 arguments
= getarguments (invo_name
, argc
, argv
, 1);
121 while ((cp
= *argp
++)) {
122 if (cp
[0] == '-' && cp
[1] == '\0') {
124 adios (NULL
, "cannot specify both standard input and a file");
127 listsw
= 0; /* turn off -list if using standard in/out */
128 verbosw
= 0; /* turn off -verbose listings */
132 switch (smatch (++cp
, switches
)) {
134 ambigsw (cp
, switches
);
137 adios (NULL
, "-%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 adios (NULL
, "missing argument to %s", argp
[-2]);
169 switch (*icachesw
= smatch (cp
, caches
)) {
171 ambigsw (cp
, caches
);
174 adios (NULL
, "%s unknown", cp
);
232 if (!(cp
= *argp
++) || *cp
== '-')
233 adios (NULL
, "missing argument to %s", argp
[-2]);
234 switch (encoding
= smatch (cp
, encodingswitches
)) {
236 ambigsw (cp
, encodingswitches
);
239 adios (NULL
, "%s unknown encoding algorithm", cp
);
241 header_encoding
= CE_BASE64
;
244 header_encoding
= CE_QUOTED
;
247 header_encoding
= CE_8BIT
;
250 adios (NULL
, "Internal error: algorithm %s", cp
);
255 case AUTOHEADERENCSW
:
256 header_encoding
= CE_UNKNOWN
;
260 if (!(cp
= *argp
++) || *cp
== '-')
261 adios (NULL
, "missing argument to %s", argp
[-2]);
262 if ((maxunencoded
= atoi(cp
)) < 1)
263 adios (NULL
, "Invalid argument for %s: %s", argp
[-2], cp
);
264 if (maxunencoded
> 998)
265 adios (NULL
, "limit of -maxunencoded is 998");
289 adios (NULL
, "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 ((struct node
**) 0, 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 ((struct node
**) 0, 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
= getcpy (m_maildir (cache_private
));
323 if (!context_find ("path"))
324 free (path ("./", TFOLDER
));
326 /* Check if we have a file to process */
328 adios (NULL
, "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 adios(NULL
, "unable to create temporary file in %s",
339 /* save a copy of the name for later removal */
340 strncpy (infile
, cp
, sizeof(infile
));
343 /* copy standard input to temporary file */
344 while ((n
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0) {
345 if (fwrite(buffer
, 1, n
, fp
) != n
) {
346 adios (NULL
, "error copying to temporary file");
351 /* build the content structures for MIME message */
352 ct
= build_mime (infile
, autobuild
, dist
, directives
, header_encoding
,
353 maxunencoded
, verbosw
);
356 * If ct == NULL, that means that -auto was set and a MIME version
357 * header was already seen. Just use the input file as the output
361 if (! (fp
= fopen(infile
, "r"))) {
362 adios(NULL
, "Unable to open %s for reading", infile
);
364 while ((n
= fread(buffer
, 1, sizeof(buffer
), fp
)) > 0) {
365 if (fwrite(buffer
, 1, n
, stdout
) != n
) {
366 adios(NULL
, "error copying %s to stdout", infile
);
370 /* output the message */
371 output_message_fp (ct
, stdout
, NULL
);
379 * Process the composition file from a file.
382 /* build the content structures for MIME message */
383 ct
= build_mime (compfile
, autobuild
, dist
, directives
, header_encoding
,
384 maxunencoded
, verbosw
);
387 * If ct == NULL, that means -auto was set and we found a MIME version
388 * header. Simply exit and do nothing.
397 /* output MIME message to this temporary file */
398 if ((cp
= m_mktemp2(compfile
, invo_name
, NULL
, &fp_out
)) == NULL
) {
399 adios(NULL
, "unable to create temporary file in %s", get_temp_dir());
401 strncpy(outfile
, cp
, sizeof(outfile
));
404 /* output the message */
405 output_message_fp (ct
, fp_out
, outfile
);
409 * List the message info
412 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, dispo
);
414 /* Rename composition draft */
415 snprintf (buffer
, sizeof(buffer
), "%s.orig", m_backup (compfile
));
416 if (rename (compfile
, buffer
) == NOTOK
) {
417 adios (compfile
, "unable to rename comp draft %s to", buffer
);
420 /* Rename output file to take its place */
421 if (rename (outfile
, compfile
) == NOTOK
) {
422 advise (outfile
, "unable to rename output %s to", compfile
);
423 rename (buffer
, compfile
);
435 unlink_done (int status
)
438 * Check if we need to remove stray
442 (void) m_unlink (infile
);
444 (void) m_unlink (outfile
);