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(int last_mbox_line_was_empty, char *mbox_line) {
240 The mbox man page in qmail documentation states:
242 > The reader should not attempt to take advantage of the fact
243 > that every From_ line (past the beginning of the file) is
244 > preceded by a blank line.
248 /* last_mbox_line_was_empty && */
249 strncmp(mbox_line, "From ", 5) == 0 &&
250 regexec(&leading_from_line_regexp, mbox_line, 0, 0, 0) == 0;
253 int mbox_line_match_search(struct search_condition *condition,
254 int mbox_id, char *mbox_value) {
256 if(condition->field_id == ID_INTERVAL) {
257 if(mbox_id == ID_LEADING_LINE) {
263 while(*c && *c != ' ') c++; while(*c && *c == ' ') c++;
264 strptime(c, "%a %b %e %k:%M:%S %Y", &tm);
267 return (t >= condition->interval_start &&
268 (condition->interval_stop == 0 ||
269 t <= condition->interval_stop));
277 (condition->field_id == mbox_id)
281 (condition->field_id == ID_PARTICIPANT && (mbox_id == ID_LEADING_LINE ||
282 mbox_id == ID_FROM ||
286 (condition->field_id == ID_FROM && mbox_id == ID_LEADING_LINE)
290 regexec(&condition->regexp, mbox_value, 0, 0, 0) == 0;
294 void update_body_hits(char *mail_filename, int position_in_mail,
295 int nb_search_conditions, struct search_condition *search_conditions,
296 int nb_body_conditions,
300 int last_mbox_line_was_empty;
301 char raw_mbox_line[BUFFER_SIZE];
307 mail_file = fopen(mail_filename, "r");
311 "mymail: Cannot open mbox '%s' for body scan.\n",
316 fseek(mail_file, position_in_mail, SEEK_SET);
318 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
319 while(nb_body_hits < nb_body_conditions) {
320 last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
322 if(last_mbox_line_was_empty) { header = 0; }
325 for(n = 0; n < nb_search_conditions; n++) {
326 if(search_conditions[n].field_id == ID_BODY && !hits[n]) {
328 (regexec(&search_conditions[n].regexp, raw_mbox_line, 0, 0, 0) == 0);
336 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
337 (is_a_leading_from_line(last_mbox_line_was_empty, raw_mbox_line)))
345 void search_in_db(const char *db_filename,
346 int nb_search_conditions,
347 struct search_condition *search_conditions,
350 int hits[MAX_NB_SEARCH_CONDITIONS];
351 char raw_db_line[BUFFER_SIZE];
352 char raw_mbox_line[BUFFER_SIZE];
353 char current_mail_filename[PATH_MAX + 1];
354 unsigned long int current_position_in_mail;
355 char mbox_name[TOKEN_BUFFER_SIZE], *mbox_value;
357 int already_written, m, n;
358 int last_mbox_line_was_empty;
359 int nb_body_conditions, nb_fulfilled_body_conditions;
363 printf("Searching in '%s' ... ", db_filename);
367 db_file = fopen(db_filename, "r");
371 "mymail: Cannot open \"%s\" for reading: %s\n",
377 if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
378 if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
380 "mymail: Header line in '%s' does not match the mymail db format.\n",
386 "mymail: Cannot read the header line in '%s'.\n",
391 current_position_in_mail = 0;
394 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
396 nb_body_conditions = 0;
397 for(n = 0; n < nb_search_conditions; n++) {
398 if(search_conditions[n].field_id == ID_BODY) {
399 nb_body_conditions++;
403 strcpy(current_mail_filename, "");
405 while(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
406 mbox_value = parse_token(mbox_name, TOKEN_BUFFER_SIZE, ' ', raw_db_line);
408 if(strcmp("mail", mbox_name) == 0) {
409 char position_in_file_string[TOKEN_BUFFER_SIZE];
411 if(current_mail_filename[0]) {
413 /* We first check all conditions but the body ones */
415 for(n = 0; n < nb_search_conditions &&
416 ((search_conditions[n].field_id == ID_BODY) ||
417 xor(hits[n], search_conditions[n].negation)); n++);
419 if(n == nb_search_conditions) {
421 /* all conditions but the body ones are fine, check the body
424 if(nb_body_conditions > 0) {
425 update_body_hits(current_mail_filename, current_position_in_mail,
426 nb_search_conditions, search_conditions,
431 nb_fulfilled_body_conditions = 0;
433 for(n = 0; n < nb_search_conditions; n++) {
434 if(search_conditions[n].field_id == ID_BODY &&
435 xor(hits[n], search_conditions[n].negation)) {
436 nb_fulfilled_body_conditions++;
440 if(nb_body_conditions == nb_fulfilled_body_conditions) {
443 mail_file = fopen(current_mail_filename, "r");
447 "mymail: Cannot open mbox '%s' for mail extraction.\n",
448 current_mail_filename);
452 fseek(mail_file, current_position_in_mail, SEEK_SET);
454 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
455 last_mbox_line_was_empty = 0;
456 fprintf(output_file, "%s", raw_mbox_line);
458 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
459 (is_a_leading_from_line(last_mbox_line_was_empty, raw_mbox_line))
462 last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
463 fprintf(output_file, "%s", raw_mbox_line);
472 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
474 mbox_value = parse_token(position_in_file_string, TOKEN_BUFFER_SIZE, ' ', mbox_value);
475 mbox_value = parse_token(current_mail_filename, TOKEN_BUFFER_SIZE, ' ', mbox_value);
476 current_position_in_mail = atol(position_in_file_string);
477 remove_eof(current_mail_filename);
483 for(m = 0; (m < MAX_ID) && mbox_id == -1; m++) {
484 if(strncmp(field_names[m], mbox_name, strlen(mbox_name)) == 0) {
488 for(n = 0; n < nb_search_conditions; n++) {
489 hits[n] |= mbox_line_match_search(&search_conditions[n],
490 mbox_id, mbox_value);
503 void recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp,
504 int nb_search_conditions,
505 struct search_condition *search_conditions,
508 struct dirent *dir_e;
510 char subname[PATH_MAX + 1];
512 if(lstat(entry_name, &sb) != 0) {
514 "mymail: Cannot stat \"%s\": %s\n",
520 dir = opendir(entry_name);
523 while((dir_e = readdir(dir))) {
524 if(!ignore_entry(dir_e->d_name)) {
525 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
526 recursive_search_in_db(subname, db_filename_regexp,
527 nb_search_conditions, search_conditions,
535 const char *s = entry_name, *filename = entry_name;
536 while(*s) { if(*s == '/') { filename = s+1; } s++; }
538 if(regexec(db_filename_regexp, filename, 0, 0, 0) == 0) {
539 search_in_db(entry_name, nb_search_conditions, search_conditions, output_file);
544 /*********************************************************************/
546 void index_one_mbox_line(int nb_fields_to_parse, struct parsable_field *fields_to_parse,
547 char *raw_mbox_line, FILE *db_file) {
550 for(f = 0; f < nb_fields_to_parse; f++) {
551 if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
552 fprintf(db_file, "%s %s\n",
553 field_names[fields_to_parse[f].id],
554 raw_mbox_line + matches.rm_eo);
559 void index_mbox(const char *mbox_filename,
560 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
562 char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
563 char *end_of_full_line;
565 int in_header, new_header, last_mbox_line_was_empty;
566 unsigned long int position_in_file;
568 file = fopen(mbox_filename, "r");
571 fprintf(stderr, "mymail: Cannot open '%s'.\n", mbox_filename);
572 if(paranoid) { exit(EXIT_FAILURE); }
579 position_in_file = 0;
580 end_of_full_line = 0;
582 last_mbox_line_was_empty = 1;
584 while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
585 if(is_a_leading_from_line(last_mbox_line_was_empty, raw_mbox_line)) {
588 "Got a ^\"From \" in the header in %s:%lu.\n",
589 mbox_filename, position_in_file);
590 fprintf(stderr, "%s", raw_mbox_line);
591 if(paranoid) { exit(EXIT_FAILURE); }
595 } else if(raw_mbox_line[0] == '\n') {
596 if(in_header) { in_header = 0; }
599 last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
603 fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
607 if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
608 char *start = raw_mbox_line;
609 while(*start == ' ' || *start == '\t') start++;
610 *(end_of_full_line++) = ' ';
611 strcpy(end_of_full_line, start);
612 while(*end_of_full_line && *end_of_full_line != '\n') {
615 *end_of_full_line = '\0';
620 if(!((raw_mbox_line[0] >= 'a' && raw_mbox_line[0] <= 'z') ||
621 (raw_mbox_line[0] >= 'A' && raw_mbox_line[0] <= 'Z'))) {
623 "Header line syntax error %s:%lu.\n",
624 mbox_filename, position_in_file);
625 fprintf(stderr, "%s", raw_mbox_line);
630 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
633 end_of_full_line = full_line;
634 strcpy(end_of_full_line, raw_mbox_line);
635 while(*end_of_full_line && *end_of_full_line != '\n') {
638 *end_of_full_line = '\0';
643 position_in_file += strlen(raw_mbox_line);
649 void recursive_index_mbox(FILE *db_file,
650 const char *entry_name,
651 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
653 struct dirent *dir_e;
655 char subname[PATH_MAX + 1];
657 if(lstat(entry_name, &sb) != 0) {
659 "mymail: Cannot stat \"%s\": %s\n",
665 dir = opendir(entry_name);
668 while((dir_e = readdir(dir))) {
669 if(!ignore_entry(dir_e->d_name)) {
670 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
671 recursive_index_mbox(db_file, subname, nb_fields_to_parse, fields_to_parse);
676 index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
680 /*********************************************************************/
682 /* For long options that have no equivalent short option, use a
683 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
685 OPT_BASH_MODE = CHAR_MAX + 1
688 static struct option long_options[] = {
689 { "help", no_argument, 0, 'h' },
690 { "version", no_argument, 0, 'v' },
691 { "quiet", no_argument, 0, 'q' },
692 { "db-file", 1, 0, 'd' },
693 { "db-pattern", 1, 0, 'p' },
694 { "db-root", 1, 0, 'r' },
695 { "db-list", 1, 0, 'l' },
696 { "search", 1, 0, 's' },
697 { "index", 0, 0, 'i' },
698 { "output", 1, 0, 'o' },
699 { "default-search", 1, 0, 'a' },
703 /*********************************************************************/
705 void init_condition(struct search_condition *condition, char *full_string) {
706 char full_search_field[TOKEN_BUFFER_SIZE], *search_field;
710 string = parse_token(full_search_field, TOKEN_BUFFER_SIZE, ' ', full_string);
711 search_field = full_search_field;
713 if(search_field[0] == '!') {
715 condition->negation = 1;
717 condition->negation = 0;
722 if(strcmp(search_field, "8h") == 0) {
723 condition->field_id = ID_INTERVAL;
724 condition->interval_start = time(0) - 3600 * 8;
725 condition->interval_stop = 0;
728 else if(strcmp(search_field, "week") == 0) {
729 condition->field_id = ID_INTERVAL;
730 condition->interval_start = time(0) - 3600 * 24 * 7;
731 condition->interval_stop = 0;
734 else if(strcmp(search_field, "month") == 0) {
735 condition->field_id = ID_INTERVAL;
736 condition->interval_start = time(0) - 3600 * 24 * 31;
737 condition->interval_stop = 0;
740 else if(strcmp(search_field, "24h") == 0 ||
741 strcmp(search_field, "today") == 0) {
742 condition->field_id = ID_INTERVAL;
743 condition->interval_start = time(0) - 3600 * 24;
744 condition->interval_stop = 0;
749 else if(strcmp(search_field, "yesterday") == 0) {
750 condition->field_id = ID_INTERVAL;
751 condition->interval_start = time(0) - 2 * 3600 * 24;
752 condition->interval_stop = condition->interval_start + 3600 * 24;
757 else if(strcmp(search_field, "monday") == 0) {
758 condition->field_id = ID_INTERVAL;
759 condition->interval_start = time_for_past_day(1);
760 condition->interval_stop = condition->interval_start + 3600 * 24;
763 else if(strcmp(search_field, "tuesday") == 0) {
764 condition->field_id = ID_INTERVAL;
765 condition->interval_start = time_for_past_day(2);
766 condition->interval_stop = condition->interval_start + 3600 * 24;
769 else if(strcmp(search_field, "wednesday") == 0) {
770 condition->field_id = ID_INTERVAL;
771 condition->interval_start = time_for_past_day(3);
772 condition->interval_stop = condition->interval_start + 3600 * 24;
775 else if(strcmp(search_field, "thursday") == 0) {
776 condition->field_id = ID_INTERVAL;
777 condition->interval_start = time_for_past_day(4);
778 condition->interval_stop = condition->interval_start + 3600 * 24;
781 else if(strcmp(search_field, "friday") == 0) {
782 condition->field_id = ID_INTERVAL;
783 condition->interval_start = time_for_past_day(5);
784 condition->interval_stop = condition->interval_start + 3600 * 24;
787 else if(strcmp(search_field, "saturday") == 0) {
788 condition->field_id = ID_INTERVAL;
789 condition->interval_start = time_for_past_day(6);
790 condition->interval_stop = condition->interval_start + 3600 * 24;
793 else if(strcmp(search_field, "sunday") == 0) {
794 condition->field_id = ID_INTERVAL;
795 condition->interval_start = time_for_past_day(7);
796 condition->interval_stop = condition->interval_start + 3600 * 24;
801 /* header-related conditions */
803 condition->field_id = -1;
805 for(m = 0; (m < MAX_ID) && condition->field_id == -1; m++) {
806 if(strncmp(field_names[m], search_field, strlen(search_field)) == 0) {
807 condition->field_id = m;
811 if(condition->field_id == -1) {
812 if(default_search_field) {
813 for(m = 0; (m < MAX_ID) && condition->field_id == -1; m++) {
814 if(strncmp(field_names[m],
815 default_search_field, strlen(default_search_field)) == 0) {
816 condition->field_id = m;
819 string = full_string;
823 if(condition->field_id == -1) {
825 "mymail: Syntax error in field name \"%s\".\n",
830 if(regcomp(&condition->regexp,
834 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
836 field_names[condition->field_id]);
842 void free_condition(struct search_condition *condition) {
843 if(condition->field_id != ID_INTERVAL) {
844 regfree(&condition->regexp);
848 /*********************************************************************/
849 /*********************************************************************/
850 /*********************************************************************/
852 int main(int argc, char **argv) {
854 char *db_filename_regexp_string;
856 char *db_filename_list;
857 char output_filename[PATH_MAX + 1];
859 int error = 0, show_help = 0;
860 const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
863 int nb_search_conditions;
865 struct search_condition search_conditions[MAX_NB_SEARCH_CONDITIONS];
867 if(regcomp(&leading_from_line_regexp,
868 "^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$",
871 "mymail: Cannot compile leading \"from\" line regexp. That is strange.\n");
878 db_filename_regexp_string = 0;
880 db_filename_list = 0;
882 default_search_field = 0;
884 setlocale(LC_ALL, "");
886 nb_search_conditions = 0;
888 while ((c = getopt_long(argc, argv, "hvqip:s:d:r:l:o:a:",
889 long_options, NULL)) != -1) {
898 print_version(stdout);
910 db_filename = strdup(optarg);
914 db_filename_regexp_string = strdup(optarg);
918 strncpy(output_filename, optarg, PATH_MAX);
922 db_root_path = strdup(optarg);
926 db_filename_list = strdup(optarg);
930 if(nb_search_conditions == MAX_NB_SEARCH_CONDITIONS) {
931 fprintf(stderr, "mymail: Too many search patterns.\n");
934 init_condition(&search_conditions[nb_search_conditions], optarg);
935 nb_search_conditions++;
939 default_search_field = optarg;
949 char *default_db_filename = getenv("MYMAIL_DB_FILE");
951 if(!default_db_filename) {
952 default_db_filename = "mymail.db";
955 db_filename = strdup(default_db_filename);
958 if(!db_filename_regexp_string) {
959 char *default_db_filename_regexp_string = getenv("MYMAIL_DB_PATTERN");
961 if(!default_db_filename_regexp_string) {
962 default_db_filename_regexp_string = "^mymail.db$";
965 db_filename_regexp_string = strdup(default_db_filename_regexp_string);
969 char *default_db_root_path = getenv("MYMAIL_DB_ROOT");
971 if(default_db_root_path) {
972 db_root_path = strdup(default_db_root_path);
976 if(!db_filename_list) {
977 char *default_db_filename_list = getenv("MYMAIL_DB_LIST");
979 if(default_db_filename_list) {
980 db_filename_list = strdup(default_db_filename_list);
984 if(output_filename[0]) {
985 output_file = fopen(output_filename, "w");
989 "mymail: Cannot open result file \"%s\" for writing: %s\n",
995 output_file = stdout;
1000 print_usage(stderr);
1005 print_usage(stdout);
1012 db_file = fopen(db_filename, "w");
1016 "mymail: Cannot open \"%s\" for writing: %s\n",
1022 for(f = 0; f < nb_fields_to_parse; f++) {
1023 if(regcomp(&fields_to_parse[f].regexp,
1024 fields_to_parse[f].regexp_string,
1027 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
1028 fields_to_parse[f].regexp_string,
1029 field_names[fields_to_parse[f].id]);
1034 fprintf(db_file, "%s version_%s raw\n", MYMAIL_DB_MAGIC_TOKEN, VERSION);
1036 while(optind < argc) {
1037 recursive_index_mbox(db_file,
1039 nb_fields_to_parse, fields_to_parse);
1046 for(f = 0; f < nb_fields_to_parse; f++) {
1047 regfree(&fields_to_parse[f].regexp);
1053 if(nb_search_conditions > 0) {
1055 /* Recursive search if db_root_path is set */
1058 regex_t db_filename_regexp;
1059 if(regcomp(&db_filename_regexp,
1060 db_filename_regexp_string,
1063 "mymail: Syntax error in regexp \"%s\".\n",
1064 db_filename_regexp_string);
1068 recursive_search_in_db(db_root_path, &db_filename_regexp,
1069 nb_search_conditions, search_conditions,
1072 regfree(&db_filename_regexp);
1075 /* Search in all db files listed in db_filename_list */
1077 if(db_filename_list) {
1078 char db_filename[PATH_MAX + 1];
1081 s = db_filename_list;
1084 s = parse_token(db_filename, PATH_MAX + 1, ';', s);
1086 if(db_filename[0]) {
1087 search_in_db(db_filename, nb_search_conditions, search_conditions, output_file);
1092 /* Search in all db files listed in the command arguments */
1094 while(optind < argc) {
1095 search_in_db(argv[optind], nb_search_conditions, search_conditions, output_file);
1101 for(n = 0; n < nb_search_conditions; n++) {
1102 free_condition(&search_conditions[n]);
1105 if(output_file != stdout) {
1106 fflush(output_file);
1107 fclose(output_file);
1111 free(db_filename_regexp_string);
1113 free(db_filename_list);
1115 regfree(&leading_from_line_regexp);