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