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