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