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>
29 static struct swit switches[] = {
41 { "outfile file", 0 },
45 { "type content", 0 },
47 { "rcache policy", 0 },
49 { "wcache policy", 0 },
56 * switches for debugging
64 int ebcdicsw = 0; /* hack for linking purposes */
68 extern char *tmp; /* directory to place temp files */
73 extern char *cache_public;
74 extern char *cache_private;
79 extern char *parts[NPARTS + 1];
80 extern char *types[NTYPES + 1];
84 * This is currently needed to keep mhparse happy.
85 * This needs to be changed.
92 #define quitser pipeser
95 CT parse_mime (char *);
98 int output_message (CT, char *);
101 int part_ok (CT, int);
102 int type_ok (CT, int);
103 void set_endian (void);
104 void flush_errors (void);
107 void free_content (CT);
109 void freects_done (int) NORETURN;
114 static int write_content (CT *, char *);
115 static RETSIGTYPE pipeser (int);
119 main (int argc, char **argv)
121 int msgnum, *icachesw;
122 char *cp, *file = NULL, *folder = NULL;
123 char *maildir, buf[100], *outfile = NULL;
124 char **argp, **arguments;
125 struct msgs_array msgs = { 0, 0, NULL };
126 struct msgs *mp = NULL;
132 setlocale(LC_ALL, "");
134 invo_name = r1bindex (argv[0], '/');
136 /* read user profile/context */
139 arguments = getarguments (invo_name, argc, argv, 1);
145 while ((cp = *argp++)) {
147 switch (smatch (++cp, switches)) {
149 ambigsw (cp, switches);
152 adios (NULL, "-%s unknown", cp);
155 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
157 print_help (buf, switches, 1);
160 print_version(invo_name);
164 icachesw = &rcachesw;
167 icachesw = &wcachesw;
169 if (!(cp = *argp++) || *cp == '-')
170 adios (NULL, "missing argument to %s", argp[-2]);
171 switch (*icachesw = smatch (cp, caches)) {
173 ambigsw (cp, caches);
176 adios (NULL, "%s unknown", cp);
190 if (!(cp = *argp++) || *cp == '-')
191 adios (NULL, "missing argument to %s", argp[-2]);
193 adios (NULL, "too many parts (starting with %s), %d max",
199 if (!(cp = *argp++) || *cp == '-')
200 adios (NULL, "missing argument to %s", argp[-2]);
202 adios (NULL, "too many types (starting with %s), %d max",
208 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
209 adios (NULL, "missing argument to %s", argp[-2]);
210 file = *cp == '-' ? cp : path (cp, TFILE);
214 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
215 adios (NULL, "missing argument to %s", argp[-2]);
216 outfile = *cp == '-' ? cp : path (cp, TFILE);
230 if (*cp == '+' || *cp == '@') {
232 adios (NULL, "only one folder at a time!");
234 folder = pluspath (cp);
236 app_msgarg(&msgs, cp);
239 /* null terminate the list of acceptable parts/types */
246 adios (NULL, "must specify output file");
248 /* Check for public cache location */
249 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
252 /* Check for private cache location */
253 if (!(cache_private = context_find (nmhprivcache)))
254 cache_private = ".cache";
255 cache_private = getcpy (m_maildir (cache_private));
258 * Check for storage directory. If specified,
259 * then store temporary files there. Else we
260 * store them in standard nmh directory.
262 if ((cp = context_find (nmhstorage)) && *cp)
263 tmp = concat (cp, "/", invo_name, NULL);
265 tmp = add (m_maildir (invo_name), NULL);
267 if (!context_find ("path"))
268 free (path ("./", TFOLDER));
270 if (file && msgs.size)
271 adios (NULL, "cannot specify msg and file at same time!");
274 * check if message is coming from file
277 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
278 adios (NULL, "out of memory");
281 if ((ct = parse_mime (file)));
285 * message(s) are coming from a folder
288 app_msgarg(&msgs, "cur");
290 folder = getfolder (1);
291 maildir = m_maildir (folder);
293 if (chdir (maildir) == NOTOK)
294 adios (maildir, "unable to change directory to");
296 /* read folder and create message structure */
297 if (!(mp = folder_read (folder)))
298 adios (NULL, "unable to read folder %s", folder);
300 /* check for empty folder */
302 adios (NULL, "no messages in %s", folder);
304 /* parse all the message ranges/sequences and set SELECTED */
305 for (msgnum = 0; msgnum < msgs.size; msgnum++)
306 if (!m_convert (mp, msgs.msgs[msgnum]))
308 seq_setprev (mp); /* set the previous-sequence */
310 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
311 adios (NULL, "out of memory");
314 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
315 if (is_selected(mp, msgnum)) {
318 msgnam = m_name (msgnum);
319 if ((ct = parse_mime (msgnam)))
329 SIGNAL (SIGQUIT, quitser);
330 SIGNAL (SIGPIPE, pipeser);
333 * Get the associated umask for the relevant contents.
335 for (ctp = cts; *ctp; ctp++) {
339 if (type_ok (ct, 1) && !ct->c_umask) {
340 if (stat (ct->c_file, &st) != NOTOK)
341 ct->c_umask = ~(st.st_mode & 0777);
343 ct->c_umask = ~m_gmprot();
348 * Write the content to a file
350 write_content (cts, outfile);
352 /* Now free all the structures for the content */
353 for (ctp = cts; *ctp; ctp++)
359 /* If reading from a folder, do some updating */
361 context_replace (pfolder, folder);/* update current folder */
362 seq_setcur (mp, mp->hghsel); /* update current message */
363 seq_save (mp); /* synchronize sequences */
364 context_save (); /* save the context file */
373 write_content (CT *cts, char *outfile)
377 for (ctp = cts; *ctp; ctp++) {
379 output_message (ct, outfile);
393 fprintf (stderr, "\n");