Merge branch 'refs/heads/t/si_update'
[paraslash.git] / stdin.c
1 /*
2 * Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file stdin.c Functions that deal with reading from stdin. */
8
9 #include <regex.h>
10
11 #include "para.h"
12 #include "list.h"
13 #include "sched.h"
14 #include "fd.h"
15 #include "error.h"
16 #include "stdin.h"
17 #include "buffer_tree.h"
18 #include "string.h"
19
20 /*
21 * If there is space left in the buffer of the stdin task add STDIN_FILENO to
22 * the read fd set of s.
23 */
24 static void stdin_pre_select(struct sched *s, void *context)
25 {
26 struct stdin_task *sit = context;
27 int ret;
28
29 ret = btr_node_status(sit->btrn, 0, BTR_NT_ROOT);
30 if (ret < 0)
31 sched_min_delay(s);
32 if (ret <= 0)
33 return;
34 if (btr_pool_unused(sit->btrp) > 0)
35 return para_fd_set(STDIN_FILENO, &s->rfds, &s->max_fileno);
36 sched_request_timeout_ms(100, s);
37 }
38
39 /*
40 * This function checks if STDIN_FILENO was included by in the read fd set of s
41 * during the previous pre_select call. If so, and if STDIN_FILENO is readable,
42 * data is read from stdin and fed into the buffer tree.
43 */
44 static int stdin_post_select(struct sched *s, void *context)
45 {
46 struct stdin_task *sit = context;
47 ssize_t ret;
48 size_t sz, n;
49 char *buf = NULL;
50
51 ret = btr_node_status(sit->btrn, 0, BTR_NT_ROOT);
52 if (ret < 0)
53 goto err;
54 if (ret == 0)
55 return 0;
56 sz = btr_pool_get_buffer(sit->btrp, &buf);
57 if (sz == 0)
58 return 0;
59 if (sit->must_set_nonblock_flag) {
60 ret = mark_fd_nonblocking(STDIN_FILENO);
61 if (ret < 0)
62 goto err;
63 sit->must_set_nonblock_flag = false;
64 }
65 /*
66 * Do not use the maximal size to avoid having only a single buffer
67 * reference for the whole pool. This is bad because if that single
68 * reference can not be freed, we're stuck.
69 */
70 sz = PARA_MIN(sz, btr_pool_size(sit->btrp) / 2);
71 ret = read_nonblock(STDIN_FILENO, buf, sz, &s->rfds, &n);
72 if (n > 0)
73 btr_add_output_pool(sit->btrp, n, sit->btrn);
74 if (ret >= 0)
75 return 0;
76 err:
77 btr_remove_node(&sit->btrn);
78 /* Revert to blocking mode if necessary. */
79 fcntl(STDIN_FILENO, F_SETFL, sit->fd_flags);
80 //btr_pool_free(sit->btrp);
81 return ret;
82 }
83
84 /**
85 * Register a stdin task structure.
86 *
87 * \param sit The stdin task structure to register.
88 * \param s The task will be added to this scheduler's task list.
89 *
90 * This allocates a buffer tree pool for I/O, sets up \a sit and registers a
91 * task with \a sit as context pointer.
92 */
93 void stdin_task_register(struct stdin_task *sit, struct sched *s)
94 {
95 int ret;
96 struct task_info ti = {
97 .name = "stdin",
98 .pre_select = stdin_pre_select,
99 .post_select = stdin_post_select,
100 .context = sit,
101 };
102
103 sit->btrp = btr_pool_new("stdin", 128 * 1024);
104 /*
105 * Both STDIN_FILENO and STDOUT_FILENO may refer to the same open file
106 * description (the terminal), and thus share the same file status
107 * flags. In order to not interfere with the stdout task, we only get
108 * the file status flags for STDIN here and save a copy. The nonblock
109 * flag is set later on the first read.
110 */
111 ret = fcntl(STDIN_FILENO, F_GETFL);
112 if (ret < 0) {
113 PARA_EMERG_LOG("F_GETFL: %s\n", strerror(errno));
114 exit(EXIT_FAILURE);
115 }
116 sit->fd_flags = ret;
117 sit->must_set_nonblock_flag = (sit->fd_flags & O_NONBLOCK) == 0
118 && !isatty(STDIN_FILENO);
119 sit->task = task_register(&ti, s);
120 }