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
78 extern char *tmp; /* directory to place temp files */
83 extern char *cache_public;
84 extern char *cache_private;
97 static char infile[BUFSIZ];
98 static int unlink_infile = 0;
100 static char outfile[BUFSIZ];
101 static int unlink_outfile = 0;
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;
130 setlocale(LC_ALL, "");
132 invo_name = r1bindex (argv[0], '/');
134 /* read user profile/context */
137 arguments = getarguments (invo_name, argc, argv, 1);
140 while ((cp = *argp++)) {
141 if (cp[0] == '-' && cp[1] == '\0') {
143 adios (NULL, "cannot specify both standard input and a file");
146 listsw = 0; /* turn off -list if using standard in/out */
147 verbosw = 0; /* turn off -verbose listings */
151 switch (smatch (++cp, switches)) {
153 ambigsw (cp, switches);
156 adios (NULL, "-%s unknown", cp);
159 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
160 print_help (buf, switches, 1);
163 print_version(invo_name);
167 icachesw = &rcachesw;
170 icachesw = &wcachesw;
172 if (!(cp = *argp++) || *cp == '-')
173 adios (NULL, "missing argument to %s", argp[-2]);
174 switch (*icachesw = smatch (cp, caches)) {
176 ambigsw (cp, caches);
179 adios (NULL, "%s unknown", cp);
246 adios (NULL, "only one composition file allowed");
253 if ((cp = getenv ("MM_NOASK")) && !strcmp (cp, "1"))
257 * Check if we've specified an additional profile
259 if ((cp = getenv ("MHBUILD"))) {
260 if ((fp = fopen (cp, "r"))) {
261 readconfig ((struct node **) 0, fp, cp, 0);
264 admonish ("", "unable to read $MHBUILD profile (%s)", cp);
269 * Read the standard profile setup
271 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
272 readconfig ((struct node **) 0, fp, cp, 0);
276 /* Check for public cache location */
277 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
280 /* Check for private cache location */
281 if (!(cache_private = context_find (nmhprivcache)))
282 cache_private = ".cache";
283 cache_private = getcpy (m_maildir (cache_private));
286 * Check for storage directory. If defined, we
287 * will store temporary files there. Else we
288 * store them in standard nmh directory.
290 if ((cp = context_find (nmhstorage)) && *cp)
291 tmp = concat (cp, "/", invo_name, NULL);
293 tmp = add (m_maildir (invo_name), NULL);
295 if (!context_find ("path"))
296 free (path ("./", TFOLDER));
298 /* Check if we have a file to process */
300 adios (NULL, "need to specify a %s composition file", invo_name);
303 * Process the composition file from standard input.
305 if (compfile[0] == '-' && compfile[1] == '\0') {
307 /* copy standard input to temporary file */
308 strncpy (infile, m_scratch ("", invo_name), sizeof(infile));
309 if ((fp = fopen (infile, "w")) == NULL)
310 adios (infile, "unable to open");
311 while (fgets (buffer, BUFSIZ, stdin))
316 /* build the content structures for MIME message */
317 ct = build_mime (infile);
321 /* output MIME message to this temporary file */
322 strncpy (outfile, m_scratch ("", invo_name), sizeof(outfile));
325 /* output the message */
326 output_message (ct, outfile);
328 /* output the temp file to standard output */
329 if ((fp = fopen (outfile, "r")) == NULL)
330 adios (outfile, "unable to open");
331 while (fgets (buffer, BUFSIZ, fp))
332 fputs (buffer, stdout);
346 * Process the composition file from a file.
349 /* build the content structures for MIME message */
350 ct = build_mime (compfile);
354 /* output MIME message to this temporary file */
355 strncpy (outfile, m_scratch (compfile, invo_name), sizeof(outfile));
358 /* output the message */
359 output_message (ct, outfile);
362 * List the message info
365 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
367 /* Rename composition draft */
368 snprintf (buffer, sizeof(buffer), "%s.orig", m_backup (compfile));
369 if (rename (compfile, buffer) == NOTOK)
370 adios (compfile, "unable to rename %s to", buffer);
372 /* Rename output file to take its place */
373 if (rename (outfile, compfile) == NOTOK) {
374 advise (outfile, "unable to rename %s to", compfile);
375 rename (buffer, compfile);
389 * Check if we need to remove stray
398 return 1; /* dead code to satisfy the compiler */