f857814a93ef8310669ca4138156a31394767a8c
[paraslash.git] / stdout.c
1 #include "para.h"
2 #include "string.h"
3 #include "list.h"
4 #include "sched.h"
5 #include "fd.h"
6 #include "error.h"
7 #include "stdout.h"
8
9 void stdout_pre_select(struct sched *s, struct task *t)
10 {
11 struct stdout_task *sot = t->private_data;
12
13 t->ret = 1;
14 sot->check_fd = 0;
15 if (!*sot->loaded) {
16 if (*sot->input_eof) {
17 t->ret = -E_STDOUT_EOF;
18 s->timeout.tv_sec = 0;
19 s->timeout.tv_usec = 1;
20 }
21 return;
22 }
23 sot->check_fd = 1;
24 para_fd_set(STDOUT_FILENO, &s->wfds, &s->max_fileno);
25 }
26
27 void stdout_post_select(struct sched *s, struct task *t)
28 {
29 struct stdout_task *sot = t->private_data;
30 ssize_t ret;
31
32 t->ret = 1;
33 if (!sot->check_fd) {
34 if (*sot->input_eof)
35 t->ret = -E_STDOUT_EOF;
36 return;
37 }
38 if (!FD_ISSET(STDOUT_FILENO, &s->wfds))
39 return;
40 t->ret = -E_STDOUT_WRITE;
41 ret = write(STDOUT_FILENO, sot->buf, *sot->loaded);
42 if (ret <= 0)
43 return;
44 *sot->loaded -= ret;
45 t->ret = 1;
46 }
47
48 void stdout_default_event_handler(struct task *t)
49 {
50 PARA_NOTICE_LOG("%p: %s\n", t, PARA_STRERROR(-t->ret));
51 unregister_task(t);
52 }
53
54
55 void stdout_set_defaults(struct stdout_task *sot)
56 {
57 sot->task.private_data = sot;
58 sot->task.pre_select = stdout_pre_select;
59 sot->task.post_select = stdout_post_select;
60 sot->task.event_handler = stdout_default_event_handler;
61 sot->eof = 0;
62 sprintf(sot->task.status, "stdout writer");
63 }