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>
25 #ifdef HAVE_SYS_WAIT_H
26 # include <sys/wait.h>
30 * We allocate space for message names (msgs array)
31 * this number of elements at a time.
36 static struct swit switches[] = {
48 { "outfile file", 0 },
52 { "type content", 0 },
54 { "rcache policy", 0 },
56 { "wcache policy", 0 },
63 * switches for debugging
71 int ebcdicsw = 0; /* hack for linking purposes */
75 extern char *tmp; /* directory to place temp files */
80 extern char *cache_public;
81 extern char *cache_private;
86 extern char *parts[NPARTS + 1];
87 extern char *types[NTYPES + 1];
91 * This is currently needed to keep mhparse happy.
92 * This needs to be changed.
99 /* The list of top-level contents to display */
102 #define quitser pipeser
105 CT parse_mime (char *);
108 int output_message (CT, char *);
111 int part_ok (CT, int);
112 int type_ok (CT, int);
113 void set_endian (void);
114 void flush_errors (void);
117 void free_content (CT);
122 static int write_content (CT *, char *);
123 static RETSIGTYPE pipeser (int);
127 main (int argc, char **argv)
129 int nummsgs, maxmsgs, msgnum, *icachesw;
130 char *cp, *file = NULL, *folder = NULL;
131 char *maildir, buf[100], *outfile = NULL;
132 char **argp, **arguments, **msgs;
133 struct msgs *mp = NULL;
137 setlocale(LC_ALL, "");
139 invo_name = r1bindex (argv[0], '/');
141 /* read user profile/context */
144 arguments = getarguments (invo_name, argc, argv, 1);
148 * Allocate the initial space to record message
149 * names, ranges, and sequences.
153 msgs = (char **) mh_xmalloc ((size_t) (maxmsgs * sizeof(*msgs)));
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 msgs = (char **) mh_xrealloc (msgs,
256 (size_t) (maxmsgs * sizeof(*msgs)));
258 msgs[nummsgs++] = cp;
262 /* null terminate the list of acceptable parts/types */
269 adios (NULL, "must specify output file");
271 /* Check for public cache location */
272 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
275 /* Check for private cache location */
276 if (!(cache_private = context_find (nmhprivcache)))
277 cache_private = ".cache";
278 cache_private = getcpy (m_maildir (cache_private));
281 * Check for storage directory. If specified,
282 * then store temporary files there. Else we
283 * store them in standard nmh directory.
285 if ((cp = context_find (nmhstorage)) && *cp)
286 tmp = concat (cp, "/", invo_name, NULL);
288 tmp = add (m_maildir (invo_name), NULL);
290 if (!context_find ("path"))
291 free (path ("./", TFOLDER));
294 adios (NULL, "cannot specify msg and file at same time!");
297 * check if message is coming from file
300 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
301 adios (NULL, "out of memory");
304 if ((ct = parse_mime (file)));
308 * message(s) are coming from a folder
311 msgs[nummsgs++] = "cur";
313 folder = getfolder (1);
314 maildir = m_maildir (folder);
316 if (chdir (maildir) == NOTOK)
317 adios (maildir, "unable to change directory to");
319 /* read folder and create message structure */
320 if (!(mp = folder_read (folder)))
321 adios (NULL, "unable to read folder %s", folder);
323 /* check for empty folder */
325 adios (NULL, "no messages in %s", folder);
327 /* parse all the message ranges/sequences and set SELECTED */
328 for (msgnum = 0; msgnum < nummsgs; msgnum++)
329 if (!m_convert (mp, msgs[msgnum]))
331 seq_setprev (mp); /* set the previous-sequence */
333 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
334 adios (NULL, "out of memory");
337 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
338 if (is_selected(mp, msgnum)) {
341 msgnam = m_name (msgnum);
342 if ((ct = parse_mime (msgnam)))
352 SIGNAL (SIGQUIT, quitser);
353 SIGNAL (SIGPIPE, pipeser);
356 * Get the associated umask for the relevant contents.
358 for (ctp = cts; *ctp; ctp++) {
362 if (type_ok (ct, 1) && !ct->c_umask) {
363 if (stat (ct->c_file, &st) != NOTOK)
364 ct->c_umask = ~(st.st_mode & 0777);
366 ct->c_umask = ~m_gmprot();
371 * Write the content to a file
373 write_content (cts, outfile);
375 /* Now free all the structures for the content */
376 for (ctp = cts; *ctp; ctp++)
382 /* If reading from a folder, do some updating */
384 context_replace (pfolder, folder);/* update current folder */
385 seq_setcur (mp, mp->hghsel); /* update current message */
386 seq_save (mp); /* synchronize sequences */
387 context_save (); /* save the context file */
395 write_content (CT *cts, char *outfile)
399 for (ctp = cts; *ctp; ctp++) {
401 output_message (ct, outfile);
415 fprintf (stderr, "\n");
434 return 1; /* dead code to satisfy the compiler */