#include <iostream>
#include <fstream>
#include <string.h>
+#include <stdlib.h>
using namespace std;
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);
}
}
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) {
}
}
- 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;
}
}
- check(nb_printed_lines != nb_match, "ouch3");
-
if(motion != 0) {
*current_line = new_line;
}
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, "<output filename>");
+ strncpy(output_filename, argv[i+1], buffer_size);
+ i += 2;
+ }
+
+ 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;
+ }
+
+ else if(strcmp(argv[i], "-b") == 0) {
+ noblink = 1;
+ i++;
+ }
+
+ else if(strcmp(argv[i], "-l") == 0) {
+ check_opt(argc, argv, i, 1, "<maximum number of lines>");
+ nb_lines_max = atoi(argv[i+1]);
+ i += 2;
+ }
+
+ else {
+ cerr << argv[0] << " [-o <output filename>] [-b] [-l <max number of lines]" << endl;
+ exit(1);
+ }
}
- ifstream file(file_name);
+ ifstream file(input_filename);
if(file.fail()) {
- cerr << "Can not open \"" << file_name << "\"" << endl;
+ cerr << "Can not open \"" << input_filename << "\"" << endl;
return 1;
}
keypad(stdscr, TRUE);
start_color();
- init_pair(1, COLOR_WHITE, COLOR_BLACK);
+ // init_pair(1, COLOR_WHITE, COLOR_BLACK);
+ init_pair(1, COLOR_WHITE, COLOR_GREEN);
init_pair(2, COLOR_BLACK, COLOR_YELLOW);
int key;
curs_set(1);
endwin();
- ofstream out("/tmp/selector.out");
- if((key == KEY_ENTER || key == '\n') && line >= 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);
}