3 * mhtest.c -- test harness for MIME routines
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #include <h/signals.h>
19 #include <h/mhparse.h>
20 #include <h/mhcachesbr.h>
23 static struct swit switches[] = {
35 { "outfile file", 0 },
39 { "type content", 0 },
41 { "rcache policy", 0 },
43 { "wcache policy", 0 },
50 * switches for debugging
58 int ebcdicsw = 0; /* hack for linking purposes */
61 extern char *tmp; /* directory to place temp files */
66 extern char *cache_public;
67 extern char *cache_private;
72 extern char *parts[NPARTS + 1];
73 extern char *types[NTYPES + 1];
77 * This is currently needed to keep mhparse happy.
78 * This needs to be changed.
85 #define quitser pipeser
88 CT parse_mime (char *);
91 int output_message (CT, char *);
94 int part_ok (CT, int);
95 int type_ok (CT, int);
96 void flush_errors (void);
99 void free_content (CT);
101 void freects_done (int) NORETURN;
106 static int write_content (CT *, char *);
107 static void pipeser (int);
111 main (int argc, char **argv)
113 int msgnum, *icachesw;
114 char *cp, *file = NULL, *folder = NULL;
115 char *maildir, buf[100], *outfile = NULL;
116 char **argp, **arguments;
117 struct msgs_array msgs = { 0, 0, NULL };
118 struct msgs *mp = NULL;
124 setlocale(LC_ALL, "");
126 invo_name = r1bindex (argv[0], '/');
128 /* read user profile/context */
131 arguments = getarguments (invo_name, argc, argv, 1);
137 while ((cp = *argp++)) {
139 switch (smatch (++cp, switches)) {
141 ambigsw (cp, switches);
144 adios (NULL, "-%s unknown", cp);
147 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
149 print_help (buf, switches, 1);
152 print_version(invo_name);
156 icachesw = &rcachesw;
159 icachesw = &wcachesw;
161 if (!(cp = *argp++) || *cp == '-')
162 adios (NULL, "missing argument to %s", argp[-2]);
163 switch (*icachesw = smatch (cp, caches)) {
165 ambigsw (cp, caches);
168 adios (NULL, "%s unknown", cp);
182 if (!(cp = *argp++) || *cp == '-')
183 adios (NULL, "missing argument to %s", argp[-2]);
185 adios (NULL, "too many parts (starting with %s), %d max",
191 if (!(cp = *argp++) || *cp == '-')
192 adios (NULL, "missing argument to %s", argp[-2]);
194 adios (NULL, "too many types (starting with %s), %d max",
200 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
201 adios (NULL, "missing argument to %s", argp[-2]);
202 file = *cp == '-' ? cp : path (cp, TFILE);
206 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
207 adios (NULL, "missing argument to %s", argp[-2]);
208 outfile = *cp == '-' ? cp : path (cp, TFILE);
222 if (*cp == '+' || *cp == '@') {
224 adios (NULL, "only one folder at a time!");
226 folder = pluspath (cp);
228 app_msgarg(&msgs, cp);
231 /* null terminate the list of acceptable parts/types */
236 adios (NULL, "must specify output file");
238 /* Check for public cache location */
239 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
242 /* Check for private cache location */
243 if (!(cache_private = context_find (nmhprivcache)))
244 cache_private = ".cache";
245 cache_private = getcpy (m_maildir (cache_private));
248 * Check for storage directory. If specified,
249 * then store temporary files there. Else we
250 * store them in standard nmh directory.
252 if ((cp = context_find (nmhstorage)) && *cp)
253 tmp = concat (cp, "/", invo_name, NULL);
255 tmp = add (m_maildir (invo_name), NULL);
257 if (!context_find ("path"))
258 free (path ("./", TFOLDER));
260 if (file && msgs.size)
261 adios (NULL, "cannot specify msg and file at same time!");
264 * check if message is coming from file
267 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
268 adios (NULL, "out of memory");
271 if ((ct = parse_mime (file)))
275 * message(s) are coming from a folder
278 app_msgarg(&msgs, "cur");
280 folder = getfolder (1);
281 maildir = m_maildir (folder);
283 if (chdir (maildir) == NOTOK)
284 adios (maildir, "unable to change directory to");
286 /* read folder and create message structure */
287 if (!(mp = folder_read (folder)))
288 adios (NULL, "unable to read folder %s", folder);
290 /* check for empty folder */
292 adios (NULL, "no messages in %s", folder);
294 /* parse all the message ranges/sequences and set SELECTED */
295 for (msgnum = 0; msgnum < msgs.size; msgnum++)
296 if (!m_convert (mp, msgs.msgs[msgnum]))
298 seq_setprev (mp); /* set the previous-sequence */
300 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
301 adios (NULL, "out of memory");
304 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
305 if (is_selected(mp, msgnum)) {
308 msgnam = m_name (msgnum);
309 if ((ct = parse_mime (msgnam)))
319 SIGNAL (SIGQUIT, quitser);
320 SIGNAL (SIGPIPE, pipeser);
323 * Get the associated umask for the relevant contents.
325 for (ctp = cts; *ctp; ctp++) {
329 if (type_ok (ct, 1) && !ct->c_umask) {
330 if (stat (ct->c_file, &st) != NOTOK)
331 ct->c_umask = ~(st.st_mode & 0777);
333 ct->c_umask = ~m_gmprot();
338 * Write the content to a file
340 write_content (cts, outfile);
342 /* Now free all the structures for the content */
343 for (ctp = cts; *ctp; ctp++)
349 /* If reading from a folder, do some updating */
351 context_replace (pfolder, folder);/* update current folder */
352 seq_setcur (mp, mp->hghsel); /* update current message */
353 seq_save (mp); /* synchronize sequences */
354 context_save (); /* save the context file */
363 write_content (CT *cts, char *outfile)
367 for (ctp = cts; *ctp; ctp++) {
369 output_message (ct, outfile);
383 fprintf (stderr, "\n");