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