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