]>
diplodocus.org Git - nmh/blob - uip/mhbuild.c
3 * mhbuild.c -- expand/translate MIME composition files
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #include <h/signals.h>
19 #include <h/mhparse.h>
20 #include <h/mhcachesbr.h>
23 static struct swit switches
[] = {
31 { "nodirectives", 0 },
35 { "noebcdicsafe", 0 },
51 { "norfc934mode", 0 },
57 { "rcache policy", 0 },
59 { "wcache policy", 0 },
60 #define CONTENTIDSW 18
62 #define NCONTENTIDSW 19
75 extern char *tmp
; /* directory to place temp files */
80 extern char *cache_public
;
81 extern char *cache_private
;
94 static char infile
[BUFSIZ
];
95 static int unlink_infile
= 0;
97 static char outfile
[BUFSIZ
];
98 static int unlink_outfile
= 0;
100 static void unlink_done (int) NORETURN
;
103 CT
build_mime (char *, int);
104 int output_message (CT
, char *);
105 int output_message_fp (CT
, FILE *, char*);
108 int list_all_messages (CT
*, int, int, int, int);
111 void free_content (CT
);
115 main (int argc
, char **argv
)
117 int sizesw
= 1, headsw
= 1, directives
= 1;
119 char *cp
, buf
[BUFSIZ
];
120 char buffer
[BUFSIZ
], *compfile
= NULL
;
121 char **argp
, **arguments
;
129 setlocale(LC_ALL
, "");
131 invo_name
= r1bindex (argv
[0], '/');
133 /* read user profile/context */
136 arguments
= getarguments (invo_name
, argc
, argv
, 1);
139 while ((cp
= *argp
++)) {
140 if (cp
[0] == '-' && cp
[1] == '\0') {
142 adios (NULL
, "cannot specify both standard input and a file");
145 listsw
= 0; /* turn off -list if using standard in/out */
146 verbosw
= 0; /* turn off -verbose listings */
150 switch (smatch (++cp
, switches
)) {
152 ambigsw (cp
, switches
);
155 adios (NULL
, "-%s unknown", cp
);
158 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
159 print_help (buf
, switches
, 1);
162 print_version(invo_name
);
166 icachesw
= &rcachesw
;
169 icachesw
= &wcachesw
;
171 if (!(cp
= *argp
++) || *cp
== '-')
172 adios (NULL
, "missing argument to %s", argp
[-2]);
173 switch (*icachesw
= smatch (cp
, caches
)) {
175 ambigsw (cp
, caches
);
178 adios (NULL
, "%s unknown", cp
);
252 adios (NULL
, "only one composition file allowed");
260 * Check if we've specified an additional profile
262 if ((cp
= getenv ("MHBUILD"))) {
263 if ((fp
= fopen (cp
, "r"))) {
264 readconfig ((struct node
**) 0, fp
, cp
, 0);
267 admonish ("", "unable to read $MHBUILD profile (%s)", cp
);
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", invo_name
);
306 * Process the composition file from standard input.
308 if (compfile
[0] == '-' && compfile
[1] == '\0') {
309 /* copy standard input to temporary file */
310 strncpy (infile
, m_mktemp(invo_name
, NULL
, &fp
), sizeof(infile
));
311 while (fgets (buffer
, BUFSIZ
, stdin
))
316 /* build the content structures for MIME message */
317 ct
= build_mime (infile
, directives
);
321 /* output MIME message to this temporary file */
322 strncpy (outfile
, m_mktemp(invo_name
, NULL
, &fp_out
), sizeof(outfile
));
325 /* output the message */
326 output_message_fp (ct
, fp_out
, outfile
);
329 /* output the temp file to standard output */
330 if ((fp
= fopen (outfile
, "r")) == NULL
)
331 adios (outfile
, "unable to open");
332 while (fgets (buffer
, BUFSIZ
, fp
))
333 fputs (buffer
, stdout
);
347 * Process the composition file from a file.
350 /* build the content structures for MIME message */
351 ct
= build_mime (compfile
, directives
);
355 /* output MIME message to this temporary file */
356 strncpy(outfile
, m_mktemp2(compfile
, invo_name
, NULL
, &fp_out
),
360 /* output the message */
361 output_message_fp (ct
, fp_out
, outfile
);
365 * List the message info
368 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
);
370 /* Rename composition draft */
371 snprintf (buffer
, sizeof(buffer
), "%s.orig", m_backup (compfile
));
372 if (rename (compfile
, buffer
) == NOTOK
) {
373 adios (compfile
, "unable to rename comp draft %s to", buffer
);
376 /* Rename output file to take its place */
377 if (rename (outfile
, compfile
) == NOTOK
) {
378 advise (outfile
, "unable to rename output %s to", compfile
);
379 rename (buffer
, compfile
);
391 unlink_done (int status
)
394 * Check if we need to remove stray