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 /* The list of top-level contents to display */
95 #define quitser pipeser
98 CT parse_mime (char *);
101 int output_message (CT, char *);
104 int part_ok (CT, int);
105 int type_ok (CT, int);
106 void set_endian (void);
107 void flush_errors (void);
110 void free_content (CT);
115 static int write_content (CT *, char *);
116 static RETSIGTYPE pipeser (int);
117 static int freectp_done (int);
121 main (int argc, char **argv)
123 int msgnum, *icachesw;
124 char *cp, *file = NULL, *folder = NULL;
125 char *maildir, buf[100], *outfile = NULL;
126 char **argp, **arguments;
127 struct msgs_array msgs = { 0, 0, NULL };
128 struct msgs *mp = NULL;
134 setlocale(LC_ALL, "");
136 invo_name = r1bindex (argv[0], '/');
138 /* read user profile/context */
141 arguments = getarguments (invo_name, argc, argv, 1);
147 while ((cp = *argp++)) {
149 switch (smatch (++cp, switches)) {
151 ambigsw (cp, switches);
154 adios (NULL, "-%s unknown", cp);
157 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
159 print_help (buf, switches, 1);
162 print_version(invo_name);
166 icachesw = &rcachesw;
169 icachesw = &wcachesw;
171 if (!(cp = *argp++) || *cp == '-')
172 adios (NULL, "missing argument to %s", argp[-2]);
173 switch (*icachesw = smatch (cp, caches)) {
175 ambigsw (cp, caches);
178 adios (NULL, "%s unknown", cp);
192 if (!(cp = *argp++) || *cp == '-')
193 adios (NULL, "missing argument to %s", argp[-2]);
195 adios (NULL, "too many parts (starting with %s), %d max",
201 if (!(cp = *argp++) || *cp == '-')
202 adios (NULL, "missing argument to %s", argp[-2]);
204 adios (NULL, "too many types (starting with %s), %d max",
210 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
211 adios (NULL, "missing argument to %s", argp[-2]);
212 file = *cp == '-' ? cp : path (cp, TFILE);
216 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
217 adios (NULL, "missing argument to %s", argp[-2]);
218 outfile = *cp == '-' ? cp : path (cp, TFILE);
232 if (*cp == '+' || *cp == '@') {
234 adios (NULL, "only one folder at a time!");
236 folder = pluspath (cp);
238 app_msgarg(&msgs, cp);
241 /* null terminate the list of acceptable parts/types */
248 adios (NULL, "must specify output file");
250 /* Check for public cache location */
251 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
254 /* Check for private cache location */
255 if (!(cache_private = context_find (nmhprivcache)))
256 cache_private = ".cache";
257 cache_private = getcpy (m_maildir (cache_private));
260 * Check for storage directory. If specified,
261 * then store temporary files there. Else we
262 * store them in standard nmh directory.
264 if ((cp = context_find (nmhstorage)) && *cp)
265 tmp = concat (cp, "/", invo_name, NULL);
267 tmp = add (m_maildir (invo_name), NULL);
269 if (!context_find ("path"))
270 free (path ("./", TFOLDER));
272 if (file && msgs.size)
273 adios (NULL, "cannot specify msg and file at same time!");
276 * check if message is coming from file
279 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
280 adios (NULL, "out of memory");
283 if ((ct = parse_mime (file)));
287 * message(s) are coming from a folder
290 app_msgarg(&msgs, "cur");
292 folder = getfolder (1);
293 maildir = m_maildir (folder);
295 if (chdir (maildir) == NOTOK)
296 adios (maildir, "unable to change directory to");
298 /* read folder and create message structure */
299 if (!(mp = folder_read (folder)))
300 adios (NULL, "unable to read folder %s", folder);
302 /* check for empty folder */
304 adios (NULL, "no messages in %s", folder);
306 /* parse all the message ranges/sequences and set SELECTED */
307 for (msgnum = 0; msgnum < msgs.size; msgnum++)
308 if (!m_convert (mp, msgs.msgs[msgnum]))
310 seq_setprev (mp); /* set the previous-sequence */
312 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
313 adios (NULL, "out of memory");
316 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
317 if (is_selected(mp, msgnum)) {
320 msgnam = m_name (msgnum);
321 if ((ct = parse_mime (msgnam)))
331 SIGNAL (SIGQUIT, quitser);
332 SIGNAL (SIGPIPE, pipeser);
335 * Get the associated umask for the relevant contents.
337 for (ctp = cts; *ctp; ctp++) {
341 if (type_ok (ct, 1) && !ct->c_umask) {
342 if (stat (ct->c_file, &st) != NOTOK)
343 ct->c_umask = ~(st.st_mode & 0777);
345 ct->c_umask = ~m_gmprot();
350 * Write the content to a file
352 write_content (cts, outfile);
354 /* Now free all the structures for the content */
355 for (ctp = cts; *ctp; ctp++)
361 /* If reading from a folder, do some updating */
363 context_replace (pfolder, folder);/* update current folder */
364 seq_setcur (mp, mp->hghsel); /* update current message */
365 seq_save (mp); /* synchronize sequences */
366 context_save (); /* save the context file */
374 write_content (CT *cts, char *outfile)
378 for (ctp = cts; *ctp; ctp++) {
380 output_message (ct, outfile);
394 fprintf (stderr, "\n");
404 freectp_done (int status)
413 return 1; /* dead code to satisfy the compiler */