char command_buf[2][COMMAND_BUF_SIZE] = {"", ""};
int cbo[2] = {0, 0}; /* command buf offsets */
struct timeval tv;
+ unsigned flags[2] = {0, 0}; /* for for_each_line() */
repeat:
tv.tv_sec = conf.timeout_arg / 1000;
COMMAND_BUF_SIZE - 1 - cbo[i], &rfds, &sz);
cbo[i] += sz;
sz = cbo[i];
- cbo[i] = for_each_line(0, command_buf[i], cbo[i],
+ cbo[i] = for_each_line(flags[i], command_buf[i], cbo[i],
add_output_line, &i);
- if (sz != cbo[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;
}
}
}
} else
end = next_cr;
num_lines++;
- if (flags & FELF_READ_ONLY) {
- size_t s = end - start;
- char *b = para_malloc(s + 1);
- memcpy(b, start, s);
- b[s] = '\0';
-// PARA_NOTICE_LOG("b: %s, start: %s\n", b, start);
- ret = line_handler(b, private_data);
- free(b);
- } else {
- *end = '\0';
- ret = line_handler(start, private_data);
+ if (!(flags & FELF_DISCARD_FIRST) || start != buf) {
+ if (flags & FELF_READ_ONLY) {
+ size_t s = end - start;
+ char *b = para_malloc(s + 1);
+ memcpy(b, start, s);
+ b[s] = '\0';
+ ret = line_handler(b, private_data);
+ free(b);
+ } else {
+ *end = '\0';
+ ret = line_handler(start, private_data);
+ }
+ if (ret < 0)
+ return ret;
}
- if (ret < 0)
- return ret;
start = ++end;
}
i = buf + size - start;
enum for_each_line_flags {
/** Activate read-only mode. */
FELF_READ_ONLY = 1 << 0,
+ /** Don't call line handler for the first input line. */
+ FELF_DISCARD_FIRST = 1 << 1,
};
/** Used for \ref for_each_line(). */