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