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/>.
37 #define BUFFER_SIZE 16384
39 struct parsable_field {
46 char *db_filename_prefix;
48 /********************************************************************/
50 /* malloc with error checking. */
52 void *safe_malloc(size_t n) {
56 "mymail: can not allocate memory: %s\n", strerror(errno));
62 /*********************************************************************/
64 void usage(FILE *out) {
65 fprintf(out, "mymail version %s (%s)\n", VERSION, UNAME);
66 fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
68 fprintf(out, "Usage: mymail [options] [<filename1> [<filename2> ...]]\n");
72 void read_file(const char *input_filename,
73 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
74 char raw_line[BUFFER_SIZE];
77 unsigned int position_in_file;
79 file = fopen(input_filename, "r");
82 fprintf(stderr, "mymail: Can not open `%s'.\n", input_filename);
90 while(fgets(raw_line, BUFFER_SIZE, file)) {
91 if(strncmp(raw_line, "From ", 5) == 0) {
94 "Got a 'From ' in the header in %s:%u.\n",
95 input_filename, position_in_file);
96 fprintf(stderr, "%s", raw_line);
100 } else if(strncmp(raw_line, "\n", 1) == 0) {
101 if(in_header) { in_header = 0; }
104 /* if(in_header) { */
105 /* printf("LINE.H %s", raw_line); */
107 /* printf("LINE.B %s", raw_line); */
113 for(f = 0; f < nb_fields_to_parse; f++) {
114 if(regexec(&fields_to_parse[f].regexp, raw_line, 1, &matches, 0) == 0) {
115 fprintf(fields_to_parse[f].db_file, "%s:%d %s",
116 input_filename, position_in_file,
117 raw_line + matches.rm_eo);
122 position_in_file += strlen(raw_line);
128 int ignore_entry(const char *name) {
130 strcmp(name, ".") == 0 ||
131 strcmp(name, "..") == 0 ||
132 (name[0] == '.' && name[1] != '/');
135 void process_entry(const char *dir_name,
136 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
138 struct dirent *dir_e;
140 char subname[PATH_MAX + 1];
142 if(lstat(dir_name, &sb) != 0) {
144 "mymail: Can not stat \"%s\": %s\n",
151 if(S_ISLNK(sb.st_mode)) {
155 dir = opendir(dir_name);
158 printf("Processing directory '%s'.\n", dir_name);
159 while((dir_e = readdir(dir))) {
160 if(!ignore_entry(dir_e->d_name)) {
161 snprintf(subname, PATH_MAX, "%s/%s", dir_name, dir_e->d_name);
162 process_entry(subname, nb_fields_to_parse, fields_to_parse);
167 if(S_ISREG(sb.st_mode)) {
168 /* printf("Processing regular file '%s'.\n", dir_name); */
169 read_file(dir_name, nb_fields_to_parse, fields_to_parse);
174 /*********************************************************************/
176 /* For long options that have no equivalent short option, use a
177 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
180 OPT_BASH_MODE = CHAR_MAX + 1
183 static struct option long_options[] = {
184 { "help", no_argument, 0, 'h' },
185 { "db-prefix", 1, 0, 'p' },
189 static struct parsable_field fields_to_parse[] = {
192 "^[Ff][Rr][Oo][Mm]: *",
193 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0
198 "^\\([Tt][Oo]\\|[Cc][Cc]\\|[Bb][Cc][Cc]\\): *",
199 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0
203 int main(int argc, char **argv) {
204 int error = 0, show_help = 0;
205 const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
209 setlocale(LC_ALL, "");
211 while ((c = getopt_long(argc, argv, "hp:",
212 long_options, NULL)) != -1) {
221 db_filename_prefix = strdup(optarg);
230 if(!db_filename_prefix) {
231 db_filename_prefix = strdup("/tmp/mymail_");
244 for(f = 0; f < nb_fields_to_parse; f++) {
245 char db_filename[BUFFER_SIZE];
246 sprintf(db_filename, "%s%s", db_filename_prefix, fields_to_parse[f].name);
247 fields_to_parse[f].db_file = fopen(db_filename, "w");
248 if(!fields_to_parse[f].db_file) {
250 "mymail: Can not open \"%s\" for writing: %s\n",
256 printf("Initialized %s.\n", db_filename);
258 if(regcomp(&fields_to_parse[f].regexp,
259 fields_to_parse[f].regexp_string,
262 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
263 fields_to_parse[f].regexp_string,
264 fields_to_parse[f].name);
269 while(optind < argc) {
270 process_entry(argv[optind],
271 nb_fields_to_parse, fields_to_parse);
275 for(f = 0; f < nb_fields_to_parse; f++) {
276 fclose(fields_to_parse[f].db_file);
277 regfree(&fields_to_parse[f].regexp);