]>
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", -4, 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 #define MIMEENCODING_SWITCHES \
60 X("base64", 0, BASE64SW) \
61 X("quoted-printable", 0, QUOTEDPRINTSW) \
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 (CT
, char *);
96 int output_message_fp (CT
, FILE *, char*);
100 main (int argc
, char **argv
)
102 int sizesw
= 1, headsw
= 1, directives
= 1, autobuild
= 0, dist
= 0;
103 int verbosw
= 0, dispo
= 0;
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], 1)) { return 1; }
119 arguments
= getarguments (invo_name
, argc
, argv
, 1);
122 while ((cp
= *argp
++)) {
123 if (cp
[0] == '-' && cp
[1] == '\0') {
125 adios (NULL
, "cannot specify both standard input and a file");
128 listsw
= 0; /* turn off -list if using standard in/out */
129 verbosw
= 0; /* turn off -verbose listings */
133 switch (smatch (++cp
, switches
)) {
135 ambigsw (cp
, switches
);
138 adios (NULL
, "-%s unknown", cp
);
141 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
142 print_help (buf
, switches
, 1);
145 print_version(invo_name
);
149 /* -auto implies -nodirectives */
155 * We're turning directives back on since this is likely here
156 * to override a profile entry
163 icachesw
= &rcachesw
;
166 icachesw
= &wcachesw
;
168 if (!(cp
= *argp
++) || *cp
== '-')
169 adios (NULL
, "missing argument to %s", argp
[-2]);
170 switch (*icachesw
= smatch (cp
, caches
)) {
172 ambigsw (cp
, caches
);
175 adios (NULL
, "%s unknown", cp
);
233 if (!(cp
= *argp
++) || *cp
== '-')
234 adios (NULL
, "missing argument to %s", argp
[-2]);
235 switch (encoding
= smatch (cp
, encodingswitches
)) {
237 ambigsw (cp
, encodingswitches
);
240 adios (NULL
, "%s unknown encoding algorithm", cp
);
242 header_encoding
= CE_BASE64
;
245 header_encoding
= CE_QUOTED
;
248 adios (NULL
, "Internal error: algorithm %s", cp
);
253 case AUTOHEADERENCSW
:
254 header_encoding
= CE_UNKNOWN
;
258 if (!(cp
= *argp
++) || *cp
== '-')
259 adios (NULL
, "missing argument to %s", argp
[-2]);
260 if ((maxunencoded
= atoi(cp
)) < 1)
261 adios (NULL
, "Invalid argument for %s: %s", argp
[-2], cp
);
262 if (maxunencoded
> 998)
263 adios (NULL
, "limit of -maxunencoded is 998");
287 adios (NULL
, "only one composition file allowed");
293 * Check if we've specified an additional profile
295 if ((cp
= getenv ("MHBUILD"))) {
296 if ((fp
= fopen (cp
, "r"))) {
297 readconfig ((struct node
**) 0, fp
, cp
, 0);
300 admonish ("", "unable to read $MHBUILD profile (%s)", cp
);
305 * Read the standard profile setup
307 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
308 readconfig ((struct node
**) 0, fp
, cp
, 0);
312 /* Check for public cache location */
313 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
316 /* Check for private cache location */
317 if (!(cache_private
= context_find (nmhprivcache
)))
318 cache_private
= ".cache";
319 cache_private
= getcpy (m_maildir (cache_private
));
321 if (!context_find ("path"))
322 free (path ("./", TFOLDER
));
324 /* Check if we have a file to process */
326 adios (NULL
, "need to specify a %s composition file", invo_name
);
329 * Process the composition file from standard input.
331 if (compfile
[0] == '-' && compfile
[1] == '\0') {
332 if ((cp
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
333 adios(NULL
, "unable to create temporary file in %s",
337 /* save a copy of the name for later removal */
338 strncpy (infile
, cp
, sizeof(infile
));
341 /* copy standard input to temporary file */
342 while ((n
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0) {
343 if (fwrite(buffer
, 1, n
, fp
) != n
) {
344 adios (NULL
, "error copying to temporary file");
349 /* build the content structures for MIME message */
350 ct
= build_mime (infile
, autobuild
, dist
, directives
, header_encoding
,
351 maxunencoded
, verbosw
);
354 * If ct == NULL, that means that -auto was set and a MIME version
355 * header was already seen. Just use the input file as the output
359 if (! (fp
= fopen(infile
, "r"))) {
360 adios(NULL
, "Unable to open %s for reading", infile
);
362 while ((n
= fread(buffer
, 1, sizeof(buffer
), fp
)) > 0) {
363 if (fwrite(buffer
, 1, n
, stdout
) != n
) {
364 adios(NULL
, "error copying %s to stdout", infile
);
368 /* output the message */
369 output_message_fp (ct
, stdout
, NULL
);
377 * Process the composition file from a file.
380 /* build the content structures for MIME message */
381 ct
= build_mime (compfile
, autobuild
, dist
, directives
, header_encoding
,
382 maxunencoded
, verbosw
);
385 * If ct == NULL, that means -auto was set and we found a MIME version
386 * header. Simply exit and do nothing.
395 /* output MIME message to this temporary file */
396 if ((cp
= m_mktemp2(compfile
, invo_name
, NULL
, &fp_out
)) == NULL
) {
397 adios(NULL
, "unable to create temporary file in %s", get_temp_dir());
399 strncpy(outfile
, cp
, sizeof(outfile
));
402 /* output the message */
403 output_message_fp (ct
, fp_out
, outfile
);
407 * List the message info
410 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, dispo
);
412 /* Rename composition draft */
413 snprintf (buffer
, sizeof(buffer
), "%s.orig", m_backup (compfile
));
414 if (rename (compfile
, buffer
) == NOTOK
) {
415 adios (compfile
, "unable to rename comp draft %s to", buffer
);
418 /* Rename output file to take its place */
419 if (rename (outfile
, compfile
) == NOTOK
) {
420 advise (outfile
, "unable to rename output %s to", compfile
);
421 rename (buffer
, compfile
);
433 unlink_done (int status
)
436 * Check if we need to remove stray
440 (void) m_unlink (infile
);
442 (void) m_unlink (outfile
);