Cosmetics.
authorFrancois Fleuret <francois@fleuret.org>
Tue, 12 Feb 2013 07:55:15 +0000 (08:55 +0100)
committerFrancois Fleuret <francois@fleuret.org>
Tue, 12 Feb 2013 07:55:15 +0000 (08:55 +0100)
mymail.c

index 5287301..5fb92be 100644 (file)
--- a/mymail.c
+++ b/mymail.c
@@ -53,7 +53,7 @@
 #define BUFFER_SIZE 65536
 #define TOKEN_BUFFER_SIZE 1024
 
-#define LEADING_FROM_LINE_REGEXP "^From [^ ]*  \\(Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\|Sun\\) \\(Jan\\|Feb\\|Mar\\|Apr\\|May\\|Jun\\|Jul\\|Aug\\|Sep\\|Oct\\|Nov\\|Dec\\) [ 123][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9] [0-9][0-9][0-9][0-9]\n$"
+#define LEADING_FROM_LINE_REGEXP_STRING "^From [^ ]*  \\(Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\|Sun\\) \\(Jan\\|Feb\\|Mar\\|Apr\\|May\\|Jun\\|Jul\\|Aug\\|Sep\\|Oct\\|Nov\\|Dec\\) [ 123][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9] [0-9][0-9][0-9][0-9]\n$"
 
 regex_t leading_from_line_regexp;
 
@@ -165,6 +165,23 @@ char *parse_token(char *token_buffer, size_t token_buffer_size,
   return string;
 }
 
+char *default_value(char *current_value,
+                    const char *env_variable,
+                    const char *hard_default_value) {
+  if(current_value) {
+    return current_value;
+  } else {
+    char *env_value = getenv(env_variable);
+    if(env_value) {
+      return strdup(env_value);
+    } else if(hard_default_value) {
+      return strdup(hard_default_value);
+    } else {
+      return 0;
+    }
+  }
+}
+
 /********************************************************************/
 
 /* malloc with error checking.  */
@@ -894,7 +911,7 @@ int main(int argc, char **argv) {
   int nb_search_conditions;
   struct search_condition search_conditions[MAX_NB_SEARCH_CONDITIONS];
 
-  if(regcomp(&leading_from_line_regexp, LEADING_FROM_LINE_REGEXP, 0)) {
+  if(regcomp(&leading_from_line_regexp, LEADING_FROM_LINE_REGEXP_STRING, 0)) {
     fprintf(stderr,
             "mymail: Cannot compile leading \"from\" line regexp. That is strange.\n");
     exit(EXIT_FAILURE);
@@ -1002,49 +1019,25 @@ int main(int argc, char **argv) {
   /* Set all the values that may defined in the arguments, through
      environment variables, or hard-coded */
 
-  if(!db_filename) {
-    char *default_db_filename = getenv("MYMAIL_DB_FILE");
-
-    if(!default_db_filename) {
-      default_db_filename = "mymail.db";
-    }
-
-    db_filename = strdup(default_db_filename);
-  }
-
-  if(!db_filename_regexp_string) {
-    char *default_db_filename_regexp_string = getenv("MYMAIL_DB_PATTERN");
+  db_filename = default_value(db_filename,
+                              "MYMAIL_DB_FILE",
+                              "mymail.db");
 
-    if(!default_db_filename_regexp_string) {
-      default_db_filename_regexp_string = "^mymail.db$";
-    }
-
-    db_filename_regexp_string = strdup(default_db_filename_regexp_string);
-  }
-
-  if(!db_root_path) {
-    char *default_db_root_path = getenv("MYMAIL_DB_ROOT");
-
-    if(default_db_root_path) {
-      db_root_path = strdup(default_db_root_path);
-    }
-  }
-
-  if(!db_filename_list) {
-    char *default_db_filename_list = getenv("MYMAIL_DB_LIST");
+  db_filename_regexp_string = default_value(db_filename_regexp_string,
+                                            "MYMAIL_DB_FILE",
+                                            "^mymail.db$");
 
-    if(default_db_filename_list) {
-      db_filename_list = strdup(default_db_filename_list);
-    }
-  }
+  db_root_path = default_value(db_root_path,
+                               "MYMAIL_DB_ROOT",
+                               0);
 
-  if(!mbox_filename_regexp_string) {
-    char *default_mbox_filename_regexp_string = getenv("MYMAIL_MBOX_PATTERN");
+  db_filename_list = default_value(db_filename_list,
+                                   "MYMAIL_DB_LIST",
+                                   0);
 
-    if(default_mbox_filename_regexp_string) {
-      mbox_filename_regexp_string = strdup(default_mbox_filename_regexp_string);
-    }
-  }
+  mbox_filename_regexp_string = default_value(mbox_filename_regexp_string,
+                                              "MYMAIL_MBOX_PATTERN",
+                                              "mbox$");
 
   /* Start the processing */