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 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
|
/*
* mewl -- Message scanner for Mew 4 or later
*
* Author: Kazu Yamamoto <Kazu@Mew.org>
* Created: Jul 7, 2000
*
* Code:
*/
#include "mew.h"
private char version_message[] = "version 6.8 20180607 Kazu Yamamoto";
#ifdef HAVE_UNISTD_H
# include <sys/types.h>
# include <unistd.h>
#endif /* HAVE_UNISTD_H */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/stat.h>
#ifdef HAVE_DIRENT_H
# include <dirent.h>
# define NAMLEN(dirent) strlen((dirent)->d_name)
#else /* HAVE_DIRENT_H */
# define dirent direct
# define NAMLEN(dirent) (dirent)->d_namlen
# ifdef HAVE_SYS_NDIR_H
# include <sys/ndir.h>
# endif /* HAVE_SYS_NDIR_H */
# ifdef HAVE_SYS_DIR_H
# include <sys/dir.h>
# endif /* HAVE_SYS_DIR_H */
# ifdef HAVE_NDIR_H
# include <ndir.h>
# endif /* HAVE_NDIR_H */
#endif /* HAVE_DIRENT_H */
#ifdef HAVE_DIRECT_H
#include <direct.h>
#endif
/* for chdir() */
#ifdef HAVE_DIR_H
# include <dir.h>
#endif /* HAVE_DIR_H */
#ifdef HAVE_PWD_H
# include <pwd.h>
#endif /* HAVE_PWD_H */
/****************************************************************
*
* Macro, structure, and tables
*
*/
#define MAILHOME "Mail"
#define CHDIR "CD:"
#define INBOX "+inbox"
#define STDINFILE "-"
#define HEAD "head"
#define FILE_SEPARATOR '\7'
/* 64k, now even 16k is not enough. */
#define MAX_HEADER 65536
#define MAX_FIELD_LEN 5
#define MAX_BODY_LEN 20
#define MAX_FOLDER 16
#define NUMBER_OF_FILE 128
#define MAX_ARGC 16
#ifdef PATH_MAX
# define MAX_FILE_NAME_LEN PATH_MAX
#else
# define MAX_FILE_NAME_LEN 1024
#endif
#define DEFAULT_FIELDS \
"Subject:,Date:,From:,To:,Cc:,Content-Type:,Content-Transfer-Encoding:,X-Mew-UIDL:,Message-ID:,In-Reply-To:,References:,X-Mew-Ref:"
#define REFERENCES "References:"
struct fld_rng {
char *folder;
unsigned int first;
unsigned int last;
unsigned int num;
};
private char Buf_header[MAX_HEADER];
private char Buf_filenm[MAX_FILE_NAME_LEN];
private char Buf_filenm2[MAX_FILE_NAME_LEN];
private char *Mail_home;
private char Current_folder[MAX_FILE_NAME_LEN];
private struct fld_rng Folders[MAX_FOLDER];
private char *Argvec[MAX_ARGC];
private char *Key_field = NULL;
private char *Exec_cmd = NULL;
private char *Exec_arg = NULL;
private char *Input_file = NULL;
private char *Suffix = NULL;
private char *Suffix_fmt = NULL;
private int All_fields = 0;
private int Field_len = MAX_FIELD_LEN;
private int Body_len = MAX_BODY_LEN;
private int PrintNumOfMsg = NO;
private int Suffix_len = 0;
private int no_fld_flag = 0;
private int default_fld_error_flag = 0;
/****************************************************************
*
* prototype
*
*/
private void usage(const char *);
private void help(const char *);
private void version(const char *);
private void ch_home(void);
private void ch_mail_home(char *);
private void ch_folder(char *);
private char *nextline(char *, char *, int);
private char *nextfield(char *, char *, char **, char **, int);
private char *eoh(char *, char *);
private void init_fields(char *);
private void print_field(char *, int, char *, char *);
private void init_search(char *, int);
private char *mystrcasechr(char *, int);
private char *mystrcasestr(char *, char *);
private int print_for_scan(FILE *, char *, char *);
private int print_for_sort(FILE *, char *, char *);
private int print_for_pick(FILE *, char *, char *);
private int scan_numfile(char *, unsigned int, unsigned int);
private int scan_intcompare(unsigned int *, unsigned int *);
private int scan_folder(struct fld_rng *);
private void scan_init(unsigned int);
private FILE *scan_getfile(char **, char **);
private void exec_init(unsigned int);
private FILE *exec_getfile(char **, char **);
private void stdin_init(unsigned int);
private void set_fld_rng(struct fld_rng *, char *, char *);
/****************************************************************
*
* options and usages
*
*/
private void
usage(const char *progname) {
fprintf(stderr, "Usage: %s [options] [folder [range]]\n", progname);
}
private const char *
help_message[] = {
" -a Print all necessary fields when picking.",
" -b <mdir> Mail home.",
" -c <bodylen> Max body length to dump.",
" -d <field> A field to be extracted.",
" -e <command> An external command to fetch mailbox.",
" -m <options> The second argument for the external command.",
" -f <fields> Fields to display.",
" -h Display this help message.",
" -i <file> A file name to be read.",
" -l <length> Field max length to dump. (0 means no limit)",
" -n Don't use fstat().",
" -p <pattern> Pick pattern.",
" -s <src> Message source: '+folder range'.",
" -w Wait for stdio input to synchronize.",
" -v Display the version.",
" -x Use this suffix.",
"",
"<range> = N | [start]-[end] | last:N",
"<pattern> = 'key=val' | 'key!=val' | '!<pattern>' | '(<pattern>)' ",
" | '<pattern>&<pattern>' ",
" | '<pattern>|<pattern>' ",
"",
"Default is mdir = ~/Mail,",
"length = 5, folder = +inbox, range = 0, bodylen = 20",
"field = Subject:,Date:,From:,To:,Cc:,Content-Type:,Content-Transfer-Encoding:,X-Mew-UIDL:,Message-ID:,In-Reply-To:,References:,X-Mew-Ref:",
NULL
};
private void
help(const char *progname) {
const char **p = help_message;
fprintf(stderr, "Help: %s\n\n", progname);
fprintf(stderr, " Message scanner.\n\n");
usage(progname);
while (*p) fprintf(stderr, "%s\n", *p++);
}
private void
version(const char *progname) {
fprintf(stderr, "%s %s\n", progname, version_message);
}
/****************************************************************
*
* folder function
*
*/
private void
ch_home() {
char *home = getenv("HOME");
#ifdef HAVE_GETPWUID
if(home == NULL || home[0] == NUL) {
struct passwd *pw = getpwuid(getuid());
if(pw == NULL)
warn_exit("failed in getting home directory.");
home = pw->pw_dir;
}
#endif /* HAVE_GETPWUID */
if (home == NULL)
warn_exit("can't change directory to home.");
if (chdir(home) != 0)
warn_exit("can't change directory to %s.", home);
}
private void
ch_mail_home(char *dir) {
char *p = dir + 1, c;
if (dir == NULL) warn_exit("%s not exist.", dir);
c = *dir;
switch (c) {
case '~':
ch_home();
if (*p == NUL)
break;
if (*p++ != FILESEP)
warn_exit("can't change directory to %s.", dir);
if (*p == NUL)
break;
if (chdir(p) != 0)
warn_exit("can't change directory to %s.", dir);
break;
case FILESEP:
if (chdir(dir) != 0)
warn_exit("can't change directory to %s.", dir);
break;
default:
ch_home();
if (chdir(dir) != 0)
warn_exit("can't change directory to %s.", dir);
break;
}
}
private void
ch_folder(char *folder) {
char c = *folder, *p = folder + 1;
switch (c) {
case '+':
ch_mail_home(Mail_home);
if (*p == NUL)
break;
if (chdir(p) != 0) {
if( no_fld_flag != 0 )
default_fld_error_flag = 1;
else
warn_exit("can't change folder to %s.", folder);
}
break;
case '~':
ch_home();
if (*p == NUL)
break;
if (*p++ != FILESEP)
warn_exit("can't change folder to %s.", folder);
if (*p == NUL)
break;
if (chdir(p) != 0)
warn_exit("can't change folder to %s.", folder);
break;
default:
if (chdir(folder) != 0)
warn_exit("can't change folder to %s.", folder);
break;
}
}
/****************************************************************
*
* header sub functions
*
*/
/*
* p: a point to be scanned in the buffer.
* lim: the end of the buffer.
* hdr_only: if YES, scan the header only (not the body).
*
* return value: the beginning point of the next line. NULL pointer is
* returned if either the buffer or the header ends.
*/
private char *
nextline(char *p, char *lim, int hdr_only) {
if (p == lim)
return NULL;
if (*p == LF && hdr_only == YES)
return NULL;
while (p < lim) {
if (*p == LF)
break;
p++;
}
p++;
if (p < lim)
return p;
else {
*(lim - 1) = LF;
return NULL;
}
}
/*
* p: a point to be scanned in the buffer.
* lim: the end of the buffer.
* truncated_end: if the number of the lines of the field is over
* Field_len, it is truncated. And the end point is
* set to truncated_end.
* prev_beg: if not NULL pointer, the beginning point of the previous
* line of the last line is set to prev_beg. This is used
* for the References: field.
* hdr_only: if YES, scan the header only (not the body).
*
* return value: a point of the next field. NULL pointer is returned
* if either the buffer or the header ends.
*/
private char *
nextfield(char *p, char *lim, char **truncated_end, char **prev_beg, int hdr_only) {
int i = 0;
char *q;
*truncated_end = NULL;
do {
q = p;
p = nextline(p, lim, hdr_only);
if (p == NULL) break;
i++;
if (Field_len != 0 && i == Field_len)
*truncated_end = p;
} while (*p == SP || *p == TAB);
if (prev_beg != NULL) *prev_beg = q;
return p;
}
private char *
eoh(char *p, char *lim) {
while (p < lim) {
if (*p == LF && p + 1 < lim && *(p + 1) == LF)
return p + 1;
p++;
}
return lim - 1;
}
/****************************************************************
*
* print sub-functions
*
*/
private char **Scan_ctx_beg;
private char **Scan_ctx_end;
private char **Scan_ctx_fields;
private unsigned int *Scan_ctx_slen;
private int Scan_ctx_ref_idx = -1;
private unsigned int Scan_ctx_ptr_size;
private unsigned int Scan_ctx_fld_num;
private void
init_fields(char *fields) {
unsigned int i;
char *start, *p;
if (fields != NULL)
p = fields;
else
p = DEFAULT_FIELDS;
STRDUP(start, p);
for (i = 1, p = start; (p = strchr(p, ',')) != NULL; i++)
*p++ = NUL;
Scan_ctx_fld_num = i;
Scan_ctx_ptr_size = Scan_ctx_fld_num * sizeof(char *);
MALLOC(Scan_ctx_fields, Scan_ctx_ptr_size);
MALLOC(Scan_ctx_slen, Scan_ctx_fld_num * sizeof(int));
MALLOC(Scan_ctx_beg, Scan_ctx_ptr_size);
MALLOC(Scan_ctx_end, Scan_ctx_ptr_size);
i = 0;
p = start;
do {
Scan_ctx_fields[i] = p;
Scan_ctx_slen[i] = strlen(p);
if (strcmp(p, REFERENCES) == 0) Scan_ctx_ref_idx = i;
p = strchr(p, NUL);
p++;
i++;
} while (i < Scan_ctx_fld_num);
}
private void
print_field(char *buf, int lim, char *fname, char *fld) {
unsigned int i;
int match;
char *p, *q, *limp, *end, *prev, *body_beg = NULL;
memset(Scan_ctx_beg, 0, Scan_ctx_ptr_size);
memset(Scan_ctx_end, 0, Scan_ctx_ptr_size);
p = buf;
limp = p + lim;
for (;;) {
i = 0;
match = NO;
while (i < Scan_ctx_fld_num) {
if (strncasecmp(Scan_ctx_fields[i], p,
Scan_ctx_slen[i]) == 0) {
match = YES;
break;
}
i++;
}
if (match == YES) {
Scan_ctx_beg[i] = p;
p = nextfield(p, limp, &end, &prev, NO);
if (i == Scan_ctx_ref_idx) {
/* For the References: field, the bottom
part of its value is important. */
if (Scan_ctx_beg[i] == prev)
/* Skip the field name since
it will be printed below. */
Scan_ctx_beg[i] = Scan_ctx_beg[i]
+ Scan_ctx_slen[i];
else
/* The last line only */
Scan_ctx_beg[i] = prev;
if (p == NULL)
Scan_ctx_end[i] = limp;
else
Scan_ctx_end[i] = p;
} else {
if (end != NULL)
Scan_ctx_end[i] = end;
else if (p == NULL)
Scan_ctx_end[i] = limp;
else
Scan_ctx_end[i] = p;
}
} else
/* not a target field. let's skip this */
p = nextfield(p, limp, &end, NULL, NO);
if (p == NULL) break; /* for */
if (*p == LF) {
/* header ends */
p++;
body_beg = p;
break; /* for */
}
}
if (fld != NULL)
printf("Folder: %s\n", fld);
printf("Filename: %s\n", fname);
for (i = 0; i < Scan_ctx_fld_num; i++)
if (Scan_ctx_beg[i] != NULL) {
if (i == Scan_ctx_ref_idx) printf("%s", REFERENCES);
p = Scan_ctx_beg[i];
while (p < Scan_ctx_end[i])
putchar(*p++);
}
printf("\n");
if (body_beg != NULL) {
p = body_beg;
i = 0;
while (i < Body_len) {
q = p;
p = nextline(q, limp, NO);
if (p == NULL) {
while (q < limp) {
putchar(*q);
q++;
}
break;
}
i++;
if (*q == '.') printf(".");
while (q < p) {
putchar(*q);
q++;
}
}
}
printf(".\n");
}
/****************************************************************
*
* String search
*
*/
private char *Search_ctx_buf;
private int Search_ctx_lim;
private void
init_search(char *buf, int lim) {
Search_ctx_buf = buf;
Search_ctx_lim = lim;
}
private char *
mystrcasechr(char *s, int c) {
int lc = tolower(c);
int uc = toupper(c);
do {
if (*s == lc || *s == uc)
return s;
} while (*s++);
return NULL;
}
private char *
mystrcasestr(char *s1, char *s2) {
char *p;
unsigned int len = strlen(s2);
for (p = s1; (p = mystrcasechr(p, *s2)) != NULL; p++)
if (strncasecmp(p, s2, len) == 0)
return p;
return NULL;
}
public int
search_string(char *key, char *value, int case_sensitive) {
unsigned int len = strlen(key);
char *beg = NULL, *end = NULL;
char *p = Search_ctx_buf, *limp = p + Search_ctx_lim, tmp;
if (strcmp(HEAD, key) == 0) {
end = eoh(p, limp);
tmp = *end;
*end = NUL;
if (case_sensitive == YES) {
if (strstr(p, value) != NULL) {
*end = tmp;
return TRUE;
}
} else {
if (mystrcasestr(p, value) != NULL) {
*end = tmp;
return TRUE;
}
}
*end = tmp;
return FALSE;
}
for (;;) {
if (strncasecmp(key, p, len) == 0) {
beg = p + len;
p = nextfield(p, limp, &end, NULL, YES);
if (end != NULL)
end--; /* end == LF */
else if (p == NULL)
end = limp - 1; /* end == LF, see nextline */
else
end = p - 1; /* end == LF */
tmp = *end;
*end = NUL;
if (case_sensitive == YES) {
if (strstr(beg, value) != NULL) {
*end = tmp;
return TRUE;
}
} else {
if (mystrcasestr(beg, value) != NULL) {
*end = tmp;
return TRUE;
}
}
*end = tmp;
} else
p = nextfield(p, limp, &end, NULL, YES);
if (p == NULL)
break;
}
return FALSE;
}
/****************************************************************
*
* print functions
*
*/
private int
print_for_scan(FILE *fp, char *fname, char *fld) {
int lim = fread(Buf_header, sizeof(char), sizeof(Buf_header), fp);
print_field(Buf_header, lim, fname, fld);
return TRUE;
}
private int
print_for_sort(FILE *fp, char *fname, char *fld) {
int lim = fread(Buf_header, sizeof(char), sizeof(Buf_header), fp);
unsigned int len = strlen(Key_field);
char *beg = NULL, *end = NULL;
char *p = Buf_header, *limp = p + lim;
for (;;) {
if (strncasecmp(Key_field, p, len) == 0) {
beg = p + len;
p = nextfield(p, limp, &end, NULL, YES);
if (end != NULL)
end--;
else if (p == NULL) {
end = limp;
} else
end = p - 1;
break;
}
p = nextfield(p, limp, &end, NULL, YES);
if (p == NULL)
break;
}
if (beg == NULL)
printf("%s: \n", fname);
else {
printf("%s:", fname);
p = beg;
while (p < end)
putchar(*p++);
printf("\n");
}
return FALSE;
}
private int
print_for_pick(FILE *fp, char *fname, char *fld) {
int lim = fread(Buf_header, sizeof(char), sizeof(Buf_header), fp);
init_search(Buf_header, lim);
if (pattern_match() == TRUE) {
if (All_fields > 0) {
print_field(Buf_header, lim, fname, fld);
return TRUE;
} else {
printf("%s\n", fname);
return FALSE;
}
}
return FALSE;
}
/****************************************************************
*
* scan functions
*
*/
private unsigned int Scan_ctx_folders;
private unsigned int Scan_ctx_messages;
private unsigned int Scan_ctx_buf_size;
private unsigned int *Scan_ctx_buf;
private int
scan_numfile(char *s, unsigned int first, unsigned int last) {
int num = 0;
unsigned char c;
while ((c = *s) != NUL && isdigit(c)) {
num = num * 10 + c - '0';
s++;
}
if (num == 0)
return FALSE;
else if (*s == NUL)
;
else if (strncmp(s, Suffix, Suffix_len) == 0 && s[Suffix_len] == NUL)
;
else
return FALSE;
if (num >= first && (last == 0 || num <= last))
return TRUE;
else
return FALSE;
}
private int
scan_intcompare(unsigned int *i, unsigned int *j) {
if (*i > *j)
return 1;
if (*i < *j)
return -1;
return 0;
}
private int
scan_folder(struct fld_rng *fr) {
unsigned int num = 0;
unsigned int first = fr->first, last = fr->last;
DIR *dirp;
struct dirent *dp;
char *fname;
int i;
ch_folder(fr->folder);
if ((dirp = opendir(".")) == NULL)
warn_exit("can't open folder: %s.", fr->folder);
while ((dp = readdir(dirp)) != NULL) {
fname = dp->d_name;
if (scan_numfile(fname, first, last)) {
if (num == Scan_ctx_buf_size) {
Scan_ctx_buf_size += NUMBER_OF_FILE;
Scan_ctx_buf = (unsigned int *)realloc(Scan_ctx_buf, Scan_ctx_buf_size * sizeof(unsigned int));
}
*(Scan_ctx_buf + num) = atoi(fname);
num++;
}
}
closedir(dirp);
qsort(Scan_ctx_buf, num, sizeof(int),
(int (*)(const void *, const void *)) scan_intcompare);
if (fr->num > 0 && fr->num < num) {
for (i = 0; i < fr->num; i++)
Scan_ctx_buf[i] = Scan_ctx_buf[num - fr->num + i] ;
num = fr->num;
}
if (PrintNumOfMsg == YES && num > 0 && Scan_ctx_folders == 1) {
printf("NumOfMsg: %d\n", num);
fflush(stdout);
}
return num;
}
private void
scan_init(unsigned int folders) {
Scan_ctx_folders = folders;
Scan_ctx_messages = 0;
Scan_ctx_buf_size = NUMBER_OF_FILE;
MALLOC(Scan_ctx_buf, Scan_ctx_buf_size * sizeof(int));
}
private FILE *
scan_getfile(char **filename, char **foldername) {
static unsigned int i = 0, j = 0;
FILE *fp;
again:
if (i == Scan_ctx_messages) {
if (j < Scan_ctx_folders) {
Scan_ctx_messages = scan_folder(&Folders[j++]);
i = 0;
} else
return NULL;
}
*foldername = Folders[j - 1].folder;
while (i < Scan_ctx_messages) {
snprintf(Buf_filenm, sizeof(Buf_filenm), "%d", *(Scan_ctx_buf + i));
snprintf(Buf_filenm2, sizeof(Buf_filenm2), Suffix_fmt, "", *(Scan_ctx_buf + i));
fp = fopen(Buf_filenm2, FDREAD);
if (fp == NULL) fp = fopen(Buf_filenm, FDREAD);
if (fp != NULL) {
i++;
*filename = Buf_filenm;
return fp;
}
snprintf(Buf_filenm, sizeof(Buf_filenm), "0%d", *(Scan_ctx_buf + i));
snprintf(Buf_filenm2, sizeof(Buf_filenm2), Suffix_fmt, "0", *(Scan_ctx_buf + i));
fp = fopen(Buf_filenm2, FDREAD);
if (fp == NULL) fp = fopen(Buf_filenm, FDREAD);
if (fp != NULL) {
i++;
*filename = Buf_filenm;
return fp;
}
i++;
}
goto again;
}
/****************************************************************
*
* exec functions
*
*/
#ifdef HAVE_FORK
private void
exec_init(unsigned int num) { /* num == 0 */
int childpid;
int pipes[2];
if (strlen(Folders[0].folder) >= sizeof(Current_folder))
warn_exit("folder name is too long.");
STRCPY(Current_folder, Folders[0].folder);
ch_folder(Current_folder);
if (pipe(pipes) != 0) warn_exit("can't open pipe.");
childpid = FORK();
if (childpid < 0) warn_exit("can't fork.");
if (childpid == 0) { /* I'm the child. */
int i = 0;
int lim = MAX_ARGC - 2;
char *p;
close(WRITE);
dup(pipes[WRITE]);
close(pipes[READ]);
Argvec[i++] = Exec_cmd;
if (Exec_arg != NULL) {
p = Exec_arg;
Argvec[i++] = p;
while (i < lim) {
if ((p = strchr(p, SP)) == NULL) break;
*p++ = NUL;
Argvec[i++] = p;
}
}
Argvec[i++] = Current_folder;
Argvec[i++] = NULL;
execvp(Exec_cmd, Argvec);
warn_exit("'%s' command not found.", Exec_cmd);
}
/* I'm the parent. */
close(READ);
dup(pipes[READ]);
close(pipes[WRITE]);
}
#else /* HAVE_FORK */
private void
exec_init(unsigned int num) { /* num == 0 */
if (strlen(Folders[0].folder) >= sizeof(Current_folder))
warn_exit("folder name is too long.");
STRCPY(Current_folder, Folders[0].folder);
ch_folder(Current_folder);
}
#endif /* HAVE_FORK */
private FILE *
exec_getfile(char **filename, char **foldername) {
FILE *fp;
char *p, *q, *r;
int c;
*foldername = Current_folder;
while ((p = fgets(Buf_filenm, sizeof(Buf_filenm), stdin)) != NULL) {
if ((q = strchr(Buf_filenm, LF)) != NULL)
*q = NUL;
else {
Buf_filenm[MAX_FILE_NAME_LEN - 1] = NUL;
while ((c = getchar()) != LF && c != EOF) ;
}
if (STRCMP(p, CHDIR) == 0) {
no_fld_flag = 0;
default_fld_error_flag = 0;
p = p + strlen(CHDIR);
while (*p == SP || *p == TAB) p++;
if (strlen(p) >= sizeof(Current_folder))
warn_exit("folder name is too long.");
STRCPY(Current_folder, p);
ch_folder(Current_folder);
*foldername = Current_folder;
continue;
}
if(default_fld_error_flag != 0)
warn_exit("default folder is not exist.");
while (*p == SP || *p == TAB) p++;
if (isdigit((unsigned char)*p) == 0) continue;
*filename = p;
while (isdigit((unsigned char)*p)) p++;
if (STRCMP(p, Suffix)==0) {
r = p;
p = p + Suffix_len;
} else
r = NULL;
*p = NUL;
fp = fopen(*filename, FDREAD);
if (fp != NULL) {
if (r != NULL) *r = NUL;
return fp;
}
if (r != NULL) {
*r = NUL;
fp = fopen(*filename, FDREAD);
if (fp != NULL) return fp;
}
}
return NULL;
}
/****************************************************************
*
* stdin functions
*
*/
private void
stdin_init(unsigned int num) { /* num == 0 */
if (strcmp(Input_file, STDINFILE) != 0)
if (freopen(Input_file, FDREAD, stdin) == NULL)
warn_exit("can't open %s.", Input_file);
if (strlen(Folders[0].folder) >= sizeof(Current_folder))
warn_exit("folder name is too long.");
STRCPY(Current_folder, Folders[0].folder);
ch_folder(Current_folder);
}
/****************************************************************
*
* parser for folder and range
*
*/
private void
set_fld_rng(struct fld_rng *fr, char *arg1, char *arg2) {
char *folder = arg1;
char *range = NULL, *last = NULL;
char *p;
if (arg1 != NULL) range = strchr(arg1, SP);
if (range == NULL)
range = arg2;
else
*range++ = NUL;
if (range == NULL)
fr->first = fr->last = fr->num = 0;
else if (strncmp(range, "last:", 5) == 0) {
range += 5;
fr->first = fr->last = 0;
fr->num = atoi(range);
} else if ((last = strchr(range, '-')) == NULL) {
fr->first = fr->last = atoi(range);
fr->num = 0;
} else {
*last++ = NUL;
fr->first = atoi(range);
fr->last = atoi(last);
fr->num = 0;
}
if (folder == NULL) folder = INBOX;
STRDUP(fr->folder, folder);
while ((p = strchr(fr->folder, FILE_SEPARATOR)) != NULL) *p = SP;
}
/****************************************************************
*
* main
*
*/
int
main(int argc, char **argv) {
int optc, rest;
int eflag = 0, nflag = 1, sflag = 0, wflag = 0;
int wait;
unsigned int lim = 0;
struct stat st;
int (*func_print)(FILE *, char *, char *);
void (*func_init)(unsigned int);
FILE *(*func_getfile)(char **, char **);
FILE *fp, *sync = NULL;
char *filename, *foldername, *fields = NULL, *pattern = NULL;
char *progname = getprognm(argv[0]);
char *fld_rng;
warn_prog = progname;
STRDUP(Mail_home, MAILHOME);
while ((optc = Getopt(argc, argv, "ab:c:d:e:f:hi:l:m:np:s:vwx:")) != EOF) {
switch (optc) {
case 'a':
All_fields++;
break;
case 'b':
STRDUP(Mail_home, Optarg);
break;
case 'c':
Body_len = atoi(Optarg);
break;
case 'd':
STRDUP(Key_field, Optarg);
break;
case 'e':
STRDUP(Exec_cmd, Optarg);
eflag++;
break;
case 'f':
STRDUP(fields, Optarg);
PrintNumOfMsg = YES;
break;
case 'h':
help(progname);
exit(EXIT_SUCCESS);
break;
case 'i':
STRDUP(Input_file, Optarg);
break;
case 'l':
Field_len = atoi(Optarg);
break;
case 'm':
STRDUP(Exec_arg, Optarg);
break;
case 'n':
nflag = 0;
break;
case 'p':
STRDUP(pattern, Optarg);
break;
case 's':
if (lim >= MAX_FOLDER)
warn_exit("too many folders.");
STRDUP(fld_rng, Optarg);
set_fld_rng(&Folders[lim++], fld_rng, NULL);
sflag++;
break;
case 'v':
version(progname);
exit(EXIT_SUCCESS);
break;
case 'w':
wflag++;
break;
case 'x':
STRDUP(Suffix, Optarg);
break;
default:
usage(progname);
exit(EXIT_FAILURE);
}
}
rest = argc - Optind;
/* Suffix */
if (Suffix == NULL) STRDUP(Suffix, SUFFIX);
Suffix_len = strlen(Suffix);
MALLOC(Suffix_fmt, (Suffix_len + 10)); /* xxx */
snprintf(Suffix_fmt, Suffix_len + 10, "%%s%%d%s", Suffix);
if (sflag == 0)
switch (rest) { /* lim == 0 */
case 0:
set_fld_rng(&Folders[lim++], NULL, NULL);
no_fld_flag = 1;
break;
case 1:
set_fld_rng(&Folders[lim++], argv[Optind], NULL);
break;
case 2:
set_fld_rng(&Folders[lim++], argv[Optind], argv[Optind + 1]);
break;
default:
usage(progname);
exit(EXIT_FAILURE);
}
else if (rest != 0)
warn_exit("the 's' option can't co-exist with arguments.");
if (Input_file != NULL) {
func_init = stdin_init;
func_getfile = exec_getfile;
} else if (eflag > 0) {
func_init = exec_init;
func_getfile = exec_getfile;
} else {
func_init = scan_init;
func_getfile = scan_getfile;
}
init_fields(fields); /* lengthy sometime */
if (pattern != NULL) {
pattern_init(pattern);
func_print = print_for_pick;
PrintNumOfMsg = NO;
} else if (Key_field != NULL)
func_print = print_for_sort;
else
func_print = print_for_scan;
dup2(READ, SYNC);
sync = fdopen(SYNC, FDREAD);
(*func_init)(lim);
while ((fp = (*func_getfile)(&filename, &foldername)) != NULL) {
if (nflag != 0) {
fstat(fileno(fp), &st);
if (!(st.st_mode & S_IFREG)) {
fclose(fp);
continue;
}
}
wait = (*func_print)(fp, filename, foldername);
if (wait == TRUE && wflag > 0 && sync != NULL) {
fflush(stdout);
fgetc(sync);
}
fclose(fp);
}
exit(EXIT_SUCCESS);
}
/*
* Copyright (C) 2000-2005 Mew developing team.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the team nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* mewl.c ends here
*/
|