X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=blobdiff_plain;f=selector.cc;h=cc25520049171e9f67ecd4e6ffb2e954477034f2;hb=fca0fb9a7bfb0c103501aaa8e00f7f8670693774;hp=50620b69f7a75e9b46f9830801028ccd4edca310;hpb=49a8d9563a444aeaa5e732ef658e8ddd900d4294;p=selector.git diff --git a/selector.cc b/selector.cc index 50620b6..cc25520 100644 --- a/selector.cc +++ b/selector.cc @@ -25,6 +25,7 @@ #include #include #include +#include using namespace std; @@ -35,12 +36,10 @@ int match(char *string, char *regexp) { return strstr(string, regexp) != 0; } -void check(int condition, const char *message) { - if(!condition) { - echo(); - curs_set(1); - endwin(); - cout << message << endl; +void check_opt(int argc, char **argv, int n_opt, int n, const char *help) { + if(n_opt + n >= argc) { + cerr << "Missing argument for " << argv[n_opt] << ". Expecting " << help << "." << endl; + exit(1); } } @@ -113,8 +112,6 @@ void update_screen(int *current_line, int motion, addstr("\n"); - check(new_line < nb_lines, "Ouch!"); - // Here new_line is either a line number matching the regexp, or -1 if(new_line >= 0) { @@ -145,8 +142,6 @@ void update_screen(int *current_line, int motion, } } - check(first_line >= 0 && last_line < nb_lines, "ouch2"); - for(int l = first_line; l <= last_line; l++) { if(match(lines[l], regexp)) { int k = 0; @@ -178,8 +173,6 @@ void update_screen(int *current_line, int motion, } } - check(nb_printed_lines != nb_match, "ouch3"); - if(motion != 0) { *current_line = new_line; } @@ -215,19 +208,45 @@ int main(int argc, char **argv) { char *lines[nb_lines_max]; int noblink = 1; - char *file_name; - char stdin_name[] = "/dev/stdin"; + char input_filename[buffer_size], output_filename[buffer_size]; + strcpy(input_filename, "/dev/stdin"); + strcpy(output_filename, "/tmp/selector.out"); - if(argc == 2 && strcmp(argv[1], "-")) { - file_name = argv[1]; - } else { - file_name = stdin_name; + int i = 1; + while(i < argc) { + if(strcmp(argv[i], "-o") == 0) { + check_opt(argc, argv, i, 1, ""); + strncpy(output_filename, argv[i+1], buffer_size); + i += 2; + } + + else if(strcmp(argv[i], "-f") == 0) { + check_opt(argc, argv, i, 1, ""); + strncpy(input_filename, argv[i+1], buffer_size); + i += 2; + } + + else if(strcmp(argv[i], "-b") == 0) { + noblink = 1; + i++; + } + + else if(strcmp(argv[i], "-l") == 0) { + check_opt(argc, argv, i, 1, ""); + nb_lines_max = atoi(argv[i+1]); + i += 2; + } + + else { + cerr << argv[0] << " [-o ] [-b] [-l = 0 && line < nb_lines) { - out << lines[line] << endl; + ofstream out(output_filename); + if(out.fail()) { + cerr << "Can not open " << output_filename << " for writing." << endl; + exit(1); } else { - out << endl; + if((key == KEY_ENTER || key == '\n') && line >= 0 && line < nb_lines) { + out << lines[line] << endl; + } else { + out << endl; + } + out.flush(); } - out.flush(); for(int l = 0; l < nb_lines; l++) { delete[] lines[l]; } - return 0; + exit(0); }