*** empty log message ***
[selector.git] / selector.cc
index 42a54d6..0865015 100644 (file)
@@ -1,9 +1,10 @@
+
 /*
  *  selector is a simple shell command for selection of strings with a
  *  dynamic pattern-matching.
  *
  *  Copyright (c) 2009 Francois Fleuret
- *  Written by Francois Fleuret <francois.fleuret@idiap.ch>
+ *  Written by Francois Fleuret <francois@fleuret.org>
  *
  *  This file is part of selector.
  *
  *
  */
 
-// Here is how to use it as a super-history-search in bash
-//
-//  ./selector -v -f ${HISTFILE}
+// Here is how to use it as a super-history-search
+// ./selector -v -f ${HISTFILE}
+
+// This software is highly Linux-specific, but I would be glad to get
+// patches to make it work on other OS
 
 #include <fstream>
 #include <iostream>
@@ -288,7 +291,7 @@ int main(int argc, char **argv) {
   int no_blink = 0;
 
   char input_filename[buffer_size], output_filename[buffer_size];
-  strcpy(input_filename, "/dev/stdin");
+  strcpy(input_filename, "");
   strcpy(output_filename, "/tmp/selector.out");
 
   int i = 1;
@@ -328,7 +331,15 @@ int main(int argc, char **argv) {
     }
 
     else {
-      cerr << argv[0] << " [-h] [-o <output filename>] [-b] [-l <max number of lines>] [-s <pattern separator>] [-v]" << endl;
+      cerr << argv[0]
+           << " [-h]"
+           << " [-b]"
+           << " [-v]"
+           << " [-o <output filename>]"
+           << " [-s <pattern separator>]"
+           << " [-l <max number of lines>]"
+           << " -f <input filename>"
+           << endl;
       if(strcmp(argv[i], "-h") == 0) {
         exit(0);
       } else {
@@ -337,6 +348,11 @@ int main(int argc, char **argv) {
     }
   }
 
+  if(!input_filename[0]) {
+    cerr << "You must specify a input file with -f." << endl;
+    exit(1);
+  }
+
   ifstream file(input_filename);
 
   if(file.fail()) {
@@ -429,8 +445,8 @@ int main(int argc, char **argv) {
   curs_set(1);
   endwin();
 
-  if(output_to_vt_buffer) {
-    if((key == KEY_ENTER || key == '\n') && temporary_line >= 0 && temporary_line < nb_lines) {
+  if((key == KEY_ENTER || key == '\n') && temporary_line >= 0 && temporary_line < nb_lines) {
+    if(output_to_vt_buffer) {
       char *tty = ttyname (STDIN_FILENO);
       int fd = open(tty, O_WRONLY);
 
@@ -440,13 +456,10 @@ int main(int argc, char **argv) {
         // Save current port settings
         tcgetattr(fd,&oldtio);
         bzero(&newtio, sizeof(newtio));
-        newtio.c_cflag = 0;
-        newtio.c_iflag = 0;
-        newtio.c_oflag = 0;
         // Set input mode (non-canonical, *no echo*,...)
-        newtio.c_lflag = 0;
         tcflush(fd, TCIFLUSH);
         tcsetattr(fd,TCSANOW, &newtio);
+        // Put the selected line in the tty input buffer
         for(char *k = lines[temporary_line]; *k; k++) {
           ioctl(fd, TIOCSTI, k);
         }
@@ -454,21 +467,16 @@ int main(int argc, char **argv) {
         tcsetattr(fd,TCSANOW, &oldtio);
         close(fd);
       } else {
-        cerr << "Can not open " << tty << endl;
+        cerr << "Can not open " << tty << "." << endl;
         exit(1);
       }
-    }
-  } else {
-
-    ofstream out(output_filename);
-    if(out.fail()) {
-      cerr << "Can not open " << output_filename << " for writing." << endl;
-      exit(1);
     } else {
-      if((key == KEY_ENTER || key == '\n') && temporary_line >= 0 && temporary_line < nb_lines) {
-        out << lines[temporary_line] << endl;
+      ofstream out(output_filename);
+      if(out.fail()) {
+        cerr << "Can not open " << output_filename << " for writing." << endl;
+        exit(1);
       } else {
-        out << endl;
+        out << lines[temporary_line] << endl;
       }
       out.flush();
     }