3 * mhbuild.c -- expand/translate MIME composition files
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
14 #include <h/signals.h>
21 #include <h/mhparse.h>
22 #include <h/mhcachesbr.h>
25 #ifdef HAVE_SYS_WAIT_H
26 # include <sys/wait.h>
29 static struct swit switches[] = {
37 { "noebcdicsafe", 0 },
53 { "norfc934mode", 0 },
59 { "rcache policy", 0 },
61 { "wcache policy", 0 },
62 #define CONTENTIDSW 16
64 #define NCONTENTIDSW 17
77 extern char *tmp; /* directory to place temp files */
82 extern char *cache_public;
83 extern char *cache_private;
96 static char infile[BUFSIZ];
97 static int unlink_infile = 0;
99 static char outfile[BUFSIZ];
100 static int unlink_outfile = 0;
102 static void unlink_done (int) NORETURN;
105 CT build_mime (char *);
106 int output_message (CT, char *);
107 int output_message_fp (CT, FILE *, char*);
110 int list_all_messages (CT *, int, int, int, int);
113 void set_endian (void);
116 void free_content (CT);
120 main (int argc, char **argv)
122 int sizesw = 1, headsw = 1;
124 char *cp, buf[BUFSIZ];
125 char buffer[BUFSIZ], *compfile = NULL;
126 char **argp, **arguments;
134 setlocale(LC_ALL, "");
136 invo_name = r1bindex (argv[0], '/');
138 /* read user profile/context */
141 arguments = getarguments (invo_name, argc, argv, 1);
144 while ((cp = *argp++)) {
145 if (cp[0] == '-' && cp[1] == '\0') {
147 adios (NULL, "cannot specify both standard input and a file");
150 listsw = 0; /* turn off -list if using standard in/out */
151 verbosw = 0; /* turn off -verbose listings */
155 switch (smatch (++cp, switches)) {
157 ambigsw (cp, switches);
160 adios (NULL, "-%s unknown", cp);
163 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
164 print_help (buf, switches, 1);
167 print_version(invo_name);
171 icachesw = &rcachesw;
174 icachesw = &wcachesw;
176 if (!(cp = *argp++) || *cp == '-')
177 adios (NULL, "missing argument to %s", argp[-2]);
178 switch (*icachesw = smatch (cp, caches)) {
180 ambigsw (cp, caches);
183 adios (NULL, "%s unknown", cp);
250 adios (NULL, "only one composition file allowed");
257 if ((cp = getenv ("MM_NOASK")) && !strcmp (cp, "1"))
261 * Check if we've specified an additional profile
263 if ((cp = getenv ("MHBUILD"))) {
264 if ((fp = fopen (cp, "r"))) {
265 readconfig ((struct node **) 0, fp, cp, 0);
268 admonish ("", "unable to read $MHBUILD profile (%s)", cp);
273 * Read the standard profile setup
275 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
276 readconfig ((struct node **) 0, fp, cp, 0);
280 /* Check for public cache location */
281 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
284 /* Check for private cache location */
285 if (!(cache_private = context_find (nmhprivcache)))
286 cache_private = ".cache";
287 cache_private = getcpy (m_maildir (cache_private));
290 * Check for storage directory. If defined, we
291 * will store temporary files there. Else we
292 * store them in standard nmh directory.
294 if ((cp = context_find (nmhstorage)) && *cp)
295 tmp = concat (cp, "/", invo_name, NULL);
297 tmp = add (m_maildir (invo_name), NULL);
299 if (!context_find ("path"))
300 free (path ("./", TFOLDER));
302 /* Check if we have a file to process */
304 adios (NULL, "need to specify a %s composition file", invo_name);
307 * Process the composition file from standard input.
309 if (compfile[0] == '-' && compfile[1] == '\0') {
310 /* copy standard input to temporary file */
311 strncpy (infile, m_mktemp(invo_name, NULL, &fp), sizeof(infile));
312 while (fgets (buffer, BUFSIZ, stdin))
317 /* build the content structures for MIME message */
318 ct = build_mime (infile);
322 /* output MIME message to this temporary file */
323 strncpy (outfile, m_mktemp(invo_name, NULL, &fp_out), sizeof(outfile));
326 /* output the message */
327 output_message_fp (ct, fp_out, outfile);
330 /* output the temp file to standard output */
331 if ((fp = fopen (outfile, "r")) == NULL)
332 adios (outfile, "unable to open");
333 while (fgets (buffer, BUFSIZ, fp))
334 fputs (buffer, stdout);
348 * Process the composition file from a file.
351 /* build the content structures for MIME message */
352 ct = build_mime (compfile);
356 /* output MIME message to this temporary file */
357 strncpy(outfile, m_mktemp2(compfile, invo_name, NULL, &fp_out),
361 /* output the message */
362 output_message_fp (ct, fp_out, outfile);
366 * List the message info
369 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
371 /* Rename composition draft */
372 snprintf (buffer, sizeof(buffer), "%s.orig", m_backup (compfile));
373 if (rename (compfile, buffer) == NOTOK) {
374 adios (compfile, "unable to rename comp draft %s to", buffer);
377 /* Rename output file to take its place */
378 if (rename (outfile, compfile) == NOTOK) {
379 advise (outfile, "unable to rename output %s to", compfile);
380 rename (buffer, compfile);
392 unlink_done (int status)
395 * Check if we need to remove stray