e9277d999681fd1108fe26b8ec257bf7fe972d87
[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 return;
17 sot->check_fd = 1;
18 para_fd_set(STDOUT_FILENO, &s->wfds, &s->max_fileno);
19 }
20
21 void stdout_post_select(struct sched *s, struct task *t)
22 {
23 struct stdout_task *sot = t->private_data;
24 ssize_t ret;
25
26 t->ret = 1;
27 if (!sot->check_fd) {
28 if (*sot->input_eof)
29 t->ret = -E_STDOUT_EOF;
30 return;
31 }
32 if (!FD_ISSET(STDOUT_FILENO, &s->wfds))
33 return;
34 t->ret = -E_STDOUT_WRITE;
35 ret = write(STDOUT_FILENO, sot->buf, *sot->loaded);
36 if (ret <= 0)
37 return;
38 *sot->loaded -= ret;
39 t->ret = 1;
40 }
41
42 void stdout_default_event_handler(struct task *t)
43 {
44 PARA_NOTICE_LOG("%p: %s\n", t, PARA_STRERROR(-t->ret));
45 unregister_task(t);
46 }
47
48
49 void stdout_set_defaults(struct stdout_task *sot)
50 {
51 sot->task.private_data = sot;
52 sot->task.pre_select = stdout_pre_select;
53 sot->task.post_select = stdout_post_select;
54 sot->task.event_handler = stdout_default_event_handler;
55 sot->eof = 0;
56 sprintf(sot->task.status, "stdout writer");
57 }