Remove caching of external MIME parts.
[mmh] / uip / mhtest.c
1 /*
2 ** mhtest.c -- test harness for MIME routines
3 **
4 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
7 */
8
9 #include <h/mh.h>
10 #include <fcntl.h>
11 #include <h/signals.h>
12 #include <errno.h>
13 #include <signal.h>
14 #include <h/tws.h>
15 #include <h/mime.h>
16 #include <h/mhparse.h>
17 #include <h/utils.h>
18
19 static struct swit switches[] = {
20 #define VERBSW  0
21         { "verbose", 0 },
22 #define NVERBSW  1
23         { "noverbose", 0 },
24 #define FILESW  2
25         { "file file", 0 },
26 #define OUTFILESW  3
27         { "outfile file", 0 },
28 #define PARTSW  4
29         { "part number", 0 },
30 #define TYPESW  5
31         { "type content", 0 },
32 #define VERSIONSW  6
33         { "version", 0 },
34 #define HELPSW  7
35         { "help", 0 },
36 #define DEBUGSW  8
37         { "debug", -5 },
38         { NULL, 0 }
39 };
40
41
42 int ebcdicsw = 0;  /* hack for linking purposes */
43
44 /* mhparse.c */
45 extern char *tmp;  /* directory to place temp files */
46
47 /* mhmisc.c */
48 extern int npart;
49 extern int ntype;
50 extern char *parts[NPARTS + 1];
51 extern char *types[NTYPES + 1];
52 extern int userrs;
53
54 /*
55 ** This is currently needed to keep mhparse happy.
56 ** This needs to be changed.
57 */
58 pid_t xpid  = 0;
59
60 int debugsw = 0;
61 int verbosw = 0;
62
63 #define quitser pipeser
64
65 /* mhparse.c */
66 CT parse_mime(char *);
67
68 /* mhoutsbr.c */
69 int output_message(CT, char *);
70
71 /* mhmisc.c */
72 int part_ok(CT, int);
73 int type_ok(CT, int);
74 void set_endian(void);
75 void flush_errors(void);
76
77 /* mhfree.c */
78 void free_content(CT);
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;
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         done=freects_done;
101
102 #ifdef LOCALE
103         setlocale(LC_ALL, "");
104 #endif
105         invo_name = mhbasename(argv[0]);
106
107         /* read user profile/context */
108         context_read();
109
110         arguments = getarguments(invo_name, argc, argv, 1);
111         argp = arguments;
112
113         /*
114         ** Parse arguments
115         */
116         while ((cp = *argp++)) {
117                 if (*cp == '-') {
118                         switch (smatch(++cp, switches)) {
119                         case AMBIGSW:
120                                 ambigsw(cp, switches);
121                                 done(1);
122                         case UNKWNSW:
123                                 adios(NULL, "-%s unknown", cp);
124
125                         case HELPSW:
126                                 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
127                                 print_help(buf, switches, 1);
128                                 done(1);
129                         case VERSIONSW:
130                                 print_version(invo_name);
131                                 done(1);
132
133                         case PARTSW:
134                                 if (!(cp = *argp++) || *cp == '-')
135                                         adios(NULL, "missing argument to %s", argp[-2]);
136                                 if (npart >= NPARTS)
137                                         adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
138                                 parts[npart++] = cp;
139                                 continue;
140
141                         case TYPESW:
142                                 if (!(cp = *argp++) || *cp == '-')
143                                         adios(NULL, "missing argument to %s", argp[-2]);
144                                 if (ntype >= NTYPES)
145                                         adios(NULL, "too many types (starting with %s), %d max",
146                                                    cp, NTYPES);
147                                 types[ntype++] = cp;
148                                 continue;
149
150                         case FILESW:
151                                 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
152                                         adios(NULL, "missing argument to %s",
153                                                         argp[-2]);
154                                 file = *cp == '-' ? cp : getcpy(expanddir(cp));
155                                 continue;
156
157                         case OUTFILESW:
158                                 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
159                                         adios(NULL, "missing argument to %s",
160                                                         argp[-2]);
161                                 outfile = *cp == '-' ? cp : getcpy(expanddir(cp));
162                                 continue;
163
164                         case VERBSW:
165                                 verbosw = 1;
166                                 continue;
167                         case NVERBSW:
168                                 verbosw = 0;
169                                 continue;
170                         case DEBUGSW:
171                                 debugsw = 1;
172                                 continue;
173                         }
174                 }
175                 if (*cp == '+' || *cp == '@') {
176                         if (folder)
177                                 adios(NULL, "only one folder at a time!");
178                         else
179                                 folder = getcpy(expandfol(cp));
180                 } else
181                         app_msgarg(&msgs, cp);
182         }
183
184         /* null terminate the list of acceptable parts/types */
185         parts[npart] = NULL;
186         types[ntype] = NULL;
187
188         set_endian();
189
190         if (outfile == NULL)
191                 adios(NULL, "must specify output file");
192
193         /*
194         ** Check for storage directory.  If specified,
195         ** then store temporary files there.  Else we
196         ** store them in standard nmh directory.
197         */
198         if ((cp = context_find(nmhstorage)) && *cp)
199                 tmp = concat(cp, "/", invo_name, NULL);
200         else
201                 tmp = getcpy(toabsdir(invo_name));
202
203         if (file && msgs.size)
204                 adios(NULL, "cannot specify msg and file at same time!");
205
206         /*
207         ** check if message is coming from file
208         */
209         if (file) {
210                 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
211                         adios(NULL, "out of memory");
212                 ctp = cts;
213
214                 if ((ct = parse_mime(file)))
215                         *ctp++ = ct;
216         } else {
217                 /*
218                 ** message(s) are coming from a folder
219                 */
220                 if (!msgs.size)
221                         app_msgarg(&msgs, seq_cur);
222                 if (!folder)
223                         folder = getcurfol();
224                 maildir = toabsdir(folder);
225
226                 if (chdir(maildir) == NOTOK)
227                         adios(maildir, "unable to change directory to");
228
229                 /* read folder and create message structure */
230                 if (!(mp = folder_read(folder)))
231                         adios(NULL, "unable to read folder %s", folder);
232
233                 /* check for empty folder */
234                 if (mp->nummsg == 0)
235                         adios(NULL, "no messages in %s", folder);
236
237                 /* parse all the message ranges/sequences and set SELECTED */
238                 for (msgnum = 0; msgnum < msgs.size; msgnum++)
239                         if (!m_convert(mp, msgs.msgs[msgnum]))
240                                 done(1);
241                 seq_setprev(mp);  /* set the previous-sequence */
242
243                 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
244                                 sizeof(*cts))))
245                         adios(NULL, "out of memory");
246                 ctp = cts;
247
248                 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
249                         if (is_selected(mp, msgnum)) {
250                                 char *msgnam;
251
252                                 msgnam = m_name(msgnum);
253                                 if ((ct = parse_mime(msgnam)))
254                                         *ctp++ = ct;
255                         }
256                 }
257         }
258
259         if (!*cts)
260                 done(1);
261
262         userrs = 1;
263         SIGNAL(SIGQUIT, quitser);
264         SIGNAL(SIGPIPE, pipeser);
265
266         /*
267         ** Get the associated umask for the relevant contents.
268         */
269         for (ctp = cts; *ctp; ctp++) {
270                 struct stat st;
271
272                 ct = *ctp;
273                 if (type_ok(ct, 1) && !ct->c_umask) {
274                         if (stat(ct->c_file, &st) != NOTOK)
275                                 ct->c_umask = ~(st.st_mode & 0777);
276                         else
277                                 ct->c_umask = ~m_gmprot();
278                 }
279         }
280
281         /*
282         ** Write the content to a file
283         */
284         write_content(cts, outfile);
285
286         /* Now free all the structures for the content */
287         for (ctp = cts; *ctp; ctp++)
288                 free_content(*ctp);
289
290         free((char *) cts);
291         cts = NULL;
292
293         /* If reading from a folder, do some updating */
294         if (mp) {
295                 context_replace(curfolder, folder); /* update current folder */
296                 seq_setcur(mp, mp->hghsel);  /* update current message */
297                 seq_save(mp);  /* synchronize sequences  */
298                 context_save();  /* save the context file  */
299         }
300
301         done(0);
302         return 1;
303 }
304
305
306 static int
307 write_content(CT *cts, char *outfile)
308 {
309         CT ct, *ctp;
310
311         for (ctp = cts; *ctp; ctp++) {
312                 ct = *ctp;
313                 output_message(ct, outfile);
314         }
315
316         flush_errors();
317         return OK;
318 }
319
320
321 static void
322 pipeser(int i)
323 {
324         if (i == SIGQUIT) {
325                 unlink("core");
326                 fflush(stdout);
327                 fprintf(stderr, "\n");
328                 fflush(stderr);
329         }
330
331         done(1);
332         /* NOTREACHED */
333 }