]>
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.
12 #include <h/signals.h>
17 #include <h/mhparse.h>
18 #include <h/mhcachesbr.h>
21 #define MHBUILD_SWITCHES \
22 X("auto", 0, AUTOSW) \
23 X("noauto", 0, NAUTOSW) \
24 X("check", 0, CHECKSW) \
25 X("nocheck", 0, NCHECKSW) \
26 X("directives", 0, DIRECTIVES) \
27 X("nodirectives", 0, NDIRECTIVES) \
28 X("headers", 0, HEADSW) \
29 X("noheaders", 0, NHEADSW) \
30 X("list", 0, LISTSW) \
31 X("nolist", 0, NLISTSW) \
32 X("realsize", 0, SIZESW) \
33 X("norealsize", 0, NSIZESW) \
34 X("rfc934mode", 0, RFC934SW) \
35 X("norfc934mode", 0, NRFC934SW) \
36 X("verbose", 0, VERBSW) \
37 X("noverbose", 0, NVERBSW) \
38 X("rcache policy", 0, RCACHESW) \
39 X("wcache policy", 0, WCACHESW) \
40 X("contentid", 0, CONTENTIDSW) \
41 X("nocontentid", 0, NCONTENTIDSW) \
42 X("headerencoding encoding-algorithm", 0, HEADERENCSW) \
43 X("autoheaderencoding", 0, AUTOHEADERENCSW) \
44 X("maxunencoded", 0, MAXUNENCSW) \
45 X("version", 0, VERSIONSW) \
46 X("help", 0, HELPSW) \
47 X("debug", -5, DEBUGSW) \
48 X("dist", -4, DISTSW) \
50 #define X(sw, minchars, id) id,
51 DEFINE_SWITCH_ENUM(MHBUILD
);
54 #define X(sw, minchars, id) { sw, minchars, id },
55 DEFINE_SWITCH_ARRAY(MHBUILD
, switches
);
58 #define MIMEENCODING_SWITCHES \
59 X("base64", 0, BASE64SW) \
60 X("quoted-printable", 0, QUOTEDPRINTSW) \
62 #define X(sw, minchars, id) id,
63 DEFINE_SWITCH_ENUM(MIMEENCODING
);
66 #define X(sw, minchars, id) { sw, minchars, id },
67 DEFINE_SWITCH_ARRAY(MIMEENCODING
, encodingswitches
);
73 extern char *cache_public
;
74 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*);
99 int list_all_messages (CT
*, int, int, int, int);
103 main (int argc
, char **argv
)
105 int sizesw
= 1, headsw
= 1, directives
= 1, autobuild
= 0, dist
= 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
;
116 if (nmh_init(argv
[0], 1)) { return 1; }
120 arguments
= getarguments (invo_name
, argc
, argv
, 1);
123 while ((cp
= *argp
++)) {
124 if (cp
[0] == '-' && cp
[1] == '\0') {
126 adios (NULL
, "cannot specify both standard input and a file");
129 listsw
= 0; /* turn off -list if using standard in/out */
130 verbosw
= 0; /* turn off -verbose listings */
134 switch (smatch (++cp
, switches
)) {
136 ambigsw (cp
, switches
);
139 adios (NULL
, "-%s unknown", cp
);
142 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
143 print_help (buf
, switches
, 1);
146 print_version(invo_name
);
150 /* -auto implies -nodirectives */
156 * We're turning directives back on since this is likely here
157 * to override a profile entry
164 icachesw
= &rcachesw
;
167 icachesw
= &wcachesw
;
169 if (!(cp
= *argp
++) || *cp
== '-')
170 adios (NULL
, "missing argument to %s", argp
[-2]);
171 switch (*icachesw
= smatch (cp
, caches
)) {
173 ambigsw (cp
, caches
);
176 adios (NULL
, "%s unknown", cp
);
234 if (!(cp
= *argp
++) || *cp
== '-')
235 adios (NULL
, "missing argument to %s", argp
[-2]);
236 switch (encoding
= smatch (cp
, encodingswitches
)) {
238 ambigsw (cp
, encodingswitches
);
241 adios (NULL
, "%s unknown encoding algorithm", cp
);
243 header_encoding
= CE_BASE64
;
246 header_encoding
= CE_QUOTED
;
249 adios (NULL
, "Internal error: algorithm %s", cp
);
254 case AUTOHEADERENCSW
:
255 header_encoding
= CE_UNKNOWN
;
259 if (!(cp
= *argp
++) || *cp
== '-')
260 adios (NULL
, "missing argument to %s", argp
[-2]);
261 if ((maxunencoded
= atoi(cp
)) < 1)
262 adios (NULL
, "Invalid argument for %s: %s", argp
[-2], cp
);
263 if (maxunencoded
> 998)
264 adios (NULL
, "limit of -maxunencoded is 998");
282 adios (NULL
, "only one composition file allowed");
288 * Check if we've specified an additional profile
290 if ((cp
= getenv ("MHBUILD"))) {
291 if ((fp
= fopen (cp
, "r"))) {
292 readconfig ((struct node
**) 0, fp
, cp
, 0);
295 admonish ("", "unable to read $MHBUILD profile (%s)", cp
);
300 * Read the standard profile setup
302 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
303 readconfig ((struct node
**) 0, fp
, cp
, 0);
307 /* Check for public cache location */
308 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
311 /* Check for private cache location */
312 if (!(cache_private
= context_find (nmhprivcache
)))
313 cache_private
= ".cache";
314 cache_private
= getcpy (m_maildir (cache_private
));
316 if (!context_find ("path"))
317 free (path ("./", TFOLDER
));
319 /* Check if we have a file to process */
321 adios (NULL
, "need to specify a %s composition file", invo_name
);
324 * Process the composition file from standard input.
326 if (compfile
[0] == '-' && compfile
[1] == '\0') {
327 if ((cp
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
328 adios(NULL
, "unable to create temporary file in %s",
332 /* copy standard input to temporary file */
333 strncpy (infile
, cp
, sizeof(infile
));
334 while (fgets (buffer
, BUFSIZ
, stdin
))
339 /* build the content structures for MIME message */
340 ct
= build_mime (infile
, autobuild
, dist
, directives
, header_encoding
,
344 * If ct == NULL, that means that -auto was set and a MIME version
345 * header was already seen. Just use the input file as the output
349 if (! (fp
= fopen(infile
, "r"))) {
350 adios(NULL
, "Unable to open %s for reading", infile
);
352 while (fgets(buffer
, BUFSIZ
, fp
))
353 fputs(buffer
, stdout
);
355 /* output the message */
356 output_message_fp (ct
, stdout
, NULL
);
360 (void) m_unlink (infile
);
367 * Process the composition file from a file.
370 /* build the content structures for MIME message */
371 ct
= build_mime (compfile
, autobuild
, dist
, directives
, header_encoding
,
375 * If ct == NULL, that means -auto was set and we found a MIME version
376 * header. Simply exit and do nothing.
385 /* output MIME message to this temporary file */
386 if ((cp
= m_mktemp2(compfile
, invo_name
, NULL
, &fp_out
)) == NULL
) {
387 adios(NULL
, "unable to create temporary file in %s", get_temp_dir());
389 strncpy(outfile
, cp
, sizeof(outfile
));
392 /* output the message */
393 output_message_fp (ct
, fp_out
, outfile
);
397 * List the message info
400 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
);
402 /* Rename composition draft */
403 snprintf (buffer
, sizeof(buffer
), "%s.orig", m_backup (compfile
));
404 if (rename (compfile
, buffer
) == NOTOK
) {
405 adios (compfile
, "unable to rename comp draft %s to", buffer
);
408 /* Rename output file to take its place */
409 if (rename (outfile
, compfile
) == NOTOK
) {
410 advise (outfile
, "unable to rename output %s to", compfile
);
411 rename (buffer
, compfile
);
423 unlink_done (int status
)
426 * Check if we need to remove stray
430 (void) m_unlink (infile
);
432 (void) m_unlink (outfile
);