2 * Copyright (C) 2006 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
= t
->private_data
;
36 if (sit
->loaded
>= sit
->bufsize
)
39 para_fd_set(STDIN_FILENO
, &s
->rfds
, &s
->max_fileno
);
42 static void stdin_default_event_handler(struct task
*t
)
44 PARA_NOTICE_LOG("%p: %s\n", t
, PARA_STRERROR(-t
->ret
));
49 * the post select function of the stdin task
51 * \param s the scheduler this task was registered to
52 * \param t the task structure of the stdin task
54 * This function checks if \p STDIN_FILENO was included by in the read fd set
55 * of \a s during the previous pre_select call. If yes, and \p STDIN_FILENO
56 * appeears to be readable, data is read from stdin into the buffer of the
59 static void stdin_post_select(struct sched
*s
, struct task
*t
)
61 struct stdin_task
*sit
= t
->private_data
;
67 if (!FD_ISSET(STDIN_FILENO
, &s
->rfds
))
69 ret
= read(STDIN_FILENO
, sit
->buf
+ sit
->loaded
, sit
->bufsize
- sit
->loaded
);
71 t
->ret
= -E_STDIN_READ
;
76 t
->ret
= -E_STDIN_EOF
;
82 * Initialize a stdin task structure with default values.
84 * \param sit The stdin task structure.
86 * This fills in the pre/post select function poinzters of the task structure
87 * given by \a sit. It also sets up a default error handler which unregisters
88 * the task. Moreover, \a loaded and \a eof are set to zero and \a bufsize is
89 * initialized to 16 KB (but no buffer is allocated).
91 void stdin_set_defaults(struct stdin_task
*sit
)
93 sit
->bufsize
= 16 * 1024,
96 sit
->task
.pre_select
= stdin_pre_select
;
97 sit
->task
.post_select
= stdin_post_select
;
98 sit
->task
.event_handler
= stdin_default_event_handler
;
99 sit
->task
.private_data
= sit
;
100 mark_fd_nonblock(STDIN_FILENO
);
101 sprintf(sit
->task
.status
, "stdin reader");