Fixed the body content 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.1"
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   strcpy(current_mail_filename, "");
235
236   while(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
237     mbox_name = raw_db_line;
238     mbox_value = segment_next_field(raw_db_line);
239
240     if(strcmp("mail", mbox_name) == 0) {
241       char *position_in_file_string;
242       char *mail_filename;
243
244       if(current_mail_filename[0]) {
245
246         /* We first check all conditions but the body ones */
247
248         for(n = 0; n < nb_search_conditions &&
249               ((search_conditions[n].field_id == ID_BODY) ||
250                xor(hits[n], search_conditions[n].negation)); n++);
251
252         if(n == nb_search_conditions) {
253
254           /* all conditions but the body ones are fine, check the body
255              ones */
256
257           nb_fulfilled_body_conditions = 0;
258
259           if(nb_body_conditions > 0) {
260             FILE *mail_file;
261             int header;
262
263             header = 1;
264             mail_file = fopen(current_mail_filename, "r");
265
266             if(!mail_file) {
267               fprintf(stderr,
268                       "mymail: Cannot open mbox '%s' for body scan.\n",
269                       current_mail_filename);
270               exit(EXIT_FAILURE);
271             }
272
273             fseek(mail_file, current_position_in_mail, SEEK_SET);
274
275             if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
276               while(nb_fulfilled_body_conditions < nb_body_conditions) {
277                 last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
278
279                 if(last_mbox_line_was_empty) { header = 0; }
280
281                 if(!header) {
282                   for(n = 0; n < nb_search_conditions; n++) {
283                     if(search_conditions[n].field_id == ID_BODY && !hits[n]) {
284                       hits[n] =
285                         (regexec(&search_conditions[n].regexp, raw_mbox_line, 0, 0, 0) == 0);
286                       if(hits[n]) {
287                         nb_fulfilled_body_conditions++;
288                       }
289                     }
290                   }
291                 }
292
293                 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
294                    (last_mbox_line_was_empty && strncmp(raw_mbox_line, "From ", 5) == 0))
295                   break;
296               }
297             }
298
299             fclose(mail_file);
300           }
301
302           if(nb_body_conditions == nb_fulfilled_body_conditions) {
303             FILE *mail_file;
304
305             mail_file = fopen(current_mail_filename, "r");
306
307             if(!mail_file) {
308               fprintf(stderr,
309                       "mymail: Cannot open mbox '%s' for mail extraction.\n",
310                       current_mail_filename);
311               exit(EXIT_FAILURE);
312             }
313
314             fseek(mail_file, current_position_in_mail, SEEK_SET);
315
316             if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
317               last_mbox_line_was_empty = 1;
318               printf("%s", raw_mbox_line);
319               while(1) {
320                 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
321                    (last_mbox_line_was_empty && strncmp(raw_mbox_line, "From ", 5) == 0))
322                   break;
323                 last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
324                 printf("%s", raw_mbox_line);
325               }
326             }
327
328             fclose(mail_file);
329           }
330         }
331       }
332
333       for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
334
335       position_in_file_string = mbox_value;
336       mail_filename = segment_next_field(mbox_value);
337       current_position_in_mail = atol(position_in_file_string);
338       strcpy(current_mail_filename, mail_filename);
339
340       remove_eof(current_mail_filename);
341       already_written = 0;
342     }
343
344     else {
345       mbox_id = -1;
346       for(m = 0; (m < MAX_ID) && mbox_id == -1; m++) {
347         if(strncmp(field_names[m], mbox_name, strlen(mbox_name)) == 0) {
348           mbox_id = m;
349         }
350       }
351       for(n = 0; n < nb_search_conditions; n++) {
352         hits[n] |= mbox_line_match_search(&search_conditions[n],
353                                           mbox_id, mbox_value);
354       }
355     }
356   }
357 }
358
359 void recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp,
360                             int nb_search_conditions,
361                             struct search_condition *search_conditions) {
362   DIR *dir;
363   struct dirent *dir_e;
364   struct stat sb;
365   char raw_db_line[BUFFER_SIZE];
366   char subname[PATH_MAX + 1];
367
368   if(lstat(entry_name, &sb) != 0) {
369     fprintf(stderr,
370             "mymail: Cannot stat \"%s\": %s\n",
371             entry_name,
372             strerror(errno));
373     exit(EXIT_FAILURE);
374   }
375
376   dir = opendir(entry_name);
377
378   if(dir) {
379     while((dir_e = readdir(dir))) {
380       if(!ignore_entry(dir_e->d_name)) {
381         snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
382         recursive_search_in_db(subname, db_filename_regexp,
383                                nb_search_conditions, search_conditions);
384       }
385     }
386     closedir(dir);
387   }
388
389   else {
390     const char *s = entry_name, *filename = entry_name;
391     while(*s) { if(*s == '/') { filename = s+1; } s++; }
392
393     if(regexec(db_filename_regexp, filename, 0, 0, 0) == 0) {
394       FILE *db_file = fopen(entry_name, "r");
395
396       if(!db_file) {
397         fprintf(stderr,
398                 "mymail: Cannot open \"%s\" for reading: %s\n",
399                 db_filename,
400                 strerror(errno));
401         exit(EXIT_FAILURE);
402       }
403
404       if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
405         if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
406           fprintf(stderr,
407                   "mymail: Header line in '%s' does not match the mymail db format.\n",
408                   entry_name);
409           exit(EXIT_FAILURE);
410         }
411       } else {
412         fprintf(stderr,
413                 "mymail: Cannot read the header line in '%s'.\n",
414                 entry_name);
415         exit(EXIT_FAILURE);
416       }
417
418       search_in_db(nb_search_conditions, search_conditions, db_file);
419
420       fclose(db_file);
421     }
422   }
423 }
424
425 /*********************************************************************/
426
427 void index_one_mbox_line(int nb_fields_to_parse, struct parsable_field *fields_to_parse,
428                          char *raw_mbox_line, FILE *db_file) {
429   regmatch_t matches;
430   int f;
431   for(f = 0; f < nb_fields_to_parse; f++) {
432     if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
433       fprintf(db_file, "%s %s\n",
434               field_names[fields_to_parse[f].id],
435               raw_mbox_line + matches.rm_eo);
436     }
437   }
438 }
439
440 void index_mbox(const char *mbox_filename,
441                 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
442                 FILE *db_file) {
443   char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
444   char *end_of_full_line;
445   FILE *file;
446   int in_header, new_header, last_mbox_line_was_empty;
447   unsigned long int position_in_file;
448
449   file = fopen(mbox_filename, "r");
450
451   if(!file) {
452     fprintf(stderr, "mymail: Cannot open '%s'.\n", mbox_filename);
453     if(paranoid) { exit(EXIT_FAILURE); }
454     return;
455   }
456
457   in_header = 0;
458   new_header = 0;
459
460   position_in_file = 0;
461   end_of_full_line = 0;
462   full_line[0] = '\0';
463   last_mbox_line_was_empty = 1;
464
465   while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
466     if(last_mbox_line_was_empty && strncmp(raw_mbox_line, "From ", 5) == 0) {
467       if(in_header) {
468         fprintf(stderr,
469                 "Got a ^\"From \" in the header in %s:%lu.\n",
470                 mbox_filename, position_in_file);
471         fprintf(stderr, "%s", raw_mbox_line);
472         if(paranoid) { exit(EXIT_FAILURE); }
473       }
474       in_header = 1;
475       new_header = 1;
476     } else if(raw_mbox_line[0] == '\n') {
477       if(in_header) { in_header = 0; }
478     }
479
480     last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
481
482     if(in_header) {
483       if(new_header) {
484         fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
485         new_header = 0;
486       }
487
488       if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
489         char *start = raw_mbox_line;
490         while(*start == ' ' || *start == '\t') start++;
491         *(end_of_full_line++) = ' ';
492         strcpy(end_of_full_line, start);
493         while(*end_of_full_line && *end_of_full_line != '\n') {
494           end_of_full_line++;
495         }
496         *end_of_full_line = '\0';
497       }
498
499       else {
500         /*
501           if(!((raw_mbox_line[0] >= 'a' && raw_mbox_line[0] <= 'z') ||
502           (raw_mbox_line[0] >= 'A' && raw_mbox_line[0] <= 'Z'))) {
503           fprintf(stderr,
504           "Header line syntax error %s:%lu.\n",
505           mbox_filename, position_in_file);
506           fprintf(stderr, "%s", raw_mbox_line);
507           }
508         */
509
510         if(full_line[0]) {
511           index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
512         }
513
514         end_of_full_line = full_line;
515         strcpy(end_of_full_line, raw_mbox_line);
516         while(*end_of_full_line && *end_of_full_line != '\n') {
517           end_of_full_line++;
518         }
519         *end_of_full_line = '\0';
520       }
521
522     }
523
524     position_in_file += strlen(raw_mbox_line);
525   }
526
527   fclose(file);
528 }
529
530 void recursive_index_mbox(FILE *db_file,
531                           const char *entry_name,
532                           int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
533   DIR *dir;
534   struct dirent *dir_e;
535   struct stat sb;
536   char subname[PATH_MAX + 1];
537
538   if(lstat(entry_name, &sb) != 0) {
539     fprintf(stderr,
540             "mymail: Cannot stat \"%s\": %s\n",
541             entry_name,
542             strerror(errno));
543     exit(EXIT_FAILURE);
544   }
545
546   dir = opendir(entry_name);
547
548   if(dir) {
549     while((dir_e = readdir(dir))) {
550       if(!ignore_entry(dir_e->d_name)) {
551         snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
552         recursive_index_mbox(db_file, subname, nb_fields_to_parse, fields_to_parse);
553       }
554     }
555     closedir(dir);
556   } else {
557     index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
558   }
559 }
560
561 /*********************************************************************/
562
563 /* For long options that have no equivalent short option, use a
564    non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
565 enum {
566   OPT_BASH_MODE = CHAR_MAX + 1
567 };
568
569 static struct option long_options[] = {
570   { "help", no_argument, 0, 'h' },
571   { "version", no_argument, 0, 'v' },
572   { "db-file", 1, 0, 'd' },
573   { "db-pattern", 1, 0, 'p' },
574   { "db-root", 1, 0, 'r' },
575   { "db-list", 1, 0, 'l' },
576   { "search", 1, 0, 's' },
577   { "index", 0, 0, 'i' },
578   { 0, 0, 0, 0 }
579 };
580
581 /*********************************************************************/
582
583 int main(int argc, char **argv) {
584   int error = 0, show_help = 0;
585   const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
586   char c;
587   int f;
588   int nb_search_conditions;
589   char *search_condition_strings[MAX_NB_SEARCH_CONDITIONS];
590
591   /* for(f = 0; f < argc; f++) { */
592   /* printf("arg %d \"%s\"\n", f, argv[f]); */
593   /* } */
594
595   paranoid = 0;
596   action_index = 0;
597   db_filename = 0;
598   db_root_path = 0;
599   db_filename_list = 0;
600
601   setlocale(LC_ALL, "");
602
603   nb_search_conditions = 0;
604
605   while ((c = getopt_long(argc, argv, "hvip:s:d:r:l:",
606                           long_options, NULL)) != -1) {
607
608     switch(c) {
609
610     case 'h':
611       show_help = 1;
612       break;
613
614     case 'v':
615       print_version(stdout);
616       break;
617
618     case 'i':
619       action_index = 1;
620       break;
621
622     case 'd':
623       db_filename = strdup(optarg);
624       break;
625
626     case 'p':
627       db_filename_regexp_string = strdup(optarg);
628       break;
629
630     case 'r':
631       db_root_path = strdup(optarg);
632       break;
633
634     case 'l':
635       db_filename_list = strdup(optarg);
636       break;
637
638     case 's':
639       if(nb_search_conditions == MAX_NB_SEARCH_CONDITIONS) {
640         fprintf(stderr, "mymail: Too many search patterns.\n");
641         exit(EXIT_FAILURE);
642       }
643       search_condition_strings[nb_search_conditions++] = strdup(optarg);
644       break;
645
646     default:
647       error = 1;
648       break;
649     }
650   }
651
652   if(!db_filename) {
653     char *default_db_filename = getenv("MYMAIL_DB_FILE");
654
655     if(!default_db_filename) {
656       default_db_filename = "mymail.db";
657     }
658
659     db_filename = strdup(default_db_filename);
660   }
661
662   if(!db_filename_regexp_string) {
663     char *default_db_filename_regexp_string = getenv("MYMAIL_DB_PATTERN");
664
665     if(!default_db_filename_regexp_string) {
666       default_db_filename_regexp_string = "^mymail.db$";
667     }
668
669     db_filename_regexp_string = strdup(default_db_filename_regexp_string);
670   }
671
672   if(!db_root_path) {
673     char *default_db_root_path = getenv("MYMAIL_DB_ROOT");
674
675     if(default_db_root_path) {
676       db_root_path = strdup(default_db_root_path);
677     }
678   }
679
680   if(!db_filename_list) {
681     char *default_db_filename_list = getenv("MYMAIL_DB_LIST");
682
683     if(default_db_filename_list) {
684       db_filename_list = strdup(default_db_filename_list);
685     }
686   }
687
688   if(error) {
689     print_usage(stderr);
690     exit(EXIT_FAILURE);
691   }
692
693   if(show_help) {
694     print_usage(stdout);
695     exit(EXIT_SUCCESS);
696   }
697
698   if(action_index) {
699     FILE *db_file;
700
701     db_file = fopen(db_filename, "w");
702
703     if(!db_file) {
704       fprintf(stderr,
705               "mymail: Cannot open \"%s\" for writing: %s\n",
706               db_filename,
707               strerror(errno));
708       exit(EXIT_FAILURE);
709     }
710
711     for(f = 0; f < nb_fields_to_parse; f++) {
712       if(regcomp(&fields_to_parse[f].regexp,
713                  fields_to_parse[f].regexp_string,
714                  REG_ICASE)) {
715         fprintf(stderr,
716                 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
717                 fields_to_parse[f].regexp_string,
718                 field_names[fields_to_parse[f].id]);
719         exit(EXIT_FAILURE);
720       }
721     }
722
723     fprintf(db_file, "%s version_%s raw\n", MYMAIL_DB_MAGIC_TOKEN, VERSION);
724
725     while(optind < argc) {
726       recursive_index_mbox(db_file,
727                            argv[optind],
728                            nb_fields_to_parse, fields_to_parse);
729       optind++;
730     }
731
732     fclose(db_file);
733
734     for(f = 0; f < nb_fields_to_parse; f++) {
735       regfree(&fields_to_parse[f].regexp);
736     }
737   }
738
739   else {
740
741     if(nb_search_conditions > 0) {
742       struct search_condition search_conditions[MAX_NB_SEARCH_CONDITIONS];
743       char *search_field, *search_regexp_string;
744       int m, n;
745
746       for(n = 0; n < nb_search_conditions; n++) {
747         search_field = search_condition_strings[n];
748         search_regexp_string = segment_next_field(search_condition_strings[n]);
749
750         if(search_field[0] == '!') {
751           search_field++;
752           search_conditions[n].negation = 1;
753         } else {
754           search_conditions[n].negation = 0;
755         }
756
757         search_conditions[n].field_id = -1;
758         for(m = 0; (m < MAX_ID) && search_conditions[n].field_id == -1; m++) {
759           if(strncmp(field_names[m], search_field, strlen(search_field)) == 0) {
760             search_conditions[n].field_id = m;
761           }
762         }
763
764         if(search_conditions[n].field_id == -1) {
765           fprintf(stderr,
766                   "mymail: Syntax error in field name \"%s\".\n",
767                   search_field);
768           exit(EXIT_FAILURE);
769         }
770
771         if(regcomp(&search_conditions[n].regexp,
772                    search_regexp_string,
773                    REG_ICASE)) {
774           fprintf(stderr,
775                   "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
776                   search_regexp_string,
777                   field_names[search_conditions[n].field_id]);
778           exit(EXIT_FAILURE);
779         }
780       }
781
782       /* Recursive search if db_root_path is set */
783
784       if(db_root_path) {
785         regex_t db_filename_regexp;
786         if(regcomp(&db_filename_regexp,
787                    db_filename_regexp_string,
788                    0)) {
789           fprintf(stderr,
790                   "mymail: Syntax error in regexp \"%s\".\n",
791                   db_filename_regexp_string);
792           exit(EXIT_FAILURE);
793         }
794
795         recursive_search_in_db(db_root_path, &db_filename_regexp,
796                                nb_search_conditions, search_conditions);
797
798         regfree(&db_filename_regexp);
799       }
800
801       /* Search in all db files listed in db_filename_list */
802
803       if(db_filename_list) {
804         char db_filename[PATH_MAX + 1];
805         char *s, *t;
806         FILE *db_file;
807
808         s = db_filename_list;
809
810         while(*s) {
811           t = db_filename;
812           while(*s == ';') { s++; }
813           while(*s && *s != ';') { *t++ = *s++; }
814           *t++ = '\0';
815
816           if(db_filename[0]) {
817             db_file = fopen(db_filename, "r");
818
819             if(!db_file) {
820               fprintf(stderr,
821                       "mymail: Cannot open \"%s\" for reading: %s\n",
822                       argv[optind],
823                       strerror(errno));
824               exit(EXIT_FAILURE);
825             }
826
827             search_in_db(nb_search_conditions, search_conditions, db_file);
828
829             fclose(db_file);
830           }
831         }
832       }
833
834       /* Search in all db files listed in the command arguments */
835
836       while(optind < argc) {
837         FILE *db_file = fopen(argv[optind], "r");
838
839         if(!db_file) {
840           fprintf(stderr,
841                   "mymail: Cannot open \"%s\" for reading: %s\n",
842                   argv[optind],
843                   strerror(errno));
844           exit(EXIT_FAILURE);
845         }
846
847         search_in_db(nb_search_conditions, search_conditions, db_file);
848
849         fclose(db_file);
850         optind++;
851       }
852
853       for(n = 0; n < nb_search_conditions; n++) {
854         regfree(&search_conditions[n].regexp);
855         free(search_condition_strings[n]);
856       }
857     }
858   }
859
860   free(db_filename);
861   free(db_filename_regexp_string);
862   free(db_root_path);
863   free(db_filename_list);
864
865   exit(EXIT_SUCCESS);
866 }