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, to create a resulting mbox file.
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_STRING "^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! */
66 /********************************************************************/
81 static char *field_names[] = {
93 /********************************************************************/
95 struct search_condition {
99 time_t interval_start, interval_stop;
102 /********************************************************************/
104 struct parsable_field {
111 static struct parsable_field fields_to_parse[] = {
116 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
122 "^\\(from\\|reply-to\\|sender\\|return-path\\): ",
123 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
129 "^\\(to\\|cc\\|bcc\\): ",
130 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
137 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
144 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
149 /********************************************************************/
151 int xor(int a, int b) {
152 return (a && !b) || (!a && b);
155 const char *parse_token(char *token_buffer, size_t token_buffer_size,
156 char separator, const char *string) {
157 char *u = token_buffer;
158 while(u < token_buffer + token_buffer_size - 1 && *string &&
159 *string != separator) {
160 *(u++) = *(string++);
162 while(*string == separator) string++;
167 char *default_value(char *current_value,
168 const char *env_variable,
169 const char *hard_default_value) {
171 return current_value;
173 char *env_value = getenv(env_variable);
175 return strdup(env_value);
176 } else if(hard_default_value) {
177 return strdup(hard_default_value);
184 FILE *safe_fopen(const char *path, const char *mode, const char *comment) {
185 FILE *result = fopen(path, mode);
186 /* printf("Opening '%s' with mode \"%s\"\n", path, mode); */
191 "mymail: Cannot open file '%s' (%s) with mode \"%s\".\n",
192 path, comment, mode);
197 /*********************************************************************/
199 void print_version(FILE *out) {
200 fprintf(out, "mymail version %s (%s)\n", VERSION, UNAME);
203 void print_usage(FILE *out) {
205 fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
207 fprintf(out, "Usage: mymail [options] [<mbox dir1> [<mbox dir2> ...]|<db file1> [<db file2> ...]]\n");
209 fprintf(out, " -h, --help\n");
210 fprintf(out, " show this help\n");
211 fprintf(out, " -v, --version\n");
212 fprintf(out, " print the version number\n");
213 fprintf(out, " -q, --quiet\n");
214 fprintf(out, " do not print information during search\n");
215 fprintf(out, " -p <db filename pattern>, --db-pattern <db filename pattern>\n");
216 fprintf(out, " set the db filename pattern for recursive search\n");
217 fprintf(out, " -r <db root path>, --db-root <db root path>\n");
218 fprintf(out, " set the db root path for recursive search\n");
219 fprintf(out, " -l <db filename list>, --db-list <db filename list>\n");
220 fprintf(out, " set the semicolon-separated list of db files for search\n");
221 fprintf(out, " -m <mbox filename pattern>, --mbox-pattern <mbox filename pattern>\n");
222 fprintf(out, " set the mbox filename pattern for recursive search\n");
223 fprintf(out, " -s <search pattern>, --search <search pattern>\n");
224 fprintf(out, " search for matching mails in the db file\n");
225 fprintf(out, " -d <db filename>, --db-file-generate <db filename>\n");
226 fprintf(out, " set the db filename for indexing\n");
227 fprintf(out, " -i, --index\n");
228 fprintf(out, " index mails\n");
229 fprintf(out, " -o <output filename>, --output <output filename>\n");
230 fprintf(out, " set the result file, use stdout if unset\n");
231 fprintf(out, " -a <search field>, --default-search <search field>\n");
232 fprintf(out, " set the default search field\n");
235 /*********************************************************************/
237 int ignore_entry(const char *name) {
239 strcmp(name, ".") == 0 ||
240 strcmp(name, "..") == 0 ||
241 (ignore_dot_files && name[0] == '.' && name[1] != '/');
244 int is_a_leading_from_line(char *mbox_line) {
246 strncmp(mbox_line, "From ", 5) == 0 &&
247 regexec(&leading_from_line_regexp, mbox_line, 0, 0, 0) == 0;
250 int mbox_line_match_search(struct search_condition *condition,
251 int mbox_id, const char *mbox_value) {
253 if(condition->field_id == ID_INTERVAL) {
254 if(mbox_id == ID_LEADING_LINE) {
260 while(*c && *c != ' ') c++; while(*c && *c == ' ') c++;
261 strptime(c, "%a %b %e %k:%M:%S %Y", &tm);
264 return (t >= condition->interval_start &&
265 (condition->interval_stop == 0 ||
266 t <= condition->interval_stop));
274 (condition->field_id == mbox_id)
278 (condition->field_id == ID_PARTICIPANT && (mbox_id == ID_LEADING_LINE ||
279 mbox_id == ID_FROM ||
283 (condition->field_id == ID_FROM && mbox_id == ID_LEADING_LINE)
289 regexec(&condition->regexp, mbox_value, 0, 0, 0) == 0;
293 void update_body_hits(char *mail_filename, int position_in_mail,
294 int nb_search_conditions, struct search_condition *search_conditions,
295 int nb_body_conditions,
299 char raw_mbox_line[BUFFER_SIZE];
305 mail_file = safe_fopen(mail_filename, "r", "mbox for body scan");
307 fseek(mail_file, position_in_mail, SEEK_SET);
309 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
310 while(nb_body_hits < nb_body_conditions) {
311 /* last_mbox_line_was_empty = (raw_mbox_line[0] == '\n'); */
312 /* if(last_mbox_line_was_empty) { header = 0; } */
314 if(raw_mbox_line[0] == '\n') { header = 0; }
317 for(n = 0; n < nb_search_conditions; n++) {
318 if(search_conditions[n].field_id == ID_BODY && !hits[n]) {
320 (regexec(&search_conditions[n].regexp, raw_mbox_line, 0, 0, 0) == 0);
328 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
329 (is_a_leading_from_line(raw_mbox_line)))
337 void extract_mail(const char *mail_filename, unsigned long int position_in_mail,
339 char raw_mbox_line[BUFFER_SIZE];
342 mail_file = safe_fopen(mail_filename, "r", "mbox for mail extraction");
343 fseek(mail_file, position_in_mail, SEEK_SET);
345 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
346 fprintf(output_file, "%s", raw_mbox_line);
348 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
349 (is_a_leading_from_line(raw_mbox_line))
352 fprintf(output_file, "%s", raw_mbox_line);
359 int search_in_db(const char *db_filename,
360 int nb_search_conditions,
361 struct search_condition *search_conditions,
364 int hits[MAX_NB_SEARCH_CONDITIONS];
365 char raw_db_line[BUFFER_SIZE];
366 char current_mail_filename[PATH_MAX + 1];
367 unsigned long int current_position_in_mail;
368 char mbox_name[TOKEN_BUFFER_SIZE];
369 const char *mbox_value;
371 int already_written, m, n;
372 int nb_body_conditions, nb_fulfilled_body_conditions;
374 int nb_extracted_mails;
376 nb_extracted_mails = 0;
379 printf("Searching in '%s' ... ", db_filename);
383 db_file = safe_fopen(db_filename, "r", "index file for search");
385 /* First, check the db file leading line integrity */
387 if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
388 if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
390 "mymail: Header line in '%s' does not match the mymail db format.\n",
396 "mymail: Cannot read the header line in '%s'.\n",
401 /* Then parse the said db file */
403 current_position_in_mail = 0;
406 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
408 nb_body_conditions = 0;
409 for(n = 0; n < nb_search_conditions; n++) {
410 if(search_conditions[n].field_id == ID_BODY) {
411 nb_body_conditions++;
415 strcpy(current_mail_filename, "");
417 while(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
418 mbox_value = parse_token(mbox_name, TOKEN_BUFFER_SIZE, ' ', raw_db_line);
420 if(strcmp("mail", mbox_name) == 0) {
421 char position_in_file_string[TOKEN_BUFFER_SIZE];
423 if(current_mail_filename[0]) {
425 /* We first check all conditions but the body ones */
427 for(n = 0; n < nb_search_conditions &&
428 ((search_conditions[n].field_id == ID_BODY) ||
429 xor(hits[n], search_conditions[n].negation)); n++);
431 if(n == nb_search_conditions) {
433 /* Now check the body ones */
435 if(nb_body_conditions > 0) {
436 update_body_hits(current_mail_filename, current_position_in_mail,
437 nb_search_conditions, search_conditions,
442 nb_fulfilled_body_conditions = 0;
444 for(n = 0; n < nb_search_conditions; n++) {
445 if(search_conditions[n].field_id == ID_BODY &&
446 xor(hits[n], search_conditions[n].negation)) {
447 nb_fulfilled_body_conditions++;
451 if(nb_body_conditions == nb_fulfilled_body_conditions) {
452 nb_extracted_mails++;
453 extract_mail(current_mail_filename, current_position_in_mail, output_file);
458 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
460 mbox_value = parse_token(position_in_file_string, TOKEN_BUFFER_SIZE, ' ', mbox_value);
461 mbox_value = parse_token(current_mail_filename, TOKEN_BUFFER_SIZE, '\n', mbox_value);
462 current_position_in_mail = atol(position_in_file_string);
468 for(m = 0; (m < MAX_ID) && mbox_id == -1; m++) {
469 if(strncmp(field_names[m], mbox_name, strlen(mbox_name)) == 0) {
473 for(n = 0; n < nb_search_conditions; n++) {
474 hits[n] |= mbox_line_match_search(&search_conditions[n],
475 mbox_id, mbox_value);
487 return nb_extracted_mails;
490 int 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];
498 int nb_extracted_mails = 0;
500 if(lstat(entry_name, &sb) != 0) {
502 "mymail: Cannot stat \"%s\": %s\n",
508 /* printf("recursive_search_in_db %s\n", entry_name); */
510 dir = opendir(entry_name);
513 while((dir_e = readdir(dir))) {
514 if(!ignore_entry(dir_e->d_name)) {
515 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
516 nb_extracted_mails += recursive_search_in_db(subname, db_filename_regexp,
517 nb_search_conditions, search_conditions,
525 const char *s = entry_name, *filename = entry_name;
526 while(*s) { if(*s == '/') { filename = s+1; } s++; }
528 if(regexec(db_filename_regexp, filename, 0, 0, 0) == 0) {
529 nb_extracted_mails +=
530 search_in_db(entry_name, nb_search_conditions, search_conditions, output_file);
534 return nb_extracted_mails;
537 /*********************************************************************/
539 void index_one_mbox_line(unsigned int nb_fields_to_parse,
540 struct parsable_field *fields_to_parse,
541 char *raw_mbox_line, FILE *db_file) {
544 for(f = 0; f < nb_fields_to_parse; f++) {
545 if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
546 fprintf(db_file, "%s %s\n",
547 field_names[fields_to_parse[f].id],
548 raw_mbox_line + matches.rm_eo);
553 void index_mbox(const char *mbox_filename,
554 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
556 char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
557 char *end_of_full_line;
559 int in_header, new_header;
560 unsigned long int position_in_file;
562 file = safe_fopen(mbox_filename, "r", "mbox for indexing");
567 position_in_file = 0;
568 end_of_full_line = 0;
571 while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
572 if(is_a_leading_from_line(raw_mbox_line)) {
575 "Got a ^\"From \" in the header in %s:%lu.\n",
576 mbox_filename, position_in_file);
577 fprintf(stderr, "%s", raw_mbox_line);
578 if(paranoid) { exit(EXIT_FAILURE); }
582 } else if(raw_mbox_line[0] == '\n') {
583 if(in_header) { in_header = 0; }
588 fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
592 if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
593 char *start = raw_mbox_line;
594 while(*start == ' ' || *start == '\t') start++;
595 *(end_of_full_line++) = ' ';
596 strcpy(end_of_full_line, start);
597 while(*end_of_full_line && *end_of_full_line != '\n') {
600 *end_of_full_line = '\0';
605 if(!((raw_mbox_line[0] >= 'a' && raw_mbox_line[0] <= 'z') ||
606 (raw_mbox_line[0] >= 'A' && raw_mbox_line[0] <= 'Z'))) {
608 "Header line syntax error %s:%lu.\n",
609 mbox_filename, position_in_file);
610 fprintf(stderr, "%s", raw_mbox_line);
615 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
618 end_of_full_line = full_line;
619 strcpy(end_of_full_line, raw_mbox_line);
620 while(*end_of_full_line && *end_of_full_line != '\n') {
623 *end_of_full_line = '\0';
628 position_in_file += strlen(raw_mbox_line);
634 void recursive_index_mbox(FILE *db_file,
635 const char *entry_name, regex_t *mbox_filename_regexp,
636 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
638 struct dirent *dir_e;
640 char subname[PATH_MAX + 1];
642 if(lstat(entry_name, &sb) != 0) {
644 "mymail: Cannot stat \"%s\": %s\n",
650 dir = opendir(entry_name);
653 while((dir_e = readdir(dir))) {
654 if(!ignore_entry(dir_e->d_name)) {
655 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
656 recursive_index_mbox(db_file, subname, mbox_filename_regexp,
657 nb_fields_to_parse, fields_to_parse);
662 const char *s = entry_name, *filename = s;
663 while(*s) { if(*s == '/') { filename = s+1; }; s++; }
664 if(!mbox_filename_regexp || regexec(mbox_filename_regexp, filename, 0, 0, 0) == 0) {
665 index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
670 /*********************************************************************/
672 /* For long options that have no equivalent short option, use a
673 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
675 OPT_BASH_MODE = CHAR_MAX + 1
678 static struct option long_options[] = {
679 { "help", no_argument, 0, 'h' },
680 { "version", no_argument, 0, 'v' },
681 { "quiet", no_argument, 0, 'q' },
682 { "db-file-generate", 1, 0, 'd' },
683 { "db-pattern", 1, 0, 'p' },
684 { "db-root", 1, 0, 'r' },
685 { "db-list", 1, 0, 'l' },
686 { "mbox-pattern", 1, 0, 'm' },
687 { "search", 1, 0, 's' },
688 { "index", 0, 0, 'i' },
689 { "output", 1, 0, 'o' },
690 { "default-search", 1, 0, 'a' },
694 struct time_criterion {
696 int start_hour, end_hour;
700 /*********************************************************************/
702 static struct time_criterion time_criteria[] = {
705 { "today", 24, -1, -1 },
706 { "24h", 24, -1, -1 },
707 { "week", 24 * 7, -1, -1 },
708 { "month", 24 * 31, -1, -1 },
709 { "year", 24 * 365, -1, -1 },
711 { "yesterday", 48, 24, -1 },
713 { "monday", -1, -1, 1 },
714 { "tuesday", -1, -1, 2 },
715 { "wednesday", -1, -1, 3 },
716 { "thursday", -1, -1, 4 },
717 { "friday", -1, -1, 5 },
718 { "saturday", -1, -1, 6 },
719 { "sunday", -1, -1, 7 },
723 /*********************************************************************/
725 time_t time_for_past_day(int day) {
731 delta_day = (7 + tm->tm_wday - day) % 7;
732 if(delta_day == 0) { delta_day = 7; }
733 return t - (delta_day * 3600 * 24 + tm->tm_sec + 60 * tm->tm_min + 3600 * tm->tm_hour);
736 void init_condition(struct search_condition *condition, const char *full_string,
737 const char *default_search_field) {
738 char full_search_field[TOKEN_BUFFER_SIZE], *search_field;
742 string = parse_token(full_search_field, TOKEN_BUFFER_SIZE, ' ', full_string);
743 search_field = full_search_field;
745 if(search_field[0] == '!') {
747 condition->negation = 1;
749 condition->negation = 0;
752 condition->field_id = -1;
756 for(k = 0; k < sizeof(time_criteria) / sizeof(struct time_criterion); k++) {
757 if(strcmp(time_criteria[k].label, search_field) == 0) {
758 condition->field_id = ID_INTERVAL;
759 if(time_criteria[k].past_week_day < 0) {
760 condition->interval_start = time(0) - 3600 * time_criteria[k].start_hour;
761 if(time_criteria[k].end_hour >= 0) {
762 condition->interval_stop = time(0) - 3600 * time_criteria[k].end_hour;
764 condition->interval_stop = 0;
767 condition->interval_start = time_for_past_day(time_criteria[k].past_week_day);
768 condition->interval_stop = condition->interval_start + 3600 * 24;
774 if(condition->field_id == -1) {
776 /* No time condition matched, look for the search fields */
778 for(m = 0; (m < MAX_ID) && condition->field_id == -1; m++) {
779 if(strncmp(field_names[m], search_field, strlen(search_field)) == 0) {
780 condition->field_id = m;
784 /* None match, if there is a default search field, re-run the search with it */
786 if(condition->field_id == -1) {
787 if(default_search_field) {
788 for(m = 0; (m < MAX_ID) && condition->field_id == -1; m++) {
789 if(strncmp(field_names[m],
790 default_search_field, strlen(default_search_field)) == 0) {
791 condition->field_id = m;
794 string = full_string;
798 if(condition->field_id == -1) {
800 "mymail: Syntax error in field name \"%s\".\n",
805 if(regcomp(&condition->regexp,
809 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
811 field_names[condition->field_id]);
817 void free_condition(struct search_condition *condition) {
818 if(condition->field_id != ID_INTERVAL) {
819 regfree(&condition->regexp);
823 /*********************************************************************/
824 /*********************************************************************/
825 /*********************************************************************/
827 int main(int argc, char **argv) {
828 char *db_filename = 0;
829 char *db_filename_regexp_string = 0;
830 char *db_root_path = 0;
831 char *db_filename_list = 0;
832 char *mbox_filename_regexp_string = 0;
833 char *default_search_field;
834 char output_filename[PATH_MAX + 1];
835 int action_index = 0;
836 int error = 0, show_help = 0;
837 const unsigned int nb_fields_to_parse =
838 sizeof(fields_to_parse) / sizeof(struct parsable_field);
841 unsigned int nb_search_conditions;
842 struct search_condition search_conditions[MAX_NB_SEARCH_CONDITIONS];
844 if(regcomp(&leading_from_line_regexp, LEADING_FROM_LINE_REGEXP_STRING, 0)) {
846 "mymail: Cannot compile leading \"from\" line regexp. That is strange.\n");
852 default_search_field = 0;
853 ignore_dot_files = 1;
854 strncpy(output_filename, "", PATH_MAX);
856 setlocale(LC_ALL, "");
858 nb_search_conditions = 0;
860 while ((c = getopt_long(argc, argv, "hvqip:s:d:r:l:o:a:m:",
861 long_options, NULL)) != -1) {
870 print_version(stdout);
883 fprintf(stderr, "mymail: Can not set the db filename twice.\n");
886 db_filename = strdup(optarg);
890 if(db_filename_regexp_string) {
891 fprintf(stderr, "mymail: Can not set the db filename pattern twice.\n");
894 db_filename_regexp_string = strdup(optarg);
898 if(mbox_filename_regexp_string) {
899 fprintf(stderr, "mymail: Can not set the mbox filename pattern twice.\n");
902 mbox_filename_regexp_string = strdup(optarg);
906 strncpy(output_filename, optarg, PATH_MAX);
911 fprintf(stderr, "mymail: Can not set the db root path twice.\n");
914 db_root_path = strdup(optarg);
918 if(db_filename_list) {
919 fprintf(stderr, "mymail: Can not set the db filename list twice.\n");
922 db_filename_list = strdup(optarg);
926 if(nb_search_conditions == MAX_NB_SEARCH_CONDITIONS) {
927 fprintf(stderr, "mymail: Too many search patterns.\n");
930 init_condition(&search_conditions[nb_search_conditions], optarg, default_search_field);
931 nb_search_conditions++;
935 default_search_field = optarg;
944 /* Set all the values that may defined in the arguments, through
945 environment variables, or hard-coded */
947 db_filename = default_value(db_filename,
951 db_filename_regexp_string = default_value(db_filename_regexp_string,
955 db_root_path = default_value(db_root_path,
959 db_filename_list = default_value(db_filename_list,
963 mbox_filename_regexp_string = default_value(mbox_filename_regexp_string,
964 "MYMAIL_MBOX_PATTERN",
967 /* Start the processing */
983 regex_t mbox_filename_regexp_static;
984 regex_t *mbox_filename_regexp;
986 if(mbox_filename_regexp_string) {
987 if(regcomp(&mbox_filename_regexp_static,
988 mbox_filename_regexp_string,
991 "mymail: Syntax error in regexp \"%s\".\n",
992 mbox_filename_regexp_string);
995 mbox_filename_regexp = &mbox_filename_regexp_static;
997 mbox_filename_regexp = 0;
1000 db_file = safe_fopen(db_filename, "w", "index file for indexing");
1002 for(f = 0; f < nb_fields_to_parse; f++) {
1003 if(regcomp(&fields_to_parse[f].regexp,
1004 fields_to_parse[f].regexp_string,
1005 fields_to_parse[f].cflags)) {
1007 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
1008 fields_to_parse[f].regexp_string,
1009 field_names[fields_to_parse[f].id]);
1014 fprintf(db_file, "%s version_%s raw\n", MYMAIL_DB_MAGIC_TOKEN, VERSION);
1016 while(optind < argc) {
1017 recursive_index_mbox(db_file,
1018 argv[optind], mbox_filename_regexp,
1019 nb_fields_to_parse, fields_to_parse);
1026 if(mbox_filename_regexp) {
1027 regfree(mbox_filename_regexp);
1030 for(f = 0; f < nb_fields_to_parse; f++) {
1031 regfree(&fields_to_parse[f].regexp);
1040 int nb_extracted_mails = 0;
1042 if(output_filename[0]) {
1043 output_file = safe_fopen(output_filename, "w", "result mbox");
1045 output_file = stdout;
1049 if(nb_search_conditions > 0) {
1051 /* Recursive search if db_root_path is set */
1054 regex_t db_filename_regexp;
1055 if(regcomp(&db_filename_regexp,
1056 db_filename_regexp_string,
1059 "mymail: Syntax error in regexp \"%s\".\n",
1060 db_filename_regexp_string);
1064 nb_extracted_mails += recursive_search_in_db(db_root_path, &db_filename_regexp,
1065 nb_search_conditions, search_conditions,
1068 regfree(&db_filename_regexp);
1071 /* Search in all db files listed in db_filename_list */
1073 if(db_filename_list) {
1074 char db_filename[PATH_MAX + 1];
1077 s = db_filename_list;
1080 s = parse_token(db_filename, PATH_MAX + 1, ';', s);
1082 if(db_filename[0]) {
1083 nb_extracted_mails +=
1084 search_in_db(db_filename, nb_search_conditions, search_conditions, output_file);
1089 /* Search in all db files listed in the command arguments */
1091 while(optind < argc) {
1092 nb_extracted_mails +=
1093 search_in_db(argv[optind], nb_search_conditions, search_conditions, output_file);
1099 if(nb_extracted_mails > 0) {
1100 printf("Found %d matching mails.\n", nb_extracted_mails);
1102 printf("No matching mail found.\n");
1106 fflush(output_file);
1108 if(output_file != stdout) {
1109 fclose(output_file);
1113 for(n = 0; n < nb_search_conditions; n++) {
1114 free_condition(&search_conditions[n]);
1118 free(db_filename_regexp_string);
1120 free(db_filename_list);
1121 free(mbox_filename_regexp_string);
1123 regfree(&leading_from_line_regexp);