]>
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
);
46 extern char *tmp
; /* directory to place temp files */
51 extern char *cache_public
;
52 extern char *cache_private
;
57 extern char *parts
[NPARTS
+ 1];
58 extern char *types
[NTYPES
+ 1];
62 * This is currently needed to keep mhparse happy.
63 * This needs to be changed.
70 #define quitser pipeser
73 CT
parse_mime (char *);
76 int output_message (CT
, char *);
79 int part_ok (CT
, int);
80 int type_ok (CT
, int);
81 void flush_errors (void);
85 void freects_done (int) NORETURN
;
90 static int write_content (CT
*, char *);
91 static void pipeser (int);
95 main (int argc
, char **argv
)
97 int msgnum
, *icachesw
;
98 char *cp
, *file
= NULL
, *folder
= NULL
;
99 char *maildir
, buf
[100], *outfile
= NULL
;
100 char **argp
, **arguments
;
101 struct msgs_array msgs
= { 0, 0, NULL
};
102 struct msgs
*mp
= NULL
;
108 setlocale(LC_ALL
, "");
110 invo_name
= r1bindex (argv
[0], '/');
112 /* read user profile/context */
115 arguments
= getarguments (invo_name
, argc
, argv
, 1);
121 while ((cp
= *argp
++)) {
123 switch (smatch (++cp
, switches
)) {
125 ambigsw (cp
, switches
);
128 adios (NULL
, "-%s unknown", cp
);
131 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
133 print_help (buf
, switches
, 1);
136 print_version(invo_name
);
140 icachesw
= &rcachesw
;
143 icachesw
= &wcachesw
;
145 if (!(cp
= *argp
++) || *cp
== '-')
146 adios (NULL
, "missing argument to %s", argp
[-2]);
147 switch (*icachesw
= smatch (cp
, caches
)) {
149 ambigsw (cp
, caches
);
152 adios (NULL
, "%s unknown", cp
);
166 if (!(cp
= *argp
++) || *cp
== '-')
167 adios (NULL
, "missing argument to %s", argp
[-2]);
169 adios (NULL
, "too many parts (starting with %s), %d max",
175 if (!(cp
= *argp
++) || *cp
== '-')
176 adios (NULL
, "missing argument to %s", argp
[-2]);
178 adios (NULL
, "too many types (starting with %s), %d max",
184 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
185 adios (NULL
, "missing argument to %s", argp
[-2]);
186 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
190 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
191 adios (NULL
, "missing argument to %s", argp
[-2]);
192 outfile
= *cp
== '-' ? cp
: path (cp
, TFILE
);
206 if (*cp
== '+' || *cp
== '@') {
208 adios (NULL
, "only one folder at a time!");
210 folder
= pluspath (cp
);
212 app_msgarg(&msgs
, cp
);
215 /* null terminate the list of acceptable parts/types */
220 adios (NULL
, "must specify output file");
222 /* Check for public cache location */
223 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
226 /* Check for private cache location */
227 if (!(cache_private
= context_find (nmhprivcache
)))
228 cache_private
= ".cache";
229 cache_private
= getcpy (m_maildir (cache_private
));
232 * Check for storage directory. If specified,
233 * then store temporary files there. Else we
234 * store them in standard nmh directory.
236 if ((cp
= context_find (nmhstorage
)) && *cp
)
237 tmp
= concat (cp
, "/", invo_name
, NULL
);
239 tmp
= add (m_maildir (invo_name
), NULL
);
241 if (!context_find ("path"))
242 free (path ("./", TFOLDER
));
244 if (file
&& msgs
.size
)
245 adios (NULL
, "cannot specify msg and file at same time!");
248 * check if message is coming from file
251 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
252 adios (NULL
, "out of memory");
255 if ((ct
= parse_mime (file
)))
259 * message(s) are coming from a folder
262 app_msgarg(&msgs
, "cur");
264 folder
= getfolder (1);
265 maildir
= m_maildir (folder
);
267 if (chdir (maildir
) == NOTOK
)
268 adios (maildir
, "unable to change directory to");
270 /* read folder and create message structure */
271 if (!(mp
= folder_read (folder
, 1)))
272 adios (NULL
, "unable to read folder %s", folder
);
274 /* check for empty folder */
276 adios (NULL
, "no messages in %s", folder
);
278 /* parse all the message ranges/sequences and set SELECTED */
279 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
280 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
282 seq_setprev (mp
); /* set the previous-sequence */
284 if (!(cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
285 adios (NULL
, "out of memory");
288 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
289 if (is_selected(mp
, msgnum
)) {
292 msgnam
= m_name (msgnum
);
293 if ((ct
= parse_mime (msgnam
)))
303 SIGNAL (SIGQUIT
, quitser
);
304 SIGNAL (SIGPIPE
, pipeser
);
307 * Get the associated umask for the relevant contents.
309 for (ctp
= cts
; *ctp
; ctp
++) {
313 if (type_ok (ct
, 1) && !ct
->c_umask
) {
314 if (stat (ct
->c_file
, &st
) != NOTOK
)
315 ct
->c_umask
= ~(st
.st_mode
& 0777);
317 ct
->c_umask
= ~m_gmprot();
322 * Write the content to a file
324 write_content (cts
, outfile
);
326 /* Now free all the structures for the content */
327 for (ctp
= cts
; *ctp
; ctp
++)
333 /* If reading from a folder, do some updating */
335 context_replace (pfolder
, folder
);/* update current folder */
336 seq_setcur (mp
, mp
->hghsel
); /* update current message */
337 seq_save (mp
); /* synchronize sequences */
338 context_save (); /* save the context file */
347 write_content (CT
*cts
, char *outfile
)
351 for (ctp
= cts
; *ctp
; ctp
++) {
353 output_message (ct
, outfile
);
367 fprintf (stderr
, "\n");