]>
diplodocus.org Git - nmh/blob - uip/picksbr.c
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.
10 #include <h/picksbr.h>
13 #ifdef HAVE_SYS_TIME_H
14 # include <sys/time.h>
18 #define PARSE_SWITCHES \
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) \
35 #define X(sw, minchars, id) id,
36 DEFINE_SWITCH_ENUM(PARSE
);
39 #define X(sw, minchars, id) { sw, minchars, id },
40 DEFINE_SWITCH_ARRAY(PARSE
, parswit
);
43 /* DEFINITIONS FOR PATTERN MATCHING */
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".
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.
65 #define LBSIZE NMH_BUFSIZ
69 static char linebuf
[LBSIZE
+ 1];
70 static char decoded_linebuf
[LBSIZE
+ 1];
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,
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,
110 * DEFINITIONS FOR NEXUS
113 #define nxtarg() (*argp ? *argp++ : NULL)
114 #define prvarg() argp--
116 #define pinform if (!talked++) inform
119 int (*n_action
)(struct nexus
*n
, FILE *fp
, int msgnum
, long start
, long stop
);
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
162 static struct nexus
*head
;
165 * prototypes for date routines
167 static struct tws
*tws_parse(char *, int);
168 static struct tws
*tws_special(char *);
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);
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
));
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
);
199 pcompile (char **vec
, char *date
)
202 if ((datesw
= date
) == NULL
)
206 if ((head
= parse ()) == NULL
)
210 inform("%s unexpected", *argp
);
218 static struct nexus
*
224 if ((n
= nexp1 ()) == NULL
|| (cp
= nxtarg ()) == NULL
)
228 pinform("%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 pinform("missing disjunctive");
260 static struct nexus
*
266 if ((n
= nexp2 ()) == NULL
|| (cp
= nxtarg ()) == NULL
)
270 pinform("%s unexpected", cp
);
277 switch (smatch (cp
, parswit
)) {
279 ambigsw (cp
, parswit
);
284 fprintf (stderr
, "-%s unknown\n", cp
);
290 o
= newnexus (ANDaction
);
292 if ((o
->n_R_child
= nexp1 ()))
294 pinform("missing conjunctive");
306 static struct nexus
*
312 if ((cp
= nxtarg ()) == NULL
)
322 switch (smatch (cp
, parswit
)) {
324 ambigsw (cp
, parswit
);
328 fprintf (stderr
, "-%s unknown\n", cp
);
333 n
= newnexus (NOTaction
);
334 if ((n
->n_L_child
= nexp3 ()))
336 pinform("missing negation");
347 static struct nexus
*
352 char buffer
[BUFSIZ
], temp
[64];
355 if ((cp
= nxtarg ()) == NULL
)
359 pinform("%s unexpected", cp
);
367 switch (i
= smatch (cp
, parswit
)) {
369 ambigsw (cp
, parswit
);
373 fprintf (stderr
, "-%s unknown\n", cp
);
378 if ((n
= parse ()) == NULL
) {
379 pinform("missing group");
382 if ((cp
= nxtarg ()) == NULL
) {
383 pinform("missing -rbrace");
386 if (*cp
++ == '-' && smatch (cp
, parswit
) == PRRBR
)
388 pinform("%s unexpected", --cp
);
400 strncpy(temp
, parswit
[i
].sw
, sizeof(temp
));
401 temp
[sizeof(temp
) - 1] = '\0';
402 dp
= *brkstring (temp
, " ", NULL
);
404 if (!(cp
= nxtarg ())) {/* allow -xyz arguments */
405 pinform("missing argument to %s", argp
[-2]);
408 n
= newnexus (GREPaction
);
410 snprintf (buffer
, sizeof(buffer
), "^%s[ \t]*:.*%s", dp
, cp
);
415 n
= newnexus (GREPaction
);
417 if (!(cp
= nxtarg ())) {/* allow -xyz arguments */
418 pinform("missing argument to %s", argp
[-2]);
424 if (!gcompile (n
, dp
)) {
425 pinform("pattern error in %s %s", argp
[-2], cp
);
429 n
->n_patbuf
= mh_xstrdup(dp
);
433 pinform("internal error!");
437 if (!(datesw
= nxtarg ()) || *datesw
== '-') {
438 pinform("missing argument to %s", argp
[-2]);
445 if (!(cp
= nxtarg ())) {/* allow -xyz arguments */
446 pinform("missing argument to %s", argp
[-2]);
449 n
= newnexus (TWSaction
);
451 if (!tcompile (cp
, &n
->n_tws
, n
->n_after
= i
== PRAFTR
)) {
452 pinform("unable to parse %s %s", argp
[-2], cp
);
461 static struct nexus
*
462 newnexus(int (*action
)(struct nexus
*n
, FILE *fp
, int msgnum
,
463 long start
, long stop
))
468 p
->n_action
= action
;
474 pmatches (FILE *fp
, int msgnum
, long start
, long stop
, int debug
)
479 if (!talked
++ && debug
)
482 return (*head
->n_action
)(head
, fp
, msgnum
, start
, stop
);
487 PRaction (struct nexus
*n
, int level
)
491 for (i
= 0; i
< level
; i
++)
492 fprintf (stderr
, "| ");
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);
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);
506 if (n
->n_action
== NOTaction
) {
507 fprintf (stderr
, "NOT\n");
508 PRaction (n
->n_L_child
, level
+ 1);
511 if (n
->n_action
== GREPaction
) {
512 fprintf (stderr
, "PATTERN(%s) %s\n",
513 n
->n_header
? "header" : "body", n
->n_patbuf
);
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
));
522 fprintf(stderr
, "UNKNOWN(%#llx)\n", (unsigned long long)n
->n_action
);
527 ORaction(struct nexus
*n
, FILE *fp
, int msgnum
, long start
, long stop
)
529 if ((*n
->n_L_child
->n_action
)(n
->n_L_child
, fp
, msgnum
, start
, stop
))
531 return (*n
->n_R_child
->n_action
)(n
->n_R_child
, fp
, msgnum
, start
, stop
);
536 ANDaction(struct nexus
*n
, FILE *fp
, int msgnum
, long start
, long stop
)
538 if (!(*n
->n_L_child
->n_action
)(n
->n_L_child
, fp
, msgnum
, start
, stop
))
540 return (*n
->n_R_child
->n_action
)(n
->n_R_child
, fp
, msgnum
, start
, stop
);
545 NOTaction(struct nexus
*n
, FILE *fp
, int msgnum
, long start
, long stop
)
547 return (!(*n
->n_L_child
->n_action
)(n
->n_L_child
, fp
, msgnum
, start
, stop
));
552 gcompile (struct nexus
*n
, char *astr
)
556 unsigned char *ep
, *dp
, *sp
, *lastep
= 0;
558 dp
= (ep
= (unsigned char *) n
->n_expbuf
) + sizeof n
->n_expbuf
;
559 sp
= (unsigned char *) astr
;
569 if ((c
= *sp
++) != '*')
596 if ((c
= *sp
++) == '^') {
606 if (c
== '-' && *sp
!= '\0' && *sp
!= ']') {
607 for (c
= ep
[-1]+1; c
< *sp
; c
++) {
610 if (c
== '\0' || ep
>= dp
)
616 if (c
== '\0' || ep
>= dp
)
619 } while ((c
= *sp
++) != ']');
626 if ((c
= *sp
++) == '\0')
642 GREPaction(struct nexus
*n
, FILE *fp
, int msgnum
, long start
, long stop
)
646 char *p1
, *p2
, *ebp
, *cbp
;
650 fseek (fp
, start
, SEEK_SET
);
654 if (body
&& n
->n_header
)
661 if (fgets (ibuf
, sizeof ibuf
, fp
) == NULL
662 || (stop
&& pos
>= stop
)) {
667 pos
+= (long) strlen (ibuf
);
669 ebp
= ibuf
+ strlen (ibuf
);
672 if (lf
&& c
!= '\n') {
673 if (c
!= ' ' && c
!= '\t') {
687 /* Unfold by skipping the newline. */
690 if (c
&& p1
< &linebuf
[LBSIZE
- 1])
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
;
707 if (advance (p1
, p2
))
715 if (*p1
== c
|| cc
[(unsigned char)*p1
] == c
)
716 if (advance (p1
, p2
))
723 if (advance (p1
, p2
))
731 advance (char *alp
, char *aep
)
733 unsigned char *lp
, *ep
, *curlp
;
735 lp
= (unsigned char *)alp
;
736 ep
= (unsigned char *)aep
;
740 if (*ep
++ == *lp
++ || ep
[-1] == cc
[lp
[-1]])
758 if (cclass (ep
, *lp
++, 1)) {
765 if (cclass (ep
, *lp
++, 0)) {
779 while (*lp
++ == *ep
|| cc
[lp
[-1]] == *ep
)
787 while (cclass (ep
, *lp
++, ep
[-1] == (CCL
| STAR
)))
795 if (advance ((char *) lp
, (char *) ep
))
797 } while (lp
> curlp
);
801 inform("advance() botch -- you lose big, continuing...");
808 cclass (unsigned char *aset
, int ac
, int af
)
811 unsigned char c
, *set
;
819 if (*set
++ == c
|| set
[-1] == cc
[c
])
827 tcompile (char *ap
, struct tws
*tb
, int isafter
)
831 if ((tw
= tws_parse (ap
, isafter
)) == NULL
)
840 tws_parse (char *ap
, int isafter
)
845 if ((tw
= tws_special (ap
)) != NULL
) {
846 tw
->tw_sec
= tw
->tw_min
= isafter
? 59 : 0;
847 tw
->tw_hour
= isafter
? 23 : 0;
850 if ((tw
= dparsetime (ap
)) != NULL
)
853 if ((ts
= dlocaltimenow ()) == NULL
)
856 snprintf (buffer
, sizeof(buffer
), "%s %s", ap
, dtwszone (ts
));
857 if ((tw
= dparsetime (buffer
)) != NULL
)
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
)
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
)
870 snprintf (buffer
, sizeof(buffer
), "%02d %s %04d %s %s",
871 ts
->tw_mday
, tw_moty
[ts
->tw_mon
], ts
->tw_year
,
873 if ((tw
= dparsetime (buffer
)) != NULL
)
881 tws_special (char *ap
)
888 if (!strcasecmp (ap
, "today"))
889 return dlocaltime (&clock
);
890 if (!strcasecmp (ap
, "yesterday")) {
891 clock
-= (long) (60 * 60 * 24);
892 return dlocaltime (&clock
);
894 if (!strcasecmp (ap
, "tomorrow")) {
895 clock
+= (long) (60 * 60 * 24);
896 return dlocaltime (&clock
);
899 for (i
= 0; tw_ldotw
[i
]; i
++)
900 if (!strcasecmp (ap
, tw_ldotw
[i
]))
903 if ((tw
= dlocaltime (&clock
)) == NULL
)
905 if ((i
-= tw
->tw_wday
) > 0)
912 i
= atoi (ap
); /* we should error check this */
915 clock
+= (long) ((60 * 60 * 24) * i
);
916 return dlocaltime (&clock
);
921 TWSaction(struct nexus
*n
, FILE *fp
, int msgnum
, long start
, long stop
)
925 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
927 m_getfld_state_t gstate
;
930 fseek (fp
, start
, SEEK_SET
);
931 gstate
= m_getfld_state_init(fp
);
933 int bufsz
= sizeof buf
;
934 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
938 bp
= mh_xstrdup(buf
);
939 while (state
== FLDPLUS
) {
941 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
944 if (!strcasecmp (name
, n
->n_datef
))
952 if (state
== LENERR
|| state
== FMTERR
)
953 inform("format error in message %d", msgnum
);
958 adios (NULL
, "internal error -- you lose");
962 m_getfld_state_destroy (&gstate
);
964 if ((tw
= dparsetime (bp
)) == NULL
)
965 inform("unable to parse %s field in message %d, matching...",
966 n
->n_datef
, msgnum
), state
= 1;
968 state
= n
->n_after
? (twsort (tw
, &n
->n_tws
) > 0)
969 : (twsort (tw
, &n
->n_tws
) < 0);