Initial commit
[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 <unistd.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <ncurses.h>
37 #include <fcntl.h>
38 #include <sys/ioctl.h>
39 #include <termios.h>
40 #include <regex.h>
41 #include <locale.h>
42 #include <getopt.h>
43 #include <limits.h>
44
45 #define VERSION "1.1.7"
46
47 #define BUFFER_SIZE 16384
48
49 /********************************************************************/
50
51 /* malloc with error checking.  */
52
53 void *safe_malloc(size_t n) {
54   void *p = malloc(n);
55   if(!p && n != 0) {
56     fprintf(stderr,
57             "mymail: can not allocate memory: %s\n", strerror(errno));
58     exit(EXIT_FAILURE);
59   }
60   return p;
61 }
62
63 /*********************************************************************/
64
65 void usage(FILE *out) {
66
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_lines_max, int *nb_lines) {
76
77   char raw_line[BUFFER_SIZE];
78   char *s;
79   FILE *file;
80   int l;
81
82   file = fopen(input_filename, "r");
83
84   if(!file) {
85     fprintf(stderr, "mymail: Can not open `%s'.\n", input_filename);
86     exit(EXIT_FAILURE);
87   }
88
89   while(*nb_lines < nb_lines_max && fgets(raw_line, BUFFER_SIZE, file)) {
90     l = strlen(raw_line);
91     fgets(raw_line + l, BUFFER_SIZE - l, file);
92     for(s = raw_line + strlen(raw_line) - 1; s > raw_line && *s == '\n'; s--) {
93       *s = '\0';
94     }
95     /* store_line(hash_table, raw_line, nb_lines, lines); */
96   }
97
98   fclose(file);
99 }
100
101 /*********************************************************************/
102
103 /* For long options that have no equivalent short option, use a
104    non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
105 enum
106 {
107   OPT_BASH_MODE = CHAR_MAX + 1
108 };
109
110 static struct option long_options[] = {
111   { "help", no_argument, 0, 'h' },
112   { 0, 0, 0, 0 }
113 };
114
115 int main(int argc, char **argv) {
116   int error = 0, show_help = 0;
117   char c;
118
119   setlocale(LC_ALL, "");
120
121   while ((c = getopt_long(argc, argv, "o:s:x:vwmqf:ibzdeajyunt:r:l:c:-h",
122                           long_options, NULL)) != -1) {
123
124     switch(c) {
125
126     case 'h':
127       show_help = 1;
128       break;
129
130     default:
131       error = 1;
132       break;
133     }
134   }
135
136   if(error) {
137     usage(stderr);
138     exit(EXIT_FAILURE);
139   }
140
141   if(show_help) {
142     usage(stdout);
143     exit(EXIT_SUCCESS);
144   }
145
146   exit(EXIT_SUCCESS);
147 }