.mailmap: (sigh..)
[s-mailx.git] / list.c
bloba25de9fcf48641ea4e5d99b8e8d3cb513e155387
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, eliminate any messages which don't match */
448 if (np > namelist || id) {
449 struct search_expr *sep = NULL;
450 bool_t allnet;
452 /* The `?' search works with struct search_expr, so build an array.
453 * To simplify array, i.e., regex_t destruction, and optimize for the
454 * common case we walk the entire array even in case of errors */
455 if (np > namelist) {
456 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') { /* XXX Simply remove from list instead? */
470 fprintf(stderr, tr(525, "Empty `?[..]?' search expression\n"));
471 rv = -1;
472 continue;
474 #ifdef HAVE_REGEX
475 if (anyof("^.[]*+?(){}|$", x)) {
476 sep[j].ss_sexpr = NULL;
477 if (regcomp(&sep[j].ss_reexpr, x,
478 REG_EXTENDED | REG_ICASE | REG_NOSUB) != 0) {
479 fprintf(stderr, tr(526,
480 "Invalid regular expression: >>> %s <<<\n"), x);
481 rv = -1;
482 continue;
484 } else
485 #endif
486 sep[j].ss_sexpr = x;
488 if (rv < 0)
489 goto jnamesearch_sepfree;
492 #ifdef HAVE_IMAP
493 if (mb.mb_type == MB_IMAP && gotheaders++ == 0)
494 imap_getheaders(1, msgCount);
495 #endif
496 srelax_hold();
497 allnet = ok_blook(allnet);
498 for (i = 1; i <= msgCount; ++i) {
499 mp = message + i - 1;
500 j = 0;
501 if (np > namelist) {
502 for (nq = namelist; *nq != NULL; ++nq) {
503 if (**nq == '?') {
504 if (_match_qm(mp, sep + PTR2SIZE(nq - namelist))) {
505 ++j;
506 break;
508 } else if (**nq == '/') {
509 if (_match_dash(mp, *nq)) {
510 ++j;
511 break;
513 } else if (_matchsender(mp, *nq, allnet)) {
514 ++j;
515 break;
519 if (j == 0 && id && _matchmid(mp, id, idfield))
520 ++j;
521 if (j == 0)
522 mp->m_flag &= ~MMARK;
523 srelax();
525 srelax_rele();
527 /* Make sure we got some decent messages */
528 j = 0;
529 for (i = 1; i <= msgCount; ++i)
530 if (message[i - 1].m_flag & MMARK) {
531 ++j;
532 break;
534 if (j == 0) {
535 if (!inhook && np > namelist) {
536 printf(tr(120, "No applicable messages from {%s"), namelist[0]);
537 for (nq = namelist + 1; *nq != NULL; ++nq)
538 printf(tr(121, ", %s"), *nq);
539 printf(tr(122, "}\n"));
540 } else if (id)
541 printf(tr(227, "Parent message not found\n"));
542 rv = -1;
543 goto jnamesearch_sepfree;
546 jnamesearch_sepfree:
547 if (sep != NULL) {
548 #ifdef HAVE_REGEX
549 for (j = PTR2SIZE(np - namelist); j-- != 0;)
550 if (sep[j].ss_sexpr == NULL)
551 regfree(&sep[j].ss_reexpr);
552 #endif
553 free(sep);
555 if (rv < 0)
556 goto jleave;
559 /* If any colon modifiers were given, go through and unmark any
560 * messages which do not satisfy the modifiers */
561 if (colmod != 0) {
562 for (i = 1; i <= msgCount; ++i) {
563 struct coltab const *colp;
564 bool_t bad = TRU1;
566 mp = &message[i - 1];
567 for (colp = _coltab; colp->co_char != '\0'; ++colp)
568 if ((colp->co_bit & colmod) &&
569 ((mp->m_flag & colp->co_mask) == (unsigned)colp->co_equal))
570 bad = FAL0;
571 if (bad)
572 unmark(i);
574 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
575 if (mp->m_flag & MMARK)
576 break;
577 if (PTRCMP(mp, >=, message + msgCount)) {
578 struct coltab const *colp;
580 if (!inhook) {
581 printf(tr(123, "No messages satisfy"));
582 for (colp = _coltab; colp->co_char != '\0'; ++colp)
583 if (colp->co_bit & colmod)
584 printf(" :%c", colp->co_char);
585 printf("\n");
587 markall_ret(-1)
591 markall_ret(0)
592 jleave:
593 free(namelist);
594 ac_free(lexstring);
595 NYD_LEAVE;
596 return rv;
598 #undef markall_ret
601 static int
602 evalcol(int col)
604 struct coltab const *colp;
605 int rv;
606 NYD_ENTER;
608 if (col == 0)
609 rv = lastcolmod;
610 else {
611 rv = 0;
612 for (colp = _coltab; colp->co_char != '\0'; ++colp)
613 if (colp->co_char == col) {
614 rv = colp->co_bit;
615 break;
618 NYD_LEAVE;
619 return rv;
622 static int
623 check(int mesg, int f)
625 struct message *mp;
626 NYD_ENTER;
628 if (mesg < 1 || mesg > msgCount) {
629 printf(tr(124, "%d: Invalid message number\n"), mesg);
630 goto jem1;
632 mp = &message[mesg - 1];
633 if (mp->m_flag & MHIDDEN ||
634 (f != MDELETED && (mp->m_flag & MDELETED) != 0)) {
635 fprintf(stderr, tr(125, "%d: Inappropriate message\n"), mesg);
636 goto jem1;
638 f = 0;
639 jleave:
640 NYD_LEAVE;
641 return f;
642 jem1:
643 f = -1;
644 goto jleave;
647 static int
648 scan(char **sp)
650 char *cp, *cp2;
651 int rv, c, inquote, quotec;
652 struct lex const *lp;
653 NYD_ENTER;
655 if (regretp >= 0) {
656 strncpy(lexstring, string_stack[regretp], STRINGLEN);
657 lexstring[STRINGLEN-1]='\0';
658 lexnumber = numberstack[regretp];
659 rv = regretstack[regretp--];
660 goto jleave;
663 cp = *sp;
664 cp2 = lexstring;
665 c = *cp++;
667 /* strip away leading white space */
668 while (blankchar(c))
669 c = *cp++;
671 /* If no characters remain, we are at end of line, so report that */
672 if (c == '\0') {
673 *sp = --cp;
674 rv = TEOL;
675 goto jleave;
678 /* Select members of a message thread */
679 if (c == '&') {
680 threadflag = 1;
681 if (*cp == '\0' || spacechar(*cp)) {
682 lexstring[0] = '.';
683 lexstring[1] = '\0';
684 *sp = cp;
685 rv = TDOT;
686 goto jleave;
688 c = *cp++;
691 /* If the leading character is a digit, scan the number and convert it
692 * on the fly. Return TNUMBER when done */
693 if (digitchar(c)) {
694 lexnumber = 0;
695 while (digitchar(c)) {
696 lexnumber = lexnumber*10 + c - '0';
697 *cp2++ = c;
698 c = *cp++;
700 *cp2 = '\0';
701 *sp = --cp;
702 rv = TNUMBER;
703 goto jleave;
706 /* An IMAP SEARCH list. Note that TOPEN has always been included in
707 * singles[] in Mail and mailx. Thus although there is no formal
708 * definition for (LIST) lists, they do not collide with historical
709 * practice because a subject string (LIST) could never been matched
710 * this way */
711 if (c == '(') {
712 ui32_t level = 1;
713 inquote = 0;
714 *cp2++ = c;
715 do {
716 if ((c = *cp++&0377) == '\0') {
717 jmtop:
718 fprintf(stderr, "Missing \")\".\n");
719 rv = TERROR;
720 goto jleave;
722 if (inquote && c == '\\') {
723 *cp2++ = c;
724 c = *cp++&0377;
725 if (c == '\0')
726 goto jmtop;
727 } else if (c == '"')
728 inquote = !inquote;
729 else if (inquote)
730 /*EMPTY*/;
731 else if (c == '(')
732 level++;
733 else if (c == ')')
734 level--;
735 else if (spacechar(c)) {
736 /* Replace unquoted whitespace by single space characters, to make
737 * the string IMAP SEARCH conformant */
738 c = ' ';
739 if (cp2[-1] == ' ')
740 cp2--;
742 *cp2++ = c;
743 } while (c != ')' || level > 0);
744 *cp2 = '\0';
745 *sp = cp;
746 rv = TOPEN;
747 goto jleave;
750 /* Check for single character tokens; return such if found */
751 for (lp = _singles; lp->l_char != '\0'; ++lp)
752 if (c == lp->l_char) {
753 lexstring[0] = c;
754 lexstring[1] = '\0';
755 *sp = cp;
756 rv = lp->l_token;
757 goto jleave;
760 /* We've got a string! Copy all the characters of the string into
761 * lexstring, until we see a null, space, or tab. If the lead character is
762 * a " or ', save it and scan until you get another */
763 quotec = 0;
764 if (c == '\'' || c == '"') {
765 quotec = c;
766 c = *cp++;
768 while (c != '\0') {
769 if (quotec == 0 && c == '\\' && *cp)
770 c = *cp++;
771 if (c == quotec) {
772 cp++;
773 break;
775 if (quotec == 0 && blankchar(c))
776 break;
777 if (PTRCMP(cp2 - lexstring, <, STRINGLEN - 1))
778 *cp2++ = c;
779 c = *cp++;
781 if (quotec && c == 0) {
782 fprintf(stderr, tr(127, "Missing %c\n"), quotec);
783 rv = TERROR;
784 goto jleave;
786 *sp = --cp;
787 *cp2 = '\0';
788 rv = TSTRING;
789 jleave:
790 NYD_LEAVE;
791 return rv;
794 static void
795 regret(int token)
797 NYD_ENTER;
798 if (++regretp >= REGDEP)
799 panic(tr(128, "Too many regrets"));
800 regretstack[regretp] = token;
801 lexstring[STRINGLEN - 1] = '\0';
802 string_stack[regretp] = savestr(lexstring);
803 numberstack[regretp] = lexnumber;
804 NYD_LEAVE;
807 static void
808 scaninit(void)
810 NYD_ENTER;
811 regretp = -1;
812 threadflag = 0;
813 NYD_LEAVE;
816 static bool_t
817 _matchsender(struct message *mp, char const *str, bool_t allnet)
819 bool_t rv;
820 NYD_ENTER;
822 if (allnet) {
823 char *cp = nameof(mp, 0);
825 do {
826 if ((*cp == '@' || *cp == '\0') && (*str == '@' || *str == '\0')) {
827 rv = TRU1;
828 goto jleave;
830 if (*cp != *str)
831 break;
832 } while (cp++, *str++ != '\0');
833 rv = FAL0;
834 goto jleave;
836 rv = !strcmp(str, (ok_blook(showname) ? realname : skin)(name1(mp, 0)));
837 jleave:
838 NYD_LEAVE;
839 return rv;
842 static bool_t
843 _matchmid(struct message *mp, char *id, enum idfield idfield)
845 char *cp;
846 bool_t rv;
847 NYD_ENTER;
849 if ((cp = hfield1("message-id", mp)) != NULL) {
850 switch (idfield) {
851 case ID_REFERENCES:
852 rv = !msgidcmp(id, cp);
853 goto jleave;
854 case ID_IN_REPLY_TO: {
855 struct name *np;
857 if ((np = extract(id, GREF)) != NULL)
858 do {
859 if (!msgidcmp(np->n_name, cp)) {
860 rv = TRU1;
861 goto jleave;
863 } while ((np = np->n_flink) != NULL);
864 break;
868 rv = FAL0;
869 jleave:
870 NYD_LEAVE;
871 return rv;
874 static bool_t
875 _match_dash(struct message *mp, char const *str)
877 static char lastscan[128];
879 struct str in, out;
880 char *hfield, *hbody;
881 bool_t rv;
882 NYD_ENTER;
884 if (*++str == '\0') {
885 str = lastscan;
886 } else {
887 strncpy(lastscan, str, sizeof lastscan); /* XXX use new n_str object! */
888 lastscan[sizeof lastscan - 1] = '\0';
891 /* Now look, ignoring case, for the word in the string */
892 if (ok_blook(searchheaders) && (hfield = strchr(str, ':'))) {
893 size_t l = PTR2SIZE(hfield - str);
894 hfield = ac_alloc(l + 1);
895 memcpy(hfield, str, l);
896 hfield[l] = '\0';
897 hbody = hfieldX(hfield, mp);
898 ac_free(hfield);
899 hfield = UNCONST(str + l + 1);
900 } else {
901 hfield = UNCONST(str);
902 hbody = hfield1("subject", mp);
904 if (hbody == NULL) {
905 rv = FAL0;
906 goto jleave;
909 in.s = hbody;
910 in.l = strlen(hbody);
911 mime_fromhdr(&in, &out, TD_ICONV);
912 rv = substr(out.s, hfield);
913 free(out.s);
914 jleave:
915 NYD_LEAVE;
916 return rv;
919 static bool_t
920 _match_qm(struct message *mp, struct search_expr *sep)
922 struct str in, out;
923 char *nfield, *cfield;
924 bool_t rv = FAL0;
925 NYD_ENTER;
927 nfield = savestr(sep->ss_where);
929 while ((cfield = n_strsep(&nfield, ',', TRU1)) != NULL) {
930 if (!asccasecmp(cfield, "body")) {
931 rv = FAL0;
932 jmsg:
933 if ((rv = message_match(mp, sep, rv)))
934 break;
935 } else if (!asccasecmp(cfield, "text")) {
936 rv = TRU1;
937 goto jmsg;
938 } else if ((in.s = hfieldX(cfield, mp)) == NULL)
939 continue;
940 else {
941 in.l = strlen(in.s);
942 mime_fromhdr(&in, &out, TD_ICONV);
943 #ifdef HAVE_REGEX
944 if (sep->ss_sexpr == NULL)
945 rv = (regexec(&sep->ss_reexpr, out.s, 0,NULL, 0) != REG_NOMATCH);
946 else
947 #endif
948 rv = substr(out.s, sep->ss_sexpr);
949 free(out.s);
950 if (rv)
951 break;
954 NYD_LEAVE;
955 return rv;
958 static void
959 unmark(int mesg)
961 size_t i;
962 NYD_ENTER;
964 i = (size_t)mesg;
965 if (i < 1 || UICMP(z, i, >, msgCount))
966 panic(tr(130, "Bad message number to unmark"));
967 message[i - 1].m_flag &= ~MMARK;
968 NYD_LEAVE;
971 static int
972 metamess(int meta, int f)
974 int c, m;
975 struct message *mp;
976 NYD_ENTER;
978 c = meta;
979 switch (c) {
980 case '^': /* First 'good' message left */
981 mp = mb.mb_threaded ? threadroot : &message[0];
982 while (PTRCMP(mp, <, message + msgCount)) {
983 if (!(mp->m_flag & MHIDDEN) && (mp->m_flag & MDELETED) ==(unsigned)f){
984 c = (int)PTR2SIZE(mp - message + 1);
985 goto jleave;
987 if (mb.mb_threaded) {
988 mp = next_in_thread(mp);
989 if (mp == NULL)
990 break;
991 } else
992 ++mp;
994 if (!inhook)
995 printf(tr(132, "No applicable messages\n"));
996 goto jem1;
998 case '$': /* Last 'good message left */
999 mp = mb.mb_threaded ? this_in_thread(threadroot, -1)
1000 : &message[msgCount-1];
1001 while (mp >= &message[0]) {
1002 if (!(mp->m_flag & MHIDDEN) && (mp->m_flag & MDELETED) == (ui32_t)f) {
1003 c = (int)PTR2SIZE(mp - message + 1);
1004 goto jleave;
1006 if (mb.mb_threaded) {
1007 mp = prev_in_thread(mp);
1008 if (mp == NULL)
1009 break;
1010 } else
1011 --mp;
1013 if (!inhook)
1014 printf(tr(132, "No applicable messages\n"));
1015 goto jem1;
1017 case '.':
1018 /* Current message */
1019 m = dot - message + 1;
1020 if ((dot->m_flag & MHIDDEN) || (dot->m_flag & MDELETED) != (unsigned)f) {
1021 printf(tr(133, "%d: Inappropriate message\n"), m);
1022 goto jem1;
1024 c = m;
1025 break;
1027 case ';':
1028 /* Previously current message */
1029 if (prevdot == NULL) {
1030 fprintf(stderr, tr(228, "No previously current message\n"));
1031 goto jem1;
1033 m = prevdot - message + 1;
1034 if ((prevdot->m_flag & MHIDDEN) ||
1035 (prevdot->m_flag & MDELETED) != (unsigned)f) {
1036 fprintf(stderr, tr(133, "%d: Inappropriate message\n"), m);
1037 goto jem1;
1039 c = m;
1040 break;
1042 default:
1043 fprintf(stderr, tr(134, "Unknown metachar (%c)\n"), c);
1044 goto jem1;
1046 jleave:
1047 NYD_LEAVE;
1048 return c;
1049 jem1:
1050 c = -1;
1051 goto jleave;
1054 FL int
1055 getmsglist(char *buf, int *vector, int flags)
1057 int *ip, mc;
1058 struct message *mp;
1059 NYD_ENTER;
1061 list_saw_numbers =
1062 msglist_is_single = FAL0;
1064 if (msgCount == 0) {
1065 *vector = 0;
1066 mc = 0;
1067 goto jleave;
1070 msglist_is_single = TRU1;
1071 if (markall(buf, flags) < 0) {
1072 mc = -1;
1073 goto jleave;
1076 ip = vector;
1077 if (inhook & 2) {
1078 mc = 0;
1079 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1080 if (mp->m_flag & MMARK) {
1081 if ((mp->m_flag & MNEWEST) == 0)
1082 unmark((int)PTR2SIZE(mp - message + 1));
1083 else
1084 ++mc;
1086 if (mc == 0) {
1087 mc = -1;
1088 goto jleave;
1092 if (mb.mb_threaded == 0) {
1093 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1094 if (mp->m_flag & MMARK)
1095 *ip++ = (int)PTR2SIZE(mp - message + 1);
1096 } else {
1097 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
1098 if (mp->m_flag & MMARK)
1099 *ip++ = (int)PTR2SIZE(mp - message + 1);
1101 *ip = 0;
1102 mc = (int)PTR2SIZE(ip - vector);
1103 msglist_is_single = (mc == 1);
1104 jleave:
1105 NYD_LEAVE;
1106 return mc;
1109 FL int
1110 getrawlist(char const *line, size_t linesize, char **argv, int argc,
1111 int echolist)
1113 char c, *cp2, quotec, *linebuf;
1114 char const *cp;
1115 int argn;
1116 NYD_ENTER;
1118 list_saw_numbers = FAL0;
1120 argn = 0;
1121 cp = line;
1122 linebuf = ac_alloc(linesize + 1);
1123 for (;;) {
1124 for (; blankchar(*cp); ++cp)
1126 if (*cp == '\0')
1127 break;
1128 if (argn >= argc - 1) {
1129 printf(tr(126, "Too many elements in the list; excess discarded.\n"));
1130 break;
1132 cp2 = linebuf;
1133 quotec = '\0';
1134 while ((c = *cp) != '\0') {
1135 cp++;
1136 if (quotec != '\0') {
1137 if (c == quotec) {
1138 quotec = '\0';
1139 if (echolist)
1140 *cp2++ = c;
1141 } else if (c == '\\')
1142 switch (c = *cp++) {
1143 case '\0':
1144 *cp2++ = '\\';
1145 cp--;
1146 break;
1148 case '0': case '1': case '2': case '3':
1149 case '4': case '5': case '6': case '7':
1150 c -= '0';
1151 if (*cp >= '0' && *cp <= '7')
1152 c = c * 8 + *cp++ - '0';
1153 if (*cp >= '0' && *cp <= '7')
1154 c = c * 8 + *cp++ - '0';
1155 *cp2++ = c;
1156 break;
1157 case 'b':
1158 *cp2++ = '\b';
1159 break;
1160 case 'f':
1161 *cp2++ = '\f';
1162 break;
1163 case 'n':
1164 *cp2++ = '\n';
1165 break;
1166 case 'r':
1167 *cp2++ = '\r';
1168 break;
1169 case 't':
1170 *cp2++ = '\t';
1171 break;
1172 case 'v':
1173 *cp2++ = '\v';
1174 break;
1176 default:
1177 if (cp[-1] != quotec || echolist)
1178 *cp2++ = '\\';
1179 *cp2++ = c;
1181 /*else if (c == '^') {
1182 c = *cp++;
1183 if (c == '?')
1184 *cp2++ = '\177';
1185 /\* null doesn't show up anyway *\/
1186 else if ((c >= 'A' && c <= '_') ||
1187 (c >= 'a' && c <= 'z'))
1188 *cp2++ = c & 037;
1189 else {
1190 *cp2++ = '^';
1191 cp--;
1193 }*/ else
1194 *cp2++ = c;
1195 } else if (c == '"' || c == '\'') {
1196 if (echolist)
1197 *cp2++ = c;
1198 quotec = c;
1199 } else if (c == '\\' && !echolist) {
1200 if (*cp)
1201 *cp2++ = *cp++;
1202 else
1203 *cp2++ = c;
1204 } else if (blankchar(c))
1205 break;
1206 else
1207 *cp2++ = c;
1209 *cp2 = '\0';
1210 argv[argn++] = savestr(linebuf);
1212 argv[argn] = NULL;
1213 ac_free(linebuf);
1214 NYD_LEAVE;
1215 return argn;
1218 FL int
1219 first(int f, int m)
1221 struct message *mp;
1222 int rv;
1223 NYD_ENTER;
1225 if (msgCount == 0) {
1226 rv = 0;
1227 goto jleave;
1230 f &= MDELETED;
1231 m &= MDELETED;
1232 for (mp = dot;
1233 mb.mb_threaded ? mp != NULL : PTRCMP(mp, <, message + msgCount);
1234 mb.mb_threaded ? mp = next_in_thread(mp) : ++mp) {
1235 if (!(mp->m_flag & MHIDDEN) && (mp->m_flag & m) == (unsigned)f) {
1236 rv = (int)PTR2SIZE(mp - message + 1);
1237 goto jleave;
1241 if (dot > message) {
1242 for (mp = dot-1; (mb.mb_threaded ? mp != NULL : mp >= message);
1243 mb.mb_threaded ? mp = prev_in_thread(mp) : --mp) {
1244 if (!(mp->m_flag & MHIDDEN) && (mp->m_flag & m) == (unsigned)f) {
1245 rv = (int)PTR2SIZE(mp - message + 1);
1246 goto jleave;
1250 rv = 0;
1251 jleave:
1252 NYD_LEAVE;
1253 return rv;
1256 FL void
1257 mark(int mesg, int f)
1259 struct message *mp;
1260 int i;
1261 NYD_ENTER;
1263 i = mesg;
1264 if (i < 1 || i > msgCount)
1265 panic(tr(129, "Bad message number to mark"));
1266 if (mb.mb_threaded == 1 && threadflag) {
1267 if ((message[i - 1].m_flag & MHIDDEN) == 0) {
1268 if (f == MDELETED || (message[i - 1].m_flag&MDELETED) == 0)
1269 message[i - 1].m_flag |= MMARK;
1272 if (message[i - 1].m_child) {
1273 mp = message[i - 1].m_child;
1274 mark((int)PTR2SIZE(mp - message + 1), f);
1275 for (mp = mp->m_younger; mp; mp = mp->m_younger)
1276 mark((int)PTR2SIZE(mp - message + 1), f);
1278 } else
1279 message[i - 1].m_flag |= MMARK;
1280 NYD_LEAVE;
1283 /* vim:set fenc=utf-8:s-it-mode */