2 * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /** \file stdin.c functions that deal with reading from stdin */
30 * the pre_select function of the stdin task
32 * \param s the scheduler this task was registered to
33 * \param t the task structure of the stdin task
35 * This function is always successful. If there is space left in the
36 * buffer of the stdin task, it adds \a STDIN_FILENO to the read fd set
39 void stdin_pre_select(struct sched
*s
, struct task
*t
)
41 struct stdin_task
*sit
= t
->private_data
;
44 if (sit
->loaded
>= sit
->bufsize
)
47 para_fd_set(STDIN_FILENO
, &s
->rfds
, &s
->max_fileno
);
50 static void stdin_default_event_handler(struct task
*t
)
52 PARA_NOTICE_LOG("%p: %s\n", t
, PARA_STRERROR(-t
->ret
));
57 * the post select function of the stdin task
59 * \param s the scheduler this task was registered to
60 * \param t the task structure of the stdin task
62 * This function checks if \a STDIN_FILENO was included by in the read fd set
63 * of \a s during the previous pre_select call. If yes, and STDIN_FILENO
64 * appeears to be readable, data is read from stdin into the buffer of the
67 void stdin_post_select(struct sched
*s
, struct task
*t
)
69 struct stdin_task
*sit
= t
->private_data
;
75 if (!FD_ISSET(STDIN_FILENO
, &s
->rfds
))
77 ret
= read(STDIN_FILENO
, sit
->buf
+ sit
->loaded
, sit
->bufsize
- sit
->loaded
);
79 t
->ret
= -E_STDIN_READ
;
84 t
->ret
= -E_STDIN_EOF
;
90 * initialize a stdin task structure with default values
92 * \param sit the stdin task structure
94 * This fills in the pre/post select function poinzters of the task structure
95 * given by \a sot. It also sets up a default error handler which unregisters
96 * the task. Moreover, \a loaded and \a eof are set to zero and \a bufsize is
97 * initialized to 16 KB (but no buffer is allocated).
99 void stdin_set_defaults(struct stdin_task
*sit
)
101 sit
->bufsize
= 16 * 1024,
104 sit
->task
.pre_select
= stdin_pre_select
;
105 sit
->task
.post_select
= stdin_post_select
;
106 sit
->task
.event_handler
= stdin_default_event_handler
;
107 sit
->task
.private_data
= sit
;
108 sprintf(sit
->task
.status
, "stdin reader");