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