3 * mhbuild.c -- expand/translate MIME composition files
10 #include <h/signals.h>
14 #include <zotnet/mts/mts.h>
17 #include <h/mhparse.h>
18 #include <h/mhcachesbr.h>
20 #ifdef HAVE_SYS_WAIT_H
21 # include <sys/wait.h>
24 static struct swit switches[] = {
32 { "noebcdicsafe", 0 },
48 { "norfc934mode", 0 },
54 { "rcache policy", 0 },
56 { "wcache policy", 0 },
71 extern char *tmp; /* directory to place temp files */
76 extern char *cache_public;
77 extern char *cache_private;
89 static char infile[BUFSIZ];
90 static int unlink_infile = 0;
92 static char outfile[BUFSIZ];
93 static int unlink_outfile = 0;
97 CT build_mime (char *);
98 int output_message (CT, char *);
101 int list_all_messages (CT *, int, int, int, int);
104 void set_endian (void);
107 void free_content (CT);
111 main (int argc, char **argv)
113 int sizesw = 1, headsw = 1;
115 char *cp, buf[BUFSIZ];
116 char buffer[BUFSIZ], *compfile = NULL;
117 char **argp, **arguments;
122 setlocale(LC_ALL, "");
124 invo_name = r1bindex (argv[0], '/');
126 /* read user profile/context */
129 arguments = getarguments (invo_name, argc, argv, 1);
132 while ((cp = *argp++)) {
133 if (cp[0] == '-' && cp[1] == '\0') {
135 adios (NULL, "cannot specify both standard input and a file");
138 listsw = 0; /* turn off -list if using standard in/out */
139 verbosw = 0; /* turn off -verbose listings */
143 switch (smatch (++cp, switches)) {
145 ambigsw (cp, switches);
148 adios (NULL, "-%s unknown", cp);
151 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
152 print_help (buf, switches, 1);
155 print_version(invo_name);
159 icachesw = &rcachesw;
162 icachesw = &wcachesw;
164 if (!(cp = *argp++) || *cp == '-')
165 adios (NULL, "missing argument to %s", argp[-2]);
166 switch (*icachesw = smatch (cp, caches)) {
168 ambigsw (cp, caches);
171 adios (NULL, "%s unknown", cp);
231 adios (NULL, "only one composition file allowed");
238 if ((cp = getenv ("MM_NOASK")) && !strcmp (cp, "1"))
242 * Check if we've specified an additional profile
244 if ((cp = getenv ("MHBUILD"))) {
245 if ((fp = fopen (cp, "r"))) {
246 readconfig ((struct node **) 0, fp, cp, 0);
249 admonish ("", "unable to read $MHBUILD profile (%s)", cp);
254 * Read the standard profile setup
256 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
257 readconfig ((struct node **) 0, fp, cp, 0);
261 /* Check for public cache location */
262 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
265 /* Check for private cache location */
266 if (!(cache_private = context_find (nmhprivcache)))
267 cache_private = ".cache";
268 cache_private = getcpy (m_maildir (cache_private));
271 * Check for storage directory. If defined, we
272 * will store temporary files there. Else we
273 * store them in standard nmh directory.
275 if ((cp = context_find (nmhstorage)) && *cp)
276 tmp = concat (cp, "/", invo_name, NULL);
278 tmp = add (m_maildir (invo_name), NULL);
280 if (!context_find ("path"))
281 free (path ("./", TFOLDER));
283 /* Check if we have a file to process */
285 adios (NULL, "need to specify a %s composition file", invo_name);
288 * Process the composition file from standard input.
290 if (compfile[0] == '-' && compfile[1] == '\0') {
292 /* copy standard input to temporary file */
293 strncpy (infile, m_scratch ("", invo_name), sizeof(infile));
294 if ((fp = fopen (infile, "w")) == NULL)
295 adios (infile, "unable to open");
296 while (fgets (buffer, BUFSIZ, stdin))
301 /* build the content structures for MIME message */
302 ct = build_mime (infile);
306 /* output MIME message to this temporary file */
307 strncpy (outfile, m_scratch ("", invo_name), sizeof(outfile));
310 /* output the message */
311 output_message (ct, outfile);
313 /* output the temp file to standard output */
314 if ((fp = fopen (outfile, "r")) == NULL)
315 adios (outfile, "unable to open");
316 while (fgets (buffer, BUFSIZ, fp))
317 fputs (buffer, stdout);
331 * Process the composition file from a file.
334 /* build the content structures for MIME message */
335 ct = build_mime (compfile);
339 /* output MIME message to this temporary file */
340 strncpy (outfile, m_scratch (compfile, invo_name), sizeof(outfile));
343 /* output the message */
344 output_message (ct, outfile);
347 * List the message info
350 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
352 /* Rename composition draft */
353 snprintf (buffer, sizeof(buffer), "%s.orig", m_backup (compfile));
354 if (rename (compfile, buffer) == NOTOK)
355 adios (compfile, "unable to rename %s to", buffer);
357 /* Rename output file to take its place */
358 if (rename (outfile, compfile) == NOTOK) {
359 advise (outfile, "unable to rename %s to", compfile);
360 rename (buffer, compfile);
374 * Check if we need to remove stray
383 return 1; /* dead code to satisfy the compiler */