Renamed all standard sequences (e.g. cur->c) and made them globally changeable
[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 : getcpy(expanddir(cp));
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 : getcpy(expanddir(cp));
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 = getcpy(expandfol(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(toabsdir(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 = getcpy(toabsdir(invo_name));
263
264         if (file && msgs.size)
265                 adios(NULL, "cannot specify msg and file at same time!");
266
267         /*
268         ** check if message is coming from file
269         */
270         if (file) {
271                 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
272                         adios(NULL, "out of memory");
273                 ctp = cts;
274
275                 if ((ct = parse_mime(file)));
276                         *ctp++ = ct;
277         } else {
278                 /*
279                 ** message(s) are coming from a folder
280                 */
281                 if (!msgs.size)
282                         app_msgarg(&msgs, seq_cur);
283                 if (!folder)
284                         folder = getcurfol();
285                 maildir = toabsdir(folder);
286
287                 if (chdir(maildir) == NOTOK)
288                         adios(maildir, "unable to change directory to");
289
290                 /* read folder and create message structure */
291                 if (!(mp = folder_read(folder)))
292                         adios(NULL, "unable to read folder %s", folder);
293
294                 /* check for empty folder */
295                 if (mp->nummsg == 0)
296                         adios(NULL, "no messages in %s", folder);
297
298                 /* parse all the message ranges/sequences and set SELECTED */
299                 for (msgnum = 0; msgnum < msgs.size; msgnum++)
300                         if (!m_convert(mp, msgs.msgs[msgnum]))
301                                 done(1);
302                 seq_setprev(mp);  /* set the previous-sequence */
303
304                 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
305                                 sizeof(*cts))))
306                         adios(NULL, "out of memory");
307                 ctp = cts;
308
309                 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
310                         if (is_selected(mp, msgnum)) {
311                                 char *msgnam;
312
313                                 msgnam = m_name(msgnum);
314                                 if ((ct = parse_mime(msgnam)))
315                                         *ctp++ = ct;
316                         }
317                 }
318         }
319
320         if (!*cts)
321                 done(1);
322
323         userrs = 1;
324         SIGNAL(SIGQUIT, quitser);
325         SIGNAL(SIGPIPE, pipeser);
326
327         /*
328         ** Get the associated umask for the relevant contents.
329         */
330         for (ctp = cts; *ctp; ctp++) {
331                 struct stat st;
332
333                 ct = *ctp;
334                 if (type_ok(ct, 1) && !ct->c_umask) {
335                         if (stat(ct->c_file, &st) != NOTOK)
336                                 ct->c_umask = ~(st.st_mode & 0777);
337                         else
338                                 ct->c_umask = ~m_gmprot();
339                 }
340         }
341
342         /*
343         ** Write the content to a file
344         */
345         write_content(cts, outfile);
346
347         /* Now free all the structures for the content */
348         for (ctp = cts; *ctp; ctp++)
349                 free_content(*ctp);
350
351         free((char *) cts);
352         cts = NULL;
353
354         /* If reading from a folder, do some updating */
355         if (mp) {
356                 context_replace(curfolder, folder); /* update current folder */
357                 seq_setcur(mp, mp->hghsel);  /* update current message */
358                 seq_save(mp);  /* synchronize sequences  */
359                 context_save();  /* save the context file  */
360         }
361
362         done(0);
363         return 1;
364 }
365
366
367 static int
368 write_content(CT *cts, char *outfile)
369 {
370         CT ct, *ctp;
371
372         for (ctp = cts; *ctp; ctp++) {
373                 ct = *ctp;
374                 output_message(ct, outfile);
375         }
376
377         flush_errors();
378         return OK;
379 }
380
381
382 static RETSIGTYPE
383 pipeser(int i)
384 {
385         if (i == SIGQUIT) {
386                 unlink("core");
387                 fflush(stdout);
388                 fprintf(stderr, "\n");
389                 fflush(stderr);
390         }
391
392         done(1);
393         /* NOTREACHED */
394 }