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