Started the indexing part.
[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
41 #define VERSION "0.1"
42
43 #define BUFFER_SIZE 16384
44
45 struct parsable_field {
46   char *regexp;
47   char *db_filename;
48 };
49
50 /********************************************************************/
51
52 /* malloc with error checking.  */
53
54 void *safe_malloc(size_t n) {
55   void *p = malloc(n);
56   if(!p && n != 0) {
57     fprintf(stderr,
58             "mymail: can not allocate memory: %s\n", strerror(errno));
59     exit(EXIT_FAILURE);
60   }
61   return p;
62 }
63
64 /*********************************************************************/
65
66 void usage(FILE *out) {
67   fprintf(out, "mymail version %s (%s)\n", VERSION, UNAME);
68   fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
69   fprintf(out, "\n");
70   fprintf(out, "Usage: mymail [options] [<filename1> [<filename2> ...]]\n");
71   fprintf(out, "\n");
72 }
73
74 void read_file(const char *input_filename,
75                int nb_fields_to_parse, struct parsable_field *fields_to_parse,
76                FILE **db_files) {
77   char raw_line[BUFFER_SIZE];
78   FILE *file;
79   int in_header;
80
81   file = fopen(input_filename, "r");
82
83   if(!file) {
84     fprintf(stderr, "mymail: Can not open `%s'.\n", input_filename);
85     exit(EXIT_FAILURE);
86   }
87
88   in_header = 0;
89
90   while(fgets(raw_line, BUFFER_SIZE, file)) {
91     if(strncmp(raw_line, "From ", 5) == 0) {
92       if(in_header) {
93         fprintf(stderr, "Got a 'From ' in the header.\n");
94         exit(EXIT_FAILURE);
95       }
96       in_header = 1;
97     } else if(strncmp(raw_line, "\n", 1) == 0) {
98       if(in_header) { in_header = 0; }
99     }
100
101     if(in_header) {
102       printf("LINE.H %s", raw_line);
103     } else {
104       printf("LINE.B %s", raw_line);
105     }
106   }
107
108   fclose(file);
109 }
110
111 int ignore_entry(const char *name) {
112   return
113     strcmp(name, ".") == 0 ||
114     strcmp(name, "..") == 0 ||
115     (name[0] == '.' && name[1] != '/');
116 }
117
118 void process_entry(const char *dir_name,
119                    int nb_fields_to_parse, struct parsable_field *fields_to_parse,
120                    FILE **db_files) {
121   DIR *dir;
122   struct dirent *dir_e;
123   struct stat sb;
124   char subname[PATH_MAX + 1];
125
126   if(lstat(dir_name, &sb) != 0) {
127     fprintf(stderr,
128             "mymail: Can not stat \"%s\": %s\n",
129             dir_name,
130             strerror(errno));
131     exit(EXIT_FAILURE);
132   } else {
133   }
134
135   if(S_ISLNK(sb.st_mode)) {
136     return;
137   }
138
139   dir = opendir(dir_name);
140
141   if(dir) {
142     printf("Processing directory '%s'.\n", dir_name);
143     while((dir_e = readdir(dir))) {
144       if(!ignore_entry(dir_e->d_name)) {
145         snprintf(subname, PATH_MAX, "%s/%s", dir_name, dir_e->d_name);
146         process_entry(subname, nb_fields_to_parse, fields_to_parse, db_files);
147       }
148     }
149     closedir(dir);
150   } else {
151     if(S_ISREG(sb.st_mode)) {
152       printf("Processing regular file '%s'.\n", dir_name);
153       read_file(dir_name, nb_fields_to_parse, fields_to_parse, db_files);
154     }
155   }
156 }
157
158 /*********************************************************************/
159
160 /* For long options that have no equivalent short option, use a
161    non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
162 enum
163 {
164   OPT_BASH_MODE = CHAR_MAX + 1
165 };
166
167 static struct option long_options[] = {
168   { "help", no_argument, 0, 'h' },
169   { 0, 0, 0, 0 }
170 };
171
172 static struct parsable_field fields_to_parse[] = {
173   { "^[Tt][Oo]:", "/tmp/mymail-to" }
174 };
175
176 int main(int argc, char **argv) {
177   int error = 0, show_help = 0;
178   const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
179   char c;
180   int f;
181   FILE **db_files;
182
183   setlocale(LC_ALL, "");
184
185   while ((c = getopt_long(argc, argv, "h",
186                           long_options, NULL)) != -1) {
187
188     switch(c) {
189
190     case 'h':
191       show_help = 1;
192       break;
193
194     default:
195       error = 1;
196       break;
197     }
198   }
199
200   if(error) {
201     usage(stderr);
202     exit(EXIT_FAILURE);
203   }
204
205   if(show_help) {
206     usage(stdout);
207     exit(EXIT_SUCCESS);
208   }
209
210   db_files = safe_malloc(nb_fields_to_parse * sizeof(FILE *));
211
212   for(f = 0; f < nb_fields_to_parse; f++) {
213     db_files[f] = fopen(fields_to_parse[f].db_filename, "w");
214     if(!db_files[f]) {
215       fprintf(stderr,
216               "mymail: Can not open \"%s\" for writing: %s\n",
217               fields_to_parse[f].db_filename,
218               strerror(errno));
219     }
220   }
221
222   while(optind < argc) {
223     process_entry(argv[optind],
224                   nb_fields_to_parse, fields_to_parse,
225                   db_files);
226     optind++;
227   }
228
229   for(f = 0; f < nb_fields_to_parse; f++) {
230     fclose(db_files[f]);
231   }
232
233   free(db_files);
234
235   exit(EXIT_SUCCESS);
236 }