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",
176 switch (*icachesw = smatch(cp, caches)) {
181 adios(NULL, "%s unknown", cp);
248 adios(NULL, "only one composition file allowed");
255 if ((cp = getenv("MM_NOASK")) && !strcmp(cp, "1"))
259 ** Check if we've specified an additional profile
261 if ((cp = getenv("MHBUILD"))) {
262 if ((fp = fopen(cp, "r"))) {
263 readconfig((struct node **) 0, fp, cp, 0);
266 admonish("", "unable to read $MHBUILD profile (%s)",
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",
307 ** Process the composition file from standard input.
309 if (compfile[0] == '-' && compfile[1] == '\0') {
310 /* copy standard input to temporary file */
311 strncpy(infile, m_mktemp(invo_name, NULL, &fp),
313 while (fgets(buffer, BUFSIZ, stdin))
318 /* build the content structures for MIME message */
319 ct = build_mime(infile);
323 /* output MIME message to this temporary file */
324 strncpy(outfile, m_mktemp(invo_name, NULL, &fp_out),
328 /* output the message */
329 output_message_fp(ct, fp_out, outfile);
332 /* output the temp file to standard output */
333 if ((fp = fopen(outfile, "r")) == NULL)
334 adios(outfile, "unable to open");
335 while (fgets(buffer, BUFSIZ, fp))
336 fputs(buffer, stdout);
350 ** Process the composition file from a file.
353 /* build the content structures for MIME message */
354 ct = build_mime(compfile);
358 /* output MIME message to this temporary file */
359 strncpy(outfile, m_mktemp2(compfile, invo_name, NULL, &fp_out),
363 /* output the message */
364 output_message_fp(ct, fp_out, outfile);
368 ** List the message info
371 list_all_messages(cts, headsw, sizesw, verbosw, debugsw);
373 /* Rename composition draft */
374 snprintf(buffer, sizeof(buffer), "%s.orig", m_backup(compfile));
375 if (rename(compfile, buffer) == NOTOK) {
376 adios(compfile, "unable to rename comp draft %s to", buffer);
379 /* Rename output file to take its place */
380 if (rename(outfile, compfile) == NOTOK) {
381 advise(outfile, "unable to rename output %s to", compfile);
382 rename(buffer, compfile);
394 unlink_done(int status)
397 ** Check if we need to remove stray temporary files.