]>
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
;
107 setlocale(LC_ALL
, "");
108 invo_name
= r1bindex (argv
[0], '/');
110 /* read user profile/context */
113 arguments
= getarguments (invo_name
, argc
, argv
, 1);
119 while ((cp
= *argp
++)) {
121 switch (smatch (++cp
, switches
)) {
123 ambigsw (cp
, switches
);
126 adios (NULL
, "-%s unknown", cp
);
129 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
131 print_help (buf
, switches
, 1);
134 print_version(invo_name
);
138 icachesw
= &rcachesw
;
141 icachesw
= &wcachesw
;
143 if (!(cp
= *argp
++) || *cp
== '-')
144 adios (NULL
, "missing argument to %s", argp
[-2]);
145 switch (*icachesw
= smatch (cp
, caches
)) {
147 ambigsw (cp
, caches
);
150 adios (NULL
, "%s unknown", cp
);
164 if (!(cp
= *argp
++) || *cp
== '-')
165 adios (NULL
, "missing argument to %s", argp
[-2]);
167 adios (NULL
, "too many parts (starting with %s), %d max",
173 if (!(cp
= *argp
++) || *cp
== '-')
174 adios (NULL
, "missing argument to %s", argp
[-2]);
176 adios (NULL
, "too many types (starting with %s), %d max",
182 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
183 adios (NULL
, "missing argument to %s", argp
[-2]);
184 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
188 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
189 adios (NULL
, "missing argument to %s", argp
[-2]);
190 outfile
= *cp
== '-' ? cp
: path (cp
, TFILE
);
204 if (*cp
== '+' || *cp
== '@') {
206 adios (NULL
, "only one folder at a time!");
208 folder
= pluspath (cp
);
210 app_msgarg(&msgs
, cp
);
213 /* null terminate the list of acceptable parts/types */
218 adios (NULL
, "must specify output file");
220 /* Check for public cache location */
221 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
224 /* Check for private cache location */
225 if (!(cache_private
= context_find (nmhprivcache
)))
226 cache_private
= ".cache";
227 cache_private
= getcpy (m_maildir (cache_private
));
230 * Check for storage directory. If specified,
231 * then store temporary files there. Else we
232 * store them in standard nmh directory.
234 if ((cp
= context_find (nmhstorage
)) && *cp
)
235 tmp
= concat (cp
, "/", invo_name
, NULL
);
237 tmp
= add (m_maildir (invo_name
), NULL
);
239 if (!context_find ("path"))
240 free (path ("./", TFOLDER
));
242 if (file
&& msgs
.size
)
243 adios (NULL
, "cannot specify msg and file at same time!");
246 * check if message is coming from file
249 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
250 adios (NULL
, "out of memory");
253 if ((ct
= parse_mime (file
)))
257 * message(s) are coming from a folder
260 app_msgarg(&msgs
, "cur");
262 folder
= getfolder (1);
263 maildir
= m_maildir (folder
);
265 if (chdir (maildir
) == NOTOK
)
266 adios (maildir
, "unable to change directory to");
268 /* read folder and create message structure */
269 if (!(mp
= folder_read (folder
, 1)))
270 adios (NULL
, "unable to read folder %s", folder
);
272 /* check for empty folder */
274 adios (NULL
, "no messages in %s", folder
);
276 /* parse all the message ranges/sequences and set SELECTED */
277 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
278 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
280 seq_setprev (mp
); /* set the previous-sequence */
282 if (!(cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
283 adios (NULL
, "out of memory");
286 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
287 if (is_selected(mp
, msgnum
)) {
290 msgnam
= m_name (msgnum
);
291 if ((ct
= parse_mime (msgnam
)))
301 SIGNAL (SIGQUIT
, quitser
);
302 SIGNAL (SIGPIPE
, pipeser
);
305 * Get the associated umask for the relevant contents.
307 for (ctp
= cts
; *ctp
; ctp
++) {
311 if (type_ok (ct
, 1) && !ct
->c_umask
) {
312 if (stat (ct
->c_file
, &st
) != NOTOK
)
313 ct
->c_umask
= ~(st
.st_mode
& 0777);
315 ct
->c_umask
= ~m_gmprot();
320 * Write the content to a file
322 write_content (cts
, outfile
);
324 /* Now free all the structures for the content */
325 for (ctp
= cts
; *ctp
; ctp
++)
331 /* If reading from a folder, do some updating */
333 context_replace (pfolder
, folder
);/* update current folder */
334 seq_setcur (mp
, mp
->hghsel
); /* update current message */
335 seq_save (mp
); /* synchronize sequences */
336 context_save (); /* save the context file */
345 write_content (CT
*cts
, char *outfile
)
349 for (ctp
= cts
; *ctp
; ctp
++) {
351 output_message (ct
, outfile
);
365 fprintf (stderr
, "\n");