]> diplodocus.org Git - nmh/blob - uip/mhn.c
More tests for extended-parameter parsing.
[nmh] / uip / mhn.c
1
2 /*
3 * mhn.c -- display, list, cache, or store the contents of MIME messages
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 MHN_SWITCHES \
22 X("auto", 0, AUTOSW) \
23 X("noauto", 0, NAUTOSW) \
24 X("cache", 0, CACHESW) \
25 X("nocache", 0, NCACHESW) \
26 X("check", 0, CHECKSW) \
27 X("nocheck", 0, NCHECKSW) \
28 X("headers", 0, HEADSW) \
29 X("noheaders", 0, NHEADSW) \
30 X("list", 0, LISTSW) \
31 X("nolist", 0, NLISTSW) \
32 X("pause", 0, PAUSESW) \
33 X("nopause", 0, NPAUSESW) \
34 X("realsize", 0, SIZESW) \
35 X("norealsize", 0, NSIZESW) \
36 X("serialonly", 0, SERIALSW) \
37 X("noserialonly", 0, NSERIALSW) \
38 X("show", 0, SHOWSW) \
39 X("noshow", 0, NSHOWSW) \
40 X("store", 0, STORESW) \
41 X("nostore", 0, NSTORESW) \
42 X("verbose", 0, VERBSW) \
43 X("noverbose", 0, NVERBSW) \
44 X("file file", 0, FILESW) \
45 X("form formfile", 0, FORMSW) \
46 X("part number", 0, PARTSW) \
47 X("type content", 0, TYPESW) \
48 X("rcache policy", 0, RCACHESW) \
49 X("wcache policy", 0, WCACHESW) \
50 X("version", 0, VERSIONSW) \
51 X("help", 0, HELPSW) \
52 /* \
53 * for debugging \
54 */ \
55 X("debug", -5, DEBUGSW) \
56 /* \
57 * switches for moreproc/mhlproc \
58 */ \
59 X("moreproc program", -4, PROGSW) \
60 X("nomoreproc", -3, NPROGSW) \
61 X("length lines", -4, LENSW) \
62 X("width columns", -4, WIDTHSW) \
63 /* \
64 * switches for mhbuild \
65 */ \
66 X("build", -5, BUILDSW) \
67 X("nobuild", -7, NBUILDSW) \
68 X("rfc934mode", -10, RFC934SW) \
69 X("norfc934mode", -12, NRFC934SW) \
70
71 #define X(sw, minchars, id) id,
72 DEFINE_SWITCH_ENUM(MHN);
73 #undef X
74
75 #define X(sw, minchars, id) { sw, minchars, id },
76 DEFINE_SWITCH_ARRAY(MHN, switches);
77 #undef X
78
79
80 /* mhcachesbr.c */
81 extern int rcachesw;
82 extern int wcachesw;
83 extern char *cache_public;
84 extern char *cache_private;
85
86 /* mhshowsbr.c */
87 extern int pausesw;
88 extern int serialsw;
89 extern char *progsw;
90 extern int nolist;
91 extern int nomore; /* flags for moreproc/header display */
92 extern char *formsw;
93
94 /* mhstoresbr.c */
95 extern int autosw;
96 extern char *cwd; /* cache current working directory */
97
98 /* mhmisc.c */
99 extern int npart;
100 extern int ntype;
101 extern char *parts[NPARTS + 1];
102 extern char *types[NTYPES + 1];
103 extern int userrs;
104
105 int debugsw = 0;
106 int verbosw = 0;
107
108 /*
109 * variables for mhbuild (mhn -build)
110 */
111 static int buildsw = 0;
112 static int rfc934sw = 0;
113
114 /*
115 * what action to take?
116 */
117 static int cachesw = 0;
118 static int listsw = 0;
119 static int showsw = 0;
120 static int storesw = 0;
121
122 #define quitser pipeser
123
124 /* mhparse.c */
125 CT parse_mime (char *);
126
127 /* mhmisc.c */
128 int part_ok (CT, int);
129 int type_ok (CT, int);
130 void flush_errors (void);
131
132 /* mhshowsbr.c */
133 void show_all_messages (CT *);
134
135 /* mhstoresbr.c */
136 void store_all_messages (CT *);
137
138 /* mhcachesbr.c */
139 void cache_all_messages (CT *);
140
141 /* mhfree.c */
142 extern CT *cts;
143 void freects_done (int) NORETURN;
144
145 /*
146 * static prototypes
147 */
148 static void pipeser (int);
149
150
151 int
152 main (int argc, char **argv)
153 {
154 int sizesw = 1, headsw = 1;
155 int msgnum, *icachesw;
156 char *cp, *file = NULL, *folder = NULL;
157 char *maildir, buf[100], **argp;
158 char **arguments;
159 struct msgs_array msgs = { 0, 0, NULL };
160 struct msgs *mp = NULL;
161 CT ct, *ctp;
162 FILE *fp;
163
164 if (nmh_init(argv[0], 1)) { return 1; }
165
166 done=freects_done;
167
168 arguments = getarguments (invo_name, argc, argv, 1);
169 argp = arguments;
170
171 /*
172 * Parse arguments
173 */
174 while ((cp = *argp++)) {
175 if (*cp == '-') {
176 switch (smatch (++cp, switches)) {
177 case AMBIGSW:
178 ambigsw (cp, switches);
179 done (1);
180 case UNKWNSW:
181 adios (NULL, "-%s unknown", cp);
182
183 case HELPSW:
184 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
185 invo_name);
186 print_help (buf, switches, 1);
187 done (0);
188 case VERSIONSW:
189 print_version(invo_name);
190 done (0);
191
192 case AUTOSW:
193 autosw++;
194 continue;
195 case NAUTOSW:
196 autosw = 0;
197 continue;
198
199 case CACHESW:
200 cachesw++;
201 continue;
202 case NCACHESW:
203 cachesw = 0;
204 continue;
205
206 case RCACHESW:
207 icachesw = &rcachesw;
208 goto do_cache;
209 case WCACHESW:
210 icachesw = &wcachesw;
211 do_cache:
212 if (!(cp = *argp++) || *cp == '-')
213 adios (NULL, "missing argument to %s", argp[-2]);
214 switch (*icachesw = smatch (cp, caches)) {
215 case AMBIGSW:
216 ambigsw (cp, caches);
217 done (1);
218 case UNKWNSW:
219 adios (NULL, "%s unknown", cp);
220 default:
221 break;
222 }
223 continue;
224
225 case CHECKSW:
226 checksw++;
227 continue;
228 case NCHECKSW:
229 checksw = 0;
230 continue;
231
232 case HEADSW:
233 headsw = 1;
234 continue;
235 case NHEADSW:
236 headsw = 0;
237 continue;
238
239 case LISTSW:
240 listsw = 1;
241 continue;
242 case NLISTSW:
243 listsw = 0;
244 continue;
245
246 case PAUSESW:
247 pausesw = 1;
248 continue;
249 case NPAUSESW:
250 pausesw = 0;
251 continue;
252
253 case SERIALSW:
254 serialsw = 1;
255 continue;
256 case NSERIALSW:
257 serialsw = 0;
258 continue;
259
260 case SHOWSW:
261 showsw = 1;
262 continue;
263 case NSHOWSW:
264 showsw = 0;
265 continue;
266
267 case SIZESW:
268 sizesw = 1;
269 continue;
270 case NSIZESW:
271 sizesw = 0;
272 continue;
273
274 case STORESW:
275 storesw = 1;
276 continue;
277 case NSTORESW:
278 storesw = 0;
279 continue;
280
281 case PARTSW:
282 if (!(cp = *argp++) || *cp == '-')
283 adios (NULL, "missing argument to %s", argp[-2]);
284 if (npart >= NPARTS)
285 adios (NULL, "too many parts (starting with %s), %d max",
286 cp, NPARTS);
287 parts[npart++] = cp;
288 continue;
289
290 case TYPESW:
291 if (!(cp = *argp++) || *cp == '-')
292 adios (NULL, "missing argument to %s", argp[-2]);
293 if (ntype >= NTYPES)
294 adios (NULL, "too many types (starting with %s), %d max",
295 cp, NTYPES);
296 types[ntype++] = cp;
297 continue;
298
299 case FILESW:
300 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
301 adios (NULL, "missing argument to %s", argp[-2]);
302 file = *cp == '-' ? cp : path (cp, TFILE);
303 continue;
304
305 case FORMSW:
306 if (!(cp = *argp++) || *cp == '-')
307 adios (NULL, "missing argument to %s", argp[-2]);
308 if (formsw)
309 free (formsw);
310 formsw = getcpy (etcpath (cp));
311 continue;
312
313 /*
314 * Switches for moreproc/mhlproc
315 */
316 case PROGSW:
317 if (!(progsw = *argp++) || *progsw == '-')
318 adios (NULL, "missing argument to %s", argp[-2]);
319 continue;
320 case NPROGSW:
321 nomore++;
322 continue;
323
324 case LENSW:
325 case WIDTHSW:
326 if (!(cp = *argp++) || *cp == '-')
327 adios (NULL, "missing argument to %s", argp[-2]);
328 continue;
329
330 /*
331 * Switches for mhbuild
332 */
333 case BUILDSW:
334 buildsw = 1;
335 continue;
336 case NBUILDSW:
337 buildsw = 0;
338 continue;
339 case RFC934SW:
340 rfc934sw = 1;
341 continue;
342 case NRFC934SW:
343 rfc934sw = -1;
344 continue;
345
346 case VERBSW:
347 verbosw = 1;
348 continue;
349 case NVERBSW:
350 verbosw = 0;
351 continue;
352 case DEBUGSW:
353 debugsw = 1;
354 continue;
355 }
356 }
357 if (*cp == '+' || *cp == '@') {
358 if (folder)
359 adios (NULL, "only one folder at a time!");
360 else
361 folder = pluspath (cp);
362 } else
363 app_msgarg(&msgs, cp);
364 }
365
366 /* null terminate the list of acceptable parts/types */
367 parts[npart] = NULL;
368 types[ntype] = NULL;
369
370 /*
371 * Check if we've specified an additional profile
372 */
373 if ((cp = getenv ("MHN"))) {
374 if ((fp = fopen (cp, "r"))) {
375 readconfig ((struct node **) 0, fp, cp, 0);
376 fclose (fp);
377 } else {
378 admonish ("", "unable to read $MHN profile (%s)", cp);
379 }
380 }
381
382 /*
383 * Read the standard profile setup
384 */
385 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
386 readconfig ((struct node **) 0, fp, cp, 0);
387 fclose (fp);
388 }
389
390 /* Check for public cache location */
391 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
392 cache_public = NULL;
393
394 /* Check for private cache location */
395 if (!(cache_private = context_find (nmhprivcache)))
396 cache_private = ".cache";
397 cache_private = getcpy (m_maildir (cache_private));
398
399 /*
400 * Cache the current directory before we do any chdirs()'s.
401 */
402 cwd = getcpy (pwd());
403
404 if (!context_find ("path"))
405 free (path ("./", TFOLDER));
406
407 /*
408 * Process a mhn composition file (mhn -build)
409 */
410 if (buildsw) {
411 char *vec[MAXARGS];
412 int vecp;
413
414 if (showsw || storesw || cachesw)
415 adios (NULL, "cannot use -build with -show, -store, -cache");
416 if (msgs.size < 1)
417 adios (NULL, "need to specify a %s composition file", invo_name);
418 if (msgs.size > 1)
419 adios (NULL, "only one %s composition file at a time", invo_name);
420
421 vecp = 0;
422 vec[vecp++] = "mhbuild";
423
424 if (rfc934sw == 1)
425 vec[vecp++] = "-rfc934mode";
426 else if (rfc934sw == -1)
427 vec[vecp++] = "-norfc934mode";
428
429 vec[vecp++] = msgs.msgs[0];
430 vec[vecp] = NULL;
431
432 execvp ("mhbuild", vec);
433 fprintf (stderr, "unable to exec ");
434 _exit (-1);
435 }
436
437 /*
438 * Process a mhn composition file (old MH style)
439 */
440 if (msgs.size == 1 && !folder && !npart && !cachesw
441 && !showsw && !storesw && !ntype && !file
442 && (cp = getenv ("mhdraft"))
443 && strcmp (cp, msgs.msgs[0]) == 0) {
444
445 char *vec[MAXARGS];
446 int vecp;
447
448 vecp = 0;
449 vec[vecp++] = "mhbuild";
450
451 if (rfc934sw == 1)
452 vec[vecp++] = "-rfc934mode";
453 else if (rfc934sw == -1)
454 vec[vecp++] = "-norfc934mode";
455
456 vec[vecp++] = cp;
457 vec[vecp] = NULL;
458
459 execvp ("mhbuild", vec);
460 fprintf (stderr, "unable to exec ");
461 _exit (-1);
462 }
463
464 if (file && msgs.size)
465 adios (NULL, "cannot specify msg and file at same time!");
466
467 /*
468 * check if message is coming from file
469 */
470 if (file) {
471 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
472 adios (NULL, "out of memory");
473 ctp = cts;
474
475 if ((ct = parse_mime (file)))
476 *ctp++ = ct;
477 } else {
478 /*
479 * message(s) are coming from a folder
480 */
481 if (!msgs.size)
482 app_msgarg(&msgs, "cur");
483 if (!folder)
484 folder = getfolder (1);
485 maildir = m_maildir (folder);
486
487 if (chdir (maildir) == NOTOK)
488 adios (maildir, "unable to change directory to");
489
490 /* read folder and create message structure */
491 if (!(mp = folder_read (folder, 1)))
492 adios (NULL, "unable to read folder %s", folder);
493
494 /* check for empty folder */
495 if (mp->nummsg == 0)
496 adios (NULL, "no messages in %s", folder);
497
498 /* parse all the message ranges/sequences and set SELECTED */
499 for (msgnum = 0; msgnum < msgs.size; msgnum++)
500 if (!m_convert (mp, msgs.msgs[msgnum]))
501 done (1);
502 seq_setprev (mp); /* set the previous-sequence */
503
504 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
505 adios (NULL, "out of memory");
506 ctp = cts;
507
508 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
509 if (is_selected(mp, msgnum)) {
510 char *msgnam;
511
512 msgnam = m_name (msgnum);
513 if ((ct = parse_mime (msgnam)))
514 *ctp++ = ct;
515 }
516 }
517 }
518
519 if (!*cts)
520 done (1);
521
522 /*
523 * You can't give more than one of these flags
524 * at a time.
525 */
526 if (showsw + listsw + storesw + cachesw > 1)
527 adios (NULL, "can only use one of -show, -list, -store, -cache at same time");
528
529 /* If no action is specified, assume -show */
530 if (!listsw && !showsw && !storesw && !cachesw)
531 showsw = 1;
532
533 userrs = 1;
534 SIGNAL (SIGQUIT, quitser);
535 SIGNAL (SIGPIPE, pipeser);
536
537 /*
538 * Get the associated umask for the relevant contents.
539 */
540 for (ctp = cts; *ctp; ctp++) {
541 struct stat st;
542
543 ct = *ctp;
544 if (type_ok (ct, 1) && !ct->c_umask) {
545 if (stat (ct->c_file, &st) != NOTOK)
546 ct->c_umask = ~(st.st_mode & 0777);
547 else
548 ct->c_umask = ~m_gmprot();
549 }
550 }
551
552 /*
553 * List the message content
554 */
555 if (listsw)
556 list_all_messages (cts, headsw, sizesw, verbosw, debugsw, 0);
557
558 /*
559 * Store the message content
560 */
561 if (storesw)
562 store_all_messages (cts);
563
564 /*
565 * Cache the message content
566 */
567 if (cachesw)
568 cache_all_messages (cts);
569
570 /*
571 * Show the message content
572 */
573 if (showsw)
574 show_all_messages (cts);
575
576 /* Now free all the structures for the content */
577 for (ctp = cts; *ctp; ctp++)
578 free_content (*ctp);
579
580 free ((char *) cts);
581 cts = NULL;
582
583 /* If reading from a folder, do some updating */
584 if (mp) {
585 context_replace (pfolder, folder);/* update current folder */
586 seq_setcur (mp, mp->hghsel); /* update current message */
587 seq_save (mp); /* synchronize sequences */
588 context_save (); /* save the context file */
589 }
590
591 done (0);
592 return 1;
593 }
594
595
596 static void
597 pipeser (int i)
598 {
599 if (i == SIGQUIT) {
600 fflush (stdout);
601 fprintf (stderr, "\n");
602 fflush (stderr);
603 }
604
605 done (1);
606 /* NOTREACHED */
607 }