doxify list.h
[paraslash.git] / stdin.c
1 /*
2  * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 /** \file stdin.c functions that deal with reading from stdin */
20
21 #include "para.h"
22 #include "string.h"
23 #include "list.h"
24 #include "sched.h"
25 #include "fd.h"
26 #include "error.h"
27 #include "stdin.h"
28
29 /**
30  * the pre_select function of the stdin task
31  *
32  * \param s the scheduler this task was registered to
33  * \param t the task structure of the stdin task
34  *
35  * This function is always successful. If there is space left in the
36  * buffer of the stdin task, it adds \p STDIN_FILENO to the read fd set
37  * of \a s.
38  */
39 void stdin_pre_select(struct sched *s, struct task *t)
40 {
41         struct stdin_task *sit = t->private_data;
42         t->ret = 1;
43         sit->check_fd = 0;
44         if (sit->loaded >= sit->bufsize)
45                 return;
46         sit->check_fd = 1;
47         para_fd_set(STDIN_FILENO, &s->rfds, &s->max_fileno);
48 }
49
50 static void stdin_default_event_handler(struct task *t)
51 {
52         PARA_NOTICE_LOG("%p: %s\n", t, PARA_STRERROR(-t->ret));
53         unregister_task(t);
54 }
55
56 /**
57  * the post select function of the stdin task
58  *
59  * \param s the scheduler this task was registered to
60  * \param t the task structure of the stdin task
61  *
62  * This function checks if \p STDIN_FILENO was included by in the read fd set
63  * of \a s during the previous pre_select call.  If yes, and \p STDIN_FILENO
64  * appeears to be readable, data is read from stdin into the buffer of the
65  * stdin task.
66  */
67 void stdin_post_select(struct sched *s, struct task *t)
68 {
69         struct stdin_task *sit = t->private_data;
70         ssize_t ret;
71
72         t->ret = 1;
73         if (!sit->check_fd)
74                 return;
75         if (!FD_ISSET(STDIN_FILENO, &s->rfds))
76                 return;
77         ret = read(STDIN_FILENO, sit->buf + sit->loaded, sit->bufsize - sit->loaded);
78         if (ret < 0)
79                 t->ret = -E_STDIN_READ;
80         else if (ret > 0) {
81                 sit->loaded += ret;
82                 t->ret = ret;
83         } else
84                 t->ret = -E_STDIN_EOF;
85         if (t->ret < 0)
86                 sit->eof = 1;
87 }
88
89 /**
90  * initialize a stdin task structure with default values
91  *
92  * \param sit the stdin task structure
93  *
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).
98  */
99 void stdin_set_defaults(struct stdin_task *sit)
100 {
101         sit->bufsize = 16 * 1024,
102         sit->loaded = 0,
103         sit->eof = 0,
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         mark_fd_nonblock(STDIN_FILENO);
109         sprintf(sit->task.status, "stdin reader");
110 }