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;
64 /********************************************************************/
79 static char *field_names[] = {
91 /********************************************************************/
93 struct search_condition {
97 time_t interval_start, interval_stop;
100 /********************************************************************/
102 struct parsable_field {
109 static struct parsable_field fields_to_parse[] = {
114 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
120 "^\\(from:\\|reply-to:\\|sender:\\|return-path:\\)",
121 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
127 "^\\(to\\|cc\\|bcc\\): ",
128 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
135 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
142 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
147 /********************************************************************/
149 int xor(int a, int b) {
150 return (a && !b) || (!a && b);
153 char *parse_token(char *token_buffer, size_t token_buffer_size,
154 char separator, char *string) {
155 char *u = token_buffer;
156 while(u < token_buffer + token_buffer_size - 1 && *string &&
157 *string != separator) {
158 *(u++) = *(string++);
160 while(*string == separator) string++;
165 void remove_eof(char *c) {
166 while(*c && *c != '\n' && *c != '\r') c++;
170 /********************************************************************/
172 /* malloc with error checking. */
174 void *safe_malloc(size_t n) {
178 "mymail: cannot allocate memory: %s\n", strerror(errno));
184 /*********************************************************************/
186 void print_version(FILE *out) {
187 fprintf(out, "mymail version %s (%s)\n", VERSION, UNAME);
190 void print_usage(FILE *out) {
192 fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
194 fprintf(out, "Usage: mymail [options] [<mbox dir1> [<mbox dir2> ...]|<db file1> [<db file2> ...]]\n");
196 fprintf(out, " -h, --help\n");
197 fprintf(out, " show this help\n");
198 fprintf(out, " -v, --version\n");
199 fprintf(out, " print the version number\n");
200 fprintf(out, " -q, --quiet\n");
201 fprintf(out, " do not print information during search\n");
202 fprintf(out, " -p <db filename pattern>, --db-pattern <db filename pattern>\n");
203 fprintf(out, " set the db filename pattern for recursive search\n");
204 fprintf(out, " -r <db root path>, --db-root <db root path>\n");
205 fprintf(out, " set the db root path for recursive search\n");
206 fprintf(out, " -l <db filename list>, --db-list <db filename list>\n");
207 fprintf(out, " set the semicolon-separated list of db files for search\n");
208 fprintf(out, " -s <search pattern>, --search <search pattern>\n");
209 fprintf(out, " search for matching mails in the db file\n");
210 fprintf(out, " -d <db filename>, --db-file <db filename>\n");
211 fprintf(out, " set the db filename for indexing\n");
212 fprintf(out, " -i, --index\n");
213 fprintf(out, " index mails\n");
214 fprintf(out, " -o <output filename>, --output <output filename>\n");
215 fprintf(out, " set the result file, use stdout if unset\n");
216 fprintf(out, " -a <search field>, --default-search <search field>\n");
217 fprintf(out, " set the default search field\n");
220 /*********************************************************************/
222 time_t time_for_past_day(int day) {
228 delta_day = (7 + tm->tm_wday - day) % 7 + 1;
229 return t - delta_day * 3600 * 24 + tm->tm_sec + 60 * tm->tm_min + 3600 * tm->tm_hour;
232 /*********************************************************************/
234 int ignore_entry(const char *name) {
236 /* strcmp(name, ".") == 0 || */
237 /* strcmp(name, "..") == 0 || */
238 (name[0] == '.' && name[1] != '/');
241 int is_a_leading_from_line(int last_mbox_line_was_empty, char *mbox_line) {
246 The mbox man page in qmail documentation states:
248 > The reader should not attempt to take advantage of the fact
249 > that every From_ line (past the beginning of the file) is
250 > preceded by a blank line.
254 /* last_mbox_line_was_empty && */
255 strncmp(mbox_line, "From ", 5) == 0 &&
256 regexec(&leading_from_line_regexp, mbox_line, 0, 0, 0) == 0;
259 int mbox_line_match_search(struct search_condition *condition,
260 int mbox_id, char *mbox_value) {
262 if(condition->field_id == ID_INTERVAL) {
263 if(mbox_id == ID_LEADING_LINE) {
269 while(*c && *c != ' ') c++; while(*c && *c == ' ') c++;
270 strptime(c, "%a %b %e %k:%M:%S %Y", &tm);
273 return (t >= condition->interval_start &&
274 (condition->interval_stop == 0 ||
275 t <= condition->interval_stop));
283 (condition->field_id == mbox_id)
287 (condition->field_id == ID_PARTICIPANT && (mbox_id == ID_LEADING_LINE ||
288 mbox_id == ID_FROM ||
292 (condition->field_id == ID_FROM && mbox_id == ID_LEADING_LINE)
296 regexec(&condition->regexp, mbox_value, 0, 0, 0) == 0;
300 void update_body_hits(char *mail_filename, int position_in_mail,
301 int nb_search_conditions, struct search_condition *search_conditions,
302 int nb_body_conditions,
306 int last_mbox_line_was_empty;
307 char raw_mbox_line[BUFFER_SIZE];
313 mail_file = fopen(mail_filename, "r");
317 "mymail: Cannot open mbox '%s' for body scan.\n",
322 fseek(mail_file, position_in_mail, SEEK_SET);
324 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
325 while(nb_body_hits < nb_body_conditions) {
326 last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
328 if(last_mbox_line_was_empty) { header = 0; }
331 for(n = 0; n < nb_search_conditions; n++) {
332 if(search_conditions[n].field_id == ID_BODY && !hits[n]) {
334 (regexec(&search_conditions[n].regexp, raw_mbox_line, 0, 0, 0) == 0);
342 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
343 (is_a_leading_from_line(last_mbox_line_was_empty, raw_mbox_line)))
351 void search_in_db(const char *db_filename,
352 int nb_search_conditions,
353 struct search_condition *search_conditions,
356 int hits[MAX_NB_SEARCH_CONDITIONS];
357 char raw_db_line[BUFFER_SIZE];
358 char raw_mbox_line[BUFFER_SIZE];
359 char current_mail_filename[PATH_MAX + 1];
360 unsigned long int current_position_in_mail;
361 char mbox_name[TOKEN_BUFFER_SIZE], *mbox_value;
363 int already_written, m, n;
364 int last_mbox_line_was_empty;
365 int nb_body_conditions, nb_fulfilled_body_conditions;
369 printf("Searching in '%s' ... ", db_filename);
373 db_file = fopen(db_filename, "r");
377 "mymail: Cannot open \"%s\" for reading: %s\n",
383 if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
384 if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
386 "mymail: Header line in '%s' does not match the mymail db format.\n",
392 "mymail: Cannot read the header line in '%s'.\n",
397 current_position_in_mail = 0;
400 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
402 nb_body_conditions = 0;
403 for(n = 0; n < nb_search_conditions; n++) {
404 if(search_conditions[n].field_id == ID_BODY) {
405 nb_body_conditions++;
409 strcpy(current_mail_filename, "");
411 while(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
412 mbox_value = parse_token(mbox_name, TOKEN_BUFFER_SIZE, ' ', raw_db_line);
414 if(strcmp("mail", mbox_name) == 0) {
415 char position_in_file_string[TOKEN_BUFFER_SIZE];
417 if(current_mail_filename[0]) {
419 /* We first check all conditions but the body ones */
421 for(n = 0; n < nb_search_conditions &&
422 ((search_conditions[n].field_id == ID_BODY) ||
423 xor(hits[n], search_conditions[n].negation)); n++);
425 if(n == nb_search_conditions) {
427 /* all conditions but the body ones are fine, check the body
430 if(nb_body_conditions > 0) {
431 update_body_hits(current_mail_filename, current_position_in_mail,
432 nb_search_conditions, search_conditions,
437 nb_fulfilled_body_conditions = 0;
439 for(n = 0; n < nb_search_conditions; n++) {
440 if(search_conditions[n].field_id == ID_BODY &&
441 xor(hits[n], search_conditions[n].negation)) {
442 nb_fulfilled_body_conditions++;
446 if(nb_body_conditions == nb_fulfilled_body_conditions) {
449 mail_file = fopen(current_mail_filename, "r");
453 "mymail: Cannot open mbox '%s' for mail extraction.\n",
454 current_mail_filename);
458 fseek(mail_file, current_position_in_mail, SEEK_SET);
460 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
461 last_mbox_line_was_empty = 0;
462 fprintf(output_file, "%s", raw_mbox_line);
464 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
465 (is_a_leading_from_line(last_mbox_line_was_empty, raw_mbox_line))
468 last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
469 fprintf(output_file, "%s", raw_mbox_line);
478 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
480 mbox_value = parse_token(position_in_file_string, TOKEN_BUFFER_SIZE, ' ', mbox_value);
481 mbox_value = parse_token(current_mail_filename, TOKEN_BUFFER_SIZE, ' ', mbox_value);
482 current_position_in_mail = atol(position_in_file_string);
483 remove_eof(current_mail_filename);
489 for(m = 0; (m < MAX_ID) && mbox_id == -1; m++) {
490 if(strncmp(field_names[m], mbox_name, strlen(mbox_name)) == 0) {
494 for(n = 0; n < nb_search_conditions; n++) {
495 hits[n] |= mbox_line_match_search(&search_conditions[n],
496 mbox_id, mbox_value);
509 void recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp,
510 int nb_search_conditions,
511 struct search_condition *search_conditions,
514 struct dirent *dir_e;
516 char subname[PATH_MAX + 1];
518 if(lstat(entry_name, &sb) != 0) {
520 "mymail: Cannot stat \"%s\": %s\n",
526 dir = opendir(entry_name);
529 while((dir_e = readdir(dir))) {
530 if(!ignore_entry(dir_e->d_name)) {
531 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
532 recursive_search_in_db(subname, db_filename_regexp,
533 nb_search_conditions, search_conditions,
541 const char *s = entry_name, *filename = entry_name;
542 while(*s) { if(*s == '/') { filename = s+1; } s++; }
544 if(regexec(db_filename_regexp, filename, 0, 0, 0) == 0) {
545 search_in_db(entry_name, nb_search_conditions, search_conditions, output_file);
550 /*********************************************************************/
552 void index_one_mbox_line(int nb_fields_to_parse, struct parsable_field *fields_to_parse,
553 char *raw_mbox_line, FILE *db_file) {
556 for(f = 0; f < nb_fields_to_parse; f++) {
557 if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
558 fprintf(db_file, "%s %s\n",
559 field_names[fields_to_parse[f].id],
560 raw_mbox_line + matches.rm_eo);
565 void index_mbox(const char *mbox_filename,
566 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
568 char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
569 char *end_of_full_line;
571 int in_header, new_header, last_mbox_line_was_empty;
572 unsigned long int position_in_file;
574 file = fopen(mbox_filename, "r");
577 fprintf(stderr, "mymail: Cannot open '%s'.\n", mbox_filename);
578 if(paranoid) { exit(EXIT_FAILURE); }
585 position_in_file = 0;
586 end_of_full_line = 0;
588 last_mbox_line_was_empty = 1;
590 while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
591 if(is_a_leading_from_line(last_mbox_line_was_empty, raw_mbox_line)) {
594 "Got a ^\"From \" in the header in %s:%lu.\n",
595 mbox_filename, position_in_file);
596 fprintf(stderr, "%s", raw_mbox_line);
597 if(paranoid) { exit(EXIT_FAILURE); }
601 } else if(raw_mbox_line[0] == '\n') {
602 if(in_header) { in_header = 0; }
605 last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
609 fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
613 if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
614 char *start = raw_mbox_line;
615 while(*start == ' ' || *start == '\t') start++;
616 *(end_of_full_line++) = ' ';
617 strcpy(end_of_full_line, start);
618 while(*end_of_full_line && *end_of_full_line != '\n') {
621 *end_of_full_line = '\0';
626 if(!((raw_mbox_line[0] >= 'a' && raw_mbox_line[0] <= 'z') ||
627 (raw_mbox_line[0] >= 'A' && raw_mbox_line[0] <= 'Z'))) {
629 "Header line syntax error %s:%lu.\n",
630 mbox_filename, position_in_file);
631 fprintf(stderr, "%s", raw_mbox_line);
636 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
639 end_of_full_line = full_line;
640 strcpy(end_of_full_line, raw_mbox_line);
641 while(*end_of_full_line && *end_of_full_line != '\n') {
644 *end_of_full_line = '\0';
649 position_in_file += strlen(raw_mbox_line);
655 void recursive_index_mbox(FILE *db_file,
656 const char *entry_name,
657 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
659 struct dirent *dir_e;
661 char subname[PATH_MAX + 1];
663 if(lstat(entry_name, &sb) != 0) {
665 "mymail: Cannot stat \"%s\": %s\n",
671 dir = opendir(entry_name);
674 while((dir_e = readdir(dir))) {
675 if(!ignore_entry(dir_e->d_name)) {
676 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
677 recursive_index_mbox(db_file, subname, nb_fields_to_parse, fields_to_parse);
682 index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
686 /*********************************************************************/
688 /* For long options that have no equivalent short option, use a
689 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
691 OPT_BASH_MODE = CHAR_MAX + 1
694 static struct option long_options[] = {
695 { "help", no_argument, 0, 'h' },
696 { "version", no_argument, 0, 'v' },
697 { "quiet", no_argument, 0, 'q' },
698 { "db-file", 1, 0, 'd' },
699 { "db-pattern", 1, 0, 'p' },
700 { "db-root", 1, 0, 'r' },
701 { "db-list", 1, 0, 'l' },
702 { "search", 1, 0, 's' },
703 { "index", 0, 0, 'i' },
704 { "output", 1, 0, 'o' },
705 { "default-search", 1, 0, 'a' },
709 /*********************************************************************/
711 void init_condition(struct search_condition *condition, char *full_string) {
712 char full_search_field[TOKEN_BUFFER_SIZE], *search_field;
716 string = parse_token(full_search_field, TOKEN_BUFFER_SIZE, ' ', full_string);
717 search_field = full_search_field;
719 if(search_field[0] == '!') {
721 condition->negation = 1;
723 condition->negation = 0;
728 if(strcmp(search_field, "8h") == 0) {
729 condition->field_id = ID_INTERVAL;
730 condition->interval_start = time(0) - 3600 * 8;
731 condition->interval_stop = 0;
734 else if(strcmp(search_field, "week") == 0) {
735 condition->field_id = ID_INTERVAL;
736 condition->interval_start = time(0) - 3600 * 24 * 7;
737 condition->interval_stop = 0;
740 else if(strcmp(search_field, "month") == 0) {
741 condition->field_id = ID_INTERVAL;
742 condition->interval_start = time(0) - 3600 * 24 * 31;
743 condition->interval_stop = 0;
746 else if(strcmp(search_field, "24h") == 0 ||
747 strcmp(search_field, "today") == 0) {
748 condition->field_id = ID_INTERVAL;
749 condition->interval_start = time(0) - 3600 * 24;
750 condition->interval_stop = 0;
755 else if(strcmp(search_field, "yesterday") == 0) {
756 condition->field_id = ID_INTERVAL;
757 condition->interval_start = time(0) - 2 * 3600 * 24;
758 condition->interval_stop = condition->interval_start + 3600 * 24;
763 else if(strcmp(search_field, "monday") == 0) {
764 condition->field_id = ID_INTERVAL;
765 condition->interval_start = time_for_past_day(1);
766 condition->interval_stop = condition->interval_start + 3600 * 24;
769 else if(strcmp(search_field, "tuesday") == 0) {
770 condition->field_id = ID_INTERVAL;
771 condition->interval_start = time_for_past_day(2);
772 condition->interval_stop = condition->interval_start + 3600 * 24;
775 else if(strcmp(search_field, "wednesday") == 0) {
776 condition->field_id = ID_INTERVAL;
777 condition->interval_start = time_for_past_day(3);
778 condition->interval_stop = condition->interval_start + 3600 * 24;
781 else if(strcmp(search_field, "thursday") == 0) {
782 condition->field_id = ID_INTERVAL;
783 condition->interval_start = time_for_past_day(4);
784 condition->interval_stop = condition->interval_start + 3600 * 24;
787 else if(strcmp(search_field, "friday") == 0) {
788 condition->field_id = ID_INTERVAL;
789 condition->interval_start = time_for_past_day(5);
790 condition->interval_stop = condition->interval_start + 3600 * 24;
793 else if(strcmp(search_field, "saturday") == 0) {
794 condition->field_id = ID_INTERVAL;
795 condition->interval_start = time_for_past_day(6);
796 condition->interval_stop = condition->interval_start + 3600 * 24;
799 else if(strcmp(search_field, "sunday") == 0) {
800 condition->field_id = ID_INTERVAL;
801 condition->interval_start = time_for_past_day(7);
802 condition->interval_stop = condition->interval_start + 3600 * 24;
807 /* header-related conditions */
809 condition->field_id = -1;
811 for(m = 0; (m < MAX_ID) && condition->field_id == -1; m++) {
812 if(strncmp(field_names[m], search_field, strlen(search_field)) == 0) {
813 condition->field_id = m;
817 if(condition->field_id == -1) {
818 if(default_search_field) {
819 for(m = 0; (m < MAX_ID) && condition->field_id == -1; m++) {
820 if(strncmp(field_names[m],
821 default_search_field, strlen(default_search_field)) == 0) {
822 condition->field_id = m;
825 string = full_string;
829 if(condition->field_id == -1) {
831 "mymail: Syntax error in field name \"%s\".\n",
836 if(regcomp(&condition->regexp,
840 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
842 field_names[condition->field_id]);
848 void free_condition(struct search_condition *condition) {
849 if(condition->field_id != ID_INTERVAL) {
850 regfree(&condition->regexp);
854 /*********************************************************************/
855 /*********************************************************************/
856 /*********************************************************************/
858 int main(int argc, char **argv) {
860 char *db_filename_regexp_string;
862 char *db_filename_list;
863 char output_filename[PATH_MAX + 1];
865 int error = 0, show_help = 0;
866 const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
869 int nb_search_conditions;
871 struct search_condition search_conditions[MAX_NB_SEARCH_CONDITIONS];
873 if(regcomp(&leading_from_line_regexp,
874 "^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$",
877 "mymail: Cannot compile leading \"from\" line regexp. That is strange.\n");
884 db_filename_regexp_string = 0;
886 db_filename_list = 0;
888 default_search_field = 0;
890 setlocale(LC_ALL, "");
892 nb_search_conditions = 0;
894 while ((c = getopt_long(argc, argv, "hvqip:s:d:r:l:o:a:",
895 long_options, NULL)) != -1) {
904 print_version(stdout);
916 db_filename = strdup(optarg);
920 db_filename_regexp_string = strdup(optarg);
924 strncpy(output_filename, optarg, PATH_MAX);
928 db_root_path = strdup(optarg);
932 db_filename_list = strdup(optarg);
936 if(nb_search_conditions == MAX_NB_SEARCH_CONDITIONS) {
937 fprintf(stderr, "mymail: Too many search patterns.\n");
940 init_condition(&search_conditions[nb_search_conditions], optarg);
941 nb_search_conditions++;
945 default_search_field = optarg;
955 char *default_db_filename = getenv("MYMAIL_DB_FILE");
957 if(!default_db_filename) {
958 default_db_filename = "mymail.db";
961 db_filename = strdup(default_db_filename);
964 if(!db_filename_regexp_string) {
965 char *default_db_filename_regexp_string = getenv("MYMAIL_DB_PATTERN");
967 if(!default_db_filename_regexp_string) {
968 default_db_filename_regexp_string = "^mymail.db$";
971 db_filename_regexp_string = strdup(default_db_filename_regexp_string);
975 char *default_db_root_path = getenv("MYMAIL_DB_ROOT");
977 if(default_db_root_path) {
978 db_root_path = strdup(default_db_root_path);
982 if(!db_filename_list) {
983 char *default_db_filename_list = getenv("MYMAIL_DB_LIST");
985 if(default_db_filename_list) {
986 db_filename_list = strdup(default_db_filename_list);
990 if(output_filename[0]) {
991 output_file = fopen(output_filename, "w");
995 "mymail: Cannot open result file \"%s\" for writing: %s\n",
1001 output_file = stdout;
1006 print_usage(stderr);
1011 print_usage(stdout);
1018 db_file = fopen(db_filename, "w");
1022 "mymail: Cannot open \"%s\" for writing: %s\n",
1028 for(f = 0; f < nb_fields_to_parse; f++) {
1029 if(regcomp(&fields_to_parse[f].regexp,
1030 fields_to_parse[f].regexp_string,
1031 fields_to_parse[f].cflags)) {
1033 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
1034 fields_to_parse[f].regexp_string,
1035 field_names[fields_to_parse[f].id]);
1040 fprintf(db_file, "%s version_%s raw\n", MYMAIL_DB_MAGIC_TOKEN, VERSION);
1042 while(optind < argc) {
1043 recursive_index_mbox(db_file,
1045 nb_fields_to_parse, fields_to_parse);
1052 for(f = 0; f < nb_fields_to_parse; f++) {
1053 regfree(&fields_to_parse[f].regexp);
1059 if(nb_search_conditions > 0) {
1061 /* Recursive search if db_root_path is set */
1064 regex_t db_filename_regexp;
1065 if(regcomp(&db_filename_regexp,
1066 db_filename_regexp_string,
1069 "mymail: Syntax error in regexp \"%s\".\n",
1070 db_filename_regexp_string);
1074 recursive_search_in_db(db_root_path, &db_filename_regexp,
1075 nb_search_conditions, search_conditions,
1078 regfree(&db_filename_regexp);
1081 /* Search in all db files listed in db_filename_list */
1083 if(db_filename_list) {
1084 char db_filename[PATH_MAX + 1];
1087 s = db_filename_list;
1090 s = parse_token(db_filename, PATH_MAX + 1, ';', s);
1092 if(db_filename[0]) {
1093 search_in_db(db_filename, nb_search_conditions, search_conditions, output_file);
1098 /* Search in all db files listed in the command arguments */
1100 while(optind < argc) {
1101 search_in_db(argv[optind], nb_search_conditions, search_conditions, output_file);
1107 for(n = 0; n < nb_search_conditions; n++) {
1108 free_condition(&search_conditions[n]);
1111 if(output_file != stdout) {
1112 fflush(output_file);
1113 fclose(output_file);
1117 free(db_filename_regexp_string);
1119 free(db_filename_list);
1121 regfree(&leading_from_line_regexp);