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 {
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");
210 fprintf(out, " -a <search field>, --default-search <search field>\n");
211 fprintf(out, " set the default search field\n");
214 /*********************************************************************/
216 time_t time_for_past_day(int day) {
222 delta_day = (7 + tm->tm_wday - day) % 7 + 1;
223 return t - delta_day * 3600 * 24 + tm->tm_sec + 60 * tm->tm_min + 3600 * tm->tm_hour;
226 /*********************************************************************/
228 int ignore_entry(const char *name) {
230 /* strcmp(name, ".") == 0 || */
231 /* strcmp(name, "..") == 0 || */
232 (name[0] == '.' && name[1] != '/');
235 int is_a_leading_from_line(char *s) {
236 return strncmp(s, "From ", 5) == 0 &&
237 regexec(&leading_from_line_regexp, s, 0, 0, 0) == 0;
240 int mbox_line_match_search(struct search_condition *condition,
241 int mbox_id, char *mbox_value) {
243 if(condition->field_id == ID_INTERVAL) {
244 if(mbox_id == ID_LEADING_LINE) {
250 while(*c && *c != ' ') c++; while(*c && *c == ' ') c++;
251 strptime(c, "%a %b %e %k:%M:%S %Y", &tm);
254 return (t >= condition->interval_start &&
255 (condition->interval_stop == 0 ||
256 t <= condition->interval_stop));
264 (condition->field_id == mbox_id)
268 (condition->field_id == ID_PARTICIPANT && (mbox_id == ID_LEADING_LINE ||
269 mbox_id == ID_FROM ||
273 (condition->field_id == ID_FROM && mbox_id == ID_LEADING_LINE)
277 regexec(&condition->regexp, mbox_value, 0, 0, 0) == 0;
281 void update_body_hits(char *mail_filename, int position_in_mail,
282 int nb_search_conditions, struct search_condition *search_conditions,
283 int nb_body_conditions,
287 int last_mbox_line_was_empty;
288 char raw_mbox_line[BUFFER_SIZE];
294 mail_file = fopen(mail_filename, "r");
298 "mymail: Cannot open mbox '%s' for body scan.\n",
303 fseek(mail_file, position_in_mail, SEEK_SET);
305 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
306 while(nb_body_hits < nb_body_conditions) {
307 last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
309 if(last_mbox_line_was_empty) { header = 0; }
312 for(n = 0; n < nb_search_conditions; n++) {
313 if(search_conditions[n].field_id == ID_BODY && !hits[n]) {
315 (regexec(&search_conditions[n].regexp, raw_mbox_line, 0, 0, 0) == 0);
323 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
324 (last_mbox_line_was_empty &&
325 is_a_leading_from_line(raw_mbox_line)))
333 void search_in_db(const char *db_filename,
334 int nb_search_conditions,
335 struct search_condition *search_conditions,
338 int hits[MAX_NB_SEARCH_CONDITIONS];
339 char raw_db_line[BUFFER_SIZE];
340 char raw_mbox_line[BUFFER_SIZE];
341 char current_mail_filename[PATH_MAX + 1];
342 unsigned long int current_position_in_mail;
343 char mbox_name[TOKEN_BUFFER_SIZE], *mbox_value;
345 int already_written, m, n;
346 int last_mbox_line_was_empty;
347 int nb_body_conditions, nb_fulfilled_body_conditions;
351 printf("Searching in '%s' ... ", db_filename);
355 db_file = fopen(db_filename, "r");
359 "mymail: Cannot open \"%s\" for reading: %s\n",
365 if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
366 if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
368 "mymail: Header line in '%s' does not match the mymail db format.\n",
374 "mymail: Cannot read the header line in '%s'.\n",
379 current_position_in_mail = 0;
382 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
384 nb_body_conditions = 0;
385 for(n = 0; n < nb_search_conditions; n++) {
386 if(search_conditions[n].field_id == ID_BODY) {
387 nb_body_conditions++;
391 strcpy(current_mail_filename, "");
393 while(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
394 mbox_value = parse_token(mbox_name, TOKEN_BUFFER_SIZE, ' ', raw_db_line);
396 if(strcmp("mail", mbox_name) == 0) {
397 char position_in_file_string[TOKEN_BUFFER_SIZE];
399 if(current_mail_filename[0]) {
401 /* We first check all conditions but the body ones */
403 for(n = 0; n < nb_search_conditions &&
404 ((search_conditions[n].field_id == ID_BODY) ||
405 xor(hits[n], search_conditions[n].negation)); n++);
407 if(n == nb_search_conditions) {
409 /* all conditions but the body ones are fine, check the body
412 if(nb_body_conditions > 0) {
413 update_body_hits(current_mail_filename, current_position_in_mail,
414 nb_search_conditions, search_conditions,
419 nb_fulfilled_body_conditions = 0;
421 for(n = 0; n < nb_search_conditions; n++) {
422 if(search_conditions[n].field_id == ID_BODY &&
423 xor(hits[n], search_conditions[n].negation)) {
424 nb_fulfilled_body_conditions++;
428 if(nb_body_conditions == nb_fulfilled_body_conditions) {
431 mail_file = fopen(current_mail_filename, "r");
435 "mymail: Cannot open mbox '%s' for mail extraction.\n",
436 current_mail_filename);
440 fseek(mail_file, current_position_in_mail, SEEK_SET);
442 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
443 last_mbox_line_was_empty = 0;
444 fprintf(output_file, "%s", raw_mbox_line);
446 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
447 (last_mbox_line_was_empty &&
448 is_a_leading_from_line(raw_mbox_line))
451 last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
452 fprintf(output_file, "%s", raw_mbox_line);
461 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
463 mbox_value = parse_token(position_in_file_string, TOKEN_BUFFER_SIZE, ' ', mbox_value);
464 mbox_value = parse_token(current_mail_filename, TOKEN_BUFFER_SIZE, ' ', mbox_value);
465 current_position_in_mail = atol(position_in_file_string);
466 remove_eof(current_mail_filename);
472 for(m = 0; (m < MAX_ID) && mbox_id == -1; m++) {
473 if(strncmp(field_names[m], mbox_name, strlen(mbox_name)) == 0) {
477 for(n = 0; n < nb_search_conditions; n++) {
478 hits[n] |= mbox_line_match_search(&search_conditions[n],
479 mbox_id, mbox_value);
492 void recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp,
493 int nb_search_conditions,
494 struct search_condition *search_conditions,
497 struct dirent *dir_e;
499 char subname[PATH_MAX + 1];
501 if(lstat(entry_name, &sb) != 0) {
503 "mymail: Cannot stat \"%s\": %s\n",
509 dir = opendir(entry_name);
512 while((dir_e = readdir(dir))) {
513 if(!ignore_entry(dir_e->d_name)) {
514 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
515 recursive_search_in_db(subname, db_filename_regexp,
516 nb_search_conditions, search_conditions,
524 const char *s = entry_name, *filename = entry_name;
525 while(*s) { if(*s == '/') { filename = s+1; } s++; }
527 if(regexec(db_filename_regexp, filename, 0, 0, 0) == 0) {
528 search_in_db(entry_name, nb_search_conditions, search_conditions, output_file);
533 /*********************************************************************/
535 void index_one_mbox_line(int nb_fields_to_parse, struct parsable_field *fields_to_parse,
536 char *raw_mbox_line, FILE *db_file) {
539 for(f = 0; f < nb_fields_to_parse; f++) {
540 if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
541 fprintf(db_file, "%s %s\n",
542 field_names[fields_to_parse[f].id],
543 raw_mbox_line + matches.rm_eo);
548 void index_mbox(const char *mbox_filename,
549 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
551 char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
552 char *end_of_full_line;
554 int in_header, new_header, last_mbox_line_was_empty;
555 unsigned long int position_in_file;
557 file = fopen(mbox_filename, "r");
560 fprintf(stderr, "mymail: Cannot open '%s'.\n", mbox_filename);
561 if(paranoid) { exit(EXIT_FAILURE); }
568 position_in_file = 0;
569 end_of_full_line = 0;
571 last_mbox_line_was_empty = 1;
573 while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
574 if(last_mbox_line_was_empty &&
575 is_a_leading_from_line(raw_mbox_line)) {
578 "Got a ^\"From \" in the header in %s:%lu.\n",
579 mbox_filename, position_in_file);
580 fprintf(stderr, "%s", raw_mbox_line);
581 if(paranoid) { exit(EXIT_FAILURE); }
585 } else if(raw_mbox_line[0] == '\n') {
586 if(in_header) { in_header = 0; }
589 last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
593 fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
597 if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
598 char *start = raw_mbox_line;
599 while(*start == ' ' || *start == '\t') start++;
600 *(end_of_full_line++) = ' ';
601 strcpy(end_of_full_line, start);
602 while(*end_of_full_line && *end_of_full_line != '\n') {
605 *end_of_full_line = '\0';
610 if(!((raw_mbox_line[0] >= 'a' && raw_mbox_line[0] <= 'z') ||
611 (raw_mbox_line[0] >= 'A' && raw_mbox_line[0] <= 'Z'))) {
613 "Header line syntax error %s:%lu.\n",
614 mbox_filename, position_in_file);
615 fprintf(stderr, "%s", raw_mbox_line);
620 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
623 end_of_full_line = full_line;
624 strcpy(end_of_full_line, raw_mbox_line);
625 while(*end_of_full_line && *end_of_full_line != '\n') {
628 *end_of_full_line = '\0';
633 position_in_file += strlen(raw_mbox_line);
639 void recursive_index_mbox(FILE *db_file,
640 const char *entry_name,
641 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
643 struct dirent *dir_e;
645 char subname[PATH_MAX + 1];
647 if(lstat(entry_name, &sb) != 0) {
649 "mymail: Cannot stat \"%s\": %s\n",
655 dir = opendir(entry_name);
658 while((dir_e = readdir(dir))) {
659 if(!ignore_entry(dir_e->d_name)) {
660 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
661 recursive_index_mbox(db_file, subname, nb_fields_to_parse, fields_to_parse);
666 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", 1, 0, 'd' },
683 { "db-pattern", 1, 0, 'p' },
684 { "db-root", 1, 0, 'r' },
685 { "db-list", 1, 0, 'l' },
686 { "search", 1, 0, 's' },
687 { "index", 0, 0, 'i' },
688 { "output", 1, 0, 'o' },
689 { "default-search", 1, 0, 'a' },
693 /*********************************************************************/
695 void init_condition(struct search_condition *condition, char *full_string) {
696 char full_search_field[TOKEN_BUFFER_SIZE], *search_field;
700 string = parse_token(full_search_field, TOKEN_BUFFER_SIZE, ' ', full_string);
701 search_field = full_search_field;
703 if(search_field[0] == '!') {
705 condition->negation = 1;
707 condition->negation = 0;
712 if(strcmp(search_field, "8h") == 0) {
713 condition->field_id = ID_INTERVAL;
714 condition->interval_start = time(0) - 3600 * 8;
715 condition->interval_stop = 0;
718 else if(strcmp(search_field, "week") == 0) {
719 condition->field_id = ID_INTERVAL;
720 condition->interval_start = time(0) - 3600 * 24 * 7;
721 condition->interval_stop = 0;
724 else if(strcmp(search_field, "month") == 0) {
725 condition->field_id = ID_INTERVAL;
726 condition->interval_start = time(0) - 3600 * 24 * 31;
727 condition->interval_stop = 0;
730 else if(strcmp(search_field, "24h") == 0 ||
731 strcmp(search_field, "today") == 0) {
732 condition->field_id = ID_INTERVAL;
733 condition->interval_start = time(0) - 3600 * 24;
734 condition->interval_stop = 0;
739 else if(strcmp(search_field, "yesterday") == 0) {
740 condition->field_id = ID_INTERVAL;
741 condition->interval_start = time(0) - 2 * 3600 * 24;
742 condition->interval_stop = condition->interval_start + 3600 * 24;
747 else if(strcmp(search_field, "monday") == 0) {
748 condition->field_id = ID_INTERVAL;
749 condition->interval_start = time_for_past_day(1);
750 condition->interval_stop = condition->interval_start + 3600 * 24;
753 else if(strcmp(search_field, "tuesday") == 0) {
754 condition->field_id = ID_INTERVAL;
755 condition->interval_start = time_for_past_day(2);
756 condition->interval_stop = condition->interval_start + 3600 * 24;
759 else if(strcmp(search_field, "wednesday") == 0) {
760 condition->field_id = ID_INTERVAL;
761 condition->interval_start = time_for_past_day(3);
762 condition->interval_stop = condition->interval_start + 3600 * 24;
765 else if(strcmp(search_field, "thursday") == 0) {
766 condition->field_id = ID_INTERVAL;
767 condition->interval_start = time_for_past_day(4);
768 condition->interval_stop = condition->interval_start + 3600 * 24;
771 else if(strcmp(search_field, "friday") == 0) {
772 condition->field_id = ID_INTERVAL;
773 condition->interval_start = time_for_past_day(5);
774 condition->interval_stop = condition->interval_start + 3600 * 24;
777 else if(strcmp(search_field, "saturday") == 0) {
778 condition->field_id = ID_INTERVAL;
779 condition->interval_start = time_for_past_day(6);
780 condition->interval_stop = condition->interval_start + 3600 * 24;
783 else if(strcmp(search_field, "sunday") == 0) {
784 condition->field_id = ID_INTERVAL;
785 condition->interval_start = time_for_past_day(7);
786 condition->interval_stop = condition->interval_start + 3600 * 24;
791 /* header-related conditions */
793 condition->field_id = -1;
795 for(m = 0; (m < MAX_ID) && condition->field_id == -1; m++) {
796 if(strncmp(field_names[m], search_field, strlen(search_field)) == 0) {
797 condition->field_id = m;
801 if(condition->field_id == -1) {
802 if(default_search_field) {
803 for(m = 0; (m < MAX_ID) && condition->field_id == -1; m++) {
804 if(strncmp(field_names[m],
805 default_search_field, strlen(default_search_field)) == 0) {
806 condition->field_id = m;
809 string = full_string;
813 if(condition->field_id == -1) {
815 "mymail: Syntax error in field name \"%s\".\n",
820 if(regcomp(&condition->regexp,
824 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
826 field_names[condition->field_id]);
832 void free_condition(struct search_condition *condition) {
833 if(condition->field_id != ID_INTERVAL) {
834 regfree(&condition->regexp);
838 /*********************************************************************/
839 /*********************************************************************/
840 /*********************************************************************/
842 int main(int argc, char **argv) {
844 char *db_filename_regexp_string;
846 char *db_filename_list;
847 char output_filename[PATH_MAX + 1];
849 int error = 0, show_help = 0;
850 const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
853 int nb_search_conditions;
855 struct search_condition search_conditions[MAX_NB_SEARCH_CONDITIONS];
857 if(regcomp(&leading_from_line_regexp,
858 "^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$",
861 "mymail: Cannot compile leading \"from\" line regexp. That is strange.\n");
868 db_filename_regexp_string = 0;
870 db_filename_list = 0;
872 default_search_field = 0;
874 setlocale(LC_ALL, "");
876 nb_search_conditions = 0;
878 while ((c = getopt_long(argc, argv, "hvqip:s:d:r:l:o:a:",
879 long_options, NULL)) != -1) {
888 print_version(stdout);
900 db_filename = strdup(optarg);
904 db_filename_regexp_string = strdup(optarg);
908 strncpy(output_filename, optarg, PATH_MAX);
912 db_root_path = strdup(optarg);
916 db_filename_list = strdup(optarg);
920 if(nb_search_conditions == MAX_NB_SEARCH_CONDITIONS) {
921 fprintf(stderr, "mymail: Too many search patterns.\n");
924 init_condition(&search_conditions[nb_search_conditions], optarg);
925 nb_search_conditions++;
929 default_search_field = optarg;
939 char *default_db_filename = getenv("MYMAIL_DB_FILE");
941 if(!default_db_filename) {
942 default_db_filename = "mymail.db";
945 db_filename = strdup(default_db_filename);
948 if(!db_filename_regexp_string) {
949 char *default_db_filename_regexp_string = getenv("MYMAIL_DB_PATTERN");
951 if(!default_db_filename_regexp_string) {
952 default_db_filename_regexp_string = "^mymail.db$";
955 db_filename_regexp_string = strdup(default_db_filename_regexp_string);
959 char *default_db_root_path = getenv("MYMAIL_DB_ROOT");
961 if(default_db_root_path) {
962 db_root_path = strdup(default_db_root_path);
966 if(!db_filename_list) {
967 char *default_db_filename_list = getenv("MYMAIL_DB_LIST");
969 if(default_db_filename_list) {
970 db_filename_list = strdup(default_db_filename_list);
974 if(output_filename[0]) {
975 output_file = fopen(output_filename, "w");
979 "mymail: Cannot open result file \"%s\" for writing: %s\n",
985 output_file = stdout;
1002 db_file = fopen(db_filename, "w");
1006 "mymail: Cannot open \"%s\" for writing: %s\n",
1012 for(f = 0; f < nb_fields_to_parse; f++) {
1013 if(regcomp(&fields_to_parse[f].regexp,
1014 fields_to_parse[f].regexp_string,
1017 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
1018 fields_to_parse[f].regexp_string,
1019 field_names[fields_to_parse[f].id]);
1024 fprintf(db_file, "%s version_%s raw\n", MYMAIL_DB_MAGIC_TOKEN, VERSION);
1026 while(optind < argc) {
1027 recursive_index_mbox(db_file,
1029 nb_fields_to_parse, fields_to_parse);
1036 for(f = 0; f < nb_fields_to_parse; f++) {
1037 regfree(&fields_to_parse[f].regexp);
1043 if(nb_search_conditions > 0) {
1045 /* Recursive search if db_root_path is set */
1048 regex_t db_filename_regexp;
1049 if(regcomp(&db_filename_regexp,
1050 db_filename_regexp_string,
1053 "mymail: Syntax error in regexp \"%s\".\n",
1054 db_filename_regexp_string);
1058 recursive_search_in_db(db_root_path, &db_filename_regexp,
1059 nb_search_conditions, search_conditions,
1062 regfree(&db_filename_regexp);
1065 /* Search in all db files listed in db_filename_list */
1067 if(db_filename_list) {
1068 char db_filename[PATH_MAX + 1];
1071 s = db_filename_list;
1074 s = parse_token(db_filename, PATH_MAX + 1, ';', s);
1076 if(db_filename[0]) {
1077 search_in_db(db_filename, nb_search_conditions, search_conditions, output_file);
1082 /* Search in all db files listed in the command arguments */
1084 while(optind < argc) {
1085 search_in_db(argv[optind], nb_search_conditions, search_conditions, output_file);
1091 for(n = 0; n < nb_search_conditions; n++) {
1092 free_condition(&search_conditions[n]);
1095 if(output_file != stdout) {
1096 fflush(output_file);
1097 fclose(output_file);
1101 free(db_filename_regexp_string);
1103 free(db_filename_list);
1105 regfree(&leading_from_line_regexp);