X-Git-Url: https://www.fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=blobdiff_plain;f=selector.c;h=0cb82ea6aa21f861ec23b34c14b9182facfe9d08;hb=af6347e4968f1b68bd2a5cdfaf47ef6ecb156ff3;hp=81286d6b8f4307f32965095d707fb5e710b2716f;hpb=98ebced475975d1dd0cb733d11e2413e8bb54c54;p=selector.git diff --git a/selector.c b/selector.c index 81286d6..0cb82ea 100644 --- a/selector.c +++ b/selector.c @@ -3,7 +3,7 @@ * selector is a simple command line utility for selection of strings * with a dynamic pattern-matching. * - * Copyright (c) 2009, 2010, 2011 Francois Fleuret + * Copyright (c) 2009, 2010, 2011, 2012 Francois Fleuret * Written by Francois Fleuret * * This file is part of selector. @@ -45,7 +45,7 @@ #include #include -#define VERSION "1.1.5" +#define VERSION "1.1.6" #define BUFFER_SIZE 4096 @@ -320,8 +320,9 @@ struct matcher { }; /* Routine to add an interval to a sorted list of intervals - extermities. Returns the number of extremities. This is an effing - nightmare */ + extremities. Returns the resulting number of extremities. + + This routine is an effing nightmare */ int add_interval(int n, int *switches, int start, int end) { int f, g, k; @@ -410,38 +411,22 @@ int match(struct matcher *matcher, char *string, int *nb_switches, int *switches *nb_switches = add_interval(*nb_switches, switches, (int) (where - string), (int) (where - string) + strlen(matcher->patterns[n])); - #warning CHECK THE INTERVALS - { - int k, i; - FILE *out = fopen("/tmp/intervals", "w"); - for(k = 0; k < (*nb_switches)/2; k++) { - i = 0; - for(; i < switches[2 * k]; i++) fprintf(out, "-"); - for(; i < switches[2 * k + 1]; i++) fprintf(out, "%c", string[i]); - for(; i < strlen(string); i++) fprintf(out, "-"); - fprintf(out, "\n"); - } - fclose(out); - for(k = 0; k < *nb_switches - 1; k++) { - if(switches[k] > switches[k+1]) { - abort(); - } - } - } } } } return 1; } else { - if(regexec(&matcher->preg, string, 1, &matches, 0) == 0) { - if(switches) { + if(switches) { + if(regexec(&matcher->preg, string, 1, &matches, 0) == 0) { *nb_switches = 2; switches[0] = matches.rm_so; switches[1] = matches.rm_eo; + return 1; + } else { + return 0; } - return 1; } else { - return 0; + return regexec(&matcher->preg, string, 0, 0, 0) == 0; } } } @@ -640,10 +625,14 @@ void update_screen(int *current_focus_line, int *displayed_focus_line, initialize_matcher(&matcher, use_regexp, case_sensitive, pattern); - if(show_hits && matcher.nb_patterns >= 0) { - switches = safe_malloc(sizeof(int) * matcher.nb_patterns * 2); + if(show_hits) { + if(matcher.nb_patterns >= 0) { + switches = safe_malloc(sizeof(int) * matcher.nb_patterns * 2); + } else { + switches = safe_malloc(sizeof(int) * 2); + } } else { - switches = safe_malloc(sizeof(int) * 2); + switches = 0; } console_width = getmaxx(stdscr); @@ -974,6 +963,7 @@ static struct option long_options[] = { { "regexp", no_argument, 0, 'e' }, { "case-sensitive", no_argument, 0, 'a' }, { "show-long-lines", no_argument, 0, 'j'}, + { "show-hits", no_argument, 0, 'j'}, { "upper-case-makes-case-sensitive", no_argument, 0, 'u' }, { "title", 1, 0, 't' }, { "number-of-lines", 1, 0, 'l' },