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