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