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 */
238 adios (NULL, "must specify output file");
240 /* Check for public cache location */
241 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
244 /* Check for private cache location */
245 if (!(cache_private = context_find (nmhprivcache)))
246 cache_private = ".cache";
247 cache_private = getcpy (m_maildir (cache_private));
250 * Check for storage directory. If specified,
251 * then store temporary files there. Else we
252 * store them in standard nmh directory.
254 if ((cp = context_find (nmhstorage)) && *cp)
255 tmp = concat (cp, "/", invo_name, NULL);
257 tmp = add (m_maildir (invo_name), NULL);
259 if (!context_find ("path"))
260 free (path ("./", TFOLDER));
262 if (file && msgs.size)
263 adios (NULL, "cannot specify msg and file at same time!");
266 * check if message is coming from file
269 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
270 adios (NULL, "out of memory");
273 if ((ct = parse_mime (file)))
277 * message(s) are coming from a folder
280 app_msgarg(&msgs, "cur");
282 folder = getfolder (1);
283 maildir = m_maildir (folder);
285 if (chdir (maildir) == NOTOK)
286 adios (maildir, "unable to change directory to");
288 /* read folder and create message structure */
289 if (!(mp = folder_read (folder)))
290 adios (NULL, "unable to read folder %s", folder);
292 /* check for empty folder */
294 adios (NULL, "no messages in %s", folder);
296 /* parse all the message ranges/sequences and set SELECTED */
297 for (msgnum = 0; msgnum < msgs.size; msgnum++)
298 if (!m_convert (mp, msgs.msgs[msgnum]))
300 seq_setprev (mp); /* set the previous-sequence */
302 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
303 adios (NULL, "out of memory");
306 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
307 if (is_selected(mp, msgnum)) {
310 msgnam = m_name (msgnum);
311 if ((ct = parse_mime (msgnam)))
321 SIGNAL (SIGQUIT, quitser);
322 SIGNAL (SIGPIPE, pipeser);
325 * Get the associated umask for the relevant contents.
327 for (ctp = cts; *ctp; ctp++) {
331 if (type_ok (ct, 1) && !ct->c_umask) {
332 if (stat (ct->c_file, &st) != NOTOK)
333 ct->c_umask = ~(st.st_mode & 0777);
335 ct->c_umask = ~m_gmprot();
340 * Write the content to a file
342 write_content (cts, outfile);
344 /* Now free all the structures for the content */
345 for (ctp = cts; *ctp; ctp++)
351 /* If reading from a folder, do some updating */
353 context_replace (pfolder, folder);/* update current folder */
354 seq_setcur (mp, mp->hghsel); /* update current message */
355 seq_save (mp); /* synchronize sequences */
356 context_save (); /* save the context file */
365 write_content (CT *cts, char *outfile)
369 for (ctp = cts; *ctp; ctp++) {
371 output_message (ct, outfile);
385 fprintf (stderr, "\n");