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