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