Cosmetics.
[selector.git] / selector.c
index 13f73b1..c8ccb5c 100644 (file)
  *
  */
 
-/* To use it as a super-history-search for bash:
-   selector -q -b -i -d -v -w -l ${HISTSIZE} <(history)
+/*
+
+  To use it as a super-history-search for bash:
+  selector -q -b -i -d -v -w -l ${HISTSIZE} <(history)
+
 */
 
 #define _GNU_SOURCE
@@ -123,12 +126,12 @@ void error_feedback() {
   }
 }
 
-/*********************************************************************/
-/* A quick and dirty hash table
+/*********************************************************************
+ A quick and dirty hash table
 
  The table itself stores indexes of the strings taken in a char
-   **table. When a string is added, if it was already in the table,
  the new index replaces the previous one. */
+ The table itself stores indexes of the strings taken in a char
+ **table. When a string is added, if it was already in the table, the
+ new index replaces the previous one. */
 
 int *new_hash_table(int hash_table_size) {
   int *result, k;
@@ -143,15 +146,16 @@ int *new_hash_table(int hash_table_size) {
    string was not already in the table, returns -1. Otherwise, returns
    the previous index it had. */
 
-int test_and_add(char *new_string, int new_index,
-                 char **strings,
-                 int *hash_table, int hash_table_size) {
+int add_and_get_previous_index(const char *new_string, int new_index,
+                               char **strings,
+                               int *hash_table, int hash_table_size) {
 
   unsigned int code = 0;
   int k;
 
   /* This is my recipe. I checked, it seems to work (as long as
-     hash_table_size is not a multiple of 387433 that should be okay) */
+     hash_table_size is not a multiple of 387433 that should be
+     okay) */
 
   for(k = 0; new_string[k]; k++) {
     code = code * 387433 + (unsigned int) (new_string[k]);
@@ -181,9 +185,9 @@ int test_and_add(char *new_string, int new_index,
   return -1;
 }
 
-/*********************************************************************/
-/* A matcher matches either with a collection of substrings, or with a
  regexp */
+/*********************************************************************
+ A matcher matches either with a collection of substrings, or with a
+ regexp */
 
 typedef struct {
   regex_t preg;
@@ -256,8 +260,8 @@ void initialize_matcher(int use_regexp, int case_sensitive,
   }
 }
 
-/*********************************************************************/
-/* Buffer edition */
+/*********************************************************************
+ Buffer edition */
 
 void delete_char(char *buffer, int *position) {
   if(buffer[*position]) {
@@ -363,14 +367,14 @@ void update_screen(int *current_focus_line, int *displayed_focus_line,
   int console_width = getmaxx(stdscr);
   int console_height = getmaxy(stdscr);
 
-  /* First, we find a visible line. */
-
   int nb_printed_lines = 0;
 
   use_default_colors();
 
   addstr("\n");
 
+  /* First, we find a visible line. */
+
   if(matcher.regexp_error) {
     attron(attr_error);
     addnstr("Regexp syntax error", console_width);
@@ -568,6 +572,49 @@ void update_screen(int *current_focus_line, int *displayed_focus_line,
 
 /*********************************************************************/
 
+void store_line(const char *t,
+                int nb_lines_max, int *nb_lines, char **lines,
+                int hash_table_size, int *hash_table) {
+
+  /* Remove the zsh history prefix */
+
+  if(zsh_history && *t == ':') {
+    while(*t && *t != ';') t++;
+    if(*t == ';') t++;
+  }
+
+  /* Remove the bash history prefix */
+
+  if(bash_history) {
+    while(*t == ' ') t++;
+    while(*t >= '0' && *t <= '9') t++;
+    while(*t == ' ') t++;
+  }
+
+  /* Check for duplicates with the hash table and insert the line in
+     the list if necessary */
+
+  int dup;
+
+  if(hash_table) {
+    dup = add_and_get_previous_index(t, *nb_lines, lines, hash_table, hash_table_size);
+  } else {
+    dup = -1;
+  }
+
+  if(dup < 0) {
+    lines[*nb_lines] = (char *) malloc((strlen(t) + 1) * sizeof(char));
+    strcpy(lines[*nb_lines], t);
+  } else {
+    /* The string was already in there, so we do not allocate a new
+       string but use the pointer to the first occurence of it */
+    lines[*nb_lines] = lines[dup];
+    lines[dup] = 0;
+  }
+
+  (*nb_lines)++;
+}
+
 void read_file(const char *input_filename,
                int nb_lines_max, int *nb_lines, char **lines,
                int hash_table_size, int *hash_table) {
@@ -600,7 +647,7 @@ void read_file(const char *input_filename,
 
     if(eol == buffer_size) {
       raw_line[buffer_size - 1] = '\0';
-      fprintf(stderr, "Line too long:\n");
+      fprintf(stderr, "Line too long (max is %d characters):\n", buffer_size);
       fprintf(stderr, raw_line);
       fprintf(stderr, "\n");
       exit(1);
@@ -608,48 +655,14 @@ void read_file(const char *input_filename,
 
     raw_line[eol] = '\0';
 
-    char *t = raw_line + start;
-
-    /* Remove the zsh history prefix */
-
-    if(zsh_history && *t == ':') {
-      while(*t && *t != ';') t++;
-      if(*t == ';') t++;
-    }
-
-    /* Remove the bash history prefix */
-
-    if(bash_history) {
-      while(*t == ' ') t++;
-      while(*t >= '0' && *t <= '9') t++;
-      while(*t == ' ') t++;
-    }
-
-    /* Check for duplicates with the hash table and insert the line in
-       the list if necessary */
-
-    int dup;
-
-    if(hash_table) {
-      dup = test_and_add(t, *nb_lines, lines, hash_table, hash_table_size);
-    } else {
-      dup = -1;
-    }
-
-    if(dup < 0) {
-      lines[*nb_lines] = (char *) malloc((strlen(t) + 1) * sizeof(char));
-      strcpy(lines[*nb_lines], t);
-    } else {
-      /* The string was already in there, so we do not allocate a new
-         string but use the pointer to the first occurence of it */
-      lines[*nb_lines] = lines[dup];
-      lines[dup] = 0;
-    }
-
-    (*nb_lines)++;
+    store_line(raw_line + start,
+               nb_lines_max, nb_lines, lines,
+               hash_table_size, hash_table);
 
     start = eol + 1;
   }
+
+  fclose(file);
 }
 
 /*********************************************************************/
@@ -1049,7 +1062,7 @@ int main(int argc, char **argv) {
   echo();
   endwin();
 
-/* Here we come back to standard display */
+  /* Here we come back to standard display */
 
   if((key == KEY_ENTER || key == '\n')) {