Add ?[list]?string searches
[s-mailx.git] / list.c
blob1e92ca796300c84c38097ec253ac1dfdc47375eb
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Message (search a.k.a. argument) list handling.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2014 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
6 */
7 /*
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
40 #ifndef HAVE_AMALGAMATION
41 # include "nail.h"
42 #endif
44 enum idfield {
45 ID_REFERENCES,
46 ID_IN_REPLY_TO
49 enum {
50 CMNEW = 1<<0, /* New messages */
51 CMOLD = 1<<1, /* Old messages */
52 CMUNREAD = 1<<2, /* Unread messages */
53 CMDELETED =1<<3, /* Deleted messages */
54 CMREAD = 1<<4, /* Read messages */
55 CMFLAG = 1<<5, /* Flagged messages */
56 CMANSWER = 1<<6, /* Answered messages */
57 CMDRAFT = 1<<7, /* Draft messages */
58 CMSPAM = 1<<8 /* Spam messages */
61 struct coltab {
62 char co_char; /* What to find past : */
63 int co_bit; /* Associated modifier bit */
64 int co_mask; /* m_status bits to mask */
65 int co_equal; /* ... must equal this */
68 struct lex {
69 char l_char;
70 enum ltoken l_token;
73 static struct coltab const _coltab[] = {
74 { 'n', CMNEW, MNEW, MNEW },
75 { 'o', CMOLD, MNEW, 0 },
76 { 'u', CMUNREAD, MREAD, 0 },
77 { 'd', CMDELETED, MDELETED, MDELETED },
78 { 'r', CMREAD, MREAD, MREAD },
79 { 'f', CMFLAG, MFLAGGED, MFLAGGED },
80 { 'a', CMANSWER, MANSWERED, MANSWERED },
81 { 't', CMDRAFT, MDRAFTED, MDRAFTED },
82 { 's', CMSPAM, MSPAM, MSPAM },
83 { '\0', 0, 0, 0 }
86 static struct lex const _singles[] = {
87 { '$', TDOLLAR },
88 { '.', TDOT },
89 { '^', TUP },
90 { '*', TSTAR },
91 { '-', TDASH },
92 { '+', TPLUS },
93 { '(', TOPEN },
94 { ')', TCLOSE },
95 { ',', TCOMMA },
96 { ';', TSEMI },
97 { '`', TBACK },
98 { '\0', 0 }
101 static int lastcolmod;
102 static size_t STRINGLEN;
103 static int lexnumber; /* Number of TNUMBER from scan() */
104 static char *lexstring; /* String from TSTRING, scan() */
105 static int regretp; /* Pointer to TOS of regret tokens */
106 static int regretstack[REGDEP]; /* Stack of regretted tokens */
107 static char *string_stack[REGDEP]; /* Stack of regretted strings */
108 static int numberstack[REGDEP]; /* Stack of regretted numbers */
109 static int threadflag; /* mark entire threads */
111 /* Append, taking care of resizes */
112 static char ** add_to_namelist(char ***namelist, size_t *nmlsize,
113 char **np, char *string);
115 /* Mark all messages that the user wanted from the command line in the message
116 * structure. Return 0 on success, -1 on error */
117 static int markall(char *buf, int f);
119 /* Turn the character after a colon modifier into a bit value */
120 static int evalcol(int col);
122 /* Check the passed message number for legality and proper flags. If f is
123 * MDELETED, then either kind will do. Otherwise, the message has to be
124 * undeleted */
125 static int check(int mesg, int f);
127 /* Scan out a single lexical item and return its token number, updating the
128 * string pointer passed **sp. Also, store the value of the number or string
129 * scanned in lexnumber or lexstring as appropriate. In any event, store the
130 * scanned `thing' in lexstring */
131 static int scan(char **sp);
133 /* Unscan the named token by pushing it onto the regret stack */
134 static void regret(int token);
136 /* Reset all the scanner global variables */
137 static void scaninit(void);
139 /* See if the passed name sent the passed message */
140 static bool_t _matchsender(struct message *mp, char const *str, bool_t allnet);
142 static bool_t _matchmid(struct message *mp, char *id, enum idfield idfield);
144 /* See if the given string matches.
145 * For the purpose of the scan, we ignore case differences.
146 * This is the engine behind the `/' search */
147 static bool_t _match_dash(struct message *mp, char const *str);
149 /* See if the given search expression matches.
150 * For the purpose of the scan, we ignore case differences.
151 * This is the engine behind the `?[..]?' search */
152 static bool_t _match_qm(struct message *mp, struct search_expr *sep);
154 /* Unmark the named message */
155 static void unmark(int mesg);
157 /* Return the message number corresponding to the passed meta character */
158 static int metamess(int meta, int f);
160 static char **
161 add_to_namelist(char ***namelist, size_t *nmlsize, char **np, char *string)
163 size_t idx;
164 NYD_ENTER;
166 if ((idx = PTR2SIZE(np - *namelist)) >= *nmlsize) {
167 *namelist = srealloc(*namelist, (*nmlsize += 8) * sizeof *np);
168 np = *namelist + idx;
170 *np++ = string;
171 NYD_LEAVE;
172 return np;
175 static int
176 markall(char *buf, int f)
178 #define markall_ret(i) do { rv = i; goto jleave; } while (0);
180 /* TODO use a bit carrier for all the states */
181 char **np, **nq, **namelist, *bufp, *id = NULL, *cp;
182 int rv = 0, i, tok, beg, mc, other, valdot, colmod, colresult;
183 struct message *mp, *mx;
184 bool_t star, topen, tback;
185 size_t j, nmlsize;
186 enum idfield idfield = ID_REFERENCES;
187 #ifdef HAVE_IMAP
188 int gotheaders;
189 #endif
190 NYD_ENTER;
192 lexstring = ac_alloc(STRINGLEN = 2 * strlen(buf) + 1);
193 valdot = (int)PTR2SIZE(dot - message + 1);
194 colmod = 0;
196 for (i = 1; i <= msgCount; ++i) {
197 enum mflag mf;
199 mf = message[i - 1].m_flag;
200 if (mf & MMARK)
201 mf |= MOLDMARK;
202 else
203 mf &= ~MOLDMARK;
204 mf &= ~MMARK;
205 message[i - 1].m_flag = mf;
208 namelist = smalloc((nmlsize = 8) * sizeof *namelist);
209 np = &namelist[0];
210 scaninit();
211 bufp = buf;
212 beg = mc = star = other = topen = tback = FAL0;
213 #ifdef HAVE_IMAP
214 gotheaders = 0;
215 #endif
217 for (tok = scan(&bufp); tok != TEOL;) {
218 switch (tok) {
219 case TNUMBER:
220 number:
221 if (star) {
222 fprintf(stderr, tr(112, "No numbers mixed with *\n"));
223 markall_ret(-1)
225 list_saw_numbers = TRU1;
226 mc++;
227 other++;
228 if (beg != 0) {
229 if (check(lexnumber, f))
230 markall_ret(-1)
231 i = beg;
232 while (mb.mb_threaded ? 1 : i <= lexnumber) {
233 if (!(message[i - 1].m_flag & MHIDDEN) &&
234 (f == MDELETED || !(message[i - 1].m_flag & MDELETED)))
235 mark(i, f);
236 if (mb.mb_threaded) {
237 if (i == lexnumber)
238 break;
239 mx = next_in_thread(&message[i - 1]);
240 if (mx == NULL)
241 markall_ret(-1)
242 i = (int)PTR2SIZE(mx - message + 1);
243 } else
244 ++i;
246 beg = 0;
247 break;
249 beg = lexnumber;
250 if (check(beg, f))
251 markall_ret(-1)
252 tok = scan(&bufp);
253 regret(tok);
254 if (tok != TDASH) {
255 mark(beg, f);
256 beg = 0;
258 break;
260 case TPLUS:
261 msglist_is_single = FAL0;
262 if (beg != 0) {
263 printf(tr(113, "Non-numeric second argument\n"));
264 markall_ret(-1)
266 i = valdot;
267 do {
268 if (mb.mb_threaded) {
269 mx = next_in_thread(&message[i - 1]);
270 i = mx ? (int)PTR2SIZE(mx - message + 1) : msgCount + 1;
271 } else
272 ++i;
273 if (i > msgCount) {
274 fprintf(stderr, tr(114, "Referencing beyond EOF\n"));
275 markall_ret(-1)
277 } while (message[i-1].m_flag == MHIDDEN ||
278 (message[i - 1].m_flag & MDELETED) != (unsigned)f);
279 mark(i, f);
280 break;
282 case TDASH:
283 msglist_is_single = FAL0;
284 if (beg == 0) {
285 i = valdot;
286 do {
287 if (mb.mb_threaded) {
288 mx = prev_in_thread(&message[i - 1]);
289 i = mx ? (int)PTR2SIZE(mx - message + 1) : 0;
290 } else
291 --i;
292 if (i <= 0) {
293 fprintf(stderr, tr(115, "Referencing before 1\n"));
294 markall_ret(-1)
296 } while ((message[i - 1].m_flag & MHIDDEN) ||
297 (message[i - 1].m_flag & MDELETED) != (unsigned)f);
298 mark(i, f);
300 break;
302 case TSTRING:
303 msglist_is_single = FAL0;
304 if (beg != 0) {
305 fprintf(stderr, tr(116, "Non-numeric second argument\n"));
306 markall_ret(-1)
308 ++other;
309 if (lexstring[0] == ':') {
310 colresult = evalcol(lexstring[1]);
311 if (colresult == 0) {
312 fprintf(stderr, tr(117, "Unknown colon modifier \"%s\"\n"),
313 lexstring);
314 markall_ret(-1)
316 colmod |= colresult;
318 else
319 np = add_to_namelist(&namelist, &nmlsize, np, savestr(lexstring));
320 break;
322 case TOPEN:
323 #ifdef HAVE_IMAP_SEARCH
324 msglist_is_single = FAL0;
325 if (imap_search(lexstring, f) == STOP)
326 markall_ret(-1)
327 topen = TRU1;
328 #else
329 fprintf(stderr, tr(42,
330 "`%s': the used selector is optional and not available\n"),
331 lexstring);
332 markall_ret(-1)
333 #endif
334 break;
336 case TDOLLAR:
337 case TUP:
338 case TDOT:
339 case TSEMI:
340 msglist_is_single = FAL0;
341 lexnumber = metamess(lexstring[0], f);
342 if (lexnumber == -1)
343 markall_ret(-1)
344 goto number;
346 case TBACK:
347 msglist_is_single = FAL0;
348 tback = TRU1;
349 for (i = 1; i <= msgCount; i++) {
350 if ((message[i - 1].m_flag & MHIDDEN) ||
351 (message[i - 1].m_flag & MDELETED) != (unsigned)f)
352 continue;
353 if (message[i - 1].m_flag & MOLDMARK)
354 mark(i, f);
356 break;
358 case TSTAR:
359 msglist_is_single = FAL0;
360 if (other) {
361 fprintf(stderr, tr(118, "Can't mix \"*\" with anything\n"));
362 markall_ret(-1)
364 star = TRU1;
365 break;
367 case TCOMMA:
368 msglist_is_single = FAL0;
369 #ifdef HAVE_IMAP
370 if (mb.mb_type == MB_IMAP && gotheaders++ == 0)
371 imap_getheaders(1, msgCount);
372 #endif
373 if (id == NULL && (cp = hfield1("in-reply-to", dot)) != NULL) {
374 id = savestr(cp);
375 idfield = ID_IN_REPLY_TO;
377 if (id == NULL && (cp = hfield1("references", dot)) != NULL) {
378 struct name *enp;
380 if ((enp = extract(cp, GREF)) != NULL) {
381 while (enp->n_flink != NULL)
382 enp = enp->n_flink;
383 id = savestr(enp->n_name);
384 idfield = ID_REFERENCES;
387 if (id == NULL) {
388 printf(tr(227,
389 "Cannot determine parent Message-ID of the current message\n"));
390 markall_ret(-1)
392 break;
394 case TERROR:
395 list_saw_numbers = TRU1;
396 msglist_is_single = FAL0;
397 markall_ret(-1)
399 threadflag = 0;
400 tok = scan(&bufp);
403 lastcolmod = colmod;
404 np = add_to_namelist(&namelist, &nmlsize, np, NULL);
405 --np;
406 mc = 0;
407 if (star) {
408 for (i = 0; i < msgCount; ++i) {
409 if (!(message[i].m_flag & MHIDDEN) &&
410 (message[i].m_flag & MDELETED) == (unsigned)f) {
411 mark(i + 1, f);
412 ++mc;
415 if (mc == 0) {
416 if (!inhook)
417 printf(tr(119, "No applicable messages.\n"));
418 markall_ret(-1)
420 markall_ret(0)
423 if ((topen || tback) && mc == 0) {
424 for (i = 0; i < msgCount; ++i)
425 if (message[i].m_flag & MMARK)
426 ++mc;
427 if (mc == 0) {
428 if (!inhook) {
429 if (tback)
430 fprintf(stderr, tr(131, "No previously marked messages.\n"));
431 else
432 printf("No messages satisfy (criteria).\n");/*TODO tr*/
434 markall_ret(-1)
438 /* If no numbers were given, mark all messages, so that we can unmark
439 * any whose sender was not selected if any user names were given */
440 if ((np > namelist || colmod != 0 || id) && mc == 0)
441 for (i = 1; i <= msgCount; ++i) {
442 if (!(message[i - 1].m_flag & MHIDDEN) &&
443 (message[i - 1].m_flag & MDELETED) == (unsigned)f)
444 mark(i, f);
447 /* If any names were given, go through and eliminate any messages which
448 * don't match */
449 if (np > namelist || id) {
450 struct search_expr *sep = NULL;
451 bool_t allnet;
453 /* If */
454 if (np > namelist) {
455 sep = scalloc(PTR2SIZE(np - namelist), sizeof(*sep));
457 for (j = 0, nq = namelist; *nq != NULL; ++j, ++nq) {
458 char *x = *nq;
460 sep[j].ss_sexpr = x;
461 if (*x != '?' || rv < 0)
462 continue;
464 x = strchr(++x, '?');
465 sep[j].ss_where = (x == NULL || x - 1 == *nq) ? "subject"
466 : savestrbuf(*nq + 1, PTR2SIZE(x - *nq) - 1);
468 x = (x == NULL ? *nq : x) + 1;
469 if (*x == '\0') { /* TODO Empty ?? expression: remove from list */
470 fprintf(stderr, tr(525,
471 "Invalid `?[..]?' search expression: >>> %s <<<\n"), *nq);
472 rv = -1;
473 continue;
475 #ifdef HAVE_REGEX
476 if (!anyof("^.[]*+?(){}|$", x))
477 #endif
478 sep[j].ss_sexpr = x;
479 #ifdef HAVE_REGEX
480 else {
481 sep[j].ss_sexpr = NULL;
482 if (regcomp(&sep[j].ss_reexpr, x,
483 REG_EXTENDED | REG_ICASE | REG_NOSUB) != 0) {
484 fprintf(stderr, tr(526,
485 "Invalid regular expression: >>> %s <<<\n"), x);
486 rv = -1;
487 continue;
490 #endif
492 if (rv < 0)
493 goto jsepfree;
496 #ifdef HAVE_IMAP
497 if (mb.mb_type == MB_IMAP && gotheaders++ == 0)
498 imap_getheaders(1, msgCount);
499 #endif
500 srelax_hold();
501 allnet = ok_blook(allnet);
502 for (i = 1; i <= msgCount; ++i) {
503 mp = message + i - 1;
504 mc = 0;
506 if (np > namelist) {
507 for (nq = namelist; *nq != NULL; ++nq) {
508 if (**nq == '?') {
509 if (_match_qm(mp, sep + PTR2SIZE(nq - namelist))) {
510 ++mc;
511 break;
513 } else if (**nq == '/') {
514 if (_match_dash(mp, *nq)) {
515 ++mc;
516 break;
518 } else if (_matchsender(mp, *nq, allnet)) {
519 ++mc;
520 break;
524 if (mc == 0 && id && _matchmid(mp, id, idfield))
525 ++mc;
526 if (mc == 0)
527 mp->m_flag &= ~MMARK;
528 srelax();
530 srelax_rele();
532 /* Make sure we got some decent messages */
533 mc = 0;
534 for (i = 1; i <= msgCount; ++i)
535 if (message[i - 1].m_flag & MMARK) {
536 ++mc;
537 break;
539 if (mc == 0) {
540 if (!inhook && np > namelist) {
541 printf(tr(120, "No applicable messages from {%s"), namelist[0]);
542 for (nq = namelist + 1; *nq != NULL; ++nq)
543 printf(tr(121, ", %s"), *nq);
544 printf(tr(122, "}\n"));
545 } else if (id)
546 printf(tr(227, "Parent message not found\n"));
547 rv = -1;
548 goto jsepfree;
551 jsepfree:
552 if (sep != NULL) {
553 #ifdef HAVE_REGEX
554 for (i = (int)PTR2SIZE(np - namelist); i-- != 0;)
555 if (sep[i].ss_sexpr == NULL)
556 regfree(&sep[i].ss_reexpr);
557 #endif
558 free(sep);
560 if (rv < 0)
561 goto jleave;
564 /* If any colon modifiers were given, go through and unmark any
565 * messages which do not satisfy the modifiers */
566 if (colmod != 0) {
567 for (i = 1; i <= msgCount; ++i) {
568 struct coltab const *colp;
569 bool_t bad = TRU1;
571 mp = &message[i - 1];
572 for (colp = _coltab; colp->co_char != '\0'; ++colp)
573 if ((colp->co_bit & colmod) &&
574 ((mp->m_flag & colp->co_mask) == (unsigned)colp->co_equal))
575 bad = FAL0;
576 if (bad)
577 unmark(i);
579 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
580 if (mp->m_flag & MMARK)
581 break;
582 if (PTRCMP(mp, >=, message + msgCount)) {
583 struct coltab const *colp;
585 if (!inhook) {
586 printf(tr(123, "No messages satisfy"));
587 for (colp = _coltab; colp->co_char != '\0'; ++colp)
588 if (colp->co_bit & colmod)
589 printf(" :%c", colp->co_char);
590 printf("\n");
592 markall_ret(-1)
596 markall_ret(0)
597 jleave:
598 free(namelist);
599 ac_free(lexstring);
600 NYD_LEAVE;
601 return rv;
603 #undef markall_ret
606 static int
607 evalcol(int col)
609 struct coltab const *colp;
610 int rv;
611 NYD_ENTER;
613 if (col == 0)
614 rv = lastcolmod;
615 else {
616 rv = 0;
617 for (colp = _coltab; colp->co_char != '\0'; ++colp)
618 if (colp->co_char == col) {
619 rv = colp->co_bit;
620 break;
623 NYD_LEAVE;
624 return rv;
627 static int
628 check(int mesg, int f)
630 struct message *mp;
631 NYD_ENTER;
633 if (mesg < 1 || mesg > msgCount) {
634 printf(tr(124, "%d: Invalid message number\n"), mesg);
635 goto jem1;
637 mp = &message[mesg - 1];
638 if (mp->m_flag & MHIDDEN ||
639 (f != MDELETED && (mp->m_flag & MDELETED) != 0)) {
640 fprintf(stderr, tr(125, "%d: Inappropriate message\n"), mesg);
641 goto jem1;
643 f = 0;
644 jleave:
645 NYD_LEAVE;
646 return f;
647 jem1:
648 f = -1;
649 goto jleave;
652 static int
653 scan(char **sp)
655 char *cp, *cp2;
656 int rv, c, inquote, quotec;
657 struct lex const *lp;
658 NYD_ENTER;
660 if (regretp >= 0) {
661 strncpy(lexstring, string_stack[regretp], STRINGLEN);
662 lexstring[STRINGLEN-1]='\0';
663 lexnumber = numberstack[regretp];
664 rv = regretstack[regretp--];
665 goto jleave;
668 cp = *sp;
669 cp2 = lexstring;
670 c = *cp++;
672 /* strip away leading white space */
673 while (blankchar(c))
674 c = *cp++;
676 /* If no characters remain, we are at end of line, so report that */
677 if (c == '\0') {
678 *sp = --cp;
679 rv = TEOL;
680 goto jleave;
683 /* Select members of a message thread */
684 if (c == '&') {
685 threadflag = 1;
686 if (*cp == '\0' || spacechar(*cp)) {
687 lexstring[0] = '.';
688 lexstring[1] = '\0';
689 *sp = cp;
690 rv = TDOT;
691 goto jleave;
693 c = *cp++;
696 /* If the leading character is a digit, scan the number and convert it
697 * on the fly. Return TNUMBER when done */
698 if (digitchar(c)) {
699 lexnumber = 0;
700 while (digitchar(c)) {
701 lexnumber = lexnumber*10 + c - '0';
702 *cp2++ = c;
703 c = *cp++;
705 *cp2 = '\0';
706 *sp = --cp;
707 rv = TNUMBER;
708 goto jleave;
711 /* An IMAP SEARCH list. Note that TOPEN has always been included in
712 * singles[] in Mail and mailx. Thus although there is no formal
713 * definition for (LIST) lists, they do not collide with historical
714 * practice because a subject string (LIST) could never been matched
715 * this way */
716 if (c == '(') {
717 ui32_t level = 1;
718 inquote = 0;
719 *cp2++ = c;
720 do {
721 if ((c = *cp++&0377) == '\0') {
722 jmtop:
723 fprintf(stderr, "Missing \")\".\n");
724 rv = TERROR;
725 goto jleave;
727 if (inquote && c == '\\') {
728 *cp2++ = c;
729 c = *cp++&0377;
730 if (c == '\0')
731 goto jmtop;
732 } else if (c == '"')
733 inquote = !inquote;
734 else if (inquote)
735 /*EMPTY*/;
736 else if (c == '(')
737 level++;
738 else if (c == ')')
739 level--;
740 else if (spacechar(c)) {
741 /* Replace unquoted whitespace by single space characters, to make
742 * the string IMAP SEARCH conformant */
743 c = ' ';
744 if (cp2[-1] == ' ')
745 cp2--;
747 *cp2++ = c;
748 } while (c != ')' || level > 0);
749 *cp2 = '\0';
750 *sp = cp;
751 rv = TOPEN;
752 goto jleave;
755 /* Check for single character tokens; return such if found */
756 for (lp = _singles; lp->l_char != '\0'; ++lp)
757 if (c == lp->l_char) {
758 lexstring[0] = c;
759 lexstring[1] = '\0';
760 *sp = cp;
761 rv = lp->l_token;
762 goto jleave;
765 /* We've got a string! Copy all the characters of the string into
766 * lexstring, until we see a null, space, or tab. If the lead character is
767 * a " or ', save it and scan until you get another */
768 quotec = 0;
769 if (c == '\'' || c == '"') {
770 quotec = c;
771 c = *cp++;
773 while (c != '\0') {
774 if (quotec == 0 && c == '\\' && *cp)
775 c = *cp++;
776 if (c == quotec) {
777 cp++;
778 break;
780 if (quotec == 0 && blankchar(c))
781 break;
782 if (PTRCMP(cp2 - lexstring, <, STRINGLEN - 1))
783 *cp2++ = c;
784 c = *cp++;
786 if (quotec && c == 0) {
787 fprintf(stderr, tr(127, "Missing %c\n"), quotec);
788 rv = TERROR;
789 goto jleave;
791 *sp = --cp;
792 *cp2 = '\0';
793 rv = TSTRING;
794 jleave:
795 NYD_LEAVE;
796 return rv;
799 static void
800 regret(int token)
802 NYD_ENTER;
803 if (++regretp >= REGDEP)
804 panic(tr(128, "Too many regrets"));
805 regretstack[regretp] = token;
806 lexstring[STRINGLEN - 1] = '\0';
807 string_stack[regretp] = savestr(lexstring);
808 numberstack[regretp] = lexnumber;
809 NYD_LEAVE;
812 static void
813 scaninit(void)
815 NYD_ENTER;
816 regretp = -1;
817 threadflag = 0;
818 NYD_LEAVE;
821 static bool_t
822 _matchsender(struct message *mp, char const *str, bool_t allnet)
824 bool_t rv;
825 NYD_ENTER;
827 if (allnet) {
828 char *cp = nameof(mp, 0);
830 do {
831 if ((*cp == '@' || *cp == '\0') && (*str == '@' || *str == '\0')) {
832 rv = TRU1;
833 goto jleave;
835 if (*cp != *str)
836 break;
837 } while (cp++, *str++ != '\0');
838 rv = FAL0;
839 goto jleave;
841 rv = !strcmp(str, (ok_blook(showname) ? realname : skin)(name1(mp, 0)));
842 jleave:
843 NYD_LEAVE;
844 return rv;
847 static bool_t
848 _matchmid(struct message *mp, char *id, enum idfield idfield)
850 char *cp;
851 bool_t rv;
852 NYD_ENTER;
854 if ((cp = hfield1("message-id", mp)) != NULL) {
855 switch (idfield) {
856 case ID_REFERENCES:
857 rv = (msgidcmp(id, cp) == 0);
858 goto jleave;
859 case ID_IN_REPLY_TO: {
860 struct name *np;
862 if ((np = extract(id, GREF)) != NULL)
863 do {
864 if (msgidcmp(np->n_name, cp) == 0) {
865 rv = TRU1;
866 goto jleave;
868 } while ((np = np->n_flink) != NULL);
869 break;
873 rv = FAL0;
874 jleave:
875 NYD_LEAVE;
876 return rv;
879 static bool_t
880 _match_dash(struct message *mp, char const *str)
882 static char lastscan[128];
884 struct str in, out;
885 char *hfield, *hbody;
886 bool_t rv;
887 NYD_ENTER;
889 if (*++str == '\0') {
890 str = lastscan;
891 } else {
892 strncpy(lastscan, str, sizeof lastscan); /* XXX use new n_str object! */
893 lastscan[sizeof lastscan - 1] = '\0';
896 /* Now look, ignoring case, for the word in the string */
897 if (ok_blook(searchheaders) && (hfield = strchr(str, ':'))) {
898 size_t l = PTR2SIZE(hfield - str);
899 hfield = ac_alloc(l + 1);
900 memcpy(hfield, str, l);
901 hfield[l] = '\0';
902 hbody = hfieldX(hfield, mp);
903 ac_free(hfield);
904 hfield = UNCONST(str + l + 1);
905 } else {
906 hfield = UNCONST(str);
907 hbody = hfield1("subject", mp);
909 if (hbody == NULL) {
910 rv = FAL0;
911 goto jleave;
914 in.s = hbody;
915 in.l = strlen(hbody);
916 mime_fromhdr(&in, &out, TD_ICONV);
917 rv = substr(out.s, hfield);
918 free(out.s);
919 jleave:
920 NYD_LEAVE;
921 return rv;
924 static bool_t
925 _match_qm(struct message *mp, struct search_expr *sep)
927 struct str in, out;
928 char *nfield, *cfield;
929 bool_t rv = FAL0;
930 NYD_ENTER;
932 nfield = savestr(sep->ss_where);
934 while ((cfield = n_strsep(&nfield, ',', TRU1)) != NULL) {
935 if (!asccasecmp(cfield, "body")) {
936 rv = FAL0;
937 jmsg:
938 if ((rv = message_match(mp, sep, rv)))
939 break;
940 } else if (!asccasecmp(cfield, "text")) {
941 rv = TRU1;
942 goto jmsg;
943 } else if ((in.s = hfieldX(cfield, mp)) == NULL)
944 continue;
945 else {
946 in.l = strlen(in.s);
947 mime_fromhdr(&in, &out, TD_ICONV);
948 #ifdef HAVE_REGEX
949 if (sep->ss_sexpr == NULL)
950 rv = (regexec(&sep->ss_reexpr, out.s, 0,NULL, 0) != REG_NOMATCH);
951 else
952 #endif
953 rv = substr(out.s, sep->ss_sexpr);
954 free(out.s);
955 if (rv)
956 break;
959 NYD_LEAVE;
960 return rv;
963 static void
964 unmark(int mesg)
966 size_t i;
967 NYD_ENTER;
969 i = (size_t)mesg;
970 if (i < 1 || UICMP(z, i, >, msgCount))
971 panic(tr(130, "Bad message number to unmark"));
972 message[i - 1].m_flag &= ~MMARK;
973 NYD_LEAVE;
976 static int
977 metamess(int meta, int f)
979 int c, m;
980 struct message *mp;
981 NYD_ENTER;
983 c = meta;
984 switch (c) {
985 case '^': /* First 'good' message left */
986 mp = mb.mb_threaded ? threadroot : &message[0];
987 while (PTRCMP(mp, <, message + msgCount)) {
988 if (!(mp->m_flag & MHIDDEN) && (mp->m_flag & MDELETED) ==(unsigned)f){
989 c = (int)PTR2SIZE(mp - message + 1);
990 goto jleave;
992 if (mb.mb_threaded) {
993 mp = next_in_thread(mp);
994 if (mp == NULL)
995 break;
996 } else
997 ++mp;
999 if (!inhook)
1000 printf(tr(132, "No applicable messages\n"));
1001 goto jem1;
1003 case '$': /* Last 'good message left */
1004 mp = mb.mb_threaded ? this_in_thread(threadroot, -1)
1005 : &message[msgCount-1];
1006 while (mp >= &message[0]) {
1007 if (!(mp->m_flag & MHIDDEN) && (mp->m_flag & MDELETED) == (ui32_t)f) {
1008 c = (int)PTR2SIZE(mp - message + 1);
1009 goto jleave;
1011 if (mb.mb_threaded) {
1012 mp = prev_in_thread(mp);
1013 if (mp == NULL)
1014 break;
1015 } else
1016 --mp;
1018 if (!inhook)
1019 printf(tr(132, "No applicable messages\n"));
1020 goto jem1;
1022 case '.':
1023 /* Current message */
1024 m = dot - message + 1;
1025 if ((dot->m_flag & MHIDDEN) || (dot->m_flag & MDELETED) != (unsigned)f) {
1026 printf(tr(133, "%d: Inappropriate message\n"), m);
1027 goto jem1;
1029 c = m;
1030 break;
1032 case ';':
1033 /* Previously current message */
1034 if (prevdot == NULL) {
1035 fprintf(stderr, tr(228, "No previously current message\n"));
1036 goto jem1;
1038 m = prevdot - message + 1;
1039 if ((prevdot->m_flag & MHIDDEN) ||
1040 (prevdot->m_flag & MDELETED) != (unsigned)f) {
1041 fprintf(stderr, tr(133, "%d: Inappropriate message\n"), m);
1042 goto jem1;
1044 c = m;
1045 break;
1047 default:
1048 fprintf(stderr, tr(134, "Unknown metachar (%c)\n"), c);
1049 goto jem1;
1051 jleave:
1052 NYD_LEAVE;
1053 return c;
1054 jem1:
1055 c = -1;
1056 goto jleave;
1059 FL int
1060 getmsglist(char *buf, int *vector, int flags)
1062 int *ip, mc;
1063 struct message *mp;
1064 NYD_ENTER;
1066 list_saw_numbers =
1067 msglist_is_single = FAL0;
1069 if (msgCount == 0) {
1070 *vector = 0;
1071 mc = 0;
1072 goto jleave;
1075 msglist_is_single = TRU1;
1076 if (markall(buf, flags) < 0) {
1077 mc = -1;
1078 goto jleave;
1081 ip = vector;
1082 if (inhook & 2) {
1083 mc = 0;
1084 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1085 if (mp->m_flag & MMARK) {
1086 if ((mp->m_flag & MNEWEST) == 0)
1087 unmark((int)PTR2SIZE(mp - message + 1));
1088 else
1089 ++mc;
1091 if (mc == 0) {
1092 mc = -1;
1093 goto jleave;
1097 if (mb.mb_threaded == 0) {
1098 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1099 if (mp->m_flag & MMARK)
1100 *ip++ = (int)PTR2SIZE(mp - message + 1);
1101 } else {
1102 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
1103 if (mp->m_flag & MMARK)
1104 *ip++ = (int)PTR2SIZE(mp - message + 1);
1106 *ip = 0;
1107 mc = (int)PTR2SIZE(ip - vector);
1108 msglist_is_single = (mc == 1);
1109 jleave:
1110 NYD_LEAVE;
1111 return mc;
1114 FL int
1115 getrawlist(char const *line, size_t linesize, char **argv, int argc,
1116 int echolist)
1118 char c, *cp2, quotec, *linebuf;
1119 char const *cp;
1120 int argn;
1121 NYD_ENTER;
1123 list_saw_numbers = FAL0;
1125 argn = 0;
1126 cp = line;
1127 linebuf = ac_alloc(linesize + 1);
1128 for (;;) {
1129 for (; blankchar(*cp); ++cp)
1131 if (*cp == '\0')
1132 break;
1133 if (argn >= argc - 1) {
1134 printf(tr(126, "Too many elements in the list; excess discarded.\n"));
1135 break;
1137 cp2 = linebuf;
1138 quotec = '\0';
1139 while ((c = *cp) != '\0') {
1140 cp++;
1141 if (quotec != '\0') {
1142 if (c == quotec) {
1143 quotec = '\0';
1144 if (echolist)
1145 *cp2++ = c;
1146 } else if (c == '\\')
1147 switch (c = *cp++) {
1148 case '\0':
1149 *cp2++ = '\\';
1150 cp--;
1151 break;
1153 case '0': case '1': case '2': case '3':
1154 case '4': case '5': case '6': case '7':
1155 c -= '0';
1156 if (*cp >= '0' && *cp <= '7')
1157 c = c * 8 + *cp++ - '0';
1158 if (*cp >= '0' && *cp <= '7')
1159 c = c * 8 + *cp++ - '0';
1160 *cp2++ = c;
1161 break;
1162 case 'b':
1163 *cp2++ = '\b';
1164 break;
1165 case 'f':
1166 *cp2++ = '\f';
1167 break;
1168 case 'n':
1169 *cp2++ = '\n';
1170 break;
1171 case 'r':
1172 *cp2++ = '\r';
1173 break;
1174 case 't':
1175 *cp2++ = '\t';
1176 break;
1177 case 'v':
1178 *cp2++ = '\v';
1179 break;
1181 default:
1182 if (cp[-1] != quotec || echolist)
1183 *cp2++ = '\\';
1184 *cp2++ = c;
1186 /*else if (c == '^') {
1187 c = *cp++;
1188 if (c == '?')
1189 *cp2++ = '\177';
1190 /\* null doesn't show up anyway *\/
1191 else if ((c >= 'A' && c <= '_') ||
1192 (c >= 'a' && c <= 'z'))
1193 *cp2++ = c & 037;
1194 else {
1195 *cp2++ = '^';
1196 cp--;
1198 }*/ else
1199 *cp2++ = c;
1200 } else if (c == '"' || c == '\'') {
1201 if (echolist)
1202 *cp2++ = c;
1203 quotec = c;
1204 } else if (c == '\\' && !echolist) {
1205 if (*cp)
1206 *cp2++ = *cp++;
1207 else
1208 *cp2++ = c;
1209 } else if (blankchar(c))
1210 break;
1211 else
1212 *cp2++ = c;
1214 *cp2 = '\0';
1215 argv[argn++] = savestr(linebuf);
1217 argv[argn] = NULL;
1218 ac_free(linebuf);
1219 NYD_LEAVE;
1220 return argn;
1223 FL int
1224 first(int f, int m)
1226 struct message *mp;
1227 int rv;
1228 NYD_ENTER;
1230 if (msgCount == 0) {
1231 rv = 0;
1232 goto jleave;
1235 f &= MDELETED;
1236 m &= MDELETED;
1237 for (mp = dot;
1238 mb.mb_threaded ? mp != NULL : PTRCMP(mp, <, message + msgCount);
1239 mb.mb_threaded ? mp = next_in_thread(mp) : ++mp) {
1240 if (!(mp->m_flag & MHIDDEN) && (mp->m_flag & m) == (unsigned)f) {
1241 rv = (int)PTR2SIZE(mp - message + 1);
1242 goto jleave;
1246 if (dot > message) {
1247 for (mp = dot-1; (mb.mb_threaded ? mp != NULL : mp >= message);
1248 mb.mb_threaded ? mp = prev_in_thread(mp) : --mp) {
1249 if (!(mp->m_flag & MHIDDEN) && (mp->m_flag & m) == (unsigned)f) {
1250 rv = (int)PTR2SIZE(mp - message + 1);
1251 goto jleave;
1255 rv = 0;
1256 jleave:
1257 NYD_LEAVE;
1258 return rv;
1261 FL void
1262 mark(int mesg, int f)
1264 struct message *mp;
1265 int i;
1266 NYD_ENTER;
1268 i = mesg;
1269 if (i < 1 || i > msgCount)
1270 panic(tr(129, "Bad message number to mark"));
1271 if (mb.mb_threaded == 1 && threadflag) {
1272 if ((message[i - 1].m_flag & MHIDDEN) == 0) {
1273 if (f == MDELETED || (message[i - 1].m_flag&MDELETED) == 0)
1274 message[i - 1].m_flag |= MMARK;
1277 if (message[i - 1].m_child) {
1278 mp = message[i - 1].m_child;
1279 mark((int)PTR2SIZE(mp - message + 1), f);
1280 for (mp = mp->m_younger; mp; mp = mp->m_younger)
1281 mark((int)PTR2SIZE(mp - message + 1), f);
1283 } else
1284 message[i - 1].m_flag |= MMARK;
1285 NYD_LEAVE;
1288 /* vim:set fenc=utf-8:s-it-mode */