]> diplodocus.org Git - nmh/blob - uip/picksbr.c
lock_file.c: close(2) file descriptor on failure, avoiding leak.
[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)(struct nexus *n, FILE *fp, int msgnum, long start, long stop);
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 (*action)(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(%#llx)\n", (unsigned long long)n->n_action);
523 }
524
525
526 static int
527 ORaction(struct nexus *n, FILE *fp, int msgnum, long start, long stop)
528 {
529 if ((*n->n_L_child->n_action)(n->n_L_child, fp, msgnum, start, stop))
530 return 1;
531 return (*n->n_R_child->n_action)(n->n_R_child, fp, msgnum, start, stop);
532 }
533
534
535 static int
536 ANDaction(struct nexus *n, FILE *fp, int msgnum, long start, long stop)
537 {
538 if (!(*n->n_L_child->n_action)(n->n_L_child, fp, msgnum, start, stop))
539 return 0;
540 return (*n->n_R_child->n_action)(n->n_R_child, fp, msgnum, start, stop);
541 }
542
543
544 static int
545 NOTaction(struct nexus *n, FILE *fp, int msgnum, long start, long stop)
546 {
547 return (!(*n->n_L_child->n_action)(n->n_L_child, fp, msgnum, start, stop));
548 }
549
550
551 static int
552 gcompile (struct nexus *n, char *astr)
553 {
554 int c;
555 int cclcnt;
556 unsigned char *ep, *dp, *sp, *lastep = 0;
557
558 dp = (ep = (unsigned char *) n->n_expbuf) + sizeof n->n_expbuf;
559 sp = (unsigned char *) astr;
560 if (*sp == '^') {
561 n->n_circf = 1;
562 sp++;
563 }
564 else
565 n->n_circf = 0;
566 for (;;) {
567 if (ep >= dp)
568 goto cerror;
569 if ((c = *sp++) != '*')
570 lastep = ep;
571 switch (c) {
572 case '\0':
573 *ep++ = CEOF;
574 return 1;
575
576 case '.':
577 *ep++ = CDOT;
578 continue;
579
580 case '*':
581 if (lastep == 0)
582 goto defchar;
583 *lastep |= STAR;
584 continue;
585
586 case '$':
587 if (*sp != '\0')
588 goto defchar;
589 *ep++ = CDOL;
590 continue;
591
592 case '[':
593 *ep++ = CCL;
594 *ep++ = 0;
595 cclcnt = 0;
596 if ((c = *sp++) == '^') {
597 c = *sp++;
598 ep[-2] = NCCL;
599 }
600 if (c == '-') {
601 *ep++ = c;
602 cclcnt++;
603 c = *sp++;
604 }
605 do {
606 if (c == '-' && *sp != '\0' && *sp != ']') {
607 for (c = ep[-1]+1; c < *sp; c++) {
608 *ep++ = c;
609 cclcnt++;
610 if (c == '\0' || ep >= dp)
611 goto cerror;
612 }
613 } else {
614 *ep++ = c;
615 cclcnt++;
616 if (c == '\0' || ep >= dp)
617 goto cerror;
618 }
619 } while ((c = *sp++) != ']');
620 if (cclcnt > 255)
621 goto cerror;
622 lastep[1] = cclcnt;
623 continue;
624
625 case '\\':
626 if ((c = *sp++) == '\0')
627 goto cerror;
628 /* FALLTHRU */
629 defchar:
630 default:
631 *ep++ = CCHR;
632 *ep++ = c;
633 }
634 }
635
636 cerror: ;
637 return 0;
638 }
639
640
641 static int
642 GREPaction(struct nexus *n, FILE *fp, int msgnum, long start, long stop)
643 {
644 int c, body, lf;
645 long pos = start;
646 char *p1, *p2, *ebp, *cbp;
647 char ibuf[BUFSIZ];
648 NMH_UNUSED (msgnum);
649
650 fseek (fp, start, SEEK_SET);
651 body = 0;
652 ebp = cbp = ibuf;
653 for (;;) {
654 if (body && n->n_header)
655 return 0;
656 p1 = linebuf;
657 p2 = cbp;
658 lf = 0;
659 for (;;) {
660 if (p2 >= ebp) {
661 if (fgets (ibuf, sizeof ibuf, fp) == NULL
662 || (stop && pos >= stop)) {
663 if (lf)
664 break;
665 return 0;
666 }
667 pos += (long) strlen (ibuf);
668 p2 = ibuf;
669 ebp = ibuf + strlen (ibuf);
670 }
671 c = *p2++;
672 if (lf && c != '\n') {
673 if (c != ' ' && c != '\t') {
674 --p2;
675 break;
676 }
677 lf = 0;
678 }
679 if (c == '\n') {
680 if (body)
681 break;
682 if (lf) {
683 body++;
684 break;
685 }
686 lf++;
687 /* Unfold by skipping the newline. */
688 c = 0;
689 }
690 if (c && p1 < &linebuf[LBSIZE - 1])
691 *p1++ = c;
692 }
693
694 *p1++ = 0;
695 cbp = p2;
696 p1 = linebuf;
697 p2 = n->n_expbuf;
698
699 /* Attempt to decode as a MIME header. If it's the last header,
700 body will be 1 and lf will be at least 1. */
701 if ((body == 0 || lf > 0) &&
702 decode_rfc2047 (linebuf, decoded_linebuf, sizeof decoded_linebuf)) {
703 p1 = decoded_linebuf;
704 }
705
706 if (n->n_circf) {
707 if (advance (p1, p2))
708 return 1;
709 continue;
710 }
711
712 if (*p2 == CCHR) {
713 c = p2[1];
714 do {
715 if (*p1 == c || cc[(unsigned char)*p1] == c)
716 if (advance (p1, p2))
717 return 1;
718 } while (*p1++);
719 continue;
720 }
721
722 do {
723 if (advance (p1, p2))
724 return 1;
725 } while (*p1++);
726 }
727 }
728
729
730 static int
731 advance (char *alp, char *aep)
732 {
733 unsigned char *lp, *ep, *curlp;
734
735 lp = (unsigned char *)alp;
736 ep = (unsigned char *)aep;
737 for (;;)
738 switch (*ep++) {
739 case CCHR:
740 if (*ep++ == *lp++ || ep[-1] == cc[lp[-1]])
741 continue;
742 return 0;
743
744 case CDOT:
745 if (*lp++)
746 continue;
747 return 0;
748
749 case CDOL:
750 if (*lp == 0)
751 continue;
752 return 0;
753
754 case CEOF:
755 return 1;
756
757 case CCL:
758 if (cclass (ep, *lp++, 1)) {
759 ep += *ep + 1;
760 continue;
761 }
762 return 0;
763
764 case NCCL:
765 if (cclass (ep, *lp++, 0)) {
766 ep += *ep + 1;
767 continue;
768 }
769 return 0;
770
771 case CDOT | STAR:
772 curlp = lp;
773 while (*lp++)
774 continue;
775 goto star;
776
777 case CCHR | STAR:
778 curlp = lp;
779 while (*lp++ == *ep || cc[lp[-1]] == *ep)
780 continue;
781 ep++;
782 goto star;
783
784 case CCL | STAR:
785 case NCCL | STAR:
786 curlp = lp;
787 while (cclass (ep, *lp++, ep[-1] == (CCL | STAR)))
788 continue;
789 ep += *ep + 1;
790 goto star;
791
792 star:
793 do {
794 lp--;
795 if (advance ((char *) lp, (char *) ep))
796 return (1);
797 } while (lp > curlp);
798 return 0;
799
800 default:
801 inform("advance() botch -- you lose big, continuing...");
802 return 0;
803 }
804 }
805
806
807 static int
808 cclass (unsigned char *aset, int ac, int af)
809 {
810 unsigned int n;
811 unsigned char c, *set;
812
813 set = aset;
814 if ((c = ac) == 0)
815 return (0);
816
817 n = *set++;
818 while (n--)
819 if (*set++ == c || set[-1] == cc[c])
820 return (af);
821
822 return (!af);
823 }
824
825
826 static int
827 tcompile (char *ap, struct tws *tb, int isafter)
828 {
829 struct tws *tw;
830
831 if ((tw = tws_parse (ap, isafter)) == NULL)
832 return 0;
833
834 *tb = *tw;
835 return 1;
836 }
837
838
839 static struct tws *
840 tws_parse (char *ap, int isafter)
841 {
842 char buffer[BUFSIZ];
843 struct tws *tw, *ts;
844
845 if ((tw = tws_special (ap)) != NULL) {
846 tw->tw_sec = tw->tw_min = isafter ? 59 : 0;
847 tw->tw_hour = isafter ? 23 : 0;
848 return tw;
849 }
850 if ((tw = dparsetime (ap)) != NULL)
851 return tw;
852
853 if ((ts = dlocaltimenow ()) == NULL)
854 return NULL;
855
856 snprintf (buffer, sizeof(buffer), "%s %s", ap, dtwszone (ts));
857 if ((tw = dparsetime (buffer)) != NULL)
858 return tw;
859
860 snprintf (buffer, sizeof(buffer), "%s %02d:%02d:%02d %s", ap,
861 ts->tw_hour, ts->tw_min, ts->tw_sec, dtwszone (ts));
862 if ((tw = dparsetime (buffer)) != NULL)
863 return tw;
864
865 snprintf (buffer, sizeof(buffer), "%02d %s %04d %s",
866 ts->tw_mday, tw_moty[ts->tw_mon], ts->tw_year, ap);
867 if ((tw = dparsetime (buffer)) != NULL)
868 return tw;
869
870 snprintf (buffer, sizeof(buffer), "%02d %s %04d %s %s",
871 ts->tw_mday, tw_moty[ts->tw_mon], ts->tw_year,
872 ap, dtwszone (ts));
873 if ((tw = dparsetime (buffer)) != NULL)
874 return tw;
875
876 return NULL;
877 }
878
879
880 static struct tws *
881 tws_special (char *ap)
882 {
883 int i;
884 time_t clock;
885 struct tws *tw;
886
887 time (&clock);
888 if (!strcasecmp (ap, "today"))
889 return dlocaltime (&clock);
890 if (!strcasecmp (ap, "yesterday")) {
891 clock -= (long) (60 * 60 * 24);
892 return dlocaltime (&clock);
893 }
894 if (!strcasecmp (ap, "tomorrow")) {
895 clock += (long) (60 * 60 * 24);
896 return dlocaltime (&clock);
897 }
898
899 for (i = 0; tw_ldotw[i]; i++)
900 if (!strcasecmp (ap, tw_ldotw[i]))
901 break;
902 if (tw_ldotw[i]) {
903 if ((tw = dlocaltime (&clock)) == NULL)
904 return NULL;
905 if ((i -= tw->tw_wday) > 0)
906 i -= 7;
907 }
908 else {
909 if (*ap != '-')
910 return NULL;
911 /* -ddd days ago */
912 i = atoi (ap); /* we should error check this */
913 }
914
915 clock += (long) ((60 * 60 * 24) * i);
916 return dlocaltime (&clock);
917 }
918
919
920 static int
921 TWSaction(struct nexus *n, FILE *fp, int msgnum, long start, long stop)
922 {
923 int state;
924 char *bp;
925 char buf[NMH_BUFSIZ], name[NAMESZ];
926 struct tws *tw;
927 m_getfld_state_t gstate;
928 NMH_UNUSED (stop);
929
930 fseek (fp, start, SEEK_SET);
931 gstate = m_getfld_state_init(fp);
932 for (bp = NULL;;) {
933 int bufsz = sizeof buf;
934 switch (state = m_getfld2(&gstate, name, buf, &bufsz)) {
935 case FLD:
936 case FLDPLUS:
937 free(bp);
938 bp = mh_xstrdup(buf);
939 while (state == FLDPLUS) {
940 bufsz = sizeof buf;
941 state = m_getfld2(&gstate, name, buf, &bufsz);
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 free(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 free(bp);
972 return state;
973 }