+ }
+
+ strcpy(current_mail_filename, "");
+
+ while(nb_extracted_mails < global_nb_mails_max &&
+ fgets(raw_db_line, BUFFER_SIZE, db_file)) {
+
+ /* Removes the CR */
+ char *s = raw_db_line;
+ while(*s && *s != '\n') { s++; }
+ *s = '\0';
+
+ db_value = parse_token(db_key_string, TOKEN_BUFFER_SIZE, ' ', raw_db_line);
+
+ if(strcmp("mail", db_key_string) == 0) {
+ if(current_mail_filename[0]) {
+ if(check_full_mail_match(current_mail_filename,
+ mail_time,
+ nb_search_conditions, search_conditions,
+ nb_body_conditions, hits, current_position_in_mail)) {
+ extract_mail(current_mail_filename, current_position_in_mail, output_file);
+ nb_extracted_mails++;
+ }
+ }
+
+ for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
+ db_value = parse_token(position_in_file_string, TOKEN_BUFFER_SIZE, ' ', db_value);
+ strncpy(current_mail_filename, db_value, PATH_MAX + 1);
+ current_position_in_mail = atol(position_in_file_string);
+ }
+
+ else {
+ db_key = -1;
+ for(m = 0; (m < MAX_ID) && db_key == -1; m++) {
+ if(strncmp(field_keys[m], db_key_string, strlen(db_key_string)) == 0) {
+ db_key = m;
+ }
+ }
+
+ for(n = 0; n < nb_search_conditions; n++) {
+ hits[n] |= db_line_match_search(&search_conditions[n],
+ db_key, db_value);
+ }
+
+ if(need_time) {
+ update_time(db_key, db_value, &mail_time);
+ }
+ }
+ }
+
+ if(nb_extracted_mails < global_nb_mails_max &&
+ current_mail_filename[0] &&
+ check_full_mail_match(current_mail_filename,
+ mail_time,
+ nb_search_conditions, search_conditions,
+ nb_body_conditions, hits, current_position_in_mail)) {
+ extract_mail(current_mail_filename, current_position_in_mail, output_file);
+ nb_extracted_mails++;
+ }
+
+ fclose(db_file);
+
+ if(!global_quiet) {
+ printf("done.\n");
+ fflush(stdout);
+ }
+
+ return nb_extracted_mails;
+}
+
+int recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp,
+ int nb_extracted_mails,
+ int nb_search_conditions,
+ struct search_condition *search_conditions,
+ FILE *output_file) {
+ DIR *dir;
+ struct dirent *dir_e;
+ struct stat sb;
+ char subname[PATH_MAX + 1];
+
+ if(lstat(entry_name, &sb) != 0) {
+ fprintf(stderr,
+ "mymail: Cannot stat \"%s\": %s\n",
+ entry_name,
+ strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+
+ /* printf("recursive_search_in_db %s\n", entry_name); */
+
+ dir = opendir(entry_name);
+
+ if(dir) {
+ while((dir_e = readdir(dir)) &&
+ nb_extracted_mails < global_nb_mails_max) {
+ if(!ignore_entry(dir_e->d_name)) {
+ snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
+ nb_extracted_mails = recursive_search_in_db(subname, db_filename_regexp,
+ nb_extracted_mails,
+ nb_search_conditions, search_conditions,
+ output_file);
+ }
+ }
+ closedir(dir);
+ }
+
+ else {
+ const char *s = entry_name, *filename = entry_name;
+ while(*s) { if(*s == '/') { filename = s+1; } s++; }
+
+ if(regexec(db_filename_regexp, filename, 0, 0, 0) == 0) {
+ nb_extracted_mails =
+ search_in_db(entry_name,
+ nb_extracted_mails,
+ nb_search_conditions, search_conditions, output_file);
+ }
+ }
+
+ return nb_extracted_mails;
+}
+
+/*********************************************************************/
+
+void index_one_mbox_line(unsigned int nb_fields_to_parse,
+ struct parsable_field *fields_to_parse,
+ char *raw_mbox_line, FILE *db_file) {
+ regmatch_t matches;
+ unsigned int f;
+ for(f = 0; f < nb_fields_to_parse; f++) {
+ if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
+ fprintf(db_file, "%s %s\n",
+ field_keys[fields_to_parse[f].id],
+ raw_mbox_line + matches.rm_eo);
+ }
+ }
+}
+
+void index_mbox(const char *mbox_filename,
+ int nb_fields_to_parse, struct parsable_field *fields_to_parse,
+ FILE *db_file) {
+ char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
+ char *end_of_full_line;
+ FILE *file;
+ int in_header, new_header;
+ unsigned long int position_in_file;
+
+ file = safe_fopen(mbox_filename, "r", "mbox for indexing");
+
+ in_header = 0;
+ new_header = 0;
+
+ position_in_file = 0;
+ end_of_full_line = 0;
+ full_line[0] = '\0';
+
+ while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
+ if(is_a_leading_from_line(raw_mbox_line)) {
+ /* This starts a new mail */
+ if(in_header) {
+ fprintf(stderr,
+ "Got a ^\"From \" in the header in %s:%lu.\n",
+ mbox_filename, position_in_file);
+ fprintf(stderr, "%s", raw_mbox_line);
+ }
+
+ /* printf("LEADING_LINE %s", raw_mbox_line); */
+
+ in_header = 1;
+ new_header = 1;
+ } else if(raw_mbox_line[0] == '\n') {
+ if(in_header) {
+ in_header = 0;
+ /* We leave the header, index the current line */
+ if(full_line[0]) {
+ /* printf("INDEX %s\n", full_line); */
+ index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
+ }
+ end_of_full_line = full_line;
+ *end_of_full_line = '\0';
+ }
+ }
+
+ if(in_header) {
+ if(new_header) {
+ fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
+ new_header = 0;
+ }
+
+ if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
+ /* Continuation of a line */
+ char *start = raw_mbox_line;
+ while(*start == ' ' || *start == '\t') start++;
+ *(end_of_full_line++) = ' ';
+ strcpy(end_of_full_line, start);
+ while(*end_of_full_line && *end_of_full_line != '\n') {
+ end_of_full_line++;
+ }
+ *end_of_full_line = '\0';
+ }
+
+ else {
+ /* Start a new header line, not a continuation */
+
+ if(full_line[0]) {
+ /* printf("INDEX %s\n", full_line); */
+ index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
+ }
+
+ end_of_full_line = full_line;
+ strcpy(end_of_full_line, raw_mbox_line);
+ while(*end_of_full_line && *end_of_full_line != '\n') {
+ end_of_full_line++;
+ }
+ *end_of_full_line = '\0';
+ }
+
+ }
+
+ position_in_file += strlen(raw_mbox_line);