]>
diplodocus.org Git - nmh/blob - uip/picksbr.c
3 * picksbr.c -- routines to help pick along...
9 #include <zotnet/tws/tws.h>
10 #include <h/picksbr.h>
12 static struct swit parswit
[] = {
26 { "date pattern", 0 },
28 { "from pattern", 0 },
30 { "search pattern", 0 },
32 { "subject pattern", 0 },
36 { "-othercomponent pattern", 15 },
42 { "datefield field", 5 },
46 /* DEFINITIONS FOR PATTERN MATCHING */
49 * We really should be using re_comp() and re_exec() here. Unfortunately,
50 * pick advertises that lowercase characters matches characters of both
51 * cases. Since re_exec() doesn't exhibit this behavior, we are stuck
52 * with this version. Furthermore, we need to be able to save and restore
53 * the state of the pattern matcher in order to do things "efficiently".
55 * The matching power of this algorithm isn't as powerful as the re_xxx()
56 * routines (no \(xxx\) and \n constructs). Such is life.
72 static char linebuf
[LBSIZE
+ 1];
74 /* the magic array for case-independence */
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,
95 * DEFINITIONS FOR NEXUS
98 #define nxtarg() (*argp ? *argp++ : NULL)
99 #define prvarg() argp--
101 #define padvise if (!talked++) advise
107 /* for {OR,AND,NOT}action */
109 struct nexus
*un_L_child
;
110 struct nexus
*un_R_child
;
117 char un_expbuf
[ESIZE
];
130 #define n_L_child un.st1.un_L_child
131 #define n_R_child un.st1.un_R_child
133 #define n_header un.st2.un_header
134 #define n_circf un.st2.un_circf
135 #define n_expbuf un.st2.un_expbuf
136 #define n_patbuf un.st2.un_patbuf
138 #define n_datef un.st3.un_datef
139 #define n_after un.st3.un_after
140 #define n_tws un.st3.un_tws
143 static int pdebug
= 0;
148 static struct nexus
*head
;
151 * prototypes for date routines
153 static struct tws
*tws_parse();
154 static struct tws
*tws_special();
159 static void PRaction();
160 static int gcompile();
161 static int advance();
163 static int tcompile();
165 static struct nexus
*parse();
166 static struct nexus
*exp1();
167 static struct nexus
*exp2();
168 static struct nexus
*exp3();
169 static struct nexus
*newnexus();
171 static int ORaction();
172 static int ANDaction();
173 static int NOTaction();
174 static int GREPaction();
175 static int TWSaction();
179 pcompile (char **vec
, char *date
)
183 if ((cp
= getenv ("MHPDEBUG")) && *cp
)
187 if ((datesw
= date
) == NULL
)
191 if ((head
= parse ()) == NULL
)
192 return (talked
? 0 : 1);
195 padvise (NULL
, "%s unexpected", *argp
);
203 static struct nexus
*
207 register struct nexus
*n
, *o
;
209 if ((n
= exp1 ()) == NULL
|| (cp
= nxtarg ()) == NULL
)
213 padvise (NULL
, "%s unexpected", cp
);
219 switch (smatch (cp
, parswit
)) {
221 ambigsw (cp
, parswit
);
225 fprintf (stderr
, "-%s unknown\n", cp
);
230 o
= newnexus (ORaction
);
232 if ((o
->n_R_child
= parse ()))
234 padvise (NULL
, "missing disjunctive");
244 static struct nexus
*
248 register struct nexus
*n
, *o
;
250 if ((n
= exp2 ()) == NULL
|| (cp
= nxtarg ()) == NULL
)
254 padvise (NULL
, "%s unexpected", cp
);
260 switch (smatch (cp
, parswit
)) {
262 ambigsw (cp
, parswit
);
266 fprintf (stderr
, "-%s unknown\n", cp
);
271 o
= newnexus (ANDaction
);
273 if ((o
->n_R_child
= exp1 ()))
275 padvise (NULL
, "missing conjunctive");
286 static struct nexus
*
290 register struct nexus
*n
;
292 if ((cp
= nxtarg ()) == NULL
)
302 switch (smatch (cp
, parswit
)) {
304 ambigsw (cp
, parswit
);
308 fprintf (stderr
, "-%s unknown\n", cp
);
313 n
= newnexus (NOTaction
);
314 if ((n
->n_L_child
= exp3 ()))
316 padvise (NULL
, "missing negation");
326 static struct nexus
*
330 register char *cp
, *dp
;
331 char buffer
[BUFSIZ
], temp
[64];
332 register struct nexus
*n
;
334 if ((cp
= nxtarg ()) == NULL
)
338 padvise (NULL
, "%s unexpected", cp
);
346 switch (i
= smatch (cp
, parswit
)) {
348 ambigsw (cp
, parswit
);
352 fprintf (stderr
, "-%s unknown\n", cp
);
357 if ((n
= parse ()) == NULL
) {
358 padvise (NULL
, "missing group");
361 if ((cp
= nxtarg ()) == NULL
) {
362 padvise (NULL
, "missing -rbrace");
365 if (*cp
++ == '-' && smatch (cp
, parswit
) == PRRBR
)
367 padvise (NULL
, "%s unexpected", --cp
);
379 strncpy(temp
, parswit
[i
].sw
, sizeof(temp
));
380 temp
[sizeof(temp
) - 1] = '\0';
381 dp
= *brkstring (temp
, " ", NULL
);
383 if (!(cp
= nxtarg ())) {/* allow -xyz arguments */
384 padvise (NULL
, "missing argument to %s", argp
[-2]);
387 n
= newnexus (GREPaction
);
389 snprintf (buffer
, sizeof(buffer
), "^%s[ \t]*:.*%s", dp
, cp
);
394 n
= newnexus (GREPaction
);
396 if (!(cp
= nxtarg ())) {/* allow -xyz arguments */
397 padvise (NULL
, "missing argument to %s", argp
[-2]);
402 if (!gcompile (n
, dp
)) {
403 padvise (NULL
, "pattern error in %s %s", argp
[-2], cp
);
406 n
->n_patbuf
= getcpy (dp
);
410 padvise (NULL
, "internal error!");
414 if (!(datesw
= nxtarg ()) || *datesw
== '-') {
415 padvise (NULL
, "missing argument to %s", argp
[-2]);
422 if (!(cp
= nxtarg ())) {/* allow -xyz arguments */
423 padvise (NULL
, "missing argument to %s", argp
[-2]);
426 n
= newnexus (TWSaction
);
428 if (!tcompile (cp
, &n
->n_tws
, n
->n_after
= i
== PRAFTR
)) {
429 padvise (NULL
, "unable to parse %s %s", argp
[-2], cp
);
437 static struct nexus
*
438 newnexus (int (*action
)())
440 register struct nexus
*p
;
442 if ((p
= (struct nexus
*) calloc ((size_t) 1, sizeof *p
)) == NULL
)
443 adios (NULL
, "unable to allocate component storage");
445 p
->n_action
= action
;
450 #define args(a) a, fp, msgnum, start, stop
451 #define params args (n)
453 register struct nexus *n; \
460 pmatches (FILE *fp
, int msgnum
, long start
, long stop
)
465 if (!talked
++ && pdebug
)
468 return (*head
->n_action
) (args (head
));
473 PRaction (struct nexus
*n
, int level
)
477 for (i
= 0; i
< level
; i
++)
478 fprintf (stderr
, "| ");
480 if (n
->n_action
== ORaction
) {
481 fprintf (stderr
, "OR\n");
482 PRaction (n
->n_L_child
, level
+ 1);
483 PRaction (n
->n_R_child
, level
+ 1);
486 if (n
->n_action
== ANDaction
) {
487 fprintf (stderr
, "AND\n");
488 PRaction (n
->n_L_child
, level
+ 1);
489 PRaction (n
->n_R_child
, level
+ 1);
492 if (n
->n_action
== NOTaction
) {
493 fprintf (stderr
, "NOT\n");
494 PRaction (n
->n_L_child
, level
+ 1);
497 if (n
->n_action
== GREPaction
) {
498 fprintf (stderr
, "PATTERN(%s) %s\n",
499 n
->n_header
? "header" : "body", n
->n_patbuf
);
502 if (n
->n_action
== TWSaction
) {
503 fprintf (stderr
, "TEMPORAL(%s) %s: %s\n",
504 n
->n_after
? "after" : "before", n
->n_datef
,
505 dasctime (&n
->n_tws
, TW_NULL
));
508 fprintf (stderr
, "UNKNOWN(0x%x)\n", (unsigned int) (*n
->n_action
));
516 if ((*n
->n_L_child
->n_action
) (args (n
->n_L_child
)))
518 return (*n
->n_R_child
->n_action
) (args (n
->n_R_child
));
526 if (!(*n
->n_L_child
->n_action
) (args (n
->n_L_child
)))
528 return (*n
->n_R_child
->n_action
) (args (n
->n_R_child
));
536 return (!(*n
->n_L_child
->n_action
) (args (n
->n_L_child
)));
541 gcompile (struct nexus
*n
, char *astr
)
545 register char *ep
, *dp
, *sp
, *lastep
;
547 dp
= (ep
= n
->n_expbuf
) + sizeof n
->n_expbuf
;
558 if ((c
= *sp
++) != '*')
585 if ((c
= *sp
++) == '^') {
592 if (c
== '\0' || ep
>= dp
)
594 } while ((c
= *sp
++) != ']');
599 if ((c
= *sp
++) == '\0')
619 register char *p1
, *p2
, *ebp
, *cbp
;
622 fseek (fp
, start
, SEEK_SET
);
626 if (body
&& n
->n_header
)
633 if (fgets (ibuf
, sizeof ibuf
, fp
) == NULL
634 || (stop
&& pos
>= stop
)) {
639 pos
+= (long) strlen (ibuf
);
641 ebp
= ibuf
+ strlen (ibuf
);
644 if (lf
&& c
!= '\n') {
645 if (c
!= ' ' && c
!= '\t') {
664 if (c
&& p1
< &linebuf
[LBSIZE
- 1])
674 if (advance (p1
, p2
))
682 if (*p1
== c
|| cc
[*p1
] == c
)
683 if (advance (p1
, p2
))
690 if (advance (p1
, p2
))
698 advance (char *alp
, char *aep
)
700 register char *lp
, *ep
, *curlp
;
707 if (*ep
++ == *lp
++ || ep
[-1] == cc
[lp
[-1]])
725 if (cclass (ep
, *lp
++, 1)) {
732 if (cclass (ep
, *lp
++, 0)) {
746 while (*lp
++ == *ep
|| cc
[lp
[-1]] == *ep
)
754 while (cclass (ep
, *lp
++, ep
[-1] == (CCL
| STAR
)))
762 if (advance (lp
, ep
))
764 } while (lp
> curlp
);
768 admonish (NULL
, "advance() botch -- you lose big");
775 cclass (char *aset
, int ac
, int af
)
795 tcompile (char *ap
, struct tws
*tb
, int isafter
)
797 register struct tws
*tw
;
799 if ((tw
= tws_parse (ap
, isafter
)) == NULL
)
808 tws_parse (char *ap
, int isafter
)
811 register struct tws
*tw
, *ts
;
813 if ((tw
= tws_special (ap
)) != NULL
) {
814 tw
->tw_sec
= tw
->tw_min
= isafter
? 59 : 0;
815 tw
->tw_hour
= isafter
? 23 : 0;
818 if ((tw
= dparsetime (ap
)) != NULL
)
821 if ((ts
= dlocaltimenow ()) == NULL
)
824 snprintf (buffer
, sizeof(buffer
), "%s %s", ap
, dtwszone (ts
));
825 if ((tw
= dparsetime (buffer
)) != NULL
)
828 snprintf (buffer
, sizeof(buffer
), "%s %02d:%02d:%02d %s", ap
,
829 ts
->tw_hour
, ts
->tw_min
, ts
->tw_sec
, dtwszone (ts
));
830 if ((tw
= dparsetime (buffer
)) != NULL
)
833 snprintf (buffer
, sizeof(buffer
), "%02d %s %04d %s",
834 ts
->tw_mday
, tw_moty
[ts
->tw_mon
], ts
->tw_year
, ap
);
835 if ((tw
= dparsetime (buffer
)) != NULL
)
838 snprintf (buffer
, sizeof(buffer
), "%02d %s %04d %s %s",
839 ts
->tw_mday
, tw_moty
[ts
->tw_mon
], ts
->tw_year
,
841 if ((tw
= dparsetime (buffer
)) != NULL
)
849 tws_special (char *ap
)
853 register struct tws
*tw
;
856 if (!strcasecmp (ap
, "today"))
857 return dlocaltime (&clock
);
858 if (!strcasecmp (ap
, "yesterday")) {
859 clock
-= (long) (60 * 60 * 24);
860 return dlocaltime (&clock
);
862 if (!strcasecmp (ap
, "tomorrow")) {
863 clock
+= (long) (60 * 60 * 24);
864 return dlocaltime (&clock
);
867 for (i
= 0; tw_ldotw
[i
]; i
++)
868 if (!strcasecmp (ap
, tw_ldotw
[i
]))
871 if ((tw
= dlocaltime (&clock
)) == NULL
)
873 if ((i
-= tw
->tw_wday
) > 0)
879 else /* -ddd days ago */
880 i
= atoi (ap
); /* we should error check this */
882 clock
+= (long) ((60 * 60 * 24) * i
);
883 return dlocaltime (&clock
);
893 char buf
[BUFSIZ
], name
[NAMESZ
];
894 register struct tws
*tw
;
896 fseek (fp
, start
, SEEK_SET
);
897 for (state
= FLD
, bp
= NULL
;;) {
898 switch (state
= m_getfld (state
, name
, buf
, sizeof buf
, fp
)) {
903 free (bp
), bp
= NULL
;
904 bp
= add (buf
, NULL
);
905 while (state
== FLDPLUS
) {
906 state
= m_getfld (state
, name
, buf
, sizeof buf
, fp
);
909 if (!strcasecmp (name
, n
->n_datef
))
919 if (state
== LENERR
|| state
== FMTERR
)
920 advise (NULL
, "format error in message %d", msgnum
);
926 adios (NULL
, "internal error -- you lose");
931 if ((tw
= dparsetime (bp
)) == NULL
)
932 advise (NULL
, "unable to parse %s field in message %d, matching...",
933 n
->n_datef
, msgnum
), state
= 1;
935 state
= n
->n_after
? (twsort (tw
, &n
->n_tws
) > 0)
936 : (twsort (tw
, &n
->n_tws
) < 0);