2 * Copyright (C) 2006-2008 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() */
22 * The pre_select function of the stdin task.
24 * \param s The scheduler this task was registered to.
25 * \param t The task structure of the stdin task.
27 * This function is always successful. If there is space left in the
28 * buffer of the stdin task, it adds \p STDIN_FILENO to the read fd set
31 static void stdin_pre_select(struct sched *s, struct task *t)
33 struct stdin_task *sit = container_of(t, struct stdin_task, task);
36 if (sit->loaded >= sit->bufsize)
39 para_fd_set(STDIN_FILENO, &s->rfds, &s->max_fileno);
43 * The post select function of the stdin task.
45 * \param s The scheduler this task was registered to.
46 * \param t The task structure of the stdin task.
48 * This function checks if \p STDIN_FILENO was included by in the read fd set
49 * of \a s during the previous pre_select call. If yes, and \p STDIN_FILENO
50 * appeears to be readable, data is read from stdin into the buffer of the
53 static void stdin_post_select(struct sched *s, struct task *t)
55 struct stdin_task *sit = container_of(t, struct stdin_task, task);
61 if (!FD_ISSET(STDIN_FILENO, &s->rfds))
63 ret = read(STDIN_FILENO, sit->buf + sit->loaded, sit->bufsize - sit->loaded);
65 t->error = ERRNO_TO_PARA_ERROR(errno);
69 t->error = -E_STDIN_EOF;
73 * Initialize a stdin task structure with default values.
75 * \param sit The stdin task structure.
77 * This fills in the pre/post select function poinzters of the task structure
78 * given by \a sit. It also sets up a default error handler which unregisters
79 * the task. Moreover, \a loaded and \a eof are set to zero and \a bufsize is
80 * initialized to 16 KB (but no buffer is allocated).
82 void stdin_set_defaults(struct stdin_task *sit)
86 sit->bufsize = 16 * 1024,
88 sit->task.pre_select = stdin_pre_select;
89 sit->task.post_select = stdin_post_select;
90 sprintf(sit->task.status, "stdin reader");
91 ret = mark_fd_nonblocking(STDIN_FILENO);
94 PARA_EMERG_LOG("%s\n", para_strerror(-ret));