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 free_content (CT);
111 main (int argc, char **argv)
113 int sizesw = 1, headsw = 1;
115 char *cp, buf[BUFSIZ];
116 char buffer[BUFSIZ], *compfile = NULL;
117 char **argp, **arguments;
125 setlocale(LC_ALL, "");
127 invo_name = r1bindex (argv[0], '/');
129 /* read user profile/context */
132 arguments = getarguments (invo_name, argc, argv, 1);
135 while ((cp = *argp++)) {
136 if (cp[0] == '-' && cp[1] == '\0') {
138 adios (NULL, "cannot specify both standard input and a file");
141 listsw = 0; /* turn off -list if using standard in/out */
142 verbosw = 0; /* turn off -verbose listings */
146 switch (smatch (++cp, switches)) {
148 ambigsw (cp, switches);
151 adios (NULL, "-%s unknown", cp);
154 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
155 print_help (buf, switches, 1);
158 print_version(invo_name);
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);
241 adios (NULL, "only one composition file allowed");
248 if ((cp = getenv ("MM_NOASK")) && !strcmp (cp, "1"))
252 * Check if we've specified an additional profile
254 if ((cp = getenv ("MHBUILD"))) {
255 if ((fp = fopen (cp, "r"))) {
256 readconfig ((struct node **) 0, fp, cp, 0);
259 admonish ("", "unable to read $MHBUILD profile (%s)", cp);
264 * Read the standard profile setup
266 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
267 readconfig ((struct node **) 0, fp, cp, 0);
271 /* Check for public cache location */
272 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
275 /* Check for private cache location */
276 if (!(cache_private = context_find (nmhprivcache)))
277 cache_private = ".cache";
278 cache_private = getcpy (m_maildir (cache_private));
281 * Check for storage directory. If defined, we
282 * will store temporary files there. Else we
283 * store them in standard nmh directory.
285 if ((cp = context_find (nmhstorage)) && *cp)
286 tmp = concat (cp, "/", invo_name, NULL);
288 tmp = add (m_maildir (invo_name), NULL);
290 if (!context_find ("path"))
291 free (path ("./", TFOLDER));
293 /* Check if we have a file to process */
295 adios (NULL, "need to specify a %s composition file", invo_name);
298 * Process the composition file from standard input.
300 if (compfile[0] == '-' && compfile[1] == '\0') {
301 /* copy standard input to temporary file */
302 strncpy (infile, m_mktemp(invo_name, NULL, &fp), sizeof(infile));
303 while (fgets (buffer, BUFSIZ, stdin))
308 /* build the content structures for MIME message */
309 ct = build_mime (infile);
313 /* output MIME message to this temporary file */
314 strncpy (outfile, m_mktemp(invo_name, NULL, &fp_out), sizeof(outfile));
317 /* output the message */
318 output_message_fp (ct, fp_out, outfile);
321 /* output the temp file to standard output */
322 if ((fp = fopen (outfile, "r")) == NULL)
323 adios (outfile, "unable to open");
324 while (fgets (buffer, BUFSIZ, fp))
325 fputs (buffer, stdout);
339 * Process the composition file from a file.
342 /* build the content structures for MIME message */
343 ct = build_mime (compfile);
347 /* output MIME message to this temporary file */
348 strncpy(outfile, m_mktemp2(compfile, invo_name, NULL, &fp_out),
352 /* output the message */
353 output_message_fp (ct, fp_out, outfile);
357 * List the message info
360 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
362 /* Rename composition draft */
363 snprintf (buffer, sizeof(buffer), "%s.orig", m_backup (compfile));
364 if (rename (compfile, buffer) == NOTOK) {
365 adios (compfile, "unable to rename comp draft %s to", buffer);
368 /* Rename output file to take its place */
369 if (rename (outfile, compfile) == NOTOK) {
370 advise (outfile, "unable to rename output %s to", compfile);
371 rename (buffer, compfile);
383 unlink_done (int status)
386 * Check if we need to remove stray