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 *);
109 int list_all_messages (CT *, int, int, int, int);
112 void set_endian (void);
115 void free_content (CT);
119 main (int argc, char **argv)
121 int sizesw = 1, headsw = 1;
123 char *cp, buf[BUFSIZ];
124 char buffer[BUFSIZ], *compfile = NULL;
125 char **argp, **arguments;
132 setlocale(LC_ALL, "");
134 invo_name = r1bindex (argv[0], '/');
136 /* read user profile/context */
139 arguments = getarguments (invo_name, argc, argv, 1);
142 while ((cp = *argp++)) {
143 if (cp[0] == '-' && cp[1] == '\0') {
145 adios (NULL, "cannot specify both standard input and a file");
148 listsw = 0; /* turn off -list if using standard in/out */
149 verbosw = 0; /* turn off -verbose listings */
153 switch (smatch (++cp, switches)) {
155 ambigsw (cp, switches);
158 adios (NULL, "-%s unknown", cp);
161 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
162 print_help (buf, switches, 1);
165 print_version(invo_name);
169 icachesw = &rcachesw;
172 icachesw = &wcachesw;
174 if (!(cp = *argp++) || *cp == '-')
175 adios (NULL, "missing argument to %s", argp[-2]);
176 switch (*icachesw = smatch (cp, caches)) {
178 ambigsw (cp, caches);
181 adios (NULL, "%s unknown", cp);
248 adios (NULL, "only one composition file allowed");
255 if ((cp = getenv ("MM_NOASK")) && !strcmp (cp, "1"))
259 * Check if we've specified an additional profile
261 if ((cp = getenv ("MHBUILD"))) {
262 if ((fp = fopen (cp, "r"))) {
263 readconfig ((struct node **) 0, fp, cp, 0);
266 admonish ("", "unable to read $MHBUILD profile (%s)", cp);
271 * Read the standard profile setup
273 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
274 readconfig ((struct node **) 0, fp, cp, 0);
278 /* Check for public cache location */
279 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
282 /* Check for private cache location */
283 if (!(cache_private = context_find (nmhprivcache)))
284 cache_private = ".cache";
285 cache_private = getcpy (m_maildir (cache_private));
288 * Check for storage directory. If defined, we
289 * will store temporary files there. Else we
290 * store them in standard nmh directory.
292 if ((cp = context_find (nmhstorage)) && *cp)
293 tmp = concat (cp, "/", invo_name, NULL);
295 tmp = add (m_maildir (invo_name), NULL);
297 if (!context_find ("path"))
298 free (path ("./", TFOLDER));
300 /* Check if we have a file to process */
302 adios (NULL, "need to specify a %s composition file", invo_name);
305 * Process the composition file from standard input.
307 if (compfile[0] == '-' && compfile[1] == '\0') {
309 /* copy standard input to temporary file */
310 strncpy (infile, m_scratch ("", invo_name), sizeof(infile));
311 if ((fp = fopen (infile, "w")) == NULL)
312 adios (infile, "unable to open");
313 while (fgets (buffer, BUFSIZ, stdin))
318 /* build the content structures for MIME message */
319 ct = build_mime (infile);
323 /* output MIME message to this temporary file */
324 strncpy (outfile, m_scratch ("", invo_name), sizeof(outfile));
327 /* output the message */
328 output_message (ct, 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_scratch (compfile, invo_name), sizeof(outfile));
360 /* output the message */
361 output_message (ct, outfile);
364 * List the message info
367 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
369 /* Rename composition draft */
370 snprintf (buffer, sizeof(buffer), "%s.orig", m_backup (compfile));
371 if (rename (compfile, buffer) == NOTOK)
372 adios (compfile, "unable to rename %s to", buffer);
374 /* Rename output file to take its place */
375 if (rename (outfile, compfile) == NOTOK) {
376 advise (outfile, "unable to rename %s to", compfile);
377 rename (buffer, compfile);
389 unlink_done (int status)
392 * Check if we need to remove stray