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.2"
51 #define MAX_NB_SEARCH_CONDITIONS 32
53 #define BUFFER_SIZE 65536
54 #define TOKEN_BUFFER_SIZE 1024
56 #define LEADING_FROM_LINE_REGEXP "^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$"
58 regex_t leading_from_line_regexp;
60 /* Global variables! */
64 char *default_search_field;
67 /********************************************************************/
82 static char *field_names[] = {
94 /********************************************************************/
96 struct search_condition {
100 time_t interval_start, interval_stop;
103 /********************************************************************/
105 struct parsable_field {
112 static struct parsable_field fields_to_parse[] = {
117 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
123 "^\\(from\\|reply-to\\|sender\\|return-path\\): ",
124 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
130 "^\\(to\\|cc\\|bcc\\): ",
131 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
138 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
145 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
150 /********************************************************************/
152 int xor(int a, int b) {
153 return (a && !b) || (!a && b);
156 char *parse_token(char *token_buffer, size_t token_buffer_size,
157 char separator, char *string) {
158 char *u = token_buffer;
159 while(u < token_buffer + token_buffer_size - 1 && *string &&
160 *string != separator) {
161 *(u++) = *(string++);
163 while(*string == separator) string++;
168 /********************************************************************/
170 /* malloc with error checking. */
172 void *safe_malloc(size_t n) {
176 "mymail: cannot allocate memory: %s\n", strerror(errno));
182 /*********************************************************************/
184 void print_version(FILE *out) {
185 fprintf(out, "mymail version %s (%s)\n", VERSION, UNAME);
188 void print_usage(FILE *out) {
190 fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
192 fprintf(out, "Usage: mymail [options] [<mbox dir1> [<mbox dir2> ...]|<db file1> [<db file2> ...]]\n");
194 fprintf(out, " -h, --help\n");
195 fprintf(out, " show this help\n");
196 fprintf(out, " -v, --version\n");
197 fprintf(out, " print the version number\n");
198 fprintf(out, " -q, --quiet\n");
199 fprintf(out, " do not print information during search\n");
200 fprintf(out, " -p <db filename pattern>, --db-pattern <db filename pattern>\n");
201 fprintf(out, " set the db filename pattern for recursive search\n");
202 fprintf(out, " -r <db root path>, --db-root <db root path>\n");
203 fprintf(out, " set the db root path for recursive search\n");
204 fprintf(out, " -l <db filename list>, --db-list <db filename list>\n");
205 fprintf(out, " set the semicolon-separated list of db files for search\n");
206 fprintf(out, " -s <search pattern>, --search <search pattern>\n");
207 fprintf(out, " search for matching mails in the db file\n");
208 fprintf(out, " -d <db filename>, --db-file <db filename>\n");
209 fprintf(out, " set the db filename for indexing\n");
210 fprintf(out, " -i, --index\n");
211 fprintf(out, " index mails\n");
212 fprintf(out, " -o <output filename>, --output <output filename>\n");
213 fprintf(out, " set the result file, use stdout if unset\n");
214 fprintf(out, " -a <search field>, --default-search <search field>\n");
215 fprintf(out, " set the default search field\n");
218 /*********************************************************************/
220 time_t time_for_past_day(int day) {
226 delta_day = (7 + tm->tm_wday - day) % 7 + 1;
227 return t - delta_day * 3600 * 24 + tm->tm_sec + 60 * tm->tm_min + 3600 * tm->tm_hour;
230 /*********************************************************************/
232 int ignore_entry(const char *name) {
234 strcmp(name, ".") == 0 ||
235 strcmp(name, "..") == 0 ||
236 (ignore_dot_files && name[0] == '.' && name[1] != '/');
239 int is_a_leading_from_line(char *mbox_line) {
244 The mbox man page in qmail documentation states:
246 > The reader should not attempt to take advantage of the fact
247 > that every From_ line (past the beginning of the file) is
248 > preceded by a blank line.
252 strncmp(mbox_line, "From ", 5) == 0 &&
253 regexec(&leading_from_line_regexp, mbox_line, 0, 0, 0) == 0;
256 int mbox_line_match_search(struct search_condition *condition,
257 int mbox_id, char *mbox_value) {
259 if(condition->field_id == ID_INTERVAL) {
260 if(mbox_id == ID_LEADING_LINE) {
266 while(*c && *c != ' ') c++; while(*c && *c == ' ') c++;
267 strptime(c, "%a %b %e %k:%M:%S %Y", &tm);
270 return (t >= condition->interval_start &&
271 (condition->interval_stop == 0 ||
272 t <= condition->interval_stop));
280 (condition->field_id == mbox_id)
284 (condition->field_id == ID_PARTICIPANT && (mbox_id == ID_LEADING_LINE ||
285 mbox_id == ID_FROM ||
289 (condition->field_id == ID_FROM && mbox_id == ID_LEADING_LINE)
295 regexec(&condition->regexp, mbox_value, 0, 0, 0) == 0;
299 void update_body_hits(char *mail_filename, int position_in_mail,
300 int nb_search_conditions, struct search_condition *search_conditions,
301 int nb_body_conditions,
305 char raw_mbox_line[BUFFER_SIZE];
311 mail_file = fopen(mail_filename, "r");
315 "mymail: Cannot open mbox '%s' for body scan.\n",
320 fseek(mail_file, position_in_mail, SEEK_SET);
322 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
323 while(nb_body_hits < nb_body_conditions) {
324 /* last_mbox_line_was_empty = (raw_mbox_line[0] == '\n'); */
325 /* if(last_mbox_line_was_empty) { header = 0; } */
327 if(raw_mbox_line[0] == '\n') { header = 0; }
330 for(n = 0; n < nb_search_conditions; n++) {
331 if(search_conditions[n].field_id == ID_BODY && !hits[n]) {
333 (regexec(&search_conditions[n].regexp, raw_mbox_line, 0, 0, 0) == 0);
341 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
342 (is_a_leading_from_line(raw_mbox_line)))
350 void extract_mail(const char *mail_filename, unsigned long int position_in_mail,
352 char raw_mbox_line[BUFFER_SIZE];
355 mail_file = fopen(mail_filename, "r");
359 "mymail: Cannot open mbox '%s' for mail extraction.\n",
364 fseek(mail_file, position_in_mail, SEEK_SET);
366 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
367 fprintf(output_file, "%s", raw_mbox_line);
369 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
370 (is_a_leading_from_line(raw_mbox_line))
373 fprintf(output_file, "%s", raw_mbox_line);
380 void search_in_db(const char *db_filename,
381 int nb_search_conditions,
382 struct search_condition *search_conditions,
385 int hits[MAX_NB_SEARCH_CONDITIONS];
386 char raw_db_line[BUFFER_SIZE];
387 char current_mail_filename[PATH_MAX + 1];
388 unsigned long int current_position_in_mail;
389 char mbox_name[TOKEN_BUFFER_SIZE], *mbox_value;
391 int already_written, m, n;
392 int nb_body_conditions, nb_fulfilled_body_conditions;
396 printf("Searching in '%s' ... ", db_filename);
400 db_file = fopen(db_filename, "r");
404 "mymail: Cannot open \"%s\" for reading: %s\n",
410 /* First, check the db file leading line integrity */
412 if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
413 if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
415 "mymail: Header line in '%s' does not match the mymail db format.\n",
421 "mymail: Cannot read the header line in '%s'.\n",
426 /* Then parse the said db file */
428 current_position_in_mail = 0;
431 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
433 nb_body_conditions = 0;
434 for(n = 0; n < nb_search_conditions; n++) {
435 if(search_conditions[n].field_id == ID_BODY) {
436 nb_body_conditions++;
440 strcpy(current_mail_filename, "");
442 while(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
443 mbox_value = parse_token(mbox_name, TOKEN_BUFFER_SIZE, ' ', raw_db_line);
445 if(strcmp("mail", mbox_name) == 0) {
446 char position_in_file_string[TOKEN_BUFFER_SIZE];
448 if(current_mail_filename[0]) {
450 /* We first check all conditions but the body ones */
452 for(n = 0; n < nb_search_conditions &&
453 ((search_conditions[n].field_id == ID_BODY) ||
454 xor(hits[n], search_conditions[n].negation)); n++);
456 if(n == nb_search_conditions) {
458 /* Now check the body ones */
460 if(nb_body_conditions > 0) {
461 update_body_hits(current_mail_filename, current_position_in_mail,
462 nb_search_conditions, search_conditions,
467 nb_fulfilled_body_conditions = 0;
469 for(n = 0; n < nb_search_conditions; n++) {
470 if(search_conditions[n].field_id == ID_BODY &&
471 xor(hits[n], search_conditions[n].negation)) {
472 nb_fulfilled_body_conditions++;
476 if(nb_body_conditions == nb_fulfilled_body_conditions) {
477 extract_mail(current_mail_filename, current_position_in_mail, output_file);
482 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
484 mbox_value = parse_token(position_in_file_string, TOKEN_BUFFER_SIZE, ' ', mbox_value);
485 mbox_value = parse_token(current_mail_filename, TOKEN_BUFFER_SIZE, '\n', mbox_value);
486 current_position_in_mail = atol(position_in_file_string);
492 for(m = 0; (m < MAX_ID) && mbox_id == -1; m++) {
493 if(strncmp(field_names[m], mbox_name, strlen(mbox_name)) == 0) {
497 for(n = 0; n < nb_search_conditions; n++) {
498 hits[n] |= mbox_line_match_search(&search_conditions[n],
499 mbox_id, mbox_value);
512 void recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp,
513 int nb_search_conditions,
514 struct search_condition *search_conditions,
517 struct dirent *dir_e;
519 char subname[PATH_MAX + 1];
521 if(lstat(entry_name, &sb) != 0) {
523 "mymail: Cannot stat \"%s\": %s\n",
529 dir = opendir(entry_name);
532 while((dir_e = readdir(dir))) {
533 if(!ignore_entry(dir_e->d_name)) {
534 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
535 recursive_search_in_db(subname, db_filename_regexp,
536 nb_search_conditions, search_conditions,
544 const char *s = entry_name, *filename = entry_name;
545 while(*s) { if(*s == '/') { filename = s+1; } s++; }
547 if(regexec(db_filename_regexp, filename, 0, 0, 0) == 0) {
548 search_in_db(entry_name, nb_search_conditions, search_conditions, output_file);
553 /*********************************************************************/
555 void index_one_mbox_line(int nb_fields_to_parse, struct parsable_field *fields_to_parse,
556 char *raw_mbox_line, FILE *db_file) {
559 for(f = 0; f < nb_fields_to_parse; f++) {
560 if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
561 fprintf(db_file, "%s %s\n",
562 field_names[fields_to_parse[f].id],
563 raw_mbox_line + matches.rm_eo);
568 void index_mbox(const char *mbox_filename,
569 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
571 char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
572 char *end_of_full_line;
574 int in_header, new_header;
575 unsigned long int position_in_file;
577 file = fopen(mbox_filename, "r");
580 fprintf(stderr, "mymail: Cannot open '%s'.\n", mbox_filename);
581 if(paranoid) { exit(EXIT_FAILURE); }
588 position_in_file = 0;
589 end_of_full_line = 0;
592 while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
593 if(is_a_leading_from_line(raw_mbox_line)) {
596 "Got a ^\"From \" in the header in %s:%lu.\n",
597 mbox_filename, position_in_file);
598 fprintf(stderr, "%s", raw_mbox_line);
599 if(paranoid) { exit(EXIT_FAILURE); }
603 } else if(raw_mbox_line[0] == '\n') {
604 if(in_header) { in_header = 0; }
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, "24h") == 0 ||
735 strcmp(search_field, "today") == 0) {
736 condition->field_id = ID_INTERVAL;
737 condition->interval_start = time(0) - 3600 * 24;
738 condition->interval_stop = 0;
741 else if(strcmp(search_field, "week") == 0) {
742 condition->field_id = ID_INTERVAL;
743 condition->interval_start = time(0) - 3600 * 24 * 7;
744 condition->interval_stop = 0;
747 else if(strcmp(search_field, "month") == 0) {
748 condition->field_id = ID_INTERVAL;
749 condition->interval_start = time(0) - 3600 * 24 * 31;
750 condition->interval_stop = 0;
753 else if(strcmp(search_field, "year") == 0) {
754 condition->field_id = ID_INTERVAL;
755 condition->interval_start = time(0) - 3600 * 24 * 365;
756 condition->interval_stop = 0;
761 else if(strcmp(search_field, "yesterday") == 0) {
762 condition->field_id = ID_INTERVAL;
763 condition->interval_start = time(0) - 2 * 3600 * 24;
764 condition->interval_stop = condition->interval_start + 3600 * 24;
769 else if(strcmp(search_field, "monday") == 0) {
770 condition->field_id = ID_INTERVAL;
771 condition->interval_start = time_for_past_day(1);
772 condition->interval_stop = condition->interval_start + 3600 * 24;
775 else if(strcmp(search_field, "tuesday") == 0) {
776 condition->field_id = ID_INTERVAL;
777 condition->interval_start = time_for_past_day(2);
778 condition->interval_stop = condition->interval_start + 3600 * 24;
781 else if(strcmp(search_field, "wednesday") == 0) {
782 condition->field_id = ID_INTERVAL;
783 condition->interval_start = time_for_past_day(3);
784 condition->interval_stop = condition->interval_start + 3600 * 24;
787 else if(strcmp(search_field, "thursday") == 0) {
788 condition->field_id = ID_INTERVAL;
789 condition->interval_start = time_for_past_day(4);
790 condition->interval_stop = condition->interval_start + 3600 * 24;
793 else if(strcmp(search_field, "friday") == 0) {
794 condition->field_id = ID_INTERVAL;
795 condition->interval_start = time_for_past_day(5);
796 condition->interval_stop = condition->interval_start + 3600 * 24;
799 else if(strcmp(search_field, "saturday") == 0) {
800 condition->field_id = ID_INTERVAL;
801 condition->interval_start = time_for_past_day(6);
802 condition->interval_stop = condition->interval_start + 3600 * 24;
805 else if(strcmp(search_field, "sunday") == 0) {
806 condition->field_id = ID_INTERVAL;
807 condition->interval_start = time_for_past_day(7);
808 condition->interval_stop = condition->interval_start + 3600 * 24;
813 /* header-related conditions */
815 condition->field_id = -1;
817 for(m = 0; (m < MAX_ID) && condition->field_id == -1; m++) {
818 if(strncmp(field_names[m], search_field, strlen(search_field)) == 0) {
819 condition->field_id = m;
823 if(condition->field_id == -1) {
824 if(default_search_field) {
825 for(m = 0; (m < MAX_ID) && condition->field_id == -1; m++) {
826 if(strncmp(field_names[m],
827 default_search_field, strlen(default_search_field)) == 0) {
828 condition->field_id = m;
831 string = full_string;
835 if(condition->field_id == -1) {
837 "mymail: Syntax error in field name \"%s\".\n",
842 if(regcomp(&condition->regexp,
846 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
848 field_names[condition->field_id]);
854 void free_condition(struct search_condition *condition) {
855 if(condition->field_id != ID_INTERVAL) {
856 regfree(&condition->regexp);
860 /*********************************************************************/
861 /*********************************************************************/
862 /*********************************************************************/
864 int main(int argc, char **argv) {
866 char *db_filename_regexp_string;
868 char *db_filename_list;
869 char output_filename[PATH_MAX + 1];
871 int error = 0, show_help = 0;
872 const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
875 int nb_search_conditions;
877 struct search_condition search_conditions[MAX_NB_SEARCH_CONDITIONS];
879 if(regcomp(&leading_from_line_regexp, LEADING_FROM_LINE_REGEXP, 0)) {
881 "mymail: Cannot compile leading \"from\" line regexp. That is strange.\n");
888 db_filename_regexp_string = 0;
890 db_filename_list = 0;
892 default_search_field = 0;
893 ignore_dot_files = 1;
895 setlocale(LC_ALL, "");
897 nb_search_conditions = 0;
899 while ((c = getopt_long(argc, argv, "hvqip:s:d:r:l:o:a:",
900 long_options, NULL)) != -1) {
909 print_version(stdout);
921 db_filename = strdup(optarg);
925 db_filename_regexp_string = strdup(optarg);
929 strncpy(output_filename, optarg, PATH_MAX);
933 db_root_path = strdup(optarg);
937 db_filename_list = strdup(optarg);
941 if(nb_search_conditions == MAX_NB_SEARCH_CONDITIONS) {
942 fprintf(stderr, "mymail: Too many search patterns.\n");
945 init_condition(&search_conditions[nb_search_conditions], optarg);
946 nb_search_conditions++;
950 default_search_field = optarg;
960 char *default_db_filename = getenv("MYMAIL_DB_FILE");
962 if(!default_db_filename) {
963 default_db_filename = "mymail.db";
966 db_filename = strdup(default_db_filename);
969 if(!db_filename_regexp_string) {
970 char *default_db_filename_regexp_string = getenv("MYMAIL_DB_PATTERN");
972 if(!default_db_filename_regexp_string) {
973 default_db_filename_regexp_string = "^mymail.db$";
976 db_filename_regexp_string = strdup(default_db_filename_regexp_string);
980 char *default_db_root_path = getenv("MYMAIL_DB_ROOT");
982 if(default_db_root_path) {
983 db_root_path = strdup(default_db_root_path);
987 if(!db_filename_list) {
988 char *default_db_filename_list = getenv("MYMAIL_DB_LIST");
990 if(default_db_filename_list) {
991 db_filename_list = strdup(default_db_filename_list);
995 if(output_filename[0]) {
996 output_file = fopen(output_filename, "w");
1000 "mymail: Cannot open result file \"%s\" for writing: %s\n",
1006 output_file = stdout;
1011 print_usage(stderr);
1016 print_usage(stdout);
1023 db_file = fopen(db_filename, "w");
1027 "mymail: Cannot open \"%s\" for writing: %s\n",
1033 for(f = 0; f < nb_fields_to_parse; f++) {
1034 if(regcomp(&fields_to_parse[f].regexp,
1035 fields_to_parse[f].regexp_string,
1036 fields_to_parse[f].cflags)) {
1038 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
1039 fields_to_parse[f].regexp_string,
1040 field_names[fields_to_parse[f].id]);
1045 fprintf(db_file, "%s version_%s raw\n", MYMAIL_DB_MAGIC_TOKEN, VERSION);
1047 while(optind < argc) {
1048 recursive_index_mbox(db_file,
1050 nb_fields_to_parse, fields_to_parse);
1057 for(f = 0; f < nb_fields_to_parse; f++) {
1058 regfree(&fields_to_parse[f].regexp);
1064 if(nb_search_conditions > 0) {
1066 /* Recursive search if db_root_path is set */
1069 regex_t db_filename_regexp;
1070 if(regcomp(&db_filename_regexp,
1071 db_filename_regexp_string,
1074 "mymail: Syntax error in regexp \"%s\".\n",
1075 db_filename_regexp_string);
1079 recursive_search_in_db(db_root_path, &db_filename_regexp,
1080 nb_search_conditions, search_conditions,
1083 regfree(&db_filename_regexp);
1086 /* Search in all db files listed in db_filename_list */
1088 if(db_filename_list) {
1089 char db_filename[PATH_MAX + 1];
1092 s = db_filename_list;
1095 s = parse_token(db_filename, PATH_MAX + 1, ';', s);
1097 if(db_filename[0]) {
1098 search_in_db(db_filename, nb_search_conditions, search_conditions, output_file);
1103 /* Search in all db files listed in the command arguments */
1105 while(optind < argc) {
1106 search_in_db(argv[optind], nb_search_conditions, search_conditions, output_file);
1112 for(n = 0; n < nb_search_conditions; n++) {
1113 free_condition(&search_conditions[n]);
1116 if(output_file != stdout) {
1117 fflush(output_file);
1118 fclose(output_file);
1122 free(db_filename_regexp_string);
1124 free(db_filename_list);
1126 regfree(&leading_from_line_regexp);