]> diplodocus.org Git - nmh/blob - uip/mhtest.c
Removed temporary probes added in commit
[nmh] / uip / mhtest.c
1
2 /*
3 * mhtest.c -- test harness for MIME routines
4 *
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.
8 */
9
10 #include <h/mh.h>
11 #include <fcntl.h>
12 #include <h/signals.h>
13 #include <h/md5.h>
14 #include <h/mts.h>
15 #include <h/tws.h>
16 #include <h/mime.h>
17 #include <h/mhparse.h>
18 #include <h/mhcachesbr.h>
19 #include <h/utils.h>
20
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) \
35
36 #define X(sw, minchars, id) id,
37 DEFINE_SWITCH_ENUM(MHTEST);
38 #undef X
39
40 #define X(sw, minchars, id) { sw, minchars, id },
41 DEFINE_SWITCH_ARRAY(MHTEST, switches);
42 #undef X
43
44
45 /* mhparse.c */
46 extern char *tmp; /* directory to place temp files */
47
48 /* mhcachesbr.c */
49 extern int rcachesw;
50 extern int wcachesw;
51 extern char *cache_public;
52 extern char *cache_private;
53
54 /* mhmisc.c */
55 extern int npart;
56 extern int ntype;
57 extern char *parts[NPARTS + 1];
58 extern char *types[NTYPES + 1];
59 extern int userrs;
60
61 /*
62 * This is currently needed to keep mhparse happy.
63 * This needs to be changed.
64 */
65 pid_t xpid = 0;
66
67 int debugsw = 0;
68 int verbosw = 0;
69
70 #define quitser pipeser
71
72 /* mhparse.c */
73 CT parse_mime (char *);
74
75 /* mhoutsbr.c */
76 int output_message (CT, char *);
77
78 /* mhmisc.c */
79 int part_ok (CT, int);
80 int type_ok (CT, int);
81 void flush_errors (void);
82
83 /* mhfree.c */
84 extern CT *cts;
85 void freects_done (int) NORETURN;
86
87 /*
88 * static prototypes
89 */
90 static int write_content (CT *, char *);
91 static void pipeser (int);
92
93
94 int
95 main (int argc, char **argv)
96 {
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;
103 CT ct, *ctp;
104
105 done=freects_done;
106
107 #ifdef LOCALE
108 setlocale(LC_ALL, "");
109 #endif
110 invo_name = r1bindex (argv[0], '/');
111
112 /* read user profile/context */
113 context_read();
114
115 arguments = getarguments (invo_name, argc, argv, 1);
116 argp = arguments;
117
118 /*
119 * Parse arguments
120 */
121 while ((cp = *argp++)) {
122 if (*cp == '-') {
123 switch (smatch (++cp, switches)) {
124 case AMBIGSW:
125 ambigsw (cp, switches);
126 done (1);
127 case UNKWNSW:
128 adios (NULL, "-%s unknown", cp);
129
130 case HELPSW:
131 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
132 invo_name);
133 print_help (buf, switches, 1);
134 done (0);
135 case VERSIONSW:
136 print_version(invo_name);
137 done (0);
138
139 case RCACHESW:
140 icachesw = &rcachesw;
141 goto do_cache;
142 case WCACHESW:
143 icachesw = &wcachesw;
144 do_cache:
145 if (!(cp = *argp++) || *cp == '-')
146 adios (NULL, "missing argument to %s", argp[-2]);
147 switch (*icachesw = smatch (cp, caches)) {
148 case AMBIGSW:
149 ambigsw (cp, caches);
150 done (1);
151 case UNKWNSW:
152 adios (NULL, "%s unknown", cp);
153 default:
154 break;
155 }
156 continue;
157
158 case CHECKSW:
159 checksw++;
160 continue;
161 case NCHECKSW:
162 checksw = 0;
163 continue;
164
165 case PARTSW:
166 if (!(cp = *argp++) || *cp == '-')
167 adios (NULL, "missing argument to %s", argp[-2]);
168 if (npart >= NPARTS)
169 adios (NULL, "too many parts (starting with %s), %d max",
170 cp, NPARTS);
171 parts[npart++] = cp;
172 continue;
173
174 case TYPESW:
175 if (!(cp = *argp++) || *cp == '-')
176 adios (NULL, "missing argument to %s", argp[-2]);
177 if (ntype >= NTYPES)
178 adios (NULL, "too many types (starting with %s), %d max",
179 cp, NTYPES);
180 types[ntype++] = cp;
181 continue;
182
183 case FILESW:
184 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
185 adios (NULL, "missing argument to %s", argp[-2]);
186 file = *cp == '-' ? cp : path (cp, TFILE);
187 continue;
188
189 case OUTFILESW:
190 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
191 adios (NULL, "missing argument to %s", argp[-2]);
192 outfile = *cp == '-' ? cp : path (cp, TFILE);
193 continue;
194
195 case VERBSW:
196 verbosw = 1;
197 continue;
198 case NVERBSW:
199 verbosw = 0;
200 continue;
201 case DEBUGSW:
202 debugsw = 1;
203 continue;
204 }
205 }
206 if (*cp == '+' || *cp == '@') {
207 if (folder)
208 adios (NULL, "only one folder at a time!");
209 else
210 folder = pluspath (cp);
211 } else
212 app_msgarg(&msgs, cp);
213 }
214
215 /* null terminate the list of acceptable parts/types */
216 parts[npart] = NULL;
217 types[ntype] = NULL;
218
219 if (outfile == NULL)
220 adios (NULL, "must specify output file");
221
222 /* Check for public cache location */
223 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
224 cache_public = NULL;
225
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));
230
231 /*
232 * Check for storage directory. If specified,
233 * then store temporary files there. Else we
234 * store them in standard nmh directory.
235 */
236 if ((cp = context_find (nmhstorage)) && *cp)
237 tmp = concat (cp, "/", invo_name, NULL);
238 else
239 tmp = add (m_maildir (invo_name), NULL);
240
241 if (!context_find ("path"))
242 free (path ("./", TFOLDER));
243
244 if (file && msgs.size)
245 adios (NULL, "cannot specify msg and file at same time!");
246
247 /*
248 * check if message is coming from file
249 */
250 if (file) {
251 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
252 adios (NULL, "out of memory");
253 ctp = cts;
254
255 if ((ct = parse_mime (file)))
256 *ctp++ = ct;
257 } else {
258 /*
259 * message(s) are coming from a folder
260 */
261 if (!msgs.size)
262 app_msgarg(&msgs, "cur");
263 if (!folder)
264 folder = getfolder (1);
265 maildir = m_maildir (folder);
266
267 if (chdir (maildir) == NOTOK)
268 adios (maildir, "unable to change directory to");
269
270 /* read folder and create message structure */
271 if (!(mp = folder_read (folder, 1)))
272 adios (NULL, "unable to read folder %s", folder);
273
274 /* check for empty folder */
275 if (mp->nummsg == 0)
276 adios (NULL, "no messages in %s", folder);
277
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]))
281 done (1);
282 seq_setprev (mp); /* set the previous-sequence */
283
284 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
285 adios (NULL, "out of memory");
286 ctp = cts;
287
288 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
289 if (is_selected(mp, msgnum)) {
290 char *msgnam;
291
292 msgnam = m_name (msgnum);
293 if ((ct = parse_mime (msgnam)))
294 *ctp++ = ct;
295 }
296 }
297 }
298
299 if (!*cts)
300 done (1);
301
302 userrs = 1;
303 SIGNAL (SIGQUIT, quitser);
304 SIGNAL (SIGPIPE, pipeser);
305
306 /*
307 * Get the associated umask for the relevant contents.
308 */
309 for (ctp = cts; *ctp; ctp++) {
310 struct stat st;
311
312 ct = *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);
316 else
317 ct->c_umask = ~m_gmprot();
318 }
319 }
320
321 /*
322 * Write the content to a file
323 */
324 write_content (cts, outfile);
325
326 /* Now free all the structures for the content */
327 for (ctp = cts; *ctp; ctp++)
328 free_content (*ctp);
329
330 free ((char *) cts);
331 cts = NULL;
332
333 /* If reading from a folder, do some updating */
334 if (mp) {
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 */
339 }
340
341 done (0);
342 return 1;
343 }
344
345
346 static int
347 write_content (CT *cts, char *outfile)
348 {
349 CT ct, *ctp;
350
351 for (ctp = cts; *ctp; ctp++) {
352 ct = *ctp;
353 output_message (ct, outfile);
354 }
355
356 flush_errors ();
357 return OK;
358 }
359
360
361 static void
362 pipeser (int i)
363 {
364 if (i == SIGQUIT) {
365 unlink ("core");
366 fflush (stdout);
367 fprintf (stderr, "\n");
368 fflush (stderr);
369 }
370
371 done (1);
372 /* NOTREACHED */
373 }