]>
diplodocus.org Git - nmh/blob - uip/mhtest.c
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 #define MHTEST_SWITCHES \
24 X("check", 0, CHECKSW) \
25 X("nocheck", 0, NCHECKSW) \
26 X("verbose", 0, VERBSW) \
27 X("noverbose", 0, NVERBSW) \
28 X("file file", 0, FILESW) \
29 X("outfile file", 0, OUTFILESW) \
30 X("part number", 0, PARTSW) \
31 X("type content", 0, TYPESW) \
32 X("rcache policy", 0, RCACHESW) \
33 X("wcache policy", 0, WCACHESW) \
34 X("version", 0, VERSIONSW) \
35 X("help", 0, HELPSW) \
36 X("debug", -5, DEBUGSW) \
38 #define X(sw, minchars, id) id,
39 DEFINE_SWITCH_ENUM(MHTEST
);
42 #define X(sw, minchars, id) { sw, minchars, id },
43 DEFINE_SWITCH_ARRAY(MHTEST
, switches
);
48 extern char *tmp
; /* directory to place temp files */
53 extern char *cache_public
;
54 extern char *cache_private
;
59 extern char *parts
[NPARTS
+ 1];
60 extern char *types
[NTYPES
+ 1];
64 * This is currently needed to keep mhparse happy.
65 * This needs to be changed.
72 #define quitser pipeser
75 CT
parse_mime (char *);
78 int output_message (CT
, char *);
81 int part_ok (CT
, int);
82 int type_ok (CT
, int);
83 void flush_errors (void);
86 void free_content (CT
);
88 void freects_done (int) NORETURN
;
93 static int write_content (CT
*, char *);
94 static void pipeser (int);
98 main (int argc
, char **argv
)
100 int msgnum
, *icachesw
;
101 char *cp
, *file
= NULL
, *folder
= NULL
;
102 char *maildir
, buf
[100], *outfile
= NULL
;
103 char **argp
, **arguments
;
104 struct msgs_array msgs
= { 0, 0, NULL
};
105 struct msgs
*mp
= NULL
;
111 setlocale(LC_ALL
, "");
113 invo_name
= r1bindex (argv
[0], '/');
115 /* read user profile/context */
118 arguments
= getarguments (invo_name
, argc
, argv
, 1);
124 while ((cp
= *argp
++)) {
126 switch (smatch (++cp
, switches
)) {
128 ambigsw (cp
, switches
);
131 adios (NULL
, "-%s unknown", cp
);
134 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
136 print_help (buf
, switches
, 1);
139 print_version(invo_name
);
143 icachesw
= &rcachesw
;
146 icachesw
= &wcachesw
;
148 if (!(cp
= *argp
++) || *cp
== '-')
149 adios (NULL
, "missing argument to %s", argp
[-2]);
150 switch (*icachesw
= smatch (cp
, caches
)) {
152 ambigsw (cp
, caches
);
155 adios (NULL
, "%s unknown", cp
);
169 if (!(cp
= *argp
++) || *cp
== '-')
170 adios (NULL
, "missing argument to %s", argp
[-2]);
172 adios (NULL
, "too many parts (starting with %s), %d max",
178 if (!(cp
= *argp
++) || *cp
== '-')
179 adios (NULL
, "missing argument to %s", argp
[-2]);
181 adios (NULL
, "too many types (starting with %s), %d max",
187 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
188 adios (NULL
, "missing argument to %s", argp
[-2]);
189 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
193 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
194 adios (NULL
, "missing argument to %s", argp
[-2]);
195 outfile
= *cp
== '-' ? cp
: path (cp
, TFILE
);
209 if (*cp
== '+' || *cp
== '@') {
211 adios (NULL
, "only one folder at a time!");
213 folder
= pluspath (cp
);
215 app_msgarg(&msgs
, cp
);
218 /* null terminate the list of acceptable parts/types */
223 adios (NULL
, "must specify output file");
225 /* Check for public cache location */
226 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
229 /* Check for private cache location */
230 if (!(cache_private
= context_find (nmhprivcache
)))
231 cache_private
= ".cache";
232 cache_private
= getcpy (m_maildir (cache_private
));
235 * Check for storage directory. If specified,
236 * then store temporary files there. Else we
237 * store them in standard nmh directory.
239 if ((cp
= context_find (nmhstorage
)) && *cp
)
240 tmp
= concat (cp
, "/", invo_name
, NULL
);
242 tmp
= add (m_maildir (invo_name
), NULL
);
244 if (!context_find ("path"))
245 free (path ("./", TFOLDER
));
247 if (file
&& msgs
.size
)
248 adios (NULL
, "cannot specify msg and file at same time!");
251 * check if message is coming from file
254 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
255 adios (NULL
, "out of memory");
258 if ((ct
= parse_mime (file
)))
262 * message(s) are coming from a folder
265 app_msgarg(&msgs
, "cur");
267 folder
= getfolder (1);
268 maildir
= m_maildir (folder
);
270 if (chdir (maildir
) == NOTOK
)
271 adios (maildir
, "unable to change directory to");
273 /* read folder and create message structure */
274 if (!(mp
= folder_read (folder
)))
275 adios (NULL
, "unable to read folder %s", folder
);
277 /* check for empty folder */
279 adios (NULL
, "no messages in %s", folder
);
281 /* parse all the message ranges/sequences and set SELECTED */
282 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
283 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
285 seq_setprev (mp
); /* set the previous-sequence */
287 if (!(cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
288 adios (NULL
, "out of memory");
291 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
292 if (is_selected(mp
, msgnum
)) {
295 msgnam
= m_name (msgnum
);
296 if ((ct
= parse_mime (msgnam
)))
306 SIGNAL (SIGQUIT
, quitser
);
307 SIGNAL (SIGPIPE
, pipeser
);
310 * Get the associated umask for the relevant contents.
312 for (ctp
= cts
; *ctp
; ctp
++) {
316 if (type_ok (ct
, 1) && !ct
->c_umask
) {
317 if (stat (ct
->c_file
, &st
) != NOTOK
)
318 ct
->c_umask
= ~(st
.st_mode
& 0777);
320 ct
->c_umask
= ~m_gmprot();
325 * Write the content to a file
327 write_content (cts
, outfile
);
329 /* Now free all the structures for the content */
330 for (ctp
= cts
; *ctp
; ctp
++)
336 /* If reading from a folder, do some updating */
338 context_replace (pfolder
, folder
);/* update current folder */
339 seq_setcur (mp
, mp
->hghsel
); /* update current message */
340 seq_save (mp
); /* synchronize sequences */
341 context_save (); /* save the context file */
350 write_content (CT
*cts
, char *outfile
)
354 for (ctp
= cts
; *ctp
; ctp
++) {
356 output_message (ct
, outfile
);
370 fprintf (stderr
, "\n");