Cleaned up the line display. Removed the clrtoeol, which seems to be useless (?)
[selector.git] / selector.c
index e3c0b33..ca9d392 100644 (file)
@@ -3,7 +3,7 @@
  *  selector is a simple command line utility for selection of strings
  *  with a dynamic pattern-matching.
  *
- *  Copyright (c) 2009, 2010 Francois Fleuret
+ *  Copyright (c) 2009, 2010, 2011 Francois Fleuret
  *  Written by Francois Fleuret <francois@fleuret.org>
  *
  *  This file is part of selector.
@@ -25,7 +25,7 @@
 /*
 
   To use it as a super-history-search for bash:
-  selector -q -b -i -d -v -w -l ${HISTSIZE} <(history)
+  selector --bash <(history)
 
 */
 
@@ -43,8 +43,9 @@
 #include <regex.h>
 #include <locale.h>
 #include <getopt.h>
+#include <limits.h>
 
-#define VERSION "1.1"
+#define VERSION "1.1.3"
 
 #define BUFFER_SIZE 4096
 
@@ -64,6 +65,7 @@ int use_regexp = 0;
 int case_sensitive = 0;
 char *title = 0;
 int error_flash = 0;
+int exclamation_negates = 0;
 
 int attr_modeline, attr_focus_line, attr_error;
 
@@ -104,35 +106,52 @@ void inject_into_tty_buffer(char *string, int add_control_qs) {
 
 /*********************************************************************/
 
-void check_opt(int argc, char **argv, int n_opt, int n, const char *help) {
-  if(n_opt + n >= argc) {
-    fprintf(stderr, "Selector: Missing argument for %s, expecting %s.\n",
-            argv[n_opt], help);
-    exit(EXIT_FAILURE);
-  }
-}
-
-int string_to_positive_integer(char *string) {
-  int error = 0;
-  int result = 0;
+void str_to_positive_integers(char *string, int *values, int nb) {
+  int current_value, gotone;
   char *s;
+  int n;
 
-  if(*string) {
-    for(s = string; *s && *s != ','; s++) {
-      if(*s >= '0' && *s <= '9') {
-        result = result * 10 + (int) (*s - '0');
-      } else error = 1;
+  n = 0;
+  current_value = 0;
+  gotone = 0;
+  s = string;
+
+  while(1) {
+    if(*s >= '0' && *s <= '9') {
+      current_value = current_value * 10 + (int) (*s - '0');
+      gotone = 1;
+    } else if(*s == ',' || *s == '\0') {
+      if(gotone) {
+        if(n < nb) {
+          values[n++] = current_value;
+          if(*s == '\0') {
+            if(n == nb) {
+              return;
+            } else {
+              fprintf(stderr,
+                      "selector: Missing value in `%s'.\n", string);
+              exit(EXIT_FAILURE);
+            }
+          }
+          current_value = 0;
+          gotone = 0;
+        } else {
+          fprintf(stderr,
+                  "selector: Too many values in `%s'.\n", string);
+          exit(EXIT_FAILURE);
+        }
+      } else {
+        fprintf(stderr,
+                "selector: Empty value in `%s'.\n", string);
+        exit(EXIT_FAILURE);
+      }
+    } else {
+      fprintf(stderr,
+              "selector: Syntax error in `%s'.\n", string);
+      exit(EXIT_FAILURE);
     }
-  } else error = 1;
-
-  if(error) {
-    fprintf(stderr,
-            "Selector: Value `%s' is not a positive integer.\n",
-            string);
-    exit(EXIT_FAILURE);
+    s++;
   }
-
-  return result;
 }
 
 void error_feedback() {
@@ -143,36 +162,52 @@ void error_feedback() {
   }
 }
 
-void print_help(FILE *out) {
+void usage(FILE *out) {
 
   fprintf(out, "Selector version %s (%s)\n", VERSION, UNAME);
   fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
   fprintf(out, "\n");
   fprintf(out, "Usage: selector [options] [<filename1> [<filename2> ...]]\n");
   fprintf(out, "\n");
-  fprintf(out, " -h      show this help\n");
-  fprintf(out, " -v      inject the selected line in the tty\n");
-  fprintf(out, " -w      quote control characters with ^Qs when using -v\n");
-  fprintf(out, " -d      remove duplicated lines\n");
-  fprintf(out, " -b      remove the bash history line prefix\n");
-  fprintf(out, " -z      remove the zsh history line prefix\n");
-  fprintf(out, " -i      invert the order of lines\n");
-  fprintf(out, " -e      start in regexp mode\n");
-  fprintf(out, " -a      start in case sensitive mode\n");
-  fprintf(out, " -m      monochrome mode\n");
-  fprintf(out, " -q      make a flash instead of a beep on an edition error\n");
+  fprintf(out, " -h, --help\n");
+  fprintf(out, "         show this help\n");
+  fprintf(out, " -v, --inject-in-tty\n");
+  fprintf(out, "         inject the selected line in the tty\n");
+  fprintf(out, " -w, --add-control-qs\n");
+  fprintf(out, "         quote control characters with ^Qs when using -v\n");
+  fprintf(out, " -d, --remove-duplicates\n");
+  fprintf(out, "         remove duplicated lines\n");
+  fprintf(out, " -b, --remove-bash-prefix\n");
+  fprintf(out, "         remove the bash history line prefix\n");
+  fprintf(out, " -z, --remove-zsh-prefix\n");
+  fprintf(out, "         remove the zsh history line prefix\n");
+  fprintf(out, " -i, --revert-order\n");
+  fprintf(out, "         invert the order of lines\n");
+  fprintf(out, " -e, --regexp\n");
+  fprintf(out, "         start in regexp mode\n");
+  fprintf(out, " -a, --case-sensitive\n");
+  fprintf(out, "         start in case sensitive mode\n");
+  fprintf(out, " -n, --exclamation-negates\n");
+  fprintf(out, "         exclamation points in substring requires the string to be absent\n");
+  fprintf(out, " -m, --monochrome\n");
+  fprintf(out, "         monochrome mode\n");
+  fprintf(out, " -q, --no-beep\n");
+  fprintf(out, "         make a flash instead of a beep on an edition error\n");
+  fprintf(out, " --bash\n");
+  fprintf(out, "         setting for bash history search, same as -b -i -d -v -w -l ${HISTSIZE}\n");
   fprintf(out, " --      all following arguments are filenames\n");
-  fprintf(out, " -t <title>\n");
+  fprintf(out, " -t <title>, --title <title>\n");
   fprintf(out, "         add a title in the modeline\n");
-  fprintf(out, " -c <fg modeline> <bg modeline> <fg highlight> <bg highlight>\n");
-  fprintf(out, "         set the display colors\n");
-  fprintf(out, " -o <output filename>\n");
+  fprintf(out, " -c <colors>, --colors <colors>\n");
+  fprintf(out, "         set the display colors with an argument of the form\n");
+  fprintf(out, "         <fg_modeline>,<bg_modeline>,<fg_highlight>,<bg_highlight>\n");
+  fprintf(out, " -o <output filename>, --output-file <output filename>\n");
   fprintf(out, "         set a file to write the selected line to\n");
-  fprintf(out, " -s <pattern separator>\n");
+  fprintf(out, " -s <pattern separator>, --pattern-separator <pattern separator>\n");
   fprintf(out, "         set the symbol to separate substrings in the pattern\n");
-  fprintf(out, " -x <label separator>\n");
+  fprintf(out, " -x <label separator>, --label-separator <label separator>\n");
   fprintf(out, "         set the symbol to terminate the label\n");
-  fprintf(out, " -l <max number of lines>\n");
+  fprintf(out, " -l <max number of lines>, --number-of-lines <max number of lines>\n");
   fprintf(out, "         set the maximum number of lines to take into account\n");
   fprintf(out, "\n");
 }
@@ -181,6 +216,8 @@ void print_help(FILE *out) {
 
 /* A quick and dirty hash table */
 
+#define MAGIC_HASH_MULTIPLIER 387433
+
 /* 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.  */
@@ -219,18 +256,19 @@ int add_and_get_previous_index(struct hash_table_t *hash_table,
                                const char *new_string, int new_index,
                                char **strings) {
 
-  unsigned int code = 0;
+  unsigned int code = 0, start;
   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 MAGIC_HASH_MULTIPLIER that
+     should be okay) */
 
   for(k = 0; new_string[k]; k++) {
-    code = code * 387433 + (unsigned int) (new_string[k]);
+    code = code * MAGIC_HASH_MULTIPLIER + (unsigned int) (new_string[k]);
   }
 
   code = code % hash_table->size;
+  start = code;
 
   while(hash_table->entries[code] >= 0) {
     /* There is a string with that code */
@@ -245,6 +283,12 @@ int add_and_get_previous_index(struct hash_table_t *hash_table,
     /* This collision was not the same string, let's move to the next
        in the table */
     code = (code + 1) % hash_table->size;
+    /* We came back to our original code, which means that the table
+       is full */
+    if(code == start) {
+      printf("Full hash table (that should not happen)\n");
+      exit(EXIT_FAILURE);
+   }
   }
 
   /* This string was not already in there, store the index in the
@@ -266,16 +310,36 @@ typedef struct {
   char *splitted_patterns, **patterns;
 } matcher_t;
 
-int match(char *string, matcher_t *matcher) {
+int match(matcher_t *matcher, char *string) {
   int n;
   if(matcher->nb_patterns >= 0) {
     if(matcher->case_sensitive) {
-      for(n = 0; n < matcher->nb_patterns; n++) {
-        if(strstr(string, matcher->patterns[n]) == 0) return 0;
+      if(exclamation_negates) {
+        for(n = 0; n < matcher->nb_patterns; n++) {
+          if(matcher->patterns[n][0] == '!') {
+            if(strstr(string, matcher->patterns[n] + 1) != 0) return 0;
+          } else {
+            if(strstr(string, matcher->patterns[n]) == 0) return 0;
+          }
+        }
+      } else {
+        for(n = 0; n < matcher->nb_patterns; n++) {
+          if(strstr(string, matcher->patterns[n]) == 0) return 0;
+        }
       }
     } else {
-      for(n = 0; n < matcher->nb_patterns; n++) {
-        if(strcasestr(string, matcher->patterns[n]) == 0) return 0;
+      if(exclamation_negates) {
+        for(n = 0; n < matcher->nb_patterns; n++) {
+          if(matcher->patterns[n][0] == '!') {
+            if(strcasestr(string, matcher->patterns[n] + 1) != 0) return 0;
+          } else {
+            if(strcasestr(string, matcher->patterns[n]) == 0) return 0;
+          }
+        }
+      } else {
+        for(n = 0; n < matcher->nb_patterns; n++) {
+          if(strcasestr(string, matcher->patterns[n]) == 0) return 0;
+        }
       }
     }
     return 1;
@@ -293,8 +357,9 @@ void free_matcher(matcher_t *matcher) {
   }
 }
 
-void initialize_matcher(int use_regexp, int case_sensitive,
-                        matcher_t *matcher, const char *pattern) {
+void initialize_matcher(matcher_t *matcher,
+                        int use_regexp, int case_sensitive,
+                        const char *pattern) {
   const char *s;
   char *t, *last_pattern_start;
   int n;
@@ -395,17 +460,16 @@ void kill_after_cursor(char *buffer, int *position) {
 
 /*********************************************************************/
 
-int previous_visible(int current_line, int nb_lines, char **lines,
-                     matcher_t *matcher) {
+int previous_visible(int current_line, char **lines, matcher_t *matcher) {
   int line = current_line - 1;
-  while(line >= 0 && !match(lines[line], matcher)) line--;
+  while(line >= 0 && !match(matcher, lines[line])) line--;
   return line;
 }
 
 int next_visible(int current_line, int nb_lines, char **lines,
                  matcher_t *matcher) {
   int line = current_line + 1;
-  while(line < nb_lines && !match(lines[line], matcher)) line++;
+  while(line < nb_lines && !match(matcher, lines[line])) line++;
 
   if(line < nb_lines)
     return line;
@@ -440,7 +504,7 @@ void update_screen(int *current_focus_line, int *displayed_focus_line,
   int nb_printed_lines = 0;
   int cursor_x;
 
-  initialize_matcher(use_regexp, case_sensitive, &matcher, pattern);
+  initialize_matcher(&matcher, use_regexp, case_sensitive, pattern);
 
   console_width = getmaxx(stdscr);
   console_height = getmaxy(stdscr);
@@ -463,14 +527,13 @@ void update_screen(int *current_focus_line, int *displayed_focus_line,
 
   else if(nb_lines > 0) {
     int new_focus_line;
-    if(match(lines[*current_focus_line], &matcher)) {
+    if(match(&matcher, lines[*current_focus_line])) {
       new_focus_line = *current_focus_line;
     } else {
       new_focus_line = next_visible(*current_focus_line, nb_lines, lines,
                                     &matcher);
       if(new_focus_line < 0) {
-        new_focus_line = previous_visible(*current_focus_line, nb_lines, lines,
-                                          &matcher);
+        new_focus_line = previous_visible(*current_focus_line, lines, &matcher);
       }
     }
 
@@ -489,7 +552,7 @@ void update_screen(int *current_focus_line, int *displayed_focus_line,
       } else {
         /* We want to go up, let's find the first visible line above */
         for(m = 0; l >= 0 && m < -motion; m++) {
-          l = previous_visible(l, nb_lines, lines, &matcher);
+          l = previous_visible(l, lines, &matcher);
           if(l >= 0) {
             new_focus_line = l;
           }
@@ -505,8 +568,8 @@ void update_screen(int *current_focus_line, int *displayed_focus_line,
       int first_line = new_focus_line, last_line = new_focus_line;
       int nb_match = 1;
 
-      /* We find the first and last line to show, so that the total of
-         visible lines between them (them included) is
+      /* We find the first and last lines to show, so that the total
+         of visible lines between them (them included) is
          console_height-1 */
 
       while(nb_match < console_height-1 &&
@@ -514,21 +577,21 @@ void update_screen(int *current_focus_line, int *displayed_focus_line,
 
         if(first_line > 0) {
           first_line--;
-          while(first_line > 0 && !match(lines[first_line], &matcher)) {
+          while(first_line > 0 && !match(&matcher, lines[first_line])) {
             first_line--;
           }
-          if(match(lines[first_line], &matcher)) {
+          if(match(&matcher, lines[first_line])) {
             nb_match++;
           }
         }
 
         if(nb_match < console_height - 1 && last_line < nb_lines - 1) {
           last_line++;
-          while(last_line < nb_lines - 1 && !match(lines[last_line], &matcher)) {
+          while(last_line < nb_lines - 1 && !match(&matcher, lines[last_line])) {
             last_line++;
           }
 
-          if(match(lines[last_line], &matcher)) {
+          if(match(&matcher, lines[last_line])) {
             nb_match++;
           }
         }
@@ -537,7 +600,7 @@ void update_screen(int *current_focus_line, int *displayed_focus_line,
       /* Now we display them */
 
       for(l = first_line; l <= last_line; l++) {
-        if(match(lines[l], &matcher)) {
+        if(match(&matcher, lines[l])) {
           int k = 0;
 
           while(lines[l][k] && k < BUFFER_SIZE - 2 && k < console_width - 2) {
@@ -545,27 +608,19 @@ void update_screen(int *current_focus_line, int *displayed_focus_line,
             k++;
           }
 
-          /* We fill the rest of the line with blanks if this is the
-             highlighted line */
+          /* Highlight the highlighted line ... */
 
           if(l == new_focus_line) {
             while(k < console_width) {
               buffer[k++] = ' ';
             }
-          }
-
-          buffer[k++] = '\n';
-          buffer[k++] = '\0';
-
-          clrtoeol();
-
-          /* Highlight the highlighted line ... */
-
-          if(l == new_focus_line) {
             attron(attr_focus_line);
             addnstr(buffer, console_width);
             attroff(attr_focus_line);
           } else {
+            buffer[k++] = '\n';
+            buffer[k++] = '\0';
+            /* clrtoeol(); */
             addnstr(buffer, console_width);
           }
 
@@ -720,7 +775,7 @@ void read_file(struct hash_table_t *hash_table,
   file = fopen(input_filename, "r");
 
   if(!file) {
-    fprintf(stderr, "Selector: Can not open `%s'.\n", input_filename);
+    fprintf(stderr, "selector: Can not open `%s'.\n", input_filename);
     exit(EXIT_FAILURE);
   }
 
@@ -753,9 +808,9 @@ void read_file(struct hash_table_t *hash_table,
 
     if(eol == BUFFER_SIZE) {
       raw_line[BUFFER_SIZE - 1] = '\0';
-      fprintf(stderr, "Selector: Line too long (max is %d characters):\n",
+      fprintf(stderr, "selector: Line too long (max is %d characters):\n",
               BUFFER_SIZE);
-      fprintf(stderr, raw_line);
+      fprintf(stderr, "%s", raw_line);
       fprintf(stderr, "\n");
       exit(EXIT_FAILURE);
     }
@@ -776,6 +831,13 @@ void read_file(struct hash_table_t *hash_table,
 
 /*********************************************************************/
 
+/* For long options that have no equivalent short option, use a
+   non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
+enum
+{
+  OPT_BASH_MODE = CHAR_MAX + 1
+};
+
 static struct option long_options[] = {
   { "output-file", 1, 0, 'o' },
   { "pattern-separator", 1, 0, 's' },
@@ -793,7 +855,7 @@ static struct option long_options[] = {
   { "title", 1, 0, 't' },
   { "number-of-lines", 1, 0, 'l' },
   { "colors", 1, 0, 'c' },
-  { "rest-are-files", no_argument, 0, '-' },
+  { "bash", no_argument, 0, OPT_BASH_MODE },
   { "help", no_argument, 0, 'h' },
   { 0, 0, 0, 0 }
 };
@@ -801,24 +863,24 @@ static struct option long_options[] = {
 int main(int argc, char **argv) {
 
   char output_filename[BUFFER_SIZE];
-  char *s;
   char pattern[BUFFER_SIZE];
   int c, k, l, n;
   int cursor_position;
-  int error = 0, show_help = 0;
-  int rest_are_files = 0;
+  int error = 0, show_help = 0, done = 0;
   int key;
   int current_focus_line, displayed_focus_line;
 
+  int colors[4];
   int color_fg_modeline, color_bg_modeline;
   int color_fg_highlight, color_bg_highlight;
 
   char **lines, **labels;
   int nb_lines;
   struct hash_table_t *hash_table;
+  char *bash_histsize;
 
   if(!isatty(STDIN_FILENO)) {
-    fprintf(stderr, "Selector: The standard input is not a tty.\n");
+    fprintf(stderr, "selector: The standard input is not a tty.\n");
     exit(EXIT_FAILURE);
   }
 
@@ -831,8 +893,7 @@ int main(int argc, char **argv) {
 
   strcpy(output_filename, "");
 
-  while (!rest_are_files &&
-         (c = getopt_long(argc, argv, "o:s:x:vwmqf:ibzdeat:l:c:-h",
+  while ((c = getopt_long(argc, argv, "o:s:x:vwmqf:ibzdeant:l:c:-h",
                           long_options, NULL)) != -1) {
 
     switch(c) {
@@ -889,6 +950,10 @@ int main(int argc, char **argv) {
       case_sensitive = 1;
       break;
 
+    case 'n':
+      exclamation_negates = 1;
+      break;
+
     case 't':
       free(title);
       title = safe_malloc((strlen(optarg) + 1) * sizeof(char));
@@ -896,42 +961,54 @@ int main(int argc, char **argv) {
       break;
 
     case 'l':
-      nb_lines_max = string_to_positive_integer(optarg);
+      str_to_positive_integers(optarg, &nb_lines_max, 1);
       break;
 
     case 'c':
-      s = optarg;
-      color_fg_modeline = string_to_positive_integer(s);
-      while(*s && *s != ',') s++; if(*s == ',') { s++; }
-      color_bg_modeline = string_to_positive_integer(s);
-      while(*s && *s != ',') s++; if(*s == ',') { s++; }
-      color_fg_highlight = string_to_positive_integer(s);
-      while(*s && *s != ',') s++; if(*s == ',') { s++; }
-      color_bg_highlight = string_to_positive_integer(s);
-      break;
-
-    case '-':
-      rest_are_files = 1;
+      str_to_positive_integers(optarg, colors, 4);
+      color_fg_modeline = colors[0];
+      color_bg_modeline = colors[1];
+      color_fg_highlight = colors[2];
+      color_bg_highlight = colors[3];
       break;
 
     case 'h':
       show_help = 1;
       break;
 
+    case OPT_BASH_MODE:
+      /* Same as -c 7,4,0,3 -q */
+      /* color_fg_modeline = 7; */
+      /* color_bg_modeline = 4; */
+      /* color_fg_highlight = 0; */
+      /* color_bg_highlight = 3; */
+      /* error_flash = 1; */
+      /* Same as -b -i -d -v -w */
+      bash_history = 1;
+      inverse_order = 1;
+      remove_duplicates = 1;
+      output_to_vt_buffer = 1;
+      add_control_qs = 1;
+      bash_histsize = getenv("HISTSIZE");
+      if(bash_histsize) {
+        str_to_positive_integers(bash_histsize, &nb_lines_max, 1);
+      }
+      break;
+
     default:
       error = 1;
       break;
     }
   }
 
-  if(show_help || error) {
-    if(error) {
-      print_help(stderr);
-      exit(EXIT_FAILURE);
-    } else {
-      print_help(stdout);
-      exit(EXIT_SUCCESS);
-    }
+  if(error) {
+    usage(stderr);
+    exit(EXIT_FAILURE);
+  }
+
+  if(show_help) {
+    usage(stdout);
+    exit(EXIT_SUCCESS);
   }
 
   lines = safe_malloc(nb_lines_max * sizeof(char *));
@@ -1029,7 +1106,7 @@ int main(int argc, char **argv) {
        color_bg_highlight < 0 || color_bg_highlight >= COLORS) {
       echo();
       endwin();
-      fprintf(stderr, "Selector: Color numbers have to be between 0 and %d.\n",
+      fprintf(stderr, "selector: Color numbers have to be between 0 and %d.\n",
               COLORS - 1);
       exit(EXIT_FAILURE);
     }
@@ -1140,14 +1217,27 @@ int main(int argc, char **argv) {
       clear();
     }
 
+    else if(key == '\007' || /* ^G */
+            key == '\033' || /* ^[ (escape) */
+            key == '\n' ||
+            key == KEY_ENTER) {
+      done = 1;
+    }
+
+    else if(key == KEY_RESIZE || key == -1) {
+      /* Do nothing when the tty is resized */
+    }
+
+    else {
+      /* Unknown key */
+      error_feedback();
+    }
+
     update_screen(&current_focus_line, &displayed_focus_line,
                   motion,
                   nb_lines, labels, cursor_position, pattern);
 
-  } while(key != '\007' && /* ^G */
-          key != '\033' && /* ^[ (escape) */
-          key != '\n' &&
-          key != KEY_ENTER);
+  } while(!done);
 
   echo();
   endwin();
@@ -1176,12 +1266,12 @@ int main(int argc, char **argv) {
       FILE *out = fopen(output_filename, "w");
       if(out) {
         if(t) {
-          fprintf(out, t);
+          fprintf(out, "%s", t);
         }
         fprintf(out, "\n");
       } else {
         fprintf(stderr,
-                "Selector: Can not open %s for writing.\n",
+                "selector: Can not open %s for writing.\n",
                 output_filename);
         exit(EXIT_FAILURE);
       }