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