Embryo of indexing works (and goes pretty fast!)
[mymail.git] / mymail.c
1
2 /*
3  *  Copyright (c) 2013 Francois Fleuret
4  *  Written by Francois Fleuret <francois@fleuret.org>
5  *
6  *  This file is part of mymail.
7  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 /*
23
24   To use it as a super-history-search for bash:
25   mymail --bash <(history)
26
27 */
28
29 #define _GNU_SOURCE
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <locale.h>
37 #include <getopt.h>
38 #include <limits.h>
39 #include <dirent.h>
40 #include <regex.h>
41
42 #define VERSION "0.1"
43
44 #define BUFFER_SIZE 16384
45
46 struct parsable_field {
47   char *name;
48   char *regexp_string;
49   regex_t regexp;
50   FILE *db_file;
51 };
52
53 char *db_filename_prefix;
54
55 /********************************************************************/
56
57 /* malloc with error checking.  */
58
59 void *safe_malloc(size_t n) {
60   void *p = malloc(n);
61   if(!p && n != 0) {
62     fprintf(stderr,
63             "mymail: can not allocate memory: %s\n", strerror(errno));
64     exit(EXIT_FAILURE);
65   }
66   return p;
67 }
68
69 /*********************************************************************/
70
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");
74   fprintf(out, "\n");
75   fprintf(out, "Usage: mymail [options] [<filename1> [<filename2> ...]]\n");
76   fprintf(out, "\n");
77 }
78
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];
82   FILE *file;
83   int in_header;
84   unsigned int position_in_file;
85
86   file = fopen(input_filename, "r");
87
88   if(!file) {
89     fprintf(stderr, "mymail: Can not open `%s'.\n", input_filename);
90     exit(EXIT_FAILURE);
91   }
92
93   in_header = 0;
94
95   position_in_file = 0;
96
97   while(fgets(raw_line, BUFFER_SIZE, file)) {
98     if(strncmp(raw_line, "From ", 5) == 0) {
99       if(in_header) {
100         fprintf(stderr,
101                 "Got a 'From ' in the header in %s:%u.\n",
102                 input_filename, position_in_file);
103         fprintf(stderr, "%s", raw_line);
104         exit(EXIT_FAILURE);
105       }
106       in_header = 1;
107     } else if(strncmp(raw_line, "\n", 1) == 0) {
108       if(in_header) { in_header = 0; }
109     }
110
111     /* if(in_header) { */
112       /* printf("LINE.H %s", raw_line); */
113     /* } else { */
114       /* printf("LINE.B %s", raw_line); */
115     /* } */
116
117     if(in_header) {
118       int f;
119       regmatch_t matches;
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);
125         }
126       }
127     }
128
129     position_in_file += strlen(raw_line);
130   }
131
132   fclose(file);
133 }
134
135 int ignore_entry(const char *name) {
136   return
137     strcmp(name, ".") == 0 ||
138     strcmp(name, "..") == 0 ||
139     (name[0] == '.' && name[1] != '/');
140 }
141
142 void process_entry(const char *dir_name,
143                    int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
144   DIR *dir;
145   struct dirent *dir_e;
146   struct stat sb;
147   char subname[PATH_MAX + 1];
148
149   if(lstat(dir_name, &sb) != 0) {
150     fprintf(stderr,
151             "mymail: Can not stat \"%s\": %s\n",
152             dir_name,
153             strerror(errno));
154     exit(EXIT_FAILURE);
155   } else {
156   }
157
158   if(S_ISLNK(sb.st_mode)) {
159     return;
160   }
161
162   dir = opendir(dir_name);
163
164   if(dir) {
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);
170       }
171     }
172     closedir(dir);
173   } else {
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);
177     }
178   }
179 }
180
181 /*********************************************************************/
182
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.  */
185 enum
186 {
187   OPT_BASH_MODE = CHAR_MAX + 1
188 };
189
190 static struct option long_options[] = {
191   { "help", no_argument, 0, 'h' },
192   { "db-prefix", 1, 0, 'p' },
193   { 0, 0, 0, 0 }
194 };
195
196 static struct parsable_field fields_to_parse[] = {
197   {
198     "from",
199     "^[Ff][Rr][Oo][Mm]: *",
200     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0
201   },
202
203   {
204     "dest",
205     "^\\([Tt][Oo]\\|[Cc][Cc]\\|[Bb][Cc][Cc]\\): *",
206     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0
207   },
208 };
209
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);
213   char c;
214   int f;
215
216   setlocale(LC_ALL, "");
217
218   while ((c = getopt_long(argc, argv, "hp:",
219                           long_options, NULL)) != -1) {
220
221     switch(c) {
222
223     case 'h':
224       show_help = 1;
225       break;
226
227     case 'p':
228       db_filename_prefix = strdup(optarg);
229       break;
230
231     default:
232       error = 1;
233       break;
234     }
235   }
236
237   if(!db_filename_prefix) {
238     db_filename_prefix = strdup("/tmp/mymail_");
239   }
240
241   if(error) {
242     usage(stderr);
243     exit(EXIT_FAILURE);
244   }
245
246   if(show_help) {
247     usage(stdout);
248     exit(EXIT_SUCCESS);
249   }
250
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) {
256       fprintf(stderr,
257               "mymail: Can not open \"%s\" for writing: %s\n",
258               db_filename,
259               strerror(errno));
260       exit(EXIT_FAILURE);
261     }
262
263     printf("Initialized %s.\n", db_filename);
264
265     if(regcomp(&fields_to_parse[f].regexp,
266                fields_to_parse[f].regexp_string,
267                REG_ICASE)) {
268       fprintf(stderr,
269               "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
270               fields_to_parse[f].regexp_string,
271               fields_to_parse[f].name);
272       exit(EXIT_FAILURE);
273     }
274   }
275
276   while(optind < argc) {
277     process_entry(argv[optind],
278                   nb_fields_to_parse, fields_to_parse);
279     optind++;
280   }
281
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);
285   }
286
287   exit(EXIT_SUCCESS);
288 }