The result from the call to parse_mime() was ignored.
[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
47 /*
48 ** switches for debugging
49 */
50 #define DEBUGSW  12
51         { "debug", -5 },
52         { NULL, 0 }
53 };
54
55
56 int ebcdicsw = 0;  /* hack for linking purposes */
57
58 /* mhparse.c */
59 extern char *tmp;  /* directory to place temp files */
60
61 /* mhcachesbr.c */
62 extern int rcachesw;
63 extern int wcachesw;
64 extern char *cache_public;
65 extern char *cache_private;
66
67 /* mhmisc.c */
68 extern int npart;
69 extern int ntype;
70 extern char *parts[NPARTS + 1];
71 extern char *types[NTYPES + 1];
72 extern int userrs;
73
74 /*
75 ** This is currently needed to keep mhparse happy.
76 ** This needs to be changed.
77 */
78 pid_t xpid  = 0;
79
80 int debugsw = 0;
81 int verbosw = 0;
82
83 #define quitser pipeser
84
85 /* mhparse.c */
86 CT parse_mime(char *);
87
88 /* mhoutsbr.c */
89 int output_message(CT, char *);
90
91 /* mhmisc.c */
92 int part_ok(CT, int);
93 int type_ok(CT, int);
94 void set_endian(void);
95 void flush_errors(void);
96
97 /* mhfree.c */
98 void free_content(CT);
99 extern CT *cts;
100 void freects_done(int) NORETURN;
101
102 /*
103 ** static prototypes
104 */
105 static int write_content(CT *, char *);
106 static RETSIGTYPE pipeser(int);
107
108
109 int
110 main(int argc, char **argv)
111 {
112         int msgnum, *icachesw;
113         char *cp, *file = NULL, *folder = NULL;
114         char *maildir, buf[100], *outfile = NULL;
115         char **argp, **arguments;
116         struct msgs_array msgs = { 0, 0, NULL };
117         struct msgs *mp = NULL;
118         CT ct, *ctp;
119
120         done=freects_done;
121
122 #ifdef LOCALE
123         setlocale(LC_ALL, "");
124 #endif
125         invo_name = mhbasename(argv[0]);
126
127         /* read user profile/context */
128         context_read();
129
130         arguments = getarguments(invo_name, argc, argv, 1);
131         argp = arguments;
132
133         /*
134         ** Parse arguments
135         */
136         while ((cp = *argp++)) {
137                 if (*cp == '-') {
138                         switch (smatch(++cp, switches)) {
139                         case AMBIGSW:
140                                 ambigsw(cp, switches);
141                                 done(1);
142                         case UNKWNSW:
143                                 adios(NULL, "-%s unknown", cp);
144
145                         case HELPSW:
146                                 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
147                                 print_help(buf, switches, 1);
148                                 done(1);
149                         case VERSIONSW:
150                                 print_version(invo_name);
151                                 done(1);
152
153                         case RCACHESW:
154                                 icachesw = &rcachesw;
155                                 goto do_cache;
156                         case WCACHESW:
157                                 icachesw = &wcachesw;
158 do_cache:
159                                 if (!(cp = *argp++) || *cp == '-')
160                                         adios(NULL, "missing argument to %s",
161                                                         argp[-2]);
162                                 switch (*icachesw = smatch(cp, caches)) {
163                                 case AMBIGSW:
164                                         ambigsw(cp, caches);
165                                         done(1);
166                                 case UNKWNSW:
167                                         adios(NULL, "%s unknown", cp);
168                                 default:
169                                         break;
170                                 }
171                                 continue;
172
173                         case CHECKSW:
174                                 checksw++;
175                                 continue;
176                         case NCHECKSW:
177                                 checksw = 0;
178                                 continue;
179
180                         case PARTSW:
181                                 if (!(cp = *argp++) || *cp == '-')
182                                         adios(NULL, "missing argument to %s", argp[-2]);
183                                 if (npart >= NPARTS)
184                                         adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
185                                 parts[npart++] = cp;
186                                 continue;
187
188                         case TYPESW:
189                                 if (!(cp = *argp++) || *cp == '-')
190                                         adios(NULL, "missing argument to %s", argp[-2]);
191                                 if (ntype >= NTYPES)
192                                         adios(NULL, "too many types (starting with %s), %d max",
193                                                    cp, NTYPES);
194                                 types[ntype++] = cp;
195                                 continue;
196
197                         case FILESW:
198                                 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
199                                         adios(NULL, "missing argument to %s",
200                                                         argp[-2]);
201                                 file = *cp == '-' ? cp : getcpy(expanddir(cp));
202                                 continue;
203
204                         case OUTFILESW:
205                                 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
206                                         adios(NULL, "missing argument to %s",
207                                                         argp[-2]);
208                                 outfile = *cp == '-' ? cp : getcpy(expanddir(cp));
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 = getcpy(expandfol(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         set_endian();
236
237         if (outfile == NULL)
238                 adios(NULL, "must specify output file");
239
240         /* Check for public cache location */
241         if ((cache_public = context_find(nmhcache)) && *cache_public != '/')
242                 cache_public = NULL;
243
244         /* Check for private cache location */
245         if (!(cache_private = context_find(nmhprivcache)))
246                 cache_private = ".cache";
247         cache_private = getcpy(toabsdir(cache_private));
248
249         /*
250         ** Check for storage directory.  If specified,
251         ** then store temporary files there.  Else we
252         ** store them in standard nmh directory.
253         */
254         if ((cp = context_find(nmhstorage)) && *cp)
255                 tmp = concat(cp, "/", invo_name, NULL);
256         else
257                 tmp = getcpy(toabsdir(invo_name));
258
259         if (file && msgs.size)
260                 adios(NULL, "cannot specify msg and file at same time!");
261
262         /*
263         ** check if message is coming from file
264         */
265         if (file) {
266                 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
267                         adios(NULL, "out of memory");
268                 ctp = cts;
269
270                 if ((ct = parse_mime(file)))
271                         *ctp++ = ct;
272         } else {
273                 /*
274                 ** message(s) are coming from a folder
275                 */
276                 if (!msgs.size)
277                         app_msgarg(&msgs, seq_cur);
278                 if (!folder)
279                         folder = getcurfol();
280                 maildir = toabsdir(folder);
281
282                 if (chdir(maildir) == NOTOK)
283                         adios(maildir, "unable to change directory to");
284
285                 /* read folder and create message structure */
286                 if (!(mp = folder_read(folder)))
287                         adios(NULL, "unable to read folder %s", folder);
288
289                 /* check for empty folder */
290                 if (mp->nummsg == 0)
291                         adios(NULL, "no messages in %s", folder);
292
293                 /* parse all the message ranges/sequences and set SELECTED */
294                 for (msgnum = 0; msgnum < msgs.size; msgnum++)
295                         if (!m_convert(mp, msgs.msgs[msgnum]))
296                                 done(1);
297                 seq_setprev(mp);  /* set the previous-sequence */
298
299                 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
300                                 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(curfolder, 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 RETSIGTYPE
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 }