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 /* Monitor STDOUT_FILENO if there is input data available. */
14 static void stdout_pre_monitor(struct sched *s, void *context)
16 struct stdout_task *sot = context;
19 ret = btr_node_status(sot->btrn, 0, BTR_NT_LEAF);
21 sched_monitor_writefd(STDOUT_FILENO, s);
27 * If input from the buffer tree is available and STDOUT_FILENO is ready, write
28 * as much as possible.
30 static int stdout_post_monitor(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 (!sched_write_ok(STDOUT_FILENO, s))
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_monitor = stdout_pre_monitor,
83 .post_monitor = stdout_post_monitor,
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);