rc4: Round up output buffer size.
[paraslash.git] / stdout.c
1 /*
2  * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file stdout.c functions that deal with writing to stdout */
8
9 #include "para.h"
10 #include "string.h"
11 #include "list.h"
12 #include "sched.h"
13 #include "fd.h"
14 #include "error.h"
15 #include "stdout.h"
16
17 /**
18  * the pre_select function of the stdout task
19  *
20  * \param s the scheduler this task was registered to
21  * \param t the task structure of the stdout task
22  *
23  * This function is always successful. If there is data available in the input
24  * buffer, it adds \p STDOUT_FILENO to the write fd set of \a s.
25  */
26 void stdout_pre_select(struct sched *s, struct task *t)
27 {
28         struct stdout_task *sot = t->private_data;
29
30         t->ret = 1;
31         sot->check_fd = 0;
32         if (!*sot->loaded) {
33                 if (*sot->input_eof) {
34                         t->ret = -E_STDOUT_EOF;
35                         s->timeout.tv_sec = 0;
36                         s->timeout.tv_usec = 1;
37                 }
38                 return;
39         }
40         sot->check_fd = 1;
41         para_fd_set(STDOUT_FILENO, &s->wfds, &s->max_fileno);
42 }
43
44 /**
45  * the post select function of the stdout task
46  *
47  * \param s the scheduler this task was registered to
48  * \param t the task structure of the stdout task
49  *
50  * This function checks if \p STDOUT_FILENO was included by in the write fd set
51  * of \a s during the previous pre_select call.  If yes, and \p STDOUT_FILENO
52  * appeears to be writable, the data loaded in the input buffer is written to
53  * stdout.
54  */
55 void stdout_post_select(struct sched *s, struct task *t)
56 {
57         struct stdout_task *sot = t->private_data;
58         ssize_t ret;
59
60         t->ret = 1;
61         if (!sot->check_fd) {
62                 if (*sot->input_eof)
63                         t->ret = -E_STDOUT_EOF;
64                 return;
65         }
66         if (!FD_ISSET(STDOUT_FILENO, &s->wfds))
67                 return;
68         t->ret = -E_STDOUT_WRITE;
69         ret = write(STDOUT_FILENO, sot->buf, *sot->loaded);
70         if (ret <= 0)
71                 return;
72         *sot->loaded -= ret;
73         if (*sot->loaded)
74                 memmove(sot->buf, sot->buf + ret, *sot->loaded);
75         t->ret = 1;
76 }
77
78 static void stdout_default_event_handler(struct task *t)
79 {
80         PARA_NOTICE_LOG("%p: %s\n", t, PARA_STRERROR(-t->ret));
81         unregister_task(t);
82 }
83
84 /**
85  * initialize a stdout task structure with default values
86  *
87  * \param sot the stdout task structure
88  *
89  * This fills in the pre/post select function poinzters of the task structure
90  * given by \a sot. It also sets up a default error handler which unregisters
91  * the task on errors and clears the eof flag of \a sot.
92  */
93 void stdout_set_defaults(struct stdout_task *sot)
94 {
95         sot->task.private_data = sot;
96         sot->task.pre_select = stdout_pre_select;
97         sot->task.post_select = stdout_post_select;
98         sot->task.event_handler = stdout_default_event_handler;
99         sot->eof = 0;
100         mark_fd_nonblock(STDOUT_FILENO);
101         sprintf(sot->task.status, "stdout writer");
102 }