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