Switch from using RETSIGTYPE via autoconf to void, since POSIX says that's
[mmh] / uip / mhtest.c
1
2 /*
3  * mhtest.c -- test harness for MIME routines
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 VERBSW                  2
29     { "verbose", 0 },
30 #define NVERBSW                 3
31     { "noverbose", 0 },
32 #define FILESW                  4
33     { "file file", 0 },
34 #define OUTFILESW               5
35     { "outfile file", 0 },
36 #define PARTSW                  6
37     { "part number", 0 },
38 #define TYPESW                  7
39     { "type content", 0 },
40 #define RCACHESW                8
41     { "rcache policy", 0 },
42 #define WCACHESW                9
43     { "wcache policy", 0 },
44 #define VERSIONSW              10
45     { "version", 0 },
46 #define HELPSW                 11
47     { "help", 0 },
48
49 /*
50  * switches for debugging
51  */
52 #define DEBUGSW                12
53     { "debug", -5 },
54     { NULL, 0 }
55 };
56
57
58 int ebcdicsw = 0;       /* hack for linking purposes */
59
60 /* mhparse.c */
61 extern char *tmp;       /* directory to place temp files */
62
63 /* mhcachesbr.c */
64 extern int rcachesw;
65 extern int wcachesw;
66 extern char *cache_public;
67 extern char *cache_private;
68
69 /* mhmisc.c */
70 extern int npart;
71 extern int ntype;
72 extern char *parts[NPARTS + 1];
73 extern char *types[NTYPES + 1];
74 extern int userrs;
75
76 /*
77  * This is currently needed to keep mhparse happy.
78  * This needs to be changed.
79  */
80 pid_t xpid  = 0;
81
82 int debugsw = 0;
83 int verbosw = 0;
84
85 #define quitser pipeser
86
87 /* mhparse.c */
88 CT parse_mime (char *);
89
90 /* mhoutsbr.c */
91 int output_message (CT, char *);
92
93 /* mhmisc.c */
94 int part_ok (CT, int);
95 int type_ok (CT, int);
96 void set_endian (void);
97 void flush_errors (void);
98
99 /* mhfree.c */
100 void free_content (CT);
101 extern CT *cts;
102 void freects_done (int) NORETURN;
103
104 /*
105  * static prototypes
106  */
107 static int write_content (CT *, char *);
108 static void pipeser (int);
109
110
111 int
112 main (int argc, char **argv)
113 {
114     int msgnum, *icachesw;
115     char *cp, *file = NULL, *folder = NULL;
116     char *maildir, buf[100], *outfile = NULL;
117     char **argp, **arguments;
118     struct msgs_array msgs = { 0, 0, NULL };
119     struct msgs *mp = NULL;
120     CT ct, *ctp;
121
122     done=freects_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     /*
136      * Parse arguments
137      */
138     while ((cp = *argp++)) {
139         if (*cp == '-') {
140             switch (smatch (++cp, switches)) {
141             case AMBIGSW: 
142                 ambigsw (cp, switches);
143                 done (1);
144             case UNKWNSW: 
145                 adios (NULL, "-%s unknown", cp);
146
147             case HELPSW: 
148                 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
149                         invo_name);
150                 print_help (buf, switches, 1);
151                 done (1);
152             case VERSIONSW:
153                 print_version(invo_name);
154                 done (1);
155
156             case RCACHESW:
157                 icachesw = &rcachesw;
158                 goto do_cache;
159             case WCACHESW:
160                 icachesw = &wcachesw;
161 do_cache:
162                 if (!(cp = *argp++) || *cp == '-')
163                     adios (NULL, "missing argument to %s", argp[-2]);
164                 switch (*icachesw = smatch (cp, caches)) {
165                 case AMBIGSW:
166                     ambigsw (cp, caches);
167                     done (1);
168                 case UNKWNSW:
169                     adios (NULL, "%s unknown", cp);
170                 default:
171                     break;
172                 }
173                 continue;
174
175             case CHECKSW:
176                 checksw++;
177                 continue;
178             case NCHECKSW:
179                 checksw = 0;
180                 continue;
181
182             case PARTSW:
183                 if (!(cp = *argp++) || *cp == '-')
184                     adios (NULL, "missing argument to %s", argp[-2]);
185                 if (npart >= NPARTS)
186                     adios (NULL, "too many parts (starting with %s), %d max",
187                            cp, NPARTS);
188                 parts[npart++] = cp;
189                 continue;
190
191             case TYPESW:
192                 if (!(cp = *argp++) || *cp == '-')
193                     adios (NULL, "missing argument to %s", argp[-2]);
194                 if (ntype >= NTYPES)
195                     adios (NULL, "too many types (starting with %s), %d max",
196                            cp, NTYPES);
197                 types[ntype++] = cp;
198                 continue;
199
200             case FILESW:
201                 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
202                     adios (NULL, "missing argument to %s", argp[-2]);
203                 file = *cp == '-' ? cp : path (cp, TFILE);
204                 continue;
205
206             case OUTFILESW:
207                 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
208                     adios (NULL, "missing argument to %s", argp[-2]);
209                 outfile = *cp == '-' ? cp : path (cp, TFILE);
210                 continue;
211
212             case VERBSW: 
213                 verbosw = 1;
214                 continue;
215             case NVERBSW: 
216                 verbosw = 0;
217                 continue;
218             case DEBUGSW:
219                 debugsw = 1;
220                 continue;
221             }
222         }
223         if (*cp == '+' || *cp == '@') {
224             if (folder)
225                 adios (NULL, "only one folder at a time!");
226             else
227                 folder = pluspath (cp);
228         } else
229                 app_msgarg(&msgs, cp);
230     }
231
232     /* null terminate the list of acceptable parts/types */
233     parts[npart] = NULL;
234     types[ntype] = NULL;
235
236     set_endian ();
237
238     if (outfile == NULL)
239         adios (NULL, "must specify output file");
240
241     /* Check for public cache location */
242     if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
243         cache_public = NULL;
244
245     /* Check for private cache location */
246     if (!(cache_private = context_find (nmhprivcache)))
247         cache_private = ".cache";
248     cache_private = getcpy (m_maildir (cache_private));
249
250     /*
251      * Check for storage directory.  If specified,
252      * then store temporary files there.  Else we
253      * store them in standard nmh directory.
254      */
255     if ((cp = context_find (nmhstorage)) && *cp)
256         tmp = concat (cp, "/", invo_name, NULL);
257     else
258         tmp = add (m_maildir (invo_name), NULL);
259
260     if (!context_find ("path"))
261         free (path ("./", TFOLDER));
262
263     if (file && msgs.size)
264         adios (NULL, "cannot specify msg and file at same time!");
265
266     /*
267      * check if message is coming from file
268      */
269     if (file) {
270         if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
271             adios (NULL, "out of memory");
272         ctp = cts;
273
274         if ((ct = parse_mime (file)))
275             *ctp++ = ct;
276     } else {
277         /*
278          * message(s) are coming from a folder
279          */
280         if (!msgs.size)
281             app_msgarg(&msgs, "cur");
282         if (!folder)
283             folder = getfolder (1);
284         maildir = m_maildir (folder);
285
286         if (chdir (maildir) == NOTOK)
287             adios (maildir, "unable to change directory to");
288
289         /* read folder and create message structure */
290         if (!(mp = folder_read (folder)))
291             adios (NULL, "unable to read folder %s", folder);
292
293         /* check for empty folder */
294         if (mp->nummsg == 0)
295             adios (NULL, "no messages in %s", folder);
296
297         /* parse all the message ranges/sequences and set SELECTED */
298         for (msgnum = 0; msgnum < msgs.size; msgnum++)
299             if (!m_convert (mp, msgs.msgs[msgnum]))
300                 done (1);
301         seq_setprev (mp);       /* set the previous-sequence */
302
303         if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
304             adios (NULL, "out of memory");
305         ctp = cts;
306
307         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
308             if (is_selected(mp, msgnum)) {
309                 char *msgnam;
310
311                 msgnam = m_name (msgnum);
312                 if ((ct = parse_mime (msgnam)))
313                     *ctp++ = ct;
314             }
315         }
316     }
317
318     if (!*cts)
319         done (1);
320
321     userrs = 1;
322     SIGNAL (SIGQUIT, quitser);
323     SIGNAL (SIGPIPE, pipeser);
324
325     /*
326      * Get the associated umask for the relevant contents.
327      */
328     for (ctp = cts; *ctp; ctp++) {
329         struct stat st;
330
331         ct = *ctp;
332         if (type_ok (ct, 1) && !ct->c_umask) {
333             if (stat (ct->c_file, &st) != NOTOK)
334                 ct->c_umask = ~(st.st_mode & 0777);
335             else
336                 ct->c_umask = ~m_gmprot();
337         }
338     }
339
340     /*
341      * Write the content to a file
342      */
343     write_content (cts, outfile);
344
345     /* Now free all the structures for the content */
346     for (ctp = cts; *ctp; ctp++)
347         free_content (*ctp);
348
349     free ((char *) cts);
350     cts = NULL;
351
352     /* If reading from a folder, do some updating */
353     if (mp) {
354         context_replace (pfolder, folder);/* update current folder  */
355         seq_setcur (mp, mp->hghsel);      /* update current message */
356         seq_save (mp);                    /* synchronize sequences  */
357         context_save ();                  /* save the context file  */
358     }
359
360     done (0);
361     return 1;
362 }
363
364
365 static int
366 write_content (CT *cts, char *outfile)
367 {
368     CT ct, *ctp;
369
370     for (ctp = cts; *ctp; ctp++) {
371         ct = *ctp;
372         output_message (ct, outfile);
373     }
374
375     flush_errors ();
376     return OK;
377 }
378
379
380 static void
381 pipeser (int i)
382 {
383     if (i == SIGQUIT) {
384         unlink ("core");
385         fflush (stdout);
386         fprintf (stderr, "\n");
387         fflush (stderr);
388     }
389
390     done (1);
391     /* NOTREACHED */
392 }