]>
diplodocus.org Git - nmh/blob - uip/picksbr.c
4732785cae3b5b81ba5c80afe3019f9542e4cd05
1 /* picksbr.c -- routines to help pick along...
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.
9 #include "sbr/ambigsw.h"
10 #include "sbr/error.h"
12 #include "h/picksbr.h"
15 #ifdef HAVE_SYS_TIME_H
16 # include <sys/time.h>
20 #define PARSE_SWITCHES \
24 X("lbrace", 0, PRLBR) \
25 X("rbrace", 0, PRRBR) \
26 X("cc pattern", 0, PRCC) \
27 X("date pattern", 0, PRDATE) \
28 X("from pattern", 0, PRFROM) \
29 X("search pattern", 0, PRSRCH) \
30 X("subject pattern", 0, PRSUBJ) \
31 X("to pattern", 0, PRTO) \
32 X("-othercomponent pattern", 15, PROTHR) \
33 X("after date", 0, PRAFTR) \
34 X("before date", 0, PRBEFR) \
35 X("datefield field", 5, PRDATF) \
37 #define X(sw, minchars, id) id,
38 DEFINE_SWITCH_ENUM(PARSE
);
41 #define X(sw, minchars, id) { sw, minchars, id },
42 DEFINE_SWITCH_ARRAY(PARSE
, parswit
);
45 /* DEFINITIONS FOR PATTERN MATCHING */
48 * We really should be using re_comp() and re_exec() here. Unfortunately,
49 * pick advertises that lowercase characters matches characters of both
50 * cases. Since re_exec() doesn't exhibit this behavior, we are stuck
51 * with this version. Furthermore, we need to be able to save and restore
52 * the state of the pattern matcher in order to do things "efficiently".
54 * The matching power of this algorithm isn't as powerful as the re_xxx()
55 * routines (no \(xxx\) and \n constructs). Such is life.
67 #define LBSIZE NMH_BUFSIZ
71 static char linebuf
[LBSIZE
+ 1];
72 static char decoded_linebuf
[LBSIZE
+ 1];
74 /* the magic array for case-independence */
75 static unsigned char cc
[] = {
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,
93 0200,0201,0202,0203,0204,0205,0206,0207,
94 0210,0211,0212,0213,0214,0215,0216,0217,
95 0220,0221,0222,0223,0224,0225,0226,0227,
96 0230,0231,0232,0233,0234,0235,0236,0237,
97 0240,0241,0242,0243,0244,0245,0246,0247,
98 0250,0251,0252,0253,0254,0255,0256,0257,
99 0260,0261,0262,0263,0264,0265,0266,0267,
100 0270,0271,0272,0273,0274,0275,0276,0277,
101 0300,0301,0302,0303,0304,0305,0306,0307,
102 0310,0311,0312,0313,0314,0315,0316,0317,
103 0320,0321,0322,0323,0324,0325,0326,0327,
104 0330,0331,0332,0333,0334,0335,0336,0337,
105 0340,0341,0342,0343,0344,0345,0346,0347,
106 0350,0351,0352,0353,0354,0355,0356,0357,
107 0360,0361,0362,0363,0364,0365,0366,0367,
108 0370,0371,0372,0373,0374,0375,0376,0377,
112 * DEFINITIONS FOR NEXUS
115 #define nxtarg() (*argp ? *argp++ : NULL)
116 #define prvarg() argp--
118 #define pinform if (!talked++) inform
121 int (*n_action
)(struct nexus
*n
, FILE *fp
, int msgnum
, long start
, long stop
);
124 /* for {OR,AND,NOT}action */
126 struct nexus
*un_L_child
;
127 struct nexus
*un_R_child
;
134 char un_expbuf
[ESIZE
];
147 #define n_L_child un.st1.un_L_child
148 #define n_R_child un.st1.un_R_child
150 #define n_header un.st2.un_header
151 #define n_circf un.st2.un_circf
152 #define n_expbuf un.st2.un_expbuf
153 #define n_patbuf un.st2.un_patbuf
155 #define n_datef un.st3.un_datef
156 #define n_after un.st3.un_after
157 #define n_tws un.st3.un_tws
164 static struct nexus
*head
;
167 * prototypes for date routines
169 static struct tws
*tws_parse(char *, int);
170 static struct tws
*tws_special(char *);
175 static void PRaction(struct nexus
*, int);
176 static int gcompile(struct nexus
*, char *);
177 static int advance(char *, char *);
178 static int cclass(unsigned char *, int, int);
179 static int tcompile(char *, struct tws
*, int);
181 static struct nexus
*parse(void);
182 static struct nexus
*nexp1(void);
183 static struct nexus
*nexp2(void);
184 static struct nexus
*nexp3(void);
185 static struct nexus
*newnexus(int (*action
)(struct nexus
*n
,
186 FILE *fp
, int msgnum
, long start
, long stop
));
188 static int ORaction(struct nexus
*n
, FILE *fp
, int msgnum
,
189 long start
, long stop
);
190 static int ANDaction(struct nexus
*n
, FILE *fp
, int msgnum
,
191 long start
, long stop
);
192 static int NOTaction(struct nexus
*n
, FILE *fp
, int msgnum
,
193 long start
, long stop
);
194 static int GREPaction(struct nexus
*n
, FILE *fp
, int msgnum
,
195 long start
, long stop
);
196 static int TWSaction(struct nexus
*n
, FILE *fp
, int msgnum
,
197 long start
, long stop
);
201 pcompile (char **vec
, char *date
)
204 if ((datesw
= date
) == NULL
)
208 if ((head
= parse ()) == NULL
)
212 inform("%s unexpected", *argp
);
220 static struct nexus
*
226 if ((n
= nexp1 ()) == NULL
|| (cp
= nxtarg ()) == NULL
)
230 pinform("%s unexpected", cp
);
236 switch (smatch (cp
, parswit
)) {
238 ambigsw (cp
, parswit
);
242 fprintf (stderr
, "-%s unknown\n", cp
);
247 o
= newnexus (ORaction
);
249 if ((o
->n_R_child
= parse ()))
251 pinform("missing disjunctive");
262 static struct nexus
*
268 if ((n
= nexp2 ()) == NULL
|| (cp
= nxtarg ()) == NULL
)
272 pinform("%s unexpected", cp
);
279 switch (smatch (cp
, parswit
)) {
281 ambigsw (cp
, parswit
);
286 fprintf (stderr
, "-%s unknown\n", cp
);
292 o
= newnexus (ANDaction
);
294 if ((o
->n_R_child
= nexp1 ()))
296 pinform("missing conjunctive");
308 static struct nexus
*
314 if ((cp
= nxtarg ()) == NULL
)
324 switch (smatch (cp
, parswit
)) {
326 ambigsw (cp
, parswit
);
330 fprintf (stderr
, "-%s unknown\n", cp
);
335 n
= newnexus (NOTaction
);
336 if ((n
->n_L_child
= nexp3 ()))
338 pinform("missing negation");
349 static struct nexus
*
354 char buffer
[BUFSIZ
], temp
[64];
357 if ((cp
= nxtarg ()) == NULL
)
361 pinform("%s unexpected", cp
);
369 switch (i
= smatch (cp
, parswit
)) {
371 ambigsw (cp
, parswit
);
375 fprintf (stderr
, "-%s unknown\n", cp
);
380 if ((n
= parse ()) == NULL
) {
381 pinform("missing group");
384 if ((cp
= nxtarg ()) == NULL
) {
385 pinform("missing -rbrace");
388 if (*cp
++ == '-' && smatch (cp
, parswit
) == PRRBR
)
390 pinform("%s unexpected", --cp
);
402 strncpy(temp
, parswit
[i
].sw
, sizeof(temp
));
403 temp
[sizeof(temp
) - 1] = '\0';
404 dp
= *brkstring (temp
, " ", NULL
);
406 if (!(cp
= nxtarg ())) {/* allow -xyz arguments */
407 pinform("missing argument to %s", argp
[-2]);
410 n
= newnexus (GREPaction
);
412 snprintf (buffer
, sizeof(buffer
), "^%s[ \t]*:.*%s", dp
, cp
);
417 n
= newnexus (GREPaction
);
419 if (!(cp
= nxtarg ())) {/* allow -xyz arguments */
420 pinform("missing argument to %s", argp
[-2]);
426 if (!gcompile (n
, dp
)) {
427 pinform("pattern error in %s %s", argp
[-2], cp
);
431 n
->n_patbuf
= mh_xstrdup(dp
);
435 pinform("internal error!");
439 if (!(datesw
= nxtarg ()) || *datesw
== '-') {
440 pinform("missing argument to %s", argp
[-2]);
447 if (!(cp
= nxtarg ())) {/* allow -xyz arguments */
448 pinform("missing argument to %s", argp
[-2]);
451 n
= newnexus (TWSaction
);
453 if (!tcompile (cp
, &n
->n_tws
, n
->n_after
= i
== PRAFTR
)) {
454 pinform("unable to parse %s %s", argp
[-2], cp
);
463 static struct nexus
*
464 newnexus(int (*action
)(struct nexus
*n
, FILE *fp
, int msgnum
,
465 long start
, long stop
))
470 p
->n_action
= action
;
476 pmatches (FILE *fp
, int msgnum
, long start
, long stop
, int debug
)
481 if (!talked
++ && debug
)
484 return (*head
->n_action
)(head
, fp
, msgnum
, start
, stop
);
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
));
526 gp
.f
= (void (*)(void))n
->n_action
;
527 fprintf(stderr
, "UNKNOWN(%p)\n", gp
.v
);
532 ORaction(struct nexus
*n
, FILE *fp
, int msgnum
, long start
, long stop
)
534 if ((*n
->n_L_child
->n_action
)(n
->n_L_child
, fp
, msgnum
, start
, stop
))
536 return (*n
->n_R_child
->n_action
)(n
->n_R_child
, fp
, msgnum
, start
, stop
);
541 ANDaction(struct nexus
*n
, FILE *fp
, int msgnum
, long start
, long stop
)
543 if (!(*n
->n_L_child
->n_action
)(n
->n_L_child
, fp
, msgnum
, start
, stop
))
545 return (*n
->n_R_child
->n_action
)(n
->n_R_child
, fp
, msgnum
, start
, stop
);
550 NOTaction(struct nexus
*n
, FILE *fp
, int msgnum
, long start
, long stop
)
552 return (!(*n
->n_L_child
->n_action
)(n
->n_L_child
, fp
, msgnum
, start
, stop
));
557 gcompile (struct nexus
*n
, char *astr
)
561 unsigned char *ep
, *dp
, *sp
, *lastep
= 0;
563 dp
= (ep
= (unsigned char *) n
->n_expbuf
) + sizeof n
->n_expbuf
;
564 sp
= (unsigned char *) astr
;
574 if ((c
= *sp
++) != '*')
601 if ((c
= *sp
++) == '^') {
611 if (c
== '-' && *sp
!= '\0' && *sp
!= ']') {
612 for (c
= ep
[-1]+1; c
< *sp
; c
++) {
615 if (c
== '\0' || ep
>= dp
)
621 if (c
== '\0' || ep
>= dp
)
624 } while ((c
= *sp
++) != ']');
631 if ((c
= *sp
++) == '\0')
647 GREPaction(struct nexus
*n
, FILE *fp
, int msgnum
, long start
, long stop
)
653 char *p1
, *p2
, *ebp
, *cbp
;
657 fseek (fp
, start
, SEEK_SET
);
661 if (body
&& n
->n_header
)
668 if (fgets (ibuf
, sizeof ibuf
, fp
) == NULL
669 || (stop
&& pos
>= stop
)) {
674 pos
+= (long) strlen (ibuf
);
676 ebp
= ibuf
+ strlen (ibuf
);
679 if (lf
&& c
!= '\n') {
680 if (c
!= ' ' && c
!= '\t') {
694 /* Unfold by skipping the newline. */
697 if (c
&& p1
< &linebuf
[LBSIZE
- 1])
706 /* Attempt to decode as a MIME header. If it's the last header,
707 body will be 1 and lf will be at least 1. */
709 decode_rfc2047 (linebuf
, decoded_linebuf
, sizeof decoded_linebuf
)) {
710 p1
= decoded_linebuf
;
714 if (advance (p1
, p2
))
722 if (*p1
== c
|| cc
[(unsigned char)*p1
] == c
)
723 if (advance (p1
, p2
))
730 if (advance (p1
, p2
))
738 advance (char *alp
, char *aep
)
740 unsigned char *lp
, *ep
, *curlp
;
742 lp
= (unsigned char *)alp
;
743 ep
= (unsigned char *)aep
;
747 if (*ep
++ == *lp
++ || ep
[-1] == cc
[lp
[-1]])
765 if (cclass (ep
, *lp
++, 1)) {
772 if (cclass (ep
, *lp
++, 0)) {
786 while (*lp
++ == *ep
|| cc
[lp
[-1]] == *ep
)
794 while (cclass (ep
, *lp
++, ep
[-1] == (CCL
| STAR
)))
802 if (advance ((char *) lp
, (char *) ep
))
804 } while (lp
> curlp
);
808 inform("advance() botch -- you lose big, continuing...");
815 cclass (unsigned char *aset
, int ac
, int af
)
818 unsigned char c
, *set
;
826 if (*set
++ == c
|| set
[-1] == cc
[c
])
834 tcompile (char *ap
, struct tws
*tb
, int isafter
)
838 if ((tw
= tws_parse (ap
, isafter
)) == NULL
)
847 tws_parse (char *ap
, int isafter
)
852 if ((tw
= tws_special (ap
)) != NULL
) {
853 tw
->tw_sec
= tw
->tw_min
= isafter
? 59 : 0;
854 tw
->tw_hour
= isafter
? 23 : 0;
857 if ((tw
= dparsetime (ap
)) != NULL
)
860 if ((ts
= dlocaltimenow ()) == NULL
)
863 snprintf (buffer
, sizeof(buffer
), "%s %s", ap
, dtwszone (ts
));
864 if ((tw
= dparsetime (buffer
)) != NULL
)
867 snprintf (buffer
, sizeof(buffer
), "%s %02d:%02d:%02d %s", ap
,
868 ts
->tw_hour
, ts
->tw_min
, ts
->tw_sec
, dtwszone (ts
));
869 if ((tw
= dparsetime (buffer
)) != NULL
)
872 snprintf (buffer
, sizeof(buffer
), "%02d %s %04d %s",
873 ts
->tw_mday
, tw_moty
[ts
->tw_mon
], ts
->tw_year
, ap
);
874 if ((tw
= dparsetime (buffer
)) != NULL
)
877 snprintf (buffer
, sizeof(buffer
), "%02d %s %04d %s %s",
878 ts
->tw_mday
, tw_moty
[ts
->tw_mon
], ts
->tw_year
,
880 if ((tw
= dparsetime (buffer
)) != NULL
)
888 tws_special (char *ap
)
895 if (!strcasecmp (ap
, "today"))
896 return dlocaltime (&clock
);
897 if (!strcasecmp (ap
, "yesterday")) {
898 clock
-= (long) (60 * 60 * 24);
899 return dlocaltime (&clock
);
901 if (!strcasecmp (ap
, "tomorrow")) {
902 clock
+= (long) (60 * 60 * 24);
903 return dlocaltime (&clock
);
906 for (i
= 0; tw_ldotw
[i
]; i
++)
907 if (!strcasecmp (ap
, tw_ldotw
[i
]))
910 if ((tw
= dlocaltime (&clock
)) == NULL
)
912 if ((i
-= tw
->tw_wday
) > 0)
919 i
= atoi (ap
); /* we should error check this */
922 clock
+= (long) ((60 * 60 * 24) * i
);
923 return dlocaltime (&clock
);
928 TWSaction(struct nexus
*n
, FILE *fp
, int msgnum
, long start
, long stop
)
932 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
934 m_getfld_state_t gstate
;
937 fseek (fp
, start
, SEEK_SET
);
938 gstate
= m_getfld_state_init(fp
);
940 int bufsz
= sizeof buf
;
941 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
945 bp
= mh_xstrdup(buf
);
946 while (state
== FLDPLUS
) {
948 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
951 if (!strcasecmp (name
, n
->n_datef
))
959 if (state
== LENERR
|| state
== FMTERR
)
960 inform("format error in message %d", msgnum
);
965 die("internal error -- you lose");
969 m_getfld_state_destroy (&gstate
);
971 if ((tw
= dparsetime (bp
)) == NULL
)
972 inform("unable to parse %s field in message %d, matching...",
973 n
->n_datef
, msgnum
), state
= 1;
975 state
= n
->n_after
? (twsort (tw
, &n
->n_tws
) > 0)
976 : (twsort (tw
, &n
->n_tws
) < 0);