3 * Copyright (c) 2013 Francois Fleuret
4 * Written by Francois Fleuret <francois@fleuret.org>
6 * This file is part of mymail.
8 * mymail is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 3 as
10 * published by the Free Software Foundation.
12 * mymail is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with mymail. If not, see <http://www.gnu.org/licenses/>.
24 To use it as a super-history-search for bash:
25 mymail --bash <(history)
44 #define BUFFER_SIZE 16384
46 struct parsable_field {
53 char *db_filename_prefix;
55 /********************************************************************/
57 /* malloc with error checking. */
59 void *safe_malloc(size_t n) {
63 "mymail: can not allocate memory: %s\n", strerror(errno));
69 /*********************************************************************/
71 void usage(FILE *out) {
72 fprintf(out, "mymail version %s (%s)\n", VERSION, UNAME);
73 fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
75 fprintf(out, "Usage: mymail [options] [<filename1> [<filename2> ...]]\n");
79 void read_file(const char *input_filename,
80 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
81 char raw_line[BUFFER_SIZE];
84 unsigned int position_in_file;
86 file = fopen(input_filename, "r");
89 fprintf(stderr, "mymail: Can not open `%s'.\n", input_filename);
97 while(fgets(raw_line, BUFFER_SIZE, file)) {
98 if(strncmp(raw_line, "From ", 5) == 0) {
101 "Got a 'From ' in the header in %s:%u.\n",
102 input_filename, position_in_file);
103 fprintf(stderr, "%s", raw_line);
107 } else if(strncmp(raw_line, "\n", 1) == 0) {
108 if(in_header) { in_header = 0; }
111 /* if(in_header) { */
112 /* printf("LINE.H %s", raw_line); */
114 /* printf("LINE.B %s", raw_line); */
120 for(f = 0; f < nb_fields_to_parse; f++) {
121 if(regexec(&fields_to_parse[f].regexp, raw_line, 1, &matches, 0) == 0) {
122 fprintf(fields_to_parse[f].db_file, "%s:%d %s",
123 input_filename, position_in_file,
124 raw_line + matches.rm_eo);
129 position_in_file += strlen(raw_line);
135 int ignore_entry(const char *name) {
137 strcmp(name, ".") == 0 ||
138 strcmp(name, "..") == 0 ||
139 (name[0] == '.' && name[1] != '/');
142 void process_entry(const char *dir_name,
143 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
145 struct dirent *dir_e;
147 char subname[PATH_MAX + 1];
149 if(lstat(dir_name, &sb) != 0) {
151 "mymail: Can not stat \"%s\": %s\n",
158 if(S_ISLNK(sb.st_mode)) {
162 dir = opendir(dir_name);
165 printf("Processing directory '%s'.\n", dir_name);
166 while((dir_e = readdir(dir))) {
167 if(!ignore_entry(dir_e->d_name)) {
168 snprintf(subname, PATH_MAX, "%s/%s", dir_name, dir_e->d_name);
169 process_entry(subname, nb_fields_to_parse, fields_to_parse);
174 if(S_ISREG(sb.st_mode)) {
175 /* printf("Processing regular file '%s'.\n", dir_name); */
176 read_file(dir_name, nb_fields_to_parse, fields_to_parse);
181 /*********************************************************************/
183 /* For long options that have no equivalent short option, use a
184 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
187 OPT_BASH_MODE = CHAR_MAX + 1
190 static struct option long_options[] = {
191 { "help", no_argument, 0, 'h' },
192 { "db-prefix", 1, 0, 'p' },
196 static struct parsable_field fields_to_parse[] = {
199 "^[Ff][Rr][Oo][Mm]: *",
200 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0
205 "^\\([Tt][Oo]\\|[Cc][Cc]\\|[Bb][Cc][Cc]\\): *",
206 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0
210 int main(int argc, char **argv) {
211 int error = 0, show_help = 0;
212 const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
216 setlocale(LC_ALL, "");
218 while ((c = getopt_long(argc, argv, "hp:",
219 long_options, NULL)) != -1) {
228 db_filename_prefix = strdup(optarg);
237 if(!db_filename_prefix) {
238 db_filename_prefix = strdup("/tmp/mymail_");
251 for(f = 0; f < nb_fields_to_parse; f++) {
252 char db_filename[BUFFER_SIZE];
253 sprintf(db_filename, "%s%s", db_filename_prefix, fields_to_parse[f].name);
254 fields_to_parse[f].db_file = fopen(db_filename, "w");
255 if(!fields_to_parse[f].db_file) {
257 "mymail: Can not open \"%s\" for writing: %s\n",
263 printf("Initialized %s.\n", db_filename);
265 if(regcomp(&fields_to_parse[f].regexp,
266 fields_to_parse[f].regexp_string,
269 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
270 fields_to_parse[f].regexp_string,
271 fields_to_parse[f].name);
276 while(optind < argc) {
277 process_entry(argv[optind],
278 nb_fields_to_parse, fields_to_parse);
282 for(f = 0; f < nb_fields_to_parse; f++) {
283 fclose(fields_to_parse[f].db_file);
284 regfree(&fields_to_parse[f].regexp);