]> diplodocus.org Git - nmh/blob - uip/picksbr.c
Another pass at cleaning up (some of) the manpages.
[nmh] / uip / picksbr.c
1
2 /*
3 * picksbr.c -- routines to help pick along...
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 <h/tws.h>
12 #include <h/picksbr.h>
13 #include <h/utils.h>
14
15 #ifdef HAVE_SYS_TIME_H
16 # include <sys/time.h>
17 #endif
18 #include <time.h>
19
20 #define PARSE_SWITCHES \
21 X("and", 0, PRAND) \
22 X("or", 0, PROR) \
23 X("not", 0, PRNOT) \
24 X("lbrace", 0, PRLBR) \
25 X("rbrace", 0, PRRBR) \
26 X("cc pattern", 0, PRCC) \
27 X("date pattern", 0, PRDATE) \
28 X("from pattern", 0, PRFROM) \
29 X("search pattern", 0, PRSRCH) \
30 X("subject pattern", 0, PRSUBJ) \
31 X("to pattern", 0, PRTO) \
32 X("-othercomponent pattern", 15, PROTHR) \
33 X("after date", 0, PRAFTR) \
34 X("before date", 0, PRBEFR) \
35 X("datefield field", 5, PRDATF) \
36
37 #define X(sw, minchars, id) id,
38 DEFINE_SWITCH_ENUM(PARSE);
39 #undef X
40
41 #define X(sw, minchars, id) { sw, minchars, id },
42 DEFINE_SWITCH_ARRAY(PARSE, parswit);
43 #undef X
44
45 /* DEFINITIONS FOR PATTERN MATCHING */
46
47 /*
48 * We really should be using re_comp() and re_exec() here. Unfortunately,
49 * pick advertises that lowercase characters matches characters of both
50 * cases. Since re_exec() doesn't exhibit this behavior, we are stuck
51 * with this version. Furthermore, we need to be able to save and restore
52 * the state of the pattern matcher in order to do things "efficiently".
53 *
54 * The matching power of this algorithm isn't as powerful as the re_xxx()
55 * routines (no \(xxx\) and \n constructs). Such is life.
56 */
57
58 #define CCHR 2
59 #define CDOT 4
60 #define CCL 6
61 #define NCCL 8
62 #define CDOL 10
63 #define CEOF 11
64
65 #define STAR 01
66
67 #define LBSIZE 1024
68 #define ESIZE 1024
69
70
71 static char linebuf[LBSIZE + 1];
72 static char decoded_linebuf[LBSIZE + 1];
73
74 /* the magic array for case-independence */
75 static unsigned char cc[] = {
76 0000,0001,0002,0003,0004,0005,0006,0007,
77 0010,0011,0012,0013,0014,0015,0016,0017,
78 0020,0021,0022,0023,0024,0025,0026,0027,
79 0030,0031,0032,0033,0034,0035,0036,0037,
80 0040,0041,0042,0043,0044,0045,0046,0047,
81 0050,0051,0052,0053,0054,0055,0056,0057,
82 0060,0061,0062,0063,0064,0065,0066,0067,
83 0070,0071,0072,0073,0074,0075,0076,0077,
84 0100,0141,0142,0143,0144,0145,0146,0147,
85 0150,0151,0152,0153,0154,0155,0156,0157,
86 0160,0161,0162,0163,0164,0165,0166,0167,
87 0170,0171,0172,0133,0134,0135,0136,0137,
88 0140,0141,0142,0143,0144,0145,0146,0147,
89 0150,0151,0152,0153,0154,0155,0156,0157,
90 0160,0161,0162,0163,0164,0165,0166,0167,
91 0170,0171,0172,0173,0174,0175,0176,0177,
92
93 0200,0201,0202,0203,0204,0205,0206,0207,
94 0210,0211,0212,0213,0214,0215,0216,0217,
95 0220,0221,0222,0223,0224,0225,0226,0227,
96 0230,0231,0232,0233,0234,0235,0236,0237,
97 0240,0241,0242,0243,0244,0245,0246,0247,
98 0250,0251,0252,0253,0254,0255,0256,0257,
99 0260,0261,0262,0263,0264,0265,0266,0267,
100 0270,0271,0272,0273,0274,0275,0276,0277,
101 0300,0301,0302,0303,0304,0305,0306,0307,
102 0310,0311,0312,0313,0314,0315,0316,0317,
103 0320,0321,0322,0323,0324,0325,0326,0327,
104 0330,0331,0332,0333,0334,0335,0336,0337,
105 0340,0341,0342,0343,0344,0345,0346,0347,
106 0350,0351,0352,0353,0354,0355,0356,0357,
107 0360,0361,0362,0363,0364,0365,0366,0367,
108 0370,0371,0372,0373,0374,0375,0376,0377,
109 };
110
111 /*
112 * DEFINITIONS FOR NEXUS
113 */
114
115 #define nxtarg() (*argp ? *argp++ : NULL)
116 #define prvarg() argp--
117
118 #define padvise if (!talked++) advise
119
120 struct nexus {
121 int (*n_action)();
122
123 union {
124 /* for {OR,AND,NOT}action */
125 struct {
126 struct nexus *un_L_child;
127 struct nexus *un_R_child;
128 } st1;
129
130 /* for GREPaction */
131 struct {
132 int un_header;
133 int un_circf;
134 char un_expbuf[ESIZE];
135 char *un_patbuf;
136 } st2;
137
138 /* for TWSaction */
139 struct {
140 char *un_datef;
141 int un_after;
142 struct tws un_tws;
143 } st3;
144 } un;
145 };
146
147 #define n_L_child un.st1.un_L_child
148 #define n_R_child un.st1.un_R_child
149
150 #define n_header un.st2.un_header
151 #define n_circf un.st2.un_circf
152 #define n_expbuf un.st2.un_expbuf
153 #define n_patbuf un.st2.un_patbuf
154
155 #define n_datef un.st3.un_datef
156 #define n_after un.st3.un_after
157 #define n_tws un.st3.un_tws
158
159 static int talked;
160
161 static char *datesw;
162 static char **argp;
163
164 static struct nexus *head;
165
166 /*
167 * prototypes for date routines
168 */
169 static struct tws *tws_parse(char *, int);
170 static struct tws *tws_special(char *);
171
172 /*
173 * static prototypes
174 */
175 static void PRaction(struct nexus *, int);
176 static int gcompile(struct nexus *, char *);
177 static int advance(char *, char *);
178 static int cclass(unsigned char *, int, int);
179 static int tcompile(char *, struct tws *, int);
180
181 static struct nexus *parse(void);
182 static struct nexus *nexp1(void);
183 static struct nexus *nexp2(void);
184 static struct nexus *nexp3(void);
185 static struct nexus *newnexus(int (*)());
186
187 static int ORaction();
188 static int ANDaction();
189 static int NOTaction();
190 static int GREPaction();
191 static int TWSaction();
192
193
194 int
195 pcompile (char **vec, char *date)
196 {
197 argp = vec;
198 if ((datesw = date) == NULL)
199 datesw = "date";
200 talked = 0;
201
202 if ((head = parse ()) == NULL)
203 return (talked ? 0 : 1);
204
205 if (*argp) {
206 padvise (NULL, "%s unexpected", *argp);
207 return 0;
208 }
209
210 return 1;
211 }
212
213
214 static struct nexus *
215 parse (void)
216 {
217 register char *cp;
218 register struct nexus *n, *o;
219
220 if ((n = nexp1 ()) == NULL || (cp = nxtarg ()) == NULL)
221 return n;
222
223 if (*cp != '-') {
224 padvise (NULL, "%s unexpected", cp);
225 return NULL;
226 }
227
228 if (*++cp == '-')
229 goto header;
230 switch (smatch (cp, parswit)) {
231 case AMBIGSW:
232 ambigsw (cp, parswit);
233 talked++;
234 return NULL;
235 case UNKWNSW:
236 fprintf (stderr, "-%s unknown\n", cp);
237 talked++;
238 return NULL;
239
240 case PROR:
241 o = newnexus (ORaction);
242 o->n_L_child = n;
243 if ((o->n_R_child = parse ()))
244 return o;
245 padvise (NULL, "missing disjunctive");
246 free (o);
247 return NULL;
248
249 header: ;
250 default:
251 prvarg ();
252 return n;
253 }
254 }
255
256 static struct nexus *
257 nexp1 (void)
258 {
259 register char *cp;
260 register struct nexus *n, *o;
261
262 if ((n = nexp2 ()) == NULL || (cp = nxtarg ()) == NULL)
263 return n;
264
265 if (*cp != '-') {
266 padvise (NULL, "%s unexpected", cp);
267 free (n);
268 return NULL;
269 }
270
271 if (*++cp == '-')
272 goto header;
273 switch (smatch (cp, parswit)) {
274 case AMBIGSW:
275 ambigsw (cp, parswit);
276 talked++;
277 free (n);
278 return NULL;
279 case UNKWNSW:
280 fprintf (stderr, "-%s unknown\n", cp);
281 talked++;
282 free (n);
283 return NULL;
284
285 case PRAND:
286 o = newnexus (ANDaction);
287 o->n_L_child = n;
288 if ((o->n_R_child = nexp1 ()))
289 return o;
290 padvise (NULL, "missing conjunctive");
291 free (o);
292 return NULL;
293
294 header: ;
295 default:
296 prvarg ();
297 return n;
298 }
299 }
300
301
302 static struct nexus *
303 nexp2 (void)
304 {
305 register char *cp;
306 register struct nexus *n;
307
308 if ((cp = nxtarg ()) == NULL)
309 return NULL;
310
311 if (*cp != '-') {
312 prvarg ();
313 return nexp3 ();
314 }
315
316 if (*++cp == '-')
317 goto header;
318 switch (smatch (cp, parswit)) {
319 case AMBIGSW:
320 ambigsw (cp, parswit);
321 talked++;
322 return NULL;
323 case UNKWNSW:
324 fprintf (stderr, "-%s unknown\n", cp);
325 talked++;
326 return NULL;
327
328 case PRNOT:
329 n = newnexus (NOTaction);
330 if ((n->n_L_child = nexp3 ()))
331 return n;
332 padvise (NULL, "missing negation");
333 free (n);
334 return NULL;
335
336 header: ;
337 default:
338 prvarg ();
339 return nexp3 ();
340 }
341 }
342
343 static struct nexus *
344 nexp3 (void)
345 {
346 int i;
347 register char *cp, *dp;
348 char buffer[BUFSIZ], temp[64];
349 register struct nexus *n;
350
351 if ((cp = nxtarg ()) == NULL)
352 return NULL;
353
354 if (*cp != '-') {
355 padvise (NULL, "%s unexpected", cp);
356 return NULL;
357 }
358
359 if (*++cp == '-') {
360 dp = ++cp;
361 goto header;
362 }
363 switch (i = smatch (cp, parswit)) {
364 case AMBIGSW:
365 ambigsw (cp, parswit);
366 talked++;
367 return NULL;
368 case UNKWNSW:
369 fprintf (stderr, "-%s unknown\n", cp);
370 talked++;
371 return NULL;
372
373 case PRLBR:
374 if ((n = parse ()) == NULL) {
375 padvise (NULL, "missing group");
376 return NULL;
377 }
378 if ((cp = nxtarg ()) == NULL) {
379 padvise (NULL, "missing -rbrace");
380 return NULL;
381 }
382 if (*cp++ == '-' && smatch (cp, parswit) == PRRBR)
383 return n;
384 padvise (NULL, "%s unexpected", --cp);
385 return NULL;
386
387 default:
388 prvarg ();
389 return NULL;
390
391 case PRCC:
392 case PRDATE:
393 case PRFROM:
394 case PRTO:
395 case PRSUBJ:
396 strncpy(temp, parswit[i].sw, sizeof(temp));
397 temp[sizeof(temp) - 1] = '\0';
398 dp = *brkstring (temp, " ", NULL);
399 header: ;
400 if (!(cp = nxtarg ())) {/* allow -xyz arguments */
401 padvise (NULL, "missing argument to %s", argp[-2]);
402 return NULL;
403 }
404 n = newnexus (GREPaction);
405 n->n_header = 1;
406 snprintf (buffer, sizeof(buffer), "^%s[ \t]*:.*%s", dp, cp);
407 dp = buffer;
408 goto pattern;
409
410 case PRSRCH:
411 n = newnexus (GREPaction);
412 n->n_header = 0;
413 if (!(cp = nxtarg ())) {/* allow -xyz arguments */
414 padvise (NULL, "missing argument to %s", argp[-2]);
415 free (n);
416 return NULL;
417 }
418 dp = cp;
419 pattern: ;
420 if (!gcompile (n, dp)) {
421 padvise (NULL, "pattern error in %s %s", argp[-2], cp);
422 free (n);
423 return NULL;
424 }
425 n->n_patbuf = getcpy (dp);
426 return n;
427
428 case PROTHR:
429 padvise (NULL, "internal error!");
430 return NULL;
431
432 case PRDATF:
433 if (!(datesw = nxtarg ()) || *datesw == '-') {
434 padvise (NULL, "missing argument to %s", argp[-2]);
435 return NULL;
436 }
437 return nexp3 ();
438
439 case PRAFTR:
440 case PRBEFR:
441 if (!(cp = nxtarg ())) {/* allow -xyz arguments */
442 padvise (NULL, "missing argument to %s", argp[-2]);
443 return NULL;
444 }
445 n = newnexus (TWSaction);
446 n->n_datef = datesw;
447 if (!tcompile (cp, &n->n_tws, n->n_after = i == PRAFTR)) {
448 padvise (NULL, "unable to parse %s %s", argp[-2], cp);
449 free (n);
450 return NULL;
451 }
452 return n;
453 }
454 }
455
456
457 static struct nexus *
458 newnexus (int (*action)())
459 {
460 register struct nexus *p;
461
462 if ((p = (struct nexus *) mh_xcalloc ((size_t) 1, sizeof *p)) == NULL)
463 adios (NULL, "unable to allocate component storage");
464
465 p->n_action = action;
466 return p;
467 }
468
469
470 #define args(a) a, fp, msgnum, start, stop
471 #define params args (n)
472 #define plist \
473 register struct nexus *n; \
474 register FILE *fp; \
475 int msgnum; \
476 long start, \
477 stop;
478
479 int
480 pmatches (FILE *fp, int msgnum, long start, long stop, int debug)
481 {
482 if (!head)
483 return 1;
484
485 if (!talked++ && debug)
486 PRaction (head, 0);
487
488 return (*head->n_action) (args (head));
489 }
490
491
492 static void
493 PRaction (struct nexus *n, int level)
494 {
495 register int i;
496
497 for (i = 0; i < level; i++)
498 fprintf (stderr, "| ");
499
500 if (n->n_action == ORaction) {
501 fprintf (stderr, "OR\n");
502 PRaction (n->n_L_child, level + 1);
503 PRaction (n->n_R_child, level + 1);
504 return;
505 }
506 if (n->n_action == ANDaction) {
507 fprintf (stderr, "AND\n");
508 PRaction (n->n_L_child, level + 1);
509 PRaction (n->n_R_child, level + 1);
510 return;
511 }
512 if (n->n_action == NOTaction) {
513 fprintf (stderr, "NOT\n");
514 PRaction (n->n_L_child, level + 1);
515 return;
516 }
517 if (n->n_action == GREPaction) {
518 fprintf (stderr, "PATTERN(%s) %s\n",
519 n->n_header ? "header" : "body", n->n_patbuf);
520 return;
521 }
522 if (n->n_action == TWSaction) {
523 fprintf (stderr, "TEMPORAL(%s) %s: %s\n",
524 n->n_after ? "after" : "before", n->n_datef,
525 dasctime (&n->n_tws, TW_NULL));
526 return;
527 }
528 fprintf (stderr, "UNKNOWN(0x%x)\n",
529 (unsigned int)(unsigned long) (*n->n_action));
530 }
531
532
533 static int
534 ORaction (params)
535 plist
536 {
537 if ((*n->n_L_child->n_action) (args (n->n_L_child)))
538 return 1;
539 return (*n->n_R_child->n_action) (args (n->n_R_child));
540 }
541
542
543 static int
544 ANDaction (params)
545 plist
546 {
547 if (!(*n->n_L_child->n_action) (args (n->n_L_child)))
548 return 0;
549 return (*n->n_R_child->n_action) (args (n->n_R_child));
550 }
551
552
553 static int
554 NOTaction (params)
555 plist
556 {
557 return (!(*n->n_L_child->n_action) (args (n->n_L_child)));
558 }
559
560
561 static int
562 gcompile (struct nexus *n, char *astr)
563 {
564 register int c;
565 int cclcnt;
566 register unsigned char *ep, *dp, *sp, *lastep = 0;
567
568 dp = (ep = (unsigned char *) n->n_expbuf) + sizeof n->n_expbuf;
569 sp = (unsigned char *) astr;
570 if (*sp == '^') {
571 n->n_circf = 1;
572 sp++;
573 }
574 else
575 n->n_circf = 0;
576 for (;;) {
577 if (ep >= dp)
578 goto cerror;
579 if ((c = *sp++) != '*')
580 lastep = ep;
581 switch (c) {
582 case '\0':
583 *ep++ = CEOF;
584 return 1;
585
586 case '.':
587 *ep++ = CDOT;
588 continue;
589
590 case '*':
591 if (lastep == 0)
592 goto defchar;
593 *lastep |= STAR;
594 continue;
595
596 case '$':
597 if (*sp != '\0')
598 goto defchar;
599 *ep++ = CDOL;
600 continue;
601
602 case '[':
603 *ep++ = CCL;
604 *ep++ = 0;
605 cclcnt = 0;
606 if ((c = *sp++) == '^') {
607 c = *sp++;
608 ep[-2] = NCCL;
609 }
610 if (c == '-') {
611 *ep++ = c;
612 cclcnt++;
613 c = *sp++;
614 }
615 do {
616 if (c == '-' && *sp != '\0' && *sp != ']') {
617 for (c = ep[-1]+1; c < *sp; c++) {
618 *ep++ = c;
619 cclcnt++;
620 if (c == '\0' || ep >= dp)
621 goto cerror;
622 }
623 } else {
624 *ep++ = c;
625 cclcnt++;
626 if (c == '\0' || ep >= dp)
627 goto cerror;
628 }
629 } while ((c = *sp++) != ']');
630 if (cclcnt > 255)
631 goto cerror;
632 lastep[1] = cclcnt;
633 continue;
634
635 case '\\':
636 if ((c = *sp++) == '\0')
637 goto cerror;
638 defchar:
639 default:
640 *ep++ = CCHR;
641 *ep++ = c;
642 }
643 }
644
645 cerror: ;
646 return 0;
647 }
648
649
650 static int
651 GREPaction (params)
652 plist
653 {
654 int c, body, lf;
655 long pos = start;
656 register char *p1, *p2, *ebp, *cbp;
657 char ibuf[BUFSIZ];
658 NMH_UNUSED (msgnum);
659
660 fseek (fp, start, SEEK_SET);
661 body = 0;
662 ebp = cbp = ibuf;
663 for (;;) {
664 if (body && n->n_header)
665 return 0;
666 p1 = linebuf;
667 p2 = cbp;
668 lf = 0;
669 for (;;) {
670 if (p2 >= ebp) {
671 if (fgets (ibuf, sizeof ibuf, fp) == NULL
672 || (stop && pos >= stop)) {
673 if (lf)
674 break;
675 return 0;
676 }
677 pos += (long) strlen (ibuf);
678 p2 = ibuf;
679 ebp = ibuf + strlen (ibuf);
680 }
681 c = *p2++;
682 if (lf && c != '\n') {
683 if (c != ' ' && c != '\t') {
684 --p2;
685 break;
686 }
687 else
688 lf = 0;
689 }
690 if (c == '\n') {
691 if (body)
692 break;
693 else {
694 if (lf) {
695 body++;
696 break;
697 }
698 lf++;
699 /* Unfold by skipping the newline. */
700 c = 0;
701 }
702 }
703 if (c && p1 < &linebuf[LBSIZE - 1])
704 *p1++ = c;
705 }
706
707 *p1++ = 0;
708 cbp = p2;
709 p1 = linebuf;
710 p2 = n->n_expbuf;
711
712 /* Attempt to decode as a MIME header. If it's the last header,
713 body will be 1 and lf will be at least 1. */
714 if ((body == 0 || lf > 0) &&
715 decode_rfc2047 (linebuf, decoded_linebuf, sizeof decoded_linebuf)) {
716 p1 = decoded_linebuf;
717 }
718
719 if (n->n_circf) {
720 if (advance (p1, p2))
721 return 1;
722 continue;
723 }
724
725 if (*p2 == CCHR) {
726 c = p2[1];
727 do {
728 if (*p1 == c || cc[(unsigned char)*p1] == c)
729 if (advance (p1, p2))
730 return 1;
731 } while (*p1++);
732 continue;
733 }
734
735 do {
736 if (advance (p1, p2))
737 return 1;
738 } while (*p1++);
739 }
740 }
741
742
743 static int
744 advance (char *alp, char *aep)
745 {
746 register unsigned char *lp, *ep, *curlp;
747
748 lp = (unsigned char *)alp;
749 ep = (unsigned char *)aep;
750 for (;;)
751 switch (*ep++) {
752 case CCHR:
753 if (*ep++ == *lp++ || ep[-1] == cc[lp[-1]])
754 continue;
755 return 0;
756
757 case CDOT:
758 if (*lp++)
759 continue;
760 return 0;
761
762 case CDOL:
763 if (*lp == 0)
764 continue;
765 return 0;
766
767 case CEOF:
768 return 1;
769
770 case CCL:
771 if (cclass (ep, *lp++, 1)) {
772 ep += *ep + 1;
773 continue;
774 }
775 return 0;
776
777 case NCCL:
778 if (cclass (ep, *lp++, 0)) {
779 ep += *ep + 1;
780 continue;
781 }
782 return 0;
783
784 case CDOT | STAR:
785 curlp = lp;
786 while (*lp++)
787 continue;
788 goto star;
789
790 case CCHR | STAR:
791 curlp = lp;
792 while (*lp++ == *ep || cc[lp[-1]] == *ep)
793 continue;
794 ep++;
795 goto star;
796
797 case CCL | STAR:
798 case NCCL | STAR:
799 curlp = lp;
800 while (cclass (ep, *lp++, ep[-1] == (CCL | STAR)))
801 continue;
802 ep += *ep + 1;
803 goto star;
804
805 star:
806 do {
807 lp--;
808 if (advance ((char *) lp, (char *) ep))
809 return (1);
810 } while (lp > curlp);
811 return 0;
812
813 default:
814 admonish (NULL, "advance() botch -- you lose big");
815 return 0;
816 }
817 }
818
819
820 static int
821 cclass (unsigned char *aset, int ac, int af)
822 {
823 register unsigned int n;
824 register unsigned char c, *set;
825
826 set = aset;
827 if ((c = ac) == 0)
828 return (0);
829
830 n = *set++;
831 while (n--)
832 if (*set++ == c || set[-1] == cc[c])
833 return (af);
834
835 return (!af);
836 }
837
838
839 static int
840 tcompile (char *ap, struct tws *tb, int isafter)
841 {
842 register struct tws *tw;
843
844 if ((tw = tws_parse (ap, isafter)) == NULL)
845 return 0;
846
847 twscopy (tb, tw);
848 return 1;
849 }
850
851
852 static struct tws *
853 tws_parse (char *ap, int isafter)
854 {
855 char buffer[BUFSIZ];
856 register struct tws *tw, *ts;
857
858 if ((tw = tws_special (ap)) != NULL) {
859 tw->tw_sec = tw->tw_min = isafter ? 59 : 0;
860 tw->tw_hour = isafter ? 23 : 0;
861 return tw;
862 }
863 if ((tw = dparsetime (ap)) != NULL)
864 return tw;
865
866 if ((ts = dlocaltimenow ()) == NULL)
867 return NULL;
868
869 snprintf (buffer, sizeof(buffer), "%s %s", ap, dtwszone (ts));
870 if ((tw = dparsetime (buffer)) != NULL)
871 return tw;
872
873 snprintf (buffer, sizeof(buffer), "%s %02d:%02d:%02d %s", ap,
874 ts->tw_hour, ts->tw_min, ts->tw_sec, dtwszone (ts));
875 if ((tw = dparsetime (buffer)) != NULL)
876 return tw;
877
878 snprintf (buffer, sizeof(buffer), "%02d %s %04d %s",
879 ts->tw_mday, tw_moty[ts->tw_mon], ts->tw_year, ap);
880 if ((tw = dparsetime (buffer)) != NULL)
881 return tw;
882
883 snprintf (buffer, sizeof(buffer), "%02d %s %04d %s %s",
884 ts->tw_mday, tw_moty[ts->tw_mon], ts->tw_year,
885 ap, dtwszone (ts));
886 if ((tw = dparsetime (buffer)) != NULL)
887 return tw;
888
889 return NULL;
890 }
891
892
893 static struct tws *
894 tws_special (char *ap)
895 {
896 int i;
897 time_t clock;
898 register struct tws *tw;
899
900 time (&clock);
901 if (!strcasecmp (ap, "today"))
902 return dlocaltime (&clock);
903 if (!strcasecmp (ap, "yesterday")) {
904 clock -= (long) (60 * 60 * 24);
905 return dlocaltime (&clock);
906 }
907 if (!strcasecmp (ap, "tomorrow")) {
908 clock += (long) (60 * 60 * 24);
909 return dlocaltime (&clock);
910 }
911
912 for (i = 0; tw_ldotw[i]; i++)
913 if (!strcasecmp (ap, tw_ldotw[i]))
914 break;
915 if (tw_ldotw[i]) {
916 if ((tw = dlocaltime (&clock)) == NULL)
917 return NULL;
918 if ((i -= tw->tw_wday) > 0)
919 i -= 7;
920 }
921 else
922 if (*ap != '-')
923 return NULL;
924 else /* -ddd days ago */
925 i = atoi (ap); /* we should error check this */
926
927 clock += (long) ((60 * 60 * 24) * i);
928 return dlocaltime (&clock);
929 }
930
931
932 static int
933 TWSaction (params)
934 plist
935 {
936 int state;
937 register char *bp;
938 char buf[BUFSIZ], name[NAMESZ];
939 register struct tws *tw;
940 m_getfld_state_t gstate = 0;
941 NMH_UNUSED (stop);
942
943 fseek (fp, start, SEEK_SET);
944 for (bp = NULL;;) {
945 int bufsz = sizeof buf;
946 switch (state = m_getfld (&gstate, name, buf, &bufsz, fp)) {
947 case FLD:
948 case FLDPLUS:
949 if (bp != NULL)
950 free (bp), bp = NULL;
951 bp = add (buf, NULL);
952 while (state == FLDPLUS) {
953 bufsz = sizeof buf;
954 state = m_getfld (&gstate, name, buf, &bufsz, fp);
955 bp = add (buf, bp);
956 }
957 if (!strcasecmp (name, n->n_datef))
958 break;
959 continue;
960
961 case BODY:
962 case FILEEOF:
963 case LENERR:
964 case FMTERR:
965 if (state == LENERR || state == FMTERR)
966 advise (NULL, "format error in message %d", msgnum);
967 if (bp != NULL)
968 free (bp);
969 return 0;
970
971 default:
972 adios (NULL, "internal error -- you lose");
973 }
974 break;
975 }
976 m_getfld_state_destroy (&gstate);
977
978 if ((tw = dparsetime (bp)) == NULL)
979 advise (NULL, "unable to parse %s field in message %d, matching...",
980 n->n_datef, msgnum), state = 1;
981 else
982 state = n->n_after ? (twsort (tw, &n->n_tws) > 0)
983 : (twsort (tw, &n->n_tws) < 0);
984
985 if (bp != NULL)
986 free (bp);
987 return state;
988 }