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