]>
diplodocus.org Git - nmh/blob - uip/mhbuild.c
17833f599f8998d422b08111f8d07bdaf48a961d
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>
18 #include <h/mhparse.h>
19 #include <h/mhcachesbr.h>
22 #define MHBUILD_SWITCHES \
23 X("check", 0, CHECKSW) \
24 X("nocheck", 0, NCHECKSW) \
25 X("directives", 0, DIRECTIVES) \
26 X("nodirectives", 0, NDIRECTIVES) \
27 X("headers", 0, HEADSW) \
28 X("noheaders", 0, NHEADSW) \
29 X("list", 0, LISTSW) \
30 X("nolist", 0, NLISTSW) \
31 X("realsize", 0, SIZESW) \
32 X("norealsize", 0, NSIZESW) \
33 X("rfc934mode", 0, RFC934SW) \
34 X("norfc934mode", 0, NRFC934SW) \
35 X("verbose", 0, VERBSW) \
36 X("noverbose", 0, NVERBSW) \
37 X("rcache policy", 0, RCACHESW) \
38 X("wcache policy", 0, WCACHESW) \
39 X("contentid", 0, CONTENTIDSW) \
40 X("nocontentid", 0, NCONTENTIDSW) \
41 X("version", 0, VERSIONSW) \
42 X("help", 0, HELPSW) \
43 X("debug", -5, DEBUGSW) \
45 #define X(sw, minchars, id) id,
46 DEFINE_SWITCH_ENUM(MHBUILD
);
49 #define X(sw, minchars, id) { sw, minchars, id },
50 DEFINE_SWITCH_ARRAY(MHBUILD
, switches
);
55 extern char *tmp
; /* directory to place temp files */
60 extern char *cache_public
;
61 extern char *cache_private
;
73 static char infile
[BUFSIZ
];
74 static int unlink_infile
= 0;
76 static char outfile
[BUFSIZ
];
77 static int unlink_outfile
= 0;
79 static void unlink_done (int) NORETURN
;
82 CT
build_mime (char *, int);
83 int output_message (CT
, char *);
84 int output_message_fp (CT
, FILE *, char*);
87 int list_all_messages (CT
*, int, int, int, int);
91 main (int argc
, char **argv
)
93 int sizesw
= 1, headsw
= 1, directives
= 1;
95 char *cp
, buf
[BUFSIZ
];
96 char buffer
[BUFSIZ
], *compfile
= NULL
;
97 char **argp
, **arguments
;
105 setlocale(LC_ALL
, "");
107 invo_name
= r1bindex (argv
[0], '/');
109 /* read user profile/context */
112 arguments
= getarguments (invo_name
, argc
, argv
, 1);
115 while ((cp
= *argp
++)) {
116 if (cp
[0] == '-' && cp
[1] == '\0') {
118 adios (NULL
, "cannot specify both standard input and a file");
121 listsw
= 0; /* turn off -list if using standard in/out */
122 verbosw
= 0; /* turn off -verbose listings */
126 switch (smatch (++cp
, switches
)) {
128 ambigsw (cp
, switches
);
131 adios (NULL
, "-%s unknown", cp
);
134 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
135 print_help (buf
, switches
, 1);
138 print_version(invo_name
);
142 icachesw
= &rcachesw
;
145 icachesw
= &wcachesw
;
147 if (!(cp
= *argp
++) || *cp
== '-')
148 adios (NULL
, "missing argument to %s", argp
[-2]);
149 switch (*icachesw
= smatch (cp
, caches
)) {
151 ambigsw (cp
, caches
);
154 adios (NULL
, "%s unknown", cp
);
221 adios (NULL
, "only one composition file allowed");
227 * Check if we've specified an additional profile
229 if ((cp
= getenv ("MHBUILD"))) {
230 if ((fp
= fopen (cp
, "r"))) {
231 readconfig ((struct node
**) 0, fp
, cp
, 0);
234 admonish ("", "unable to read $MHBUILD profile (%s)", cp
);
239 * Read the standard profile setup
241 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
242 readconfig ((struct node
**) 0, fp
, cp
, 0);
246 /* Check for public cache location */
247 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
250 /* Check for private cache location */
251 if (!(cache_private
= context_find (nmhprivcache
)))
252 cache_private
= ".cache";
253 cache_private
= getcpy (m_maildir (cache_private
));
256 * Check for storage directory. If defined, we
257 * will store temporary files there. Else we
258 * store them in standard nmh directory.
260 if ((cp
= context_find (nmhstorage
)) && *cp
)
261 tmp
= concat (cp
, "/", invo_name
, NULL
);
263 tmp
= add (m_maildir (invo_name
), NULL
);
265 if (!context_find ("path"))
266 free (path ("./", TFOLDER
));
268 /* Check if we have a file to process */
270 adios (NULL
, "need to specify a %s composition file", invo_name
);
273 * Process the composition file from standard input.
275 if (compfile
[0] == '-' && compfile
[1] == '\0') {
276 /* copy standard input to temporary file */
277 strncpy (infile
, m_mktemp(invo_name
, NULL
, &fp
), sizeof(infile
));
278 while (fgets (buffer
, BUFSIZ
, stdin
))
283 /* build the content structures for MIME message */
284 ct
= build_mime (infile
, directives
);
288 /* output MIME message to this temporary file */
289 strncpy (outfile
, m_mktemp(invo_name
, NULL
, &fp_out
), sizeof(outfile
));
292 /* output the message */
293 output_message_fp (ct
, fp_out
, outfile
);
296 /* output the temp file to standard output */
297 if ((fp
= fopen (outfile
, "r")) == NULL
)
298 adios (outfile
, "unable to open");
299 while (fgets (buffer
, BUFSIZ
, fp
))
300 fputs (buffer
, stdout
);
314 * Process the composition file from a file.
317 /* build the content structures for MIME message */
318 ct
= build_mime (compfile
, directives
);
322 /* output MIME message to this temporary file */
323 strncpy(outfile
, m_mktemp2(compfile
, invo_name
, NULL
, &fp_out
),
327 /* output the message */
328 output_message_fp (ct
, fp_out
, outfile
);
332 * List the message info
335 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
);
337 /* Rename composition draft */
338 snprintf (buffer
, sizeof(buffer
), "%s.orig", m_backup (compfile
));
339 if (rename (compfile
, buffer
) == NOTOK
) {
340 adios (compfile
, "unable to rename comp draft %s to", buffer
);
343 /* Rename output file to take its place */
344 if (rename (outfile
, compfile
) == NOTOK
) {
345 advise (outfile
, "unable to rename output %s to", compfile
);
346 rename (buffer
, compfile
);
358 unlink_done (int status
)
361 * Check if we need to remove stray