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;
103 static int unlink_done (int);
106 CT build_mime (char *);
107 int output_message (CT, 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;
133 setlocale(LC_ALL, "");
135 invo_name = r1bindex (argv[0], '/');
137 /* read user profile/context */
140 arguments = getarguments (invo_name, argc, argv, 1);
143 while ((cp = *argp++)) {
144 if (cp[0] == '-' && cp[1] == '\0') {
146 adios (NULL, "cannot specify both standard input and a file");
149 listsw = 0; /* turn off -list if using standard in/out */
150 verbosw = 0; /* turn off -verbose listings */
154 switch (smatch (++cp, switches)) {
156 ambigsw (cp, switches);
159 adios (NULL, "-%s unknown", cp);
162 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
163 print_help (buf, switches, 1);
166 print_version(invo_name);
170 icachesw = &rcachesw;
173 icachesw = &wcachesw;
175 if (!(cp = *argp++) || *cp == '-')
176 adios (NULL, "missing argument to %s", argp[-2]);
177 switch (*icachesw = smatch (cp, caches)) {
179 ambigsw (cp, caches);
182 adios (NULL, "%s unknown", cp);
249 adios (NULL, "only one composition file allowed");
256 if ((cp = getenv ("MM_NOASK")) && !strcmp (cp, "1"))
260 * Check if we've specified an additional profile
262 if ((cp = getenv ("MHBUILD"))) {
263 if ((fp = fopen (cp, "r"))) {
264 readconfig ((struct node **) 0, fp, cp, 0);
267 admonish ("", "unable to read $MHBUILD profile (%s)", cp);
272 * Read the standard profile setup
274 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
275 readconfig ((struct node **) 0, fp, cp, 0);
279 /* Check for public cache location */
280 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
283 /* Check for private cache location */
284 if (!(cache_private = context_find (nmhprivcache)))
285 cache_private = ".cache";
286 cache_private = getcpy (m_maildir (cache_private));
289 * Check for storage directory. If defined, we
290 * will store temporary files there. Else we
291 * store them in standard nmh directory.
293 if ((cp = context_find (nmhstorage)) && *cp)
294 tmp = concat (cp, "/", invo_name, NULL);
296 tmp = add (m_maildir (invo_name), NULL);
298 if (!context_find ("path"))
299 free (path ("./", TFOLDER));
301 /* Check if we have a file to process */
303 adios (NULL, "need to specify a %s composition file", invo_name);
306 * Process the composition file from standard input.
308 if (compfile[0] == '-' && compfile[1] == '\0') {
310 /* copy standard input to temporary file */
311 strncpy (infile, m_scratch ("", invo_name), sizeof(infile));
312 if ((fp = fopen (infile, "w")) == NULL)
313 adios (infile, "unable to open");
314 while (fgets (buffer, BUFSIZ, stdin))
319 /* build the content structures for MIME message */
320 ct = build_mime (infile);
324 /* output MIME message to this temporary file */
325 strncpy (outfile, m_scratch ("", invo_name), sizeof(outfile));
328 /* output the message */
329 output_message (ct, outfile);
331 /* output the temp file to standard output */
332 if ((fp = fopen (outfile, "r")) == NULL)
333 adios (outfile, "unable to open");
334 while (fgets (buffer, BUFSIZ, fp))
335 fputs (buffer, stdout);
349 * Process the composition file from a file.
352 /* build the content structures for MIME message */
353 ct = build_mime (compfile);
357 /* output MIME message to this temporary file */
358 strncpy (outfile, m_scratch (compfile, invo_name), sizeof(outfile));
361 /* output the message */
362 output_message (ct, outfile);
365 * List the message info
368 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
370 /* Rename composition draft */
371 snprintf (buffer, sizeof(buffer), "%s.orig", m_backup (compfile));
372 if (rename (compfile, buffer) == NOTOK)
373 adios (compfile, "unable to rename %s to", buffer);
375 /* Rename output file to take its place */
376 if (rename (outfile, compfile) == NOTOK) {
377 advise (outfile, "unable to rename %s to", compfile);
378 rename (buffer, compfile);
389 unlink_done (int status)
392 * Check if we need to remove stray
401 return 1; /* dead code to satisfy the compiler */