3 * mhtest.c -- test harness for MIME routines
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.
14 #include <h/signals.h>
21 #include <h/mhparse.h>
22 #include <h/mhcachesbr.h>
24 #ifdef HAVE_SYS_WAIT_H
25 # include <sys/wait.h>
29 * We allocate space for message names (msgs array)
30 * this number of elements at a time.
35 static struct swit switches[] = {
47 { "outfile file", 0 },
51 { "type content", 0 },
53 { "rcache policy", 0 },
55 { "wcache policy", 0 },
62 * switches for debugging
70 int ebcdicsw = 0; /* hack for linking purposes */
74 extern char *tmp; /* directory to place temp files */
79 extern char *cache_public;
80 extern char *cache_private;
85 extern char *parts[NPARTS + 1];
86 extern char *types[NTYPES + 1];
90 * This is currently needed to keep mhparse happy.
91 * This needs to be changed.
98 /* The list of top-level contents to display */
101 #define quitser pipeser
104 CT parse_mime (char *);
107 int output_message (CT, char *);
110 int part_ok (CT, int);
111 int type_ok (CT, int);
112 void set_endian (void);
113 void flush_errors (void);
116 void free_content (CT);
121 static int write_content (CT *, char *);
122 static RETSIGTYPE pipeser (int);
126 main (int argc, char **argv)
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;
136 setlocale(LC_ALL, "");
138 invo_name = r1bindex (argv[0], '/');
140 /* read user profile/context */
143 arguments = getarguments (invo_name, argc, argv, 1);
147 * Allocate the initial space to record message
148 * names, ranges, and sequences.
152 if (!(msgs = (char **) malloc ((size_t) (maxmsgs * sizeof(*msgs)))))
153 adios (NULL, "unable to allocate storage");
158 while ((cp = *argp++)) {
160 switch (smatch (++cp, switches)) {
162 ambigsw (cp, switches);
165 adios (NULL, "-%s unknown", cp);
168 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
170 print_help (buf, switches, 1);
173 print_version(invo_name);
177 icachesw = &rcachesw;
180 icachesw = &wcachesw;
182 if (!(cp = *argp++) || *cp == '-')
183 adios (NULL, "missing argument to %s", argp[-2]);
184 switch (*icachesw = smatch (cp, caches)) {
186 ambigsw (cp, caches);
189 adios (NULL, "%s unknown", cp);
203 if (!(cp = *argp++) || *cp == '-')
204 adios (NULL, "missing argument to %s", argp[-2]);
206 adios (NULL, "too many parts (starting with %s), %d max",
212 if (!(cp = *argp++) || *cp == '-')
213 adios (NULL, "missing argument to %s", argp[-2]);
215 adios (NULL, "too many types (starting with %s), %d max",
221 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
222 adios (NULL, "missing argument to %s", argp[-2]);
223 file = *cp == '-' ? cp : path (cp, TFILE);
227 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
228 adios (NULL, "missing argument to %s", argp[-2]);
229 outfile = *cp == '-' ? cp : path (cp, TFILE);
243 if (*cp == '+' || *cp == '@') {
245 adios (NULL, "only one folder at a time!");
247 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
250 * Check if we need to allocate more space
251 * for message names/ranges/sequences.
253 if (nummsgs >= maxmsgs) {
255 if (!(msgs = (char **) realloc (msgs,
256 (size_t) (maxmsgs * sizeof(*msgs)))))
257 adios (NULL, "unable to reallocate msgs storage");
259 msgs[nummsgs++] = cp;
263 /* null terminate the list of acceptable parts/types */
270 adios (NULL, "must specify output file");
272 /* Check for public cache location */
273 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
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));
282 * Check for storage directory. If specified,
283 * then store temporary files there. Else we
284 * store them in standard nmh directory.
286 if ((cp = context_find (nmhstorage)) && *cp)
287 tmp = concat (cp, "/", invo_name, NULL);
289 tmp = add (m_maildir (invo_name), NULL);
291 if (!context_find ("path"))
292 free (path ("./", TFOLDER));
295 adios (NULL, "cannot specify msg and file at same time!");
298 * check if message is coming from file
301 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
302 adios (NULL, "out of memory");
305 if ((ct = parse_mime (file)));
309 * message(s) are coming from a folder
312 msgs[nummsgs++] = "cur";
314 folder = getfolder (1);
315 maildir = m_maildir (folder);
317 if (chdir (maildir) == NOTOK)
318 adios (maildir, "unable to change directory to");
320 /* read folder and create message structure */
321 if (!(mp = folder_read (folder)))
322 adios (NULL, "unable to read folder %s", folder);
324 /* check for empty folder */
326 adios (NULL, "no messages in %s", folder);
328 /* parse all the message ranges/sequences and set SELECTED */
329 for (msgnum = 0; msgnum < nummsgs; msgnum++)
330 if (!m_convert (mp, msgs[msgnum]))
332 seq_setprev (mp); /* set the previous-sequence */
334 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
335 adios (NULL, "out of memory");
338 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
339 if (is_selected(mp, msgnum)) {
342 msgnam = m_name (msgnum);
343 if ((ct = parse_mime (msgnam)))
353 SIGNAL (SIGQUIT, quitser);
354 SIGNAL (SIGPIPE, pipeser);
357 * Get the associated umask for the relevant contents.
359 for (ctp = cts; *ctp; 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);
367 ct->c_umask = ~m_gmprot();
372 * Write the content to a file
374 write_content (cts, outfile);
376 /* Now free all the structures for the content */
377 for (ctp = cts; *ctp; ctp++)
383 /* If reading from a folder, do some updating */
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 */
396 write_content (CT *cts, char *outfile)
400 for (ctp = cts; *ctp; ctp++) {
402 output_message (ct, outfile);
416 fprintf (stderr, "\n");
435 return 1; /* dead code to satisfy the compiler */