]> diplodocus.org Git - nmh/blob - uip/mhbuild.c
Formatting cleanup.
[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 /* mhfree.c */
107 void free_content (CT);
108
109
110 int
111 main (int argc, char **argv)
112 {
113 int sizesw = 1, headsw = 1;
114 int *icachesw;
115 char *cp, buf[BUFSIZ];
116 char buffer[BUFSIZ], *compfile = NULL;
117 char **argp, **arguments;
118 CT ct, cts[2];
119 FILE *fp = NULL;
120 FILE *fp_out = NULL;
121
122 done=unlink_done;
123
124 #ifdef LOCALE
125 setlocale(LC_ALL, "");
126 #endif
127 invo_name = r1bindex (argv[0], '/');
128
129 /* read user profile/context */
130 context_read();
131
132 arguments = getarguments (invo_name, argc, argv, 1);
133 argp = arguments;
134
135 while ((cp = *argp++)) {
136 if (cp[0] == '-' && cp[1] == '\0') {
137 if (compfile)
138 adios (NULL, "cannot specify both standard input and a file");
139 else
140 compfile = cp;
141 listsw = 0; /* turn off -list if using standard in/out */
142 verbosw = 0; /* turn off -verbose listings */
143 break;
144 }
145 if (*cp == '-') {
146 switch (smatch (++cp, switches)) {
147 case AMBIGSW:
148 ambigsw (cp, switches);
149 done (1);
150 case UNKWNSW:
151 adios (NULL, "-%s unknown", cp);
152
153 case HELPSW:
154 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
155 print_help (buf, switches, 1);
156 done (1);
157 case VERSIONSW:
158 print_version(invo_name);
159 done (1);
160
161 case RCACHESW:
162 icachesw = &rcachesw;
163 goto do_cache;
164 case WCACHESW:
165 icachesw = &wcachesw;
166 do_cache: ;
167 if (!(cp = *argp++) || *cp == '-')
168 adios (NULL, "missing argument to %s", argp[-2]);
169 switch (*icachesw = smatch (cp, caches)) {
170 case AMBIGSW:
171 ambigsw (cp, caches);
172 done (1);
173 case UNKWNSW:
174 adios (NULL, "%s unknown", cp);
175 default:
176 break;
177 }
178 continue;
179
180 case CHECKSW:
181 checksw++;
182 continue;
183 case NCHECKSW:
184 checksw = 0;
185 continue;
186
187 case EBCDICSW:
188 ebcdicsw++;
189 continue;
190 case NEBCDICSW:
191 ebcdicsw = 0;
192 continue;
193
194 case HEADSW:
195 headsw++;
196 continue;
197 case NHEADSW:
198 headsw = 0;
199 continue;
200
201 case LISTSW:
202 listsw++;
203 continue;
204 case NLISTSW:
205 listsw = 0;
206 continue;
207
208 case RFC934SW:
209 rfc934sw++;
210 continue;
211 case NRFC934SW:
212 rfc934sw = 0;
213 continue;
214
215 case SIZESW:
216 sizesw++;
217 continue;
218 case NSIZESW:
219 sizesw = 0;
220 continue;
221
222 case CONTENTIDSW:
223 contentidsw = 1;
224 continue;
225 case NCONTENTIDSW:
226 contentidsw = 0;
227 continue;
228
229 case VERBSW:
230 verbosw++;
231 continue;
232 case NVERBSW:
233 verbosw = 0;
234 continue;
235 case DEBUGSW:
236 debugsw = 1;
237 continue;
238 }
239 }
240 if (compfile)
241 adios (NULL, "only one composition file allowed");
242 else
243 compfile = cp;
244 }
245
246 set_endian ();
247
248 if ((cp = getenv ("MM_NOASK")) && !strcmp (cp, "1"))
249 listsw = 0;
250
251 /*
252 * Check if we've specified an additional profile
253 */
254 if ((cp = getenv ("MHBUILD"))) {
255 if ((fp = fopen (cp, "r"))) {
256 readconfig ((struct node **) 0, fp, cp, 0);
257 fclose (fp);
258 } else {
259 admonish ("", "unable to read $MHBUILD profile (%s)", cp);
260 }
261 }
262
263 /*
264 * Read the standard profile setup
265 */
266 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
267 readconfig ((struct node **) 0, fp, cp, 0);
268 fclose (fp);
269 }
270
271 /* Check for public cache location */
272 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
273 cache_public = NULL;
274
275 /* Check for private cache location */
276 if (!(cache_private = context_find (nmhprivcache)))
277 cache_private = ".cache";
278 cache_private = getcpy (m_maildir (cache_private));
279
280 /*
281 * Check for storage directory. If defined, we
282 * will store temporary files there. Else we
283 * store them in standard nmh directory.
284 */
285 if ((cp = context_find (nmhstorage)) && *cp)
286 tmp = concat (cp, "/", invo_name, NULL);
287 else
288 tmp = add (m_maildir (invo_name), NULL);
289
290 if (!context_find ("path"))
291 free (path ("./", TFOLDER));
292
293 /* Check if we have a file to process */
294 if (!compfile)
295 adios (NULL, "need to specify a %s composition file", invo_name);
296
297 /*
298 * Process the composition file from standard input.
299 */
300 if (compfile[0] == '-' && compfile[1] == '\0') {
301 /* copy standard input to temporary file */
302 strncpy (infile, m_mktemp(invo_name, NULL, &fp), sizeof(infile));
303 while (fgets (buffer, BUFSIZ, stdin))
304 fputs (buffer, fp);
305 fclose (fp);
306 unlink_infile = 1;
307
308 /* build the content structures for MIME message */
309 ct = build_mime (infile);
310 cts[0] = ct;
311 cts[1] = NULL;
312
313 /* output MIME message to this temporary file */
314 strncpy (outfile, m_mktemp(invo_name, NULL, &fp_out), sizeof(outfile));
315 unlink_outfile = 1;
316
317 /* output the message */
318 output_message_fp (ct, fp_out, outfile);
319 fclose(fp_out);
320
321 /* output the temp file to standard output */
322 if ((fp = fopen (outfile, "r")) == NULL)
323 adios (outfile, "unable to open");
324 while (fgets (buffer, BUFSIZ, fp))
325 fputs (buffer, stdout);
326 fclose (fp);
327
328 unlink (infile);
329 unlink_infile = 0;
330
331 unlink (outfile);
332 unlink_outfile = 0;
333
334 free_content (ct);
335 done (0);
336 }
337
338 /*
339 * Process the composition file from a file.
340 */
341
342 /* build the content structures for MIME message */
343 ct = build_mime (compfile);
344 cts[0] = ct;
345 cts[1] = NULL;
346
347 /* output MIME message to this temporary file */
348 strncpy(outfile, m_mktemp2(compfile, invo_name, NULL, &fp_out),
349 sizeof(outfile));
350 unlink_outfile = 1;
351
352 /* output the message */
353 output_message_fp (ct, fp_out, outfile);
354 fclose(fp_out);
355
356 /*
357 * List the message info
358 */
359 if (listsw)
360 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
361
362 /* Rename composition draft */
363 snprintf (buffer, sizeof(buffer), "%s.orig", m_backup (compfile));
364 if (rename (compfile, buffer) == NOTOK) {
365 adios (compfile, "unable to rename comp draft %s to", buffer);
366 }
367
368 /* Rename output file to take its place */
369 if (rename (outfile, compfile) == NOTOK) {
370 advise (outfile, "unable to rename output %s to", compfile);
371 rename (buffer, compfile);
372 done (1);
373 }
374 unlink_outfile = 0;
375
376 free_content (ct);
377 done (0);
378 return 1;
379 }
380
381
382 static void
383 unlink_done (int status)
384 {
385 /*
386 * Check if we need to remove stray
387 * temporary files.
388 */
389 if (unlink_infile)
390 unlink (infile);
391 if (unlink_outfile)
392 unlink (outfile);
393
394 exit (status);
395 }