* patch #3968: Move the add() function from its own file (add.c) and
[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 VERSIONSW              16
63     { "version", 0 },
64 #define HELPSW                 17
65     { "help", 0 },
66 #define DEBUGSW                18
67     { "debug", -5 },
68     { NULL, 0 }
69 };
70
71
72 /* mhbuildsbr.c */
73 extern int checksw;
74 extern char *tmp;       /* directory to place temp files */
75
76 /* mhcachesbr.c */
77 extern int rcachesw;
78 extern int wcachesw;
79 extern char *cache_public;
80 extern char *cache_private;
81
82 int debugsw = 0;
83 int verbosw = 0;
84
85 int ebcdicsw = 0;
86 int listsw   = 0;
87 int rfc934sw = 0;
88
89 /*
90  * Temporary files
91  */
92 static char infile[BUFSIZ];
93 static int unlink_infile  = 0;
94
95 static char outfile[BUFSIZ];
96 static int unlink_outfile = 0;
97
98
99 /* mhbuildsbr.c */
100 CT build_mime (char *);
101 int output_message (CT, char *);
102
103 /* mhlistsbr.c */
104 int list_all_messages (CT *, int, int, int, int);
105
106 /* mhmisc.c */
107 void set_endian (void);
108
109 /* mhfree.c */
110 void free_content (CT);
111
112
113 int
114 main (int argc, char **argv)
115 {
116     int sizesw = 1, headsw = 1;
117     int *icachesw;
118     char *cp, buf[BUFSIZ];
119     char buffer[BUFSIZ], *compfile = NULL;
120     char **argp, **arguments;
121     CT ct, cts[2];
122     FILE *fp;
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 VERBSW: 
223                 verbosw++;
224                 continue;
225             case NVERBSW: 
226                 verbosw = 0;
227                 continue;
228             case DEBUGSW:
229                 debugsw = 1;
230                 continue;
231             }
232         }
233         if (compfile)
234             adios (NULL, "only one composition file allowed");
235         else
236             compfile = cp;
237     }
238
239     set_endian ();
240
241     if ((cp = getenv ("MM_NOASK")) && !strcmp (cp, "1"))
242         listsw  = 0;
243
244     /*
245      * Check if we've specified an additional profile
246      */
247     if ((cp = getenv ("MHBUILD"))) {
248         if ((fp = fopen (cp, "r"))) {
249             readconfig ((struct node **) 0, fp, cp, 0);
250             fclose (fp);
251         } else {
252             admonish ("", "unable to read $MHBUILD profile (%s)", cp);
253         }
254     }
255
256     /*
257      * Read the standard profile setup
258      */
259     if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
260         readconfig ((struct node **) 0, fp, cp, 0);
261         fclose (fp);
262     }
263
264     /* Check for public cache location */
265     if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
266         cache_public = NULL;
267
268     /* Check for private cache location */
269     if (!(cache_private = context_find (nmhprivcache)))
270         cache_private = ".cache";
271     cache_private = getcpy (m_maildir (cache_private));
272
273     /*
274      * Check for storage directory.  If defined, we
275      * will store temporary files there.  Else we
276      * store them in standard nmh directory.
277      */
278     if ((cp = context_find (nmhstorage)) && *cp)
279         tmp = concat (cp, "/", invo_name, NULL);
280     else
281         tmp = add (m_maildir (invo_name), NULL);
282
283     if (!context_find ("path"))
284         free (path ("./", TFOLDER));
285
286     /* Check if we have a file to process */
287     if (!compfile)
288         adios (NULL, "need to specify a %s composition file", invo_name);
289
290     /*
291      * Process the composition file from standard input.
292      */
293     if (compfile[0] == '-' && compfile[1] == '\0') {
294
295         /* copy standard input to temporary file */
296         strncpy (infile, m_scratch ("", invo_name), sizeof(infile));
297         if ((fp = fopen (infile, "w")) == NULL)
298             adios (infile, "unable to open");
299         while (fgets (buffer, BUFSIZ, stdin))
300             fputs (buffer, fp);
301         fclose (fp);
302         unlink_infile = 1;
303
304         /* build the content structures for MIME message */
305         ct = build_mime (infile);
306         cts[0] = ct;
307         cts[1] = NULL;
308
309         /* output MIME message to this temporary file */
310         strncpy (outfile, m_scratch ("", invo_name), sizeof(outfile));
311         unlink_outfile = 1;
312
313         /* output the message */
314         output_message (ct, outfile);
315
316         /* output the temp file to standard output */
317         if ((fp = fopen (outfile, "r")) == NULL)
318             adios (outfile, "unable to open");
319         while (fgets (buffer, BUFSIZ, fp))
320             fputs (buffer, stdout);
321         fclose (fp);
322
323         unlink (infile);
324         unlink_infile = 0;
325
326         unlink (outfile);
327         unlink_outfile = 0;
328
329         free_content (ct);
330         done (0);
331     }
332
333     /*
334      * Process the composition file from a file.
335      */
336
337     /* build the content structures for MIME message */
338     ct = build_mime (compfile);
339     cts[0] = ct;
340     cts[1] = NULL;
341
342     /* output MIME message to this temporary file */
343     strncpy (outfile, m_scratch (compfile, invo_name), sizeof(outfile));
344     unlink_outfile = 1;
345
346     /* output the message */
347     output_message (ct, outfile);
348
349     /*
350      * List the message info
351      */
352     if (listsw)
353         list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
354
355     /* Rename composition draft */
356     snprintf (buffer, sizeof(buffer), "%s.orig", m_backup (compfile));
357     if (rename (compfile, buffer) == NOTOK)
358         adios (compfile, "unable to rename %s to", buffer);
359
360     /* Rename output file to take its place */
361     if (rename (outfile, compfile) == NOTOK) {
362         advise (outfile, "unable to rename %s to", compfile);
363         rename (buffer, compfile);
364         done (1);
365     }
366     unlink_outfile = 0;
367
368     free_content (ct);
369     return done (0);
370 }
371
372
373 int
374 done (int status)
375 {
376     /*
377      * Check if we need to remove stray
378      * temporary files.
379      */
380     if (unlink_infile)
381         unlink (infile);
382     if (unlink_outfile)
383         unlink (outfile);
384
385     exit (status);
386     return 1;  /* dead code to satisfy the compiler */
387 }