]>
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>
17 #include <h/mhparse.h>
18 #include <h/mhcachesbr.h>
21 #define MHBUILD_SWITCHES \
22 X("auto", 0, AUTOSW) \
23 X("noauto", 0, NAUTOSW) \
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("headerencoding encoding-algorithm", 0, HEADERENCSW) \
43 X("autoheaderencoding", 0, AUTOHEADERENCSW) \
44 X("version", 0, VERSIONSW) \
45 X("help", 0, HELPSW) \
46 X("debug", -5, DEBUGSW) \
48 #define X(sw, minchars, id) id,
49 DEFINE_SWITCH_ENUM(MHBUILD
);
52 #define X(sw, minchars, id) { sw, minchars, id },
53 DEFINE_SWITCH_ARRAY(MHBUILD
, switches
);
56 #define MIMEENCODING_SWITCHES \
57 X("base64", 0, BASE64SW) \
58 X("quoted-printable", 0, QUOTEDPRINTSW) \
60 #define X(sw, minchars, id) id,
61 DEFINE_SWITCH_ENUM(MIMEENCODING
);
64 #define X(sw, minchars, id) { sw, minchars, id },
65 DEFINE_SWITCH_ARRAY(MIMEENCODING
, encodingswitches
);
69 extern char *tmp
; /* directory to place temp files */
74 extern char *cache_public
;
75 extern char *cache_private
;
87 static char infile
[BUFSIZ
];
88 static int unlink_infile
= 0;
90 static char outfile
[BUFSIZ
];
91 static int unlink_outfile
= 0;
93 static void unlink_done (int) NORETURN
;
96 int output_message (CT
, char *);
97 int output_message_fp (CT
, FILE *, char*);
100 int list_all_messages (CT
*, int, int, int, int);
104 main (int argc
, char **argv
)
106 int sizesw
= 1, headsw
= 1, directives
= 1, autobuild
= 0;
108 char *cp
, buf
[BUFSIZ
];
109 char buffer
[BUFSIZ
], *compfile
= NULL
;
110 char **argp
, **arguments
;
114 int header_encoding
= CE_UNKNOWN
;
118 setlocale(LC_ALL
, "");
119 invo_name
= r1bindex (argv
[0], '/');
121 /* read user profile/context */
124 arguments
= getarguments (invo_name
, argc
, argv
, 1);
127 while ((cp
= *argp
++)) {
128 if (cp
[0] == '-' && cp
[1] == '\0') {
130 adios (NULL
, "cannot specify both standard input and a file");
133 listsw
= 0; /* turn off -list if using standard in/out */
134 verbosw
= 0; /* turn off -verbose listings */
138 switch (smatch (++cp
, switches
)) {
140 ambigsw (cp
, switches
);
143 adios (NULL
, "-%s unknown", cp
);
146 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
147 print_help (buf
, switches
, 1);
150 print_version(invo_name
);
154 /* -auto implies -nodirectives */
160 * We're turning directives back on since this is likely here
161 * to override a profile entry
168 icachesw
= &rcachesw
;
171 icachesw
= &wcachesw
;
173 if (!(cp
= *argp
++) || *cp
== '-')
174 adios (NULL
, "missing argument to %s", argp
[-2]);
175 switch (*icachesw
= smatch (cp
, caches
)) {
177 ambigsw (cp
, caches
);
180 adios (NULL
, "%s unknown", cp
);
238 if (!(cp
= *argp
++) || *cp
== '-')
239 adios (NULL
, "missing argument to %s", argp
[-2]);
240 switch (encoding
= smatch (cp
, encodingswitches
)) {
242 ambigsw (cp
, encodingswitches
);
245 adios (NULL
, "%s unknown encoding algorithm", cp
);
247 header_encoding
= CE_BASE64
;
250 header_encoding
= CE_QUOTED
;
253 adios (NULL
, "Internal error: algorithm %s", cp
);
258 case AUTOHEADERENCSW
:
259 header_encoding
= CE_UNKNOWN
;
274 adios (NULL
, "only one composition file allowed");
280 * Check if we've specified an additional profile
282 if ((cp
= getenv ("MHBUILD"))) {
283 if ((fp
= fopen (cp
, "r"))) {
284 readconfig ((struct node
**) 0, fp
, cp
, 0);
287 admonish ("", "unable to read $MHBUILD profile (%s)", cp
);
292 * Read the standard profile setup
294 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
295 readconfig ((struct node
**) 0, fp
, cp
, 0);
299 /* Check for public cache location */
300 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
303 /* Check for private cache location */
304 if (!(cache_private
= context_find (nmhprivcache
)))
305 cache_private
= ".cache";
306 cache_private
= getcpy (m_maildir (cache_private
));
309 * Check for storage directory. If defined, we
310 * will store temporary files there. Else we
311 * store them in standard nmh directory.
313 if ((cp
= context_find (nmhstorage
)) && *cp
)
314 tmp
= concat (cp
, "/", invo_name
, NULL
);
316 tmp
= add (m_maildir (invo_name
), NULL
);
318 if (!context_find ("path"))
319 free (path ("./", TFOLDER
));
321 /* Check if we have a file to process */
323 adios (NULL
, "need to specify a %s composition file", invo_name
);
326 * Process the composition file from standard input.
328 if (compfile
[0] == '-' && compfile
[1] == '\0') {
329 /* copy standard input to temporary file */
330 strncpy (infile
, m_mktemp(invo_name
, NULL
, &fp
), sizeof(infile
));
331 while (fgets (buffer
, BUFSIZ
, stdin
))
336 /* build the content structures for MIME message */
337 ct
= build_mime (infile
, autobuild
, directives
, header_encoding
);
340 * If ct == NULL, that means that -auto was set and a MIME version
341 * header was already seen. Just use the input file as the output
345 if (! (fp
= fopen(infile
, "r"))) {
346 adios(NULL
, "Unable to open %s for reading", infile
);
348 while (fgets(buffer
, BUFSIZ
, fp
))
349 fputs(buffer
, stdout
);
351 /* output the message */
352 output_message_fp (ct
, stdout
, NULL
);
363 * Process the composition file from a file.
366 /* build the content structures for MIME message */
367 ct
= build_mime (compfile
, autobuild
, directives
, header_encoding
);
370 * If ct == NULL, that means -auto was set and we found a MIME version
371 * header. Simply exit and do nothing.
380 /* output MIME message to this temporary file */
381 strncpy(outfile
, m_mktemp2(compfile
, invo_name
, NULL
, &fp_out
),
385 /* output the message */
386 output_message_fp (ct
, fp_out
, outfile
);
390 * List the message info
393 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
);
395 /* Rename composition draft */
396 snprintf (buffer
, sizeof(buffer
), "%s.orig", m_backup (compfile
));
397 if (rename (compfile
, buffer
) == NOTOK
) {
398 adios (compfile
, "unable to rename comp draft %s to", buffer
);
401 /* Rename output file to take its place */
402 if (rename (outfile
, compfile
) == NOTOK
) {
403 advise (outfile
, "unable to rename output %s to", compfile
);
404 rename (buffer
, compfile
);
416 unlink_done (int status
)
419 * Check if we need to remove stray