]>
diplodocus.org Git - nmh/blob - uip/mhtest.c
155ec05633b1d7d4c616dda452057010f6966ebd
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>
18 #include <h/mhparse.h>
19 #include <h/mhcachesbr.h>
22 #define MHTEST_SWITCHES \
23 X("check", 0, CHECKSW) \
24 X("nocheck", 0, NCHECKSW) \
25 X("verbose", 0, VERBSW) \
26 X("noverbose", 0, NVERBSW) \
27 X("file file", 0, FILESW) \
28 X("outfile file", 0, OUTFILESW) \
29 X("part number", 0, PARTSW) \
30 X("type content", 0, TYPESW) \
31 X("rcache policy", 0, RCACHESW) \
32 X("wcache policy", 0, WCACHESW) \
33 X("version", 0, VERSIONSW) \
34 X("help", 0, HELPSW) \
35 X("debug", -5, DEBUGSW) \
37 #define X(sw, minchars, id) id,
38 DEFINE_SWITCH_ENUM(MHTEST
);
41 #define X(sw, minchars, id) { sw, minchars, id },
42 DEFINE_SWITCH_ARRAY(MHTEST
, switches
);
47 extern char *tmp
; /* directory to place temp files */
52 extern char *cache_public
;
53 extern char *cache_private
;
58 extern char *parts
[NPARTS
+ 1];
59 extern char *types
[NTYPES
+ 1];
63 * This is currently needed to keep mhparse happy.
64 * This needs to be changed.
71 #define quitser pipeser
74 CT
parse_mime (char *);
77 int output_message (CT
, char *);
80 int part_ok (CT
, int);
81 int type_ok (CT
, int);
82 void flush_errors (void);
86 void freects_done (int) NORETURN
;
91 static int write_content (CT
*, char *);
92 static void pipeser (int);
96 main (int argc
, char **argv
)
98 int msgnum
, *icachesw
;
99 char *cp
, *file
= NULL
, *folder
= NULL
;
100 char *maildir
, buf
[100], *outfile
= NULL
;
101 char **argp
, **arguments
;
102 struct msgs_array msgs
= { 0, 0, NULL
};
103 struct msgs
*mp
= NULL
;
109 setlocale(LC_ALL
, "");
111 invo_name
= r1bindex (argv
[0], '/');
113 /* read user profile/context */
116 arguments
= getarguments (invo_name
, argc
, argv
, 1);
122 while ((cp
= *argp
++)) {
124 switch (smatch (++cp
, switches
)) {
126 ambigsw (cp
, switches
);
129 adios (NULL
, "-%s unknown", cp
);
132 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
134 print_help (buf
, switches
, 1);
137 print_version(invo_name
);
141 icachesw
= &rcachesw
;
144 icachesw
= &wcachesw
;
146 if (!(cp
= *argp
++) || *cp
== '-')
147 adios (NULL
, "missing argument to %s", argp
[-2]);
148 switch (*icachesw
= smatch (cp
, caches
)) {
150 ambigsw (cp
, caches
);
153 adios (NULL
, "%s unknown", cp
);
167 if (!(cp
= *argp
++) || *cp
== '-')
168 adios (NULL
, "missing argument to %s", argp
[-2]);
170 adios (NULL
, "too many parts (starting with %s), %d max",
176 if (!(cp
= *argp
++) || *cp
== '-')
177 adios (NULL
, "missing argument to %s", argp
[-2]);
179 adios (NULL
, "too many types (starting with %s), %d max",
185 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
186 adios (NULL
, "missing argument to %s", argp
[-2]);
187 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
191 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
192 adios (NULL
, "missing argument to %s", argp
[-2]);
193 outfile
= *cp
== '-' ? cp
: path (cp
, TFILE
);
207 if (*cp
== '+' || *cp
== '@') {
209 adios (NULL
, "only one folder at a time!");
211 folder
= pluspath (cp
);
213 app_msgarg(&msgs
, cp
);
216 /* null terminate the list of acceptable parts/types */
221 adios (NULL
, "must specify output file");
223 /* Check for public cache location */
224 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
227 /* Check for private cache location */
228 if (!(cache_private
= context_find (nmhprivcache
)))
229 cache_private
= ".cache";
230 cache_private
= getcpy (m_maildir (cache_private
));
233 * Check for storage directory. If specified,
234 * then store temporary files there. Else we
235 * store them in standard nmh directory.
237 if ((cp
= context_find (nmhstorage
)) && *cp
)
238 tmp
= concat (cp
, "/", invo_name
, NULL
);
240 tmp
= add (m_maildir (invo_name
), NULL
);
242 if (!context_find ("path"))
243 free (path ("./", TFOLDER
));
245 if (file
&& msgs
.size
)
246 adios (NULL
, "cannot specify msg and file at same time!");
249 * check if message is coming from file
252 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
253 adios (NULL
, "out of memory");
256 if ((ct
= parse_mime (file
)))
260 * message(s) are coming from a folder
263 app_msgarg(&msgs
, "cur");
265 folder
= getfolder (1);
266 maildir
= m_maildir (folder
);
268 if (chdir (maildir
) == NOTOK
)
269 adios (maildir
, "unable to change directory to");
271 /* read folder and create message structure */
272 if (!(mp
= folder_read (folder
, 1)))
273 adios (NULL
, "unable to read folder %s", folder
);
275 /* check for empty folder */
277 adios (NULL
, "no messages in %s", folder
);
279 /* parse all the message ranges/sequences and set SELECTED */
280 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
281 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
283 seq_setprev (mp
); /* set the previous-sequence */
285 if (!(cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
286 adios (NULL
, "out of memory");
289 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
290 if (is_selected(mp
, msgnum
)) {
293 msgnam
= m_name (msgnum
);
294 if ((ct
= parse_mime (msgnam
)))
304 SIGNAL (SIGQUIT
, quitser
);
305 SIGNAL (SIGPIPE
, pipeser
);
308 * Get the associated umask for the relevant contents.
310 for (ctp
= cts
; *ctp
; ctp
++) {
314 if (type_ok (ct
, 1) && !ct
->c_umask
) {
315 if (stat (ct
->c_file
, &st
) != NOTOK
)
316 ct
->c_umask
= ~(st
.st_mode
& 0777);
318 ct
->c_umask
= ~m_gmprot();
323 * Write the content to a file
325 write_content (cts
, outfile
);
327 /* Now free all the structures for the content */
328 for (ctp
= cts
; *ctp
; ctp
++)
334 /* If reading from a folder, do some updating */
336 context_replace (pfolder
, folder
);/* update current folder */
337 seq_setcur (mp
, mp
->hghsel
); /* update current message */
338 seq_save (mp
); /* synchronize sequences */
339 context_save (); /* save the context file */
348 write_content (CT
*cts
, char *outfile
)
352 for (ctp
= cts
; *ctp
; ctp
++) {
354 output_message (ct
, outfile
);
368 fprintf (stderr
, "\n");