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