]>
diplodocus.org Git - nmh/blob - uip/mhbuild.c
85b5a21b369a012d0bc151dd499e268b3a792a9d
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) \
47 X("dist", -4, DISTSW) \
49 #define X(sw, minchars, id) id,
50 DEFINE_SWITCH_ENUM(MHBUILD
);
53 #define X(sw, minchars, id) { sw, minchars, id },
54 DEFINE_SWITCH_ARRAY(MHBUILD
, switches
);
57 #define MIMEENCODING_SWITCHES \
58 X("base64", 0, BASE64SW) \
59 X("quoted-printable", 0, QUOTEDPRINTSW) \
61 #define X(sw, minchars, id) id,
62 DEFINE_SWITCH_ENUM(MIMEENCODING
);
65 #define X(sw, minchars, id) { sw, minchars, id },
66 DEFINE_SWITCH_ARRAY(MIMEENCODING
, encodingswitches
);
70 extern char *tmp
; /* directory to place temp files */
75 extern char *cache_public
;
76 extern char *cache_private
;
88 static char infile
[BUFSIZ
];
89 static int unlink_infile
= 0;
91 static char outfile
[BUFSIZ
];
92 static int unlink_outfile
= 0;
94 static void unlink_done (int) NORETURN
;
97 int output_message (CT
, char *);
98 int output_message_fp (CT
, FILE *, char*);
101 int list_all_messages (CT
*, int, int, int, int);
105 main (int argc
, char **argv
)
107 int sizesw
= 1, headsw
= 1, directives
= 1, autobuild
= 0, dist
= 0;
109 char *cp
, buf
[BUFSIZ
];
110 char buffer
[BUFSIZ
], *compfile
= NULL
;
111 char **argp
, **arguments
;
115 int header_encoding
= CE_UNKNOWN
;
119 setlocale(LC_ALL
, "");
120 invo_name
= r1bindex (argv
[0], '/');
122 /* read user profile/context */
125 arguments
= getarguments (invo_name
, argc
, argv
, 1);
128 while ((cp
= *argp
++)) {
129 if (cp
[0] == '-' && cp
[1] == '\0') {
131 adios (NULL
, "cannot specify both standard input and a file");
134 listsw
= 0; /* turn off -list if using standard in/out */
135 verbosw
= 0; /* turn off -verbose listings */
139 switch (smatch (++cp
, switches
)) {
141 ambigsw (cp
, switches
);
144 adios (NULL
, "-%s unknown", cp
);
147 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
148 print_help (buf
, switches
, 1);
151 print_version(invo_name
);
155 /* -auto implies -nodirectives */
161 * We're turning directives back on since this is likely here
162 * to override a profile entry
169 icachesw
= &rcachesw
;
172 icachesw
= &wcachesw
;
174 if (!(cp
= *argp
++) || *cp
== '-')
175 adios (NULL
, "missing argument to %s", argp
[-2]);
176 switch (*icachesw
= smatch (cp
, caches
)) {
178 ambigsw (cp
, caches
);
181 adios (NULL
, "%s unknown", cp
);
239 if (!(cp
= *argp
++) || *cp
== '-')
240 adios (NULL
, "missing argument to %s", argp
[-2]);
241 switch (encoding
= smatch (cp
, encodingswitches
)) {
243 ambigsw (cp
, encodingswitches
);
246 adios (NULL
, "%s unknown encoding algorithm", cp
);
248 header_encoding
= CE_BASE64
;
251 header_encoding
= CE_QUOTED
;
254 adios (NULL
, "Internal error: algorithm %s", cp
);
259 case AUTOHEADERENCSW
:
260 header_encoding
= CE_UNKNOWN
;
278 adios (NULL
, "only one composition file allowed");
284 * Check if we've specified an additional profile
286 if ((cp
= getenv ("MHBUILD"))) {
287 if ((fp
= fopen (cp
, "r"))) {
288 readconfig ((struct node
**) 0, fp
, cp
, 0);
291 admonish ("", "unable to read $MHBUILD profile (%s)", cp
);
296 * Read the standard profile setup
298 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
299 readconfig ((struct node
**) 0, fp
, cp
, 0);
303 /* Check for public cache location */
304 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
307 /* Check for private cache location */
308 if (!(cache_private
= context_find (nmhprivcache
)))
309 cache_private
= ".cache";
310 cache_private
= getcpy (m_maildir (cache_private
));
313 * Check for storage directory. If defined, we
314 * will store temporary files there. Else we
315 * store them in standard nmh directory.
317 if ((cp
= context_find (nmhstorage
)) && *cp
)
318 tmp
= concat (cp
, "/", invo_name
, NULL
);
320 tmp
= add (m_maildir (invo_name
), NULL
);
322 if (!context_find ("path"))
323 free (path ("./", TFOLDER
));
325 /* Check if we have a file to process */
327 adios (NULL
, "need to specify a %s composition file", invo_name
);
330 * Process the composition file from standard input.
332 if (compfile
[0] == '-' && compfile
[1] == '\0') {
333 /* copy standard input to temporary file */
334 strncpy (infile
, m_mktemp(invo_name
, NULL
, &fp
), sizeof(infile
));
335 while (fgets (buffer
, BUFSIZ
, stdin
))
340 /* build the content structures for MIME message */
341 ct
= build_mime (infile
, autobuild
, dist
, directives
, header_encoding
);
344 * If ct == NULL, that means that -auto was set and a MIME version
345 * header was already seen. Just use the input file as the output
349 if (! (fp
= fopen(infile
, "r"))) {
350 adios(NULL
, "Unable to open %s for reading", infile
);
352 while (fgets(buffer
, BUFSIZ
, fp
))
353 fputs(buffer
, stdout
);
355 /* output the message */
356 output_message_fp (ct
, stdout
, NULL
);
367 * Process the composition file from a file.
370 /* build the content structures for MIME message */
371 ct
= build_mime (compfile
, autobuild
, dist
, directives
, header_encoding
);
374 * If ct == NULL, that means -auto was set and we found a MIME version
375 * header. Simply exit and do nothing.
384 /* output MIME message to this temporary file */
385 strncpy(outfile
, m_mktemp2(compfile
, invo_name
, NULL
, &fp_out
),
389 /* output the message */
390 output_message_fp (ct
, fp_out
, outfile
);
394 * List the message info
397 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
);
399 /* Rename composition draft */
400 snprintf (buffer
, sizeof(buffer
), "%s.orig", m_backup (compfile
));
401 if (rename (compfile
, buffer
) == NOTOK
) {
402 adios (compfile
, "unable to rename comp draft %s to", buffer
);
405 /* Rename output file to take its place */
406 if (rename (outfile
, compfile
) == NOTOK
) {
407 advise (outfile
, "unable to rename output %s to", compfile
);
408 rename (buffer
, compfile
);
420 unlink_done (int status
)
423 * Check if we need to remove stray