From: Francois Fleuret Date: Sat, 2 Feb 2013 23:08:41 +0000 (+0100) Subject: Now use db_filename_regexp_string for recursive search of db files. X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=commitdiff_plain;h=e940e981c95741ead06c3a862e070f7183f8c90c;p=mymail.git Now use db_filename_regexp_string for recursive search of db files. --- diff --git a/mymail.c b/mymail.c index 88e0954..f3fc8d5 100644 --- a/mymail.c +++ b/mymail.c @@ -52,7 +52,9 @@ #define BUFFER_SIZE 65536 char *db_filename; +char *db_filename_regexp_string; char *db_root_path; +char *db_filename_list; int paranoid; int action_index; @@ -281,9 +283,8 @@ void search_in_db(int nb_search_requests, } } -void recursive_search_in_db(const char *entry_name, - int nb_search_requests, - struct search_request *search_requests) { +void recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp, + int nb_search_requests, struct search_request *search_requests) { DIR *dir; struct dirent *dir_e; struct stat sb; @@ -304,17 +305,18 @@ void recursive_search_in_db(const char *entry_name, while((dir_e = readdir(dir))) { if(!ignore_entry(dir_e->d_name)) { snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name); - recursive_search_in_db(subname, - nb_search_requests, - search_requests); + recursive_search_in_db(subname, db_filename_regexp, + nb_search_requests, search_requests); } } closedir(dir); - } else { + } + + else { const char *s = entry_name, *filename = entry_name; while(*s) { if(*s == '/') { filename = s+1; } s++; } - if(strcmp(filename, db_filename) == 0) { + if(regexec(db_filename_regexp, filename, 0, 0, 0) == 0) { FILE *db_file = fopen(entry_name, "r"); if(!db_file) { @@ -494,7 +496,9 @@ static struct option long_options[] = { { "help", no_argument, 0, 'h' }, { "version", no_argument, 0, 'v' }, { "db-file", 1, 0, 'd' }, + { "db-pattern", 1, 0, 'p' }, { "db-root", 1, 0, 'r' }, + { "db-list", 1, 0, 'l' }, { "search", 1, 0, 's' }, { "index", 0, 0, 'i' }, { 0, 0, 0, 0 } @@ -518,12 +522,13 @@ int main(int argc, char **argv) { action_index = 0; db_filename = 0; db_root_path = 0; + db_filename_list = 0; setlocale(LC_ALL, ""); nb_search_requests = 0; - while ((c = getopt_long(argc, argv, "hvip:s:d:r:", + while ((c = getopt_long(argc, argv, "hvip:s:d:r:l:", long_options, NULL)) != -1) { switch(c) { @@ -544,10 +549,18 @@ int main(int argc, char **argv) { db_filename = strdup(optarg); break; + case 'p': + db_filename_regexp_string = strdup(optarg); + break; + case 'r': db_root_path = strdup(optarg); break; + case 'l': + db_filename_list = strdup(optarg); + break; + case 's': if(nb_search_requests == MAX_NB_SEARCH_REQUESTS) { fprintf(stderr, "mymail: Too many search patterns.\n"); @@ -572,6 +585,16 @@ int main(int argc, char **argv) { db_filename = strdup(default_db_filename); } + if(!db_filename_regexp_string) { + char *default_db_filename_regexp_string = getenv("MYMAIL_DB_PATTERN"); + + if(!default_db_filename_regexp_string) { + default_db_filename_regexp_string = "^mymail.db$"; + } + + db_filename_regexp_string = strdup(default_db_filename_regexp_string); + } + if(!db_root_path) { char *default_db_root_path = getenv("MYMAIL_DB_ROOT"); @@ -580,6 +603,14 @@ int main(int argc, char **argv) { } } + if(!db_filename_list) { + char *default_db_filename_list = getenv("MYMAIL_DB_LIST"); + + if(default_db_filename_list) { + db_filename_list = strdup(default_db_filename_list); + } + } + if(error) { print_usage(stderr); exit(EXIT_FAILURE); @@ -633,12 +664,6 @@ int main(int argc, char **argv) { else { - if(!db_root_path) { - fprintf(stderr, - "mymail: db root path is not set\n"); - exit(EXIT_FAILURE); - } - if(nb_search_requests > 0) { struct search_request search_requests[MAX_NB_SEARCH_REQUESTS]; char *search_field, *search_regexp_string; @@ -680,8 +705,76 @@ int main(int argc, char **argv) { } } - recursive_search_in_db(db_root_path, - nb_search_requests, search_requests); + /* Recursive search if db_root_path is set */ + + if(db_root_path) { + regex_t db_filename_regexp; + if(regcomp(&db_filename_regexp, + db_filename_regexp_string, + 0)) { + fprintf(stderr, + "mymail: Syntax error in regexp \"%s\".\n", + db_filename_regexp_string); + exit(EXIT_FAILURE); + } + + recursive_search_in_db(db_root_path, &db_filename_regexp, + nb_search_requests, search_requests); + + regfree(&db_filename_regexp); + } + + /* Search in all db files listed in db_filename_list */ + + if(db_filename_list) { + char db_filename[PATH_MAX + 1]; + char *s, *t; + FILE *db_file; + + s = db_filename_list; + + while(*s) { + t = db_filename; + while(*s == ';') { s++; } + while(*s && *s != ';') { *t++ = *s++; } + *t++ = '\0'; + + if(db_filename[0]) { + db_file = fopen(db_filename, "r"); + + if(!db_file) { + fprintf(stderr, + "mymail: Cannot open \"%s\" for reading: %s\n", + argv[optind], + strerror(errno)); + exit(EXIT_FAILURE); + } + + search_in_db(nb_search_requests, search_requests, db_file); + + fclose(db_file); + } + } + } + + /* Search in all db files listed in the command arguments */ + + while(optind < argc) { + FILE *db_file = fopen(argv[optind], "r"); + + if(!db_file) { + fprintf(stderr, + "mymail: Cannot open \"%s\" for reading: %s\n", + argv[optind], + strerror(errno)); + exit(EXIT_FAILURE); + } + + search_in_db(nb_search_requests, search_requests, db_file); + + fclose(db_file); + optind++; + } for(n = 0; n < nb_search_requests; n++) { regfree(&search_requests[n].regexp); @@ -691,7 +784,9 @@ int main(int argc, char **argv) { } free(db_filename); + free(db_filename_regexp_string); free(db_root_path); + free(db_filename_list); exit(EXIT_SUCCESS); }