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