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