X-Git-Url: https://www.fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=blobdiff_plain;f=selector.c;h=38c62acb66ae342c259f1ed9deb7538d413e2f65;hb=e9b84f8dd42f4c22744c0b061b876dc6eaf23722;hp=e6b49aa103d2068bdb66c9debb81d28c8637d60a;hpb=5ca5782212c0371f36f1ebbb397208e5eee2110a;p=selector.git diff --git a/selector.c b/selector.c index e6b49aa..38c62ac 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 Francois Fleuret + * Copyright (c) 2009, 2010 Francois Fleuret * Written by Francois Fleuret * * This file is part of selector. @@ -42,8 +42,9 @@ #include #include #include +#include -#define VERSION "1.0" +#define VERSION "1.1.1" #define BUFFER_SIZE 4096 @@ -103,35 +104,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++) { - 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() { @@ -142,9 +160,9 @@ void error_feedback() { } } -void print_help(FILE *out) { +void usage(FILE *out) { - fprintf(out, "Selector version %s-R%s (%s)\n", VERSION, REVISION_NUMBER, UNAME); + fprintf(out, "Selector version %s (%s)\n", VERSION, UNAME); fprintf(out, "Written by Francois Fleuret .\n"); fprintf(out, "\n"); fprintf(out, "Usage: selector [options] [ [ ...]]\n"); @@ -504,8 +522,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 && @@ -775,17 +793,40 @@ void read_file(struct hash_table_t *hash_table, /*********************************************************************/ +static struct option long_options[] = { + { "output-file", 1, 0, 'o' }, + { "pattern-separator", 1, 0, 's' }, + { "label-separator", 1, 0, 'x' }, + { "inject-in-tty", no_argument, 0, 'v' }, + { "add-control-qs", no_argument, 0, 'w' }, + { "monochrome", no_argument, 0, 'm' }, + { "no-beep", no_argument, 0, 'q' }, + { "revert-order", no_argument, 0, 'i' }, + { "remove-bash-prefix", no_argument, 0, 'b' }, + { "remove-zsh-prefix", no_argument, 0, 'z' }, + { "remove-duplicates", no_argument, 0, 'd' }, + { "regexp", no_argument, 0, 'e' }, + { "case-sensitive", no_argument, 0, 'a' }, + { "title", 1, 0, 't' }, + { "number-of-lines", 1, 0, 'l' }, + { "colors", 1, 0, 'c' }, + { "rest-are-files", no_argument, 0, '-' }, + { "help", no_argument, 0, 'h' }, + { 0, 0, 0, 0 } +}; + int main(int argc, char **argv) { - char input_filename[BUFFER_SIZE], output_filename[BUFFER_SIZE]; + char output_filename[BUFFER_SIZE]; char pattern[BUFFER_SIZE]; - int i, k, l, n; + int c, k, l, n; int cursor_position; int error = 0, show_help = 0; int rest_are_files = 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; @@ -805,135 +846,104 @@ int main(int argc, char **argv) { setlocale(LC_ALL, ""); - strcpy(input_filename, ""); strcpy(output_filename, ""); - i = 1; + while (!rest_are_files && + (c = getopt_long(argc, argv, "o:s:x:vwmqf:ibzdeat:l:c:-h", + long_options, NULL)) != -1) { - while(!error && !show_help && - i < argc && - argv[i][0] == '-' && !rest_are_files) { + switch(c) { - if(strcmp(argv[i], "-o") == 0) { - check_opt(argc, argv, i, 1, ""); - strncpy(output_filename, argv[i+1], BUFFER_SIZE); - i += 2; - } + case 'o': + strncpy(output_filename, optarg, BUFFER_SIZE); + break; - else if(strcmp(argv[i], "-s") == 0) { - check_opt(argc, argv, i, 1, ""); - pattern_separator = argv[i+1][0]; - i += 2; - } + case 's': + pattern_separator = optarg[0]; + break; - else if(strcmp(argv[i], "-x") == 0) { - check_opt(argc, argv, i, 1, "