From: Francois Fleuret Date: Tue, 13 Apr 2010 16:25:25 +0000 (+0200) Subject: Now uses getopt_long to parse the arguments. X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=commitdiff_plain;h=31d6b34af7e6587c8eecabeb44da4de86ec93f3d;p=selector.git Now uses getopt_long to parse the arguments. --- diff --git a/selector.1 b/selector.1 index 6663558..31f3832 100644 --- a/selector.1 +++ b/selector.1 @@ -59,63 +59,64 @@ case-insensitive modes. .SH "OPTIONS" .TP -\fB-h\fR +\fB-h\fR, \fB--help\fR display help and exit .TP -\fB-m\fR +\fB-m\fR, \fB--monochrome\fR force the monochrome mode .TP -\fB-i\fR +\fB-i\fR, \fB--revert-order\fR revert the order of the lines .TP -\fB-b\fR +\fB-b\fR, \fB--remove-bash-prefix\fR remove the numeric prefix from bash history .TP -\fB-z\fR +\fB-z\fR, \fB--remove-zsh-prefix\fR remove the time prefix from zsh history .TP -\fB-d\fR +\fB-d\fR, \fB--remove-duplicates\fR remove duplicated lines (note that you can have two different lines with the same visible part if you use the -x option) .TP -\fB-e\fR +\fB-e\fR, \fB--regexp\fR start in regexp mode .TP -\fB-a\fR +\fB-a\fR, \fB--case-sensitive\fR start in case sensitive mode .TP -\fB-q\fR +\fB-q\fR, \fB--no-beep\fR make a flash instead of a beep when there is an edition error .TP -\fB--\fR +\fB--\fR, \fB--rest-are-files\fR state that all following arguments are filenames .TP -\fB-t \fI\fR +\fB-t \fI<title>\fR, \fB--title \fI<title>\fR add a title in the modeline .TP -\fB-c \fI<fg_modeline> <bg_modeline> <fg_highlight> <bg_highlight>\fR +\fB-c \fI<fg_modeline>,<bg_modeline>,<fg_highlight>,<bg_highlight>\fR, +\fB--colors \fI<fg_modeline>,<bg_modeline>,<fg_highlight>,<bg_highlight>\fR select the modeline and highlight color numbers .TP -\fB-v\fR +\fB-v\fR, \fB--inject-in-tty\fR inject the selected line into the tty input buffer .TP -\fB-w\fR +\fB-w\fR, \fB--add-control-qs\fR add ^Q between characters during tty injection to quote control characters .TP -\fB-o \fI<output_filename>\fR +\fB-o \fI<output_filename>\fR, \fB--output-file \fI<output_filename>\fR write the selected line into the specified file .TP -\fB-s \fI<pattern_separator>\fR +\fB-s \fI<pattern_separator>\fR, \fB--pattern-separator \fI<pattern_separator>\fR specify the symbol to separate the substrings in the search pattern .TP -\fB-x \fI<label_separator>\fR +\fB-x \fI<label_separator>\fR, \fB--label-separator \fI<label_separator>\fR specify the symbol to separate what to show to the user during the selection from the line to return .TP -\fB-l \fI<max_number_of_lines>\fR +\fB-l \fI<max_number_of_lines>\fR, \fB--number-of-lines \fI<max_number_of_lines>\fR specify the maximum number of lines to take into account .TP -\fB-f \fI<input_filename>\fR +\fB-f \fI<input_filename>\fR, \fB--input-file \fI<input_filename>\fR specify a file to search into (option kept for compatibility reasons) .SH "EXAMPLES" diff --git a/selector.c b/selector.c index 5c9d85f..5054f6b 100644 --- a/selector.c +++ b/selector.c @@ -42,6 +42,7 @@ #include <termios.h> #include <regex.h> #include <locale.h> +#include <getopt.h> #define VERSION "1.0.1" @@ -116,8 +117,9 @@ int string_to_positive_integer(char *string) { int result = 0; char *s; + printf("string_to_positive_integer string=\"%s\"\n", string); if(*string) { - for(s = string; *s; s++) { + for(s = string; *s && *s != ','; s++) { if(*s >= '0' && *s <= '9') { result = result * 10 + (int) (*s - '0'); } else error = 1; @@ -775,11 +777,35 @@ void read_file(struct hash_table_t *hash_table, /*********************************************************************/ +static struct option long_options[] = { + { "output-file", 1, 0, 'o' }, + { "separator-patterns", 1, 0, 's' }, + { "separator-display", 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' }, + { "input-file", 1, 0, 'f' }, + { "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 c, *s; char pattern[BUFFER_SIZE]; - int i, k, l, n; + int k, l, n; int cursor_position; int error = 0, show_help = 0; int rest_are_files = 0; @@ -808,123 +834,100 @@ int main(int argc, char **argv) { 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, "<output filename>"); - 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>"); - 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, "<label separator>"); - label_separator = argv[i+1][0]; - i += 2; - } + case 'x': + label_separator = optarg[0]; + break; - else if(strcmp(argv[i], "-v") == 0) { + case 'v': output_to_vt_buffer = 1; - i++; - } + break; - else if(strcmp(argv[i], "-w") == 0) { + case 'w': add_control_qs = 1; - i++; - } + break; - else if(strcmp(argv[i], "-m") == 0) { + case 'm': with_colors = 0; - i++; - } + break; - else if(strcmp(argv[i], "-q") == 0) { + case 'q': error_flash = 1; - i++; - } + break; - else if(strcmp(argv[i], "-f") == 0) { - check_opt(argc, argv, i, 1, "<input filename>"); - strncpy(input_filename, argv[i+1], BUFFER_SIZE); - i += 2; - } + case 'f': + strncpy(input_filename, optarg, BUFFER_SIZE); + break; - else if(strcmp(argv[i], "-i") == 0) { + case 'i': inverse_order = 1; - i++; - } + break; - else if(strcmp(argv[i], "-b") == 0) { + case 'b': bash_history = 1; - i++; - } + break; - else if(strcmp(argv[i], "-z") == 0) { + case 'z': zsh_history = 1; - i++; - } + break; - else if(strcmp(argv[i], "-d") == 0) { + case 'd': remove_duplicates = 1; - i++; - } + break; - else if(strcmp(argv[i], "-e") == 0) { + case 'e': use_regexp = 1; - i++; - } + break; - else if(strcmp(argv[i], "-a") == 0) { + case 'a': case_sensitive = 1; - i++; - } + break; - else if(strcmp(argv[i], "-t") == 0) { - check_opt(argc, argv, i, 1, "<title>"); + case 't': free(title); - title = safe_malloc((strlen(argv[i+1]) + 1) * sizeof(char)); - strcpy(title, argv[i+1]); - i += 2; - } - - else if(strcmp(argv[i], "-l") == 0) { - check_opt(argc, argv, i, 1, "<maximum number of lines>"); - nb_lines_max = string_to_positive_integer(argv[i+1]); - i += 2; - } - - else if(strcmp(argv[i], "-c") == 0) { - check_opt(argc, argv, i, 4, - "<fg modeline> <bg modeline> <fg highlight> <bg highlight>"); - color_fg_modeline = string_to_positive_integer(argv[i + 1]); - color_bg_modeline = string_to_positive_integer(argv[i + 2]); - color_fg_highlight = string_to_positive_integer(argv[i + 3]); - color_bg_highlight = string_to_positive_integer(argv[i + 4]); - i += 5; - } - - else if(strcmp(argv[i], "--") == 0) { + title = safe_malloc((strlen(optarg) + 1) * sizeof(char)); + strcpy(title, optarg); + break; + + case 'l': + nb_lines_max = string_to_positive_integer(optarg); + 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; - i++; - } + break; - else if(strcmp(argv[i], "-h") == 0) { + case 'h': show_help = 1; - i++; - } + break; - else { - fprintf(stderr, "Selector: Unknown option %s.\n", argv[i]); + default: error = 1; + break; } } @@ -954,11 +957,11 @@ int main(int argc, char **argv) { nb_lines_max, &nb_lines, lines); } - while(i < argc) { + while(optind < argc) { read_file(hash_table, - argv[i], + argv[optind], nb_lines_max, &nb_lines, lines); - i++; + optind++; } if(hash_table) { @@ -977,10 +980,10 @@ int main(int argc, char **argv) { nb_lines = n; if(inverse_order) { - for(i = 0; i < nb_lines / 2; i++) { - char *s = lines[nb_lines - 1 - i]; - lines[nb_lines - 1 - i] = lines[i]; - lines[i] = s; + for(l = 0; l < nb_lines / 2; l++) { + char *s = lines[nb_lines - 1 - l]; + lines[nb_lines - 1 - l] = lines[l]; + lines[l] = s; } }