Introduced FDEF and FCUR for speaking arguments to getfolder().
[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/mts.h>
16 #include <h/tws.h>
17 #include <h/mime.h>
18 #include <h/mhparse.h>
19 #include <h/mhcachesbr.h>
20 #include <h/utils.h>
21
22 #ifdef HAVE_SYS_WAIT_H
23 # include <sys/wait.h>
24 #endif
25
26 static struct swit switches[] = {
27 #define CHECKSW  0
28         { "check", 0 },
29 #define NCHECKSW  1
30         { "nocheck", 0 },
31 #define VERBSW  2
32         { "verbose", 0 },
33 #define NVERBSW  3
34         { "noverbose", 0 },
35 #define FILESW  4
36         { "file file", 0 },
37 #define OUTFILESW  5
38         { "outfile file", 0 },
39 #define PARTSW  6
40         { "part number", 0 },
41 #define TYPESW  7
42         { "type content", 0 },
43 #define RCACHESW  8
44         { "rcache policy", 0 },
45 #define WCACHESW  9
46         { "wcache policy", 0 },
47 #define VERSIONSW  10
48         { "version", 0 },
49 #define HELPSW  11
50         { "help", 0 },
51
52 /*
53 ** switches for debugging
54 */
55 #define DEBUGSW  12
56         { "debug", -5 },
57         { NULL, 0 }
58 };
59
60
61 int ebcdicsw = 0;  /* hack for linking purposes */
62
63 /* mhparse.c */
64 extern char *tmp;  /* directory to place temp files */
65
66 /* mhcachesbr.c */
67 extern int rcachesw;
68 extern int wcachesw;
69 extern char *cache_public;
70 extern char *cache_private;
71
72 /* mhmisc.c */
73 extern int npart;
74 extern int ntype;
75 extern char *parts[NPARTS + 1];
76 extern char *types[NTYPES + 1];
77 extern int userrs;
78
79 /*
80 ** This is currently needed to keep mhparse happy.
81 ** This needs to be changed.
82 */
83 pid_t xpid  = 0;
84
85 int debugsw = 0;
86 int verbosw = 0;
87
88 #define quitser pipeser
89
90 /* mhparse.c */
91 CT parse_mime(char *);
92
93 /* mhoutsbr.c */
94 int output_message(CT, char *);
95
96 /* mhmisc.c */
97 int part_ok(CT, int);
98 int type_ok(CT, int);
99 void set_endian(void);
100 void flush_errors(void);
101
102 /* mhfree.c */
103 void free_content(CT);
104 extern CT *cts;
105 void freects_done(int) NORETURN;
106
107 /*
108 ** static prototypes
109 */
110 static int write_content(CT *, char *);
111 static RETSIGTYPE pipeser(int);
112
113
114 int
115 main(int argc, char **argv)
116 {
117         int msgnum, *icachesw;
118         char *cp, *file = NULL, *folder = NULL;
119         char *maildir, buf[100], *outfile = NULL;
120         char **argp, **arguments;
121         struct msgs_array msgs = { 0, 0, NULL };
122         struct msgs *mp = NULL;
123         CT ct, *ctp;
124
125         done=freects_done;
126
127 #ifdef LOCALE
128         setlocale(LC_ALL, "");
129 #endif
130         invo_name = mhbasename(argv[0]);
131
132         /* read user profile/context */
133         context_read();
134
135         arguments = getarguments(invo_name, argc, argv, 1);
136         argp = arguments;
137
138         /*
139         ** Parse arguments
140         */
141         while ((cp = *argp++)) {
142                 if (*cp == '-') {
143                         switch (smatch(++cp, switches)) {
144                         case AMBIGSW:
145                                 ambigsw(cp, switches);
146                                 done(1);
147                         case UNKWNSW:
148                                 adios(NULL, "-%s unknown", cp);
149
150                         case HELPSW:
151                                 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
152                                 print_help(buf, switches, 1);
153                                 done(1);
154                         case VERSIONSW:
155                                 print_version(invo_name);
156                                 done(1);
157
158                         case RCACHESW:
159                                 icachesw = &rcachesw;
160                                 goto do_cache;
161                         case WCACHESW:
162                                 icachesw = &wcachesw;
163 do_cache:
164                                 if (!(cp = *argp++) || *cp == '-')
165                                         adios(NULL, "missing argument to %s",
166                                                         argp[-2]);
167                                 switch (*icachesw = smatch(cp, caches)) {
168                                 case AMBIGSW:
169                                         ambigsw(cp, caches);
170                                         done(1);
171                                 case UNKWNSW:
172                                         adios(NULL, "%s unknown", cp);
173                                 default:
174                                         break;
175                                 }
176                                 continue;
177
178                         case CHECKSW:
179                                 checksw++;
180                                 continue;
181                         case NCHECKSW:
182                                 checksw = 0;
183                                 continue;
184
185                         case PARTSW:
186                                 if (!(cp = *argp++) || *cp == '-')
187                                         adios(NULL, "missing argument to %s", argp[-2]);
188                                 if (npart >= NPARTS)
189                                         adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
190                                 parts[npart++] = cp;
191                                 continue;
192
193                         case TYPESW:
194                                 if (!(cp = *argp++) || *cp == '-')
195                                         adios(NULL, "missing argument to %s", argp[-2]);
196                                 if (ntype >= NTYPES)
197                                         adios(NULL, "too many types (starting with %s), %d max",
198                                                    cp, NTYPES);
199                                 types[ntype++] = cp;
200                                 continue;
201
202                         case FILESW:
203                                 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
204                                         adios(NULL, "missing argument to %s",
205                                                         argp[-2]);
206                                 file = *cp == '-' ? cp : path(cp, TFILE);
207                                 continue;
208
209                         case OUTFILESW:
210                                 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
211                                         adios(NULL, "missing argument to %s",
212                                                         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(FCUR);
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),
308                                 sizeof(*cts))))
309                         adios(NULL, "out of memory");
310                 ctp = cts;
311
312                 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
313                         if (is_selected(mp, msgnum)) {
314                                 char *msgnam;
315
316                                 msgnam = m_name(msgnum);
317                                 if ((ct = parse_mime(msgnam)))
318                                         *ctp++ = ct;
319                         }
320                 }
321         }
322
323         if (!*cts)
324                 done(1);
325
326         userrs = 1;
327         SIGNAL(SIGQUIT, quitser);
328         SIGNAL(SIGPIPE, pipeser);
329
330         /*
331         ** Get the associated umask for the relevant contents.
332         */
333         for (ctp = cts; *ctp; ctp++) {
334                 struct stat st;
335
336                 ct = *ctp;
337                 if (type_ok(ct, 1) && !ct->c_umask) {
338                         if (stat(ct->c_file, &st) != NOTOK)
339                                 ct->c_umask = ~(st.st_mode & 0777);
340                         else
341                                 ct->c_umask = ~m_gmprot();
342                 }
343         }
344
345         /*
346         ** Write the content to a file
347         */
348         write_content(cts, outfile);
349
350         /* Now free all the structures for the content */
351         for (ctp = cts; *ctp; ctp++)
352                 free_content(*ctp);
353
354         free((char *) cts);
355         cts = NULL;
356
357         /* If reading from a folder, do some updating */
358         if (mp) {
359                 context_replace(pfolder, folder);  /* update current folder */
360                 seq_setcur(mp, mp->hghsel);  /* update current message */
361                 seq_save(mp);  /* synchronize sequences  */
362                 context_save();  /* save the context file  */
363         }
364
365         done(0);
366         return 1;
367 }
368
369
370 static int
371 write_content(CT *cts, char *outfile)
372 {
373         CT ct, *ctp;
374
375         for (ctp = cts; *ctp; ctp++) {
376                 ct = *ctp;
377                 output_message(ct, outfile);
378         }
379
380         flush_errors();
381         return OK;
382 }
383
384
385 static RETSIGTYPE
386 pipeser(int i)
387 {
388         if (i == SIGQUIT) {
389                 unlink("core");
390                 fflush(stdout);
391                 fprintf(stderr, "\n");
392                 fflush(stderr);
393         }
394
395         done(1);
396         /* NOTREACHED */
397 }