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 },
73 extern char *tmp; /* directory to place temp files */
78 extern char *cache_public;
79 extern char *cache_private;
91 static char infile[BUFSIZ];
92 static int unlink_infile = 0;
94 static char outfile[BUFSIZ];
95 static int unlink_outfile = 0;
99 CT build_mime (char *);
100 int output_message (CT, char *);
103 int list_all_messages (CT *, int, int, int, int);
106 void set_endian (void);
109 void free_content (CT);
113 main (int argc, char **argv)
115 int sizesw = 1, headsw = 1;
117 char *cp, buf[BUFSIZ];
118 char buffer[BUFSIZ], *compfile = NULL;
119 char **argp, **arguments;
124 setlocale(LC_ALL, "");
126 invo_name = r1bindex (argv[0], '/');
128 /* read user profile/context */
131 arguments = getarguments (invo_name, argc, argv, 1);
134 while ((cp = *argp++)) {
135 if (cp[0] == '-' && cp[1] == '\0') {
137 adios (NULL, "cannot specify both standard input and a file");
140 listsw = 0; /* turn off -list if using standard in/out */
141 verbosw = 0; /* turn off -verbose listings */
145 switch (smatch (++cp, switches)) {
147 ambigsw (cp, switches);
150 adios (NULL, "-%s unknown", cp);
153 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
154 print_help (buf, switches, 1);
157 print_version(invo_name);
161 icachesw = &rcachesw;
164 icachesw = &wcachesw;
166 if (!(cp = *argp++) || *cp == '-')
167 adios (NULL, "missing argument to %s", argp[-2]);
168 switch (*icachesw = smatch (cp, caches)) {
170 ambigsw (cp, caches);
173 adios (NULL, "%s unknown", cp);
233 adios (NULL, "only one composition file allowed");
240 if ((cp = getenv ("MM_NOASK")) && !strcmp (cp, "1"))
244 * Check if we've specified an additional profile
246 if ((cp = getenv ("MHBUILD"))) {
247 if ((fp = fopen (cp, "r"))) {
248 readconfig ((struct node **) 0, fp, cp, 0);
251 admonish ("", "unable to read $MHBUILD profile (%s)", cp);
256 * Read the standard profile setup
258 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
259 readconfig ((struct node **) 0, fp, cp, 0);
263 /* Check for public cache location */
264 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
267 /* Check for private cache location */
268 if (!(cache_private = context_find (nmhprivcache)))
269 cache_private = ".cache";
270 cache_private = getcpy (m_maildir (cache_private));
273 * Check for storage directory. If defined, we
274 * will store temporary files there. Else we
275 * store them in standard nmh directory.
277 if ((cp = context_find (nmhstorage)) && *cp)
278 tmp = concat (cp, "/", invo_name, NULL);
280 tmp = add (m_maildir (invo_name), NULL);
282 if (!context_find ("path"))
283 free (path ("./", TFOLDER));
285 /* Check if we have a file to process */
287 adios (NULL, "need to specify a %s composition file", invo_name);
290 * Process the composition file from standard input.
292 if (compfile[0] == '-' && compfile[1] == '\0') {
294 /* copy standard input to temporary file */
295 strncpy (infile, m_scratch ("", invo_name), sizeof(infile));
296 if ((fp = fopen (infile, "w")) == NULL)
297 adios (infile, "unable to open");
298 while (fgets (buffer, BUFSIZ, stdin))
303 /* build the content structures for MIME message */
304 ct = build_mime (infile);
308 /* output MIME message to this temporary file */
309 strncpy (outfile, m_scratch ("", invo_name), sizeof(outfile));
312 /* output the message */
313 output_message (ct, outfile);
315 /* output the temp file to standard output */
316 if ((fp = fopen (outfile, "r")) == NULL)
317 adios (outfile, "unable to open");
318 while (fgets (buffer, BUFSIZ, fp))
319 fputs (buffer, stdout);
333 * Process the composition file from a file.
336 /* build the content structures for MIME message */
337 ct = build_mime (compfile);
341 /* output MIME message to this temporary file */
342 strncpy (outfile, m_scratch (compfile, invo_name), sizeof(outfile));
345 /* output the message */
346 output_message (ct, outfile);
349 * List the message info
352 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
354 /* Rename composition draft */
355 snprintf (buffer, sizeof(buffer), "%s.orig", m_backup (compfile));
356 if (rename (compfile, buffer) == NOTOK)
357 adios (compfile, "unable to rename %s to", buffer);
359 /* Rename output file to take its place */
360 if (rename (outfile, compfile) == NOTOK) {
361 advise (outfile, "unable to rename %s to", compfile);
362 rename (buffer, compfile);
376 * Check if we need to remove stray
385 return 1; /* dead code to satisfy the compiler */