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