3 * selector is a simple command line utility for selection of strings
4 * with a dynamic pattern-matching.
6 * Copyright (c) 2009-2015 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)
42 #include <sys/ioctl.h>
49 #define VERSION "1.1.7"
51 #define BUFFER_SIZE 16384
53 /* Yeah, global variables! */
55 int global_nb_lines_max = 1000;
56 char global_pattern_separator = ';';
57 char global_label_separator = '\0';
58 int global_output_to_vt_buffer = 0;
59 int global_add_control_qs = 0;
60 int global_with_colors = 1;
61 int global_zsh_history = 0;
62 int global_bash_history = 0;
63 int global_inverse_order = 0;
64 int global_remove_duplicates = 0;
65 int global_use_regexp = 0;
66 int global_case_sensitive = 0;
67 char *global_title = 0;
68 int global_error_flash = 0;
69 int global_upper_caps_makes_case_sensitive = 0;
70 int global_show_long_lines = 0;
71 int global_show_hits = 0;
73 int global_attr_modeline, global_attr_focus_line, global_attr_error, global_attr_hits;
75 /********************************************************************/
77 /* malloc with error checking. */
79 void *safe_malloc(size_t n) {
83 "selector: cannot allocate memory: %s\n", strerror(errno));
89 /*********************************************************************/
91 void inject_into_tty_buffer(char *string, int add_control_qs) {
92 struct termios oldtio, newtio;
94 const char control_q = '\021';
95 tcgetattr(STDIN_FILENO, &oldtio);
96 memset(&newtio, 0, sizeof(newtio));
97 /* Set input mode (non-canonical, *no echo*,...) */
98 tcsetattr(STDIN_FILENO, TCSANOW, &newtio);
99 /* Put the selected string in the tty input buffer */
100 for(k = string; *k; k++) {
101 if(add_control_qs && !(*k >= ' ' && *k <= '~')) {
102 /* Add ^Q to quote control characters */
103 ioctl(STDIN_FILENO, TIOCSTI, &control_q);
105 ioctl(STDIN_FILENO, TIOCSTI, k);
107 /* Restore the old settings */
108 tcsetattr(STDIN_FILENO, TCSANOW, &oldtio);
111 /*********************************************************************/
113 void str_to_positive_integers(char *string, int *values, int nb) {
114 int current_value, got_one;
124 if(*s >= '0' && *s <= '9') {
125 current_value = current_value * 10 + (int) (*s - '0');
127 } else if(*s == ',' || *s == '\0') {
130 values[n++] = current_value;
136 "selector: Missing value in `%s'.\n", string);
144 "selector: Too many values in `%s'.\n", string);
149 "selector: Empty value in `%s'.\n", string);
154 "selector: Syntax error in `%s'.\n", string);
161 void error_feedback() {
162 if(global_error_flash) {
169 void usage(FILE *out) {
171 fprintf(out, "Selector version %s (%s)\n", VERSION, UNAME);
172 fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
174 fprintf(out, "Usage: selector [options] [<filename1> [<filename2> ...]]\n");
176 fprintf(out, " -h, --help\n");
177 fprintf(out, " show this help\n");
178 fprintf(out, " -v, --inject-in-tty\n");
179 fprintf(out, " inject the selected line in the tty\n");
180 fprintf(out, " -w, --add-control-qs\n");
181 fprintf(out, " quote control characters with ^Qs when using -v\n");
182 fprintf(out, " -d, --remove-duplicates\n");
183 fprintf(out, " remove duplicated lines\n");
184 fprintf(out, " -b, --remove-bash-prefix\n");
185 fprintf(out, " remove the bash history line prefix\n");
186 fprintf(out, " -z, --remove-zsh-prefix\n");
187 fprintf(out, " remove the zsh history line prefix\n");
188 fprintf(out, " -i, --revert-order\n");
189 fprintf(out, " invert the order of lines\n");
190 fprintf(out, " -e, --regexp\n");
191 fprintf(out, " start in regexp mode\n");
192 fprintf(out, " -a, --case-sensitive\n");
193 fprintf(out, " start in case sensitive mode\n");
194 fprintf(out, " -j, --show-long-lines\n");
195 fprintf(out, " print a long-line indicator at the end of truncated lines\n");
196 fprintf(out, " -y, --show-hits\n");
197 fprintf(out, " highlight the matching substrings\n");
198 fprintf(out, " -u, --upper-case-makes-case-sensitive\n");
199 fprintf(out, " using an upper case character in the matching string makes\n");
200 fprintf(out, " the matching case-sensitive\n");
201 fprintf(out, " -m, --monochrome\n");
202 fprintf(out, " monochrome mode\n");
203 fprintf(out, " -q, --no-beep\n");
204 fprintf(out, " make a flash instead of a beep on an edition error\n");
205 fprintf(out, " --bash\n");
206 fprintf(out, " setting for bash history search, same as -b -i -d -v -w -l ${HISTSIZE}\n");
207 fprintf(out, " --\n");
208 fprintf(out, " all following arguments are filenames\n");
209 fprintf(out, " -t <title>, --title <title>\n");
210 fprintf(out, " add a title in the modeline\n");
211 fprintf(out, " -r <pattern>, --pattern <pattern>\n");
212 fprintf(out, " set an initial pattern\n");
213 fprintf(out, " -c <colors>, --colors <colors>\n");
214 fprintf(out, " set the display colors with an argument of the form\n");
215 fprintf(out, " <fg_modeline>,<bg_modeline>,<fg_highlight>,<bg_highlight>\n");
216 fprintf(out, " -o <output filename>, --output-file <output filename>\n");
217 fprintf(out, " set a file to write the selected line to\n");
218 fprintf(out, " -s <pattern separator>, --pattern-separator <pattern separator>\n");
219 fprintf(out, " set the symbol to separate substrings in the pattern\n");
220 fprintf(out, " -x <label separator>, --label-separator <label separator>\n");
221 fprintf(out, " set the character to separate the label to show from the\n");
222 fprintf(out, " string to return\n");
223 fprintf(out, " -l <max number of lines>, --number-of-lines <max number of lines>\n");
224 fprintf(out, " set the maximum number of lines to take into account\n");
228 /*********************************************************************/
230 /* A quick and dirty hash table */
232 #define MAGIC_HASH_MULTIPLIER 387433
234 /* The table itself stores indexes of the strings taken in a char**
235 table. When a string is added, if it was already in the table, the
236 new index replaces the previous one. */
238 struct hash_table_t {
243 struct hash_table_t *new_hash_table(int size) {
245 struct hash_table_t *hash_table;
247 hash_table = safe_malloc(sizeof(struct hash_table_t));
249 hash_table->size = size;
250 hash_table->entries = safe_malloc(hash_table->size * sizeof(int));
252 for(k = 0; k < hash_table->size; k++) {
253 hash_table->entries[k] = -1;
259 void free_hash_table(struct hash_table_t *hash_table) {
260 free(hash_table->entries);
264 /* Adds new_string in the table, associated to new_index. If this
265 string was not already in the table, returns -1. Otherwise, returns
266 the previous index it had. */
268 int add_and_get_previous_index(struct hash_table_t *hash_table,
269 const char *new_string, int new_index,
272 unsigned int code = 0, start;
275 /* This is my recipe. I checked, it seems to work (as long as
276 hash_table->size is not a multiple of MAGIC_HASH_MULTIPLIER that
279 for(k = 0; new_string[k]; k++) {
280 code = code * MAGIC_HASH_MULTIPLIER + (unsigned int) (new_string[k]);
283 code = code % hash_table->size;
286 while(hash_table->entries[code] >= 0) {
287 /* There is a string with that code */
288 if(strcmp(new_string, strings[hash_table->entries[code]]) == 0) {
289 /* It is the same string, we keep a copy of the stored index */
290 int result = hash_table->entries[code];
291 /* Put the new one */
292 hash_table->entries[code] = new_index;
293 /* And return the previous one */
296 /* This collision was not the same string, let's move to the next
298 code = (code + 1) % hash_table->size;
299 /* We came back to our original code, which means that the table
303 "Full hash table (that should not happen)\n");
308 /* This string was not already in there, store the index in the
309 table and return -1 */
311 hash_table->entries[code] = new_index;
315 /*********************************************************************
316 A matcher matches either with a collection of substrings, or with a
324 char *splitted_patterns, **patterns;
327 /* Routine to add an interval to a sorted list of intervals
328 extremities. Returns the resulting number of extremities.
330 This routine is an effing nightmare */
332 int add_interval(int n, int *switches, int start, int end) {
335 if(start == end) { return n; }
338 while(f < n && switches[f] <= start) { f++; }
340 while(g < n && switches[g] <= end) { g++; }
343 /* switches[f-1] start end */
353 /* switches[f-1] start switches[f] switches[g-1] end switches[g] */
354 /* |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX| ... |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX| */
355 for(k = f; k < n; k++) { switches[k] = switches[k + (g - f)]; }
358 /* switches[f-1] start switches[f] switches[g-1] end switches[g] */
359 /* |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX| ... XXXXXXXXXXXX| |XXXXXXXXXX */
360 switches[g - 1] = end;
361 for(k = f; k < n; k++) { switches[k] = switches[k + ((g - 1) - f)]; }
362 return n - ((g - 1) - f);
368 /* switches[f-1] start end switches[f] */
369 /* XXXXXXXXXXXX| |XXXXXXXXXX */
370 for(k = n - 1; k >= f; k--) {
371 switches[k + 2] = switches[k];
374 switches[f + 1] = end;
379 /* switches[f-1] start switches[f] switches[g-1] end switches[g] */
380 /* XXXXXXXXXXXX| |XXXXXXXXXX ... |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX| */
382 for(k = f + 1; k < n; k++) { switches[k] = switches[k + (g - (f + 1))]; }
383 return n - (g - (f + 1));
385 /* switches[f-1] start switches[f] switches[g-1] end switches[g] */
386 /* XXXXXXXXXXXX| |XXXXXXXXXX ... XXXXXXXXXXXX| |XXXXXXXXXX */
388 switches[g - 1] = end;
389 for(k = f + 1; k < n; k++) { switches[k] = switches[k + ((g - 1) - (f + 1))]; }
390 return n - ((g - 1) - (f + 1));
395 int match(struct matcher *matcher, char *string, int *nb_switches, int *switches) {
400 if(nb_switches) { *nb_switches = 0; }
402 if(matcher->nb_patterns >= 0) {
403 if(matcher->case_sensitive) {
404 for(n = 0; n < matcher->nb_patterns; n++) {
405 if((where = strstr(string, matcher->patterns[n])) == 0) return 0;
407 *nb_switches = add_interval(*nb_switches, switches,
408 (int) (where - string),
409 (int) (where - string) + strlen(matcher->patterns[n]));
413 for(n = 0; n < matcher->nb_patterns; n++) {
414 if((where = strcasestr(string, matcher->patterns[n])) == 0) return 0;
416 *nb_switches = add_interval(*nb_switches, switches,
417 (int) (where - string),
418 (int) (where - string) + strlen(matcher->patterns[n]));
425 if(regexec(&matcher->preg, string, 1, &matches, 0) == 0) {
427 switches[0] = matches.rm_so;
428 switches[1] = matches.rm_eo;
434 return regexec(&matcher->preg, string, 0, 0, 0) == 0;
439 void free_matcher(struct matcher *matcher) {
440 if(matcher->nb_patterns < 0) {
441 if(!matcher->regexp_error) regfree(&matcher->preg);
443 free(matcher->splitted_patterns);
444 free(matcher->patterns);
448 void initialize_matcher(struct matcher *matcher,
449 int use_regexp, int case_sensitive,
450 const char *pattern) {
452 char *t, *last_pattern_start;
456 matcher->case_sensitive = case_sensitive;
457 matcher->nb_patterns = -1;
458 matcher->regexp_error = regcomp(&matcher->preg, pattern,
459 case_sensitive ? 0 : REG_ICASE);
461 matcher->regexp_error = 0;
462 matcher->nb_patterns = 1;
464 if(global_upper_caps_makes_case_sensitive) {
465 for(s = pattern; *s && !case_sensitive; s++) {
466 case_sensitive = (*s >= 'A' && *s <= 'Z');
470 matcher->case_sensitive = case_sensitive;
472 for(s = pattern; *s; s++) {
473 if(*s == global_pattern_separator) {
474 matcher->nb_patterns++;
478 matcher->splitted_patterns =
479 safe_malloc((strlen(pattern) + 1) * sizeof(char));
482 safe_malloc(matcher->nb_patterns * sizeof(char *));
484 strcpy(matcher->splitted_patterns, pattern);
487 last_pattern_start = matcher->splitted_patterns;
488 for(t = matcher->splitted_patterns; n < matcher->nb_patterns; t++) {
489 if(*t == global_pattern_separator || *t == '\0') {
491 matcher->patterns[n++] = last_pattern_start;
492 last_pattern_start = t + 1;
498 /*********************************************************************
501 void delete_char(char *buffer, int *position) {
502 if(buffer[*position]) {
504 while(c < BUFFER_SIZE && buffer[c]) {
505 buffer[c] = buffer[c+1];
508 } else error_feedback();
511 void backspace_char(char *buffer, int *position) {
513 if(buffer[*position]) {
514 int c = *position - 1;
516 buffer[c] = buffer[c+1];
520 buffer[*position - 1] = '\0';
524 } else error_feedback();
527 void insert_char(char *buffer, int *position, char character) {
528 if(strlen(buffer) < BUFFER_SIZE - 1) {
530 char t = buffer[c], u;
539 buffer[(*position)++] = character;
540 } else error_feedback();
543 void kill_before_cursor(char *buffer, int *position) {
545 while(buffer[*position + s]) {
546 buffer[s] = buffer[*position + s];
553 void kill_after_cursor(char *buffer, int *position) {
554 buffer[*position] = '\0';
557 /*********************************************************************/
559 int previous_visible(int current_line, char **lines, struct matcher *matcher) {
560 int line = current_line - 1;
561 while(line >= 0 && !match(matcher, lines[line], 0, 0)) line--;
565 int next_visible(int current_line, int nb_lines, char **lines,
566 struct matcher *matcher) {
567 int line = current_line + 1;
568 while(line < nb_lines && !match(matcher, lines[line], 0, 0)) line++;
576 /*********************************************************************/
578 void print_string_with_switches(char *buffer, int line_width,
579 int nb_patterns, int *switches) {
580 int w, current = 0, next;
582 for(w = 0; w < nb_patterns && switches[2 * w] < line_width; w++) {
583 if(switches[2 * w] < switches[2 * w + 1]) {
584 next = switches[2 * w];
585 if(next > line_width) { next = line_width; }
586 if(next > current) { addnstr(buffer + current, next - current); }
587 attron(global_attr_hits);
589 next = switches[2 * w + 1];
590 if(next > line_width) { next = line_width; }
591 if(next > current) { addnstr(buffer + current, next - current); }
592 attroff(global_attr_hits);
596 if(current < line_width) {
597 addnstr(buffer + current, line_width - current);
600 addnstr(buffer, line_width);
604 /* The line highlighted is the first one matching the matcher in that
605 order: (1) current_focus_line after motion, if it does not match,
606 then (2) the first with a greater index, if none matches, then (3)
607 the first with a lesser index.
609 The index of the line actually shown highlighted is written in
610 displayed_focus_line (it can be -1 if no line at all matches the
613 If there is a motion and a line is actually shown highlighted, its
614 value is written in current_focus_line. */
616 void update_screen(int *current_focus_line, int *displayed_focus_line,
618 int nb_lines, char **lines,
622 char buffer[BUFFER_SIZE];
623 struct matcher matcher;
625 int console_width, console_height;
626 int nb_printed_lines = 0;
630 initialize_matcher(&matcher, global_use_regexp, global_case_sensitive, pattern);
632 if(global_show_hits) {
633 if(matcher.nb_patterns >= 0) {
634 switches = safe_malloc(sizeof(int) * matcher.nb_patterns * 2);
636 switches = safe_malloc(sizeof(int) * 2);
642 console_width = getmaxx(stdscr);
643 console_height = getmaxy(stdscr);
645 use_default_colors();
647 /* Add an empty line where we will print the modeline at the end */
651 /* If the regexp is erroneous, print a message saying so */
653 if(matcher.regexp_error) {
654 attron(global_attr_error);
655 addnstr("Regexp syntax error", console_width);
656 attroff(global_attr_error);
659 /* Else, and we do have lines to select from, find a visible line. */
661 else if(nb_lines > 0) {
663 if(match(&matcher, lines[*current_focus_line], 0, 0)) {
664 new_focus_line = *current_focus_line;
666 new_focus_line = next_visible(*current_focus_line, nb_lines, lines,
668 if(new_focus_line < 0) {
669 new_focus_line = previous_visible(*current_focus_line, lines, &matcher);
673 /* If we found a visible line and we should move, let's move */
675 if(new_focus_line >= 0 && motion != 0) {
676 int l = new_focus_line;
678 /* We want to go down, let's find the first visible line below */
679 for(m = 0; l >= 0 && m < motion; m++) {
680 l = next_visible(l, nb_lines, lines, &matcher);
686 /* We want to go up, let's find the first visible line above */
687 for(m = 0; l >= 0 && m < -motion; m++) {
688 l = previous_visible(l, lines, &matcher);
696 /* Here new_focus_line is either a line number matching the
699 if(new_focus_line >= 0) {
701 int first_line = new_focus_line, last_line = new_focus_line;
704 /* We find the first and last lines to show, so that the total
705 of visible lines between them (them included) is
708 while(nb_match < console_height-1 &&
709 (first_line > 0 || last_line < nb_lines - 1)) {
713 while(first_line > 0 && !match(&matcher, lines[first_line], 0, 0)) {
716 if(match(&matcher, lines[first_line], 0, 0)) {
721 if(nb_match < console_height - 1 && last_line < nb_lines - 1) {
723 while(last_line < nb_lines - 1 && !match(&matcher, lines[last_line], 0, 0)) {
727 if(match(&matcher, lines[last_line], 0, 0)) {
733 /* Now we display them */
735 for(l = first_line; l <= last_line; l++) {
736 if(match(&matcher, lines[l], &nb_switches, switches)) {
739 while(lines[l][k] && k < BUFFER_SIZE - 2 && k < console_width) {
740 buffer[k] = lines[l][k];
744 /* Highlight the highlighted line ... */
746 if(l == new_focus_line) {
747 if(global_show_long_lines && k >= console_width) {
748 attron(global_attr_focus_line);
749 print_string_with_switches(buffer, console_width-1,
750 nb_switches / 2, switches);
751 /* attron(global_attr_error); */
753 /* attroff(global_attr_error); */
754 attroff(global_attr_focus_line);
756 while(k < console_width) {
759 attron(global_attr_focus_line);
760 print_string_with_switches(buffer, k,
761 nb_switches / 2, switches);
762 attroff(global_attr_focus_line);
765 if(global_show_long_lines && k >= console_width) {
766 print_string_with_switches(buffer, console_width-1,
767 nb_switches / 2, switches);
768 attron(global_attr_focus_line);
770 attroff(global_attr_focus_line);
772 if(k < console_width) {
776 print_string_with_switches(buffer, k,
777 nb_switches / 2, switches);
785 /* If we are on a focused line and we moved, this become the new
789 *current_focus_line = new_focus_line;
793 *displayed_focus_line = new_focus_line;
795 if(nb_printed_lines == 0) {
796 attron(global_attr_error);
797 addnstr("No selection", console_width);
798 attroff(global_attr_error);
802 /* Else, print a message saying that there are no lines to select from */
805 attron(global_attr_error);
806 addnstr("Empty choice", console_width);
807 attroff(global_attr_error);
812 /* Draw the modeline */
816 attron(global_attr_modeline);
818 for(k = 0; k < console_width; k++) buffer[k] = ' ';
819 buffer[console_width] = '\0';
820 addnstr(buffer, console_width);
824 /* There must be a more elegant way of moving the cursor at a
825 location met during display */
830 addstr(global_title);
832 cursor_x += strlen(global_title) + 1;
835 sprintf(buffer, "%d/%d ", nb_printed_lines, nb_lines);
837 cursor_x += strlen(buffer);
839 addnstr(pattern, cursor_position);
840 cursor_x += cursor_position;
842 if(pattern[cursor_position]) {
843 addstr(pattern + cursor_position);
848 /* Add a few info about the mode we are in (regexp and/or case
851 if(global_use_regexp || matcher.case_sensitive) {
853 if(global_use_regexp) {
857 if(matcher.case_sensitive) {
858 if(global_use_regexp) {
868 attroff(global_attr_modeline);
873 if(switches) { free(switches); }
874 free_matcher(&matcher);
877 /*********************************************************************/
879 void store_line(struct hash_table_t *hash_table,
880 const char *new_line,
881 int *nb_lines, char **lines) {
884 /* Remove the zsh history prefix */
886 if(global_zsh_history && *new_line == ':') {
887 while(*new_line && *new_line != ';') new_line++;
888 if(*new_line == ';') new_line++;
891 /* Remove the bash history prefix */
893 if(global_bash_history) {
894 while(*new_line == ' ') new_line++;
895 while(*new_line >= '0' && *new_line <= '9') new_line++;
896 while(*new_line == ' ') new_line++;
899 /* Check for duplicates with the hash table and insert the line in
900 the list if necessary */
903 dup = add_and_get_previous_index(hash_table,
904 new_line, *nb_lines, lines);
910 lines[*nb_lines] = safe_malloc((strlen(new_line) + 1) * sizeof(char));
911 strcpy(lines[*nb_lines], new_line);
913 /* The string was already in there, so we do not allocate a new
914 string but use the pointer to the first occurence of it */
915 lines[*nb_lines] = lines[dup];
922 void read_file(struct hash_table_t *hash_table,
923 const char *input_filename,
924 int nb_lines_max, int *nb_lines, char **lines) {
926 char raw_line[BUFFER_SIZE];
931 file = fopen(input_filename, "r");
934 fprintf(stderr, "selector: Cannot open `%s'.\n", input_filename);
938 if(global_label_separator == '\n') {
939 while(*nb_lines < nb_lines_max && fgets(raw_line, BUFFER_SIZE, file)) {
940 l = strlen(raw_line);
941 fgets(raw_line + l, BUFFER_SIZE - l, file);
942 for(s = raw_line + strlen(raw_line) - 1; s > raw_line && *s == '\n'; s--) {
945 store_line(hash_table, raw_line, nb_lines, lines);
948 while(*nb_lines < nb_lines_max && fgets(raw_line, BUFFER_SIZE, file)) {
949 for(s = raw_line + strlen(raw_line) - 1; s > raw_line && *s == '\n'; s--) {
952 store_line(hash_table, raw_line, nb_lines, lines);
959 /*********************************************************************/
961 /* For long options that have no equivalent short option, use a
962 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
964 OPT_BASH_MODE = CHAR_MAX + 1
967 static struct option long_options[] = {
968 { "output-file", 1, 0, 'o' },
969 { "pattern-separator", 1, 0, 's' },
970 { "label-separator", 1, 0, 'x' },
971 { "inject-in-tty", no_argument, 0, 'v' },
972 { "add-control-qs", no_argument, 0, 'w' },
973 { "monochrome", no_argument, 0, 'm' },
974 { "no-beep", no_argument, 0, 'q' },
975 { "revert-order", no_argument, 0, 'i' },
976 { "remove-bash-prefix", no_argument, 0, 'b' },
977 { "remove-zsh-prefix", no_argument, 0, 'z' },
978 { "remove-duplicates", no_argument, 0, 'd' },
979 { "regexp", no_argument, 0, 'e' },
980 { "case-sensitive", no_argument, 0, 'a' },
981 { "show-long-lines", no_argument, 0, 'j'},
982 { "show-hits", no_argument, 0, 'j'},
983 { "upper-case-makes-case-sensitive", no_argument, 0, 'u' },
984 { "title", 1, 0, 't' },
985 { "pattern", 1, 0, 'r' },
986 { "number-of-lines", 1, 0, 'l' },
987 { "colors", 1, 0, 'c' },
988 { "bash", no_argument, 0, OPT_BASH_MODE },
989 { "help", no_argument, 0, 'h' },
993 int main(int argc, char **argv) {
995 char output_filename[BUFFER_SIZE];
996 char pattern[BUFFER_SIZE];
999 int error = 0, show_help = 0, done = 0;
1001 int current_focus_line, displayed_focus_line;
1004 int color_fg_modeline, color_bg_modeline;
1005 int color_fg_highlight, color_bg_highlight;
1007 char **lines, **labels;
1009 struct hash_table_t *hash_table;
1010 char *bash_histsize;
1012 if(!isatty(STDIN_FILENO)) {
1013 fprintf(stderr, "selector: The standard input is not a tty.\n");
1017 /* Group and others have no access to created files */
1018 umask(S_IRWXG | S_IRWXO);
1022 color_fg_modeline = COLOR_WHITE;
1023 color_bg_modeline = COLOR_BLACK;
1024 color_fg_highlight = COLOR_BLACK;
1025 color_bg_highlight = COLOR_YELLOW;
1027 setlocale(LC_ALL, "");
1029 strcpy(output_filename, "");
1031 while ((c = getopt_long(argc, argv, "o:s:x:vwmqf:ibzdeajyunt:r:l:c:-h",
1032 long_options, NULL)) != -1) {
1037 strncpy(output_filename, optarg, BUFFER_SIZE);
1041 global_pattern_separator = optarg[0];
1045 if(strcmp(optarg, "\\n") == 0) {
1046 global_label_separator = '\n';
1048 global_label_separator = optarg[0];
1053 global_output_to_vt_buffer = 1;
1057 global_add_control_qs = 1;
1061 global_with_colors = 0;
1065 global_error_flash = 1;
1069 global_inverse_order = 1;
1073 global_bash_history = 1;
1077 global_zsh_history = 1;
1081 global_remove_duplicates = 1;
1085 global_use_regexp = 1;
1089 global_case_sensitive = 1;
1093 global_show_long_lines = 1;
1097 global_show_hits = 1;
1101 global_upper_caps_makes_case_sensitive = 1;
1106 global_title = safe_malloc((strlen(optarg) + 1) * sizeof(char));
1107 strcpy(global_title, optarg);
1111 strcpy(pattern, optarg);
1115 str_to_positive_integers(optarg, &global_nb_lines_max, 1);
1119 str_to_positive_integers(optarg, colors, 4);
1120 color_fg_modeline = colors[0];
1121 color_bg_modeline = colors[1];
1122 color_fg_highlight = colors[2];
1123 color_bg_highlight = colors[3];
1131 /* Same as -c 7,4,0,3 -q */
1132 /* color_fg_modeline = 7; */
1133 /* color_bg_modeline = 4; */
1134 /* color_fg_highlight = 0; */
1135 /* color_bg_highlight = 3; */
1136 /* error_flash = 1; */
1137 /* Same as -b -i -d -v -w */
1138 global_bash_history = 1;
1139 global_inverse_order = 1;
1140 global_remove_duplicates = 1;
1141 global_output_to_vt_buffer = 1;
1142 global_add_control_qs = 1;
1143 bash_histsize = getenv("HISTSIZE");
1145 str_to_positive_integers(bash_histsize, &global_nb_lines_max, 1);
1165 lines = safe_malloc(global_nb_lines_max * sizeof(char *));
1169 if(global_remove_duplicates) {
1170 hash_table = new_hash_table(global_nb_lines_max * 10);
1175 while(optind < argc) {
1176 read_file(hash_table,
1178 global_nb_lines_max, &nb_lines, lines);
1183 free_hash_table(hash_table);
1186 /* Now remove the null strings */
1189 for(k = 0; k < nb_lines; k++) {
1191 lines[n++] = lines[k];
1197 if(global_inverse_order) {
1198 for(l = 0; l < nb_lines / 2; l++) {
1199 char *s = lines[nb_lines - 1 - l];
1200 lines[nb_lines - 1 - l] = lines[l];
1205 /* Build the labels from the strings, take only the part before the
1206 label_separator and transform control characters to printable
1209 labels = safe_malloc(nb_lines * sizeof(char *));
1211 for(l = 0; l < nb_lines; l++) {
1217 while(*t && *t != global_label_separator) {
1222 labels[l] = safe_malloc((e + 1) * sizeof(char));
1225 while(*t && *t != global_label_separator) {
1227 while(*u) { *s++ = *u++; }
1232 cursor_position = 0;
1234 /* Here we start to display with curse */
1239 intrflush(stdscr, FALSE);
1241 /* So that the arrow keys work */
1242 keypad(stdscr, TRUE);
1244 global_attr_error = A_STANDOUT;
1245 global_attr_modeline = A_REVERSE;
1246 global_attr_focus_line = A_STANDOUT;
1247 global_attr_hits = A_BOLD;
1249 if(global_with_colors && has_colors()) {
1253 if(color_fg_modeline < 0 || color_fg_modeline >= COLORS ||
1254 color_bg_modeline < 0 || color_bg_modeline >= COLORS ||
1255 color_fg_highlight < 0 || color_bg_highlight >= COLORS ||
1256 color_bg_highlight < 0 || color_bg_highlight >= COLORS) {
1259 fprintf(stderr, "selector: Color numbers have to be between 0 and %d.\n",
1264 init_pair(1, color_fg_modeline, color_bg_modeline);
1265 global_attr_modeline = COLOR_PAIR(1);
1267 init_pair(2, color_fg_highlight, color_bg_highlight);
1268 global_attr_focus_line = COLOR_PAIR(2);
1270 init_pair(3, COLOR_WHITE, COLOR_RED);
1271 global_attr_error = COLOR_PAIR(3);
1275 current_focus_line = 0;
1276 displayed_focus_line = 0;
1277 cursor_position = strlen(pattern);
1279 update_screen(¤t_focus_line, &displayed_focus_line,
1281 nb_lines, labels, cursor_position, pattern);
1288 if(key >= ' ' && key <= '~') { /* Insert character */
1289 insert_char(pattern, &cursor_position, key);
1292 else if(key == KEY_BACKSPACE ||
1293 key == '\010' || /* ^H */
1294 key == '\177') { /* ^? */
1295 backspace_char(pattern, &cursor_position);
1298 else if(key == KEY_DC ||
1299 key == '\004') { /* ^D */
1300 delete_char(pattern, &cursor_position);
1303 else if(key == KEY_HOME) {
1304 current_focus_line = 0;
1307 else if(key == KEY_END) {
1308 current_focus_line = nb_lines - 1;
1311 else if(key == KEY_NPAGE) {
1315 else if(key == KEY_PPAGE) {
1319 else if(key == KEY_DOWN ||
1320 key == '\016') { /* ^N */
1324 else if(key == KEY_UP ||
1325 key == '\020') { /* ^P */
1329 else if(key == KEY_LEFT ||
1330 key == '\002') { /* ^B */
1331 if(cursor_position > 0) cursor_position--;
1332 else error_feedback();
1335 else if(key == KEY_RIGHT ||
1336 key == '\006') { /* ^F */
1337 if(pattern[cursor_position]) cursor_position++;
1338 else error_feedback();
1341 else if(key == '\001') { /* ^A */
1342 cursor_position = 0;
1345 else if(key == '\005') { /* ^E */
1346 cursor_position = strlen(pattern);
1349 else if(key == '\022') { /* ^R */
1350 global_use_regexp = !global_use_regexp;
1353 else if(key == '\011') { /* ^I */
1354 global_case_sensitive = !global_case_sensitive;
1357 else if(key == '\025') { /* ^U */
1358 kill_before_cursor(pattern, &cursor_position);
1361 else if(key == '\013') { /* ^K */
1362 kill_after_cursor(pattern, &cursor_position);
1365 else if(key == '\014') { /* ^L */
1366 /* I suspect that we may sometime mess up the display, so ^L is
1367 here to force a full refresh */
1371 else if(key == '\007' || /* ^G */
1372 key == '\033' || /* ^[ (escape) */
1378 else if(key == KEY_RESIZE || key == -1) {
1379 /* Do nothing when the tty is resized */
1387 update_screen(¤t_focus_line, &displayed_focus_line,
1389 nb_lines, labels, cursor_position, pattern);
1396 /* Here we come back to standard display */
1398 if(key == KEY_ENTER || key == '\n') {
1402 if(displayed_focus_line >= 0 && displayed_focus_line < nb_lines) {
1403 t = lines[displayed_focus_line];
1404 if(global_label_separator) {
1405 while(*t && *t != global_label_separator) t++;
1412 if(global_output_to_vt_buffer && t) {
1413 inject_into_tty_buffer(t, global_add_control_qs);
1416 if(output_filename[0]) {
1417 FILE *out = fopen(output_filename, "w");
1420 fprintf(out, "%s", t);
1425 "selector: Cannot open %s for writing.\n",
1433 printf("Aborted.\n");
1436 for(l = 0; l < nb_lines; l++) {