]>
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);
92 main (int argc
, char **argv
)
94 int sizesw
= 1, headsw
= 1, directives
= 1;
96 char *cp
, buf
[BUFSIZ
];
97 char buffer
[BUFSIZ
], *compfile
= NULL
;
98 char **argp
, **arguments
;
106 setlocale(LC_ALL
, "");
108 invo_name
= r1bindex (argv
[0], '/');
110 /* read user profile/context */
113 arguments
= getarguments (invo_name
, argc
, argv
, 1);
116 while ((cp
= *argp
++)) {
117 if (cp
[0] == '-' && cp
[1] == '\0') {
119 adios (NULL
, "cannot specify both standard input and a file");
122 listsw
= 0; /* turn off -list if using standard in/out */
123 verbosw
= 0; /* turn off -verbose listings */
127 switch (smatch (++cp
, switches
)) {
129 ambigsw (cp
, switches
);
132 adios (NULL
, "-%s unknown", cp
);
135 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
136 print_help (buf
, switches
, 1);
139 print_version(invo_name
);
143 icachesw
= &rcachesw
;
146 icachesw
= &wcachesw
;
148 if (!(cp
= *argp
++) || *cp
== '-')
149 adios (NULL
, "missing argument to %s", argp
[-2]);
150 switch (*icachesw
= smatch (cp
, caches
)) {
152 ambigsw (cp
, caches
);
155 adios (NULL
, "%s unknown", cp
);
222 adios (NULL
, "only one composition file allowed");
228 * Check if we've specified an additional profile
230 if ((cp
= getenv ("MHBUILD"))) {
231 if ((fp
= fopen (cp
, "r"))) {
232 readconfig ((struct node
**) 0, fp
, cp
, 0);
235 admonish ("", "unable to read $MHBUILD profile (%s)", cp
);
240 * Read the standard profile setup
242 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
243 readconfig ((struct node
**) 0, fp
, cp
, 0);
247 /* Check for public cache location */
248 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
251 /* Check for private cache location */
252 if (!(cache_private
= context_find (nmhprivcache
)))
253 cache_private
= ".cache";
254 cache_private
= getcpy (m_maildir (cache_private
));
257 * Check for storage directory. If defined, we
258 * will store temporary files there. Else we
259 * store them in standard nmh directory.
261 if ((cp
= context_find (nmhstorage
)) && *cp
)
262 tmp
= concat (cp
, "/", invo_name
, NULL
);
264 tmp
= add (m_maildir (invo_name
), NULL
);
266 if (!context_find ("path"))
267 free (path ("./", TFOLDER
));
269 /* Check if we have a file to process */
271 adios (NULL
, "need to specify a %s composition file", invo_name
);
274 * Process the composition file from standard input.
276 if (compfile
[0] == '-' && compfile
[1] == '\0') {
277 /* copy standard input to temporary file */
278 strncpy (infile
, m_mktemp(invo_name
, NULL
, &fp
), sizeof(infile
));
279 while (fgets (buffer
, BUFSIZ
, stdin
))
284 /* build the content structures for MIME message */
285 ct
= build_mime (infile
, directives
);
289 /* output MIME message to this temporary file */
290 strncpy (outfile
, m_mktemp(invo_name
, NULL
, &fp_out
), sizeof(outfile
));
293 /* output the message */
294 output_message_fp (ct
, fp_out
, outfile
);
297 /* output the temp file to standard output */
298 if ((fp
= fopen (outfile
, "r")) == NULL
)
299 adios (outfile
, "unable to open");
300 while (fgets (buffer
, BUFSIZ
, fp
))
301 fputs (buffer
, stdout
);
315 * Process the composition file from a file.
318 /* build the content structures for MIME message */
319 ct
= build_mime (compfile
, directives
);
323 /* output MIME message to this temporary file */
324 strncpy(outfile
, m_mktemp2(compfile
, invo_name
, NULL
, &fp_out
),
328 /* output the message */
329 output_message_fp (ct
, fp_out
, outfile
);
333 * List the message info
336 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
);
338 /* Rename composition draft */
339 snprintf (buffer
, sizeof(buffer
), "%s.orig", m_backup (compfile
));
340 if (rename (compfile
, buffer
) == NOTOK
) {
341 adios (compfile
, "unable to rename comp draft %s to", buffer
);
344 /* Rename output file to take its place */
345 if (rename (outfile
, compfile
) == NOTOK
) {
346 advise (outfile
, "unable to rename output %s to", compfile
);
347 rename (buffer
, compfile
);
359 unlink_done (int status
)
362 * Check if we need to remove stray