From fca0fb9a7bfb0c103501aaa8e00f7f8670693774 Mon Sep 17 00:00:00 2001 From: Francois Fleuret Date: Wed, 11 Mar 2009 22:40:26 +0100 Subject: [PATCH] Automatic commit --- selector.cc | 79 ++++++++++++++++++++++++++++++++++------------------ smarthist.sh | 4 +-- 2 files changed, 54 insertions(+), 29 deletions(-) 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); } diff --git a/smarthist.sh b/smarthist.sh index 7b9967f..24b755d 100755 --- a/smarthist.sh +++ b/smarthist.sh @@ -17,11 +17,11 @@ EXE=./selector # We do not want a security breach -OUT=/tmp/selector.out +OUT=`mktemp /tmp/selector.XXXXXX` touch ${OUT} chmod go-rwx ${OUT} -${EXE} ~/.bash_history +${EXE} -o ${OUT} -f ~/.bash_history # We set the echo off -- 2.39.5