]>
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.
67 #define quitser pipeser
70 CT
parse_mime (char *);
73 int output_message (CT
, char *);
76 int part_ok (CT
, int);
77 int type_ok (CT
, int);
78 void flush_errors (void);
82 void freects_done (int) NORETURN
;
87 static int write_content (CT
*, char *);
88 static void pipeser (int);
92 main (int argc
, char **argv
)
94 int msgnum
, *icachesw
;
95 char *cp
, *file
= NULL
, *folder
= NULL
;
96 char *maildir
, buf
[100], *outfile
= NULL
;
97 char **argp
, **arguments
;
98 struct msgs_array msgs
= { 0, 0, NULL
};
99 struct msgs
*mp
= NULL
;
102 if (nmh_init(argv
[0], 1)) { return 1; }
106 arguments
= getarguments (invo_name
, argc
, argv
, 1);
112 while ((cp
= *argp
++)) {
114 switch (smatch (++cp
, switches
)) {
116 ambigsw (cp
, switches
);
119 adios (NULL
, "-%s unknown", cp
);
122 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
124 print_help (buf
, switches
, 1);
127 print_version(invo_name
);
131 icachesw
= &rcachesw
;
134 icachesw
= &wcachesw
;
136 if (!(cp
= *argp
++) || *cp
== '-')
137 adios (NULL
, "missing argument to %s", argp
[-2]);
138 switch (*icachesw
= smatch (cp
, caches
)) {
140 ambigsw (cp
, caches
);
143 adios (NULL
, "%s unknown", cp
);
157 if (!(cp
= *argp
++) || *cp
== '-')
158 adios (NULL
, "missing argument to %s", argp
[-2]);
160 adios (NULL
, "too many parts (starting with %s), %d max",
166 if (!(cp
= *argp
++) || *cp
== '-')
167 adios (NULL
, "missing argument to %s", argp
[-2]);
169 adios (NULL
, "too many types (starting with %s), %d max",
175 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
176 adios (NULL
, "missing argument to %s", argp
[-2]);
177 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
181 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
182 adios (NULL
, "missing argument to %s", argp
[-2]);
183 outfile
= *cp
== '-' ? cp
: path (cp
, TFILE
);
197 if (*cp
== '+' || *cp
== '@') {
199 adios (NULL
, "only one folder at a time!");
201 folder
= pluspath (cp
);
203 app_msgarg(&msgs
, cp
);
206 /* null terminate the list of acceptable parts/types */
211 adios (NULL
, "must specify output file");
213 /* Check for public cache location */
214 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
217 /* Check for private cache location */
218 if (!(cache_private
= context_find (nmhprivcache
)))
219 cache_private
= ".cache";
220 cache_private
= getcpy (m_maildir (cache_private
));
222 if (!context_find ("path"))
223 free (path ("./", TFOLDER
));
225 if (file
&& msgs
.size
)
226 adios (NULL
, "cannot specify msg and file at same time!");
229 * check if message is coming from file
232 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
233 adios (NULL
, "out of memory");
236 if ((ct
= parse_mime (file
)))
240 * message(s) are coming from a folder
243 app_msgarg(&msgs
, "cur");
245 folder
= getfolder (1);
246 maildir
= m_maildir (folder
);
248 if (chdir (maildir
) == NOTOK
)
249 adios (maildir
, "unable to change directory to");
251 /* read folder and create message structure */
252 if (!(mp
= folder_read (folder
, 1)))
253 adios (NULL
, "unable to read folder %s", folder
);
255 /* check for empty folder */
257 adios (NULL
, "no messages in %s", folder
);
259 /* parse all the message ranges/sequences and set SELECTED */
260 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
261 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
263 seq_setprev (mp
); /* set the previous-sequence */
265 if (!(cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
266 adios (NULL
, "out of memory");
269 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
270 if (is_selected(mp
, msgnum
)) {
273 msgnam
= m_name (msgnum
);
274 if ((ct
= parse_mime (msgnam
)))
284 SIGNAL (SIGQUIT
, quitser
);
285 SIGNAL (SIGPIPE
, pipeser
);
288 * Get the associated umask for the relevant contents.
290 for (ctp
= cts
; *ctp
; ctp
++) {
294 if (type_ok (ct
, 1) && !ct
->c_umask
) {
295 if (stat (ct
->c_file
, &st
) != NOTOK
)
296 ct
->c_umask
= ~(st
.st_mode
& 0777);
298 ct
->c_umask
= ~m_gmprot();
303 * Write the content to a file
305 write_content (cts
, outfile
);
307 /* Now free all the structures for the content */
308 for (ctp
= cts
; *ctp
; ctp
++)
314 /* If reading from a folder, do some updating */
316 context_replace (pfolder
, folder
);/* update current folder */
317 seq_setcur (mp
, mp
->hghsel
); /* update current message */
318 seq_save (mp
); /* synchronize sequences */
319 context_save (); /* save the context file */
328 write_content (CT
*cts
, char *outfile
)
332 for (ctp
= cts
; *ctp
; ctp
++) {
334 output_message (ct
, outfile
);
347 fprintf (stderr
, "\n");