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>
19 #include <h/mhparse.h>
20 #include <h/mhcachesbr.h>
23 static struct swit switches[] = {
31 { "noebcdicsafe", 0 },
47 { "norfc934mode", 0 },
53 { "rcache policy", 0 },
55 { "wcache policy", 0 },
56 #define CONTENTIDSW 16
58 #define NCONTENTIDSW 17
71 extern char *tmp; /* directory to place temp files */
76 extern char *cache_public;
77 extern char *cache_private;
90 static char infile[BUFSIZ];
91 static int unlink_infile = 0;
93 static char outfile[BUFSIZ];
94 static int unlink_outfile = 0;
96 static void unlink_done (int) NORETURN;
99 CT build_mime (char *);
100 int output_message (CT, char *);
101 int output_message_fp (CT, FILE *, char*);
104 int list_all_messages (CT *, int, int, int, int);
107 void set_endian (void);
110 void free_content (CT);
114 main (int argc, char **argv)
116 int sizesw = 1, headsw = 1;
118 char *cp, buf[BUFSIZ];
119 char buffer[BUFSIZ], *compfile = NULL;
120 char **argp, **arguments;
128 setlocale(LC_ALL, "");
130 invo_name = r1bindex (argv[0], '/');
132 /* read user profile/context */
135 arguments = getarguments (invo_name, argc, argv, 1);
138 while ((cp = *argp++)) {
139 if (cp[0] == '-' && cp[1] == '\0') {
141 adios (NULL, "cannot specify both standard input and a file");
144 listsw = 0; /* turn off -list if using standard in/out */
145 verbosw = 0; /* turn off -verbose listings */
149 switch (smatch (++cp, switches)) {
151 ambigsw (cp, switches);
154 adios (NULL, "-%s unknown", cp);
157 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
158 print_help (buf, switches, 1);
161 print_version(invo_name);
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);
244 adios (NULL, "only one composition file allowed");
251 if ((cp = getenv ("MM_NOASK")) && !strcmp (cp, "1"))
255 * Check if we've specified an additional profile
257 if ((cp = getenv ("MHBUILD"))) {
258 if ((fp = fopen (cp, "r"))) {
259 readconfig ((struct node **) 0, fp, cp, 0);
262 admonish ("", "unable to read $MHBUILD profile (%s)", cp);
267 * Read the standard profile setup
269 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
270 readconfig ((struct node **) 0, fp, cp, 0);
274 /* Check for public cache location */
275 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
278 /* Check for private cache location */
279 if (!(cache_private = context_find (nmhprivcache)))
280 cache_private = ".cache";
281 cache_private = getcpy (m_maildir (cache_private));
284 * Check for storage directory. If defined, we
285 * will store temporary files there. Else we
286 * store them in standard nmh directory.
288 if ((cp = context_find (nmhstorage)) && *cp)
289 tmp = concat (cp, "/", invo_name, NULL);
291 tmp = add (m_maildir (invo_name), NULL);
293 if (!context_find ("path"))
294 free (path ("./", TFOLDER));
296 /* Check if we have a file to process */
298 adios (NULL, "need to specify a %s composition file", invo_name);
301 * Process the composition file from standard input.
303 if (compfile[0] == '-' && compfile[1] == '\0') {
304 /* copy standard input to temporary file */
305 strncpy (infile, m_mktemp(invo_name, NULL, &fp), sizeof(infile));
306 while (fgets (buffer, BUFSIZ, stdin))
311 /* build the content structures for MIME message */
312 ct = build_mime (infile);
316 /* output MIME message to this temporary file */
317 strncpy (outfile, m_mktemp(invo_name, NULL, &fp_out), sizeof(outfile));
320 /* output the message */
321 output_message_fp (ct, fp_out, outfile);
324 /* output the temp file to standard output */
325 if ((fp = fopen (outfile, "r")) == NULL)
326 adios (outfile, "unable to open");
327 while (fgets (buffer, BUFSIZ, fp))
328 fputs (buffer, stdout);
342 * Process the composition file from a file.
345 /* build the content structures for MIME message */
346 ct = build_mime (compfile);
350 /* output MIME message to this temporary file */
351 strncpy(outfile, m_mktemp2(compfile, invo_name, NULL, &fp_out),
355 /* output the message */
356 output_message_fp (ct, fp_out, outfile);
360 * List the message info
363 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
365 /* Rename composition draft */
366 snprintf (buffer, sizeof(buffer), "%s.orig", m_backup (compfile));
367 if (rename (compfile, buffer) == NOTOK) {
368 adios (compfile, "unable to rename comp draft %s to", buffer);
371 /* Rename output file to take its place */
372 if (rename (outfile, compfile) == NOTOK) {
373 advise (outfile, "unable to rename output %s to", compfile);
374 rename (buffer, compfile);
386 unlink_done (int status)
389 * Check if we need to remove stray