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