+ listsw = 0; /* HACK */
+ return 0;
+}
+
+static void
+scan_mbox(char *file, char *fmtstr, int width)
+{
+ FILE *in;
+ int msgnum;
+ int state;
+
+ if (strcmp(file, "-") == 0) {
+ in = stdin;
+ file = "stdin";
+ } else if (!(in = fopen(file, "r"))) {
+ adios(EX_IOERR, file, "unable to open");
+ }
+
+ for (msgnum = 1; ;msgnum++) {
+ state = scan(in, msgnum, SCN_MBOX, fmtstr, width, 0, 0);
+ if (state != SCNMSG) {
+ break;
+ }
+ }
+ fclose(in);
+}
+
+
+void
+putzero_done()
+{
+ if (listsw && !isatty(fileno(stdout)))
+ printf("0\n");
+}
+
+static void
+printmsg(FILE *f, struct msgs *mp, int msgnum, char *fmtstr, int width)
+{
+ int seqnum;
+ int state;
+ boolean unseen = FALSE;
+
+ fseek(f, 0L, SEEK_SET);
+
+ seqnum = seq_getnum(mp, seq_unseen);
+ unseen = in_sequence(mp, seqnum, msgnum);
+
+ switch (state = scan(f, msgnum, SCN_FOLD, fmtstr,
+ width, msgnum==mp->curmsg, unseen)) {
+ case SCNMSG:
+ case SCNERR:
+ break;
+ case SCNEOF:
+ advise(NULL, "message %d: empty", msgnum);
+ break;
+ default:
+ adios(EX_SOFTWARE, NULL, "scan() botch(%d)", state);
+ }
+}
+
+
+static struct swit parswit[] = {
+#define PRAND 0
+ { "and", 0 },
+#define PROR 1
+ { "or", 0 },
+#define PRNOT 2
+ { "not", 0 },
+#define PRLBR 3
+ { "lbrace", 0 },
+#define PRRBR 4
+ { "rbrace", 0 },
+#define PRCC 5
+ { "cc pattern", 0 },
+#define PRDATE 6
+ { "date pattern", 0 },
+#define PRFROM 7
+ { "from pattern", 0 },
+#define PRSRCH 8
+ { "search pattern", 0 },
+#define PRSUBJ 9
+ { "subject pattern", 0 },
+#define PRTO 10
+ { "to pattern", 0 },
+#define PROTHR 11
+ { "-othercomponent pattern", 15 },
+#define PRAFTR 12
+ { "after date", 0 },
+#define PRBEFR 13
+ { "before date", 0 },
+#define PRDATF 14
+ { "datefield field", 5 },
+#define PRTHREAD 15
+ { "thread msg", 0 },
+ { NULL, 0 }
+};
+
+/* DEFINITIONS FOR PATTERN MATCHING */
+
+/*
+** We really should be using re_comp() and re_exec() here. Unfortunately,
+** pick advertises that lowercase characters matches characters of both
+** cases. Since re_exec() doesn't exhibit this behavior, we are stuck
+** with this version. Furthermore, we need to be able to save and restore
+** the state of the pattern matcher in order to do things "efficiently".
+**
+** The matching power of this algorithm isn't as powerful as the re_xxx()
+** routines (no \(xxx\) and \n constructs). Such is life.
+*/
+
+#define CCHR 2
+#define CDOT 4
+#define CCL 6
+#define NCCL 8
+#define CDOL 10
+#define CEOF 11
+
+#define STAR 01
+
+#define LBSIZE 1024
+#define ESIZE 1024
+
+/*
+** DEFINITIONS FOR NEXUS
+*/
+
+#define nxtarg() (*argp ? *argp++ : NULL)
+#define prvarg() argp--
+
+#define padvise if (!talked++) advise
+
+
+enum nexus_type {
+ TYPE_GREP,
+ TYPE_DATE,
+ TYPE_OR,
+ TYPE_AND,
+ TYPE_NOT
+};
+
+struct bin_data {
+ struct nexus *left;
+ struct nexus *right;
+ enum nexus_type type;
+ int oldmsgnum;
+ boolean leftmatch;
+ boolean rightmatch;
+ boolean match;
+};
+
+struct date_data {
+ char *datef;
+ boolean after;
+ struct tws tws;
+};
+
+struct grep_data {
+ char *header;
+ char *pattern;
+ regex_t *preg;
+};
+
+static int talked;
+static int pdebug = 0;
+
+static char *datesw;
+static char **argp;
+
+
+/*
+** prototypes for date routines
+*/
+static struct tws *tws_parse(char *, int);
+static struct tws *tws_special(char *);
+
+/*
+** static prototypes
+*/
+static int gcompile(struct grep_data *, const char *);
+static int tcompile(char *, struct tws *, int);
+
+static struct nexus *parse(void);
+static struct nexus *nexp1(void);
+static struct nexus *nexp2(void);
+static struct nexus *nexp3(void);
+static struct nexus *newnexus(enum nexus_type);
+
+static boolean BINaction(struct field *, int, void *);
+static boolean NOTaction(struct field *, int, void *);
+static boolean GREPaction(struct field *, int, void *);
+static boolean DATEaction(struct field *, int, void *);
+
+static void BINfree(struct nexus **);
+static void GREPfree(struct nexus **);
+static void DATEfree(struct nexus **);
+
+static void BINdebug(void *, size_t);
+static void GREPdebug(void *, size_t);
+static void DATEdebug(void *, size_t);
+
+static int
+pcompile(char **vec, char *date)
+{
+ char *cp;
+
+ if ((cp = getenv("MHPDEBUG")) && *cp)
+ pdebug++;
+
+ argp = vec;
+ if ((datesw = date) == NULL)
+ datesw = "date";
+ talked = 0;
+
+ if ((head = parse()) == NULL)
+ return (talked ? 0 : 1);
+
+ if (*argp) {
+ padvise(NULL, "%s unexpected", *argp);
+ return 0;
+ }
+
+ return 1;
+}
+
+
+static struct nexus *
+parse(void)
+{
+ char *cp;
+ struct nexus *n, *o;
+ struct bin_data *bin;
+
+ if ((n = nexp1()) == NULL || (cp = nxtarg()) == NULL)
+ return n;
+
+ if (*cp != '-') {
+ padvise(NULL, "%s unexpected", cp);
+ return NULL;
+ }
+
+ if (*++cp == '-')
+ goto header;
+ switch (smatch(cp, parswit)) {
+ case AMBIGSW:
+ ambigsw(cp, parswit);
+ talked++;
+ return NULL;
+ case UNKWNSW:
+ fprintf(stderr, "-%s unknown\n", cp);
+ talked++;
+ return NULL;
+
+ case PROR:
+ o = newnexus(TYPE_OR);
+ bin = o->data;
+ bin->left = n;
+ if ((bin->right = parse()))
+ return o;
+ padvise(NULL, "missing disjunctive");
+ return NULL;
+
+header: ;
+ default:
+ prvarg();
+ return n;
+ }
+}
+
+static struct nexus *
+nexp1(void)
+{
+ char *cp;
+ struct nexus *n, *o;
+ struct bin_data *bin;
+
+ if ((n = nexp2()) == NULL || (cp = nxtarg()) == NULL)
+ return n;
+
+ if (*cp != '-') {
+ padvise(NULL, "%s unexpected", cp);
+ return NULL;
+ }
+
+ if (*++cp == '-')
+ goto header;
+ switch (smatch(cp, parswit)) {
+ case AMBIGSW:
+ ambigsw(cp, parswit);
+ talked++;
+ return NULL;
+ case UNKWNSW:
+ fprintf(stderr, "-%s unknown\n", cp);
+ talked++;
+ return NULL;
+
+ case PRAND:
+ o = newnexus(TYPE_AND);
+ bin = o->data;
+ bin->left = n;
+ if ((bin->right = nexp1()))
+ return o;
+ padvise(NULL, "missing conjunctive");
+ return NULL;
+
+header: ;
+ default:
+ prvarg();
+ return n;
+ }
+}
+
+
+static struct nexus *
+nexp2(void)
+{
+ char *cp;
+ struct nexus *n;
+ struct bin_data *bin;
+
+ if ((cp = nxtarg()) == NULL)
+ return NULL;
+
+ if (*cp != '-') {
+ prvarg();
+ return nexp3();
+ }
+
+ if (*++cp == '-')
+ goto header;
+ switch (smatch(cp, parswit)) {
+ case AMBIGSW:
+ ambigsw(cp, parswit);
+ talked++;
+ return NULL;
+ case UNKWNSW:
+ fprintf(stderr, "-%s unknown\n", cp);
+ talked++;
+ return NULL;
+
+ case PRNOT:
+ n = newnexus(TYPE_NOT);
+ bin = n->data;
+ if ((bin->left = nexp3()))
+ return n;
+ padvise(NULL, "missing negation");
+ return NULL;
+
+header: ;
+ default:
+ prvarg();
+ return nexp3();
+ }
+}
+
+static struct nexus *
+nexp3(void)
+{
+ int i;
+ char *cp, *dp;
+ char buffer[BUFSIZ], temp[64];
+ struct nexus *n;
+ struct grep_data *gdata;
+ struct date_data *twsd;
+
+ if ((cp = nxtarg()) == NULL)
+ return NULL;
+
+ if (*cp != '-') {
+ padvise(NULL, "%s unexpected", cp);
+ return NULL;
+ }
+
+ if (*++cp == '-') {
+ dp = ++cp;
+ goto header;
+ }
+ switch (i = smatch(cp, parswit)) {
+ case AMBIGSW:
+ ambigsw(cp, parswit);
+ talked++;
+ return NULL;
+ case UNKWNSW:
+ fprintf(stderr, "-%s unknown\n", cp);
+ talked++;
+ return NULL;
+
+ case PRLBR:
+ if ((n = parse()) == NULL) {
+ padvise(NULL, "missing group");
+ return NULL;
+ }
+ if ((cp = nxtarg()) == NULL) {
+ padvise(NULL, "missing -rbrace");
+ return NULL;
+ }
+ if (*cp++ == '-' && smatch(cp, parswit) == PRRBR)
+ return n;
+ padvise(NULL, "%s unexpected", --cp);
+ return NULL;
+
+ default:
+ prvarg();
+ return NULL;
+
+ case PRTHREAD:
+ if (!(cp = nxtarg())) { /* allow -xyz arguments */
+ padvise(NULL, "missing argument to %s", argp[-2]);
+ }
+ return createpickthread(cp);
+ case PRCC:
+ case PRDATE:
+ case PRFROM:
+ case PRTO:
+ case PRSUBJ:
+ strncpy(temp, parswit[i].sw, sizeof(temp));
+ temp[sizeof(temp) - 1] = '\0';
+ dp = *brkstring(temp, " ", NULL);
+header: ;
+ if (!(cp = nxtarg())) { /* allow -xyz arguments */
+ padvise(NULL, "missing argument to %s", argp[-2]);
+ return NULL;
+ }
+ n = newnexus(TYPE_GREP);
+ gdata = n->data;
+ gdata->header = mh_xstrdup(dp);
+ snprintf(buffer, sizeof(buffer), "%s", cp);
+ dp = buffer;
+ goto pattern;
+
+ case PRSRCH:
+ n = newnexus(TYPE_GREP);
+ gdata = n->data;
+ gdata->header = NULL;
+ body = TRUE;
+ if (!(cp = nxtarg())) { /* allow -xyz arguments */
+ padvise(NULL, "missing argument to %s", argp[-2]);
+ return NULL;
+ }
+ dp = cp;
+pattern: ;
+ if (!gcompile(gdata, dp)) {
+ padvise("regcomp", "pattern error in %s %s", argp[-2], cp);
+ return NULL;
+ }
+ return n;
+
+ case PROTHR:
+ padvise(NULL, "internal error!");
+ return NULL;
+
+ case PRDATF:
+ if (!(datesw = nxtarg()) || *datesw == '-') {
+ padvise(NULL, "missing argument to %s",
+ argp[-2]);
+ return NULL;
+ }
+ return nexp3();
+
+ case PRAFTR:
+ case PRBEFR:
+ if (!(cp = nxtarg())) { /* allow -xyz arguments */
+ padvise(NULL, "missing argument to %s", argp[-2]);
+ return NULL;
+ }
+ n = newnexus(TYPE_DATE);
+ twsd = n->data;
+ twsd->datef = datesw;
+ if (!tcompile(cp, &twsd->tws, twsd->after = i == PRAFTR)) {
+ padvise(NULL, "unable to parse %s %s", argp[-2], cp);
+ return NULL;
+ }
+ return n;
+ }
+}
+
+
+static struct nexus *
+newnexus(enum nexus_type t)
+{
+ struct nexus *p = NULL;
+ struct bin_data *bin;
+
+ p = mh_xcalloc(1, sizeof(struct nexus));
+
+ switch (t) {
+ case TYPE_NOT:
+ p->action = NOTaction;
+ p->debug = BINdebug;
+ p->free = BINfree;
+ p->data = bin = mh_xcalloc(1, sizeof(struct bin_data));
+ bin->type = t;
+ break;
+ case TYPE_AND:
+ case TYPE_OR:
+ p->action = BINaction;
+ p->debug = BINdebug;
+ p->free = BINfree;
+ p->data = bin = mh_xcalloc(1, sizeof(struct bin_data));
+ bin->type = t;
+ break;
+ case TYPE_GREP:
+ p->action = GREPaction;
+ p->debug = GREPdebug;
+ p->free = GREPfree;
+ p->data = mh_xcalloc(1, sizeof(struct grep_data));
+ break;
+ case TYPE_DATE:
+ p->action = DATEaction;
+ p->debug = DATEdebug;
+ p->free = DATEfree;
+ p->data = mh_xcalloc(1, sizeof(struct date_data));
+ break;
+ default:
+ adios(EX_SOFTWARE, NULL, "unknown nexus type %d", t);
+ }
+
+ return p;
+
+}
+
+static int
+pmatches(FILE *fp, int msgnum)
+{
+ struct field f = {{0}};
+ enum state s = FLD2;
+
+ if (!head)
+ return 1;
+
+ if (!talked++ && pdebug && head->debug) {
+ head->debug(head->data, 0);
+ }
+
+ while (s == FLD2 || s == BODY2) {
+ switch (s = m_getfld2(s, &f, fp)) {
+ case LENERR2:
+ s = FLD2;
+ /* FALL */
+ case FLD2:
+ if (head->action(&f, msgnum, head->data)) {
+ return TRUE;
+ }
+ break;
+ case BODY2:
+ if (!body) {
+ return FALSE;
+ }
+ if (head->action(&f, msgnum, head->data)) {
+ return TRUE;
+ }
+ break;
+ case IOERR2:
+ advise(NULL, "IOERR in message %d\n", msgnum);
+ return FALSE;
+ case FILEEOF2:
+ return FALSE;
+ default:
+ adios(EX_SOFTWARE, "m_getfld2", "returned unknown state %d at message %d", s, msgnum);
+ }
+ }
+ return FALSE;
+}
+
+void
+print_debug_level(size_t level)
+{
+ size_t i;
+
+ for (i = 0; i < level; i++) {
+ fputs("| ", stderr);
+ }
+}
+
+void
+BINdebug(void *data, size_t level)
+{
+ struct bin_data *bd = data;
+
+ print_debug_level(level);
+
+ switch (bd->type) {
+ case TYPE_OR:
+ fputs("OR\n", stderr);
+ break;
+ case TYPE_AND:
+ fputs("AND\n", stderr);
+ break;
+ case TYPE_NOT:
+ fputs("NOT\n", stderr);
+ break;
+ default:
+ advise(NULL, "binary nexus has unknown type: %d\n", bd->type);
+ return;
+ }
+
+ if (bd->left && bd->left->debug) {
+ bd->left->debug(bd->left->data, level+1);
+ } else {
+ print_debug_level(level+1);
+ fputs("can't debug left child\n", stderr);
+ }
+
+ if (bd->right && bd->right->debug) {
+ bd->right->debug(bd->right->data, level+1);
+ } else if (bd->type != TYPE_NOT) {
+ print_debug_level(level+1);
+ fputs("can't debug right child\n", stderr);
+ }
+}
+
+static boolean
+NOTaction(struct field *f, int msgnum, void *data)
+{
+ struct bin_data *bin = data;
+ return !bin->left->action(f, msgnum, bin->left->data);
+}
+
+static boolean
+BINaction(struct field *f, int msgnum, void *data)
+{
+ struct bin_data *bin = data;
+
+ if (bin->oldmsgnum != msgnum) {
+ bin->oldmsgnum = msgnum;
+ bin->match = FALSE;
+ bin->leftmatch = FALSE;
+ bin->rightmatch = FALSE;
+ }
+
+ if (bin->match) {
+ return bin->match;
+ }
+
+ bin->leftmatch = bin->leftmatch || bin->left->action(f, msgnum, bin->left->data);
+ bin->rightmatch = bin->rightmatch || bin->right->action(f, msgnum, bin->right->data);
+
+ switch (bin->type) {
+ case TYPE_OR:
+ bin->match = bin->leftmatch || bin->rightmatch;
+ break;
+ case TYPE_AND:
+ bin->match = bin->leftmatch && bin->rightmatch;
+ break;
+ default:
+ adios(EX_SOFTWARE, NULL, "unknown nexus type: %d\n", bin->type);
+ }
+
+ return bin->match;
+}
+
+static void
+BINfree(struct nexus **n)
+{
+ struct bin_data *bd;
+
+ if (!(*n)) {
+ return;
+ }
+
+ bd = (*n)->data;
+
+ if (bd->left && bd->left->free) {
+ bd->left->free(&bd->left);
+ } else {
+ advise(NULL, "BUG: can't free left child");
+ }
+
+ if (bd->right && bd->right->free) {
+ bd->right->free(&bd->right);
+ } else {
+ advise(NULL, "BUG: can't free right child");
+ }
+
+ mh_free0(n);
+}
+
+static int
+gcompile(struct grep_data *g, const char *astr)
+{
+ regex_t *preg = mh_xcalloc(1, sizeof(regex_t));
+ char *buf;
+ int ret;
+
+ g->preg = preg;
+ g->pattern = mh_xstrdup(astr);
+ ret = regcomp(preg, astr, REG_ICASE | REG_NOSUB);
+ if (ret != 0) {
+ buf = mh_xcalloc(BUFSIZ, sizeof(char));
+ regerror(ret, g->preg, buf, BUFSIZ*sizeof(char));
+ fprintf(stderr, "%s\n", buf);
+ return FALSE;
+ }
+ return TRUE;
+
+}
+
+static boolean
+GREPaction(struct field *f, int msgnum, void *data)
+{
+ struct grep_data *g = data;
+ int ret;
+ char buf[BUFSIZ];
+
+ if (!g->header && *f->name) {
+ return FALSE;
+ }
+
+ /* check for the right field */
+ if (!(g->header && *g->header && mh_strcasecmp(g->header, f->name)==0)) {
+ return FALSE;
+ }
+
+ if(decode_rfc2047(f->value, buf, sizeof(buf))) {
+ ret = regexec(g->preg, buf, 0, NULL, 0);
+ } else {
+ ret = regexec(g->preg, f->value, 0, NULL, 0);
+ }
+ switch (ret) {
+ case 0:
+ return TRUE;
+ case REG_NOMATCH:
+ return FALSE;
+ default:
+ regerror(ret, g->preg, buf, sizeof(buf));
+ fprintf(stderr, "%s\n", buf);
+ return FALSE;
+ }
+
+}
+
+static void
+GREPfree(struct nexus **n)
+{
+ struct grep_data *gd;
+ if (!(*n)) {
+ return;
+ }
+ gd = (*n)->data;
+ mh_free0(&gd->header);
+ regfree(gd->preg);
+ mh_free0(n);
+}
+
+static void
+GREPdebug(void *data, size_t level)
+{
+ struct grep_data *gd = data;
+ char *buf, *buf2, *pbuf, *pbuf2;
+
+ pbuf = pbuf2 = mh_xstrdup(gd->pattern);
+
+ for (;*pbuf2; pbuf2++) {
+ *pbuf2 = tolower(*pbuf2);
+ }
+
+ print_debug_level(level);
+
+ if (gd->header) {
+ buf = buf2 = mh_xstrdup(gd->header);
+ for (;*buf2; buf2++) {
+ *buf2 = tolower(*buf2);
+ }
+ fprintf(stderr, "PETTERN(%s) %s\n", buf, pbuf);
+ } else {
+ fprintf(stderr, "PETTERN(BODY) %s\n", pbuf);
+ }
+ mh_free0(&buf);
+ mh_free0(&pbuf);
+}
+
+static int
+tcompile(char *ap, struct tws *tb, int isafter)
+{
+ struct tws *tw;
+
+ if ((tw = tws_parse(ap, isafter)) == NULL)
+ return 0;
+
+ twscopy(tb, tw);