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