]> diplodocus.org Git - nmh/blob - uip/mhbuild.c
It looks like the first test fails on Solaris because it
[nmh] / uip / mhbuild.c
1
2 /*
3 * mhbuild.c -- expand/translate MIME composition files
4 *
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.
8 */
9
10 #include <h/mh.h>
11 #include <fcntl.h>
12 #include <h/signals.h>
13 #include <h/md5.h>
14 #include <errno.h>
15 #include <signal.h>
16 #include <h/mts.h>
17 #include <h/tws.h>
18 #include <h/mime.h>
19 #include <h/mhparse.h>
20 #include <h/mhcachesbr.h>
21 #include <h/utils.h>
22
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) \
45
46 #define X(sw, minchars, id) id,
47 DEFINE_SWITCH_ENUM(MHBUILD);
48 #undef X
49
50 #define X(sw, minchars, id) { sw, minchars, id },
51 DEFINE_SWITCH_ARRAY(MHBUILD, switches);
52 #undef X
53
54
55 /* mhbuildsbr.c */
56 extern char *tmp; /* directory to place temp files */
57
58 /* mhcachesbr.c */
59 extern int rcachesw;
60 extern int wcachesw;
61 extern char *cache_public;
62 extern char *cache_private;
63
64 int debugsw = 0;
65 int verbosw = 0;
66
67 int listsw = 0;
68 int rfc934sw = 0;
69 int contentidsw = 1;
70
71 /*
72 * Temporary files
73 */
74 static char infile[BUFSIZ];
75 static int unlink_infile = 0;
76
77 static char outfile[BUFSIZ];
78 static int unlink_outfile = 0;
79
80 static void unlink_done (int) NORETURN;
81
82 /* mhbuildsbr.c */
83 CT build_mime (char *, int);
84 int output_message (CT, char *);
85 int output_message_fp (CT, FILE *, char*);
86
87 /* mhlistsbr.c */
88 int list_all_messages (CT *, int, int, int, int);
89
90 /* mhfree.c */
91 void free_content (CT);
92
93
94 int
95 main (int argc, char **argv)
96 {
97 int sizesw = 1, headsw = 1, directives = 1;
98 int *icachesw;
99 char *cp, buf[BUFSIZ];
100 char buffer[BUFSIZ], *compfile = NULL;
101 char **argp, **arguments;
102 CT ct, cts[2];
103 FILE *fp = NULL;
104 FILE *fp_out = NULL;
105
106 done=unlink_done;
107
108 #ifdef LOCALE
109 setlocale(LC_ALL, "");
110 #endif
111 invo_name = r1bindex (argv[0], '/');
112
113 /* read user profile/context */
114 context_read();
115
116 arguments = getarguments (invo_name, argc, argv, 1);
117 argp = arguments;
118
119 while ((cp = *argp++)) {
120 if (cp[0] == '-' && cp[1] == '\0') {
121 if (compfile)
122 adios (NULL, "cannot specify both standard input and a file");
123 else
124 compfile = cp;
125 listsw = 0; /* turn off -list if using standard in/out */
126 verbosw = 0; /* turn off -verbose listings */
127 break;
128 }
129 if (*cp == '-') {
130 switch (smatch (++cp, switches)) {
131 case AMBIGSW:
132 ambigsw (cp, switches);
133 done (1);
134 case UNKWNSW:
135 adios (NULL, "-%s unknown", cp);
136
137 case HELPSW:
138 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
139 print_help (buf, switches, 1);
140 done (0);
141 case VERSIONSW:
142 print_version(invo_name);
143 done (0);
144
145 case RCACHESW:
146 icachesw = &rcachesw;
147 goto do_cache;
148 case WCACHESW:
149 icachesw = &wcachesw;
150 do_cache: ;
151 if (!(cp = *argp++) || *cp == '-')
152 adios (NULL, "missing argument to %s", argp[-2]);
153 switch (*icachesw = smatch (cp, caches)) {
154 case AMBIGSW:
155 ambigsw (cp, caches);
156 done (1);
157 case UNKWNSW:
158 adios (NULL, "%s unknown", cp);
159 default:
160 break;
161 }
162 continue;
163
164 case CHECKSW:
165 checksw++;
166 continue;
167 case NCHECKSW:
168 checksw = 0;
169 continue;
170
171 case HEADSW:
172 headsw++;
173 continue;
174 case NHEADSW:
175 headsw = 0;
176 continue;
177
178 case DIRECTIVES:
179 directives = 1;
180 continue;
181 case NDIRECTIVES:
182 directives = 0;
183 continue;
184
185 case LISTSW:
186 listsw++;
187 continue;
188 case NLISTSW:
189 listsw = 0;
190 continue;
191
192 case RFC934SW:
193 rfc934sw++;
194 continue;
195 case NRFC934SW:
196 rfc934sw = 0;
197 continue;
198
199 case SIZESW:
200 sizesw++;
201 continue;
202 case NSIZESW:
203 sizesw = 0;
204 continue;
205
206 case CONTENTIDSW:
207 contentidsw = 1;
208 continue;
209 case NCONTENTIDSW:
210 contentidsw = 0;
211 continue;
212
213 case VERBSW:
214 verbosw++;
215 continue;
216 case NVERBSW:
217 verbosw = 0;
218 continue;
219 case DEBUGSW:
220 debugsw = 1;
221 continue;
222 }
223 }
224 if (compfile)
225 adios (NULL, "only one composition file allowed");
226 else
227 compfile = cp;
228 }
229
230 /*
231 * Check if we've specified an additional profile
232 */
233 if ((cp = getenv ("MHBUILD"))) {
234 if ((fp = fopen (cp, "r"))) {
235 readconfig ((struct node **) 0, fp, cp, 0);
236 fclose (fp);
237 } else {
238 admonish ("", "unable to read $MHBUILD profile (%s)", cp);
239 }
240 }
241
242 /*
243 * Read the standard profile setup
244 */
245 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
246 readconfig ((struct node **) 0, fp, cp, 0);
247 fclose (fp);
248 }
249
250 /* Check for public cache location */
251 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
252 cache_public = NULL;
253
254 /* Check for private cache location */
255 if (!(cache_private = context_find (nmhprivcache)))
256 cache_private = ".cache";
257 cache_private = getcpy (m_maildir (cache_private));
258
259 /*
260 * Check for storage directory. If defined, we
261 * will store temporary files there. Else we
262 * store them in standard nmh directory.
263 */
264 if ((cp = context_find (nmhstorage)) && *cp)
265 tmp = concat (cp, "/", invo_name, NULL);
266 else
267 tmp = add (m_maildir (invo_name), NULL);
268
269 if (!context_find ("path"))
270 free (path ("./", TFOLDER));
271
272 /* Check if we have a file to process */
273 if (!compfile)
274 adios (NULL, "need to specify a %s composition file", invo_name);
275
276 /*
277 * Process the composition file from standard input.
278 */
279 if (compfile[0] == '-' && compfile[1] == '\0') {
280 /* copy standard input to temporary file */
281 strncpy (infile, m_mktemp(invo_name, NULL, &fp), sizeof(infile));
282 while (fgets (buffer, BUFSIZ, stdin))
283 fputs (buffer, fp);
284 fclose (fp);
285 unlink_infile = 1;
286
287 /* build the content structures for MIME message */
288 ct = build_mime (infile, directives);
289 cts[0] = ct;
290 cts[1] = NULL;
291
292 /* output MIME message to this temporary file */
293 strncpy (outfile, m_mktemp(invo_name, NULL, &fp_out), sizeof(outfile));
294 unlink_outfile = 1;
295
296 /* output the message */
297 output_message_fp (ct, fp_out, outfile);
298 fclose(fp_out);
299
300 /* output the temp file to standard output */
301 if ((fp = fopen (outfile, "r")) == NULL)
302 adios (outfile, "unable to open");
303 while (fgets (buffer, BUFSIZ, fp))
304 fputs (buffer, stdout);
305 fclose (fp);
306
307 unlink (infile);
308 unlink_infile = 0;
309
310 unlink (outfile);
311 unlink_outfile = 0;
312
313 free_content (ct);
314 done (0);
315 }
316
317 /*
318 * Process the composition file from a file.
319 */
320
321 /* build the content structures for MIME message */
322 ct = build_mime (compfile, directives);
323 cts[0] = ct;
324 cts[1] = NULL;
325
326 /* output MIME message to this temporary file */
327 strncpy(outfile, m_mktemp2(compfile, invo_name, NULL, &fp_out),
328 sizeof(outfile));
329 unlink_outfile = 1;
330
331 /* output the message */
332 output_message_fp (ct, fp_out, outfile);
333 fclose(fp_out);
334
335 /*
336 * List the message info
337 */
338 if (listsw)
339 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
340
341 /* Rename composition draft */
342 snprintf (buffer, sizeof(buffer), "%s.orig", m_backup (compfile));
343 if (rename (compfile, buffer) == NOTOK) {
344 adios (compfile, "unable to rename comp draft %s to", buffer);
345 }
346
347 /* Rename output file to take its place */
348 if (rename (outfile, compfile) == NOTOK) {
349 advise (outfile, "unable to rename output %s to", compfile);
350 rename (buffer, compfile);
351 done (1);
352 }
353 unlink_outfile = 0;
354
355 free_content (ct);
356 done (0);
357 return 1;
358 }
359
360
361 static void
362 unlink_done (int status)
363 {
364 /*
365 * Check if we need to remove stray
366 * temporary files.
367 */
368 if (unlink_infile)
369 unlink (infile);
370 if (unlink_outfile)
371 unlink (outfile);
372
373 exit (status);
374 }