3 * picksbr.c -- routines to help pick along...
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
14 #include <h/picksbr.h>
17 #ifdef TIME_WITH_SYS_TIME
18 # include <sys/time.h>
21 # ifdef TM_IN_SYS_TIME
22 # include <sys/time.h>
28 static struct swit parswit[] = {
42 { "date pattern", 0 },
44 { "from pattern", 0 },
46 { "search pattern", 0 },
48 { "subject pattern", 0 },
52 { "-othercomponent pattern", 15 },
58 { "datefield field", 5 },
62 /* DEFINITIONS FOR PATTERN MATCHING */
65 * We really should be using re_comp() and re_exec() here. Unfortunately,
66 * pick advertises that lowercase characters matches characters of both
67 * cases. Since re_exec() doesn't exhibit this behavior, we are stuck
68 * with this version. Furthermore, we need to be able to save and restore
69 * the state of the pattern matcher in order to do things "efficiently".
71 * The matching power of this algorithm isn't as powerful as the re_xxx()
72 * routines (no \(xxx\) and \n constructs). Such is life.
88 static char linebuf[LBSIZE + 1];
90 /* the magic array for case-independence */
92 0000,0001,0002,0003,0004,0005,0006,0007,
93 0010,0011,0012,0013,0014,0015,0016,0017,
94 0020,0021,0022,0023,0024,0025,0026,0027,
95 0030,0031,0032,0033,0034,0035,0036,0037,
96 0040,0041,0042,0043,0044,0045,0046,0047,
97 0050,0051,0052,0053,0054,0055,0056,0057,
98 0060,0061,0062,0063,0064,0065,0066,0067,
99 0070,0071,0072,0073,0074,0075,0076,0077,
100 0100,0141,0142,0143,0144,0145,0146,0147,
101 0150,0151,0152,0153,0154,0155,0156,0157,
102 0160,0161,0162,0163,0164,0165,0166,0167,
103 0170,0171,0172,0133,0134,0135,0136,0137,
104 0140,0141,0142,0143,0144,0145,0146,0147,
105 0150,0151,0152,0153,0154,0155,0156,0157,
106 0160,0161,0162,0163,0164,0165,0166,0167,
107 0170,0171,0172,0173,0174,0175,0176,0177,
111 * DEFINITIONS FOR NEXUS
114 #define nxtarg() (*argp ? *argp++ : NULL)
115 #define prvarg() argp--
117 #define padvise if (!talked++) advise
123 /* for {OR,AND,NOT}action */
125 struct nexus *un_L_child;
126 struct nexus *un_R_child;
133 char un_expbuf[ESIZE];
146 #define n_L_child un.st1.un_L_child
147 #define n_R_child un.st1.un_R_child
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
154 #define n_datef un.st3.un_datef
155 #define n_after un.st3.un_after
156 #define n_tws un.st3.un_tws
159 static int pdebug = 0;
164 static struct nexus *head;
167 * prototypes for date routines
169 static struct tws *tws_parse();
170 static struct tws *tws_special();
175 static void PRaction();
176 static int gcompile();
177 static int advance();
179 static int tcompile();
181 static struct nexus *parse();
182 static struct nexus *exp1();
183 static struct nexus *exp2();
184 static struct nexus *exp3();
185 static struct nexus *newnexus();
187 static int ORaction();
188 static int ANDaction();
189 static int NOTaction();
190 static int GREPaction();
191 static int TWSaction();
195 pcompile (char **vec, char *date)
199 if ((cp = getenv ("MHPDEBUG")) && *cp)
203 if ((datesw = date) == NULL)
207 if ((head = parse ()) == NULL)
208 return (talked ? 0 : 1);
211 padvise (NULL, "%s unexpected", *argp);
219 static struct nexus *
223 register struct nexus *n, *o;
225 if ((n = exp1 ()) == NULL || (cp = nxtarg ()) == NULL)
229 padvise (NULL, "%s unexpected", cp);
235 switch (smatch (cp, parswit)) {
237 ambigsw (cp, parswit);
241 fprintf (stderr, "-%s unknown\n", cp);
246 o = newnexus (ORaction);
248 if ((o->n_R_child = parse ()))
250 padvise (NULL, "missing disjunctive");
260 static struct nexus *
264 register struct nexus *n, *o;
266 if ((n = exp2 ()) == NULL || (cp = nxtarg ()) == NULL)
270 padvise (NULL, "%s unexpected", cp);
276 switch (smatch (cp, parswit)) {
278 ambigsw (cp, parswit);
282 fprintf (stderr, "-%s unknown\n", cp);
287 o = newnexus (ANDaction);
289 if ((o->n_R_child = exp1 ()))
291 padvise (NULL, "missing conjunctive");
302 static struct nexus *
306 register struct nexus *n;
308 if ((cp = nxtarg ()) == NULL)
318 switch (smatch (cp, parswit)) {
320 ambigsw (cp, parswit);
324 fprintf (stderr, "-%s unknown\n", cp);
329 n = newnexus (NOTaction);
330 if ((n->n_L_child = exp3 ()))
332 padvise (NULL, "missing negation");
342 static struct nexus *
346 register char *cp, *dp;
347 char buffer[BUFSIZ], temp[64];
348 register struct nexus *n;
350 if ((cp = nxtarg ()) == NULL)
354 padvise (NULL, "%s unexpected", cp);
362 switch (i = smatch (cp, parswit)) {
364 ambigsw (cp, parswit);
368 fprintf (stderr, "-%s unknown\n", cp);
373 if ((n = parse ()) == NULL) {
374 padvise (NULL, "missing group");
377 if ((cp = nxtarg ()) == NULL) {
378 padvise (NULL, "missing -rbrace");
381 if (*cp++ == '-' && smatch (cp, parswit) == PRRBR)
383 padvise (NULL, "%s unexpected", --cp);
395 strncpy(temp, parswit[i].sw, sizeof(temp));
396 temp[sizeof(temp) - 1] = '\0';
397 dp = *brkstring (temp, " ", NULL);
399 if (!(cp = nxtarg ())) {/* allow -xyz arguments */
400 padvise (NULL, "missing argument to %s", argp[-2]);
403 n = newnexus (GREPaction);
405 snprintf (buffer, sizeof(buffer), "^%s[ \t]*:.*%s", dp, cp);
410 n = newnexus (GREPaction);
412 if (!(cp = nxtarg ())) {/* allow -xyz arguments */
413 padvise (NULL, "missing argument to %s", argp[-2]);
418 if (!gcompile (n, dp)) {
419 padvise (NULL, "pattern error in %s %s", argp[-2], cp);
422 n->n_patbuf = getcpy (dp);
426 padvise (NULL, "internal error!");
430 if (!(datesw = nxtarg ()) || *datesw == '-') {
431 padvise (NULL, "missing argument to %s", argp[-2]);
438 if (!(cp = nxtarg ())) {/* allow -xyz arguments */
439 padvise (NULL, "missing argument to %s", argp[-2]);
442 n = newnexus (TWSaction);
444 if (!tcompile (cp, &n->n_tws, n->n_after = i == PRAFTR)) {
445 padvise (NULL, "unable to parse %s %s", argp[-2], cp);
453 static struct nexus *
454 newnexus (int (*action)())
456 register struct nexus *p;
458 if ((p = (struct nexus *) calloc ((size_t) 1, sizeof *p)) == NULL)
459 adios (NULL, "unable to allocate component storage");
461 p->n_action = action;
466 #define args(a) a, fp, msgnum, start, stop
467 #define params args (n)
469 register struct nexus *n; \
476 pmatches (FILE *fp, int msgnum, long start, long stop)
481 if (!talked++ && pdebug)
484 return (*head->n_action) (args (head));
489 PRaction (struct nexus *n, int level)
493 for (i = 0; i < level; i++)
494 fprintf (stderr, "| ");
496 if (n->n_action == ORaction) {
497 fprintf (stderr, "OR\n");
498 PRaction (n->n_L_child, level + 1);
499 PRaction (n->n_R_child, level + 1);
502 if (n->n_action == ANDaction) {
503 fprintf (stderr, "AND\n");
504 PRaction (n->n_L_child, level + 1);
505 PRaction (n->n_R_child, level + 1);
508 if (n->n_action == NOTaction) {
509 fprintf (stderr, "NOT\n");
510 PRaction (n->n_L_child, level + 1);
513 if (n->n_action == GREPaction) {
514 fprintf (stderr, "PATTERN(%s) %s\n",
515 n->n_header ? "header" : "body", n->n_patbuf);
518 if (n->n_action == TWSaction) {
519 fprintf (stderr, "TEMPORAL(%s) %s: %s\n",
520 n->n_after ? "after" : "before", n->n_datef,
521 dasctime (&n->n_tws, TW_NULL));
524 fprintf (stderr, "UNKNOWN(0x%x)\n", (unsigned int) (*n->n_action));
532 if ((*n->n_L_child->n_action) (args (n->n_L_child)))
534 return (*n->n_R_child->n_action) (args (n->n_R_child));
542 if (!(*n->n_L_child->n_action) (args (n->n_L_child)))
544 return (*n->n_R_child->n_action) (args (n->n_R_child));
552 return (!(*n->n_L_child->n_action) (args (n->n_L_child)));
557 gcompile (struct nexus *n, char *astr)
561 register char *ep, *dp, *sp, *lastep;
563 dp = (ep = n->n_expbuf) + sizeof n->n_expbuf;
574 if ((c = *sp++) != '*')
601 if ((c = *sp++) == '^') {
608 if (c == '\0' || ep >= dp)
610 } while ((c = *sp++) != ']');
615 if ((c = *sp++) == '\0')
635 register char *p1, *p2, *ebp, *cbp;
638 fseek (fp, start, SEEK_SET);
642 if (body && n->n_header)
649 if (fgets (ibuf, sizeof ibuf, fp) == NULL
650 || (stop && pos >= stop)) {
655 pos += (long) strlen (ibuf);
657 ebp = ibuf + strlen (ibuf);
660 if (lf && c != '\n') {
661 if (c != ' ' && c != '\t') {
680 if (c && p1 < &linebuf[LBSIZE - 1])
690 if (advance (p1, p2))
698 if (*p1 == c || cc[(unsigned char)*p1] == c)
699 if (advance (p1, p2))
706 if (advance (p1, p2))
714 advance (char *alp, char *aep)
716 register char *lp, *ep, *curlp;
723 if (*ep++ == *lp++ || ep[-1] == cc[(unsigned char)lp[-1]])
741 if (cclass (ep, *lp++, 1)) {
748 if (cclass (ep, *lp++, 0)) {
762 while (*lp++ == *ep || cc[(unsigned char)lp[-1]] == *ep)
770 while (cclass (ep, *lp++, ep[-1] == (CCL | STAR)))
778 if (advance (lp, ep))
780 } while (lp > curlp);
784 admonish (NULL, "advance() botch -- you lose big");
791 cclass (char *aset, int ac, int af)
811 tcompile (char *ap, struct tws *tb, int isafter)
813 register struct tws *tw;
815 if ((tw = tws_parse (ap, isafter)) == NULL)
824 tws_parse (char *ap, int isafter)
827 register struct tws *tw, *ts;
829 if ((tw = tws_special (ap)) != NULL) {
830 tw->tw_sec = tw->tw_min = isafter ? 59 : 0;
831 tw->tw_hour = isafter ? 23 : 0;
834 if ((tw = dparsetime (ap)) != NULL)
837 if ((ts = dlocaltimenow ()) == NULL)
840 snprintf (buffer, sizeof(buffer), "%s %s", ap, dtwszone (ts));
841 if ((tw = dparsetime (buffer)) != NULL)
844 snprintf (buffer, sizeof(buffer), "%s %02d:%02d:%02d %s", ap,
845 ts->tw_hour, ts->tw_min, ts->tw_sec, dtwszone (ts));
846 if ((tw = dparsetime (buffer)) != NULL)
849 snprintf (buffer, sizeof(buffer), "%02d %s %04d %s",
850 ts->tw_mday, tw_moty[ts->tw_mon], ts->tw_year, ap);
851 if ((tw = dparsetime (buffer)) != NULL)
854 snprintf (buffer, sizeof(buffer), "%02d %s %04d %s %s",
855 ts->tw_mday, tw_moty[ts->tw_mon], ts->tw_year,
857 if ((tw = dparsetime (buffer)) != NULL)
865 tws_special (char *ap)
869 register struct tws *tw;
872 if (!strcasecmp (ap, "today"))
873 return dlocaltime (&clock);
874 if (!strcasecmp (ap, "yesterday")) {
875 clock -= (long) (60 * 60 * 24);
876 return dlocaltime (&clock);
878 if (!strcasecmp (ap, "tomorrow")) {
879 clock += (long) (60 * 60 * 24);
880 return dlocaltime (&clock);
883 for (i = 0; tw_ldotw[i]; i++)
884 if (!strcasecmp (ap, tw_ldotw[i]))
887 if ((tw = dlocaltime (&clock)) == NULL)
889 if ((i -= tw->tw_wday) > 0)
895 else /* -ddd days ago */
896 i = atoi (ap); /* we should error check this */
898 clock += (long) ((60 * 60 * 24) * i);
899 return dlocaltime (&clock);
909 char buf[BUFSIZ], name[NAMESZ];
910 register struct tws *tw;
912 fseek (fp, start, SEEK_SET);
913 for (state = FLD, bp = NULL;;) {
914 switch (state = m_getfld (state, name, buf, sizeof buf, fp)) {
919 free (bp), bp = NULL;
920 bp = add (buf, NULL);
921 while (state == FLDPLUS) {
922 state = m_getfld (state, name, buf, sizeof buf, fp);
925 if (!strcasecmp (name, n->n_datef))
935 if (state == LENERR || state == FMTERR)
936 advise (NULL, "format error in message %d", msgnum);
942 adios (NULL, "internal error -- you lose");
947 if ((tw = dparsetime (bp)) == NULL)
948 advise (NULL, "unable to parse %s field in message %d, matching...",
949 n->n_datef, msgnum), state = 1;
951 state = n->n_after ? (twsort (tw, &n->n_tws) > 0)
952 : (twsort (tw, &n->n_tws) < 0);