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