+ int publicsw = -1, zerosw = 1, vecp = 0, width = 0;
+ unsigned int seqp = 0;
+ int lo, hi, msgnum;
+ char *maildir, *folder = NULL, buf[100];
+ char *cp, **argp, **arguments;
+ char *seqs[NUMATTRS + 1], *vec[MAXARGS];
+ struct msgs_array msgs = { 0, 0, NULL };
+ struct msgs *mp;
+ char *form = NULL;
+ char *fmtstr;
+ FILE *fp;
+ char *file = NULL;
+
+ if (atexit(putzero_done) != 0) {
+ adios(EX_OSERR, NULL, "atexit failed");
+ }
+
+ setlocale(LC_ALL, "");
+ invo_name = mhbasename(argv[0]);
+
+ /* read user profile/context */
+ context_read();
+
+ arguments = getarguments(invo_name, argc, argv, 1);
+ argp = arguments;
+
+ if (strcmp(invo_name, "scan")==0) {
+ form = scanformat;
+ }
+
+ while ((cp = *argp++)) {
+ if (*cp == '-') {
+ if (*++cp == '-') {
+ vec[vecp++] = --cp;
+ goto pattern;
+ }
+ switch (smatch(cp, switches)) {
+ case AMBIGSW:
+ ambigsw(cp, switches);
+ listsw = 0; /* HACK */
+ exit(EX_USAGE);
+ case UNKWNSW:
+ adios(EX_USAGE, NULL, "-%s unknown", cp);
+
+ case HELPSW:
+ snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
+ print_help(buf, switches, 1);
+ listsw = 0; /* HACK */
+ exit(argc == 2 ? EX_OK : EX_USAGE);
+ case VERSIONSW:
+ print_version(invo_name);
+ listsw = 0; /* HACK */
+ exit(argc == 2 ? EX_OK : EX_USAGE);
+
+ case CCSW:
+ case DATESW:
+ case FROMSW:
+ case SUBJSW:
+ case TOSW:
+ case DATFDSW:
+ case AFTRSW:
+ case BEFRSW:
+ case SRCHSW:
+ case THREADSW:
+ vec[vecp++] = --cp;
+ pattern:
+ if (!(cp = *argp++)) /* allow -xyz arguments */
+ adios(EX_USAGE, NULL, "missing argument to %s",
+ argp[-2]);
+ vec[vecp++] = cp;
+ continue;
+ case OTHRSW:
+ adios(EX_SOFTWARE, NULL, "internal error!");
+
+ case ANDSW:
+ case ORSW:
+ case NOTSW:
+ case LBRSW:
+ case RBRSW:
+ vec[vecp++] = --cp;
+ continue;
+
+ case SEQSW:
+ if (!(cp = *argp++) || *cp == '-')
+ adios(EX_USAGE, NULL, "missing argument to %s",
+ argp[-2]);
+
+ /* check if too many sequences specified */
+ if (seqp >= NUMATTRS)
+ adios(EX_USAGE, NULL, "too many sequences (more than %d) specified", NUMATTRS);
+
+ if (!seq_nameok(cp))
+ exit(EX_USAGE);
+
+ seqs[seqp++] = cp;
+ continue;
+ case PUBLSW:
+ publicsw = 1;
+ continue;
+ case NPUBLSW:
+ publicsw = 0;
+ continue;
+ case ZEROSW:
+ zerosw++;
+ continue;
+ case NZEROSW:
+ zerosw = 0;
+ continue;
+
+ case LISTSW:
+ listsw = 1;
+ continue;
+ case NLISTSW:
+ listsw = 0;
+ continue;
+ case FORMATSW:
+ if (!(form = *argp++) || *form == '-') {
+ adios(EX_USAGE, NULL, "missing argument to %s", argp[-2]);
+ }
+ continue;
+ case WIDTHSW:
+ if (!(cp = *argp++) || *cp == '-') {
+ adios(EX_USAGE, NULL, "missing argument to %s",
+ argp[-2]);
+ }
+ width = atoi(cp);
+ continue;
+ case FILESW:
+ if (!(cp = *argp++) || (cp[0] == '-' && cp[1])) {
+ adios(EX_USAGE, NULL, "missing argument to %s",
+ argp[-2]);
+ }
+ if (strcmp(file = cp, "-")!=0) {
+ file = mh_xstrdup(expanddir(cp));
+ }
+ continue;
+ }
+ }
+ if (*cp == '+' || *cp == '@') {
+ if (folder)
+ adios(EX_USAGE, NULL, "only one folder at a time!");
+ else
+ folder = mh_xstrdup(expandfol(cp));
+ } else
+ app_msgarg(&msgs, cp);
+ }
+ vec[vecp] = NULL;
+
+ fmtstr = new_fs(form, "pick.default");
+
+ if (file) {
+ if (folder) {
+ adios(EX_USAGE, NULL, "\"+folder\" not allowed with -file");
+ }
+ if (msgs.size) {
+ adios(EX_USAGE, NULL, "\"msgs\" not allowed with -file");
+ }
+ if (vecp) {
+ adios(EX_USAGE, NULL, "section arguments not allowed with -file");
+ }
+
+ scan_mbox(file, fmtstr, width);
+ exit(EX_OK);
+ }
+
+ /*
+ ** If we didn't specify which messages to search,
+ ** then search the whole folder.
+ */
+ if (!msgs.size)
+ app_msgarg(&msgs, seq_all);
+
+ if (!folder)
+ folder = getcurfol();
+ maildir = toabsdir(folder);
+
+ if (chdir(maildir) == NOTOK)
+ adios(EX_OSERR, maildir, "unable to change directory to");
+
+ /* read folder and create message structure */
+ if (!(mp = folder_read(folder)))
+ adios(EX_IOERR, NULL, "unable to read folder %s", folder);
+
+ /* check for empty folder */
+ if (mp->nummsg == 0)
+ adios(EX_DATAERR, NULL, "no messages in %s", folder);
+
+ /* parse all the message ranges/sequences and set SELECTED */
+ for (msgnum = 0; msgnum < msgs.size; msgnum++)
+ if (!m_convert(mp, msgs.msgs[msgnum]))
+ exit(EX_USAGE);
+ seq_setprev(mp); /* set the previous-sequence */
+
+ /*
+ ** If we aren't saving the results to a sequence,
+ ** we default to list the results.
+ */
+ if (listsw == -1)
+ listsw = !seqp;
+
+ if (publicsw == 1 && is_readonly(mp))
+ adios(EX_NOPERM, NULL, "folder %s is read-only, so -public not allowed",
+ folder);
+
+ if (!pcompile(vec, NULL))
+ exit(EX_SOFTWARE);
+
+ lo = mp->lowsel;
+ hi = mp->hghsel;
+
+ /*
+ ** If printing message numbers to standard out,
+ ** force line buffering on.
+ */
+ if (listsw)
+ setvbuf(stdout, NULL, _IOLBF, 0);
+
+ /*
+ ** Scan through all the SELECTED messages and check for a
+ ** match. If the message does not match, then unselect it.
+ */
+ for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
+ if (is_selected(mp, msgnum)) {
+ if ((fp = fopen(cp = m_name(msgnum), "r")) == NULL)
+ admonish(cp, "unable to read message");
+ if (fp && pmatches(fp, msgnum)) {
+ if (msgnum < lo)
+ lo = msgnum;
+ if (msgnum > hi)
+ hi = msgnum;
+
+ 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);
+ }
+ }
+
+ if (head) {
+ head->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 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;