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