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