gui: Execute stat command in status_post_select().
[paraslash.git] / gui.c
diff --git a/gui.c b/gui.c
index ef9410cc26dc6a26268a9873202495bd43ffaa7d..894fc2f87298962b7f4a8de7729f74f853b7c49c 100644 (file)
--- a/gui.c
+++ b/gui.c
@@ -53,7 +53,11 @@ static int stat_pipe = -1;
 static struct gui_args_info conf;
 static int loglevel;
 
-enum gui_select_mode{GETCH_MODE, COMMAND_MODE, EXTERNAL_MODE};
+enum cmd_status {
+       CMDS_IDLE, /* no command running */
+       CMDS_DCMD, /* para or display command running */
+       CMDS_XCMD, /* external command running */
+};
 
 /**
  * Codes for various colors.
@@ -640,15 +644,50 @@ static void clear_all_items(void)
        }
 }
 
+static struct timeval next_exec;
+
+static void status_pre_select(fd_set *rfds, int *max_fileno, struct timeval *tv)
+{
+       struct timeval atm, diff;
+
+       if (stat_pipe >= 0)
+               return para_fd_set(stat_pipe, rfds, max_fileno);
+       gettimeofday(&atm, NULL);
+       if (tv_diff(&next_exec, &atm, &diff) > 0) {
+               *tv = diff;
+               return;
+       }
+       tv->tv_sec = tv->tv_usec = 0; /* min delay */
+}
+
 static void status_post_select(fd_set *rfds)
 {
        static char *buf;
        static int bufsize, loaded;
-       int ret, ret2;
        size_t sz;
+       pid_t pid;
+       int ret, ret2;
 
-       if (stat_pipe < 0)
+       if (stat_pipe < 0) {
+               struct timeval atm;
+               int fds[3] = {0, 1, 0};
+               /* Avoid busy loop */
+               gettimeofday(&atm, NULL);
+               if (tv_diff(&next_exec, &atm, NULL) > 0)
+                       return;
+               next_exec.tv_sec = atm.tv_sec + 2;
+               ret = para_exec_cmdline_pid(&pid, conf.stat_cmd_arg, fds);
+               if (ret < 0)
+                       return;
+               ret = mark_fd_nonblocking(fds[1]);
+               if (ret < 0) {
+                       close(fds[1]);
+                       return;
+               }
+               stat_pipe = fds[1];
                return;
+       }
+
        if (loaded >= bufsize) {
                if (bufsize > 1000 * 1000) {
                        loaded = 0;
@@ -950,51 +989,22 @@ static void signal_post_select(fd_set *rfds)
        }
 }
 
-static void status_pre_select(fd_set *rfds, int *max_fileno, struct timeval *tv)
-{
-       static struct timeval next_exec, atm, diff;
-       int ret, fds[3] = {0, 1, 0};
-       pid_t pid;
-
-       if (stat_pipe >= 0)
-               goto success;
-       /* Avoid busy loop */
-       gettimeofday(&atm, NULL);
-       if (tv_diff(&next_exec, &atm, &diff) > 0) {
-               if (tv_diff(&diff, tv, NULL) < 0)
-                       *tv = diff;
-               return;
-       }
-       next_exec.tv_sec = atm.tv_sec + 2;
-       ret = para_exec_cmdline_pid(&pid, conf.stat_cmd_arg, fds);
-       if (ret < 0)
-               return;
-       ret = mark_fd_nonblocking(fds[1]);
-       if (ret < 0) {
-               close(fds[1]);
-               return;
-       }
-       stat_pipe = fds[1];
-success:
-       para_fd_set(stat_pipe, rfds, max_fileno);
-}
-
 #define COMMAND_BUF_SIZE 32768
 
-static enum gui_select_mode get_select_mode(void)
+static enum cmd_status cmd_status(void)
 {
        if (command_fds[0] >= 0 || command_fds[1] >= 0)
-               return COMMAND_MODE;
+               return CMDS_DCMD;
        if (cmd_pid > 0)
-               return EXTERNAL_MODE;
-       return GETCH_MODE;
+               return CMDS_XCMD;
+       return CMDS_IDLE;
 }
 
 static void command_pre_select(fd_set *rfds, int *max_fileno)
 {
-       enum gui_select_mode mode = get_select_mode();
+       enum cmd_status cmds = cmd_status();
 
-       if (mode != COMMAND_MODE)
+       if (cmds != CMDS_DCMD)
                return;
        if (command_fds[0] >= 0)
                para_fd_set(command_fds[0], rfds, max_fileno);
@@ -1008,9 +1018,9 @@ static void command_post_select(fd_set *rfds)
        static char command_buf[2][COMMAND_BUF_SIZE];
        static int cbo[2]; /* command buf offsets */
        static unsigned flags[2]; /* for for_each_line() */
-       enum gui_select_mode mode = get_select_mode();
+       enum cmd_status cmds = cmd_status();
 
-       if (mode != COMMAND_MODE)
+       if (cmds != CMDS_DCMD)
                return;
        for (i = 0; i < 2; i++) {
                size_t sz;
@@ -1048,9 +1058,9 @@ static void command_post_select(fd_set *rfds)
 
 static void input_pre_select(fd_set *rfds, int *max_fileno)
 {
-       enum gui_select_mode mode = get_select_mode();
+       enum cmd_status cmds = cmd_status();
 
-       if (mode == GETCH_MODE || mode == COMMAND_MODE)
+       if (cmds != CMDS_XCMD)
                para_fd_set(STDIN_FILENO, rfds, max_fileno);
 }
 
@@ -1152,14 +1162,14 @@ static void handle_command(int c)
 static void input_post_select(void)
 {
        int ret;
-       enum gui_select_mode mode = get_select_mode();
+       enum cmd_status cmds = cmd_status();
 
-       if (mode == EXTERNAL_MODE)
+       if (cmds == CMDS_XCMD)
                return;
        ret = wgetch(top.win);
        if (ret == ERR || ret == KEY_RESIZE)
                return;
-       if (mode == GETCH_MODE)
+       if (cmds == CMDS_IDLE)
                return handle_command(ret);
        if (cmd_pid != 0)
                kill(cmd_pid, SIGTERM);