#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;
}
}
-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;
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) {
{ "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 }
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) {
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");
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");
}
}
+ 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);
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;
}
}
- 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);
}
free(db_filename);
+ free(db_filename_regexp_string);
free(db_root_path);
+ free(db_filename_list);
exit(EXIT_SUCCESS);
}