1 /* Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file stdout.c Functions that deal with writing to stdout. */
11 #include "buffer_tree.h"
13 /* Add STDOUT_FILENO to the write fd set if there is input data available. */
14 static void stdout_pre_select(struct sched
*s
, void *context
)
16 struct stdout_task
*sot
= context
;
19 ret
= btr_node_status(sot
->btrn
, 0, BTR_NT_LEAF
);
21 para_fd_set(STDOUT_FILENO
, &s
->wfds
, &s
->max_fileno
);
27 * This function writes input data from the buffer tree to stdout if
28 * STDOUT_FILENO is writable.
30 static int stdout_post_select(struct sched
*s
, void *context
)
32 struct stdout_task
*sot
= context
;
33 struct btr_node
*btrn
= sot
->btrn
;
38 ret
= btr_node_status(btrn
, 0, BTR_NT_LEAF
);
43 if (!FD_ISSET(STDOUT_FILENO
, &s
->wfds
))
46 if (sot
->must_set_nonblock_flag
) {
47 ret
= mark_fd_nonblocking(STDOUT_FILENO
);
50 sot
->must_set_nonblock_flag
= false;
53 sz
= btr_next_buffer(btrn
, &buf
);
56 ret
= xwrite(STDOUT_FILENO
, buf
, sz
);
59 btr_consume(btrn
, ret
);
63 btr_remove_node(&sot
->btrn
);
64 /* Revert to blocking mode if necessary. */
65 fcntl(STDOUT_FILENO
, F_SETFL
, sot
->fd_flags
);
71 * Register a stdout task structure.
73 * \param sot The stdout task structure to register.
74 * \param s The task will be added to this scheduler's task list.
76 * This sets up \a sot and registers a task with \a sot as context pointer.
78 void stdout_task_register(struct stdout_task
*sot
, struct sched
*s
)
81 struct task_info ti
= {
82 .pre_select
= stdout_pre_select
,
83 .post_select
= stdout_post_select
,
88 /* See \ref stdin.c for details. */
89 ret
= fcntl(STDOUT_FILENO
, F_GETFL
);
91 PARA_EMERG_LOG("F_GETFL: %s\n", strerror(errno
));
95 sot
->must_set_nonblock_flag
= (sot
->fd_flags
& O_NONBLOCK
) == 0
96 && !isatty(STDOUT_FILENO
);
97 sot
->task
= task_register(&ti
, s
);