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>
17 #include <h/mhparse.h>
18 #include <h/mhcachesbr.h>
21 #ifdef HAVE_SYS_WAIT_H
22 # include <sys/wait.h>
25 static struct swit switches[] = {
33 { "noebcdicsafe", 0 },
49 { "norfc934mode", 0 },
55 { "rcache policy", 0 },
57 { "wcache policy", 0 },
58 #define CONTENTIDSW 16
60 #define NCONTENTIDSW 17
73 extern char *tmp; /* directory to place temp files */
78 extern char *cache_public;
79 extern char *cache_private;
92 static char infile[BUFSIZ];
93 static int unlink_infile = 0;
95 static char outfile[BUFSIZ];
96 static int unlink_outfile = 0;
98 static void unlink_done(int) NORETURN;
101 CT build_mime(char *);
102 int output_message(CT, char *);
103 int output_message_fp(CT, FILE *, char*);
106 int list_all_messages(CT *, int, int, int, int);
109 void set_endian(void);
112 void free_content(CT);
116 main(int argc, char **argv)
118 int sizesw = 1, headsw = 1;
120 char *cp, buf[BUFSIZ];
121 char buffer[BUFSIZ], *compfile = NULL;
122 char **argp, **arguments;
130 setlocale(LC_ALL, "");
132 invo_name = mhbasename(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 std 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",
175 switch (*icachesw = smatch(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")==0)
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)",
271 ** Read the standard profile setup
273 if ((fp = fopen(cp = etcpath("mhn.defaults"), "r"))) {
274 readconfig((struct node **) 0, fp, cp, 0);
278 /* Check for public cache location */
279 if ((cache_public = context_find(nmhcache)) && *cache_public != '/')
282 /* Check for private cache location */
283 if (!(cache_private = context_find(nmhprivcache)))
284 cache_private = ".cache";
285 cache_private = getcpy(toabsdir(cache_private));
288 ** Check for storage directory. If defined, we
289 ** will store temporary files there. Else we
290 ** store them in standard nmh directory.
292 if ((cp = context_find(nmhstorage)) && *cp)
293 tmp = concat(cp, "/", invo_name, NULL);
295 tmp = getcpy(toabsdir(invo_name));
297 /* Check if we have a file to process */
299 adios(NULL, "need to specify a %s composition file",
303 ** Process the composition file from standard input.
305 if (compfile[0] == '-' && compfile[1] == '\0') {
306 /* copy standard input to temporary file */
307 strncpy(infile, m_mktemp(invo_name, NULL, &fp),
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),
324 /* output the message */
325 output_message_fp(ct, fp_out, 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_mktemp2(compfile, invo_name, NULL, &fp_out),
359 /* output the message */
360 output_message_fp(ct, fp_out, outfile);
364 ** List the message info
367 list_all_messages(cts, headsw, sizesw, verbosw, debugsw);
369 /* Rename composition draft */
370 snprintf(buffer, sizeof(buffer), "%s.orig", m_backup(compfile));
371 if (rename(compfile, buffer) == NOTOK) {
372 adios(compfile, "unable to rename comp draft %s to", buffer);
375 /* Rename output file to take its place */
376 if (rename(outfile, compfile) == NOTOK) {
377 advise(outfile, "unable to rename output %s to", compfile);
378 rename(buffer, compfile);
390 unlink_done(int status)
393 ** Check if we need to remove stray temporary files.