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 set_endian (void);
97 void flush_errors (void);
100 void free_content (CT);
102 void freects_done (int) NORETURN;
107 static int write_content (CT *, char *);
108 static void pipeser (int);
112 main (int argc, char **argv)
114 int msgnum, *icachesw;
115 char *cp, *file = NULL, *folder = NULL;
116 char *maildir, buf[100], *outfile = NULL;
117 char **argp, **arguments;
118 struct msgs_array msgs = { 0, 0, NULL };
119 struct msgs *mp = NULL;
125 setlocale(LC_ALL, "");
127 invo_name = r1bindex (argv[0], '/');
129 /* read user profile/context */
132 arguments = getarguments (invo_name, argc, argv, 1);
138 while ((cp = *argp++)) {
140 switch (smatch (++cp, switches)) {
142 ambigsw (cp, switches);
145 adios (NULL, "-%s unknown", cp);
148 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
150 print_help (buf, switches, 1);
153 print_version(invo_name);
157 icachesw = &rcachesw;
160 icachesw = &wcachesw;
162 if (!(cp = *argp++) || *cp == '-')
163 adios (NULL, "missing argument to %s", argp[-2]);
164 switch (*icachesw = smatch (cp, caches)) {
166 ambigsw (cp, caches);
169 adios (NULL, "%s unknown", cp);
183 if (!(cp = *argp++) || *cp == '-')
184 adios (NULL, "missing argument to %s", argp[-2]);
186 adios (NULL, "too many parts (starting with %s), %d max",
192 if (!(cp = *argp++) || *cp == '-')
193 adios (NULL, "missing argument to %s", argp[-2]);
195 adios (NULL, "too many types (starting with %s), %d max",
201 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
202 adios (NULL, "missing argument to %s", argp[-2]);
203 file = *cp == '-' ? cp : path (cp, TFILE);
207 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
208 adios (NULL, "missing argument to %s", argp[-2]);
209 outfile = *cp == '-' ? cp : path (cp, TFILE);
223 if (*cp == '+' || *cp == '@') {
225 adios (NULL, "only one folder at a time!");
227 folder = pluspath (cp);
229 app_msgarg(&msgs, cp);
232 /* null terminate the list of acceptable parts/types */
239 adios (NULL, "must specify output file");
241 /* Check for public cache location */
242 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
245 /* Check for private cache location */
246 if (!(cache_private = context_find (nmhprivcache)))
247 cache_private = ".cache";
248 cache_private = getcpy (m_maildir (cache_private));
251 * Check for storage directory. If specified,
252 * then store temporary files there. Else we
253 * store them in standard nmh directory.
255 if ((cp = context_find (nmhstorage)) && *cp)
256 tmp = concat (cp, "/", invo_name, NULL);
258 tmp = add (m_maildir (invo_name), NULL);
260 if (!context_find ("path"))
261 free (path ("./", TFOLDER));
263 if (file && msgs.size)
264 adios (NULL, "cannot specify msg and file at same time!");
267 * check if message is coming from file
270 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
271 adios (NULL, "out of memory");
274 if ((ct = parse_mime (file)))
278 * message(s) are coming from a folder
281 app_msgarg(&msgs, "cur");
283 folder = getfolder (1);
284 maildir = m_maildir (folder);
286 if (chdir (maildir) == NOTOK)
287 adios (maildir, "unable to change directory to");
289 /* read folder and create message structure */
290 if (!(mp = folder_read (folder)))
291 adios (NULL, "unable to read folder %s", folder);
293 /* check for empty folder */
295 adios (NULL, "no messages in %s", folder);
297 /* parse all the message ranges/sequences and set SELECTED */
298 for (msgnum = 0; msgnum < msgs.size; msgnum++)
299 if (!m_convert (mp, msgs.msgs[msgnum]))
301 seq_setprev (mp); /* set the previous-sequence */
303 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
304 adios (NULL, "out of memory");
307 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
308 if (is_selected(mp, msgnum)) {
311 msgnam = m_name (msgnum);
312 if ((ct = parse_mime (msgnam)))
322 SIGNAL (SIGQUIT, quitser);
323 SIGNAL (SIGPIPE, pipeser);
326 * Get the associated umask for the relevant contents.
328 for (ctp = cts; *ctp; ctp++) {
332 if (type_ok (ct, 1) && !ct->c_umask) {
333 if (stat (ct->c_file, &st) != NOTOK)
334 ct->c_umask = ~(st.st_mode & 0777);
336 ct->c_umask = ~m_gmprot();
341 * Write the content to a file
343 write_content (cts, outfile);
345 /* Now free all the structures for the content */
346 for (ctp = cts; *ctp; ctp++)
352 /* If reading from a folder, do some updating */
354 context_replace (pfolder, folder);/* update current folder */
355 seq_setcur (mp, mp->hghsel); /* update current message */
356 seq_save (mp); /* synchronize sequences */
357 context_save (); /* save the context file */
366 write_content (CT *cts, char *outfile)
370 for (ctp = cts; *ctp; ctp++) {
372 output_message (ct, outfile);
386 fprintf (stderr, "\n");