2 ** mhbuild.c -- expand/translate MIME composition files
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
11 #include <h/signals.h>
18 #include <h/mhparse.h>
19 #include <h/mhcachesbr.h>
22 #ifdef HAVE_SYS_WAIT_H
23 # include <sys/wait.h>
26 static struct swit switches[] = {
34 { "noebcdicsafe", 0 },
50 { "norfc934mode", 0 },
56 { "rcache policy", 0 },
58 { "wcache policy", 0 },
59 #define CONTENTIDSW 16
61 #define NCONTENTIDSW 17
74 extern char *tmp; /* directory to place temp files */
79 extern char *cache_public;
80 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;
99 static void unlink_done (int) NORETURN;
102 CT build_mime (char *);
103 int output_message (CT, char *);
104 int output_message_fp (CT, FILE *, char*);
107 int list_all_messages (CT *, int, int, int, int);
110 void set_endian (void);
113 void free_content (CT);
117 main (int argc, char **argv)
119 int sizesw = 1, headsw = 1;
121 char *cp, buf[BUFSIZ];
122 char buffer[BUFSIZ], *compfile = NULL;
123 char **argp, **arguments;
131 setlocale(LC_ALL, "");
133 invo_name = r1bindex (argv[0], '/');
135 /* read user profile/context */
138 arguments = getarguments (invo_name, argc, argv, 1);
141 while ((cp = *argp++)) {
142 if (cp[0] == '-' && cp[1] == '\0') {
144 adios (NULL, "cannot specify both standard input and a file");
147 listsw = 0; /* turn off -list if using std in/out */
148 verbosw = 0; /* turn off -verbose listings */
152 switch (smatch (++cp, switches)) {
154 ambigsw (cp, switches);
157 adios (NULL, "-%s unknown", cp);
160 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
161 print_help (buf, switches, 1);
164 print_version(invo_name);
168 icachesw = &rcachesw;
171 icachesw = &wcachesw;
173 if (!(cp = *argp++) || *cp == '-')
174 adios (NULL, "missing argument to %s", argp[-2]);
175 switch (*icachesw = smatch (cp, caches)) {
177 ambigsw (cp, caches);
180 adios (NULL, "%s unknown", cp);
247 adios (NULL, "only one composition file allowed");
254 if ((cp = getenv ("MM_NOASK")) && !strcmp (cp, "1"))
258 ** Check if we've specified an additional profile
260 if ((cp = getenv ("MHBUILD"))) {
261 if ((fp = fopen (cp, "r"))) {
262 readconfig ((struct node **) 0, fp, cp, 0);
265 admonish ("", "unable to read $MHBUILD profile (%s)", cp);
270 ** Read the standard profile setup
272 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
273 readconfig ((struct node **) 0, fp, cp, 0);
277 /* Check for public cache location */
278 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
281 /* Check for private cache location */
282 if (!(cache_private = context_find (nmhprivcache)))
283 cache_private = ".cache";
284 cache_private = getcpy (m_maildir (cache_private));
287 ** Check for storage directory. If defined, we
288 ** will store temporary files there. Else we
289 ** store them in standard nmh directory.
291 if ((cp = context_find (nmhstorage)) && *cp)
292 tmp = concat (cp, "/", invo_name, NULL);
294 tmp = add (m_maildir (invo_name), NULL);
296 if (!context_find ("path"))
297 free (path ("./", TFOLDER));
299 /* Check if we have a file to process */
301 adios (NULL, "need to specify a %s composition file", invo_name);
304 ** Process the composition file from standard input.
306 if (compfile[0] == '-' && compfile[1] == '\0') {
307 /* copy standard input to temporary file */
308 strncpy (infile, m_mktemp(invo_name, NULL, &fp), sizeof(infile));
309 while (fgets (buffer, BUFSIZ, stdin))
314 /* build the content structures for MIME message */
315 ct = build_mime (infile);
319 /* output MIME message to this temporary file */
320 strncpy (outfile, m_mktemp(invo_name, NULL, &fp_out), sizeof(outfile));
323 /* output the message */
324 output_message_fp (ct, fp_out, outfile);
327 /* output the temp file to standard output */
328 if ((fp = fopen (outfile, "r")) == NULL)
329 adios (outfile, "unable to open");
330 while (fgets (buffer, BUFSIZ, fp))
331 fputs (buffer, stdout);
345 ** Process the composition file from a file.
348 /* build the content structures for MIME message */
349 ct = build_mime (compfile);
353 /* output MIME message to this temporary file */
354 strncpy(outfile, m_mktemp2(compfile, invo_name, NULL, &fp_out),
358 /* output the message */
359 output_message_fp (ct, fp_out, outfile);
363 ** List the message info
366 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
368 /* Rename composition draft */
369 snprintf (buffer, sizeof(buffer), "%s.orig", m_backup (compfile));
370 if (rename (compfile, buffer) == NOTOK) {
371 adios (compfile, "unable to rename comp draft %s to", buffer);
374 /* Rename output file to take its place */
375 if (rename (outfile, compfile) == NOTOK) {
376 advise (outfile, "unable to rename output %s to", compfile);
377 rename (buffer, compfile);
389 unlink_done (int status)
392 ** Check if we need to remove stray temporary files.