2 * Copyright (C) 2006-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file stdin.c Functions that deal with reading from stdin. */
9 #include <dirent.h> /* readdir() */
20 #include "buffer_tree.h"
24 * The pre_select function of the stdin task.
26 * \param s The scheduler this task was registered to.
27 * \param t The task structure of the stdin task.
29 * This function is always successful. If there is space left in the
30 * buffer of the stdin task, it adds \p STDIN_FILENO to the read fd set
33 static void stdin_pre_select(struct sched *s, struct task *t)
35 struct stdin_task *sit = container_of(t, struct stdin_task, task);
37 if (sit->output_error && *sit->output_error < 0) {
38 t->error = *sit->output_error;
43 if (sit->loaded >= sit->bufsize)
46 para_fd_set(STDIN_FILENO, &s->rfds, &s->max_fileno);
49 static void stdin_pre_select_btr(struct sched *s, struct task *t)
51 struct stdin_task *sit = container_of(t, struct stdin_task, task);
55 ret = btr_node_status(sit->btrn, 0, BTR_NT_ROOT);
57 para_fd_set(STDIN_FILENO, &s->rfds, &s->max_fileno);
59 s->timeout.tv_sec = 0;
60 s->timeout.tv_usec = 1;
65 * The post select function of the stdin task.
67 * \param s The scheduler this task was registered to.
68 * \param t The task structure of the stdin task.
70 * This function checks if \p STDIN_FILENO was included by in the read fd set
71 * of \a s during the previous pre_select call. If yes, and \p STDIN_FILENO
72 * appears to be readable, data is read from stdin into the buffer of the
75 static void stdin_post_select(struct sched *s, struct task *t)
77 struct stdin_task *sit = container_of(t, struct stdin_task, task);
80 if (sit->output_error && *sit->output_error < 0) {
81 t->error = *sit->output_error;
87 if (!FD_ISSET(STDIN_FILENO, &s->rfds))
89 ret = read(STDIN_FILENO, sit->buf + sit->loaded, sit->bufsize - sit->loaded);
91 t->error = ERRNO_TO_PARA_ERROR(errno);
95 t->error = -E_STDIN_EOF;
98 #define STDIN_INPUT_BUFFER_SIZE (1024 * 32)
99 static void stdin_post_select_btr(struct sched *s, struct task *t)
101 struct stdin_task *sit = container_of(t, struct stdin_task, task);
106 ret = btr_node_status(sit->btrn, 0, BTR_NT_ROOT);
111 if (!FD_ISSET(STDIN_FILENO, &s->rfds))
113 buf = para_malloc(STDIN_INPUT_BUFFER_SIZE);
114 ret = read(STDIN_FILENO, buf, STDIN_INPUT_BUFFER_SIZE);
115 //PARA_CRIT_LOG("read ret: %d\n", ret);
118 ret = -ERRNO_TO_PARA_ERROR(errno);
123 btr_add_output(buf, ret, sit->btrn);
127 btr_remove_node(sit->btrn);
132 * Initialize a stdin task structure with default values.
134 * \param sit The stdin task structure.
136 * This fills in the pre/post select function pointers of the task structure
137 * given by \a sit. Moreover, the stdin file desctiptor is set to nonblocking
138 * mode and \a bufsize is initialized to 16 KB (but no buffer is allocated).
140 void stdin_set_defaults(struct stdin_task *sit)
144 sit->bufsize = 32 * 1024;
146 sit->task.pre_select = stdin_pre_select_btr;
147 sit->task.post_select = stdin_post_select_btr;
149 sit->task.pre_select = stdin_pre_select;
150 sit->task.post_select = stdin_post_select;
152 sprintf(sit->task.status, "stdin reader");
153 ret = mark_fd_nonblocking(STDIN_FILENO);
156 sit->output_error = NULL;
157 PARA_EMERG_LOG("%s\n", para_strerror(-ret));