]>
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("check", 0, CHECKSW) \
23 X("nocheck", 0, NCHECKSW) \
24 X("directives", 0, DIRECTIVES) \
25 X("nodirectives", 0, NDIRECTIVES) \
26 X("headers", 0, HEADSW) \
27 X("noheaders", 0, NHEADSW) \
28 X("list", 0, LISTSW) \
29 X("nolist", 0, NLISTSW) \
30 X("realsize", 0, SIZESW) \
31 X("norealsize", 0, NSIZESW) \
32 X("rfc934mode", 0, RFC934SW) \
33 X("norfc934mode", 0, NRFC934SW) \
34 X("verbose", 0, VERBSW) \
35 X("noverbose", 0, NVERBSW) \
36 X("rcache policy", 0, RCACHESW) \
37 X("wcache policy", 0, WCACHESW) \
38 X("contentid", 0, CONTENTIDSW) \
39 X("nocontentid", 0, NCONTENTIDSW) \
40 X("headerencoding encoding-algorithm", 0, HEADERENCSW) \
41 X("autoheaderencoding", 0, AUTOHEADERENCSW) \
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
);
54 #define MIMEENCODING_SWITCHES \
55 X("base64", 0, BASE64SW) \
56 X("quoted-printable", 0, QUOTEDPRINTSW) \
58 #define X(sw, minchars, id) id,
59 DEFINE_SWITCH_ENUM(MIMEENCODING
);
62 #define X(sw, minchars, id) { sw, minchars, id },
63 DEFINE_SWITCH_ARRAY(MIMEENCODING
, encodingswitches
);
67 extern char *tmp
; /* directory to place temp files */
72 extern char *cache_public
;
73 extern char *cache_private
;
85 static char infile
[BUFSIZ
];
86 static int unlink_infile
= 0;
88 static char outfile
[BUFSIZ
];
89 static int unlink_outfile
= 0;
91 static void unlink_done (int) NORETURN
;
94 int output_message (CT
, char *);
95 int output_message_fp (CT
, FILE *, char*);
98 int list_all_messages (CT
*, int, int, int, int);
102 main (int argc
, char **argv
)
104 int sizesw
= 1, headsw
= 1, directives
= 1;
106 char *cp
, buf
[BUFSIZ
];
107 char buffer
[BUFSIZ
], *compfile
= NULL
;
108 char **argp
, **arguments
;
112 int header_encoding
= CE_UNKNOWN
;
117 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 icachesw
= &rcachesw
;
157 icachesw
= &wcachesw
;
159 if (!(cp
= *argp
++) || *cp
== '-')
160 adios (NULL
, "missing argument to %s", argp
[-2]);
161 switch (*icachesw
= smatch (cp
, caches
)) {
163 ambigsw (cp
, caches
);
166 adios (NULL
, "%s unknown", cp
);
224 if (!(cp
= *argp
++) || *cp
== '-')
225 adios (NULL
, "missing argument to %s", argp
[-2]);
226 switch (encoding
= smatch (cp
, encodingswitches
)) {
228 ambigsw (cp
, encodingswitches
);
231 adios (NULL
, "%s unknown encoding algorithm", cp
);
233 header_encoding
= CE_BASE64
;
236 header_encoding
= CE_QUOTED
;
239 adios (NULL
, "Internal error: algorithm %s", cp
);
244 case AUTOHEADERENCSW
:
245 header_encoding
= CE_UNKNOWN
;
260 adios (NULL
, "only one composition file allowed");
266 * Check if we've specified an additional profile
268 if ((cp
= getenv ("MHBUILD"))) {
269 if ((fp
= fopen (cp
, "r"))) {
270 readconfig ((struct node
**) 0, fp
, cp
, 0);
273 admonish ("", "unable to read $MHBUILD profile (%s)", cp
);
278 * Read the standard profile setup
280 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
281 readconfig ((struct node
**) 0, fp
, cp
, 0);
285 /* Check for public cache location */
286 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
289 /* Check for private cache location */
290 if (!(cache_private
= context_find (nmhprivcache
)))
291 cache_private
= ".cache";
292 cache_private
= getcpy (m_maildir (cache_private
));
295 * Check for storage directory. If defined, we
296 * will store temporary files there. Else we
297 * store them in standard nmh directory.
299 if ((cp
= context_find (nmhstorage
)) && *cp
)
300 tmp
= concat (cp
, "/", invo_name
, NULL
);
302 tmp
= add (m_maildir (invo_name
), NULL
);
304 if (!context_find ("path"))
305 free (path ("./", TFOLDER
));
307 /* Check if we have a file to process */
309 adios (NULL
, "need to specify a %s composition file", invo_name
);
312 * Process the composition file from standard input.
314 if (compfile
[0] == '-' && compfile
[1] == '\0') {
315 /* copy standard input to temporary file */
316 strncpy (infile
, m_mktemp(invo_name
, NULL
, &fp
), sizeof(infile
));
317 while (fgets (buffer
, BUFSIZ
, stdin
))
322 /* build the content structures for MIME message */
323 ct
= build_mime (infile
, directives
, header_encoding
);
327 /* output MIME message to this temporary file */
328 strncpy (outfile
, m_mktemp(invo_name
, NULL
, &fp_out
), sizeof(outfile
));
331 /* output the message */
332 output_message_fp (ct
, fp_out
, outfile
);
335 /* output the temp file to standard output */
336 if ((fp
= fopen (outfile
, "r")) == NULL
)
337 adios (outfile
, "unable to open");
338 while (fgets (buffer
, BUFSIZ
, fp
))
339 fputs (buffer
, stdout
);
353 * Process the composition file from a file.
356 /* build the content structures for MIME message */
357 ct
= build_mime (compfile
, directives
, header_encoding
);
361 /* output MIME message to this temporary file */
362 strncpy(outfile
, m_mktemp2(compfile
, invo_name
, NULL
, &fp_out
),
366 /* output the message */
367 output_message_fp (ct
, fp_out
, outfile
);
371 * List the message info
374 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
);
376 /* Rename composition draft */
377 snprintf (buffer
, sizeof(buffer
), "%s.orig", m_backup (compfile
));
378 if (rename (compfile
, buffer
) == NOTOK
) {
379 adios (compfile
, "unable to rename comp draft %s to", buffer
);
382 /* Rename output file to take its place */
383 if (rename (outfile
, compfile
) == NOTOK
) {
384 advise (outfile
, "unable to rename output %s to", compfile
);
385 rename (buffer
, compfile
);
397 unlink_done (int status
)
400 * Check if we need to remove stray