]>
diplodocus.org Git - nmh/blob - uip/picksbr.c
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>
16 #ifdef TIME_WITH_SYS_TIME
17 # include <sys/time.h>
20 # ifdef TM_IN_SYS_TIME
21 # include <sys/time.h>
27 static struct swit parswit
[] = {
41 { "date pattern", 0 },
43 { "from pattern", 0 },
45 { "search pattern", 0 },
47 { "subject pattern", 0 },
51 { "-othercomponent pattern", 15 },
57 { "datefield field", 5 },
61 /* DEFINITIONS FOR PATTERN MATCHING */
64 * We really should be using re_comp() and re_exec() here. Unfortunately,
65 * pick advertises that lowercase characters matches characters of both
66 * cases. Since re_exec() doesn't exhibit this behavior, we are stuck
67 * with this version. Furthermore, we need to be able to save and restore
68 * the state of the pattern matcher in order to do things "efficiently".
70 * The matching power of this algorithm isn't as powerful as the re_xxx()
71 * routines (no \(xxx\) and \n constructs). Such is life.
87 static char linebuf
[LBSIZE
+ 1];
89 /* the magic array for case-independence */
91 0000,0001,0002,0003,0004,0005,0006,0007,
92 0010,0011,0012,0013,0014,0015,0016,0017,
93 0020,0021,0022,0023,0024,0025,0026,0027,
94 0030,0031,0032,0033,0034,0035,0036,0037,
95 0040,0041,0042,0043,0044,0045,0046,0047,
96 0050,0051,0052,0053,0054,0055,0056,0057,
97 0060,0061,0062,0063,0064,0065,0066,0067,
98 0070,0071,0072,0073,0074,0075,0076,0077,
99 0100,0141,0142,0143,0144,0145,0146,0147,
100 0150,0151,0152,0153,0154,0155,0156,0157,
101 0160,0161,0162,0163,0164,0165,0166,0167,
102 0170,0171,0172,0133,0134,0135,0136,0137,
103 0140,0141,0142,0143,0144,0145,0146,0147,
104 0150,0151,0152,0153,0154,0155,0156,0157,
105 0160,0161,0162,0163,0164,0165,0166,0167,
106 0170,0171,0172,0173,0174,0175,0176,0177,
110 * DEFINITIONS FOR NEXUS
113 #define nxtarg() (*argp ? *argp++ : NULL)
114 #define prvarg() argp--
116 #define padvise if (!talked++) advise
122 /* for {OR,AND,NOT}action */
124 struct nexus
*un_L_child
;
125 struct nexus
*un_R_child
;
132 char un_expbuf
[ESIZE
];
145 #define n_L_child un.st1.un_L_child
146 #define n_R_child un.st1.un_R_child
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
153 #define n_datef un.st3.un_datef
154 #define n_after un.st3.un_after
155 #define n_tws un.st3.un_tws
158 static int pdebug
= 0;
163 static struct nexus
*head
;
166 * prototypes for date routines
168 static struct tws
*tws_parse();
169 static struct tws
*tws_special();
174 static void PRaction();
175 static int gcompile();
176 static int advance();
178 static int tcompile();
180 static struct nexus
*parse();
181 static struct nexus
*exp1();
182 static struct nexus
*exp2();
183 static struct nexus
*exp3();
184 static struct nexus
*newnexus();
186 static int ORaction();
187 static int ANDaction();
188 static int NOTaction();
189 static int GREPaction();
190 static int TWSaction();
194 pcompile (char **vec
, char *date
)
198 if ((cp
= getenv ("MHPDEBUG")) && *cp
)
202 if ((datesw
= date
) == NULL
)
206 if ((head
= parse ()) == NULL
)
207 return (talked
? 0 : 1);
210 padvise (NULL
, "%s unexpected", *argp
);
218 static struct nexus
*
222 register struct nexus
*n
, *o
;
224 if ((n
= exp1 ()) == NULL
|| (cp
= nxtarg ()) == NULL
)
228 padvise (NULL
, "%s unexpected", cp
);
234 switch (smatch (cp
, parswit
)) {
236 ambigsw (cp
, parswit
);
240 fprintf (stderr
, "-%s unknown\n", cp
);
245 o
= newnexus (ORaction
);
247 if ((o
->n_R_child
= parse ()))
249 padvise (NULL
, "missing disjunctive");
259 static struct nexus
*
263 register struct nexus
*n
, *o
;
265 if ((n
= exp2 ()) == NULL
|| (cp
= nxtarg ()) == NULL
)
269 padvise (NULL
, "%s unexpected", cp
);
275 switch (smatch (cp
, parswit
)) {
277 ambigsw (cp
, parswit
);
281 fprintf (stderr
, "-%s unknown\n", cp
);
286 o
= newnexus (ANDaction
);
288 if ((o
->n_R_child
= exp1 ()))
290 padvise (NULL
, "missing conjunctive");
301 static struct nexus
*
305 register struct nexus
*n
;
307 if ((cp
= nxtarg ()) == NULL
)
317 switch (smatch (cp
, parswit
)) {
319 ambigsw (cp
, parswit
);
323 fprintf (stderr
, "-%s unknown\n", cp
);
328 n
= newnexus (NOTaction
);
329 if ((n
->n_L_child
= exp3 ()))
331 padvise (NULL
, "missing negation");
341 static struct nexus
*
345 register char *cp
, *dp
;
346 char buffer
[BUFSIZ
], temp
[64];
347 register struct nexus
*n
;
349 if ((cp
= nxtarg ()) == NULL
)
353 padvise (NULL
, "%s unexpected", cp
);
361 switch (i
= smatch (cp
, parswit
)) {
363 ambigsw (cp
, parswit
);
367 fprintf (stderr
, "-%s unknown\n", cp
);
372 if ((n
= parse ()) == NULL
) {
373 padvise (NULL
, "missing group");
376 if ((cp
= nxtarg ()) == NULL
) {
377 padvise (NULL
, "missing -rbrace");
380 if (*cp
++ == '-' && smatch (cp
, parswit
) == PRRBR
)
382 padvise (NULL
, "%s unexpected", --cp
);
394 strncpy(temp
, parswit
[i
].sw
, sizeof(temp
));
395 temp
[sizeof(temp
) - 1] = '\0';
396 dp
= *brkstring (temp
, " ", NULL
);
398 if (!(cp
= nxtarg ())) {/* allow -xyz arguments */
399 padvise (NULL
, "missing argument to %s", argp
[-2]);
402 n
= newnexus (GREPaction
);
404 snprintf (buffer
, sizeof(buffer
), "^%s[ \t]*:.*%s", dp
, cp
);
409 n
= newnexus (GREPaction
);
411 if (!(cp
= nxtarg ())) {/* allow -xyz arguments */
412 padvise (NULL
, "missing argument to %s", argp
[-2]);
417 if (!gcompile (n
, dp
)) {
418 padvise (NULL
, "pattern error in %s %s", argp
[-2], cp
);
421 n
->n_patbuf
= getcpy (dp
);
425 padvise (NULL
, "internal error!");
429 if (!(datesw
= nxtarg ()) || *datesw
== '-') {
430 padvise (NULL
, "missing argument to %s", argp
[-2]);
437 if (!(cp
= nxtarg ())) {/* allow -xyz arguments */
438 padvise (NULL
, "missing argument to %s", argp
[-2]);
441 n
= newnexus (TWSaction
);
443 if (!tcompile (cp
, &n
->n_tws
, n
->n_after
= i
== PRAFTR
)) {
444 padvise (NULL
, "unable to parse %s %s", argp
[-2], cp
);
452 static struct nexus
*
453 newnexus (int (*action
)())
455 register struct nexus
*p
;
457 if ((p
= (struct nexus
*) calloc ((size_t) 1, sizeof *p
)) == NULL
)
458 adios (NULL
, "unable to allocate component storage");
460 p
->n_action
= action
;
465 #define args(a) a, fp, msgnum, start, stop
466 #define params args (n)
468 register struct nexus *n; \
475 pmatches (FILE *fp
, int msgnum
, long start
, long stop
)
480 if (!talked
++ && pdebug
)
483 return (*head
->n_action
) (args (head
));
488 PRaction (struct nexus
*n
, int level
)
492 for (i
= 0; i
< level
; i
++)
493 fprintf (stderr
, "| ");
495 if (n
->n_action
== ORaction
) {
496 fprintf (stderr
, "OR\n");
497 PRaction (n
->n_L_child
, level
+ 1);
498 PRaction (n
->n_R_child
, level
+ 1);
501 if (n
->n_action
== ANDaction
) {
502 fprintf (stderr
, "AND\n");
503 PRaction (n
->n_L_child
, level
+ 1);
504 PRaction (n
->n_R_child
, level
+ 1);
507 if (n
->n_action
== NOTaction
) {
508 fprintf (stderr
, "NOT\n");
509 PRaction (n
->n_L_child
, level
+ 1);
512 if (n
->n_action
== GREPaction
) {
513 fprintf (stderr
, "PATTERN(%s) %s\n",
514 n
->n_header
? "header" : "body", n
->n_patbuf
);
517 if (n
->n_action
== TWSaction
) {
518 fprintf (stderr
, "TEMPORAL(%s) %s: %s\n",
519 n
->n_after
? "after" : "before", n
->n_datef
,
520 dasctime (&n
->n_tws
, TW_NULL
));
523 fprintf (stderr
, "UNKNOWN(0x%x)\n", (unsigned int) (*n
->n_action
));
531 if ((*n
->n_L_child
->n_action
) (args (n
->n_L_child
)))
533 return (*n
->n_R_child
->n_action
) (args (n
->n_R_child
));
541 if (!(*n
->n_L_child
->n_action
) (args (n
->n_L_child
)))
543 return (*n
->n_R_child
->n_action
) (args (n
->n_R_child
));
551 return (!(*n
->n_L_child
->n_action
) (args (n
->n_L_child
)));
556 gcompile (struct nexus
*n
, char *astr
)
560 register char *ep
, *dp
, *sp
, *lastep
;
562 dp
= (ep
= n
->n_expbuf
) + sizeof n
->n_expbuf
;
573 if ((c
= *sp
++) != '*')
600 if ((c
= *sp
++) == '^') {
607 if (c
== '\0' || ep
>= dp
)
609 } while ((c
= *sp
++) != ']');
614 if ((c
= *sp
++) == '\0')
634 register char *p1
, *p2
, *ebp
, *cbp
;
637 fseek (fp
, start
, SEEK_SET
);
641 if (body
&& n
->n_header
)
648 if (fgets (ibuf
, sizeof ibuf
, fp
) == NULL
649 || (stop
&& pos
>= stop
)) {
654 pos
+= (long) strlen (ibuf
);
656 ebp
= ibuf
+ strlen (ibuf
);
659 if (lf
&& c
!= '\n') {
660 if (c
!= ' ' && c
!= '\t') {
679 if (c
&& p1
< &linebuf
[LBSIZE
- 1])
689 if (advance (p1
, p2
))
697 if (*p1
== c
|| cc
[(unsigned char)*p1
] == c
)
698 if (advance (p1
, p2
))
705 if (advance (p1
, p2
))
713 advance (char *alp
, char *aep
)
715 register char *lp
, *ep
, *curlp
;
722 if (*ep
++ == *lp
++ || ep
[-1] == cc
[(unsigned char)lp
[-1]])
740 if (cclass (ep
, *lp
++, 1)) {
747 if (cclass (ep
, *lp
++, 0)) {
761 while (*lp
++ == *ep
|| cc
[(unsigned char)lp
[-1]] == *ep
)
769 while (cclass (ep
, *lp
++, ep
[-1] == (CCL
| STAR
)))
777 if (advance (lp
, ep
))
779 } while (lp
> curlp
);
783 admonish (NULL
, "advance() botch -- you lose big");
790 cclass (char *aset
, int ac
, int af
)
810 tcompile (char *ap
, struct tws
*tb
, int isafter
)
812 register struct tws
*tw
;
814 if ((tw
= tws_parse (ap
, isafter
)) == NULL
)
823 tws_parse (char *ap
, int isafter
)
826 register struct tws
*tw
, *ts
;
828 if ((tw
= tws_special (ap
)) != NULL
) {
829 tw
->tw_sec
= tw
->tw_min
= isafter
? 59 : 0;
830 tw
->tw_hour
= isafter
? 23 : 0;
833 if ((tw
= dparsetime (ap
)) != NULL
)
836 if ((ts
= dlocaltimenow ()) == NULL
)
839 snprintf (buffer
, sizeof(buffer
), "%s %s", ap
, dtwszone (ts
));
840 if ((tw
= dparsetime (buffer
)) != NULL
)
843 snprintf (buffer
, sizeof(buffer
), "%s %02d:%02d:%02d %s", ap
,
844 ts
->tw_hour
, ts
->tw_min
, ts
->tw_sec
, dtwszone (ts
));
845 if ((tw
= dparsetime (buffer
)) != NULL
)
848 snprintf (buffer
, sizeof(buffer
), "%02d %s %04d %s",
849 ts
->tw_mday
, tw_moty
[ts
->tw_mon
], ts
->tw_year
, ap
);
850 if ((tw
= dparsetime (buffer
)) != NULL
)
853 snprintf (buffer
, sizeof(buffer
), "%02d %s %04d %s %s",
854 ts
->tw_mday
, tw_moty
[ts
->tw_mon
], ts
->tw_year
,
856 if ((tw
= dparsetime (buffer
)) != NULL
)
864 tws_special (char *ap
)
868 register struct tws
*tw
;
871 if (!strcasecmp (ap
, "today"))
872 return dlocaltime (&clock
);
873 if (!strcasecmp (ap
, "yesterday")) {
874 clock
-= (long) (60 * 60 * 24);
875 return dlocaltime (&clock
);
877 if (!strcasecmp (ap
, "tomorrow")) {
878 clock
+= (long) (60 * 60 * 24);
879 return dlocaltime (&clock
);
882 for (i
= 0; tw_ldotw
[i
]; i
++)
883 if (!strcasecmp (ap
, tw_ldotw
[i
]))
886 if ((tw
= dlocaltime (&clock
)) == NULL
)
888 if ((i
-= tw
->tw_wday
) > 0)
894 else /* -ddd days ago */
895 i
= atoi (ap
); /* we should error check this */
897 clock
+= (long) ((60 * 60 * 24) * i
);
898 return dlocaltime (&clock
);
908 char buf
[BUFSIZ
], name
[NAMESZ
];
909 register struct tws
*tw
;
911 fseek (fp
, start
, SEEK_SET
);
912 for (state
= FLD
, bp
= NULL
;;) {
913 switch (state
= m_getfld (state
, name
, buf
, sizeof buf
, fp
)) {
918 free (bp
), bp
= NULL
;
919 bp
= add (buf
, NULL
);
920 while (state
== FLDPLUS
) {
921 state
= m_getfld (state
, name
, buf
, sizeof buf
, fp
);
924 if (!strcasecmp (name
, n
->n_datef
))
934 if (state
== LENERR
|| state
== FMTERR
)
935 advise (NULL
, "format error in message %d", msgnum
);
941 adios (NULL
, "internal error -- you lose");
946 if ((tw
= dparsetime (bp
)) == NULL
)
947 advise (NULL
, "unable to parse %s field in message %d, matching...",
948 n
->n_datef
, msgnum
), state
= 1;
950 state
= n
->n_after
? (twsort (tw
, &n
->n_tws
) > 0)
951 : (twsort (tw
, &n
->n_tws
) < 0);