]>
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>
19 #include <h/mhparse.h>
20 #include <h/mhcachesbr.h>
23 #define MHTEST_SWITCHES \
24 X("check", 0, CHECKSW) \
25 X("nocheck", 0, NCHECKSW) \
26 X("verbose", 0, VERBSW) \
27 X("noverbose", 0, NVERBSW) \
28 X("file file", 0, FILESW) \
29 X("outfile file", 0, OUTFILESW) \
30 X("part number", 0, PARTSW) \
31 X("type content", 0, TYPESW) \
32 X("rcache policy", 0, RCACHESW) \
33 X("wcache policy", 0, WCACHESW) \
34 X("version", 0, VERSIONSW) \
35 X("help", 0, HELPSW) \
36 X("debug", -5, DEBUGSW) \
38 #define X(sw, minchars, id) id,
39 DEFINE_SWITCH_ENUM(MHTEST
);
42 #define X(sw, minchars, id) { sw, minchars, id },
43 DEFINE_SWITCH_ARRAY(MHTEST
, switches
);
48 extern char *tmp
; /* directory to place temp files */
53 extern char *cache_public
;
54 extern char *cache_private
;
59 extern char *parts
[NPARTS
+ 1];
60 extern char *types
[NTYPES
+ 1];
64 * This is currently needed to keep mhparse happy.
65 * This needs to be changed.
72 #define quitser pipeser
75 CT
parse_mime (char *);
78 int output_message (CT
, char *);
81 int part_ok (CT
, int);
82 int type_ok (CT
, int);
83 void flush_errors (void);
87 void freects_done (int) NORETURN
;
92 static int write_content (CT
*, char *);
93 static void pipeser (int);
97 main (int argc
, char **argv
)
99 int msgnum
, *icachesw
;
100 char *cp
, *file
= NULL
, *folder
= NULL
;
101 char *maildir
, buf
[100], *outfile
= NULL
;
102 char **argp
, **arguments
;
103 struct msgs_array msgs
= { 0, 0, NULL
};
104 struct msgs
*mp
= NULL
;
110 setlocale(LC_ALL
, "");
112 invo_name
= r1bindex (argv
[0], '/');
114 /* read user profile/context */
117 arguments
= getarguments (invo_name
, argc
, argv
, 1);
123 while ((cp
= *argp
++)) {
125 switch (smatch (++cp
, switches
)) {
127 ambigsw (cp
, switches
);
130 adios (NULL
, "-%s unknown", cp
);
133 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
135 print_help (buf
, switches
, 1);
138 print_version(invo_name
);
142 icachesw
= &rcachesw
;
145 icachesw
= &wcachesw
;
147 if (!(cp
= *argp
++) || *cp
== '-')
148 adios (NULL
, "missing argument to %s", argp
[-2]);
149 switch (*icachesw
= smatch (cp
, caches
)) {
151 ambigsw (cp
, caches
);
154 adios (NULL
, "%s unknown", cp
);
168 if (!(cp
= *argp
++) || *cp
== '-')
169 adios (NULL
, "missing argument to %s", argp
[-2]);
171 adios (NULL
, "too many parts (starting with %s), %d max",
177 if (!(cp
= *argp
++) || *cp
== '-')
178 adios (NULL
, "missing argument to %s", argp
[-2]);
180 adios (NULL
, "too many types (starting with %s), %d max",
186 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
187 adios (NULL
, "missing argument to %s", argp
[-2]);
188 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
192 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
193 adios (NULL
, "missing argument to %s", argp
[-2]);
194 outfile
= *cp
== '-' ? cp
: path (cp
, TFILE
);
208 if (*cp
== '+' || *cp
== '@') {
210 adios (NULL
, "only one folder at a time!");
212 folder
= pluspath (cp
);
214 app_msgarg(&msgs
, cp
);
217 /* null terminate the list of acceptable parts/types */
222 adios (NULL
, "must specify output file");
224 /* Check for public cache location */
225 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
228 /* Check for private cache location */
229 if (!(cache_private
= context_find (nmhprivcache
)))
230 cache_private
= ".cache";
231 cache_private
= getcpy (m_maildir (cache_private
));
234 * Check for storage directory. If specified,
235 * then store temporary files there. Else we
236 * store them in standard nmh directory.
238 if ((cp
= context_find (nmhstorage
)) && *cp
)
239 tmp
= concat (cp
, "/", invo_name
, NULL
);
241 tmp
= add (m_maildir (invo_name
), NULL
);
243 if (!context_find ("path"))
244 free (path ("./", TFOLDER
));
246 if (file
&& msgs
.size
)
247 adios (NULL
, "cannot specify msg and file at same time!");
250 * check if message is coming from file
253 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
254 adios (NULL
, "out of memory");
257 if ((ct
= parse_mime (file
)))
261 * message(s) are coming from a folder
264 app_msgarg(&msgs
, "cur");
266 folder
= getfolder (1);
267 maildir
= m_maildir (folder
);
269 if (chdir (maildir
) == NOTOK
)
270 adios (maildir
, "unable to change directory to");
272 /* read folder and create message structure */
273 if (!(mp
= folder_read (folder
)))
274 adios (NULL
, "unable to read folder %s", folder
);
276 /* check for empty folder */
278 adios (NULL
, "no messages in %s", folder
);
280 /* parse all the message ranges/sequences and set SELECTED */
281 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
282 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
284 seq_setprev (mp
); /* set the previous-sequence */
286 if (!(cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
287 adios (NULL
, "out of memory");
290 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
291 if (is_selected(mp
, msgnum
)) {
294 msgnam
= m_name (msgnum
);
295 if ((ct
= parse_mime (msgnam
)))
305 SIGNAL (SIGQUIT
, quitser
);
306 SIGNAL (SIGPIPE
, pipeser
);
309 * Get the associated umask for the relevant contents.
311 for (ctp
= cts
; *ctp
; ctp
++) {
315 if (type_ok (ct
, 1) && !ct
->c_umask
) {
316 if (stat (ct
->c_file
, &st
) != NOTOK
)
317 ct
->c_umask
= ~(st
.st_mode
& 0777);
319 ct
->c_umask
= ~m_gmprot();
324 * Write the content to a file
326 write_content (cts
, outfile
);
328 /* Now free all the structures for the content */
329 for (ctp
= cts
; *ctp
; ctp
++)
335 /* If reading from a folder, do some updating */
337 context_replace (pfolder
, folder
);/* update current folder */
338 seq_setcur (mp
, mp
->hghsel
); /* update current message */
339 seq_save (mp
); /* synchronize sequences */
340 context_save (); /* save the context file */
349 write_content (CT
*cts
, char *outfile
)
353 for (ctp
= cts
; *ctp
; ctp
++) {
355 output_message (ct
, outfile
);
369 fprintf (stderr
, "\n");