+ if (listsw) {
+ printmsg(fp, mp, msgnum, fmtstr, width);
+ }
+ } else {
+ /* if it doesn't match, then unselect it */
+ unset_selected(mp, msgnum);
+ }
+ if (fp)
+ fclose(fp);
+ }
+ }
+
+ nexus_free(&head);
+
+ mp->lowsel = lo;
+ mp->hghsel = hi;
+
+ if (mp->numsel <= 0)
+ adios(EX_DATAERR, NULL, "no messages match specification");
+
+ seqs[seqp] = NULL;
+
+ /*
+ ** Add the matching messages to sequences
+ */
+ for (seqp = 0; seqs[seqp]; seqp++)
+ if (!seq_addsel(mp, seqs[seqp], publicsw, zerosw))
+ exit(EX_USAGE);
+
+ /*
+ ** Print total matched if not printing each matched message number.
+ */
+ if (!listsw) {
+ printf("%d hit%s\n", mp->numsel, mp->numsel == 1 ? "" : "s");
+ }
+
+ context_replace(curfolder, folder); /* update current folder */
+ seq_save(mp); /* synchronize message sequences */
+ context_save(); /* save the context file */
+ folder_free(mp); /* free folder/message structure */
+ 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 NEXUS
+*/
+
+#define nxtarg() (*argp ? *argp++ : NULL)
+#define prvarg() argp--
+
+#define padvise if (!talked++) advise
+
+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 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(or_t);
+ bin = &o->data.b;
+ 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(and_t);
+ bin = &o->data.b;
+ 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(not_t);
+ bin = &n->data.b;
+ 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(grep_t);
+ gdata = &n->data.g;
+ gdata->header = mh_xstrdup(dp);
+ snprintf(buffer, sizeof(buffer), "%s", cp);
+ dp = buffer;