- if (command_pipe >= 0 && mode == COMMAND_MODE) {
- size_t sz;
- ret = read_nonblock(command_pipe, command_buf + cbo,
- sizeof(command_buf) - 1 - cbo, &rfds, &sz);
- cbo += sz;
- sz = cbo;
- cbo = for_each_line(command_buf, cbo, &add_output_line, NULL);
- if (sz != cbo)
- wrefresh(bot.win);
- if (ret < 0) {
- PARA_NOTICE_LOG("closing command pipe: %s",
- para_strerror(-ret));
- close(command_pipe);
- command_pipe = -1;
- return 0;
+ if (mode == COMMAND_MODE) {
+ for (i = 0; i < 2; i++) {
+ size_t sz;
+ if (command_fds[i] < 0)
+ continue;
+ ret = read_nonblock(command_fds[i],
+ command_buf[i] + cbo[i],
+ COMMAND_BUF_SIZE - 1 - cbo[i], &rfds, &sz);
+ cbo[i] += sz;
+ sz = cbo[i];
+ cbo[i] = for_each_line(flags[i], command_buf[i], cbo[i],
+ add_output_line, &i);
+ if (sz != cbo[i]) { /* at least one line found */
+ wrefresh(bot.win);
+ flags[i] = 0;
+ }
+ if (ret < 0) {
+ PARA_NOTICE_LOG("closing command fd %d: %s",
+ i, para_strerror(-ret));
+ close(command_fds[i]);
+ command_fds[i] = -1;
+ flags[i] = 0;
+ if (command_fds[!i] < 0) /* both fds closed */
+ return 0;
+ }
+ if (cbo[i] == COMMAND_BUF_SIZE - 1) {
+ PARA_NOTICE_LOG("discarding overlong line");
+ cbo[i] = 0;
+ flags[i] = FELF_DISCARD_FIRST;
+ }