]> diplodocus.org Git - nmh/blob - uip/mhbuild.c
Fixed three LC_ALL settings:
[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("auto", 0, AUTOSW) \
23 X("noauto", 0, NAUTOSW) \
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("headerencoding encoding-algorithm", 0, HEADERENCSW) \
43 X("autoheaderencoding", 0, AUTOHEADERENCSW) \
44 X("version", 0, VERSIONSW) \
45 X("help", 0, HELPSW) \
46 X("debug", -5, DEBUGSW) \
47
48 #define X(sw, minchars, id) id,
49 DEFINE_SWITCH_ENUM(MHBUILD);
50 #undef X
51
52 #define X(sw, minchars, id) { sw, minchars, id },
53 DEFINE_SWITCH_ARRAY(MHBUILD, switches);
54 #undef X
55
56 #define MIMEENCODING_SWITCHES \
57 X("base64", 0, BASE64SW) \
58 X("quoted-printable", 0, QUOTEDPRINTSW) \
59
60 #define X(sw, minchars, id) id,
61 DEFINE_SWITCH_ENUM(MIMEENCODING);
62 #undef X
63
64 #define X(sw, minchars, id) { sw, minchars, id },
65 DEFINE_SWITCH_ARRAY(MIMEENCODING, encodingswitches);
66 #undef X
67
68 /* mhbuildsbr.c */
69 extern char *tmp; /* directory to place temp files */
70
71 /* mhcachesbr.c */
72 extern int rcachesw;
73 extern int wcachesw;
74 extern char *cache_public;
75 extern char *cache_private;
76
77 int debugsw = 0;
78 int verbosw = 0;
79
80 int listsw = 0;
81 int rfc934sw = 0;
82 int contentidsw = 1;
83
84 /*
85 * Temporary files
86 */
87 static char infile[BUFSIZ];
88 static int unlink_infile = 0;
89
90 static char outfile[BUFSIZ];
91 static int unlink_outfile = 0;
92
93 static void unlink_done (int) NORETURN;
94
95 /* mhbuildsbr.c */
96 int output_message (CT, char *);
97 int output_message_fp (CT, FILE *, char*);
98
99 /* mhlistsbr.c */
100 int list_all_messages (CT *, int, int, int, int);
101
102
103 int
104 main (int argc, char **argv)
105 {
106 int sizesw = 1, headsw = 1, directives = 1, autobuild = 0;
107 int *icachesw;
108 char *cp, buf[BUFSIZ];
109 char buffer[BUFSIZ], *compfile = NULL;
110 char **argp, **arguments;
111 CT ct, cts[2];
112 FILE *fp = NULL;
113 FILE *fp_out = NULL;
114 int header_encoding = CE_UNKNOWN;
115
116 done=unlink_done;
117
118 setlocale(LC_ALL, "");
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 AUTOSW:
154 /* -auto implies -nodirectives */
155 autobuild = 1;
156 directives = 0;
157 continue;
158 case NAUTOSW:
159 /*
160 * We're turning directives back on since this is likely here
161 * to override a profile entry
162 */
163 autobuild = 0;
164 directives = 1;
165 continue;
166
167 case RCACHESW:
168 icachesw = &rcachesw;
169 goto do_cache;
170 case WCACHESW:
171 icachesw = &wcachesw;
172 do_cache: ;
173 if (!(cp = *argp++) || *cp == '-')
174 adios (NULL, "missing argument to %s", argp[-2]);
175 switch (*icachesw = smatch (cp, caches)) {
176 case AMBIGSW:
177 ambigsw (cp, caches);
178 done (1);
179 case UNKWNSW:
180 adios (NULL, "%s unknown", cp);
181 default:
182 break;
183 }
184 continue;
185
186 case CHECKSW:
187 checksw++;
188 continue;
189 case NCHECKSW:
190 checksw = 0;
191 continue;
192
193 case HEADSW:
194 headsw++;
195 continue;
196 case NHEADSW:
197 headsw = 0;
198 continue;
199
200 case DIRECTIVES:
201 directives = 1;
202 continue;
203 case NDIRECTIVES:
204 directives = 0;
205 continue;
206
207 case LISTSW:
208 listsw++;
209 continue;
210 case NLISTSW:
211 listsw = 0;
212 continue;
213
214 case RFC934SW:
215 rfc934sw++;
216 continue;
217 case NRFC934SW:
218 rfc934sw = 0;
219 continue;
220
221 case SIZESW:
222 sizesw++;
223 continue;
224 case NSIZESW:
225 sizesw = 0;
226 continue;
227
228 case CONTENTIDSW:
229 contentidsw = 1;
230 continue;
231 case NCONTENTIDSW:
232 contentidsw = 0;
233 continue;
234
235 case HEADERENCSW: {
236 int encoding;
237
238 if (!(cp = *argp++) || *cp == '-')
239 adios (NULL, "missing argument to %s", argp[-2]);
240 switch (encoding = smatch (cp, encodingswitches)) {
241 case AMBIGSW:
242 ambigsw (cp, encodingswitches);
243 done (1);
244 case UNKWNSW:
245 adios (NULL, "%s unknown encoding algorithm", cp);
246 case BASE64SW:
247 header_encoding = CE_BASE64;
248 break;
249 case QUOTEDPRINTSW:
250 header_encoding = CE_QUOTED;
251 break;
252 default:
253 adios (NULL, "Internal error: algorithm %s", cp);
254 }
255 continue;
256 }
257
258 case AUTOHEADERENCSW:
259 header_encoding = CE_UNKNOWN;
260 continue;
261
262 case VERBSW:
263 verbosw++;
264 continue;
265 case NVERBSW:
266 verbosw = 0;
267 continue;
268 case DEBUGSW:
269 debugsw = 1;
270 continue;
271 }
272 }
273 if (compfile)
274 adios (NULL, "only one composition file allowed");
275 else
276 compfile = cp;
277 }
278
279 /*
280 * Check if we've specified an additional profile
281 */
282 if ((cp = getenv ("MHBUILD"))) {
283 if ((fp = fopen (cp, "r"))) {
284 readconfig ((struct node **) 0, fp, cp, 0);
285 fclose (fp);
286 } else {
287 admonish ("", "unable to read $MHBUILD profile (%s)", cp);
288 }
289 }
290
291 /*
292 * Read the standard profile setup
293 */
294 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
295 readconfig ((struct node **) 0, fp, cp, 0);
296 fclose (fp);
297 }
298
299 /* Check for public cache location */
300 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
301 cache_public = NULL;
302
303 /* Check for private cache location */
304 if (!(cache_private = context_find (nmhprivcache)))
305 cache_private = ".cache";
306 cache_private = getcpy (m_maildir (cache_private));
307
308 /*
309 * Check for storage directory. If defined, we
310 * will store temporary files there. Else we
311 * store them in standard nmh directory.
312 */
313 if ((cp = context_find (nmhstorage)) && *cp)
314 tmp = concat (cp, "/", invo_name, NULL);
315 else
316 tmp = add (m_maildir (invo_name), NULL);
317
318 if (!context_find ("path"))
319 free (path ("./", TFOLDER));
320
321 /* Check if we have a file to process */
322 if (!compfile)
323 adios (NULL, "need to specify a %s composition file", invo_name);
324
325 /*
326 * Process the composition file from standard input.
327 */
328 if (compfile[0] == '-' && compfile[1] == '\0') {
329 /* copy standard input to temporary file */
330 strncpy (infile, m_mktemp(invo_name, NULL, &fp), sizeof(infile));
331 while (fgets (buffer, BUFSIZ, stdin))
332 fputs (buffer, fp);
333 fclose (fp);
334 unlink_infile = 1;
335
336 /* build the content structures for MIME message */
337 ct = build_mime (infile, autobuild, directives, header_encoding);
338
339 /*
340 * If ct == NULL, that means that -auto was set and a MIME version
341 * header was already seen. Just use the input file as the output
342 */
343
344 if (!ct) {
345 if (! (fp = fopen(infile, "r"))) {
346 adios(NULL, "Unable to open %s for reading", infile);
347 }
348 while (fgets(buffer, BUFSIZ, fp))
349 fputs(buffer, stdout);
350 } else {
351 /* output the message */
352 output_message_fp (ct, stdout, NULL);
353 free_content (ct);
354 }
355
356 unlink (infile);
357 unlink_infile = 0;
358
359 done (0);
360 }
361
362 /*
363 * Process the composition file from a file.
364 */
365
366 /* build the content structures for MIME message */
367 ct = build_mime (compfile, autobuild, directives, header_encoding);
368
369 /*
370 * If ct == NULL, that means -auto was set and we found a MIME version
371 * header. Simply exit and do nothing.
372 */
373
374 if (! ct)
375 done(0);
376
377 cts[0] = ct;
378 cts[1] = NULL;
379
380 /* output MIME message to this temporary file */
381 strncpy(outfile, m_mktemp2(compfile, invo_name, NULL, &fp_out),
382 sizeof(outfile));
383 unlink_outfile = 1;
384
385 /* output the message */
386 output_message_fp (ct, fp_out, outfile);
387 fclose(fp_out);
388
389 /*
390 * List the message info
391 */
392 if (listsw)
393 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
394
395 /* Rename composition draft */
396 snprintf (buffer, sizeof(buffer), "%s.orig", m_backup (compfile));
397 if (rename (compfile, buffer) == NOTOK) {
398 adios (compfile, "unable to rename comp draft %s to", buffer);
399 }
400
401 /* Rename output file to take its place */
402 if (rename (outfile, compfile) == NOTOK) {
403 advise (outfile, "unable to rename output %s to", compfile);
404 rename (buffer, compfile);
405 done (1);
406 }
407 unlink_outfile = 0;
408
409 free_content (ct);
410 done (0);
411 return 1;
412 }
413
414
415 static void
416 unlink_done (int status)
417 {
418 /*
419 * Check if we need to remove stray
420 * temporary files.
421 */
422 if (unlink_infile)
423 unlink (infile);
424 if (unlink_outfile)
425 unlink (outfile);
426
427 exit (status);
428 }