From: Francois Fleuret Date: Fri, 1 Feb 2013 23:14:32 +0000 (+0100) Subject: Introduced struct search_request. X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=commitdiff_plain;h=4494ac307c7d814e9ee3a1e6e1ead3e1cb35f781;p=mymail.git Introduced struct search_request. --- diff --git a/mymail.c b/mymail.c index abd4658..35a0344 100644 --- a/mymail.c +++ b/mymail.c @@ -68,6 +68,12 @@ static char *field_names[] = { "part" }; +struct search_request { + int field_id; + int negation; + regex_t regexp; +}; + struct parsable_field { int id; char *regexp_string; @@ -141,17 +147,17 @@ int ignore_entry(const char *name) { (name[0] == '.' && name[1] != '/'); } -int mbox_line_match_search(int search_id, regex_t *search_regexp, +int mbox_line_match_search(struct search_request *request, int mbox_id, char *mbox_value) { return - (search_id == mbox_id || - (search_id == ID_PARTICIPANT && (mbox_id == ID_FROM || mbox_id == ID_DEST))) + (request->field_id == mbox_id || + (request->field_id == ID_PARTICIPANT && (mbox_id == ID_FROM || mbox_id == ID_DEST))) && - regexec(search_regexp, mbox_value, 0, 0, 0) == 0; + regexec(&request->regexp, mbox_value, 0, 0, 0) == 0; } void search_in_db(int nb_search_patterns, - int *search_ids, char **search_regexp_strings, + struct search_request *search_requests, FILE *db_file) { int hits[MAX_NB_SEARCH_PATTERNS]; char raw_db_line[BUFFER_SIZE]; @@ -160,21 +166,8 @@ void search_in_db(int nb_search_patterns, unsigned long int current_position_in_mail; char *mbox_name, *mbox_value; int mbox_id; - regex_t search_regexps[MAX_NB_SEARCH_PATTERNS]; int already_written, m, n; - for(n = 0; n < nb_search_patterns; n++) { - if(regcomp(&search_regexps[n], - search_regexp_strings[n], - REG_ICASE)) { - fprintf(stderr, - "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n", - search_regexp_strings[n], - field_names[search_ids[n]]); - exit(EXIT_FAILURE); - } - } - current_position_in_mail = 0; already_written = 0; @@ -232,20 +225,16 @@ void search_in_db(int nb_search_patterns, } } for(n = 0; n < nb_search_patterns; n++) { - hits[n] |= mbox_line_match_search(search_ids[n], &search_regexps[n], + hits[n] |= mbox_line_match_search(&search_requests[n], mbox_id, mbox_value); } } } - - for(n = 0; n < nb_search_patterns; n++) { - regfree(&search_regexps[n]); - } } void recursive_search_in_db(const char *entry_name, int nb_search_patterns, - int *search_ids, char **search_regexp_strings) { + struct search_request *search_requests) { DIR *dir; struct dirent *dir_e; struct stat sb; @@ -268,7 +257,7 @@ void recursive_search_in_db(const char *entry_name, snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name); recursive_search_in_db(subname, nb_search_patterns, - search_ids, search_regexp_strings); + search_requests); } } closedir(dir); @@ -301,8 +290,7 @@ void recursive_search_in_db(const char *entry_name, exit(EXIT_FAILURE); } - search_in_db(nb_search_patterns, search_ids, search_regexp_strings, - db_file); + search_in_db(nb_search_patterns, search_requests, db_file); fclose(db_file); } @@ -492,7 +480,7 @@ int main(int argc, char **argv) { char *search_pattern[MAX_NB_SEARCH_PATTERNS]; /* for(f = 0; f < argc; f++) { */ - /* printf("arg %d \"%s\"\n", f, argv[f]); */ + /* printf("arg %d \"%s\"\n", f, argv[f]); */ /* } */ paranoid = 0; @@ -622,28 +610,33 @@ int main(int argc, char **argv) { else { if(nb_search_patterns > 0) { - int search_ids[MAX_NB_SEARCH_PATTERNS]; - char *search_regexp_strings[MAX_NB_SEARCH_PATTERNS]; + struct search_request search_requests[MAX_NB_SEARCH_PATTERNS]; + char *search_regexp_string; int m, n; for(n = 0; n < nb_search_patterns; n++) { - search_regexp_strings[n] = segment_next_field(search_pattern[n]); - search_ids[n] = -1; - for(m = 0; (m < MAX_ID) && search_ids[n] == -1; m++) { + search_regexp_string = segment_next_field(search_pattern[n]); + + search_requests[n].field_id = -1; + for(m = 0; (m < MAX_ID) && search_requests[n].field_id == -1; m++) { if(strncmp(field_names[m], search_pattern[n], strlen(search_pattern[n])) == 0) { - search_ids[n] = m; + search_requests[n].field_id = m; } } - } - if(!*search_regexp_strings) { - fprintf(stderr, - "Syntax error in the search pattern.\n"); - exit(EXIT_FAILURE); + if(regcomp(&search_requests[n].regexp, + search_regexp_string, + REG_ICASE)) { + fprintf(stderr, + "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n", + search_regexp_string, + field_names[search_requests[n].field_id]); + exit(EXIT_FAILURE); + } } recursive_search_in_db(db_root_path, - nb_search_patterns, search_ids, search_regexp_strings); + nb_search_patterns, search_requests); for(n = 0; n < nb_search_patterns; n++) { free(search_pattern[n]);