sched: Kill old ->post_select variant.
[paraslash.git] / interactive.c
index f9ea3612ee8a1b0e80bef508051ed737b9a0ba05..cfc1d60663b28e615f31d3c70e6a64e906bdf650 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2011-2012 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2011-2013 Andre Noll <maan@systemlinux.org>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
 struct i9e_private {
        struct i9e_client_info *ici;
        FILE *stderr_stream;
+       int num_columns;
        char empty_line[1000];
        struct task task;
        struct btr_node *stdout_btrn;
+       bool last_write_was_status;
        bool line_handler_running;
        bool input_eof;
        bool caught_sigint;
+       bool caught_sigterm;
+       Keymap standard_km;
+       Keymap bare_km;
 };
 static struct i9e_private i9e_private, *i9ep = &i9e_private;
 
+/**
+ * Return the error state of the i9e task.
+ *
+ * This is mainly useful for other tasks to tell whether the i9e task is still
+ * running.
+ *
+ * \return A negative return value of zero means the i9e task terminated. Only
+ * in this case it is safe to call ie9_close().
+ */
+int i9e_get_error(void)
+{
+       return i9ep->task.error;
+}
+
 static bool is_prefix(const char *partial, const char *full, size_t len)
 {
        if (len == 0)
@@ -178,11 +197,47 @@ static char **i9e_completer(const char *text, int start, __a_unused int end)
  */
 void i9e_attach_to_stdout(struct btr_node *producer)
 {
-       assert(!i9ep->stdout_btrn);
+       btr_remove_node(&i9ep->stdout_btrn);
        i9ep->stdout_btrn = btr_new_node(&(struct btr_node_description)
                EMBRACE(.name = "interactive_stdout", .parent = producer));
+       rl_set_keymap(i9ep->bare_km);
+}
+
+static void wipe_bottom_line(void)
+{
+       char x[] = "          ";
+       int n = i9ep->num_columns;
+
+       /*
+        * For reasons beyond my understanding, writing more than 68 characters
+        * here causes MacOS to mess up the terminal. Writing a line of spaces
+        * in smaller chunks works fine though. Weird.
+        */
+       fprintf(i9ep->stderr_stream, "\r");
+       while (n > 0) {
+               if (n >= sizeof(x)) {
+                       fprintf(i9ep->stderr_stream, "%s", x);
+                       n -= sizeof(x);
+                       continue;
+               }
+               x[n] = '\0';
+               fprintf(i9ep->stderr_stream, "%s", x);
+               break;
+       }
+       fprintf(i9ep->stderr_stream, "\r");
 }
 
+/**
+ * Free all storage associated with a keymap.
+ *
+ * This function is not declared in the readline headers although the symbol is
+ * exported and the function is documented in the readline info file. So we
+ * have to declare it here.
+ *
+ * \param keymap The keymap to deallocate.
+ */
+void rl_free_keymap(Keymap keymap);
+
 /**
  * Reset the terminal and save the in-memory command line history.
  *
@@ -192,16 +247,11 @@ void i9e_close(void)
 {
        char *hf = i9ep->ici->history_file;
 
-       rl_deprep_terminal();
-       fprintf(i9ep->stderr_stream, "\n");
+       rl_free_keymap(i9ep->bare_km);
+       rl_callback_handler_remove();
        if (hf)
                write_history(hf);
-       fclose(i9ep->stderr_stream);
-}
-
-static void wipe_bottom_line(void)
-{
-       fprintf(i9ep->stderr_stream, "\r%s\r", i9ep->empty_line);
+       wipe_bottom_line();
 }
 
 static void clear_bottom_line(void)
@@ -241,9 +291,7 @@ static void i9e_line_handler(char *line)
 {
        int ret;
 
-       i9ep->line_handler_running = true;
        ret = i9ep->ici->line_handler(line);
-       i9ep->line_handler_running = false;
        if (ret < 0)
                PARA_WARNING_LOG("%s\n", para_strerror(-ret));
        rl_set_prompt("");
@@ -259,59 +307,68 @@ static void i9e_line_handler(char *line)
        }
 }
 
-static void i9e_input(void)
-{
-       do {
-               rl_callback_read_char();
-       } while (input_available());
-}
-
-static void i9e_post_select(struct sched *s, struct task *t)
+static int i9e_post_select(__a_unused struct sched *s, __a_unused struct task *t)
 {
        int ret;
-       struct btr_node *btrn = i9ep->stdout_btrn;
        struct i9e_client_info *ici = i9ep->ici;
        char *buf;
-       size_t sz;
+       size_t sz, consumed = 0;
 
-       if (i9ep->input_eof) {
-               t->error = -E_I9E_EOF;
-               return;
-       }
-       if (!btrn) {
-               i9ep->caught_sigint = false;
-               if (FD_ISSET(ici->fds[0], &s->rfds))
-                       i9e_input();
-               return;
-       }
+       ret = -E_I9E_EOF;
+       if (i9ep->input_eof)
+               goto rm_btrn;
+       ret = -E_I9E_TERM_RQ;
+       if (i9ep->caught_sigterm)
+               goto rm_btrn;
+       ret = 0;
        if (i9ep->caught_sigint)
                goto rm_btrn;
+       while (input_available())
+               rl_callback_read_char();
+       if (!i9ep->stdout_btrn)
+               goto out;
        ret = btr_node_status(i9ep->stdout_btrn, 0, BTR_NT_LEAF);
-       if (ret < 0)
+       if (ret < 0) {
+               ret = 0;
                goto rm_btrn;
-       sz = btr_next_buffer(btrn, &buf);
+       }
+       if (ret == 0)
+               goto out;
+again:
+       sz = btr_next_buffer(i9ep->stdout_btrn, &buf);
        if (sz == 0)
                goto out;
+       if (i9ep->last_write_was_status)
+               fprintf(i9ep->stderr_stream, "\n");
+       i9ep->last_write_was_status = false;
        ret = xwrite(ici->fds[1], buf, sz);
        if (ret < 0)
                goto rm_btrn;
-       btr_consume(btrn, ret);
+       btr_consume(i9ep->stdout_btrn, ret);
+       consumed += ret;
+       if (ret == sz && consumed < 10000)
+               goto again;
        goto out;
 rm_btrn:
-       btr_remove_node(btrn);
-       btr_free_node(btrn);
-       i9ep->stdout_btrn = NULL;
-       rl_set_prompt(i9ep->ici->prompt);
-       rl_forced_update_display();
+       if (i9ep->stdout_btrn) {
+               wipe_bottom_line();
+               btr_remove_node(&i9ep->stdout_btrn);
+               rl_set_keymap(i9ep->standard_km);
+               rl_set_prompt(i9ep->ici->prompt);
+               rl_redisplay();
+       }
+       if (ret < 0)
+               wipe_bottom_line();
 out:
-       t->error = 0;
+       i9ep->caught_sigint = false;
+       return ret;
 }
 
 static void i9e_pre_select(struct sched *s, __a_unused struct task *t)
 {
        int ret;
 
-       if (i9ep->input_eof || i9ep->caught_sigint) {
+       if (i9ep->input_eof || i9ep->caught_sigint || i9ep->caught_sigterm) {
                sched_min_delay(s);
                return;
        }
@@ -340,14 +397,30 @@ static void update_winsize(void)
 {
        struct winsize w;
        int ret = ioctl(i9ep->ici->fds[2], TIOCGWINSZ, (char *)&w);
-       int num_columns = 80;
 
        if (ret >= 0) {
                assert(w.ws_col < sizeof(i9ep->empty_line));
-               num_columns = w.ws_col;
-       }
-       memset(i9ep->empty_line, ' ', num_columns);
-       i9ep->empty_line[num_columns] = '\0';
+               i9ep->num_columns = w.ws_col;
+       } else
+               i9ep->num_columns = 80;
+
+       memset(i9ep->empty_line, ' ', i9ep->num_columns);
+       i9ep->empty_line[i9ep->num_columns] = '\0';
+}
+
+/**
+ * Defined key sequences are mapped to keys starting with this offset. I.e.
+ * pressing the first defined key sequence yields the key number \p KEY_OFFSET.
+ */
+#define KEY_OFFSET 64
+
+static int dispatch_key(__a_unused int count, int key)
+{
+       int ret;
+
+       assert(key >= KEY_OFFSET);
+       ret = i9ep->ici->key_handler(key - KEY_OFFSET);
+       return ret < 0? ret : 0;
 }
 
 /**
@@ -374,7 +447,7 @@ int i9e_open(struct i9e_client_info *ici, struct sched *s)
        if (ret < 0)
                return ret;
        i9ep->task.pre_select = i9e_pre_select;
-       i9ep->task.post_select = i9e_post_select;
+       i9ep->task.new_post_select = i9e_post_select;
        sprintf(i9ep->task.status, "i9e");
        register_task(s, &i9ep->task);
        rl_readline_name = "para_i9e";
@@ -384,16 +457,34 @@ int i9e_open(struct i9e_client_info *ici, struct sched *s)
        i9ep->stderr_stream = fdopen(ici->fds[2], "w");
        setvbuf(i9ep->stderr_stream, NULL, _IONBF, 0);
 
+       i9ep->standard_km = rl_get_keymap();
+       i9ep->bare_km = rl_make_bare_keymap();
+       if (ici->bound_keyseqs) {
+               char *seq;
+               int i;
+               /* FIXME: This is an arbitrary constant.  */
+               for (i = 0; i < 32 && (seq = ici->bound_keyseqs[i]); i++) {
+                       char buf[2] = {KEY_OFFSET + i, '\0'};
+                       /* readline needs an allocated buffer for the macro */
+                       rl_generic_bind(ISMACR, seq, para_strdup(buf), i9ep->bare_km);
+                       rl_bind_key_in_map(KEY_OFFSET + i, dispatch_key, i9ep->bare_km);
+               }
+       }
        if (ici->history_file)
                read_history(ici->history_file);
        update_winsize();
-       rl_callback_handler_install(i9ep->ici->prompt, i9e_line_handler);
+       if (ici->producer) {
+               rl_callback_handler_install("", i9e_line_handler);
+               i9e_attach_to_stdout(ici->producer);
+               rl_set_keymap(i9ep->bare_km);
+       } else
+               rl_callback_handler_install(i9ep->ici->prompt, i9e_line_handler);
        return 1;
 }
 
 static void reset_line_state(void)
 {
-       if (i9ep->line_handler_running)
+       if (i9ep->stdout_btrn)
                return;
        rl_on_new_line();
        rl_reset_line_state();
@@ -416,12 +507,46 @@ __printf_2_3 void i9e_log(int ll, const char* fmt,...)
 
        if (ll < i9ep->ici->loglevel)
                return;
-       if (i9ep->line_handler_running == false)
-               clear_bottom_line();
+       clear_bottom_line();
        va_start(argp, fmt);
        vfprintf(i9ep->stderr_stream, fmt, argp);
        va_end(argp);
        reset_line_state();
+       i9ep->last_write_was_status = false;
+}
+
+/**
+ * Print the current status to stderr.
+ *
+ * \param buf The text to print.
+ * \param len The number of bytes in \a buf.
+ *
+ * This clears the bottom line, moves to the beginning of the line and prints
+ * the given text. If the length of this text exceeds the width of the
+ * terminal, the text is shortened by leaving out a part in the middle.
+ */
+void ie9_print_status_bar(char *buf, unsigned len)
+{
+       size_t x = i9ep->num_columns, y = (x - 4) / 2;
+
+       assert(x >= 6);
+       if (len > x) {
+               buf[y] = '\0';
+               fprintf(i9ep->stderr_stream, "\r%s", buf);
+               fprintf(i9ep->stderr_stream, " .. ");
+               fprintf(i9ep->stderr_stream, "%s", buf + len - y);
+       } else {
+               char scratch[1000];
+
+               y = x - len;
+               scratch[0] = '\r';
+               strcpy(scratch + 1, buf);
+               memset(scratch + 1 + len, ' ', y);
+               scratch[1 + len + y] = '\r';
+               scratch[2 + len + y] = '\0';
+               fprintf(i9ep->stderr_stream, "\r%s", scratch);
+       }
+       i9ep->last_write_was_status = true;
 }
 
 /**
@@ -433,12 +558,16 @@ __printf_2_3 void i9e_log(int ll, const char* fmt,...)
  */
 void i9e_signal_dispatch(int sig_num)
 {
+       if (sig_num == SIGWINCH)
+               return update_winsize();
        if (sig_num == SIGINT) {
                fprintf(i9ep->stderr_stream, "\n");
                rl_replace_line ("", false /* clear_undo */);
                reset_line_state();
                i9ep->caught_sigint = true;
        }
+       if (sig_num == SIGTERM)
+               i9ep->caught_sigterm = true;
 }
 
 /**