+/*
+ * Copyright (C) 2006-2014 Andre Noll <maan@systemlinux.org>
+ *
+ * Licensed under the GPL v2. For licencing details see COPYING.
+ */
+
+/** \file stdin.c Functions that deal with reading from stdin. */
+
+#include <assert.h>
+#include <regex.h>
+
#include "para.h"
-#include "string.h"
#include "list.h"
#include "sched.h"
#include "fd.h"
#include "error.h"
#include "stdin.h"
+#include "buffer_tree.h"
+#include "string.h"
-void stdin_pre_select(struct sched *s, struct task *t)
+/**
+ * The pre_select function of the stdin task.
+ *
+ * \param s The scheduler this task was registered to.
+ * \param t The task structure of the stdin task.
+ *
+ * This function is always successful. If there is space left in the
+ * buffer of the stdin task, it adds \p STDIN_FILENO to the read fd set
+ * of \a s.
+ */
+static void stdin_pre_select(struct sched *s, struct task *t)
{
- struct stdin_task *sit = t->private_data;
- if (sit->loaded < sit->bufsize)
- para_fd_set(STDIN_FILENO, &s->rfds, &s->max_fileno);
- t->ret = 1; /* success */
-}
+ struct stdin_task *sit = container_of(t, struct stdin_task, task);
+ int ret;
-static void stdin_default_event_handler(struct task *t)
-{
- PARA_NOTICE_LOG("%p: %s\n", t, PARA_STRERROR(-t->ret));
- unregister_task(t);
+ ret = btr_node_status(sit->btrn, 0, BTR_NT_ROOT);
+ if (ret < 0)
+ sched_min_delay(s);
+ if (ret <= 0)
+ return;
+ if (btr_pool_unused(sit->btrp) > 0)
+ return para_fd_set(STDIN_FILENO, &s->rfds, &s->max_fileno);
+ sched_request_timeout_ms(100, s);
}
-void stdin_post_select(struct sched *s, struct task *t)
+/**
+ * The post select function of the stdin task.
+ *
+ * \param s The scheduler this task was registered to.
+ * \param t The task structure of the stdin task.
+ *
+ * This function checks if \p STDIN_FILENO was included by in the read fd set
+ * of \a s during the previous pre_select call. If yes, and \p STDIN_FILENO
+ * appears to be readable, data is read from stdin and fed into the buffer
+ * tree.
+ */
+static int stdin_post_select(struct sched *s, struct task *t)
{
- struct stdin_task *sit = t->private_data;
+ struct stdin_task *sit = container_of(t, struct stdin_task, task);
ssize_t ret;
+ size_t sz, n;
+ char *buf = NULL;
- t->ret = 1;
- if (sit->loaded >= sit->bufsize)
- return;
- if (!FD_ISSET(STDIN_FILENO, &s->rfds))
- return;
- ret = read(STDIN_FILENO, sit->buf + sit->loaded, sit->bufsize - sit->loaded);
+ ret = btr_node_status(sit->btrn, 0, BTR_NT_ROOT);
if (ret < 0)
- t->ret = -E_STDIN_READ;
- else if (ret > 0) {
- sit->loaded += ret;
- t->ret = ret;
- } else
- t->ret = -E_STDIN_EOF;
- if (t->ret < 0)
- sit->eof = 1;
+ goto err;
+ if (ret == 0)
+ return 0;
+ sz = btr_pool_get_buffer(sit->btrp, &buf);
+ if (sz == 0)
+ return 0;
+ if (sit->must_set_nonblock_flag) {
+ ret = mark_fd_nonblocking(STDIN_FILENO);
+ if (ret < 0)
+ goto err;
+ sit->must_set_nonblock_flag = false;
+ }
+ /*
+ * Do not use the maximal size to avoid having only a single buffer
+ * reference for the whole pool. This is bad because if that single
+ * reference can not be freed, we're stuck.
+ */
+ sz = PARA_MIN(sz, btr_pool_size(sit->btrp) / 2);
+ ret = read_nonblock(STDIN_FILENO, buf, sz, &s->rfds, &n);
+ if (n > 0)
+ btr_add_output_pool(sit->btrp, n, sit->btrn);
+ if (ret >= 0)
+ return 0;
+err:
+ btr_remove_node(&sit->btrn);
+ /* Revert to blocking mode if necessary. */
+ fcntl(STDIN_FILENO, F_SETFL, sit->fd_flags);
+ //btr_pool_free(sit->btrp);
+ return ret;
}
+/**
+ * Initialize a stdin task structure with default values.
+ *
+ * \param sit The stdin task structure.
+ *
+ * This fills in the pre/post select function pointers of the task structure
+ * given by \a sit and creates a buffer tree for I/O.
+ */
void stdin_set_defaults(struct stdin_task *sit)
{
- sit->bufsize = 16 * 1024,
- sit->loaded = 0,
- sit->eof = 0,
+ int ret;
+
sit->task.pre_select = stdin_pre_select;
sit->task.post_select = stdin_post_select;
- sit->task.event_handler = stdin_default_event_handler;
- sit->task.private_data = sit;
+ sit->btrp = btr_pool_new("stdin", 128 * 1024);
sprintf(sit->task.status, "stdin reader");
+ /*
+ * Both STDIN_FILENO and STDOUT_FILENO may refer to the same open file
+ * description (the terminal), and thus share the same file status
+ * flags. In order to not interfere with the stdout task, we only get
+ * the file status flags for STDIN here and save a copy. The nonblock
+ * flag is set later on the first read.
+ */
+ ret = fcntl(STDIN_FILENO, F_GETFL);
+ if (ret < 0) {
+ PARA_EMERG_LOG("F_GETFL: %s\n", strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ sit->fd_flags = ret;
+ sit->must_set_nonblock_flag = (sit->fd_flags & O_NONBLOCK) == 0;
}