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