1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
|
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999-2025 Free Software Foundation, Inc.
GNU Mailutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Mailutils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
%{
#include "mail.h"
#include <stdio.h>
#include <stdlib.h>
/* Defined in <limits.h> on some systems, but redefined in <regex.h>
if we are using GNU's regex. So, undef it to avoid duplicate definition
warnings. */
#ifdef RE_DUP_MAX
# undef RE_DUP_MAX
#endif
#include <regex.h>
struct header_data
{
char *header;
char *expr;
};
static msgset_t *msgset_select (int (*sel) (mu_message_t, void *),
void *closure, int rev,
unsigned int max_matches);
static int select_header (mu_message_t msg, void *closure);
static int select_body (mu_message_t msg, void *closure);
static int select_sender (mu_message_t msg, void *closure);
static int select_deleted (mu_message_t msg, void *closure);
static int check_set (msgset_t **pset, int flags);
struct msgset_parser
{
int flags;
size_t message_count;
msgset_t *result;
};
struct msgset_lexer
{
int xargc;
char **xargv;
int cur_ind;
char *cur_p;
mu_opool_t tokpool;
};
int yyerror (struct msgset_parser *, struct msgset_lexer *, const char *);
int yylex (struct msgset_lexer *);
static void
msgset_parser_free (struct msgset_parser *parser)
{
msgset_free (parser->result);
}
static void
msgset_lexer_free (struct msgset_lexer *lexer)
{
mu_opool_destroy (&lexer->tokpool);
}
static msgset_t *
msgset_concat (msgset_t *mset, msgset_t *part)
{
msgset_t *ret = msgset_expand (mset, part);
msgset_free (mset);
msgset_free (part);
return ret;
}
typedef int (*message_selector_t) (mu_message_t, void *);
static message_selector_t find_type_selector (int type);
%}
%parse-param { struct msgset_parser *parser } { struct msgset_lexer *lexer }
%lex-param { struct msgset_lexer *lexer }
%expect 3
%union {
char *string;
int number;
int type;
msgset_t *mset;
}
%token DELIM
%token <type> TYPE
%token <string> IDENT REGEXP HEADER BODY
%token <number> NUMBER
%type <mset> msgset msgspec msgexpr msg rangeset range partno number
%type <string> header
%%
input : /* empty */
{
parser->result = msgset_make_1 (get_cursor ());
}
| msgset
{
parser->result = $1;
}
| '*'
{
parser->result = msgset_select (select_deleted, lexer, 0, total);
}
;
msgset : msgexpr
| msgset delim msgexpr
{
$$ = msgset_append ($1, $3);
}
;
delim : DELIM
| ','
| delim DELIM
| delim ','
;
opt_delim: /* empty */
| delim
;
msgexpr : msgspec
{
$$ = $1;
if (check_set (&$$, parser->flags))
YYABORT;
}
| '{' opt_delim msgset opt_delim '}'
{
$$ = $3;
}
| '!' opt_delim msgexpr
{
$$ = msgset_negate ($3);
}
;
msgspec : msg
| msg dot rangeset
{
$$ = msgset_concat ($1, $3);
}
| '.' rangeset
{
$$ = msgset_concat (msgset_make_1 (get_cursor ()), $2);
}
| msg obracket rangeset ']'
{
$$ = msgset_concat ($1, $3);
}
| range
;
msg : '.'
{
$$ = msgset_make_1 (get_cursor ());
}
| first
{
$$ = msgset_select (select_deleted, lexer, 0, 1);
}
| last
{
$$ = msgset_select (select_deleted, lexer, 1, 1);
}
| header REGEXP /* /.../ */
{
struct header_data hd;
hd.header = $1;
hd.expr = $2;
$$ = msgset_select (select_header, &hd, 0, 0);
if (!$$)
{
if ($1)
mu_error (_("No applicable messages from {%s:/%s}"), $1, $2);
else
mu_error (_("No applicable messages from {/%s}"), $2);
YYERROR;
}
}
| BODY
{
$$ = msgset_select (select_body, $1, 0, 0);
if (!$$)
{
mu_error (_("No applicable messages from {:/%s}"), $1);
YYERROR;
}
}
| TYPE /* :n, :d, etc */
{
message_selector_t sel = find_type_selector ($1);
if (!sel)
{
yyerror (parser, lexer, _("unknown message type"));
YYERROR;
}
$$ = msgset_select (sel, NULL, 0, 0);
if (!$$)
{
mu_error (_("No messages satisfy :%c"), $1);
YYERROR;
}
}
| IDENT /* Sender name */
{
$$ = msgset_select (select_sender, (void *)$1, 0, 0);
if (!$$)
{
mu_error (_("No applicable messages from {%s}"), $1);
YYERROR;
}
}
;
first : '^'
| '+'
;
last : '$'
| '-'
;
header : /* empty */
{
$$ = NULL;
}
| HEADER
{
$$ = $1;
}
;
rangeset : range
| rangeset ',' range
{
$$ = msgset_append ($1, $3);
}
;
range : number
| NUMBER '-' number
{
if (msgset_length ($3) == 1)
{
if (($$ = msgset_range ($1, msgset_msgno ($3))) == NULL)
yyerror (parser, lexer, _("range error"));
}
else
{
$$ = msgset_range ($1, msgset_msgno ($3) - 1);
if (!$$)
{
yyerror (parser, lexer, _("range error"));
YYERROR;
}
msgset_append ($$, $3);
}
}
| NUMBER '-' '*'
{
if (($$ = msgset_range ($1, total)) == NULL)
yyerror (parser, lexer, _("range error"));
}
;
number : partno
| partno dot rangeset
{
$$ = msgset_concat ($1, $3);
}
| partno obracket rangeset ']'
{
$$ = msgset_concat ($1, $3);
}
;
partno : NUMBER
{
$$ = msgset_make_1 ($1);
}
| '(' rangeset ')'
{
$$ = $2;
}
;
dot : '.'
{
if (!(parser->flags & MSG_ALLOWPART))
{
yyerror (parser, lexer, _("message parts not allowed"));
YYERROR;
}
}
obracket : '['
{
if (!(parser->flags & MSG_ALLOWPART))
{
yyerror (parser, lexer, _("message parts not allowed"));
YYERROR;
}
}
%%
int
yyerror (struct msgset_parser *parser, struct msgset_lexer *lexer,
const char *s)
{
mu_stream_printf (mu_strerr, "%s: ", lexer->xargv[0]);
mu_stream_printf (mu_strerr, "%s", s);
if (!lexer->cur_p)
mu_stream_printf (mu_strerr, _(" near end"));
else if (*lexer->cur_p == 0)
{
int i = (*lexer->cur_p == 0) ? lexer->cur_ind + 1 : lexer->cur_ind;
if (i == lexer->xargc)
mu_stream_printf (mu_strerr, _(" near end"));
else
mu_stream_printf (mu_strerr, _(" near %s"), lexer->xargv[i]);
}
else
mu_stream_printf (mu_strerr, _(" near %s"), lexer->cur_p);
mu_stream_printf (mu_strerr, "\n");
return 0;
}
int
yylex (struct msgset_lexer *lexer)
{
if (lexer->cur_ind == lexer->xargc)
return 0;
if (!lexer->cur_p)
lexer->cur_p = lexer->xargv[lexer->cur_ind];
if (*lexer->cur_p == 0)
{
lexer->cur_ind++;
lexer->cur_p = NULL;
if (lexer->cur_ind == lexer->xargc)
return 0;
return DELIM;
}
if (mu_isdigit (*lexer->cur_p))
{
yylval.number = strtoul (lexer->cur_p, &lexer->cur_p, 10);
return NUMBER;
}
if (mu_isalpha (*lexer->cur_p))
{
char *p = lexer->cur_p;
while (*lexer->cur_p && *lexer->cur_p != ',' && *lexer->cur_p != ':')
lexer->cur_p++;
mu_opool_append (lexer->tokpool, p, lexer->cur_p - p);
mu_opool_append_char (lexer->tokpool, 0);
yylval.string = mu_opool_finish (lexer->tokpool, NULL);
if (*lexer->cur_p == ':')
{
++lexer->cur_p;
return HEADER;
}
return IDENT;
}
if (*lexer->cur_p == '/')
{
char *p = ++lexer->cur_p;
while (*lexer->cur_p && *lexer->cur_p != '/')
lexer->cur_p++;
mu_opool_append (lexer->tokpool, p, lexer->cur_p - p);
mu_opool_append_char (lexer->tokpool, 0);
yylval.string = mu_opool_finish (lexer->tokpool, NULL);
if (*lexer->cur_p)
lexer->cur_p++;
return REGEXP;
}
if (*lexer->cur_p == ':')
{
lexer->cur_p++;
if (*lexer->cur_p == '/')
{
char *p = ++lexer->cur_p;
while (*lexer->cur_p && *lexer->cur_p != '/')
lexer->cur_p++;
mu_opool_append (lexer->tokpool, p, lexer->cur_p - p);
mu_opool_append_char (lexer->tokpool, 0);
yylval.string = mu_opool_finish (lexer->tokpool, NULL);
if (*lexer->cur_p)
lexer->cur_p++;
return BODY;
}
if (*lexer->cur_p == 0)
return 0;
yylval.type = *lexer->cur_p++;
return TYPE;
}
return *lexer->cur_p++;
}
int
msgset_parse (const int argc, char **argv, int flags, msgset_t **mset)
{
int rc;
struct msgset_parser parser;
struct msgset_lexer lexer;
lexer.xargc = argc;
lexer.xargv = argv;
lexer.cur_ind = 1;
lexer.cur_p = NULL;
mu_opool_create (&lexer.tokpool, MU_OPOOL_ENOMEMABRT);
parser.flags = flags;
mu_mailbox_messages_count (mbox, &parser.message_count);
parser.result = NULL;
rc = yyparse (&parser, &lexer);
if (rc == 0)
{
if (!parser.result)
{
util_noapp ();
rc = 1;
}
else
{
if (parser.result->crd[1] > parser.message_count)
{
util_error_range (parser.result->crd[1]);
rc = 1;
}
else
{
*mset = parser.result;
parser.result = NULL;
}
}
}
msgset_parser_free (&parser);
msgset_lexer_free (&lexer);
return rc;
}
void
msgset_free (msgset_t *msg_set)
{
msgset_t *next;
while (msg_set)
{
next = msg_set->next;
free (msg_set->crd);
free (msg_set);
msg_set = next;
}
}
size_t
msgset_count (msgset_t *set)
{
size_t count = 0;
for (; set; set = set->next)
count++;
return count;
}
/* Create a message set consisting of a single msg_num and no subparts */
msgset_t *
msgset_make_1 (size_t number)
{
msgset_t *mp;
if (number == 0)
return NULL;
mp = mu_alloc (sizeof (*mp));
mp->next = NULL;
if (mu_coord_alloc (&mp->crd, 1))
mu_alloc_die ();
mp->crd[1] = number;
return mp;
}
msgset_t *
msgset_dup (const msgset_t *set)
{
msgset_t *mp;
mp = mu_alloc (sizeof (*mp));
mp->next = NULL;
if (mu_coord_dup (set->crd, &mp->crd))
mu_alloc_die ();
return mp;
}
/* Append message set TWO to the end of message set ONE. Take care to
eliminate duplicates. Preserve the ordering of both lists. Return
the resulting set.
The function is destructive: the set TWO is attached to ONE and
eventually modified to avoid duplicates.
*/
msgset_t *
msgset_append (msgset_t *one, msgset_t *two)
{
msgset_t *last;
if (!one)
return two;
for (last = one; last->next; last = last->next)
{
msgset_remove (&two, msgset_msgno (last));
}
last->next = two;
return one;
}
int
msgset_member (msgset_t *set, size_t n)
{
for (; set; set = set->next)
if (msgset_msgno (set) == n)
return 1;
return 0;
}
void
msgset_remove (msgset_t **pset, size_t n)
{
msgset_t *cur = *pset, **pnext = pset;
while (1)
{
if (cur == NULL)
return;
if (msgset_msgno (cur) == n)
break;
pnext = &cur->next;
cur = cur->next;
}
*pnext = cur->next;
cur->next = NULL;
msgset_free (cur);
}
msgset_t *
msgset_negate (msgset_t *set)
{
size_t i;
msgset_t *first = NULL, *last = NULL;
for (i = 1; i <= total; i++)
{
if (!msgset_member (set, i))
{
msgset_t *mp = msgset_make_1 (i);
if (!first)
first = mp;
else
last->next = mp;
last = mp;
}
}
return first;
}
msgset_t *
msgset_range (int low, int high)
{
int i;
msgset_t *mp, *first = NULL, *last = NULL;
if (low == high)
return msgset_make_1 (low);
if (low >= high)
return NULL;
for (i = 0; low <= high; i++, low++)
{
mp = msgset_make_1 (low);
if (!first)
first = mp;
else
last->next = mp;
last = mp;
}
return first;
}
msgset_t *
msgset_expand (msgset_t *set, msgset_t *expand_by)
{
msgset_t *i, *j;
msgset_t *first = NULL, *last = NULL, *mp;
for (i = set; i; i = i->next)
for (j = expand_by; j; j = j->next)
{
mp = mu_alloc (sizeof *mp);
mp->next = NULL;
if (mu_coord_alloc (&mp->crd,
mu_coord_length (i->crd) + mu_coord_length (j->crd)))
mu_alloc_die ();
memcpy (&mp->crd[1], &i->crd[1],
mu_coord_length (i->crd) * sizeof i->crd[0]);
memcpy (&mp->crd[1] + mu_coord_length (i->crd), &j->crd[1],
mu_coord_length (j->crd) * sizeof j->crd[0]);
if (!first)
first = mp;
else
last->next = mp;
last = mp;
}
return first;
}
msgset_t *
msgset_select (message_selector_t sel, void *closure, int rev,
unsigned int max_matches)
{
size_t i, match_count = 0;
msgset_t *first = NULL, *last = NULL, *mp;
mu_message_t msg = NULL;
if (max_matches == 0)
max_matches = total;
if (rev)
{
for (i = total; i > 0; i--)
{
mu_mailbox_get_message (mbox, i, &msg);
if ((*sel) (msg, closure))
{
mp = msgset_make_1 (i);
if (!first)
first = mp;
else
last->next = mp;
last = mp;
if (++match_count == max_matches)
break;
}
}
}
else
{
for (i = 1; i <= total; i++)
{
mu_mailbox_get_message (mbox, i, &msg);
if ((*sel) (msg, closure))
{
mp = msgset_make_1 (i);
if (!first)
first = mp;
else
last->next = mp;
last = mp;
if (++match_count == max_matches)
break;
}
}
}
return first;
}
int
select_header (mu_message_t msg, void *closure)
{
struct header_data *hd = (struct header_data *)closure;
mu_header_t hdr;
char *contents;
const char *header = hd->header ? hd->header : MU_HEADER_SUBJECT;
mu_message_get_header (msg, &hdr);
if (mu_header_aget_value (hdr, header, &contents) == 0)
{
if (mailvar_get (NULL, "regex", mailvar_type_boolean, 0) == 0)
{
/* Match string against the extended regular expression(ignoring
case) in pattern, treating errors as no match.
Return 1 for match, 0 for no match.
*/
regex_t re;
int status;
int flags = REG_EXTENDED;
if (mu_islower (header[0]))
flags |= REG_ICASE;
if (regcomp (&re, hd->expr, flags) != 0)
{
free (contents);
return 0;
}
status = regexec (&re, contents, 0, NULL, 0);
free (contents);
regfree (&re);
return status == 0;
}
else
{
int rc;
mu_strupper (contents);
rc = strstr (contents, hd->expr) != NULL;
free (contents);
return rc;
}
}
return 0;
}
int
select_body (mu_message_t msg, void *closure)
{
char *expr = closure;
int noregex = mailvar_get (NULL, "regex", mailvar_type_boolean, 0);
regex_t re;
int status = 0;
mu_body_t body = NULL;
mu_stream_t stream = NULL;
int rc;
if (noregex)
mu_strupper (expr);
else if (regcomp (&re, expr, REG_EXTENDED | REG_ICASE) != 0)
return 0;
mu_message_get_body (msg, &body);
rc = mu_body_get_streamref (body, &stream);
if (rc == 0)
{
char *buffer = NULL;
size_t size = 0;
size_t n = 0;
while (status == 0
&& (rc = mu_stream_getline (stream, &buffer, &size, &n)) == 0
&& n > 0)
{
if (noregex)
{
/* FIXME: charset */
mu_strupper (buffer);
status = strstr (buffer, expr) != NULL;
}
else
status = regexec (&re, buffer, 0, NULL, 0) == 0;
}
mu_stream_destroy (&stream);
free (buffer);
if (rc)
mu_diag_funcall (MU_DIAG_ERROR, "mu_stream_getline", NULL, rc);
}
else
mu_diag_funcall (MU_DIAG_ERROR, "mu_body_get_streamref", NULL, rc);
if (!noregex)
regfree (&re);
return status;
}
int
select_sender (mu_message_t msg, void *closure)
{
char *needle = (char*) closure;
char *sender = sender_string (msg);
int status = strcmp (sender, needle) == 0;
free (sender);
return status;
}
static int
select_type_d (mu_message_t msg, void *unused MU_ARG_UNUSED)
{
mu_attribute_t attr;
if (mu_message_get_attribute (msg, &attr) == 0)
return mu_attribute_is_deleted (attr);
return 0;
}
static int
select_type_n (mu_message_t msg, void *unused MU_ARG_UNUSED)
{
mu_attribute_t attr;
if (mu_message_get_attribute (msg, &attr) == 0)
return mu_attribute_is_recent (attr);
return 0;
}
static int
select_type_o (mu_message_t msg, void *unused MU_ARG_UNUSED)
{
mu_attribute_t attr;
if (mu_message_get_attribute (msg, &attr) == 0)
return mu_attribute_is_seen (attr);
return 0;
}
static int
select_type_r (mu_message_t msg, void *unused MU_ARG_UNUSED)
{
mu_attribute_t attr;
if (mu_message_get_attribute (msg, &attr) == 0)
return mu_attribute_is_read (attr);
return 0;
}
static int
select_type_s (mu_message_t msg, void *unused MU_ARG_UNUSED)
{
mu_attribute_t attr;
if (mu_message_get_attribute (msg, &attr) == 0)
return mu_attribute_is_userflag (attr, MAIL_ATTRIBUTE_SAVED);
return 0;
}
static int
select_type_t (mu_message_t msg, void *unused MU_ARG_UNUSED)
{
mu_attribute_t attr;
if (mu_message_get_attribute (msg, &attr) == 0)
return mu_attribute_is_userflag (attr, MAIL_ATTRIBUTE_TAGGED);
return 0;
}
static int
select_type_T (mu_message_t msg, void *unused MU_ARG_UNUSED)
{
mu_attribute_t attr;
if (mu_message_get_attribute (msg, &attr) == 0)
return !mu_attribute_is_userflag (attr, MAIL_ATTRIBUTE_TAGGED);
return 0;
}
static int
select_type_u (mu_message_t msg, void *unused MU_ARG_UNUSED)
{
mu_attribute_t attr;
if (mu_message_get_attribute (msg, &attr) == 0)
return !mu_attribute_is_read (attr);
return 0;
}
struct type_selector
{
int letter;
message_selector_t func;
};
static struct type_selector type_selector[] = {
{ 'd', select_type_d },
{ 'n', select_type_n },
{ 'o', select_type_o },
{ 'r', select_type_r },
{ 's', select_type_s },
{ 't', select_type_t },
{ 'T', select_type_T },
{ 'u', select_type_u },
{ '/', NULL }, /* A pseudo-entry needed for msgtype_generator only */
{ 0 }
};
static message_selector_t
find_type_selector (int type)
{
struct type_selector *p;
for (p = type_selector; p->func; p++)
{
if (p->letter == type)
return p->func;
}
return NULL;
}
#ifdef WITH_READLINE
char *
msgtype_generator (const char *text, int state)
{
/* Allowed message types, plus '/'. The latter can folow a colon,
meaning body lookup */
static int i;
char c;
if (!state)
{
i = 0;
}
while ((c = type_selector[i].letter))
{
i++;
if (!text[1] || text[1] == c)
{
char *s = mu_alloc (3);
s[0] = ':';
s[1] = c;
s[2] = 0;
return s;
}
}
return NULL;
}
#endif
int
select_deleted (mu_message_t msg, void *closure)
{
mu_attribute_t attr= NULL;
int rc;
struct msgset_lexer *lexer = closure;
mu_message_get_attribute (msg, &attr);
rc = mu_attribute_is_deleted (attr);
return strcmp (lexer->xargv[0], "undelete") == 0 ? rc : !rc;
}
int
check_set (msgset_t **pset, int flags)
{
int rc = 0;
if (!*pset)
{
util_noapp ();
return 1;
}
if (msgset_count (*pset) == 1)
flags ^= MSG_SILENT;
if (flags & MSG_NODELETED)
{
msgset_t *p = *pset, *prev = NULL;
msgset_t *delset = NULL;
while (p)
{
msgset_t *next = p->next;
if (util_isdeleted (msgset_msgno (p)))
{
if ((flags & MSG_SILENT) && (prev || next))
{
/* Mark subset as deleted */
p->next = delset;
delset = p;
/* Remove it from the set */
if (prev)
prev->next = next;
else
*pset = next;
}
else
{
mu_error (_("%lu: Inappropriate message (has been deleted)"),
(unsigned long) msgset_msgno (p));
/* Delete entire set */
delset = *pset;
*pset = NULL;
rc = 1;
break;
}
}
else
prev = p;
p = next;
}
if (delset)
msgset_free (delset);
if (!*pset)
rc = 1;
}
return rc;
}
#if 0
void
msgset_print (msgset_t *mset)
{
int i;
mu_printf ("(");
mu_printf ("%d .", mset->msg_part[0]);
for (i = 1; i < mset->npart; i++)
{
mu_printf (" %d", mset->msg_part[i]);
}
mu_printf (")\n");
}
int
main(int argc, char **argv)
{
msgset_t *mset = NULL;
int rc = msgset_parse (argc, argv, &mset);
for (; mset; mset = mset->next)
msgset_print (mset);
return 0;
}
#endif
|