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