getmsglist(): if user gives A-B, try fulfill even in threaded mode
[s-mailx.git] / message.c
blob870b2f5ff2db29f2d36772e1deffc7f253c72dcd
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Message, message array, getmsglist(), and related operations.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2016 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
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. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
35 #undef n_FILE
36 #define n_FILE message
38 #ifndef HAVE_AMALGAMATION
39 # include "nail.h"
40 #endif
42 /* Token values returned by the scanner used for argument lists.
43 * Also, sizes of scanner-related things */
44 enum a_message_token{
45 a_MESSAGE_T_EOL, /* End of the command line */
46 a_MESSAGE_T_NUMBER, /* Message number */
47 a_MESSAGE_T_MINUS, /* - */
48 a_MESSAGE_T_STRING, /* A string (possibly containing -) */
49 a_MESSAGE_T_DOT, /* . */
50 a_MESSAGE_T_UP, /* ^ */
51 a_MESSAGE_T_DOLLAR, /* $ */
52 a_MESSAGE_T_ASTER, /* * */
53 a_MESSAGE_T_OPEN, /* ( */
54 a_MESSAGE_T_CLOSE, /* ) */
55 a_MESSAGE_T_PLUS, /* + */
56 a_MESSAGE_T_COMMA, /* , */
57 a_MESSAGE_T_SEMI, /* ; */
58 a_MESSAGE_T_BACK, /* ` */
59 a_MESSAGE_T_ERROR /* Lexical error */
62 enum a_message_idfield{
63 a_MESSAGE_ID_REFERENCES,
64 a_MESSAGE_ID_IN_REPLY_TO
67 enum a_message_state{
68 a_MESSAGE_S_NEW = 1<<0,
69 a_MESSAGE_S_OLD = 1<<1,
70 a_MESSAGE_S_UNREAD = 1<<2,
71 a_MESSAGE_S_DELETED = 1<<3,
72 a_MESSAGE_S_READ = 1<<4,
73 a_MESSAGE_S_FLAG = 1<<5,
74 a_MESSAGE_S_ANSWERED = 1<<6,
75 a_MESSAGE_S_DRAFT = 1<<7,
76 a_MESSAGE_S_SPAM = 1<<8,
77 a_MESSAGE_S_SPAMUNSURE = 1<<9
80 struct a_message_coltab{
81 char mco_char; /* What to find past : */
82 ui8_t mco__dummy[3];
83 int mco_bit; /* Associated modifier bit */
84 int mco_mask; /* m_status bits to mask */
85 int mco_equal; /* ... must equal this */
88 struct a_message_lex{
89 char ml_char;
90 ui8_t ml_token;
93 static struct a_message_coltab const a_message_coltabs[] = {
94 {'n', {0,}, a_MESSAGE_S_NEW, MNEW, MNEW},
95 {'o', {0,}, a_MESSAGE_S_OLD, MNEW, 0},
96 {'u', {0,}, a_MESSAGE_S_UNREAD, MREAD, 0},
97 {'d', {0,}, a_MESSAGE_S_DELETED, MDELETED, MDELETED},
98 {'r', {0,}, a_MESSAGE_S_READ, MREAD, MREAD},
99 {'f', {0,}, a_MESSAGE_S_FLAG, MFLAGGED, MFLAGGED},
100 {'a', {0,}, a_MESSAGE_S_ANSWERED, MANSWERED, MANSWERED},
101 {'t', {0,}, a_MESSAGE_S_DRAFT, MDRAFTED, MDRAFTED},
102 {'s', {0,}, a_MESSAGE_S_SPAM, MSPAM, MSPAM},
103 {'S', {0,}, a_MESSAGE_S_SPAMUNSURE, MSPAMUNSURE, MSPAMUNSURE}
106 static struct a_message_lex const a_message_singles[] = {
107 {'$', a_MESSAGE_T_DOLLAR},
108 {'.', a_MESSAGE_T_DOT},
109 {'^', a_MESSAGE_T_UP},
110 {'*', a_MESSAGE_T_ASTER},
111 {'-', a_MESSAGE_T_MINUS},
112 {'+', a_MESSAGE_T_PLUS},
113 {'(', a_MESSAGE_T_OPEN},
114 {')', a_MESSAGE_T_CLOSE},
115 {',', a_MESSAGE_T_COMMA},
116 {';', a_MESSAGE_T_SEMI},
117 {'`', a_MESSAGE_T_BACK}
120 /* Slots in ::message */
121 static size_t a_message_mem_space;
123 /* Mark entire threads */
124 static bool_t a_message_threadflag;
126 /* :d on its way HACK TODO */
127 static bool_t a_message_list_saw_d, a_message_list_last_saw_d;
129 /* String from a_MESSAGE_T_STRING, scan() */
130 static struct str a_message_lexstr;
131 /* Number of a_MESSAGE_T_NUMBER from scan() */
132 static int a_message_lexno;
134 /* Lazy load message header fields */
135 static enum okay a_message_get_header(struct message *mp);
137 /* Append, taking care of resizes TODO vector */
138 static char **a_message_add_to_namelist(char ***namelist, size_t *nmlsize,
139 char **np, char *string);
141 /* Mark all messages that the user wanted from the command line in the message
142 * structure. Return 0 on success, -1 on error */
143 static int a_message_markall(char *buf, int f);
145 /* Turn the character after a colon modifier into a bit value */
146 static int a_message_evalcol(int col);
148 /* Check the passed message number for legality and proper flags. Unless f is
149 * MDELETED the message has to be undeleted */
150 static bool_t a_message_check(int mno, int f);
152 /* Scan out a single lexical item and return its token number, updating the
153 * string pointer passed *sp. Also, store the value of the number or string
154 * scanned in a_message_lexno or a_message_lexstr as appropriate.
155 * In any event, store the scanned "thing" in a_message_lexstr.
156 * Returns the token as a negative number when we also saw & to mark a thread */
157 static int a_message_scan(char **sp);
159 /* See if the passed name sent the passed message */
160 static bool_t a_message_match_sender(struct message *mp, char const *str,
161 bool_t allnet);
163 /* Check whether the given message-id or references match */
164 static bool_t a_message_match_mid(struct message *mp, char const *id,
165 enum a_message_idfield idfield);
167 /* See if the given string matches.
168 * For the purpose of the scan, we ignore case differences.
169 * This is the engine behind the "/" search */
170 static bool_t a_message_match_dash(struct message *mp, char const *str);
172 /* See if the given search expression matches.
173 * For the purpose of the scan, we ignore case differences.
174 * This is the engine behind the "@[..@].." search */
175 static bool_t a_message_match_at(struct message *mp, struct search_expr *sep);
177 /* Unmark the named message */
178 static void a_message_unmark(int mesg);
180 /* Return the message number corresponding to the passed meta character */
181 static int a_message_metamess(int meta, int f);
183 /* Helper for mark(): self valid, threading enabled */
184 static void a_message__threadmark(struct message *self, int f);
186 static enum okay
187 a_message_get_header(struct message *mp){
188 enum okay rv;
189 NYD2_ENTER;
190 UNUSED(mp);
192 switch(mb.mb_type){
193 case MB_FILE:
194 case MB_MAILDIR:
195 rv = OKAY;
196 break;
197 #ifdef HAVE_POP3
198 case MB_POP3:
199 rv = pop3_header(mp);
200 break;
201 #endif
202 case MB_VOID:
203 default:
204 rv = STOP;
205 break;
207 NYD2_LEAVE;
208 return rv;
211 static char **
212 a_message_add_to_namelist(char ***namelist, size_t *nmlsize, char **np,
213 char *string){
214 size_t idx;
215 NYD2_ENTER;
217 if((idx = PTR2SIZE(np - *namelist)) >= *nmlsize){
218 *namelist = srealloc(*namelist, (*nmlsize += 8) * sizeof *np);
219 np = &(*namelist)[idx];
221 *np++ = string;
222 NYD2_LEAVE;
223 return np;
226 static int
227 a_message_markall(char *buf, int f){
228 struct message *mp, *mx;
229 enum a_message_idfield idfield;
230 size_t j, nmlsize;
231 char const *id;
232 char **np, **nq, **namelist, *bufp, *cp;
233 int i, valdot, beg, colmod, tok, colresult;
234 enum{
235 a_NONE = 0,
236 a_ALLNET = 1u<<0, /* Must be TRU1 */
237 a_ALLOC = 1u<<1, /* Have allocated something */
238 a_THREADED = 1u<<2,
239 a_ERROR = 1u<<3,
240 a_ANY = 1u<<4, /* Have marked just ANY */
241 a_RANGE = 1u<<5, /* Seen dash, await close */
242 a_ASTER = 1u<<8,
243 a_TOPEN = 1u<<9, /* ( used (and didn't match) */
244 a_TBACK = 1u<<10, /* ` used (and didn't match) */
245 a_TMP = 1u<<15
246 } flags;
247 NYD_ENTER;
248 n_LCTA((ui32_t)a_ALLNET == (ui32_t)TRU1,
249 "Constant is converted to bool_t via AND, thus");
251 /* Update message array: clear MMARK but remember its former state for `.
252 * An empty selector input is identical to * asterisk */
253 for(i = msgCount; i-- > 0;){
254 enum mflag mf;
256 mf = (mp = &message[i])->m_flag;
257 if(mf & MMARK)
258 mf |= MOLDMARK;
259 else
260 mf &= ~MOLDMARK;
261 mf &= ~MMARK;
262 mp->m_flag = mf;
265 /* Strip all leading WS from user buffer */
266 while(blankspacechar(*buf))
267 ++buf;
268 /* If there is no input buffer, we are done! */
269 if(buf[0] == '\0'){
270 flags = a_NONE;
271 goto jleave;
274 UNINIT(beg, 0);
275 UNINIT(idfield, a_MESSAGE_ID_REFERENCES);
276 a_message_threadflag = FAL0;
277 a_message_lexstr.s = ac_alloc(a_message_lexstr.l = 2 * strlen(buf) +1);
278 np = namelist = smalloc((nmlsize = 8) * sizeof *namelist); /* TODO vector */
279 bufp = buf;
280 valdot = (int)PTR2SIZE(dot - message + 1);
281 colmod = 0;
282 id = NULL;
283 flags = a_ALLOC | (mb.mb_threaded ? a_THREADED : 0);
285 while((tok = a_message_scan(&bufp)) != a_MESSAGE_T_EOL){
286 if((a_message_threadflag = (tok < 0)))
287 tok &= INT_MAX;
289 switch(tok){
290 case a_MESSAGE_T_NUMBER:
291 pstate |= PS_MSGLIST_GABBY;
292 jnumber:
293 if(!a_message_check(a_message_lexno, f))
294 goto jerr;
296 if(flags & a_RANGE){
297 flags ^= a_RANGE;
299 if(!(flags & a_THREADED)){
300 if(beg < a_message_lexno)
301 i = beg;
302 else{
303 i = a_message_lexno;
304 a_message_lexno = beg;
307 for(; i <= a_message_lexno; ++i){
308 mp = &message[i - 1];
309 if(!(mp->m_flag & MHIDDEN) &&
310 (f == MDELETED || !(mp->m_flag & MDELETED))){
311 mark(i, f);
312 flags |= a_ANY;
315 }else{
316 /* TODO threaded ranges are a mess */
317 enum{
318 a_T_NONE,
319 a_T_HOT = 1u<<0,
320 a_T_DIR_PREV = 1u<<1
321 } tf;
322 int i_base;
324 if(beg < a_message_lexno)
325 i = beg;
326 else{
327 i = a_message_lexno;
328 a_message_lexno = beg;
331 i_base = i;
332 tf = a_T_NONE;
333 jnumber__thr:
334 for(;;){
335 mp = &message[i - 1];
336 if(!(mp->m_flag & MHIDDEN) &&
337 (f == MDELETED || !(mp->m_flag & MDELETED))){
338 if(tf & a_T_HOT){
339 mark(i, f);
340 flags |= a_ANY;
344 /* We may have reached the endpoint. If we were still
345 * detecting the direction to search for it, restart.
346 * Otherwise finished */
347 if(i == a_message_lexno){ /* XXX */
348 if(!(tf & a_T_HOT)){
349 tf |= a_T_HOT;
350 i = i_base;
351 goto jnumber__thr;
353 break;
356 mx = (tf & a_T_DIR_PREV) ? prev_in_thread(mp)
357 : next_in_thread(mp);
358 if(mx == NULL){
359 /* We anyway have failed to reach the endpoint in this
360 * direction; if we already switched that, report error */
361 if(!(tf & a_T_DIR_PREV)){
362 tf |= a_T_DIR_PREV;
363 i = i_base;
364 goto jnumber__thr;
366 id = N_("Range crosses multiple threads\n");
367 goto jerrmsg;
369 i = (int)PTR2SIZE(mx - message + 1);
373 beg = 0;
374 }else{
375 /* Could be an inclusive range? */
376 if(bufp[0] == '-'){
377 ++bufp;
378 beg = a_message_lexno;
379 flags |= a_RANGE;
380 }else{
381 mark(a_message_lexno, f);
382 flags |= a_ANY;
385 break;
386 case a_MESSAGE_T_PLUS:
387 pstate &= ~PS_MSGLIST_DIRECT;
388 pstate |= PS_MSGLIST_GABBY;
389 i = valdot;
391 if(flags & a_THREADED){
392 mx = next_in_thread(&message[i - 1]);
393 i = mx ? (int)PTR2SIZE(mx - message + 1) : msgCount + 1;
394 }else
395 ++i;
396 if(i > msgCount){
397 id = N_("Referencing beyond last message\n");
398 goto jerrmsg;
400 }while(message[i - 1].m_flag == MHIDDEN ||
401 (message[i - 1].m_flag & MDELETED) != (unsigned)f);
402 a_message_lexno = i;
403 goto jnumber;
404 case a_MESSAGE_T_MINUS:
405 pstate &= ~PS_MSGLIST_DIRECT;
406 pstate |= PS_MSGLIST_GABBY;
407 i = valdot;
409 if(flags & a_THREADED){
410 mx = prev_in_thread(&message[i - 1]);
411 i = mx ? (int)PTR2SIZE(mx - message + 1) : 0;
412 }else
413 --i;
414 if(i <= 0){
415 id = N_("Referencing before first message\n");
416 goto jerrmsg;
418 }while(message[i - 1].m_flag == MHIDDEN ||
419 (message[i - 1].m_flag & MDELETED) != (unsigned)f);
420 a_message_lexno = i;
421 goto jnumber;
422 case a_MESSAGE_T_STRING:
423 pstate &= ~PS_MSGLIST_DIRECT;
424 if(flags & a_RANGE)
425 goto jebadrange;
427 /* This may be a colon modifier */
428 if((cp = a_message_lexstr.s)[0] != ':')
429 np = a_message_add_to_namelist(&namelist, &nmlsize, np,
430 savestr(a_message_lexstr.s));
431 else{
432 while(*++cp != '\0'){
433 colresult = a_message_evalcol(*cp);
434 if(colresult == 0){
435 n_err(_("Unknown colon modifier: %s\n"), a_message_lexstr.s);
436 goto jerr;
438 if(colresult == a_MESSAGE_S_DELETED){
439 a_message_list_saw_d = TRU1;
440 f |= MDELETED;
442 colmod |= colresult;
445 break;
446 case a_MESSAGE_T_OPEN:
447 pstate &= ~PS_MSGLIST_DIRECT;
448 if(flags & a_RANGE)
449 goto jebadrange;
450 flags |= a_TOPEN;
452 #ifdef HAVE_IMAP_SEARCH
453 /* C99 */{
454 ssize_t ires;
456 if((ires = imap_search(a_message_lexstr.s, f)) >= 0){
457 if(ires > 0)
458 flags |= a_ANY;
459 break;
462 #else
463 n_err(_("Optional selector is not available: %s\n"),
464 a_message_lexstr.s);
465 #endif
466 goto jerr;
467 case a_MESSAGE_T_DOLLAR:
468 case a_MESSAGE_T_UP:
469 case a_MESSAGE_T_SEMI:
470 pstate |= PS_MSGLIST_GABBY;
471 /* FALLTHRU */
472 case a_MESSAGE_T_DOT: /* Don't set _GABBY for dot, to _allow_ history.. */
473 pstate &= ~PS_MSGLIST_DIRECT;
474 a_message_lexno = a_message_metamess(a_message_lexstr.s[0], f);
475 if(a_message_lexno == -1)
476 goto jerr;
477 goto jnumber;
478 case a_MESSAGE_T_BACK:
479 pstate &= ~PS_MSGLIST_DIRECT;
480 if(flags & a_RANGE)
481 goto jebadrange;
483 flags |= a_TBACK;
484 for(i = 0; i < msgCount; ++i){
485 if((mp = &message[i])->m_flag & MHIDDEN)
486 continue;
487 if((mp->m_flag & MDELETED) != (unsigned)f){
488 if(!a_message_list_last_saw_d)
489 continue;
490 a_message_list_saw_d = TRU1;
492 if(mp->m_flag & MOLDMARK){
493 mark(i + 1, f);
494 flags &= ~a_TBACK;
495 flags |= a_ANY;
498 break;
499 case a_MESSAGE_T_ASTER:
500 pstate &= ~PS_MSGLIST_DIRECT;
501 if(flags & a_RANGE)
502 goto jebadrange;
503 flags |= a_ASTER;
504 break;
505 case a_MESSAGE_T_COMMA:
506 pstate &= ~PS_MSGLIST_DIRECT;
507 pstate |= PS_MSGLIST_GABBY;
508 if(flags & a_RANGE)
509 goto jebadrange;
511 if(id == NULL){
512 if((cp = hfield1("in-reply-to", dot)) != NULL)
513 idfield = a_MESSAGE_ID_IN_REPLY_TO;
514 else if((cp = hfield1("references", dot)) != NULL){
515 struct name *enp;
517 if((enp = extract(cp, GREF)) != NULL){
518 while(enp->n_flink != NULL)
519 enp = enp->n_flink;
520 cp = enp->n_name;
521 idfield = a_MESSAGE_ID_REFERENCES;
522 }else
523 cp = NULL;
526 if(cp != NULL)
527 id = savestr(cp);
528 else{
529 id = N_("Message-ID of parent of \"dot\" is indeterminable\n");
530 goto jerrmsg;
532 }else if(!(pstate & PS_HOOK) && (options & OPT_D_V))
533 n_err(_("Ignoring redundant specification of , selector\n"));
534 break;
535 case a_MESSAGE_T_ERROR:
536 pstate &= ~PS_MSGLIST_DIRECT;
537 pstate |= PS_MSGLIST_GABBY;
538 goto jerr;
541 /* Explicitly disallow invalid ranges for future safety */
542 if(bufp[0] == '-' && !(flags & a_RANGE)){
543 if(!(pstate & PS_HOOK))
544 n_err(_("Ignoring invalid range before: %s\n"), bufp);
545 ++bufp;
548 if(flags & a_RANGE){
549 id = N_("Missing second range argument\n");
550 goto jerrmsg;
553 np = a_message_add_to_namelist(&namelist, &nmlsize, np, NULL);
554 --np;
556 /* * is special at this point, after we have parsed the entire line */
557 if(flags & a_ASTER){
558 for(i = 0; i < msgCount; ++i){
559 if((mp = &message[i])->m_flag & MHIDDEN)
560 continue;
561 if(!a_message_list_saw_d && (mp->m_flag & MDELETED) != (unsigned)f)
562 continue;
563 mark(i + 1, f);
564 flags |= a_ANY;
566 if(!(flags & a_ANY))
567 goto jenoapp;
568 goto jleave;
571 /* If any names were given, add any messages which match */
572 if(np > namelist || id != NULL){
573 struct search_expr *sep = NULL;
575 /* The @ search works with struct search_expr, so build an array.
576 * To simplify array, i.e., regex_t destruction, and optimize for the
577 * common case we walk the entire array even in case of errors */
578 if(np > namelist){
579 sep = scalloc(PTR2SIZE(np - namelist), sizeof(*sep));
580 for(j = 0, nq = namelist; *nq != NULL; ++j, ++nq){
581 char *x, *y;
583 sep[j].ss_sexpr = x = *nq;
584 if(*x != '@' || (flags & a_ERROR))
585 continue;
587 for(y = &x[1];; ++y){
588 if(*y == '\0' || !fieldnamechar(*y)){
589 x = NULL;
590 break;
592 if(*y == '@'){
593 x = y;
594 break;
597 sep[j].ss_where = (x == NULL || x - 1 == *nq)
598 ? "subject" : savestrbuf(&(*nq)[1], PTR2SIZE(x - *nq) - 1);
600 x = (x == NULL ? *nq : x) + 1;
601 if(*x == '\0'){ /* XXX Simply remove from list instead? */
602 n_err(_("Empty [@..]@ search expression\n"));
603 flags |= a_ERROR;
604 continue;
606 #ifdef HAVE_REGEX
607 if(is_maybe_regex(x)){
608 sep[j].ss_sexpr = NULL;
609 if(regcomp(&sep[j].ss_regex, x,
610 REG_EXTENDED | REG_ICASE | REG_NOSUB) != 0){
611 if(!(pstate & PS_HOOK) && (options & OPT_D_V))
612 n_err(_("Invalid regular expression: >>> %s <<<\n"), x);
613 flags |= a_ERROR;
614 continue;
616 }else
617 #endif
618 sep[j].ss_sexpr = x;
620 if(flags & a_ERROR)
621 goto jnamesearch_sepfree;
624 /* Iterate the entire message array */
625 srelax_hold();
626 if(ok_blook(allnet))
627 flags |= a_ALLNET;
628 for(i = 0; i < msgCount; ++i){
629 if((mp = &message[i])->m_flag & (MMARK | MHIDDEN))
630 continue;
631 if(!a_message_list_saw_d && (mp->m_flag & MDELETED) != (unsigned)f)
632 continue;
634 flags &= ~a_TMP;
635 if(np > namelist){
636 for(nq = namelist; *nq != NULL; ++nq){
637 if(**nq == '@'){
638 if(a_message_match_at(mp, sep + PTR2SIZE(nq - namelist))){
639 flags |= a_TMP;
640 break;
642 }else if(**nq == '/'){
643 if(a_message_match_dash(mp, *nq)){
644 flags |= a_TMP;
645 break;
647 }else if(a_message_match_sender(mp, *nq, (flags & a_ALLNET))){
648 flags |= a_TMP;
649 break;
653 if(!(flags & a_TMP) &&
654 id != NULL && a_message_match_mid(mp, id, idfield))
655 flags |= a_TMP;
657 if(flags & a_TMP){
658 mark(i + 1, f);
659 flags |= a_ANY;
661 srelax();
663 srelax_rele();
665 jnamesearch_sepfree:
666 if(sep != NULL){
667 #ifdef HAVE_REGEX
668 for(j = PTR2SIZE(np - namelist); j-- != 0;)
669 if(sep[j].ss_sexpr == NULL)
670 regfree(&sep[j].ss_regex);
671 #endif
672 free(sep);
674 if(flags & a_ERROR)
675 goto jerr;
678 /* If any colon modifiers were given, go through and mark any messages which
679 * do satisfy the modifiers */
680 if(colmod != 0){
681 for(i = 0; i < msgCount; ++i){
682 struct a_message_coltab const *colp;
684 if((mp = &message[i])->m_flag & (MMARK | MHIDDEN))
685 continue;
686 if(!a_message_list_saw_d && (mp->m_flag & MDELETED) != (unsigned)f)
687 continue;
689 for(colp = a_message_coltabs;
690 PTRCMP(colp, <, &a_message_coltabs[NELEM(a_message_coltabs)]);
691 ++colp)
692 if((colp->mco_bit & colmod) &&
693 ((mp->m_flag & colp->mco_mask) == (unsigned)colp->mco_equal)){
694 mark(i + 1, f);
695 flags |= a_ANY;
696 break;
701 /* It shall be an error if ` didn't match anything, and nothing else did */
702 if((flags & (a_TBACK | a_ANY)) == a_TBACK){
703 id = N_("No previously marked messages\n");
704 goto jerrmsg;
705 }else if(!(flags & a_ANY))
706 goto jenoapp;
708 assert(!(flags & a_ERROR));
709 jleave:
710 if(flags & a_ALLOC){
711 free(namelist);
712 ac_free(a_message_lexstr.s);
714 NYD_LEAVE;
715 return (flags & a_ERROR) ? -1 : 0;
717 jebadrange:
718 id = N_("Invalid range endpoint\n");
719 goto jerrmsg;
720 jenoapp:
721 id = N_("No applicable messages\n");
722 jerrmsg:
723 if(!(pstate & PS_HOOK_MASK))
724 n_err(V_(id));
725 jerr:
726 flags |= a_ERROR;
727 goto jleave;
730 static int
731 a_message_evalcol(int col){
732 struct a_message_coltab const *colp;
733 int rv;
734 NYD2_ENTER;
736 rv = 0;
737 for(colp = a_message_coltabs;
738 PTRCMP(colp, <, &a_message_coltabs[NELEM(a_message_coltabs)]); ++colp)
739 if(colp->mco_char == col){
740 rv = colp->mco_bit;
741 break;
743 NYD2_LEAVE;
744 return rv;
747 static bool_t
748 a_message_check(int mno, int f){
749 struct message *mp;
750 NYD2_ENTER;
752 if(mno < 1 || mno > msgCount){
753 n_err(_("%d: Invalid message number\n"), mno);
754 mno = 1;
755 }else if(((mp = &message[mno - 1])->m_flag & MHIDDEN) ||
756 (f != MDELETED && (mp->m_flag & MDELETED) != 0))
757 n_err(_("%d: inappropriate message\n"), mno);
758 else
759 mno = 0;
760 NYD2_LEAVE;
761 return (mno == 0);
764 static int
765 a_message_scan(char **sp)
767 char *cp, *cp2;
768 struct a_message_lex const *lp;
769 int rv, c, inquote, quotec;
770 NYD_ENTER;
772 rv = a_MESSAGE_T_EOL;
774 cp = *sp;
775 cp2 = a_message_lexstr.s;
776 c = *cp++;
778 /* strip away leading white space */
779 while(blankchar(c))
780 c = *cp++;
782 /* If no characters remain, we are at end of line, so report that */
783 if(c == '\0'){
784 *sp = --cp;
785 goto jleave;
788 /* Select members of a message thread */
789 if(c == '&'){
790 if(*cp == '\0' || spacechar(*cp)){
791 a_message_lexstr.s[0] = '.';
792 a_message_lexstr.s[1] = '\0';
793 *sp = cp;
794 rv = a_MESSAGE_T_DOT | INT_MIN;
795 goto jleave;
797 rv = INT_MIN;
798 c = *cp++;
801 /* If the leading character is a digit, scan the number and convert it
802 * on the fly. Return a_MESSAGE_T_NUMBER when done */
803 if(digitchar(c)){
804 a_message_lexno = 0;
806 a_message_lexno = (a_message_lexno * 10) + c - '0';
807 *cp2++ = c;
808 }while((c = *cp++, digitchar(c)));
809 *cp2 = '\0';
810 *sp = --cp;
811 rv |= a_MESSAGE_T_NUMBER;
812 goto jleave;
815 /* An IMAP SEARCH list. Note that a_MESSAGE_T_OPEN has always been included
816 * in singles[] in Mail and mailx. Thus although there is no formal
817 * definition for (LIST) lists, they do not collide with historical
818 * practice because a subject string (LIST) could never been matched
819 * this way */
820 if (c == '(') {
821 ui32_t level = 1;
822 inquote = 0;
823 *cp2++ = c;
824 do {
825 if ((c = *cp++&0377) == '\0') {
826 jmtop:
827 n_err(_("Missing )\n"));
828 rv = a_MESSAGE_T_ERROR;
829 goto jleave;
831 if (inquote && c == '\\') {
832 *cp2++ = c;
833 c = *cp++&0377;
834 if (c == '\0')
835 goto jmtop;
836 } else if (c == '"')
837 inquote = !inquote;
838 else if (inquote)
839 /*EMPTY*/;
840 else if (c == '(')
841 ++level;
842 else if (c == ')')
843 --level;
844 else if (spacechar(c)) {
845 /* Replace unquoted whitespace by single space characters, to make
846 * the string IMAP SEARCH conformant */
847 c = ' ';
848 if (cp2[-1] == ' ')
849 --cp2;
851 *cp2++ = c;
852 } while (c != ')' || level > 0);
853 *cp2 = '\0';
854 *sp = cp;
855 rv |= a_MESSAGE_T_OPEN;
856 goto jleave;
859 /* Check for single character tokens; return such if found */
860 for(lp = a_message_singles;
861 PTRCMP(lp, <, &a_message_singles[NELEM(a_message_singles)]); ++lp)
862 if(c == lp->ml_char){
863 a_message_lexstr.s[0] = c;
864 a_message_lexstr.s[1] = '\0';
865 *sp = cp;
866 rv |= lp->ml_token;
867 goto jleave;
870 /* We've got a string! Copy all the characters of the string into
871 * a_message_lexstr, until we see a null, space, or tab. If the lead
872 * character is a " or ', save it and scan until you get another */
873 quotec = 0;
874 if (c == '\'' || c == '"') {
875 quotec = c;
876 c = *cp++;
878 while (c != '\0') {
879 if (quotec == 0 && c == '\\' && *cp != '\0')
880 c = *cp++;
881 if (c == quotec) {
882 ++cp;
883 break;
885 if (quotec == 0 && blankchar(c))
886 break;
887 if (PTRCMP(cp2 - a_message_lexstr.s, <, a_message_lexstr.l))
888 *cp2++ = c;
889 c = *cp++;
891 if (quotec && c == 0) {
892 n_err(_("Missing %c\n"), quotec);
893 rv = a_MESSAGE_T_ERROR;
894 goto jleave;
896 *sp = --cp;
897 *cp2 = '\0';
898 rv |= a_MESSAGE_T_STRING;
899 jleave:
900 NYD_LEAVE;
901 return rv;
904 static bool_t
905 a_message_match_sender(struct message *mp, char const *str, bool_t allnet){
906 char const *str_base, *np_base, *np;
907 char sc, nc;
908 bool_t rv;
909 NYD2_ENTER;
911 /* Empty string doesn't match */
912 if(*(str_base = str) == '\0'){
913 rv = FAL0;
914 goto jleave;
917 /* *allnet* is POSIX and, since it explicitly mentions login and user names,
918 * most likely case-sensitive. XXX Still allow substr matching, though
919 * XXX possibly the first letter should be case-insensitive, then? */
920 if(allnet){
921 for(np_base = np = nameof(mp, 0);;){
922 if((sc = *str++) == '@')
923 sc = '\0';
924 if((nc = *np++) == '@' || nc == '\0' || sc == '\0')
925 break;
926 if(sc != nc){
927 np = ++np_base;
928 str = str_base;
931 rv = (sc == '\0');
932 }else{
933 /* TODO POSIX says ~"match any address as shown in header overview",
934 * TODO but a normalized match would be more sane i guess.
935 * TODO struct name should gain a comparison method, normalize realname
936 * TODO content (in TODO) and thus match as likewise
937 * TODO "Buddy (Today) <here>" and "(Now) Buddy <here>" */
938 char const *real_base;
939 bool_t again;
941 real_base = name1(mp, 0);
942 again = ok_blook(showname);
943 jagain:
944 np_base = np = again ? realname(real_base) : skin(real_base);
945 str = str_base;
946 for(;;){
947 sc = *str++;
948 if((nc = *np++) == '\0' || sc == '\0')
949 break;
950 sc = upperconv(sc);
951 nc = upperconv(nc);
952 if(sc != nc){
953 np = ++np_base;
954 str = str_base;
958 /* And really if i want to match 'on@' then i want it to match even if
959 * *showname* is set! */
960 if(!(rv = (sc == '\0')) && again){
961 again = FAL0;
962 goto jagain;
965 jleave:
966 NYD2_LEAVE;
967 return rv;
970 static bool_t
971 a_message_match_mid(struct message *mp, char const *id,
972 enum a_message_idfield idfield){
973 char const *cp;
974 bool_t rv;
975 NYD2_ENTER;
977 rv = FAL0;
979 if((cp = hfield1("message-id", mp)) != NULL){
980 switch(idfield){
981 case a_MESSAGE_ID_REFERENCES:
982 if(!msgidcmp(id, cp))
983 rv = TRU1;
984 break;
985 case a_MESSAGE_ID_IN_REPLY_TO:{
986 struct name *np;
988 if((np = extract(id, GREF)) != NULL)
990 if(!msgidcmp(np->n_name, cp)){
991 rv = TRU1;
992 break;
994 }while((np = np->n_flink) != NULL);
995 break;
999 NYD2_LEAVE;
1000 return rv;
1003 static bool_t
1004 a_message_match_dash(struct message *mp, char const *str){
1005 static char lastscan[128];
1007 struct str in, out;
1008 char *hfield, *hbody;
1009 bool_t rv;
1010 NYD2_ENTER;
1012 rv = FAL0;
1014 if(*++str == '\0')
1015 str = lastscan;
1016 else
1017 n_strscpy(lastscan, str, sizeof lastscan); /* XXX use new n_str object! */
1019 /* Now look, ignoring case, for the word in the string */
1020 if(ok_blook(searchheaders) && (hfield = strchr(str, ':'))){
1021 size_t l;
1023 l = PTR2SIZE(hfield - str);
1024 hfield = ac_alloc(l +1);
1025 memcpy(hfield, str, l);
1026 hfield[l] = '\0';
1027 hbody = hfieldX(hfield, mp);
1028 ac_free(hfield);
1029 hfield = UNCONST(str + l + 1);
1030 }else{
1031 hfield = UNCONST(str);
1032 hbody = hfield1("subject", mp);
1034 if(hbody == NULL)
1035 goto jleave;
1037 in.l = strlen(in.s = hbody);
1038 mime_fromhdr(&in, &out, TD_ICONV);
1039 rv = substr(out.s, hfield);
1040 free(out.s);
1041 jleave:
1042 NYD2_LEAVE;
1043 return rv;
1046 static bool_t
1047 a_message_match_at(struct message *mp, struct search_expr *sep){
1048 struct str in, out;
1049 char *nfield;
1050 char const *cfield;
1051 bool_t rv;
1052 NYD2_ENTER;
1054 rv = FAL0;
1055 nfield = savestr(sep->ss_where);
1057 while((cfield = n_strsep(&nfield, ',', TRU1)) != NULL){
1058 if(!asccasecmp(cfield, "body") ||
1059 (cfield[1] == '\0' && cfield[0] == '>')){
1060 rv = FAL0;
1061 jmsg:
1062 if((rv = message_match(mp, sep, rv)))
1063 break;
1064 continue;
1065 }else if(!asccasecmp(cfield, "text") ||
1066 (cfield[1] == '\0' && cfield[0] == '=')){
1067 rv = TRU1;
1068 goto jmsg;
1071 if(!asccasecmp(cfield, "header") ||
1072 (cfield[1] == '\0' && cfield[0] == '<')){
1073 if((rv = header_match(mp, sep)))
1074 break;
1075 continue;
1078 /* This is not a special name, so take care for the "skin" prefix !
1079 * and possible abbreviations */
1080 /* C99 */{
1081 struct name *np;
1082 bool_t doskin;
1084 if((doskin = (*cfield == '~')))
1085 ++cfield;
1086 if(cfield[0] != '\0' && cfield[1] == '\0'){
1087 char const x[][8] = {"from", "to", "cc", "bcc", "subject"};
1088 size_t i;
1089 char c1;
1091 c1 = lowerconv(cfield[0]);
1092 for(i = 0; i < NELEM(x); ++i){
1093 if(c1 == x[i][0]){
1094 cfield = x[i];
1095 break;
1099 if((in.s = hfieldX(cfield, mp)) == NULL)
1100 continue;
1102 /* Shall we split into address list and match the addresses only? */
1103 if(doskin){
1104 np = lextract(in.s, GSKIN);
1105 if(np == NULL)
1106 continue;
1107 out.s = np->n_name;
1108 }else{
1109 np = NULL;
1110 in.l = strlen(in.s);
1111 mime_fromhdr(&in, &out, TD_ICONV);
1114 jnext_name:
1115 #ifdef HAVE_REGEX
1116 if(sep->ss_sexpr == NULL)
1117 rv = (regexec(&sep->ss_regex, out.s, 0,NULL, 0) != REG_NOMATCH);
1118 else
1119 #endif
1120 rv = substr(out.s, sep->ss_sexpr);
1121 if(np == NULL)
1122 free(out.s);
1123 if(rv)
1124 break;
1125 if(np != NULL && (np = np->n_flink) != NULL){
1126 out.s = np->n_name;
1127 goto jnext_name;
1131 NYD2_LEAVE;
1132 return rv;
1135 static void
1136 a_message_unmark(int mesg){
1137 size_t i;
1138 NYD2_ENTER;
1140 i = (size_t)mesg;
1141 if(i < 1 || UICMP(z, i, >, msgCount))
1142 n_panic(_("Bad message number to unmark"));
1143 message[--i].m_flag &= ~MMARK;
1144 NYD2_LEAVE;
1147 static int
1148 a_message_metamess(int meta, int f)
1150 int c, m;
1151 struct message *mp;
1152 NYD2_ENTER;
1154 c = meta;
1155 switch (c) {
1156 case '^': /* First 'good' message left */
1157 mp = mb.mb_threaded ? threadroot : message;
1158 while (PTRCMP(mp, <, message + msgCount)) {
1159 if (!(mp->m_flag & MHIDDEN) && (mp->m_flag & MDELETED) == (ui32_t)f) {
1160 c = (int)PTR2SIZE(mp - message + 1);
1161 goto jleave;
1163 if (mb.mb_threaded) {
1164 mp = next_in_thread(mp);
1165 if (mp == NULL)
1166 break;
1167 } else
1168 ++mp;
1170 if (!(pstate & PS_HOOK_MASK))
1171 n_err(_("No applicable messages\n"));
1172 goto jem1;
1174 case '$': /* Last 'good message left */
1175 mp = mb.mb_threaded
1176 ? this_in_thread(threadroot, -1) : message + msgCount - 1;
1177 while (mp >= message) {
1178 if (!(mp->m_flag & MHIDDEN) && (mp->m_flag & MDELETED) == (ui32_t)f) {
1179 c = (int)PTR2SIZE(mp - message + 1);
1180 goto jleave;
1182 if (mb.mb_threaded) {
1183 mp = prev_in_thread(mp);
1184 if (mp == NULL)
1185 break;
1186 } else
1187 --mp;
1189 if (!(pstate & PS_HOOK_MASK))
1190 n_err(_("No applicable messages\n"));
1191 goto jem1;
1193 case '.':
1194 /* Current message */
1195 m = dot - message + 1;
1196 if ((dot->m_flag & MHIDDEN) || (dot->m_flag & MDELETED) != (ui32_t)f) {
1197 n_err(_("%d: inappropriate message\n"), m);
1198 goto jem1;
1200 c = m;
1201 break;
1203 case ';':
1204 /* Previously current message */
1205 if (prevdot == NULL) {
1206 n_err(_("No previously current message\n"));
1207 goto jem1;
1209 m = prevdot - message + 1;
1210 if ((prevdot->m_flag & MHIDDEN) ||
1211 (prevdot->m_flag & MDELETED) != (ui32_t)f) {
1212 n_err(_("%d: inappropriate message\n"), m);
1213 goto jem1;
1215 c = m;
1216 break;
1218 default:
1219 n_err(_("Unknown selector: %c\n"), c);
1220 goto jem1;
1222 jleave:
1223 NYD2_LEAVE;
1224 return c;
1225 jem1:
1226 c = -1;
1227 goto jleave;
1230 static void
1231 a_message__threadmark(struct message *self, int f){
1232 NYD2_ENTER;
1233 if(!(self->m_flag & MHIDDEN) &&
1234 (f == MDELETED || !(self->m_flag & MDELETED) || a_message_list_saw_d))
1235 self->m_flag |= MMARK;
1237 if((self = self->m_child) != NULL){
1238 goto jcall;
1239 while((self = self->m_younger) != NULL)
1240 if(self->m_child != NULL)
1241 jcall:
1242 a_message__threadmark(self, f);
1243 else
1244 self->m_flag |= MMARK;
1246 NYD2_LEAVE;
1249 FL FILE *
1250 setinput(struct mailbox *mp, struct message *m, enum needspec need){
1251 FILE *rv;
1252 enum okay ok;
1253 NYD_ENTER;
1255 rv = NULL;
1256 ok = STOP;
1258 switch(need){
1259 case NEED_HEADER:
1260 ok = (m->m_have & HAVE_HEADER) ? OKAY : a_message_get_header(m);
1261 break;
1262 case NEED_BODY:
1263 ok = (m->m_have & HAVE_BODY) ? OKAY : get_body(m);
1264 break;
1265 case NEED_UNSPEC:
1266 ok = OKAY;
1267 break;
1269 if(ok != OKAY)
1270 goto jleave;
1272 fflush(mp->mb_otf);
1273 if(fseek(mp->mb_itf, (long)mailx_positionof(m->m_block, m->m_offset),
1274 SEEK_SET) == -1){
1275 n_perr(_("fseek"), 0);
1276 n_panic(_("temporary file seek"));
1278 rv = mp->mb_itf;
1279 jleave:
1280 NYD_LEAVE;
1281 return rv;
1284 FL enum okay
1285 get_body(struct message *mp){
1286 enum okay rv;
1287 NYD_ENTER;
1288 UNUSED(mp);
1290 switch(mb.mb_type){
1291 case MB_FILE:
1292 case MB_MAILDIR:
1293 rv = OKAY;
1294 break;
1295 #ifdef HAVE_POP3
1296 case MB_POP3:
1297 rv = pop3_body(mp);
1298 break;
1299 #endif
1300 case MB_VOID:
1301 default:
1302 rv = STOP;
1303 break;
1305 NYD_LEAVE;
1306 return rv;
1309 FL void
1310 message_reset(void){
1311 NYD_ENTER;
1312 if(message != NULL){
1313 free(message);
1314 message = NULL;
1316 msgCount = 0;
1317 a_message_mem_space = 0;
1318 NYD_LEAVE;
1321 FL void
1322 message_append(struct message *mp){
1323 NYD_ENTER;
1324 if(UICMP(z, msgCount + 1, >=, a_message_mem_space)){
1325 /* XXX remove _mem_space magics (or use s_Vector) */
1326 a_message_mem_space = ((a_message_mem_space >= 128 &&
1327 a_message_mem_space <= 1000000)
1328 ? a_message_mem_space << 1 : a_message_mem_space + 64);
1329 message = srealloc(message, a_message_mem_space * sizeof(*message));
1331 if(msgCount > 0){
1332 if(mp != NULL)
1333 message[msgCount - 1] = *mp;
1334 else
1335 memset(&message[msgCount - 1], 0, sizeof *message);
1337 NYD_LEAVE;
1340 FL void
1341 message_append_null(void){
1342 NYD_ENTER;
1343 if(msgCount == 0)
1344 message_append(NULL);
1345 setdot(message);
1346 message[msgCount].m_size = 0;
1347 message[msgCount].m_lines = 0;
1348 NYD_LEAVE;
1351 FL bool_t
1352 message_match(struct message *mp, struct search_expr const *sep,
1353 bool_t with_headers){
1354 char **line;
1355 size_t *linesize, cnt;
1356 FILE *fp;
1357 bool_t rv;
1358 NYD_ENTER;
1360 rv = FAL0;
1362 if((fp = Ftmp(NULL, "mpmatch", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL)
1363 goto j_leave;
1365 if(sendmp(mp, fp, NULL, NULL, SEND_TOSRCH, NULL) < 0)
1366 goto jleave;
1367 fflush_rewind(fp);
1369 cnt = fsize(fp);
1370 line = &termios_state.ts_linebuf; /* XXX line pool */
1371 linesize = &termios_state.ts_linesize; /* XXX line pool */
1373 if(!with_headers)
1374 while(fgetline(line, linesize, &cnt, NULL, fp, 0))
1375 if (**line == '\n')
1376 break;
1378 while(fgetline(line, linesize, &cnt, NULL, fp, 0)){
1379 #ifdef HAVE_REGEX
1380 if(sep->ss_sexpr == NULL){
1381 if(regexec(&sep->ss_regex, *line, 0,NULL, 0) == REG_NOMATCH)
1382 continue;
1383 }else
1384 #endif
1385 if(!substr(*line, sep->ss_sexpr))
1386 continue;
1387 rv = TRU1;
1388 break;
1391 jleave:
1392 Fclose(fp);
1393 j_leave:
1394 NYD_LEAVE;
1395 return rv;
1398 FL struct message *
1399 setdot(struct message *mp){
1400 NYD_ENTER;
1401 if(dot != mp){
1402 prevdot = dot;
1403 pstate &= ~PS_DID_PRINT_DOT;
1405 dot = mp;
1406 uncollapse1(dot, 0);
1407 NYD_LEAVE;
1408 return dot;
1411 FL void
1412 touch(struct message *mp){
1413 NYD_ENTER;
1414 mp->m_flag |= MTOUCH;
1415 if(!(mp->m_flag & MREAD))
1416 mp->m_flag |= MREAD | MSTATUS;
1417 NYD_LEAVE;
1420 FL int
1421 getmsglist(char *buf, int *vector, int flags)
1423 int *ip, mc;
1424 struct message *mp;
1425 NYD_ENTER;
1427 pstate &= ~PS_ARGLIST_MASK;
1428 a_message_list_last_saw_d = a_message_list_saw_d;
1429 a_message_list_saw_d = FAL0;
1431 if(msgCount == 0){
1432 *vector = 0;
1433 mc = 0;
1434 goto jleave;
1437 pstate |= PS_MSGLIST_DIRECT;
1439 if(a_message_markall(buf, flags) < 0){
1440 mc = -1;
1441 goto jleave;
1444 ip = vector;
1445 if(pstate & PS_HOOK_NEWMAIL){
1446 mc = 0;
1447 for(mp = message; mp < &message[msgCount]; ++mp)
1448 if(mp->m_flag & MMARK){
1449 if(!(mp->m_flag & MNEWEST))
1450 a_message_unmark((int)PTR2SIZE(mp - message + 1));
1451 else
1452 ++mc;
1454 if(mc == 0){
1455 mc = -1;
1456 goto jleave;
1460 if(mb.mb_threaded == 0){
1461 for(mp = message; mp < &message[msgCount]; ++mp)
1462 if(mp->m_flag & MMARK)
1463 *ip++ = (int)PTR2SIZE(mp - message + 1);
1464 }else{
1465 for(mp = threadroot; mp != NULL; mp = next_in_thread(mp))
1466 if(mp->m_flag & MMARK)
1467 *ip++ = (int)PTR2SIZE(mp - message + 1);
1469 *ip = 0;
1470 mc = (int)PTR2SIZE(ip - vector);
1471 if(mc != 1)
1472 pstate &= ~PS_MSGLIST_DIRECT;
1473 jleave:
1474 NYD_LEAVE;
1475 return mc;
1478 FL int
1479 first(int f, int m)
1481 struct message *mp;
1482 int rv;
1483 NYD_ENTER;
1485 if (msgCount == 0) {
1486 rv = 0;
1487 goto jleave;
1490 f &= MDELETED;
1491 m &= MDELETED;
1492 for (mp = dot;
1493 mb.mb_threaded ? (mp != NULL) : PTRCMP(mp, <, message + msgCount);
1494 mb.mb_threaded ? (mp = next_in_thread(mp)) : ++mp) {
1495 if (!(mp->m_flag & MHIDDEN) && (mp->m_flag & m) == (ui32_t)f) {
1496 rv = (int)PTR2SIZE(mp - message + 1);
1497 goto jleave;
1501 if (dot > message) {
1502 for (mp = dot - 1; (mb.mb_threaded ? (mp != NULL) : (mp >= message));
1503 mb.mb_threaded ? (mp = prev_in_thread(mp)) : --mp) {
1504 if (!(mp->m_flag & MHIDDEN) && (mp->m_flag & m) == (ui32_t)f) {
1505 rv = (int)PTR2SIZE(mp - message + 1);
1506 goto jleave;
1510 rv = 0;
1511 jleave:
1512 NYD_LEAVE;
1513 return rv;
1516 FL void
1517 mark(int mno, int f){
1518 struct message *mp;
1519 int i;
1520 NYD_ENTER;
1522 i = mno;
1523 if(i < 1 || i > msgCount)
1524 n_panic(_("Bad message number to mark"));
1525 mp = &message[--i];
1527 if(mb.mb_threaded == 1 && a_message_threadflag)
1528 a_message__threadmark(mp, f);
1529 else{
1530 assert(!(mp->m_flag & MHIDDEN));
1531 mp->m_flag |= MMARK;
1533 NYD_LEAVE;
1536 /* s-it-mode */