]>
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("version", 0, VERSIONSW) \
41 X("help", 0, HELPSW) \
42 X("debug", -5, DEBUGSW) \
44 #define X(sw, minchars, id) id,
45 DEFINE_SWITCH_ENUM(MHBUILD
);
48 #define X(sw, minchars, id) { sw, minchars, id },
49 DEFINE_SWITCH_ARRAY(MHBUILD
, switches
);
54 extern char *tmp
; /* directory to place temp files */
59 extern char *cache_public
;
60 extern char *cache_private
;
72 static char infile
[BUFSIZ
];
73 static int unlink_infile
= 0;
75 static char outfile
[BUFSIZ
];
76 static int unlink_outfile
= 0;
78 static void unlink_done (int) NORETURN
;
81 CT
build_mime (char *, int);
82 int output_message (CT
, char *);
83 int output_message_fp (CT
, FILE *, char*);
86 int list_all_messages (CT
*, int, int, int, int);
90 main (int argc
, char **argv
)
92 int sizesw
= 1, headsw
= 1, directives
= 1;
94 char *cp
, buf
[BUFSIZ
];
95 char buffer
[BUFSIZ
], *compfile
= NULL
;
96 char **argp
, **arguments
;
104 setlocale(LC_ALL
, "");
106 invo_name
= r1bindex (argv
[0], '/');
108 /* read user profile/context */
111 arguments
= getarguments (invo_name
, argc
, argv
, 1);
114 while ((cp
= *argp
++)) {
115 if (cp
[0] == '-' && cp
[1] == '\0') {
117 adios (NULL
, "cannot specify both standard input and a file");
120 listsw
= 0; /* turn off -list if using standard in/out */
121 verbosw
= 0; /* turn off -verbose listings */
125 switch (smatch (++cp
, switches
)) {
127 ambigsw (cp
, switches
);
130 adios (NULL
, "-%s unknown", cp
);
133 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
134 print_help (buf
, switches
, 1);
137 print_version(invo_name
);
141 icachesw
= &rcachesw
;
144 icachesw
= &wcachesw
;
146 if (!(cp
= *argp
++) || *cp
== '-')
147 adios (NULL
, "missing argument to %s", argp
[-2]);
148 switch (*icachesw
= smatch (cp
, caches
)) {
150 ambigsw (cp
, caches
);
153 adios (NULL
, "%s unknown", cp
);
220 adios (NULL
, "only one composition file allowed");
226 * Check if we've specified an additional profile
228 if ((cp
= getenv ("MHBUILD"))) {
229 if ((fp
= fopen (cp
, "r"))) {
230 readconfig ((struct node
**) 0, fp
, cp
, 0);
233 admonish ("", "unable to read $MHBUILD profile (%s)", cp
);
238 * Read the standard profile setup
240 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
241 readconfig ((struct node
**) 0, fp
, cp
, 0);
245 /* Check for public cache location */
246 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
249 /* Check for private cache location */
250 if (!(cache_private
= context_find (nmhprivcache
)))
251 cache_private
= ".cache";
252 cache_private
= getcpy (m_maildir (cache_private
));
255 * Check for storage directory. If defined, we
256 * will store temporary files there. Else we
257 * store them in standard nmh directory.
259 if ((cp
= context_find (nmhstorage
)) && *cp
)
260 tmp
= concat (cp
, "/", invo_name
, NULL
);
262 tmp
= add (m_maildir (invo_name
), NULL
);
264 if (!context_find ("path"))
265 free (path ("./", TFOLDER
));
267 /* Check if we have a file to process */
269 adios (NULL
, "need to specify a %s composition file", invo_name
);
272 * Process the composition file from standard input.
274 if (compfile
[0] == '-' && compfile
[1] == '\0') {
275 /* copy standard input to temporary file */
276 strncpy (infile
, m_mktemp(invo_name
, NULL
, &fp
), sizeof(infile
));
277 while (fgets (buffer
, BUFSIZ
, stdin
))
282 /* build the content structures for MIME message */
283 ct
= build_mime (infile
, directives
);
287 /* output MIME message to this temporary file */
288 strncpy (outfile
, m_mktemp(invo_name
, NULL
, &fp_out
), sizeof(outfile
));
291 /* output the message */
292 output_message_fp (ct
, fp_out
, outfile
);
295 /* output the temp file to standard output */
296 if ((fp
= fopen (outfile
, "r")) == NULL
)
297 adios (outfile
, "unable to open");
298 while (fgets (buffer
, BUFSIZ
, fp
))
299 fputs (buffer
, stdout
);
313 * Process the composition file from a file.
316 /* build the content structures for MIME message */
317 ct
= build_mime (compfile
, directives
);
321 /* output MIME message to this temporary file */
322 strncpy(outfile
, m_mktemp2(compfile
, invo_name
, NULL
, &fp_out
),
326 /* output the message */
327 output_message_fp (ct
, fp_out
, outfile
);
331 * List the message info
334 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
);
336 /* Rename composition draft */
337 snprintf (buffer
, sizeof(buffer
), "%s.orig", m_backup (compfile
));
338 if (rename (compfile
, buffer
) == NOTOK
) {
339 adios (compfile
, "unable to rename comp draft %s to", buffer
);
342 /* Rename output file to take its place */
343 if (rename (outfile
, compfile
) == NOTOK
) {
344 advise (outfile
, "unable to rename output %s to", compfile
);
345 rename (buffer
, compfile
);
357 unlink_done (int status
)
360 * Check if we need to remove stray