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>
24 #ifdef HAVE_SYS_WAIT_H
25 # include <sys/wait.h>
28 static struct swit switches[] = {
36 { "noebcdicsafe", 0 },
52 { "norfc934mode", 0 },
58 { "rcache policy", 0 },
60 { "wcache policy", 0 },
75 extern char *tmp; /* directory to place temp files */
80 extern char *cache_public;
81 extern char *cache_private;
93 static char infile[BUFSIZ];
94 static int unlink_infile = 0;
96 static char outfile[BUFSIZ];
97 static int unlink_outfile = 0;
101 CT build_mime (char *);
102 int output_message (CT, char *);
105 int list_all_messages (CT *, int, int, int, int);
108 void set_endian (void);
111 void free_content (CT);
115 main (int argc, char **argv)
117 int sizesw = 1, headsw = 1;
119 char *cp, buf[BUFSIZ];
120 char buffer[BUFSIZ], *compfile = NULL;
121 char **argp, **arguments;
126 setlocale(LC_ALL, "");
128 invo_name = r1bindex (argv[0], '/');
130 /* read user profile/context */
133 arguments = getarguments (invo_name, argc, argv, 1);
136 while ((cp = *argp++)) {
137 if (cp[0] == '-' && cp[1] == '\0') {
139 adios (NULL, "cannot specify both standard input and a file");
142 listsw = 0; /* turn off -list if using standard in/out */
143 verbosw = 0; /* turn off -verbose listings */
147 switch (smatch (++cp, switches)) {
149 ambigsw (cp, switches);
152 adios (NULL, "-%s unknown", cp);
155 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
156 print_help (buf, switches, 1);
159 print_version(invo_name);
163 icachesw = &rcachesw;
166 icachesw = &wcachesw;
168 if (!(cp = *argp++) || *cp == '-')
169 adios (NULL, "missing argument to %s", argp[-2]);
170 switch (*icachesw = smatch (cp, caches)) {
172 ambigsw (cp, caches);
175 adios (NULL, "%s unknown", cp);
235 adios (NULL, "only one composition file allowed");
242 if ((cp = getenv ("MM_NOASK")) && !strcmp (cp, "1"))
246 * Check if we've specified an additional profile
248 if ((cp = getenv ("MHBUILD"))) {
249 if ((fp = fopen (cp, "r"))) {
250 readconfig ((struct node **) 0, fp, cp, 0);
253 admonish ("", "unable to read $MHBUILD profile (%s)", cp);
258 * Read the standard profile setup
260 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
261 readconfig ((struct node **) 0, fp, cp, 0);
265 /* Check for public cache location */
266 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
269 /* Check for private cache location */
270 if (!(cache_private = context_find (nmhprivcache)))
271 cache_private = ".cache";
272 cache_private = getcpy (m_maildir (cache_private));
275 * Check for storage directory. If defined, we
276 * will store temporary files there. Else we
277 * store them in standard nmh directory.
279 if ((cp = context_find (nmhstorage)) && *cp)
280 tmp = concat (cp, "/", invo_name, NULL);
282 tmp = add (m_maildir (invo_name), NULL);
284 if (!context_find ("path"))
285 free (path ("./", TFOLDER));
287 /* Check if we have a file to process */
289 adios (NULL, "need to specify a %s composition file", invo_name);
292 * Process the composition file from standard input.
294 if (compfile[0] == '-' && compfile[1] == '\0') {
296 /* copy standard input to temporary file */
297 strncpy (infile, m_scratch ("", invo_name), sizeof(infile));
298 if ((fp = fopen (infile, "w")) == NULL)
299 adios (infile, "unable to open");
300 while (fgets (buffer, BUFSIZ, stdin))
305 /* build the content structures for MIME message */
306 ct = build_mime (infile);
310 /* output MIME message to this temporary file */
311 strncpy (outfile, m_scratch ("", invo_name), sizeof(outfile));
314 /* output the message */
315 output_message (ct, outfile);
317 /* output the temp file to standard output */
318 if ((fp = fopen (outfile, "r")) == NULL)
319 adios (outfile, "unable to open");
320 while (fgets (buffer, BUFSIZ, fp))
321 fputs (buffer, stdout);
335 * Process the composition file from a file.
338 /* build the content structures for MIME message */
339 ct = build_mime (compfile);
343 /* output MIME message to this temporary file */
344 strncpy (outfile, m_scratch (compfile, invo_name), sizeof(outfile));
347 /* output the message */
348 output_message (ct, outfile);
351 * List the message info
354 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
356 /* Rename composition draft */
357 snprintf (buffer, sizeof(buffer), "%s.orig", m_backup (compfile));
358 if (rename (compfile, buffer) == NOTOK)
359 adios (compfile, "unable to rename %s to", buffer);
361 /* Rename output file to take its place */
362 if (rename (outfile, compfile) == NOTOK) {
363 advise (outfile, "unable to rename %s to", compfile);
364 rename (buffer, compfile);
378 * Check if we need to remove stray
387 return 1; /* dead code to satisfy the compiler */