]>
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>
17 #include <h/mhparse.h>
18 #include <h/mhcachesbr.h>
21 #define MHTEST_SWITCHES \
22 X("check", 0, CHECKSW) \
23 X("nocheck", 0, NCHECKSW) \
24 X("verbose", 0, VERBSW) \
25 X("noverbose", 0, NVERBSW) \
26 X("file file", 0, FILESW) \
27 X("outfile file", 0, OUTFILESW) \
28 X("part number", 0, PARTSW) \
29 X("type content", 0, TYPESW) \
30 X("rcache policy", 0, RCACHESW) \
31 X("wcache policy", 0, WCACHESW) \
32 X("version", 0, VERSIONSW) \
33 X("help", 0, HELPSW) \
34 X("debug", -5, DEBUGSW) \
36 #define X(sw, minchars, id) id,
37 DEFINE_SWITCH_ENUM(MHTEST
);
40 #define X(sw, minchars, id) { sw, minchars, id },
41 DEFINE_SWITCH_ARRAY(MHTEST
, switches
);
48 extern char *cache_public
;
49 extern char *cache_private
;
54 extern char *parts
[NPARTS
+ 1];
55 extern char *types
[NTYPES
+ 1];
59 * This is currently needed to keep mhparse happy.
60 * This needs to be changed.
65 #define quitser pipeser
68 CT
parse_mime (char *);
71 int output_message (CT
, char *);
74 int part_ok (CT
, int);
75 int type_ok (CT
, int);
76 void flush_errors (void);
80 void freects_done (int) NORETURN
;
85 static int write_content (CT
*, char *);
86 static void pipeser (int);
90 main (int argc
, char **argv
)
92 int msgnum
, *icachesw
;
93 char *cp
, *file
= NULL
, *folder
= NULL
;
94 char *maildir
, buf
[100], *outfile
= NULL
;
95 char **argp
, **arguments
;
96 struct msgs_array msgs
= { 0, 0, NULL
};
97 struct msgs
*mp
= NULL
;
100 if (nmh_init(argv
[0], 1)) { return 1; }
104 arguments
= getarguments (invo_name
, argc
, argv
, 1);
110 while ((cp
= *argp
++)) {
112 switch (smatch (++cp
, switches
)) {
114 ambigsw (cp
, switches
);
117 adios (NULL
, "-%s unknown", cp
);
120 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
122 print_help (buf
, switches
, 1);
125 print_version(invo_name
);
129 icachesw
= &rcachesw
;
132 icachesw
= &wcachesw
;
134 if (!(cp
= *argp
++) || *cp
== '-')
135 adios (NULL
, "missing argument to %s", argp
[-2]);
136 switch (*icachesw
= smatch (cp
, caches
)) {
138 ambigsw (cp
, caches
);
141 adios (NULL
, "%s unknown", cp
);
155 if (!(cp
= *argp
++) || *cp
== '-')
156 adios (NULL
, "missing argument to %s", argp
[-2]);
158 adios (NULL
, "too many parts (starting with %s), %d max",
164 if (!(cp
= *argp
++) || *cp
== '-')
165 adios (NULL
, "missing argument to %s", argp
[-2]);
167 adios (NULL
, "too many types (starting with %s), %d max",
173 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
174 adios (NULL
, "missing argument to %s", argp
[-2]);
175 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
179 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
180 adios (NULL
, "missing argument to %s", argp
[-2]);
181 outfile
= *cp
== '-' ? cp
: path (cp
, TFILE
);
195 if (*cp
== '+' || *cp
== '@') {
197 adios (NULL
, "only one folder at a time!");
199 folder
= pluspath (cp
);
201 app_msgarg(&msgs
, cp
);
204 /* null terminate the list of acceptable parts/types */
209 adios (NULL
, "must specify output file");
211 /* Check for public cache location */
212 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
215 /* Check for private cache location */
216 if (!(cache_private
= context_find (nmhprivcache
)))
217 cache_private
= ".cache";
218 cache_private
= getcpy (m_maildir (cache_private
));
220 if (!context_find ("path"))
221 free (path ("./", TFOLDER
));
223 if (file
&& msgs
.size
)
224 adios (NULL
, "cannot specify msg and file at same time!");
227 * check if message is coming from file
230 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
231 adios (NULL
, "out of memory");
234 if ((ct
= parse_mime (file
)))
238 * message(s) are coming from a folder
241 app_msgarg(&msgs
, "cur");
243 folder
= getfolder (1);
244 maildir
= m_maildir (folder
);
246 if (chdir (maildir
) == NOTOK
)
247 adios (maildir
, "unable to change directory to");
249 /* read folder and create message structure */
250 if (!(mp
= folder_read (folder
, 1)))
251 adios (NULL
, "unable to read folder %s", folder
);
253 /* check for empty folder */
255 adios (NULL
, "no messages in %s", folder
);
257 /* parse all the message ranges/sequences and set SELECTED */
258 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
259 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
261 seq_setprev (mp
); /* set the previous-sequence */
263 if (!(cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
264 adios (NULL
, "out of memory");
267 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
268 if (is_selected(mp
, msgnum
)) {
271 msgnam
= m_name (msgnum
);
272 if ((ct
= parse_mime (msgnam
)))
282 SIGNAL (SIGQUIT
, quitser
);
283 SIGNAL (SIGPIPE
, pipeser
);
286 * Get the associated umask for the relevant contents.
288 for (ctp
= cts
; *ctp
; ctp
++) {
292 if (type_ok (ct
, 1) && !ct
->c_umask
) {
293 if (stat (ct
->c_file
, &st
) != NOTOK
)
294 ct
->c_umask
= ~(st
.st_mode
& 0777);
296 ct
->c_umask
= ~m_gmprot();
301 * Write the content to a file
303 write_content (cts
, outfile
);
305 /* Now free all the structures for the content */
306 for (ctp
= cts
; *ctp
; ctp
++)
312 /* If reading from a folder, do some updating */
314 context_replace (pfolder
, folder
);/* update current folder */
315 seq_setcur (mp
, mp
->hghsel
); /* update current message */
316 seq_save (mp
); /* synchronize sequences */
317 context_save (); /* save the context file */
326 write_content (CT
*cts
, char *outfile
)
330 for (ctp
= cts
; *ctp
; ctp
++) {
332 output_message (ct
, outfile
);
345 fprintf (stderr
, "\n");