X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=blobdiff_plain;f=mymail.c;h=21ad5eabff678aaad71b8bb24b164803369f06ea;hb=e46b2c9e8a690c4767e07a3f992ec58716a50086;hp=88e09549769aa819540c9571d11047eea49287e5;hpb=93e55a350d0053c490a31e4a671925eb9c2fb6ee;p=mymail.git diff --git a/mymail.c b/mymail.c index 88e0954..21ad5ea 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; @@ -163,20 +165,24 @@ void print_usage(FILE *out) { print_version(out); fprintf(out, "Written by Francois Fleuret .\n"); fprintf(out, "\n"); - fprintf(out, "Usage: mymail [options] [ [ ...]]\n"); + fprintf(out, "Usage: mymail [options] [ [ ...]| [ ...]]\n"); fprintf(out, "\n"); fprintf(out, " -h, --help\n"); fprintf(out, " show this help\n"); fprintf(out, " -v, --version\n"); fprintf(out, " print the version number\n"); - fprintf(out, " -i, --index\n"); - fprintf(out, " index mails\n"); + fprintf(out, " -p , --db-pattern \n"); + fprintf(out, " set the db filename pattern for recursive search\n"); + fprintf(out, " -r , --db-root \n"); + fprintf(out, " set the db root path for recursive search\n"); + fprintf(out, " -l , --db-list \n"); + fprintf(out, " set the semicolon-separated list of db files for search\n"); fprintf(out, " -s , --search \n"); - fprintf(out, " search for matching mails in the data-base file\n"); + fprintf(out, " search for matching mails in the db file\n"); fprintf(out, " -d , --db-file \n"); - fprintf(out, " set the data-base file\n"); - fprintf(out, " -r , --db-root \n"); - fprintf(out, " set the data-base root path for recursive search\n"); + fprintf(out, " set the db filename for indexing\n"); + fprintf(out, " -i, --index\n"); + fprintf(out, " index mails\n"); } /*********************************************************************/ @@ -281,9 +287,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 +309,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 +500,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 +526,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 +553,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 +589,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 +607,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 +668,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 +709,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 +788,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); }