3 * Copyright (c) 2013 Francois Fleuret
4 * Written by Francois Fleuret <francois@fleuret.org>
6 * This file is part of mymail.
8 * mymail is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 3 as
10 * published by the Free Software Foundation.
12 * mymail is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with mymail. If not, see <http://www.gnu.org/licenses/>.
24 This command is a dumb mail indexer. It can either (1) scan
25 directories containing mbox files, and create a db file containing
26 for each mail a list of fields computed from the header, or (2)
27 read such a db file and get all the mails matching regexp-defined
28 conditions on the fields.
30 It is low-tech, simple, light and fast.
48 #define MYMAIL_DB_MAGIC_TOKEN "mymail_index_file"
49 #define VERSION "0.9.1"
51 #define MAX_NB_SEARCH_CONDITIONS 10
53 #define BUFFER_SIZE 65536
54 #define TOKEN_BUFFER_SIZE 1024
56 regex_t leading_from_line_regexp;
58 /* Global variables! */
62 char *default_search_field = "p";
64 /********************************************************************/
79 static char *field_names[] = {
91 /********************************************************************/
93 struct search_condition {
97 time_t interval_start, interval_stop;
100 /********************************************************************/
102 struct parsable_field {
108 static struct parsable_field fields_to_parse[] = {
112 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
117 "^\\([Ff][Rr][Oo][Mm]:\\|[Rr][Ee][Pp][Ll][Yy]-[Tt][Oo]:\\|[Ss][Ee][Nn][Dd][Ee][Rr]:\\)",
118 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
123 "^\\([Tt][Oo]\\|[Cc][Cc]\\|[Bb][Cc][Cc]\\): ",
124 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
129 "^[Ss][Uu][Bb][Jj][Ee][Cc][Tt]: ",
130 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
135 "^[Dd][Aa][Tt][Ee]: ",
136 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
141 /********************************************************************/
143 int xor(int a, int b) {
144 return (a && !b) || (!a && b);
147 char *parse_token(char *token_buffer, size_t token_buffer_size,
148 char separator, char *string) {
149 char *u = token_buffer;
150 while(u < token_buffer + token_buffer_size - 1 && *string &&
151 *string != separator) {
152 *(u++) = *(string++);
154 while(*string == separator) string++;
159 void remove_eof(char *c) {
160 while(*c && *c != '\n' && *c != '\r') c++;
164 /********************************************************************/
166 /* malloc with error checking. */
168 void *safe_malloc(size_t n) {
172 "mymail: cannot allocate memory: %s\n", strerror(errno));
178 /*********************************************************************/
180 void print_version(FILE *out) {
181 fprintf(out, "mymail version %s (%s)\n", VERSION, UNAME);
184 void print_usage(FILE *out) {
186 fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
188 fprintf(out, "Usage: mymail [options] [<mbox dir1> [<mbox dir2> ...]|<db file1> [<db file2> ...]]\n");
190 fprintf(out, " -h, --help\n");
191 fprintf(out, " show this help\n");
192 fprintf(out, " -v, --version\n");
193 fprintf(out, " print the version number\n");
194 fprintf(out, " -q, --quiet\n");
195 fprintf(out, " do not print information during search\n");
196 fprintf(out, " -p <db filename pattern>, --db-pattern <db filename pattern>\n");
197 fprintf(out, " set the db filename pattern for recursive search\n");
198 fprintf(out, " -r <db root path>, --db-root <db root path>\n");
199 fprintf(out, " set the db root path for recursive search\n");
200 fprintf(out, " -l <db filename list>, --db-list <db filename list>\n");
201 fprintf(out, " set the semicolon-separated list of db files for search\n");
202 fprintf(out, " -s <search pattern>, --search <search pattern>\n");
203 fprintf(out, " search for matching mails in the db file\n");
204 fprintf(out, " -d <db filename>, --db-file <db filename>\n");
205 fprintf(out, " set the db filename for indexing\n");
206 fprintf(out, " -i, --index\n");
207 fprintf(out, " index mails\n");
208 fprintf(out, " -o <output filename>, --output <output filename>\n");
209 fprintf(out, " set the result file, use stdout if unset\n");
212 /*********************************************************************/
214 time_t time_for_past_day(int day) {
220 delta_day = (7 + tm->tm_wday - day) % 7 + 1;
221 return t - delta_day * 3600 * 24 + tm->tm_sec + 60 * tm->tm_min + 3600 * tm->tm_hour;
224 /*********************************************************************/
226 int ignore_entry(const char *name) {
228 /* strcmp(name, ".") == 0 || */
229 /* strcmp(name, "..") == 0 || */
230 (name[0] == '.' && name[1] != '/');
233 int is_a_leading_from_line(char *s) {
234 return strncmp(s, "From ", 5) == 0 &&
235 regexec(&leading_from_line_regexp, s, 0, 0, 0) == 0;
238 int mbox_line_match_search(struct search_condition *condition,
239 int mbox_id, char *mbox_value) {
241 if(condition->field_id == ID_INTERVAL) {
242 if(mbox_id == ID_LEADING_LINE) {
248 while(*c && *c != ' ') c++; while(*c && *c == ' ') c++;
249 strptime(c, "%a %b %e %k:%M:%S %Y", &tm);
252 return (t >= condition->interval_start &&
253 (condition->interval_stop == 0 ||
254 t <= condition->interval_stop));
262 (condition->field_id == mbox_id)
266 (condition->field_id == ID_PARTICIPANT && (mbox_id == ID_LEADING_LINE ||
267 mbox_id == ID_FROM ||
271 (condition->field_id == ID_FROM && mbox_id == ID_LEADING_LINE)
275 regexec(&condition->regexp, mbox_value, 0, 0, 0) == 0;
279 void update_body_hits(char *mail_filename, int position_in_mail,
280 int nb_search_conditions, struct search_condition *search_conditions,
281 int nb_body_conditions,
285 int last_mbox_line_was_empty;
286 char raw_mbox_line[BUFFER_SIZE];
292 mail_file = fopen(mail_filename, "r");
296 "mymail: Cannot open mbox '%s' for body scan.\n",
301 fseek(mail_file, position_in_mail, SEEK_SET);
303 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
304 while(nb_body_hits < nb_body_conditions) {
305 last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
307 if(last_mbox_line_was_empty) { header = 0; }
310 for(n = 0; n < nb_search_conditions; n++) {
311 if(search_conditions[n].field_id == ID_BODY && !hits[n]) {
313 (regexec(&search_conditions[n].regexp, raw_mbox_line, 0, 0, 0) == 0);
321 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
322 (last_mbox_line_was_empty &&
323 is_a_leading_from_line(raw_mbox_line)))
331 void search_in_db(const char *db_filename,
332 int nb_search_conditions,
333 struct search_condition *search_conditions,
336 int hits[MAX_NB_SEARCH_CONDITIONS];
337 char raw_db_line[BUFFER_SIZE];
338 char raw_mbox_line[BUFFER_SIZE];
339 char current_mail_filename[PATH_MAX + 1];
340 unsigned long int current_position_in_mail;
341 char mbox_name[TOKEN_BUFFER_SIZE], *mbox_value;
343 int already_written, m, n;
344 int last_mbox_line_was_empty;
345 int nb_body_conditions, nb_fulfilled_body_conditions;
349 printf("Searching in '%s' ... ", db_filename);
353 db_file = fopen(db_filename, "r");
357 "mymail: Cannot open \"%s\" for reading: %s\n",
363 if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
364 if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
366 "mymail: Header line in '%s' does not match the mymail db format.\n",
372 "mymail: Cannot read the header line in '%s'.\n",
377 current_position_in_mail = 0;
380 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
382 nb_body_conditions = 0;
383 for(n = 0; n < nb_search_conditions; n++) {
384 if(search_conditions[n].field_id == ID_BODY) {
385 nb_body_conditions++;
389 strcpy(current_mail_filename, "");
391 while(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
392 mbox_value = parse_token(mbox_name, TOKEN_BUFFER_SIZE, ' ', raw_db_line);
394 if(strcmp("mail", mbox_name) == 0) {
395 char position_in_file_string[TOKEN_BUFFER_SIZE];
397 if(current_mail_filename[0]) {
399 /* We first check all conditions but the body ones */
401 for(n = 0; n < nb_search_conditions &&
402 ((search_conditions[n].field_id == ID_BODY) ||
403 xor(hits[n], search_conditions[n].negation)); n++);
405 if(n == nb_search_conditions) {
407 /* all conditions but the body ones are fine, check the body
410 if(nb_body_conditions > 0) {
411 update_body_hits(current_mail_filename, current_position_in_mail,
412 nb_search_conditions, search_conditions,
417 nb_fulfilled_body_conditions = 0;
419 for(n = 0; n < nb_search_conditions; n++) {
420 if(search_conditions[n].field_id == ID_BODY &&
421 xor(hits[n], search_conditions[n].negation)) {
422 nb_fulfilled_body_conditions++;
426 if(nb_body_conditions == nb_fulfilled_body_conditions) {
429 mail_file = fopen(current_mail_filename, "r");
433 "mymail: Cannot open mbox '%s' for mail extraction.\n",
434 current_mail_filename);
438 fseek(mail_file, current_position_in_mail, SEEK_SET);
440 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
441 last_mbox_line_was_empty = 0;
442 fprintf(output_file, "%s", raw_mbox_line);
444 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
445 (last_mbox_line_was_empty &&
446 is_a_leading_from_line(raw_mbox_line))
449 last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
450 fprintf(output_file, "%s", raw_mbox_line);
459 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
461 mbox_value = parse_token(position_in_file_string, TOKEN_BUFFER_SIZE, ' ', mbox_value);
462 mbox_value = parse_token(current_mail_filename, TOKEN_BUFFER_SIZE, ' ', mbox_value);
463 current_position_in_mail = atol(position_in_file_string);
464 remove_eof(current_mail_filename);
470 for(m = 0; (m < MAX_ID) && mbox_id == -1; m++) {
471 if(strncmp(field_names[m], mbox_name, strlen(mbox_name)) == 0) {
475 for(n = 0; n < nb_search_conditions; n++) {
476 hits[n] |= mbox_line_match_search(&search_conditions[n],
477 mbox_id, mbox_value);
490 void recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp,
491 int nb_search_conditions,
492 struct search_condition *search_conditions,
495 struct dirent *dir_e;
497 char subname[PATH_MAX + 1];
499 if(lstat(entry_name, &sb) != 0) {
501 "mymail: Cannot stat \"%s\": %s\n",
507 dir = opendir(entry_name);
510 while((dir_e = readdir(dir))) {
511 if(!ignore_entry(dir_e->d_name)) {
512 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
513 recursive_search_in_db(subname, db_filename_regexp,
514 nb_search_conditions, search_conditions,
522 const char *s = entry_name, *filename = entry_name;
523 while(*s) { if(*s == '/') { filename = s+1; } s++; }
525 if(regexec(db_filename_regexp, filename, 0, 0, 0) == 0) {
526 search_in_db(entry_name, nb_search_conditions, search_conditions, output_file);
531 /*********************************************************************/
533 void index_one_mbox_line(int nb_fields_to_parse, struct parsable_field *fields_to_parse,
534 char *raw_mbox_line, FILE *db_file) {
537 for(f = 0; f < nb_fields_to_parse; f++) {
538 if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
539 fprintf(db_file, "%s %s\n",
540 field_names[fields_to_parse[f].id],
541 raw_mbox_line + matches.rm_eo);
546 void index_mbox(const char *mbox_filename,
547 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
549 char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
550 char *end_of_full_line;
552 int in_header, new_header, last_mbox_line_was_empty;
553 unsigned long int position_in_file;
555 file = fopen(mbox_filename, "r");
558 fprintf(stderr, "mymail: Cannot open '%s'.\n", mbox_filename);
559 if(paranoid) { exit(EXIT_FAILURE); }
566 position_in_file = 0;
567 end_of_full_line = 0;
569 last_mbox_line_was_empty = 1;
571 while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
572 if(last_mbox_line_was_empty &&
573 is_a_leading_from_line(raw_mbox_line)) {
576 "Got a ^\"From \" in the header in %s:%lu.\n",
577 mbox_filename, position_in_file);
578 fprintf(stderr, "%s", raw_mbox_line);
579 if(paranoid) { exit(EXIT_FAILURE); }
583 } else if(raw_mbox_line[0] == '\n') {
584 if(in_header) { in_header = 0; }
587 last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
591 fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
595 if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
596 char *start = raw_mbox_line;
597 while(*start == ' ' || *start == '\t') start++;
598 *(end_of_full_line++) = ' ';
599 strcpy(end_of_full_line, start);
600 while(*end_of_full_line && *end_of_full_line != '\n') {
603 *end_of_full_line = '\0';
608 if(!((raw_mbox_line[0] >= 'a' && raw_mbox_line[0] <= 'z') ||
609 (raw_mbox_line[0] >= 'A' && raw_mbox_line[0] <= 'Z'))) {
611 "Header line syntax error %s:%lu.\n",
612 mbox_filename, position_in_file);
613 fprintf(stderr, "%s", raw_mbox_line);
618 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
621 end_of_full_line = full_line;
622 strcpy(end_of_full_line, raw_mbox_line);
623 while(*end_of_full_line && *end_of_full_line != '\n') {
626 *end_of_full_line = '\0';
631 position_in_file += strlen(raw_mbox_line);
637 void recursive_index_mbox(FILE *db_file,
638 const char *entry_name,
639 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
641 struct dirent *dir_e;
643 char subname[PATH_MAX + 1];
645 if(lstat(entry_name, &sb) != 0) {
647 "mymail: Cannot stat \"%s\": %s\n",
653 dir = opendir(entry_name);
656 while((dir_e = readdir(dir))) {
657 if(!ignore_entry(dir_e->d_name)) {
658 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
659 recursive_index_mbox(db_file, subname, nb_fields_to_parse, fields_to_parse);
664 index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
668 /*********************************************************************/
670 /* For long options that have no equivalent short option, use a
671 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
673 OPT_BASH_MODE = CHAR_MAX + 1
676 static struct option long_options[] = {
677 { "help", no_argument, 0, 'h' },
678 { "version", no_argument, 0, 'v' },
679 { "quiet", no_argument, 0, 'q' },
680 { "db-file", 1, 0, 'd' },
681 { "db-pattern", 1, 0, 'p' },
682 { "db-root", 1, 0, 'r' },
683 { "db-list", 1, 0, 'l' },
684 { "search", 1, 0, 's' },
685 { "index", 0, 0, 'i' },
686 { "output", 1, 0, 'o' },
690 /*********************************************************************/
692 void init_condition(struct search_condition *condition, char *full_string) {
693 char full_search_field[TOKEN_BUFFER_SIZE], *search_field;
697 string = parse_token(full_search_field, TOKEN_BUFFER_SIZE, ' ', full_string);
698 search_field = full_search_field;
700 if(search_field[0] == '!') {
702 condition->negation = 1;
704 condition->negation = 0;
709 if(strcmp(search_field, "8h") == 0) {
710 condition->field_id = ID_INTERVAL;
711 condition->interval_start = time(0) - 3600 * 8;
712 condition->interval_stop = 0;
715 else if(strcmp(search_field, "week") == 0) {
716 condition->field_id = ID_INTERVAL;
717 condition->interval_start = time(0) - 3600 * 24 * 7;
718 condition->interval_stop = 0;
721 /* Today and yesterday */
723 else if(strcmp(search_field, "24h") == 0 ||
724 strcmp(search_field, "today") == 0) {
725 condition->field_id = ID_INTERVAL;
726 condition->interval_start = time(0) - 3600 * 24;
727 condition->interval_stop = 0;
730 else if(strcmp(search_field, "yesterday") == 0) {
731 condition->field_id = ID_INTERVAL;
732 condition->interval_start = time(0) - 2 * 3600 * 24;
733 condition->interval_stop = condition->interval_start + 3600 * 24;
738 else if(strcmp(search_field, "monday") == 0) {
739 condition->field_id = ID_INTERVAL;
740 condition->interval_start = time_for_past_day(1);
741 condition->interval_stop = condition->interval_start + 3600 * 24;
744 else if(strcmp(search_field, "tuesday") == 0) {
745 condition->field_id = ID_INTERVAL;
746 condition->interval_start = time_for_past_day(2);
747 condition->interval_stop = condition->interval_start + 3600 * 24;
750 else if(strcmp(search_field, "wednesday") == 0) {
751 condition->field_id = ID_INTERVAL;
752 condition->interval_start = time_for_past_day(3);
753 condition->interval_stop = condition->interval_start + 3600 * 24;
756 else if(strcmp(search_field, "thursday") == 0) {
757 condition->field_id = ID_INTERVAL;
758 condition->interval_start = time_for_past_day(4);
759 condition->interval_stop = condition->interval_start + 3600 * 24;
762 else if(strcmp(search_field, "friday") == 0) {
763 condition->field_id = ID_INTERVAL;
764 condition->interval_start = time_for_past_day(5);
765 condition->interval_stop = condition->interval_start + 3600 * 24;
768 else if(strcmp(search_field, "saturday") == 0) {
769 condition->field_id = ID_INTERVAL;
770 condition->interval_start = time_for_past_day(6);
771 condition->interval_stop = condition->interval_start + 3600 * 24;
774 else if(strcmp(search_field, "sunday") == 0) {
775 condition->field_id = ID_INTERVAL;
776 condition->interval_start = time_for_past_day(7);
777 condition->interval_stop = condition->interval_start + 3600 * 24;
781 condition->field_id = -1;
783 for(m = 0; (m < MAX_ID) && condition->field_id == -1; m++) {
784 if(strncmp(field_names[m], search_field, strlen(search_field)) == 0) {
785 condition->field_id = m;
789 if(condition->field_id == -1) {
790 if(default_search_field) {
791 for(m = 0; (m < MAX_ID) && condition->field_id == -1; m++) {
792 if(strncmp(field_names[m],
793 default_search_field, strlen(default_search_field)) == 0) {
794 condition->field_id = m;
797 string = full_string;
801 if(condition->field_id == -1) {
803 "mymail: Syntax error in field name \"%s\".\n",
808 if(regcomp(&condition->regexp,
812 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
814 field_names[condition->field_id]);
820 void free_condition(struct search_condition *condition) {
821 if(condition->field_id != ID_INTERVAL) {
822 regfree(&condition->regexp);
826 /*********************************************************************/
827 /*********************************************************************/
828 /*********************************************************************/
830 int main(int argc, char **argv) {
832 char *db_filename_regexp_string;
834 char *db_filename_list;
835 char output_filename[PATH_MAX + 1];
838 int error = 0, show_help = 0;
839 const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
842 int nb_search_conditions;
844 struct search_condition search_conditions[MAX_NB_SEARCH_CONDITIONS];
846 if(regcomp(&leading_from_line_regexp,
847 "^From [^ ]* \\(Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\|Sun\\) \\(Jan\\|Feb\\|Mar\\|Apr\\|May\\|Jun\\|Jul\\|Aug\\|Sep\\|Oct\\|Nov\\|Dec\\) [ 123][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9] [0-9][0-9][0-9][0-9]\n$",
850 "mymail: Cannot compile leading \"from\" line regexp. That is strange.\n");
857 db_filename_regexp_string = 0;
859 db_filename_list = 0;
862 setlocale(LC_ALL, "");
864 nb_search_conditions = 0;
866 while ((c = getopt_long(argc, argv, "hvqip:s:d:r:l:o:",
867 long_options, NULL)) != -1) {
876 print_version(stdout);
888 db_filename = strdup(optarg);
892 db_filename_regexp_string = strdup(optarg);
896 strncpy(output_filename, optarg, PATH_MAX);
900 db_root_path = strdup(optarg);
904 db_filename_list = strdup(optarg);
908 if(nb_search_conditions == MAX_NB_SEARCH_CONDITIONS) {
909 fprintf(stderr, "mymail: Too many search patterns.\n");
912 init_condition(&search_conditions[nb_search_conditions], optarg);
913 nb_search_conditions++;
923 char *default_db_filename = getenv("MYMAIL_DB_FILE");
925 if(!default_db_filename) {
926 default_db_filename = "mymail.db";
929 db_filename = strdup(default_db_filename);
932 if(!db_filename_regexp_string) {
933 char *default_db_filename_regexp_string = getenv("MYMAIL_DB_PATTERN");
935 if(!default_db_filename_regexp_string) {
936 default_db_filename_regexp_string = "^mymail.db$";
939 db_filename_regexp_string = strdup(default_db_filename_regexp_string);
943 char *default_db_root_path = getenv("MYMAIL_DB_ROOT");
945 if(default_db_root_path) {
946 db_root_path = strdup(default_db_root_path);
950 if(!db_filename_list) {
951 char *default_db_filename_list = getenv("MYMAIL_DB_LIST");
953 if(default_db_filename_list) {
954 db_filename_list = strdup(default_db_filename_list);
958 if(output_filename[0]) {
959 output_file = fopen(output_filename, "w");
963 "mymail: Cannot open result file \"%s\" for writing: %s\n",
969 output_file = stdout;
986 db_file = fopen(db_filename, "w");
990 "mymail: Cannot open \"%s\" for writing: %s\n",
996 for(f = 0; f < nb_fields_to_parse; f++) {
997 if(regcomp(&fields_to_parse[f].regexp,
998 fields_to_parse[f].regexp_string,
1001 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
1002 fields_to_parse[f].regexp_string,
1003 field_names[fields_to_parse[f].id]);
1008 fprintf(db_file, "%s version_%s raw\n", MYMAIL_DB_MAGIC_TOKEN, VERSION);
1010 while(optind < argc) {
1011 recursive_index_mbox(db_file,
1013 nb_fields_to_parse, fields_to_parse);
1020 for(f = 0; f < nb_fields_to_parse; f++) {
1021 regfree(&fields_to_parse[f].regexp);
1027 if(nb_search_conditions > 0) {
1029 /* Recursive search if db_root_path is set */
1032 regex_t db_filename_regexp;
1033 if(regcomp(&db_filename_regexp,
1034 db_filename_regexp_string,
1037 "mymail: Syntax error in regexp \"%s\".\n",
1038 db_filename_regexp_string);
1042 recursive_search_in_db(db_root_path, &db_filename_regexp,
1043 nb_search_conditions, search_conditions,
1046 regfree(&db_filename_regexp);
1049 /* Search in all db files listed in db_filename_list */
1051 if(db_filename_list) {
1052 char db_filename[PATH_MAX + 1];
1055 s = db_filename_list;
1058 s = parse_token(db_filename, PATH_MAX + 1, ';', s);
1060 if(db_filename[0]) {
1061 search_in_db(db_filename, nb_search_conditions, search_conditions, output_file);
1066 /* Search in all db files listed in the command arguments */
1068 while(optind < argc) {
1069 search_in_db(argv[optind], nb_search_conditions, search_conditions, output_file);
1075 for(n = 0; n < nb_search_conditions; n++) {
1076 free_condition(&search_conditions[n]);
1079 if(output_file != stdout) {
1080 fflush(output_file);
1081 fclose(output_file);
1085 free(db_filename_regexp_string);
1087 free(db_filename_list);
1089 regfree(&leading_from_line_regexp);