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
72 int ebcdicsw = 0; /* hack for linking purposes */
76 extern char *tmp; /* directory to place temp files */
81 extern char *cache_public;
82 extern char *cache_private;
87 extern char *parts[NPARTS + 1];
88 extern char *types[NTYPES + 1];
92 * This is currently needed to keep mhparse happy.
93 * This needs to be changed.
100 /* The list of top-level contents to display */
103 #define quitser pipeser
106 CT parse_mime (char *);
109 int output_message (CT, char *);
112 int part_ok (CT, int);
113 int type_ok (CT, int);
114 void set_endian (void);
115 void flush_errors (void);
118 void free_content (CT);
123 static int write_content (CT *, char *);
124 static RETSIGTYPE pipeser (int);
128 main (int argc, char **argv)
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;
138 setlocale(LC_ALL, "");
140 invo_name = r1bindex (argv[0], '/');
142 /* read user profile/context */
145 arguments = getarguments (invo_name, argc, argv, 1);
149 * Allocate the initial space to record message
150 * names, ranges, and sequences.
154 if (!(msgs = (char **) malloc ((size_t) (maxmsgs * sizeof(*msgs)))))
155 adios (NULL, "unable to allocate storage");
160 while ((cp = *argp++)) {
162 switch (smatch (++cp, switches)) {
164 ambigsw (cp, switches);
167 adios (NULL, "-%s unknown", cp);
170 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
172 print_help (buf, switches, 1);
175 print_version(invo_name);
179 icachesw = &rcachesw;
182 icachesw = &wcachesw;
184 if (!(cp = *argp++) || *cp == '-')
185 adios (NULL, "missing argument to %s", argp[-2]);
186 switch (*icachesw = smatch (cp, caches)) {
188 ambigsw (cp, caches);
191 adios (NULL, "%s unknown", cp);
205 if (!(cp = *argp++) || *cp == '-')
206 adios (NULL, "missing argument to %s", argp[-2]);
208 adios (NULL, "too many parts (starting with %s), %d max",
214 if (!(cp = *argp++) || *cp == '-')
215 adios (NULL, "missing argument to %s", argp[-2]);
217 adios (NULL, "too many types (starting with %s), %d max",
223 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
224 adios (NULL, "missing argument to %s", argp[-2]);
225 file = *cp == '-' ? cp : path (cp, TFILE);
229 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
230 adios (NULL, "missing argument to %s", argp[-2]);
231 outfile = *cp == '-' ? cp : path (cp, TFILE);
245 if (*cp == '+' || *cp == '@') {
247 adios (NULL, "only one folder at a time!");
249 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
252 * Check if we need to allocate more space
253 * for message names/ranges/sequences.
255 if (nummsgs >= maxmsgs) {
257 if (!(msgs = (char **) realloc (msgs,
258 (size_t) (maxmsgs * sizeof(*msgs)))))
259 adios (NULL, "unable to reallocate msgs storage");
261 msgs[nummsgs++] = cp;
265 /* null terminate the list of acceptable parts/types */
272 adios (NULL, "must specify output file");
274 /* Check for public cache location */
275 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
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));
284 * Check for storage directory. If specified,
285 * then store temporary files there. Else we
286 * store them in standard nmh directory.
288 if ((cp = context_find (nmhstorage)) && *cp)
289 tmp = concat (cp, "/", invo_name, NULL);
291 tmp = add (m_maildir (invo_name), NULL);
293 if (!context_find ("path"))
294 free (path ("./", TFOLDER));
297 adios (NULL, "cannot specify msg and file at same time!");
300 * check if message is coming from file
303 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
304 adios (NULL, "out of memory");
307 if ((ct = parse_mime (file)));
311 * message(s) are coming from a folder
314 msgs[nummsgs++] = "cur";
316 folder = getfolder (1);
317 maildir = m_maildir (folder);
319 if (chdir (maildir) == NOTOK)
320 adios (maildir, "unable to change directory to");
322 /* read folder and create message structure */
323 if (!(mp = folder_read (folder)))
324 adios (NULL, "unable to read folder %s", folder);
326 /* check for empty folder */
328 adios (NULL, "no messages in %s", folder);
330 /* parse all the message ranges/sequences and set SELECTED */
331 for (msgnum = 0; msgnum < nummsgs; msgnum++)
332 if (!m_convert (mp, msgs[msgnum]))
334 seq_setprev (mp); /* set the previous-sequence */
336 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
337 adios (NULL, "out of memory");
340 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
341 if (is_selected(mp, msgnum)) {
344 msgnam = m_name (msgnum);
345 if ((ct = parse_mime (msgnam)))
355 SIGNAL (SIGQUIT, quitser);
356 SIGNAL (SIGPIPE, pipeser);
359 * Get the associated umask for the relevant contents.
361 for (ctp = cts; *ctp; 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);
369 ct->c_umask = ~m_gmprot();
374 * Write the content to a file
376 write_content (cts, outfile);
378 /* Now free all the structures for the content */
379 for (ctp = cts; *ctp; ctp++)
385 /* If reading from a folder, do some updating */
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 */
398 write_content (CT *cts, char *outfile)
402 for (ctp = cts; *ctp; ctp++) {
404 output_message (ct, outfile);
418 fprintf (stderr, "\n");
437 return 1; /* dead code to satisfy the compiler */