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