Embryo of search seems to work.
authorFrancois Fleuret <francois@fleuret.org>
Sun, 27 Jan 2013 23:20:25 +0000 (00:20 +0100)
committerFrancois Fleuret <francois@fleuret.org>
Sun, 27 Jan 2013 23:20:25 +0000 (00:20 +0100)
mymail.c

index 25121a4..877215c 100644 (file)
--- a/mymail.c
+++ b/mymail.c
@@ -71,6 +71,48 @@ void usage(FILE *out) {
   fprintf(out, "\n");
 }
 
+/*********************************************************************/
+
+void search_in_db(const char *search_name, const char *search_regexp_string,
+                  FILE *db_file) {
+  char raw_line[BUFFER_SIZE];
+  char current_mail[BUFFER_SIZE];
+  char *name, *value;
+  regex_t regexp;
+
+  if(regcomp(&regexp,
+             search_regexp_string,
+             REG_ICASE)) {
+    fprintf(stderr,
+            "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
+            search_regexp_string,
+            search_name);
+    exit(EXIT_FAILURE);
+  }
+
+  while(fgets(raw_line, BUFFER_SIZE, db_file)) {
+    name = raw_line;
+    value = raw_line;
+    while(*value && *value != ' ') value++;
+    *value = '\0'; value++;
+    while(*value && *value == ' ') value++;
+
+    /* printf("LINE [%s] %s", name, value); */
+
+    if(strcmp("mail", name) == 0) {
+      strcpy(current_mail, value);
+      /* printf("READING [%s]\n", current_mail); */
+    } else {
+      if(strcmp(search_name, name) == 0 && regexec(&regexp, value, 0, 0, 0) == 0) {
+        printf("%s", current_mail);
+      }
+    }
+  }
+}
+
+
+/*********************************************************************/
+
 void read_file(const char *input_filename,
                int nb_fields_to_parse, struct parsable_field *fields_to_parse,
                FILE *db_file) {
@@ -306,22 +348,37 @@ int main(int argc, char **argv) {
 
   else {
     if(search_pattern) {
+      FILE *db_file;
       char *search_name;
-      char *search_regexp;
+      char *search_regexp_string;
       search_name = search_pattern;
-      search_regexp = search_pattern;
-      while(*search_regexp && *search_regexp != ' ') search_regexp++;
-      *search_regexp = '\0'; search_regexp++;
-      while(*search_regexp && *search_regexp == ' ') search_regexp++;
-      if(!*search_regexp) {
+      search_regexp_string = search_pattern;
+      while(*search_regexp_string && *search_regexp_string != ' ') search_regexp_string++;
+      *search_regexp_string = '\0'; search_regexp_string++;
+      while(*search_regexp_string && *search_regexp_string == ' ') search_regexp_string++;
+      if(!*search_regexp_string) {
         fprintf(stderr,
                 "Syntax error in the search pattern.\n");
         exit(EXIT_FAILURE);
       }
+
       printf("Starting search in %s for field \"%s\" matching \"%s\".\n",
              db_filename,
              search_name,
-             search_regexp);
+             search_regexp_string);
+
+      db_file = fopen(db_filename, "r");
+
+      if(!db_file) {
+        fprintf(stderr,
+                "mymail: Can not open \"%s\" for reading: %s\n",
+                db_filename,
+                strerror(errno));
+        exit(EXIT_FAILURE);
+      }
+      search_in_db(search_name, search_regexp_string, db_file);
+
+      fclose(db_file);
       free(search_pattern);
     }
   }