2 ** pick.c -- search for messages by content
4 ** This code is Copyright (c) 2002, 2008, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
12 #include <h/scansbr.h>
13 #include <h/fmt_scan.h>
20 #ifdef HAVE_SYS_TIME_H
21 # include <sys/time.h>
25 static struct swit switches[] = {
39 { "date pattern", 0 },
41 { "from pattern", 0 },
43 { "search pattern", 0 },
45 { "subject pattern", 0 },
49 { "-othercomponent pattern", 0 },
55 { "datefield field", 5 }, /* 5 chars required to differ from -date */
57 { "sequence name", 0 },
71 { "format format", 0 },
73 { "width columns", 0 },
81 char *version=VERSION;
84 boolean (*action)(struct field *, int, void *);
85 void (*free)(struct nexus **);
86 void (*debug)(void *, size_t);
91 static struct nexus *head;
92 static boolean body = FALSE;
97 static int pcompile(char **, char *);
98 static int pmatches(FILE *, int);
101 static int listsw = -1;
105 static void printmsg(FILE *, struct msgs *, int, char *, int);
108 main(int argc, char **argv)
110 int publicsw = -1, zerosw = 1, vecp = 0, width = 0;
111 unsigned int seqp = 0;
113 char *maildir, *folder = NULL, buf[100];
114 char *cp, **argp, **arguments;
115 char *seqs[NUMATTRS + 1], *vec[MAXARGS];
116 struct msgs_array msgs = { 0, 0, NULL };
122 if (atexit(putzero_done) != 0) {
123 adios(EX_OSERR, NULL, "atexit failed");
126 setlocale(LC_ALL, "");
127 invo_name = mhbasename(argv[0]);
129 /* read user profile/context */
132 arguments = getarguments(invo_name, argc, argv, 1);
135 if (strcmp(invo_name, "scan")==0) {
139 while ((cp = *argp++)) {
145 switch (smatch(cp, switches)) {
147 ambigsw(cp, switches);
148 listsw = 0; /* HACK */
151 adios(EX_USAGE, NULL, "-%s unknown", cp);
154 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
155 print_help(buf, switches, 1);
156 listsw = 0; /* HACK */
157 exit(argc == 2 ? EX_OK : EX_USAGE);
159 print_version(invo_name);
160 listsw = 0; /* HACK */
161 exit(argc == 2 ? EX_OK : EX_USAGE);
174 if (!(cp = *argp++)) /* allow -xyz arguments */
175 adios(EX_USAGE, NULL, "missing argument to %s",
180 adios(EX_SOFTWARE, NULL, "internal error!");
191 if (!(cp = *argp++) || *cp == '-')
192 adios(EX_USAGE, NULL, "missing argument to %s",
195 /* check if too many sequences specified */
196 if (seqp >= NUMATTRS)
197 adios(EX_USAGE, NULL, "too many sequences (more than %d) specified", NUMATTRS);
224 if (!(form = *argp++) || *form == '-') {
225 adios(EX_USAGE, NULL, "missing argument to %s", argp[-2]);
229 if (!(cp = *argp++) || *cp == '-') {
230 adios(EX_USAGE, NULL, "missing argument to %s",
237 if (*cp == '+' || *cp == '@') {
239 adios(EX_USAGE, NULL, "only one folder at a time!");
241 folder = mh_xstrdup(expandfol(cp));
243 app_msgarg(&msgs, cp);
247 fmtstr = new_fs(form, "pick.default");
250 ** If we didn't specify which messages to search,
251 ** then search the whole folder.
254 app_msgarg(&msgs, seq_all);
257 folder = getcurfol();
258 maildir = toabsdir(folder);
260 if (chdir(maildir) == NOTOK)
261 adios(EX_OSERR, maildir, "unable to change directory to");
263 /* read folder and create message structure */
264 if (!(mp = folder_read(folder)))
265 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
267 /* check for empty folder */
269 adios(EX_DATAERR, NULL, "no messages in %s", folder);
271 /* parse all the message ranges/sequences and set SELECTED */
272 for (msgnum = 0; msgnum < msgs.size; msgnum++)
273 if (!m_convert(mp, msgs.msgs[msgnum]))
275 seq_setprev(mp); /* set the previous-sequence */
278 ** If we aren't saving the results to a sequence,
279 ** we default to list the results.
284 if (publicsw == 1 && is_readonly(mp))
285 adios(EX_NOPERM, NULL, "folder %s is read-only, so -public not allowed",
288 if (!pcompile(vec, NULL))
295 ** If printing message numbers to standard out,
296 ** force line buffering on.
299 setvbuf(stdout, NULL, _IOLBF, 0);
302 ** Scan through all the SELECTED messages and check for a
303 ** match. If the message does not match, then unselect it.
305 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
306 if (is_selected(mp, msgnum)) {
307 if ((fp = fopen(cp = m_name(msgnum), "r")) == NULL)
308 admonish(cp, "unable to read message");
309 if (fp && pmatches(fp, msgnum)) {
316 printmsg(fp, mp, msgnum, fmtstr, width);
319 /* if it doesn't match, then unselect it */
320 unset_selected(mp, msgnum);
333 adios(EX_DATAERR, NULL, "no messages match specification");
338 ** Add the matching messages to sequences
340 for (seqp = 0; seqs[seqp]; seqp++)
341 if (!seq_addsel(mp, seqs[seqp], publicsw, zerosw))
345 ** Print total matched if not printing each matched message number.
348 printf("%d hit%s\n", mp->numsel, mp->numsel == 1 ? "" : "s");
351 context_replace(curfolder, folder); /* update current folder */
352 seq_save(mp); /* synchronize message sequences */
353 context_save(); /* save the context file */
354 folder_free(mp); /* free folder/message structure */
355 listsw = 0; /* HACK */
363 if (listsw && !isatty(fileno(stdout)))
368 printmsg(FILE *f, struct msgs *mp, int msgnum, char *fmtstr, int width)
372 boolean unseen = FALSE;
374 fseek(f, 0L, SEEK_SET);
376 seqnum = seq_getnum(mp, seq_unseen);
377 unseen = in_sequence(mp, seqnum, msgnum);
379 switch (state = scan(f, msgnum, SCN_FOLD, fmtstr,
380 width, msgnum==mp->curmsg, unseen)) {
385 advise(NULL, "message %d: empty", msgnum);
388 adios(EX_SOFTWARE, NULL, "scan() botch(%d)", state);
393 static struct swit parswit[] = {
407 { "date pattern", 0 },
409 { "from pattern", 0 },
411 { "search pattern", 0 },
413 { "subject pattern", 0 },
417 { "-othercomponent pattern", 15 },
421 { "before date", 0 },
423 { "datefield field", 5 },
427 /* DEFINITIONS FOR PATTERN MATCHING */
430 ** We really should be using re_comp() and re_exec() here. Unfortunately,
431 ** pick advertises that lowercase characters matches characters of both
432 ** cases. Since re_exec() doesn't exhibit this behavior, we are stuck
433 ** with this version. Furthermore, we need to be able to save and restore
434 ** the state of the pattern matcher in order to do things "efficiently".
436 ** The matching power of this algorithm isn't as powerful as the re_xxx()
437 ** routines (no \(xxx\) and \n constructs). Such is life.
453 ** DEFINITIONS FOR NEXUS
456 #define nxtarg() (*argp ? *argp++ : NULL)
457 #define prvarg() argp--
459 #define padvise if (!talked++) advise
473 enum nexus_type type;
493 static int pdebug = 0;
500 ** prototypes for date routines
502 static struct tws *tws_parse(char *, int);
503 static struct tws *tws_special(char *);
508 static int gcompile(struct grep_data *, const char *);
509 static int tcompile(char *, struct tws *, int);
511 static struct nexus *parse(void);
512 static struct nexus *nexp1(void);
513 static struct nexus *nexp2(void);
514 static struct nexus *nexp3(void);
515 static struct nexus *newnexus(enum nexus_type);
517 static boolean BINaction(struct field *, int, void *);
518 static boolean NOTaction(struct field *, int, void *);
519 static boolean GREPaction(struct field *, int, void *);
520 static boolean DATEaction(struct field *, int, void *);
522 static void BINfree(struct nexus **);
523 static void GREPfree(struct nexus **);
524 static void DATEfree(struct nexus **);
526 static void BINdebug(void *, size_t);
527 static void GREPdebug(void *, size_t);
528 static void DATEdebug(void *, size_t);
531 pcompile(char **vec, char *date)
535 if ((cp = getenv("MHPDEBUG")) && *cp)
539 if ((datesw = date) == NULL)
543 if ((head = parse()) == NULL)
544 return (talked ? 0 : 1);
547 padvise(NULL, "%s unexpected", *argp);
555 static struct nexus *
560 struct bin_data *bin;
562 if ((n = nexp1()) == NULL || (cp = nxtarg()) == NULL)
566 padvise(NULL, "%s unexpected", cp);
572 switch (smatch(cp, parswit)) {
574 ambigsw(cp, parswit);
578 fprintf(stderr, "-%s unknown\n", cp);
583 o = newnexus(TYPE_OR);
586 if ((bin->right = parse()))
588 padvise(NULL, "missing disjunctive");
598 static struct nexus *
603 struct bin_data *bin;
605 if ((n = nexp2()) == NULL || (cp = nxtarg()) == NULL)
609 padvise(NULL, "%s unexpected", cp);
615 switch (smatch(cp, parswit)) {
617 ambigsw(cp, parswit);
621 fprintf(stderr, "-%s unknown\n", cp);
626 o = newnexus(TYPE_AND);
629 if ((bin->right = nexp1()))
631 padvise(NULL, "missing conjunctive");
642 static struct nexus *
647 struct bin_data *bin;
649 if ((cp = nxtarg()) == NULL)
659 switch (smatch(cp, parswit)) {
661 ambigsw(cp, parswit);
665 fprintf(stderr, "-%s unknown\n", cp);
670 n = newnexus(TYPE_NOT);
672 if ((bin->left = nexp3()))
674 padvise(NULL, "missing negation");
684 static struct nexus *
689 char buffer[BUFSIZ], temp[64];
691 struct grep_data *gdata;
692 struct date_data *twsd;
694 if ((cp = nxtarg()) == NULL)
698 padvise(NULL, "%s unexpected", cp);
706 switch (i = smatch(cp, parswit)) {
708 ambigsw(cp, parswit);
712 fprintf(stderr, "-%s unknown\n", cp);
717 if ((n = parse()) == NULL) {
718 padvise(NULL, "missing group");
721 if ((cp = nxtarg()) == NULL) {
722 padvise(NULL, "missing -rbrace");
725 if (*cp++ == '-' && smatch(cp, parswit) == PRRBR)
727 padvise(NULL, "%s unexpected", --cp);
739 strncpy(temp, parswit[i].sw, sizeof(temp));
740 temp[sizeof(temp) - 1] = '\0';
741 dp = *brkstring(temp, " ", NULL);
743 if (!(cp = nxtarg())) { /* allow -xyz arguments */
744 padvise(NULL, "missing argument to %s", argp[-2]);
747 n = newnexus(TYPE_GREP);
749 gdata->header = mh_xstrdup(dp);
750 snprintf(buffer, sizeof(buffer), "%s", cp);
755 n = newnexus(TYPE_GREP);
757 gdata->header = NULL;
759 if (!(cp = nxtarg())) { /* allow -xyz arguments */
760 padvise(NULL, "missing argument to %s", argp[-2]);
765 if (!gcompile(gdata, dp)) {
766 padvise("regcomp", "pattern error in %s %s", argp[-2], cp);
772 padvise(NULL, "internal error!");
776 if (!(datesw = nxtarg()) || *datesw == '-') {
777 padvise(NULL, "missing argument to %s",
785 if (!(cp = nxtarg())) { /* allow -xyz arguments */
786 padvise(NULL, "missing argument to %s", argp[-2]);
789 n = newnexus(TYPE_DATE);
791 twsd->datef = datesw;
792 if (!tcompile(cp, &twsd->tws, twsd->after = i == PRAFTR)) {
793 padvise(NULL, "unable to parse %s %s", argp[-2], cp);
801 static struct nexus *
802 newnexus(enum nexus_type t)
804 struct nexus *p = NULL;
805 struct bin_data *bin;
807 p = mh_xcalloc(1, sizeof(struct nexus));
811 p->action = NOTaction;
814 p->data = bin = mh_xcalloc(1, sizeof(struct bin_data));
819 p->action = BINaction;
822 p->data = bin = mh_xcalloc(1, sizeof(struct bin_data));
826 p->action = GREPaction;
827 p->debug = GREPdebug;
829 p->data = mh_xcalloc(1, sizeof(struct grep_data));
832 p->action = DATEaction;
833 p->debug = DATEdebug;
835 p->data = mh_xcalloc(1, sizeof(struct date_data));
838 adios(EX_SOFTWARE, NULL, "unknown nexus type %d", t);
846 pmatches(FILE *fp, int msgnum)
848 struct field f = {{0}};
854 if (!talked++ && pdebug && head->debug) {
855 head->debug(head->data, 0);
858 while (s == FLD2 || s == BODY2) {
859 switch (s = m_getfld2(s, &f, fp)) {
864 if (head->action(&f, msgnum, head->data)) {
872 if (head->action(&f, msgnum, head->data)) {
877 advise(NULL, "IOERR in message %d\n", msgnum);
882 adios(EX_SOFTWARE, "m_getfld2", "returned unknown state %d at message %d", s, msgnum);
889 print_debug_level(size_t level)
893 for (i = 0; i < level; i++) {
899 BINdebug(void *data, size_t level)
901 struct bin_data *bd = data;
903 print_debug_level(level);
907 fputs("OR\n", stderr);
910 fputs("AND\n", stderr);
913 fputs("NOT\n", stderr);
916 advise(NULL, "binary nexus has unknown type: %d\n", bd->type);
920 if (bd->left && bd->left->debug) {
921 bd->left->debug(bd->left->data, level+1);
923 print_debug_level(level+1);
924 fputs("can't debug left child\n", stderr);
927 if (bd->right && bd->right->debug) {
928 bd->right->debug(bd->right->data, level+1);
929 } else if (bd->type != TYPE_NOT) {
930 print_debug_level(level+1);
931 fputs("can't debug right child\n", stderr);
936 NOTaction(struct field *f, int msgnum, void *data)
938 struct bin_data *bin = data;
939 return !bin->left->action(f, msgnum, bin->left->data);
943 BINaction(struct field *f, int msgnum, void *data)
945 struct bin_data *bin = data;
947 if (bin->oldmsgnum != msgnum) {
948 bin->oldmsgnum = msgnum;
950 bin->leftmatch = FALSE;
951 bin->rightmatch = FALSE;
958 bin->leftmatch = bin->leftmatch || bin->left->action(f, msgnum, bin->left->data);
959 bin->rightmatch = bin->rightmatch || bin->right->action(f, msgnum, bin->right->data);
963 bin->match = bin->leftmatch || bin->rightmatch;
966 bin->match = bin->leftmatch && bin->rightmatch;
969 adios(EX_SOFTWARE, NULL, "unknown nexus type: %d\n", bin->type);
976 BINfree(struct nexus **n)
978 struct nexus *bin = *n;
979 struct bin_data *bd = bin->data;
981 if (bd->left && bd->left->free) {
982 bd->left->free(&bd->left);
984 advise(NULL, "BUG: can't free left child");
987 if (bd->right && bd->right->free) {
988 bd->right->free(&bd->right);
990 advise(NULL, "BUG: can't free right child");
997 gcompile(struct grep_data *g, const char *astr)
999 regex_t *preg = mh_xcalloc(1, sizeof(regex_t));
1004 g->pattern = mh_xstrdup(astr);
1005 ret = regcomp(preg, astr, REG_ICASE | REG_NOSUB);
1007 buf = mh_xcalloc(BUFSIZ, sizeof(char));
1008 regerror(ret, g->preg, buf, BUFSIZ*sizeof(char));
1009 fprintf(stderr, "%s\n", buf);
1017 GREPaction(struct field *f, int msgnum, void *data)
1019 struct grep_data *g = data;
1023 /* check for the write field */
1024 if (g->header && *g->header && mh_strcasecmp(g->header, f->name)) {
1028 if (!g->header && *f->name) {
1032 ret = regexec(g->preg, f->value, 0, NULL, 0) == REG_NOMATCH;
1039 buf = mh_xcalloc(BUFSIZ, sizeof(char));
1040 regerror(ret, g->preg, buf, BUFSIZ*sizeof(char));
1041 fprintf(stderr, "%s\n", buf);
1048 GREPfree(struct nexus **n)
1050 struct grep_data *gd = (*n)->data;
1051 mh_free0(&gd->header);
1057 GREPdebug(void *data, size_t level)
1059 struct grep_data *gd = data;
1060 char *buf, *buf2, *pbuf, *pbuf2;
1062 pbuf = pbuf2 = mh_xstrdup(gd->pattern);
1064 for (;*pbuf2; pbuf2++) {
1065 *pbuf2 = tolower(*pbuf2);
1068 print_debug_level(level);
1071 buf = buf2 = mh_xstrdup(gd->header);
1072 for (;*buf2; buf2++) {
1073 *buf2 = tolower(*buf2);
1075 fprintf(stderr, "PETTERN(%s) %s\n", buf, pbuf);
1077 fprintf(stderr, "PETTERN(BODY) %s\n", pbuf);
1084 tcompile(char *ap, struct tws *tb, int isafter)
1088 if ((tw = tws_parse(ap, isafter)) == NULL)
1097 tws_parse(char *ap, int isafter)
1099 char buffer[BUFSIZ];
1100 struct tws *tw, *ts;
1102 if ((tw = tws_special(ap)) != NULL) {
1103 tw->tw_sec = tw->tw_min = isafter ? 59 : 0;
1104 tw->tw_hour = isafter ? 23 : 0;
1107 if ((tw = dparsetime(ap)) != NULL)
1110 if ((ts = dlocaltimenow()) == NULL)
1113 snprintf(buffer, sizeof(buffer), "%s %s", ap, dtwszone(ts));
1114 if ((tw = dparsetime(buffer)) != NULL)
1117 snprintf(buffer, sizeof(buffer), "%s %02d:%02d:%02d %s", ap,
1118 ts->tw_hour, ts->tw_min, ts->tw_sec, dtwszone(ts));
1119 if ((tw = dparsetime(buffer)) != NULL)
1122 snprintf(buffer, sizeof(buffer), "%02d %s %04d %s",
1123 ts->tw_mday, tw_moty[ts->tw_mon], ts->tw_year, ap);
1124 if ((tw = dparsetime(buffer)) != NULL)
1127 snprintf(buffer, sizeof(buffer), "%02d %s %04d %s %s",
1128 ts->tw_mday, tw_moty[ts->tw_mon], ts->tw_year,
1130 if ((tw = dparsetime(buffer)) != NULL)
1138 tws_special(char *ap)
1145 if (!mh_strcasecmp(ap, "today"))
1146 return dlocaltime(&clock);
1147 if (!mh_strcasecmp(ap, "yesterday")) {
1148 clock -= (long) (60 * 60 * 24);
1149 return dlocaltime(&clock);
1151 if (!mh_strcasecmp(ap, "tomorrow")) {
1152 clock += (long) (60 * 60 * 24);
1153 return dlocaltime(&clock);
1156 for (i = 0; tw_ldotw[i]; i++)
1157 if (!mh_strcasecmp(ap, tw_ldotw[i]))
1160 if ((tw = dlocaltime(&clock)) == NULL)
1162 if ((i -= tw->tw_wday) > 0)
1168 else /* -ddd days ago */
1169 i = atoi(ap); /* we should error check this */
1171 clock += (long) ((60 * 60 * 24) * i);
1172 return dlocaltime(&clock);
1177 DATEaction(struct field *f, int msgnum, void *data)
1179 struct date_data *dd = data;
1180 boolean state = FALSE;
1184 if (mh_strcasecmp(f->name, dd->datef)!=0) {
1187 bp = mh_xstrdup(f->value);
1188 if ((tw = dparsetime(bp)) == NULL) {
1189 advise(NULL, "unable to parse %s field in message %d, not matching...", dd->datef, msgnum);
1191 } else if (dd->after) {
1192 state = twsort(tw, &dd->tws) > 0;
1194 state = twsort(tw, &dd->tws) < 0;
1203 DATEfree(struct nexus **n)
1205 struct date_data *dd = (*n)->data;
1207 mh_free0(&dd->datef);
1212 DATEdebug(void *data, size_t level)
1214 struct date_data *dd = data;
1215 print_debug_level(level);
1216 fprintf(stderr, "TEMPORAL(%s) %s: %s\n",dd->after ? "after" : "before", dd->datef, dasctime(&dd->tws));