Added the information from uname into the help.
[selector.git] / selector.c
index 67a0976..746e2f0 100644 (file)
@@ -67,7 +67,7 @@ int attr_modeline, attr_focus_line, attr_error;
 
 /*********************************************************************/
 
-void inject_into_tty_buffer(char *string) {
+void inject_into_tty_buffer(char *string, int add_control_qs) {
   struct termios oldtio, newtio;
   const char *k;
   const char control_q = '\021';
@@ -93,7 +93,7 @@ void check_opt(int argc, char **argv, int n_opt, int n, const char *help) {
   if(n_opt + n >= argc) {
     fprintf(stderr, "Selector: Missing argument for %s, expecting %s.\n",
             argv[n_opt], help);
-    exit(1);
+    exit(EXIT_FAILURE);
   }
 }
 
@@ -114,7 +114,7 @@ int string_to_positive_integer(char *string) {
     fprintf(stderr,
             "Selector: Value `%s' is not a positive integer.\n",
             string);
-    exit(1);
+    exit(EXIT_FAILURE);
   }
 
   return result;
@@ -128,22 +128,24 @@ void error_feedback() {
   }
 }
 
+/*********************************************************************/
+
 /* A quick and dirty hash table */
 
-/* The table itself stores indexes of the strings taken in a char
-   **table. When a string is added, if it was already in the table,
-   **the new index replaces the previous one.  */
+/* The table itself stores indexes of the strings taken in a char**
+   table. When a string is added, if it was already in the table, the
+   new index replaces the previous one.  */
 
-typedef struct {
+struct hash_table_t {
   int size;
   int *entries;
-} hash_table_t;
+};
 
-hash_table_t *new_hash_table(int size) {
+struct hash_table_t *new_hash_table(int size) {
   int k;
-  hash_table_t *hash_table;
+  struct hash_table_t *hash_table;
 
-  hash_table = (hash_table_t *) malloc(sizeof(hash_table_t));
+  hash_table = (struct hash_table_t *) malloc(sizeof(struct hash_table_t));
 
   hash_table->size = size;
   hash_table->entries = (int *) malloc(hash_table->size * sizeof(int));
@@ -155,7 +157,7 @@ hash_table_t *new_hash_table(int size) {
   return hash_table;
 }
 
-void free_hash_table(hash_table_t *hash_table) {
+void free_hash_table(struct hash_table_t *hash_table) {
   free(hash_table->entries);
   free(hash_table);
 }
@@ -164,7 +166,7 @@ void free_hash_table(hash_table_t *hash_table) {
    string was not already in the table, returns -1. Otherwise, returns
    the previous index it had. */
 
-int add_and_get_previous_index(hash_table_t *hash_table,
+int add_and_get_previous_index(struct hash_table_t *hash_table,
                                const char *new_string, int new_index,
                                char **strings) {
 
@@ -615,7 +617,7 @@ void update_screen(int *current_focus_line, int *displayed_focus_line,
 
 /*********************************************************************/
 
-void store_line(hash_table_t *hash_table,
+void store_line(struct hash_table_t *hash_table,
                 const char *new_line,
                 int *nb_lines, char **lines) {
   int dup;
@@ -658,7 +660,7 @@ void store_line(hash_table_t *hash_table,
   (*nb_lines)++;
 }
 
-void read_file(hash_table_t *hash_table,
+void read_file(struct hash_table_t *hash_table,
                const char *input_filename,
                int nb_lines_max, int *nb_lines, char **lines) {
 
@@ -670,7 +672,7 @@ void read_file(hash_table_t *hash_table,
 
   if(!file) {
     fprintf(stderr, "Selector: Can not open `%s'.\n", input_filename);
-    exit(1);
+    exit(EXIT_FAILURE);
   }
 
   start = 0;
@@ -706,7 +708,7 @@ void read_file(hash_table_t *hash_table,
               BUFFER_SIZE);
       fprintf(stderr, raw_line);
       fprintf(stderr, "\n");
-      exit(1);
+      exit(EXIT_FAILURE);
     }
 
     /* If we got a line, we replace the carriage return by a \0 to
@@ -741,11 +743,11 @@ int main(int argc, char **argv) {
 
   char **lines, **labels;
   int nb_lines;
-  hash_table_t *hash_table;
+  struct hash_table_t *hash_table;
 
-  if(!ttyname(STDIN_FILENO)) {
+  if(!isatty(STDIN_FILENO)) {
     fprintf(stderr, "Selector: The standard input is not a tty.\n");
-    exit(1);
+    exit(EXIT_FAILURE);
   }
 
   color_fg_modeline  = COLOR_WHITE;
@@ -761,8 +763,8 @@ int main(int argc, char **argv) {
   i = 1;
 
   while(!error && !show_help &&
-        i < argc && argv[i][0] == '-' &&
-        !rest_are_files) {
+        i < argc &&
+        argv[i][0] == '-' && !rest_are_files) {
 
     if(strcmp(argv[i], "-o") == 0) {
       check_opt(argc, argv, i, 1, "<output filename>");
@@ -886,7 +888,7 @@ int main(int argc, char **argv) {
       out = stderr;
     }
 
-    fprintf(out, "Selector version %s-R%s\n", VERSION, REVISION_NUMBER);
+    fprintf(out, "Selector version %s-R%s (%s)\n", VERSION, REVISION_NUMBER, UNAME);
     fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
     fprintf(out, "\n");
     fprintf(out, "Usage: %s [options] [<filename1> [<filename2> ...]]\n",
@@ -1002,7 +1004,6 @@ int main(int argc, char **argv) {
   initscr();
   cbreak();
   noecho();
-  /* nonl(); */
   intrflush(stdscr, FALSE);
 
   /* So that the arrow keys work */
@@ -1024,7 +1025,7 @@ int main(int argc, char **argv) {
       endwin();
       fprintf(stderr, "Selector: Color numbers have to be between 0 and %d.\n",
               COLORS - 1);
-      exit(1);
+      exit(EXIT_FAILURE);
     }
 
     init_pair(1, color_fg_modeline, color_bg_modeline);
@@ -1128,7 +1129,8 @@ int main(int argc, char **argv) {
     }
 
     else if(key == '\014') { /* ^L */
-      /* I suspect that we may sometime mess up the display */
+      /* I suspect that we may sometime mess up the display, so ^L is
+         here to force a full refresh */
       clear();
     }
 
@@ -1161,7 +1163,7 @@ int main(int argc, char **argv) {
     }
 
     if(output_to_vt_buffer && t) {
-      inject_into_tty_buffer(t);
+      inject_into_tty_buffer(t, add_control_qs);
     }
 
     if(output_filename[0]) {
@@ -1175,7 +1177,7 @@ int main(int argc, char **argv) {
         fprintf(stderr,
                 "Selector: Can not open %s for writing.\n",
                 output_filename);
-        exit(1);
+        exit(EXIT_FAILURE);
       }
       fclose(out);
     }
@@ -1193,5 +1195,5 @@ int main(int argc, char **argv) {
   free(lines);
   free(title);
 
-  exit(0);
+  exit(EXIT_SUCCESS);
 }