2 * mhtest.c -- test harness for MIME routines
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
11 #include <h/signals.h>
18 #include <h/mhparse.h>
19 #include <h/mhcachesbr.h>
22 #ifdef HAVE_SYS_WAIT_H
23 # include <sys/wait.h>
26 static struct swit switches[] = {
38 { "outfile file", 0 },
42 { "type content", 0 },
44 { "rcache policy", 0 },
46 { "wcache policy", 0 },
53 * switches for debugging
61 int ebcdicsw = 0; /* hack for linking purposes */
64 extern char *tmp; /* directory to place temp files */
69 extern char *cache_public;
70 extern char *cache_private;
75 extern char *parts[NPARTS + 1];
76 extern char *types[NTYPES + 1];
80 * This is currently needed to keep mhparse happy.
81 * This needs to be changed.
88 #define quitser pipeser
91 CT parse_mime (char *);
94 int output_message (CT, char *);
97 int part_ok (CT, int);
98 int type_ok (CT, int);
99 void set_endian (void);
100 void flush_errors (void);
103 void free_content (CT);
105 void freects_done (int) NORETURN;
110 static int write_content (CT *, char *);
111 static RETSIGTYPE pipeser (int);
115 main (int argc, char **argv)
117 int msgnum, *icachesw;
118 char *cp, *file = NULL, *folder = NULL;
119 char *maildir, buf[100], *outfile = NULL;
120 char **argp, **arguments;
121 struct msgs_array msgs = { 0, 0, NULL };
122 struct msgs *mp = NULL;
128 setlocale(LC_ALL, "");
130 invo_name = r1bindex (argv[0], '/');
132 /* read user profile/context */
135 arguments = getarguments (invo_name, argc, argv, 1);
141 while ((cp = *argp++)) {
143 switch (smatch (++cp, switches)) {
145 ambigsw (cp, switches);
148 adios (NULL, "-%s unknown", cp);
151 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
153 print_help (buf, switches, 1);
156 print_version(invo_name);
160 icachesw = &rcachesw;
163 icachesw = &wcachesw;
165 if (!(cp = *argp++) || *cp == '-')
166 adios (NULL, "missing argument to %s", argp[-2]);
167 switch (*icachesw = smatch (cp, caches)) {
169 ambigsw (cp, caches);
172 adios (NULL, "%s unknown", cp);
186 if (!(cp = *argp++) || *cp == '-')
187 adios (NULL, "missing argument to %s", argp[-2]);
189 adios (NULL, "too many parts (starting with %s), %d max",
195 if (!(cp = *argp++) || *cp == '-')
196 adios (NULL, "missing argument to %s", argp[-2]);
198 adios (NULL, "too many types (starting with %s), %d max",
204 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
205 adios (NULL, "missing argument to %s", argp[-2]);
206 file = *cp == '-' ? cp : path (cp, TFILE);
210 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
211 adios (NULL, "missing argument to %s", argp[-2]);
212 outfile = *cp == '-' ? cp : path (cp, TFILE);
226 if (*cp == '+' || *cp == '@') {
228 adios (NULL, "only one folder at a time!");
230 folder = pluspath (cp);
232 app_msgarg(&msgs, cp);
235 /* null terminate the list of acceptable parts/types */
242 adios (NULL, "must specify output file");
244 /* Check for public cache location */
245 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
248 /* Check for private cache location */
249 if (!(cache_private = context_find (nmhprivcache)))
250 cache_private = ".cache";
251 cache_private = getcpy (m_maildir (cache_private));
254 * Check for storage directory. If specified,
255 * then store temporary files there. Else we
256 * store them in standard nmh directory.
258 if ((cp = context_find (nmhstorage)) && *cp)
259 tmp = concat (cp, "/", invo_name, NULL);
261 tmp = add (m_maildir (invo_name), NULL);
263 if (!context_find ("path"))
264 free (path ("./", TFOLDER));
266 if (file && msgs.size)
267 adios (NULL, "cannot specify msg and file at same time!");
270 * check if message is coming from file
273 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
274 adios (NULL, "out of memory");
277 if ((ct = parse_mime (file)));
281 * message(s) are coming from a folder
284 app_msgarg(&msgs, "cur");
286 folder = getfolder (1);
287 maildir = m_maildir (folder);
289 if (chdir (maildir) == NOTOK)
290 adios (maildir, "unable to change directory to");
292 /* read folder and create message structure */
293 if (!(mp = folder_read (folder)))
294 adios (NULL, "unable to read folder %s", folder);
296 /* check for empty folder */
298 adios (NULL, "no messages in %s", folder);
300 /* parse all the message ranges/sequences and set SELECTED */
301 for (msgnum = 0; msgnum < msgs.size; msgnum++)
302 if (!m_convert (mp, msgs.msgs[msgnum]))
304 seq_setprev (mp); /* set the previous-sequence */
306 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
307 adios (NULL, "out of memory");
310 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
311 if (is_selected(mp, msgnum)) {
314 msgnam = m_name (msgnum);
315 if ((ct = parse_mime (msgnam)))
325 SIGNAL (SIGQUIT, quitser);
326 SIGNAL (SIGPIPE, pipeser);
329 * Get the associated umask for the relevant contents.
331 for (ctp = cts; *ctp; ctp++) {
335 if (type_ok (ct, 1) && !ct->c_umask) {
336 if (stat (ct->c_file, &st) != NOTOK)
337 ct->c_umask = ~(st.st_mode & 0777);
339 ct->c_umask = ~m_gmprot();
344 * Write the content to a file
346 write_content (cts, outfile);
348 /* Now free all the structures for the content */
349 for (ctp = cts; *ctp; ctp++)
355 /* If reading from a folder, do some updating */
357 context_replace (pfolder, folder); /* update current folder */
358 seq_setcur (mp, mp->hghsel); /* update current message */
359 seq_save (mp); /* synchronize sequences */
360 context_save (); /* save the context file */
369 write_content (CT *cts, char *outfile)
373 for (ctp = cts; *ctp; ctp++) {
375 output_message (ct, outfile);
389 fprintf (stderr, "\n");