+ case '-':
+ parse_opts_opt(cfg, argc, argv, i);
+ break;
+ default :
+ parse_opts_spec(cfg, argc, argv, i);
+ }
+ }
+}
+
+void
+opts_parse(Config *cfg, int argc, char *argv[])
+{
+ opts_parse_any(cfg, argc, argv, 1);
+
+ Fifo *last = cfg->fifos;
+ cfg->fifos = NULL;
+ for (Fifo *f = last; f; ) {
+ Fifo *next = f->next;
+ f->next = cfg->fifos;
+ cfg->fifos = f;
+ f = next;
+ }
+}
+
+void
+fifo_expire(Fifo *f, struct timespec t, char *buf)
+{
+ struct timespec td;
+
+ timespecsub(&t, &(f->last_read), &td);
+ if (timespeccmp(&td, &(f->ttl), >=)) {
+ /* TODO: Maybe configurable expiry character. */
+ memset(buf + f->pos_init, '_', f->pos_final - f->pos_init);
+ warn("Data source expired: \"%s\"\n", f->name);
+ }
+}
+
+void
+fifo_read_error(Fifo *f, char *buf)
+{
+ char *b;
+ int i;
+
+ b = buf + f->pos_init;
+ /* Copy as much of the error message as possible.
+ * EXCLUDING the terminating \0. */
+ for (i = 0; i < errlen && i < f->width; i++)
+ b[i] = errmsg[i];
+ /* Any remaining slots: */
+ for (; i < f->width; i++)
+ b[i] = '_';
+}
+
+enum read_status
+fifo_read_one(Fifo *f, struct timespec t, char *buf)
+{
+ char c; /* Character read. */
+ int r; /* Remaining unused slots in buffer range. */
+
+ for (;;) {
+ switch (read(f->fd, &c, 1)) {
+ case -1:
+ error("Failed to read: \"%s\". errno: %d, msg: %s\n",
+ f->name, errno, strerror(errno));
+ switch (errno) {
+ case EINTR:
+ case EAGAIN:
+ return RETRY;
+ default:
+ return FAILURE;
+ }
+ case 0:
+ debug("%s: End of FILE\n", f->name);
+ f->pos_curr = f->pos_init;
+ return END_OF_FILE;
+ case 1:
+ /* TODO: Consider making msg term char a CLI option */
+ if (c == '\n' || c == '\0') {
+ r = f->pos_final - f->pos_curr;
+ if (r > 0)
+ memset(buf + f->pos_curr, ' ', r);
+ f->pos_curr = f->pos_init;
+ f->last_read = t;
+ return END_OF_MESSAGE;
+ } else {
+ if (f->pos_curr <= f->pos_final)
+ buf[f->pos_curr++] = c;
+ /* Drop beyond available range. */
+ /*
+ * TODO Define max after which we stop reading.
+ * To ensure that a rogue large message
+ * doesn't trap us here.
+ */
+ }
+ break;
+ default:
+ assert(0);
+ }
+ }
+}
+
+void
+fifo_read_all(Config *cfg, struct timespec *ti, char *buf)
+{
+ fd_set fds;
+ int maxfd = -1;
+ int ready = 0;
+ struct stat st;
+ struct timespec t;
+
+ FD_ZERO(&fds);
+ for (Fifo *f = cfg->fifos; f; f = f->next) {
+ /* TODO: Create the FIFO if it doesn't already exist. */
+ if (lstat(f->name, &st) < 0) {
+ error(
+ "Cannot stat \"%s\". Error: %s\n",
+ f->name,
+ strerror(errno)
+ );
+ fifo_read_error(f, buf);
+ continue;
+ }
+ if (!(st.st_mode & S_IFIFO)) {
+ error("\"%s\" is not a FIFO\n", f->name);
+ fifo_read_error(f, buf);
+ continue;
+ }
+ if (f->fd < 0) {
+ debug("%s: closed. opening. fd: %d\n", f->name, f->fd);
+ f->fd = open(f->name, O_RDONLY | O_NONBLOCK);
+ } else {
+ debug("%s: already openned. fd: %d\n", f->name, f->fd);
+ }
+ if (f->fd == -1) {
+ /* TODO Consider backing off retries for failed fifos */
+ error("Failed to open \"%s\"\n", f->name);
+ fifo_read_error(f, buf);
+ continue;
+ }
+ debug("%s: open. fd: %d\n", f->name, f->fd);
+ if (f->fd > maxfd)
+ maxfd = f->fd;
+ FD_SET(f->fd, &fds);
+ }
+ debug("selecting...\n");
+ ready = pselect(maxfd + 1, &fds, NULL, NULL, ti, NULL);
+ debug("ready: %d\n", ready);
+ clock_gettime(CLOCK_MONOTONIC, &t);
+ if (ready == -1) {
+ switch (errno) {
+ case EINTR:
+ error("pselect temp failure: %d, errno: %d, msg: %s\n",
+ ready, errno, strerror(errno));
+ /* TODO: Reconsider what to do here. */
+ return;
+ default:
+ fatal("pselect failed: %d, errno: %d, msg: %s\n",
+ ready, errno, strerror(errno));