Added the body condition.
[mymail.git] / mymail.c
1
2 /*
3  *  Copyright (c) 2013 Francois Fleuret
4  *  Written by Francois Fleuret <francois@fleuret.org>
5  *
6  *  This file is part of mymail.
7  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 /*
23
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.
29
30   It is low-tech, simple, light and fast.
31
32 */
33
34 #define _GNU_SOURCE
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <locale.h>
42 #include <getopt.h>
43 #include <limits.h>
44 #include <dirent.h>
45 #include <regex.h>
46
47 #define MYMAIL_DB_MAGIC_TOKEN "mymail_index_file"
48 #define VERSION "0.9"
49
50 #define MAX_NB_SEARCH_CONDITIONS 10
51
52 #define BUFFER_SIZE 65536
53
54 char *db_filename;
55 char *db_filename_regexp_string;
56 char *db_root_path;
57 char *db_filename_list;
58
59 int paranoid;
60 int action_index;
61
62 /********************************************************************/
63
64 enum {
65   ID_MAIL = 0,
66   ID_FROM,
67   ID_TO,
68   ID_SUBJECT,
69   ID_DATE,
70   ID_PARTICIPANT,
71   ID_BODY,
72   MAX_ID
73 };
74
75 static char *field_names[] = {
76   "mail",
77   "from",
78   "to",
79   "subject",
80   "date",
81   "part",
82   "body"
83 };
84
85 /********************************************************************/
86
87 struct search_condition {
88   int field_id;
89   int negation;
90   regex_t regexp;
91 };
92
93 /********************************************************************/
94
95 struct parsable_field {
96   int id;
97   char *regexp_string;
98   regex_t regexp;
99 };
100
101 static struct parsable_field fields_to_parse[] = {
102   {
103     ID_FROM,
104     "^\\(From \\|[Ff][Rr][Oo][Mm]:\\|[R][r][E][e][P][p][L][l][Y][y]-[T][t][O][o]:\\)",
105     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
106   },
107
108   {
109     ID_TO,
110     "^\\([Tt][Oo]\\|[Cc][Cc]\\|[Bb][Cc][Cc]\\): ",
111     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
112   },
113
114   {
115     ID_SUBJECT,
116     "^[Ss][Uu][Bb][Jj][Ee][Cc][Tt]: ",
117     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
118   },
119
120   {
121     ID_DATE,
122     "^[Dd][Aa][Tt][Ee]: ",
123     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
124   },
125
126 };
127
128 /********************************************************************/
129
130 int xor(int a, int b) {
131   return (a && !b) || (!a && b);
132 }
133
134 char *segment_next_field(char *current) {
135   while(*current && *current != ' ') current++;
136   *current = '\0'; current++;
137   while(*current && *current == ' ') current++;
138   return current;
139 }
140
141 void remove_eof(char *c) {
142   while(*c && *c != '\n' && *c != '\r') c++;
143   *c = '\0';
144 }
145
146 /********************************************************************/
147
148 /* malloc with error checking.  */
149
150 void *safe_malloc(size_t n) {
151   void *p = malloc(n);
152   if(!p && n != 0) {
153     fprintf(stderr,
154             "mymail: cannot allocate memory: %s\n", strerror(errno));
155     exit(EXIT_FAILURE);
156   }
157   return p;
158 }
159
160 /*********************************************************************/
161
162 void print_version(FILE *out) {
163   fprintf(out, "mymail version %s (%s)\n", VERSION, UNAME);
164 }
165
166 void print_usage(FILE *out) {
167   print_version(out);
168   fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
169   fprintf(out, "\n");
170   fprintf(out, "Usage: mymail [options] [<mbox dir1> [<mbox dir2> ...]|<db file1> [<db file2> ...]]\n");
171   fprintf(out, "\n");
172   fprintf(out, " -h, --help\n");
173   fprintf(out, "         show this help\n");
174   fprintf(out, " -v, --version\n");
175   fprintf(out, "         print the version number\n");
176   fprintf(out, " -p <db filename pattern>, --db-pattern <db filename pattern>\n");
177   fprintf(out, "         set the db filename pattern for recursive search\n");
178   fprintf(out, " -r <db root path>, --db-root <db root path>\n");
179   fprintf(out, "         set the db root path for recursive search\n");
180   fprintf(out, " -l <db filename list>, --db-list <db filename list>\n");
181   fprintf(out, "         set the semicolon-separated list of db files for search\n");
182   fprintf(out, " -s <search pattern>, --search <search pattern>\n");
183   fprintf(out, "         search for matching mails in the db file\n");
184   fprintf(out, " -d <db filename>, --db-file <db filename>\n");
185   fprintf(out, "         set the db filename for indexing\n");
186   fprintf(out, " -i, --index\n");
187   fprintf(out, "         index mails\n");
188 }
189
190 /*********************************************************************/
191
192 int ignore_entry(const char *name) {
193   return
194     /* strcmp(name, ".") == 0 || */
195     /* strcmp(name, "..") == 0 || */
196     (name[0] == '.' && name[1] != '/');
197 }
198
199 int mbox_line_match_search(struct search_condition *condition,
200                            int mbox_id, char *mbox_value) {
201   return
202     (condition->field_id == mbox_id ||
203      (condition->field_id == ID_PARTICIPANT && (mbox_id == ID_FROM || mbox_id == ID_TO)))
204     &&
205     regexec(&condition->regexp, mbox_value, 0, 0, 0) == 0;
206 }
207
208 void search_in_db(int nb_search_conditions,
209                   struct search_condition *search_conditions,
210                   FILE *db_file) {
211   int hits[MAX_NB_SEARCH_CONDITIONS];
212   char raw_db_line[BUFFER_SIZE];
213   char raw_mbox_line[BUFFER_SIZE];
214   char current_mail_filename[PATH_MAX + 1];
215   unsigned long int current_position_in_mail;
216   char *mbox_name, *mbox_value;
217   int mbox_id;
218   int already_written, m, n;
219   int last_mbox_line_was_empty;
220   int nb_body_conditions, nb_fulfilled_body_conditions;
221
222   current_position_in_mail = 0;
223   already_written = 0;
224
225   for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
226
227   nb_body_conditions = 0;
228   for(n = 0; n < nb_search_conditions; n++) {
229     if(search_conditions[n].field_id == ID_BODY) {
230       nb_body_conditions++;
231     }
232   }
233
234   while(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
235     mbox_name = raw_db_line;
236     mbox_value = segment_next_field(raw_db_line);
237
238     if(strcmp("mail", mbox_name) == 0) {
239       char *position_in_file_string;
240       char *mail_filename;
241
242       /* We first check all conditions but the body ones */
243
244       for(n = 0; n < nb_search_conditions &&
245             ((search_conditions[n].field_id == ID_BODY) ||
246              xor(hits[n], search_conditions[n].negation)); n++);
247
248       if(n == nb_search_conditions) {
249
250         /* all conditions but the body ones are fine, check the body
251            ones */
252
253         nb_fulfilled_body_conditions = 0;
254
255         if(nb_body_conditions > 0) {
256           FILE *mail_file;
257           int header;
258
259           header = 1;
260           mail_file = fopen(current_mail_filename, "r");
261
262           if(!mail_file) {
263             fprintf(stderr, "mymail: Cannot open mbox '%s'.\n", current_mail_filename);
264             exit(EXIT_FAILURE);
265           }
266
267           fseek(mail_file, current_position_in_mail, SEEK_SET);
268
269           if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
270             while(nb_fulfilled_body_conditions < nb_body_conditions) {
271               last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
272
273               if(last_mbox_line_was_empty) { header = 0; }
274
275               if(!header) {
276                 for(n = 0; n < nb_search_conditions; n++) {
277                   if(search_conditions[n].field_id == ID_BODY && !hits[n]) {
278                     hits[n] =
279                       (regexec(&search_conditions[n].regexp, raw_mbox_line, 0, 0, 0) == 0);
280                     if(hits[n]) {
281                       fprintf(stderr, "** %d %s", header, raw_mbox_line);
282                       nb_fulfilled_body_conditions++;
283                     }
284                   }
285                 }
286               }
287
288               if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
289                  (last_mbox_line_was_empty && strncmp(raw_mbox_line, "From ", 5) == 0)) break;
290             }
291           }
292
293           fclose(mail_file);
294         }
295
296         if(nb_body_conditions == nb_fulfilled_body_conditions) {
297           FILE *mail_file;
298
299           mail_file = fopen(current_mail_filename, "r");
300
301           if(!mail_file) {
302             fprintf(stderr, "mymail: Cannot open mbox '%s'.\n", current_mail_filename);
303             exit(EXIT_FAILURE);
304           }
305
306           fseek(mail_file, current_position_in_mail, SEEK_SET);
307
308           if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
309             last_mbox_line_was_empty = 1;
310             printf("%s", raw_mbox_line);
311             while(1) {
312               if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
313                  (last_mbox_line_was_empty && strncmp(raw_mbox_line, "From ", 5) == 0)) break;
314               last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
315               printf("%s", raw_mbox_line);
316             }
317           }
318
319           fclose(mail_file);
320         }
321       }
322
323       for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
324
325       position_in_file_string = mbox_value;
326       mail_filename = segment_next_field(mbox_value);
327       current_position_in_mail = atol(position_in_file_string);
328       strcpy(current_mail_filename, mail_filename);
329
330       remove_eof(current_mail_filename);
331       already_written = 0;
332     }
333
334     else {
335       mbox_id = -1;
336       for(m = 0; (m < MAX_ID) && mbox_id == -1; m++) {
337         if(strncmp(field_names[m], mbox_name, strlen(mbox_name)) == 0) {
338           mbox_id = m;
339         }
340       }
341       for(n = 0; n < nb_search_conditions; n++) {
342         hits[n] |= mbox_line_match_search(&search_conditions[n],
343                                           mbox_id, mbox_value);
344       }
345     }
346   }
347 }
348
349 void recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp,
350                             int nb_search_conditions, struct search_condition *search_conditions) {
351   DIR *dir;
352   struct dirent *dir_e;
353   struct stat sb;
354   char raw_db_line[BUFFER_SIZE];
355   char subname[PATH_MAX + 1];
356
357   if(lstat(entry_name, &sb) != 0) {
358     fprintf(stderr,
359             "mymail: Cannot stat \"%s\": %s\n",
360             entry_name,
361             strerror(errno));
362     exit(EXIT_FAILURE);
363   }
364
365   dir = opendir(entry_name);
366
367   if(dir) {
368     while((dir_e = readdir(dir))) {
369       if(!ignore_entry(dir_e->d_name)) {
370         snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
371         recursive_search_in_db(subname, db_filename_regexp,
372                                nb_search_conditions, search_conditions);
373       }
374     }
375     closedir(dir);
376   }
377
378   else {
379     const char *s = entry_name, *filename = entry_name;
380     while(*s) { if(*s == '/') { filename = s+1; } s++; }
381
382     if(regexec(db_filename_regexp, filename, 0, 0, 0) == 0) {
383       FILE *db_file = fopen(entry_name, "r");
384
385       if(!db_file) {
386         fprintf(stderr,
387                 "mymail: Cannot open \"%s\" for reading: %s\n",
388                 db_filename,
389                 strerror(errno));
390         exit(EXIT_FAILURE);
391       }
392
393       if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
394         if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
395           fprintf(stderr,
396                   "mymail: Header line in '%s' does not match the mymail db format.\n",
397                   entry_name);
398           exit(EXIT_FAILURE);
399         }
400       } else {
401         fprintf(stderr,
402                 "mymail: Cannot read the header line in '%s'.\n",
403                 entry_name);
404         exit(EXIT_FAILURE);
405       }
406
407       search_in_db(nb_search_conditions, search_conditions, db_file);
408
409       fclose(db_file);
410     }
411   }
412 }
413
414 /*********************************************************************/
415
416 void index_one_mbox_line(int nb_fields_to_parse, struct parsable_field *fields_to_parse,
417                          char *raw_mbox_line, FILE *db_file) {
418   regmatch_t matches;
419   int f;
420   for(f = 0; f < nb_fields_to_parse; f++) {
421     if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
422       fprintf(db_file, "%s %s\n",
423               field_names[fields_to_parse[f].id],
424               raw_mbox_line + matches.rm_eo);
425     }
426   }
427 }
428
429 void index_mbox(const char *mbox_filename,
430                 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
431                 FILE *db_file) {
432   char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
433   char *end_of_full_line;
434   FILE *file;
435   int in_header, new_header, last_mbox_line_was_empty;
436   unsigned long int position_in_file;
437
438   file = fopen(mbox_filename, "r");
439
440   if(!file) {
441     fprintf(stderr, "mymail: Cannot open '%s'.\n", mbox_filename);
442     if(paranoid) { exit(EXIT_FAILURE); }
443     return;
444   }
445
446   in_header = 0;
447   new_header = 0;
448
449   position_in_file = 0;
450   end_of_full_line = 0;
451   full_line[0] = '\0';
452   last_mbox_line_was_empty = 1;
453
454   while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
455     if(last_mbox_line_was_empty && strncmp(raw_mbox_line, "From ", 5) == 0) {
456       if(in_header) {
457         fprintf(stderr,
458                 "Got a ^\"From \" in the header in %s:%lu.\n",
459                 mbox_filename, position_in_file);
460         fprintf(stderr, "%s", raw_mbox_line);
461         if(paranoid) { exit(EXIT_FAILURE); }
462       }
463       in_header = 1;
464       new_header = 1;
465     } else if(raw_mbox_line[0] == '\n') {
466       if(in_header) { in_header = 0; }
467     }
468
469     last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
470
471     if(in_header) {
472       if(new_header) {
473         fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
474         new_header = 0;
475       }
476
477       if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
478         char *start = raw_mbox_line;
479         while(*start == ' ' || *start == '\t') start++;
480         *(end_of_full_line++) = ' ';
481         strcpy(end_of_full_line, start);
482         while(*end_of_full_line && *end_of_full_line != '\n') {
483           end_of_full_line++;
484         }
485         *end_of_full_line = '\0';
486       }
487
488       else {
489         /*
490           if(!((raw_mbox_line[0] >= 'a' && raw_mbox_line[0] <= 'z') ||
491           (raw_mbox_line[0] >= 'A' && raw_mbox_line[0] <= 'Z'))) {
492           fprintf(stderr,
493           "Header line syntax error %s:%lu.\n",
494           mbox_filename, position_in_file);
495           fprintf(stderr, "%s", raw_mbox_line);
496           }
497         */
498
499         if(full_line[0]) {
500           index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
501         }
502
503         end_of_full_line = full_line;
504         strcpy(end_of_full_line, raw_mbox_line);
505         while(*end_of_full_line && *end_of_full_line != '\n') {
506           end_of_full_line++;
507         }
508         *end_of_full_line = '\0';
509       }
510
511     }
512
513     position_in_file += strlen(raw_mbox_line);
514   }
515
516   fclose(file);
517 }
518
519 void recursive_index_mbox(FILE *db_file,
520                           const char *entry_name,
521                           int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
522   DIR *dir;
523   struct dirent *dir_e;
524   struct stat sb;
525   char subname[PATH_MAX + 1];
526
527   if(lstat(entry_name, &sb) != 0) {
528     fprintf(stderr,
529             "mymail: Cannot stat \"%s\": %s\n",
530             entry_name,
531             strerror(errno));
532     exit(EXIT_FAILURE);
533   }
534
535   dir = opendir(entry_name);
536
537   if(dir) {
538     while((dir_e = readdir(dir))) {
539       if(!ignore_entry(dir_e->d_name)) {
540         snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
541         recursive_index_mbox(db_file, subname, nb_fields_to_parse, fields_to_parse);
542       }
543     }
544     closedir(dir);
545   } else {
546     index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
547   }
548 }
549
550 /*********************************************************************/
551
552 /* For long options that have no equivalent short option, use a
553    non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
554 enum {
555   OPT_BASH_MODE = CHAR_MAX + 1
556 };
557
558 static struct option long_options[] = {
559   { "help", no_argument, 0, 'h' },
560   { "version", no_argument, 0, 'v' },
561   { "db-file", 1, 0, 'd' },
562   { "db-pattern", 1, 0, 'p' },
563   { "db-root", 1, 0, 'r' },
564   { "db-list", 1, 0, 'l' },
565   { "search", 1, 0, 's' },
566   { "index", 0, 0, 'i' },
567   { 0, 0, 0, 0 }
568 };
569
570 /*********************************************************************/
571
572 int main(int argc, char **argv) {
573   int error = 0, show_help = 0;
574   const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
575   char c;
576   int f;
577   int nb_search_conditions;
578   char *search_condition_strings[MAX_NB_SEARCH_CONDITIONS];
579
580   /* for(f = 0; f < argc; f++) { */
581   /* printf("arg %d \"%s\"\n", f, argv[f]); */
582   /* } */
583
584   paranoid = 0;
585   action_index = 0;
586   db_filename = 0;
587   db_root_path = 0;
588   db_filename_list = 0;
589
590   setlocale(LC_ALL, "");
591
592   nb_search_conditions = 0;
593
594   while ((c = getopt_long(argc, argv, "hvip:s:d:r:l:",
595                           long_options, NULL)) != -1) {
596
597     switch(c) {
598
599     case 'h':
600       show_help = 1;
601       break;
602
603     case 'v':
604       print_version(stdout);
605       break;
606
607     case 'i':
608       action_index = 1;
609       break;
610
611     case 'd':
612       db_filename = strdup(optarg);
613       break;
614
615     case 'p':
616       db_filename_regexp_string = strdup(optarg);
617       break;
618
619     case 'r':
620       db_root_path = strdup(optarg);
621       break;
622
623     case 'l':
624       db_filename_list = strdup(optarg);
625       break;
626
627     case 's':
628       if(nb_search_conditions == MAX_NB_SEARCH_CONDITIONS) {
629         fprintf(stderr, "mymail: Too many search patterns.\n");
630         exit(EXIT_FAILURE);
631       }
632       search_condition_strings[nb_search_conditions++] = strdup(optarg);
633       break;
634
635     default:
636       error = 1;
637       break;
638     }
639   }
640
641   if(!db_filename) {
642     char *default_db_filename = getenv("MYMAIL_DB_FILE");
643
644     if(!default_db_filename) {
645       default_db_filename = "mymail.db";
646     }
647
648     db_filename = strdup(default_db_filename);
649   }
650
651   if(!db_filename_regexp_string) {
652     char *default_db_filename_regexp_string = getenv("MYMAIL_DB_PATTERN");
653
654     if(!default_db_filename_regexp_string) {
655       default_db_filename_regexp_string = "^mymail.db$";
656     }
657
658     db_filename_regexp_string = strdup(default_db_filename_regexp_string);
659   }
660
661   if(!db_root_path) {
662     char *default_db_root_path = getenv("MYMAIL_DB_ROOT");
663
664     if(default_db_root_path) {
665       db_root_path = strdup(default_db_root_path);
666     }
667   }
668
669   if(!db_filename_list) {
670     char *default_db_filename_list = getenv("MYMAIL_DB_LIST");
671
672     if(default_db_filename_list) {
673       db_filename_list = strdup(default_db_filename_list);
674     }
675   }
676
677   if(error) {
678     print_usage(stderr);
679     exit(EXIT_FAILURE);
680   }
681
682   if(show_help) {
683     print_usage(stdout);
684     exit(EXIT_SUCCESS);
685   }
686
687   if(action_index) {
688     FILE *db_file;
689
690     db_file = fopen(db_filename, "w");
691
692     if(!db_file) {
693       fprintf(stderr,
694               "mymail: Cannot open \"%s\" for writing: %s\n",
695               db_filename,
696               strerror(errno));
697       exit(EXIT_FAILURE);
698     }
699
700     for(f = 0; f < nb_fields_to_parse; f++) {
701       if(regcomp(&fields_to_parse[f].regexp,
702                  fields_to_parse[f].regexp_string,
703                  REG_ICASE)) {
704         fprintf(stderr,
705                 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
706                 fields_to_parse[f].regexp_string,
707                 field_names[fields_to_parse[f].id]);
708         exit(EXIT_FAILURE);
709       }
710     }
711
712     fprintf(db_file, "%s version_%s raw\n", MYMAIL_DB_MAGIC_TOKEN, VERSION);
713
714     while(optind < argc) {
715       recursive_index_mbox(db_file,
716                            argv[optind],
717                            nb_fields_to_parse, fields_to_parse);
718       optind++;
719     }
720
721     fclose(db_file);
722
723     for(f = 0; f < nb_fields_to_parse; f++) {
724       regfree(&fields_to_parse[f].regexp);
725     }
726   }
727
728   else {
729
730     if(nb_search_conditions > 0) {
731       struct search_condition search_conditions[MAX_NB_SEARCH_CONDITIONS];
732       char *search_field, *search_regexp_string;
733       int m, n;
734
735       for(n = 0; n < nb_search_conditions; n++) {
736         search_field = search_condition_strings[n];
737         search_regexp_string = segment_next_field(search_condition_strings[n]);
738
739         if(search_field[0] == '!') {
740           search_field++;
741           search_conditions[n].negation = 1;
742         } else {
743           search_conditions[n].negation = 0;
744         }
745
746         search_conditions[n].field_id = -1;
747         for(m = 0; (m < MAX_ID) && search_conditions[n].field_id == -1; m++) {
748           if(strncmp(field_names[m], search_field, strlen(search_field)) == 0) {
749             search_conditions[n].field_id = m;
750           }
751         }
752
753         if(search_conditions[n].field_id == -1) {
754           fprintf(stderr,
755                   "mymail: Syntax error in field name \"%s\".\n",
756                   search_field);
757           exit(EXIT_FAILURE);
758         }
759
760         if(regcomp(&search_conditions[n].regexp,
761                    search_regexp_string,
762                    REG_ICASE)) {
763           fprintf(stderr,
764                   "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
765                   search_regexp_string,
766                   field_names[search_conditions[n].field_id]);
767           exit(EXIT_FAILURE);
768         }
769       }
770
771       /* Recursive search if db_root_path is set */
772
773       if(db_root_path) {
774         regex_t db_filename_regexp;
775         if(regcomp(&db_filename_regexp,
776                    db_filename_regexp_string,
777                    0)) {
778           fprintf(stderr,
779                   "mymail: Syntax error in regexp \"%s\".\n",
780                   db_filename_regexp_string);
781           exit(EXIT_FAILURE);
782         }
783
784         recursive_search_in_db(db_root_path, &db_filename_regexp,
785                                nb_search_conditions, search_conditions);
786
787         regfree(&db_filename_regexp);
788       }
789
790       /* Search in all db files listed in db_filename_list */
791
792       if(db_filename_list) {
793         char db_filename[PATH_MAX + 1];
794         char *s, *t;
795         FILE *db_file;
796
797         s = db_filename_list;
798
799         while(*s) {
800           t = db_filename;
801           while(*s == ';') { s++; }
802           while(*s && *s != ';') { *t++ = *s++; }
803           *t++ = '\0';
804
805           if(db_filename[0]) {
806             db_file = fopen(db_filename, "r");
807
808             if(!db_file) {
809               fprintf(stderr,
810                       "mymail: Cannot open \"%s\" for reading: %s\n",
811                       argv[optind],
812                       strerror(errno));
813               exit(EXIT_FAILURE);
814             }
815
816             search_in_db(nb_search_conditions, search_conditions, db_file);
817
818             fclose(db_file);
819           }
820         }
821       }
822
823       /* Search in all db files listed in the command arguments */
824
825       while(optind < argc) {
826         FILE *db_file = fopen(argv[optind], "r");
827
828         if(!db_file) {
829           fprintf(stderr,
830                   "mymail: Cannot open \"%s\" for reading: %s\n",
831                   argv[optind],
832                   strerror(errno));
833           exit(EXIT_FAILURE);
834         }
835
836         search_in_db(nb_search_conditions, search_conditions, db_file);
837
838         fclose(db_file);
839         optind++;
840       }
841
842       for(n = 0; n < nb_search_conditions; n++) {
843         regfree(&search_conditions[n].regexp);
844         free(search_condition_strings[n]);
845       }
846     }
847   }
848
849   free(db_filename);
850   free(db_filename_regexp_string);
851   free(db_root_path);
852   free(db_filename_list);
853
854   exit(EXIT_SUCCESS);
855 }