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)
43 #define BUFFER_SIZE 16384
45 struct parsable_field {
50 /********************************************************************/
52 /* malloc with error checking. */
54 void *safe_malloc(size_t n) {
58 "mymail: can not allocate memory: %s\n", strerror(errno));
64 /*********************************************************************/
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");
70 fprintf(out, "Usage: mymail [options] [<filename1> [<filename2> ...]]\n");
74 void read_file(const char *input_filename,
75 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
77 char raw_line[BUFFER_SIZE];
81 file = fopen(input_filename, "r");
84 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) {
93 fprintf(stderr, "Got a 'From ' in the header.\n");
97 } else if(strncmp(raw_line, "\n", 1) == 0) {
98 if(in_header) { in_header = 0; }
102 printf("LINE.H %s", raw_line);
104 printf("LINE.B %s", raw_line);
111 int ignore_entry(const char *name) {
113 strcmp(name, ".") == 0 ||
114 strcmp(name, "..") == 0 ||
115 (name[0] == '.' && name[1] != '/');
118 void process_entry(const char *dir_name,
119 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
122 struct dirent *dir_e;
124 char subname[PATH_MAX + 1];
126 if(lstat(dir_name, &sb) != 0) {
128 "mymail: Can not stat \"%s\": %s\n",
135 if(S_ISLNK(sb.st_mode)) {
139 dir = opendir(dir_name);
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);
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);
158 /*********************************************************************/
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. */
164 OPT_BASH_MODE = CHAR_MAX + 1
167 static struct option long_options[] = {
168 { "help", no_argument, 0, 'h' },
172 static struct parsable_field fields_to_parse[] = {
173 { "^[Tt][Oo]:", "/tmp/mymail-to" }
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);
183 setlocale(LC_ALL, "");
185 while ((c = getopt_long(argc, argv, "h",
186 long_options, NULL)) != -1) {
210 db_files = safe_malloc(nb_fields_to_parse * sizeof(FILE *));
212 for(f = 0; f < nb_fields_to_parse; f++) {
213 db_files[f] = fopen(fields_to_parse[f].db_filename, "w");
216 "mymail: Can not open \"%s\" for writing: %s\n",
217 fields_to_parse[f].db_filename,
222 while(optind < argc) {
223 process_entry(argv[optind],
224 nb_fields_to_parse, fields_to_parse,
229 for(f = 0; f < nb_fields_to_parse; f++) {