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