]>
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("version", 0, VERSIONSW) \
45 X("help", 0, HELPSW) \
46 X("debug", -5, DEBUGSW) \
47 X("dist", -4, 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 #define MIMEENCODING_SWITCHES \
58 X("base64", 0, BASE64SW) \
59 X("quoted-printable", 0, QUOTEDPRINTSW) \
61 #define X(sw, minchars, id) id,
62 DEFINE_SWITCH_ENUM(MIMEENCODING
);
65 #define X(sw, minchars, id) { sw, minchars, id },
66 DEFINE_SWITCH_ARRAY(MIMEENCODING
, encodingswitches
);
72 extern char *cache_public
;
73 extern char *cache_private
;
85 static char infile
[BUFSIZ
];
86 static int unlink_infile
= 0;
88 static char outfile
[BUFSIZ
];
89 static int unlink_outfile
= 0;
91 static void unlink_done (int) NORETURN
;
94 int output_message (CT
, char *);
95 int output_message_fp (CT
, FILE *, char*);
98 int list_all_messages (CT
*, int, int, int, int);
102 main (int argc
, char **argv
)
104 int sizesw
= 1, headsw
= 1, directives
= 1, autobuild
= 0, dist
= 0;
106 char *cp
, buf
[BUFSIZ
];
107 char buffer
[BUFSIZ
], *compfile
= NULL
;
108 char **argp
, **arguments
;
112 int header_encoding
= CE_UNKNOWN
;
114 if (nmh_init(argv
[0], 1)) { 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 adios (NULL
, "Internal error: algorithm %s", cp
);
252 case AUTOHEADERENCSW
:
253 header_encoding
= CE_UNKNOWN
;
271 adios (NULL
, "only one composition file allowed");
277 * Check if we've specified an additional profile
279 if ((cp
= getenv ("MHBUILD"))) {
280 if ((fp
= fopen (cp
, "r"))) {
281 readconfig ((struct node
**) 0, fp
, cp
, 0);
284 admonish ("", "unable to read $MHBUILD profile (%s)", cp
);
289 * Read the standard profile setup
291 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
292 readconfig ((struct node
**) 0, fp
, cp
, 0);
296 /* Check for public cache location */
297 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
300 /* Check for private cache location */
301 if (!(cache_private
= context_find (nmhprivcache
)))
302 cache_private
= ".cache";
303 cache_private
= getcpy (m_maildir (cache_private
));
305 if (!context_find ("path"))
306 free (path ("./", TFOLDER
));
308 /* Check if we have a file to process */
310 adios (NULL
, "need to specify a %s composition file", invo_name
);
313 * Process the composition file from standard input.
315 if (compfile
[0] == '-' && compfile
[1] == '\0') {
316 if ((cp
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
317 adios(NULL
, "unable to create temporary file in %s",
321 /* copy standard input to temporary file */
322 strncpy (infile
, cp
, sizeof(infile
));
323 while (fgets (buffer
, BUFSIZ
, stdin
))
328 /* build the content structures for MIME message */
329 ct
= build_mime (infile
, autobuild
, dist
, directives
, header_encoding
);
332 * If ct == NULL, that means that -auto was set and a MIME version
333 * header was already seen. Just use the input file as the output
337 if (! (fp
= fopen(infile
, "r"))) {
338 adios(NULL
, "Unable to open %s for reading", infile
);
340 while (fgets(buffer
, BUFSIZ
, fp
))
341 fputs(buffer
, stdout
);
343 /* output the message */
344 output_message_fp (ct
, stdout
, NULL
);
348 (void) m_unlink (infile
);
355 * Process the composition file from a file.
358 /* build the content structures for MIME message */
359 ct
= build_mime (compfile
, autobuild
, dist
, directives
, header_encoding
);
362 * If ct == NULL, that means -auto was set and we found a MIME version
363 * header. Simply exit and do nothing.
372 /* output MIME message to this temporary file */
373 if ((cp
= m_mktemp2(compfile
, invo_name
, NULL
, &fp_out
)) == NULL
) {
374 adios(NULL
, "unable to create temporary file in %s", get_temp_dir());
376 strncpy(outfile
, cp
, sizeof(outfile
));
379 /* output the message */
380 output_message_fp (ct
, fp_out
, outfile
);
384 * List the message info
387 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
);
389 /* Rename composition draft */
390 snprintf (buffer
, sizeof(buffer
), "%s.orig", m_backup (compfile
));
391 if (rename (compfile
, buffer
) == NOTOK
) {
392 adios (compfile
, "unable to rename comp draft %s to", buffer
);
395 /* Rename output file to take its place */
396 if (rename (outfile
, compfile
) == NOTOK
) {
397 advise (outfile
, "unable to rename output %s to", compfile
);
398 rename (buffer
, compfile
);
410 unlink_done (int status
)
413 * Check if we need to remove stray
417 (void) m_unlink (infile
);
419 (void) m_unlink (outfile
);