the paraslash-0.2.14 release tarball
[paraslash.git] / stdout.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 stdout.c functions that deal with writing to stdout */
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 "stdout.h"
28
29 /**
30  * the pre_select function of the stdout task
31  *
32  * \param s the scheduler this task was registered to
33  * \param t the task structure of the stdout task
34  *
35  * This function is always successful. If there is data available in the input
36  * buffer, it adds \p STDOUT_FILENO to the write fd set of \a s.
37  */
38 void stdout_pre_select(struct sched *s, struct task *t)
39 {
40         struct stdout_task *sot = t->private_data;
41
42         t->ret = 1;
43         sot->check_fd = 0;
44         if (!*sot->loaded) {
45                 if (*sot->input_eof) {
46                         t->ret = -E_STDOUT_EOF;
47                         s->timeout.tv_sec = 0;
48                         s->timeout.tv_usec = 1;
49                 }
50                 return;
51         }
52         sot->check_fd = 1;
53         para_fd_set(STDOUT_FILENO, &s->wfds, &s->max_fileno);
54 }
55
56 /**
57  * the post select function of the stdout task
58  *
59  * \param s the scheduler this task was registered to
60  * \param t the task structure of the stdout task
61  *
62  * This function checks if \p STDOUT_FILENO was included by in the write fd set
63  * of \a s during the previous pre_select call.  If yes, and \p STDOUT_FILENO
64  * appeears to be writable, the data loaded in the input buffer is written to
65  * stdout.
66  */
67 void stdout_post_select(struct sched *s, struct task *t)
68 {
69         struct stdout_task *sot = t->private_data;
70         ssize_t ret;
71
72         t->ret = 1;
73         if (!sot->check_fd) {
74                 if (*sot->input_eof)
75                         t->ret = -E_STDOUT_EOF;
76                 return;
77         }
78         if (!FD_ISSET(STDOUT_FILENO, &s->wfds))
79                 return;
80         t->ret = -E_STDOUT_WRITE;
81         ret = write(STDOUT_FILENO, sot->buf, *sot->loaded);
82         if (ret <= 0)
83                 return;
84         *sot->loaded -= ret;
85         if (*sot->loaded)
86                 memmove(sot->buf, sot->buf + ret, *sot->loaded);
87         t->ret = 1;
88 }
89
90 static void stdout_default_event_handler(struct task *t)
91 {
92         PARA_NOTICE_LOG("%p: %s\n", t, PARA_STRERROR(-t->ret));
93         unregister_task(t);
94 }
95
96 /**
97  * initialize a stdout task structure with default values
98  *
99  * \param sot the stdout task structure
100  *
101  * This fills in the pre/post select function poinzters of the task structure
102  * given by \a sot. It also sets up a default error handler which unregisters
103  * the task on errors and clears the eof flag of \a sot.
104  */
105 void stdout_set_defaults(struct stdout_task *sot)
106 {
107         sot->task.private_data = sot;
108         sot->task.pre_select = stdout_pre_select;
109         sot->task.post_select = stdout_post_select;
110         sot->task.event_handler = stdout_default_event_handler;
111         sot->eof = 0;
112         mark_fd_nonblock(STDOUT_FILENO);
113         sprintf(sot->task.status, "stdout writer");
114 }