]>
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 #define MHBUILD_SWITCHES \
24 X("check", 0, CHECKSW) \
25 X("nocheck", 0, NCHECKSW) \
26 X("directives", 0, DIRECTIVES) \
27 X("nodirectives", 0, NDIRECTIVES) \
28 X("headers", 0, HEADSW) \
29 X("noheaders", 0, NHEADSW) \
30 X("list", 0, LISTSW) \
31 X("nolist", 0, NLISTSW) \
32 X("realsize", 0, SIZESW) \
33 X("norealsize", 0, NSIZESW) \
34 X("rfc934mode", 0, RFC934SW) \
35 X("norfc934mode", 0, NRFC934SW) \
36 X("verbose", 0, VERBSW) \
37 X("noverbose", 0, NVERBSW) \
38 X("rcache policy", 0, RCACHESW) \
39 X("wcache policy", 0, WCACHESW) \
40 X("contentid", 0, CONTENTIDSW) \
41 X("nocontentid", 0, NCONTENTIDSW) \
42 X("version", 0, VERSIONSW) \
43 X("help", 0, HELPSW) \
44 X("debug", -5, DEBUGSW) \
46 #define X(sw, minchars, id) id,
47 DEFINE_SWITCH_ENUM(MHBUILD
);
50 #define X(sw, minchars, id) { sw, minchars, id },
51 DEFINE_SWITCH_ARRAY(MHBUILD
, switches
);
56 extern char *tmp
; /* directory to place temp files */
61 extern char *cache_public
;
62 extern char *cache_private
;
74 static char infile
[BUFSIZ
];
75 static int unlink_infile
= 0;
77 static char outfile
[BUFSIZ
];
78 static int unlink_outfile
= 0;
80 static void unlink_done (int) NORETURN
;
83 CT
build_mime (char *, int);
84 int output_message (CT
, char *);
85 int output_message_fp (CT
, FILE *, char*);
88 int list_all_messages (CT
*, int, int, int, int);
91 void free_content (CT
);
95 main (int argc
, char **argv
)
97 int sizesw
= 1, headsw
= 1, directives
= 1;
99 char *cp
, buf
[BUFSIZ
];
100 char buffer
[BUFSIZ
], *compfile
= NULL
;
101 char **argp
, **arguments
;
109 setlocale(LC_ALL
, "");
111 invo_name
= r1bindex (argv
[0], '/');
113 /* read user profile/context */
116 arguments
= getarguments (invo_name
, argc
, argv
, 1);
119 while ((cp
= *argp
++)) {
120 if (cp
[0] == '-' && cp
[1] == '\0') {
122 adios (NULL
, "cannot specify both standard input and a file");
125 listsw
= 0; /* turn off -list if using standard in/out */
126 verbosw
= 0; /* turn off -verbose listings */
130 switch (smatch (++cp
, switches
)) {
132 ambigsw (cp
, switches
);
135 adios (NULL
, "-%s unknown", cp
);
138 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
139 print_help (buf
, switches
, 1);
142 print_version(invo_name
);
146 icachesw
= &rcachesw
;
149 icachesw
= &wcachesw
;
151 if (!(cp
= *argp
++) || *cp
== '-')
152 adios (NULL
, "missing argument to %s", argp
[-2]);
153 switch (*icachesw
= smatch (cp
, caches
)) {
155 ambigsw (cp
, caches
);
158 adios (NULL
, "%s unknown", cp
);
225 adios (NULL
, "only one composition file allowed");
231 * Check if we've specified an additional profile
233 if ((cp
= getenv ("MHBUILD"))) {
234 if ((fp
= fopen (cp
, "r"))) {
235 readconfig ((struct node
**) 0, fp
, cp
, 0);
238 admonish ("", "unable to read $MHBUILD profile (%s)", cp
);
243 * Read the standard profile setup
245 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
246 readconfig ((struct node
**) 0, fp
, cp
, 0);
250 /* Check for public cache location */
251 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
254 /* Check for private cache location */
255 if (!(cache_private
= context_find (nmhprivcache
)))
256 cache_private
= ".cache";
257 cache_private
= getcpy (m_maildir (cache_private
));
260 * Check for storage directory. If defined, we
261 * will store temporary files there. Else we
262 * store them in standard nmh directory.
264 if ((cp
= context_find (nmhstorage
)) && *cp
)
265 tmp
= concat (cp
, "/", invo_name
, NULL
);
267 tmp
= add (m_maildir (invo_name
), NULL
);
269 if (!context_find ("path"))
270 free (path ("./", TFOLDER
));
272 /* Check if we have a file to process */
274 adios (NULL
, "need to specify a %s composition file", invo_name
);
277 * Process the composition file from standard input.
279 if (compfile
[0] == '-' && compfile
[1] == '\0') {
280 /* copy standard input to temporary file */
281 strncpy (infile
, m_mktemp(invo_name
, NULL
, &fp
), sizeof(infile
));
282 while (fgets (buffer
, BUFSIZ
, stdin
))
287 /* build the content structures for MIME message */
288 ct
= build_mime (infile
, directives
);
292 /* output MIME message to this temporary file */
293 strncpy (outfile
, m_mktemp(invo_name
, NULL
, &fp_out
), sizeof(outfile
));
296 /* output the message */
297 output_message_fp (ct
, fp_out
, outfile
);
300 /* output the temp file to standard output */
301 if ((fp
= fopen (outfile
, "r")) == NULL
)
302 adios (outfile
, "unable to open");
303 while (fgets (buffer
, BUFSIZ
, fp
))
304 fputs (buffer
, stdout
);
318 * Process the composition file from a file.
321 /* build the content structures for MIME message */
322 ct
= build_mime (compfile
, directives
);
326 /* output MIME message to this temporary file */
327 strncpy(outfile
, m_mktemp2(compfile
, invo_name
, NULL
, &fp_out
),
331 /* output the message */
332 output_message_fp (ct
, fp_out
, outfile
);
336 * List the message info
339 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
);
341 /* Rename composition draft */
342 snprintf (buffer
, sizeof(buffer
), "%s.orig", m_backup (compfile
));
343 if (rename (compfile
, buffer
) == NOTOK
) {
344 adios (compfile
, "unable to rename comp draft %s to", buffer
);
347 /* Rename output file to take its place */
348 if (rename (outfile
, compfile
) == NOTOK
) {
349 advise (outfile
, "unable to rename output %s to", compfile
);
350 rename (buffer
, compfile
);
362 unlink_done (int status
)
365 * Check if we need to remove stray