3 * selector is a simple command line utility for selection of strings
4 * with a dynamic pattern-matching.
6 * Copyright (c) 2009, 2010, 2011, 2012 Francois Fleuret
7 * Written by Francois Fleuret <francois@fleuret.org>
9 * This file is part of selector.
11 * selector is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 3 as
13 * published by the Free Software Foundation.
15 * selector is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with selector. If not, see <http://www.gnu.org/licenses/>.
27 To use it as a super-history-search for bash:
28 selector --bash <(history)
41 #include <sys/ioctl.h>
48 #define VERSION "1.1.6"
50 #define BUFFER_SIZE 4096
52 /* Yeah, global variables! */
54 int nb_lines_max = 1000;
55 char pattern_separator = ';';
56 char label_separator = '\0';
57 int output_to_vt_buffer = 0;
58 int add_control_qs = 0;
62 int inverse_order = 0;
63 int remove_duplicates = 0;
65 int case_sensitive = 0;
68 int upper_caps_makes_case_sensitive = 0;
69 int show_long_lines = 0;
72 int attr_modeline, attr_focus_line, attr_error, attr_hits;
74 /********************************************************************/
76 /* malloc with error checking. */
78 void *safe_malloc(size_t n) {
82 "selector: can not allocate memory: %s\n", strerror(errno));
88 /*********************************************************************/
90 void inject_into_tty_buffer(char *string, int add_control_qs) {
91 struct termios oldtio, newtio;
93 const char control_q = '\021';
94 tcgetattr(STDIN_FILENO, &oldtio);
95 memset(&newtio, 0, sizeof(newtio));
96 /* Set input mode (non-canonical, *no echo*,...) */
97 tcsetattr(STDIN_FILENO, TCSANOW, &newtio);
98 /* Put the selected string in the tty input buffer */
99 for(k = string; *k; k++) {
100 if(add_control_qs && !(*k >= ' ' && *k <= '~')) {
101 /* Add ^Q to quote control characters */
102 ioctl(STDIN_FILENO, TIOCSTI, &control_q);
104 ioctl(STDIN_FILENO, TIOCSTI, k);
106 /* Restore the old settings */
107 tcsetattr(STDIN_FILENO, TCSANOW, &oldtio);
110 /*********************************************************************/
112 void str_to_positive_integers(char *string, int *values, int nb) {
113 int current_value, gotone;
123 if(*s >= '0' && *s <= '9') {
124 current_value = current_value * 10 + (int) (*s - '0');
126 } else if(*s == ',' || *s == '\0') {
129 values[n++] = current_value;
135 "selector: Missing value in `%s'.\n", string);
143 "selector: Too many values in `%s'.\n", string);
148 "selector: Empty value in `%s'.\n", string);
153 "selector: Syntax error in `%s'.\n", string);
160 void error_feedback() {
168 void usage(FILE *out) {
170 fprintf(out, "Selector version %s (%s)\n", VERSION, UNAME);
171 fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
173 fprintf(out, "Usage: selector [options] [<filename1> [<filename2> ...]]\n");
175 fprintf(out, " -h, --help\n");
176 fprintf(out, " show this help\n");
177 fprintf(out, " -v, --inject-in-tty\n");
178 fprintf(out, " inject the selected line in the tty\n");
179 fprintf(out, " -w, --add-control-qs\n");
180 fprintf(out, " quote control characters with ^Qs when using -v\n");
181 fprintf(out, " -d, --remove-duplicates\n");
182 fprintf(out, " remove duplicated lines\n");
183 fprintf(out, " -b, --remove-bash-prefix\n");
184 fprintf(out, " remove the bash history line prefix\n");
185 fprintf(out, " -z, --remove-zsh-prefix\n");
186 fprintf(out, " remove the zsh history line prefix\n");
187 fprintf(out, " -i, --revert-order\n");
188 fprintf(out, " invert the order of lines\n");
189 fprintf(out, " -e, --regexp\n");
190 fprintf(out, " start in regexp mode\n");
191 fprintf(out, " -a, --case-sensitive\n");
192 fprintf(out, " start in case sensitive mode\n");
193 fprintf(out, " -j, --show-long-lines\n");
194 fprintf(out, " print three dots at the end of truncated lines\n");
195 fprintf(out, " -y, --show-hits\n");
196 fprintf(out, " highlight the matching substrings\n");
197 fprintf(out, " -u, --upper-case-makes-case-sensitive\n");
198 fprintf(out, " using an upper case character in the matching string makes\n");
199 fprintf(out, " the matching case-sensitive\n");
200 fprintf(out, " -m, --monochrome\n");
201 fprintf(out, " monochrome mode\n");
202 fprintf(out, " -q, --no-beep\n");
203 fprintf(out, " make a flash instead of a beep on an edition error\n");
204 fprintf(out, " --bash\n");
205 fprintf(out, " setting for bash history search, same as -b -i -d -v -w -l ${HISTSIZE}\n");
206 fprintf(out, " -- all following arguments are filenames\n");
207 fprintf(out, " -t <title>, --title <title>\n");
208 fprintf(out, " add a title in the modeline\n");
209 fprintf(out, " -c <colors>, --colors <colors>\n");
210 fprintf(out, " set the display colors with an argument of the form\n");
211 fprintf(out, " <fg_modeline>,<bg_modeline>,<fg_highlight>,<bg_highlight>\n");
212 fprintf(out, " -o <output filename>, --output-file <output filename>\n");
213 fprintf(out, " set a file to write the selected line to\n");
214 fprintf(out, " -s <pattern separator>, --pattern-separator <pattern separator>\n");
215 fprintf(out, " set the symbol to separate substrings in the pattern\n");
216 fprintf(out, " -x <label separator>, --label-separator <label separator>\n");
217 fprintf(out, " set the symbol to terminate the label\n");
218 fprintf(out, " -l <max number of lines>, --number-of-lines <max number of lines>\n");
219 fprintf(out, " set the maximum number of lines to take into account\n");
223 /*********************************************************************/
225 /* A quick and dirty hash table */
227 #define MAGIC_HASH_MULTIPLIER 387433
229 /* The table itself stores indexes of the strings taken in a char**
230 table. When a string is added, if it was already in the table, the
231 new index replaces the previous one. */
233 struct hash_table_t {
238 struct hash_table_t *new_hash_table(int size) {
240 struct hash_table_t *hash_table;
242 hash_table = safe_malloc(sizeof(struct hash_table_t));
244 hash_table->size = size;
245 hash_table->entries = safe_malloc(hash_table->size * sizeof(int));
247 for(k = 0; k < hash_table->size; k++) {
248 hash_table->entries[k] = -1;
254 void free_hash_table(struct hash_table_t *hash_table) {
255 free(hash_table->entries);
259 /* Adds new_string in the table, associated to new_index. If this
260 string was not already in the table, returns -1. Otherwise, returns
261 the previous index it had. */
263 int add_and_get_previous_index(struct hash_table_t *hash_table,
264 const char *new_string, int new_index,
267 unsigned int code = 0, start;
270 /* This is my recipe. I checked, it seems to work (as long as
271 hash_table->size is not a multiple of MAGIC_HASH_MULTIPLIER that
274 for(k = 0; new_string[k]; k++) {
275 code = code * MAGIC_HASH_MULTIPLIER + (unsigned int) (new_string[k]);
278 code = code % hash_table->size;
281 while(hash_table->entries[code] >= 0) {
282 /* There is a string with that code */
283 if(strcmp(new_string, strings[hash_table->entries[code]]) == 0) {
284 /* It is the same string, we keep a copy of the stored index */
285 int result = hash_table->entries[code];
286 /* Put the new one */
287 hash_table->entries[code] = new_index;
288 /* And return the previous one */
291 /* This collision was not the same string, let's move to the next
293 code = (code + 1) % hash_table->size;
294 /* We came back to our original code, which means that the table
298 "Full hash table (that should not happen)\n");
303 /* This string was not already in there, store the index in the
304 table and return -1 */
306 hash_table->entries[code] = new_index;
310 /*********************************************************************
311 A matcher matches either with a collection of substrings, or with a
319 char *splitted_patterns, **patterns;
322 /* Routine to add an interval to a sorted list of intervals
323 extremities. Returns the resulting number of extremities.
325 This routine is an effing nightmare */
327 int add_interval(int n, int *switches, int start, int end) {
330 if(start == end) { return n; }
333 while(f < n && switches[f] <= start) { f++; }
335 while(g < n && switches[g] <= end) { g++; }
338 /* switches[n] start end */
348 /* switches[f-1] start switches[f] switches[g-1] end switches[g] */
349 /* |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX| ... |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX| */
350 for(k = f; k < n; k++) { switches[k] = switches[k + (g - f)]; }
353 /* switches[f-1] start switches[f] switches[g-1] end switches[g] */
354 /* |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX| ... XXXXXXXXXXXX| |XXXXXXXXXX */
355 switches[g - 1] = end;
356 for(k = f; k < n; k++) { switches[k] = switches[k + ((g - 1) - f)]; }
357 return n - ((g - 1) - f);
363 /* switches[f-1] start end switches[f] */
364 /* XXXXXXXXXXXX| |XXXXXXXXXX */
365 for(k = n - 1; k >= f; k--) {
366 switches[k + 2] = switches[k];
369 switches[f + 1] = end;
374 /* switches[f-1] start switches[f] switches[g-1] end switches[g] */
375 /* XXXXXXXXXXXX| |XXXXXXXXXX ... |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX| */
377 for(k = f + 1; k < n; k++) { switches[k] = switches[k + (g - (f + 1))]; }
378 return n - (g - (f + 1));
380 /* switches[f-1] start switches[f] switches[g-1] end switches[g] */
381 /* XXXXXXXXXXXX| |XXXXXXXXXX ... XXXXXXXXXXXX| |XXXXXXXXXX */
383 switches[g - 1] = end;
384 for(k = f + 1; k < n; k++) { switches[k] = switches[k + ((g - 1) - (f + 1))]; }
385 return n - ((g - 1) - (f + 1));
390 int match(struct matcher *matcher, char *string, int *nb_switches, int *switches) {
395 if(nb_switches) { *nb_switches = 0; }
397 if(matcher->nb_patterns >= 0) {
398 if(matcher->case_sensitive) {
399 for(n = 0; n < matcher->nb_patterns; n++) {
400 if((where = strstr(string, matcher->patterns[n])) == 0) return 0;
402 *nb_switches = add_interval(*nb_switches, switches,
403 (int) (where - string),
404 (int) (where - string) + strlen(matcher->patterns[n]));
408 for(n = 0; n < matcher->nb_patterns; n++) {
409 if((where = strcasestr(string, matcher->patterns[n])) == 0) return 0;
411 *nb_switches = add_interval(*nb_switches, switches,
412 (int) (where - string),
413 (int) (where - string) + strlen(matcher->patterns[n]));
420 if(regexec(&matcher->preg, string, 1, &matches, 0) == 0) {
422 switches[0] = matches.rm_so;
423 switches[1] = matches.rm_eo;
429 return regexec(&matcher->preg, string, 0, 0, 0) == 0;
434 void free_matcher(struct matcher *matcher) {
435 if(matcher->nb_patterns < 0) {
436 if(!matcher->regexp_error) regfree(&matcher->preg);
438 free(matcher->splitted_patterns);
439 free(matcher->patterns);
443 void initialize_matcher(struct matcher *matcher,
444 int use_regexp, int case_sensitive,
445 const char *pattern) {
447 char *t, *last_pattern_start;
451 matcher->case_sensitive = case_sensitive;
452 matcher->nb_patterns = -1;
453 matcher->regexp_error = regcomp(&matcher->preg, pattern,
454 case_sensitive ? 0 : REG_ICASE);
456 matcher->regexp_error = 0;
457 matcher->nb_patterns = 1;
459 if(upper_caps_makes_case_sensitive) {
460 for(s = pattern; *s && !case_sensitive; s++) {
461 case_sensitive = (*s >= 'A' && *s <= 'Z');
465 matcher->case_sensitive = case_sensitive;
467 for(s = pattern; *s; s++) {
468 if(*s == pattern_separator) {
469 matcher->nb_patterns++;
473 matcher->splitted_patterns =
474 safe_malloc((strlen(pattern) + 1) * sizeof(char));
477 safe_malloc(matcher->nb_patterns * sizeof(char *));
479 strcpy(matcher->splitted_patterns, pattern);
482 last_pattern_start = matcher->splitted_patterns;
483 for(t = matcher->splitted_patterns; n < matcher->nb_patterns; t++) {
484 if(*t == pattern_separator || *t == '\0') {
486 matcher->patterns[n++] = last_pattern_start;
487 last_pattern_start = t + 1;
493 /*********************************************************************
496 void delete_char(char *buffer, int *position) {
497 if(buffer[*position]) {
499 while(c < BUFFER_SIZE && buffer[c]) {
500 buffer[c] = buffer[c+1];
503 } else error_feedback();
506 void backspace_char(char *buffer, int *position) {
508 if(buffer[*position]) {
509 int c = *position - 1;
511 buffer[c] = buffer[c+1];
515 buffer[*position - 1] = '\0';
519 } else error_feedback();
522 void insert_char(char *buffer, int *position, char character) {
523 if(strlen(buffer) < BUFFER_SIZE - 1) {
525 char t = buffer[c], u;
534 buffer[(*position)++] = character;
535 } else error_feedback();
538 void kill_before_cursor(char *buffer, int *position) {
540 while(buffer[*position + s]) {
541 buffer[s] = buffer[*position + s];
548 void kill_after_cursor(char *buffer, int *position) {
549 buffer[*position] = '\0';
552 /*********************************************************************/
554 int previous_visible(int current_line, char **lines, struct matcher *matcher) {
555 int line = current_line - 1;
556 while(line >= 0 && !match(matcher, lines[line], 0, 0)) line--;
560 int next_visible(int current_line, int nb_lines, char **lines,
561 struct matcher *matcher) {
562 int line = current_line + 1;
563 while(line < nb_lines && !match(matcher, lines[line], 0, 0)) line++;
571 /*********************************************************************/
573 void print_string_with_switches(char *buffer, int line_width,
575 int nb_patterns, int *switches) {
576 int w, current = 0, next;
578 for(w = 0; w < nb_patterns && switches[2 * w] < line_width; w++) {
579 if(switches[2 * w] < switches[2 * w + 1]) {
580 next = switches[2 * w];
581 if(next > line_width) { next = line_width; }
582 if(next > current) { addnstr(buffer + current, next - current); }
585 next = switches[2 * w + 1];
586 if(next > line_width) { next = line_width; }
587 if(next > current) { addnstr(buffer + current, next - current); }
592 if(current < line_width) {
593 addnstr(buffer + current, console_width - current);
596 addnstr(buffer, console_width);
600 /* The line highlighted is the first one matching the matcher in that
601 order: (1) current_focus_line after motion, if it does not match,
602 then (2) the first with a greater index, if none matches, then (3)
603 the first with a lesser index.
605 The index of the line actually shown highlighted is written in
606 displayed_focus_line (it can be -1 if no line at all matches the
609 If there is a motion and a line is actually shown highlighted, its
610 value is written in current_focus_line. */
612 void update_screen(int *current_focus_line, int *displayed_focus_line,
614 int nb_lines, char **lines,
618 char buffer[BUFFER_SIZE];
619 struct matcher matcher;
621 int console_width, console_height;
622 int nb_printed_lines = 0;
626 initialize_matcher(&matcher, use_regexp, case_sensitive, pattern);
629 if(matcher.nb_patterns >= 0) {
630 switches = safe_malloc(sizeof(int) * matcher.nb_patterns * 2);
632 switches = safe_malloc(sizeof(int) * 2);
638 console_width = getmaxx(stdscr);
639 console_height = getmaxy(stdscr);
641 use_default_colors();
643 /* Add an empty line where we will print the modeline at the end */
647 /* If the regexp is erroneous, print a message saying so */
649 if(matcher.regexp_error) {
651 addnstr("Regexp syntax error", console_width);
655 /* Else, and we do have lines to select from, find a visible line. */
657 else if(nb_lines > 0) {
659 if(match(&matcher, lines[*current_focus_line], 0, 0)) {
660 new_focus_line = *current_focus_line;
662 new_focus_line = next_visible(*current_focus_line, nb_lines, lines,
664 if(new_focus_line < 0) {
665 new_focus_line = previous_visible(*current_focus_line, lines, &matcher);
669 /* If we found a visible line and we should move, let's move */
671 if(new_focus_line >= 0 && motion != 0) {
672 int l = new_focus_line;
674 /* We want to go down, let's find the first visible line below */
675 for(m = 0; l >= 0 && m < motion; m++) {
676 l = next_visible(l, nb_lines, lines, &matcher);
682 /* We want to go up, let's find the first visible line above */
683 for(m = 0; l >= 0 && m < -motion; m++) {
684 l = previous_visible(l, lines, &matcher);
692 /* Here new_focus_line is either a line number matching the
695 if(new_focus_line >= 0) {
697 int first_line = new_focus_line, last_line = new_focus_line;
700 /* We find the first and last lines to show, so that the total
701 of visible lines between them (them included) is
704 while(nb_match < console_height-1 &&
705 (first_line > 0 || last_line < nb_lines - 1)) {
709 while(first_line > 0 && !match(&matcher, lines[first_line], 0, 0)) {
712 if(match(&matcher, lines[first_line], 0, 0)) {
717 if(nb_match < console_height - 1 && last_line < nb_lines - 1) {
719 while(last_line < nb_lines - 1 && !match(&matcher, lines[last_line], 0, 0)) {
723 if(match(&matcher, lines[last_line], 0, 0)) {
729 /* Now we display them */
731 for(l = first_line; l <= last_line; l++) {
732 if(match(&matcher, lines[l], &nb_switches, switches)) {
735 while(lines[l][k] && k < BUFFER_SIZE - 2 && k < console_width) {
736 buffer[k] = lines[l][k];
740 /* Highlight the highlighted line ... */
742 if(l == new_focus_line) {
743 if(show_long_lines && k >= console_width) {
744 if(console_width >= 4) {
745 buffer[console_width - 4] = ' ';
746 buffer[console_width - 3] = '.';
747 buffer[console_width - 2] = '.';
748 buffer[console_width - 1] = '.';
751 while(k < console_width) {
755 attron(attr_focus_line);
756 print_string_with_switches(buffer, k, console_width,
757 nb_switches / 2, switches);
758 attroff(attr_focus_line);
760 if(show_long_lines && k >= console_width) {
761 if(console_width >= 4) {
762 buffer[console_width - 4] = ' ';
763 buffer[console_width - 3] = '.';
764 buffer[console_width - 2] = '.';
765 buffer[console_width - 1] = '.';
772 print_string_with_switches(buffer, k, console_width,
773 nb_switches / 2, switches);
780 /* If we are on a focused line and we moved, this become the new
784 *current_focus_line = new_focus_line;
788 *displayed_focus_line = new_focus_line;
790 if(nb_printed_lines == 0) {
792 addnstr("No selection", console_width);
797 /* Else, print a message saying that there are no lines to select from */
801 addnstr("Empty choice", console_width);
807 /* Draw the modeline */
811 attron(attr_modeline);
813 for(k = 0; k < console_width; k++) buffer[k] = ' ';
814 buffer[console_width] = '\0';
815 addnstr(buffer, console_width);
819 /* There must be a more elegant way of moving the cursor at a
820 location met during display */
827 cursor_x += strlen(title) + 1;
830 sprintf(buffer, "%d/%d ", nb_printed_lines, nb_lines);
832 cursor_x += strlen(buffer);
834 addnstr(pattern, cursor_position);
835 cursor_x += cursor_position;
837 if(pattern[cursor_position]) {
838 addstr(pattern + cursor_position);
843 /* Add a few info about the mode we are in (regexp and/or case
846 if(use_regexp || matcher.case_sensitive) {
852 if(matcher.case_sensitive) {
863 attroff(attr_modeline);
868 if(switches) { free(switches); }
869 free_matcher(&matcher);
872 /*********************************************************************/
874 void store_line(struct hash_table_t *hash_table,
875 const char *new_line,
876 int *nb_lines, char **lines) {
879 /* Remove the zsh history prefix */
881 if(zsh_history && *new_line == ':') {
882 while(*new_line && *new_line != ';') new_line++;
883 if(*new_line == ';') new_line++;
886 /* Remove the bash history prefix */
889 while(*new_line == ' ') new_line++;
890 while(*new_line >= '0' && *new_line <= '9') new_line++;
891 while(*new_line == ' ') new_line++;
894 /* Check for duplicates with the hash table and insert the line in
895 the list if necessary */
898 dup = add_and_get_previous_index(hash_table,
899 new_line, *nb_lines, lines);
905 lines[*nb_lines] = safe_malloc((strlen(new_line) + 1) * sizeof(char));
906 strcpy(lines[*nb_lines], new_line);
908 /* The string was already in there, so we do not allocate a new
909 string but use the pointer to the first occurence of it */
910 lines[*nb_lines] = lines[dup];
917 void read_file(struct hash_table_t *hash_table,
918 const char *input_filename,
919 int nb_lines_max, int *nb_lines, char **lines) {
921 char raw_line[BUFFER_SIZE];
925 file = fopen(input_filename, "r");
928 fprintf(stderr, "selector: Can not open `%s'.\n", input_filename);
932 while(*nb_lines < nb_lines_max && fgets(raw_line, BUFFER_SIZE, file)) {
933 for(s = raw_line + strlen(raw_line) - 1; s > raw_line && *s == '\n'; s--) {
936 store_line(hash_table, raw_line, nb_lines, lines);
942 /*********************************************************************/
944 /* For long options that have no equivalent short option, use a
945 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
948 OPT_BASH_MODE = CHAR_MAX + 1
951 static struct option long_options[] = {
952 { "output-file", 1, 0, 'o' },
953 { "pattern-separator", 1, 0, 's' },
954 { "label-separator", 1, 0, 'x' },
955 { "inject-in-tty", no_argument, 0, 'v' },
956 { "add-control-qs", no_argument, 0, 'w' },
957 { "monochrome", no_argument, 0, 'm' },
958 { "no-beep", no_argument, 0, 'q' },
959 { "revert-order", no_argument, 0, 'i' },
960 { "remove-bash-prefix", no_argument, 0, 'b' },
961 { "remove-zsh-prefix", no_argument, 0, 'z' },
962 { "remove-duplicates", no_argument, 0, 'd' },
963 { "regexp", no_argument, 0, 'e' },
964 { "case-sensitive", no_argument, 0, 'a' },
965 { "show-long-lines", no_argument, 0, 'j'},
966 { "show-hits", no_argument, 0, 'j'},
967 { "upper-case-makes-case-sensitive", no_argument, 0, 'u' },
968 { "title", 1, 0, 't' },
969 { "number-of-lines", 1, 0, 'l' },
970 { "colors", 1, 0, 'c' },
971 { "bash", no_argument, 0, OPT_BASH_MODE },
972 { "help", no_argument, 0, 'h' },
976 int main(int argc, char **argv) {
978 char output_filename[BUFFER_SIZE];
979 char pattern[BUFFER_SIZE];
982 int error = 0, show_help = 0, done = 0;
984 int current_focus_line, displayed_focus_line;
987 int color_fg_modeline, color_bg_modeline;
988 int color_fg_highlight, color_bg_highlight;
990 char **lines, **labels;
992 struct hash_table_t *hash_table;
995 if(!isatty(STDIN_FILENO)) {
996 fprintf(stderr, "selector: The standard input is not a tty.\n");
1000 color_fg_modeline = COLOR_WHITE;
1001 color_bg_modeline = COLOR_BLACK;
1002 color_fg_highlight = COLOR_BLACK;
1003 color_bg_highlight = COLOR_YELLOW;
1005 setlocale(LC_ALL, "");
1007 strcpy(output_filename, "");
1009 while ((c = getopt_long(argc, argv, "o:s:x:vwmqf:ibzdeajyunt:l:c:-h",
1010 long_options, NULL)) != -1) {
1015 strncpy(output_filename, optarg, BUFFER_SIZE);
1019 pattern_separator = optarg[0];
1023 label_separator = optarg[0];
1027 output_to_vt_buffer = 1;
1055 remove_duplicates = 1;
1067 show_long_lines = 1;
1075 upper_caps_makes_case_sensitive = 1;
1080 title = safe_malloc((strlen(optarg) + 1) * sizeof(char));
1081 strcpy(title, optarg);
1085 str_to_positive_integers(optarg, &nb_lines_max, 1);
1089 str_to_positive_integers(optarg, colors, 4);
1090 color_fg_modeline = colors[0];
1091 color_bg_modeline = colors[1];
1092 color_fg_highlight = colors[2];
1093 color_bg_highlight = colors[3];
1101 /* Same as -c 7,4,0,3 -q */
1102 /* color_fg_modeline = 7; */
1103 /* color_bg_modeline = 4; */
1104 /* color_fg_highlight = 0; */
1105 /* color_bg_highlight = 3; */
1106 /* error_flash = 1; */
1107 /* Same as -b -i -d -v -w */
1110 remove_duplicates = 1;
1111 output_to_vt_buffer = 1;
1113 bash_histsize = getenv("HISTSIZE");
1115 str_to_positive_integers(bash_histsize, &nb_lines_max, 1);
1135 lines = safe_malloc(nb_lines_max * sizeof(char *));
1139 if(remove_duplicates) {
1140 hash_table = new_hash_table(nb_lines_max * 10);
1145 while(optind < argc) {
1146 read_file(hash_table,
1148 nb_lines_max, &nb_lines, lines);
1153 free_hash_table(hash_table);
1156 /* Now remove the null strings */
1159 for(k = 0; k < nb_lines; k++) {
1161 lines[n++] = lines[k];
1168 for(l = 0; l < nb_lines / 2; l++) {
1169 char *s = lines[nb_lines - 1 - l];
1170 lines[nb_lines - 1 - l] = lines[l];
1175 /* Build the labels from the strings, take only the part before the
1176 label_separator and transform control characters to printable
1179 labels = safe_malloc(nb_lines * sizeof(char *));
1181 for(l = 0; l < nb_lines; l++) {
1187 while(*t && *t != label_separator) {
1192 labels[l] = safe_malloc((e + 1) * sizeof(char));
1195 while(*t && *t != label_separator) {
1197 while(*u) { *s++ = *u++; }
1204 cursor_position = 0;
1206 /* Here we start to display with curse */
1211 intrflush(stdscr, FALSE);
1213 /* So that the arrow keys work */
1214 keypad(stdscr, TRUE);
1216 attr_error = A_STANDOUT;
1217 attr_modeline = A_REVERSE;
1218 attr_focus_line = A_STANDOUT;
1221 if(with_colors && has_colors()) {
1225 if(color_fg_modeline < 0 || color_fg_modeline >= COLORS ||
1226 color_bg_modeline < 0 || color_bg_modeline >= COLORS ||
1227 color_fg_highlight < 0 || color_bg_highlight >= COLORS ||
1228 color_bg_highlight < 0 || color_bg_highlight >= COLORS) {
1231 fprintf(stderr, "selector: Color numbers have to be between 0 and %d.\n",
1236 init_pair(1, color_fg_modeline, color_bg_modeline);
1237 attr_modeline = COLOR_PAIR(1);
1239 init_pair(2, color_fg_highlight, color_bg_highlight);
1240 attr_focus_line = COLOR_PAIR(2);
1242 init_pair(3, COLOR_WHITE, COLOR_RED);
1243 attr_error = COLOR_PAIR(3);
1247 current_focus_line = 0;
1248 displayed_focus_line = 0;
1250 update_screen(¤t_focus_line, &displayed_focus_line,
1252 nb_lines, labels, cursor_position, pattern);
1259 if(key >= ' ' && key <= '~') { /* Insert character */
1260 insert_char(pattern, &cursor_position, key);
1263 else if(key == KEY_BACKSPACE ||
1264 key == '\010' || /* ^H */
1265 key == '\177') { /* ^? */
1266 backspace_char(pattern, &cursor_position);
1269 else if(key == KEY_DC ||
1270 key == '\004') { /* ^D */
1271 delete_char(pattern, &cursor_position);
1274 else if(key == KEY_HOME) {
1275 current_focus_line = 0;
1278 else if(key == KEY_END) {
1279 current_focus_line = nb_lines - 1;
1282 else if(key == KEY_NPAGE) {
1286 else if(key == KEY_PPAGE) {
1290 else if(key == KEY_DOWN ||
1291 key == '\016') { /* ^N */
1295 else if(key == KEY_UP ||
1296 key == '\020') { /* ^P */
1300 else if(key == KEY_LEFT ||
1301 key == '\002') { /* ^B */
1302 if(cursor_position > 0) cursor_position--;
1303 else error_feedback();
1306 else if(key == KEY_RIGHT ||
1307 key == '\006') { /* ^F */
1308 if(pattern[cursor_position]) cursor_position++;
1309 else error_feedback();
1312 else if(key == '\001') { /* ^A */
1313 cursor_position = 0;
1316 else if(key == '\005') { /* ^E */
1317 cursor_position = strlen(pattern);
1320 else if(key == '\022') { /* ^R */
1321 use_regexp = !use_regexp;
1324 else if(key == '\011') { /* ^I */
1325 case_sensitive = !case_sensitive;
1328 else if(key == '\025') { /* ^U */
1329 kill_before_cursor(pattern, &cursor_position);
1332 else if(key == '\013') { /* ^K */
1333 kill_after_cursor(pattern, &cursor_position);
1336 else if(key == '\014') { /* ^L */
1337 /* I suspect that we may sometime mess up the display, so ^L is
1338 here to force a full refresh */
1342 else if(key == '\007' || /* ^G */
1343 key == '\033' || /* ^[ (escape) */
1349 else if(key == KEY_RESIZE || key == -1) {
1350 /* Do nothing when the tty is resized */
1358 update_screen(¤t_focus_line, &displayed_focus_line,
1360 nb_lines, labels, cursor_position, pattern);
1367 /* Here we come back to standard display */
1369 if(key == KEY_ENTER || key == '\n') {
1373 if(displayed_focus_line >= 0 && displayed_focus_line < nb_lines) {
1374 t = lines[displayed_focus_line];
1375 if(label_separator) {
1376 while(*t && *t != label_separator) t++;
1383 if(output_to_vt_buffer && t) {
1384 inject_into_tty_buffer(t, add_control_qs);
1387 if(output_filename[0]) {
1388 FILE *out = fopen(output_filename, "w");
1391 fprintf(out, "%s", t);
1396 "selector: Can not open %s for writing.\n",
1404 printf("Aborted.\n");
1407 for(l = 0; l < nb_lines; l++) {