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