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