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