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