oggdec: Fix a thinko in post_select().
[paraslash.git] / stdin.c
1 /*
2 * Copyright (C) 2006-2009 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 <dirent.h> /* readdir() */
10 #include <assert.h>
11 #include <stdbool.h>
12 #include <regex.h>
13
14 #include "para.h"
15 #include "list.h"
16 #include "sched.h"
17 #include "fd.h"
18 #include "error.h"
19 #include "stdin.h"
20 #include "buffer_tree.h"
21 #include "string.h"
22
23 /**
24 * The pre_select function of the stdin task.
25 *
26 * \param s The scheduler this task was registered to.
27 * \param t The task structure of the stdin task.
28 *
29 * This function is always successful. If there is space left in the
30 * buffer of the stdin task, it adds \p STDIN_FILENO to the read fd set
31 * of \a s.
32 */
33 static void stdin_pre_select(struct sched *s, struct task *t)
34 {
35 struct stdin_task *sit = container_of(t, struct stdin_task, task);
36 int ret;
37
38 t->error = 0;
39 ret = btr_node_status(sit->btrn, 0, BTR_NT_ROOT);
40 if (ret < 0)
41 sched_min_delay(s);
42 if (ret <= 0)
43 return;
44 if (btr_pool_unused(sit->btrp) > 0)
45 return para_fd_set(STDIN_FILENO, &s->rfds, &s->max_fileno);
46 sched_request_timeout_ms(100, s);
47 }
48
49 /**
50 * The post select function of the stdin task.
51 *
52 * \param s The scheduler this task was registered to.
53 * \param t The task structure of the stdin task.
54 *
55 * This function checks if \p STDIN_FILENO was included by in the read fd set
56 * of \a s during the previous pre_select call. If yes, and \p STDIN_FILENO
57 * appears to be readable, data is read from stdin and fed into the buffer
58 * tree.
59 */
60 static void stdin_post_select(struct sched *s, struct task *t)
61 {
62 struct stdin_task *sit = container_of(t, struct stdin_task, task);
63 ssize_t ret;
64 size_t sz;
65 char *buf = NULL;
66
67 t->error = 0;
68 ret = btr_node_status(sit->btrn, 0, BTR_NT_ROOT);
69 if (ret < 0)
70 goto err;
71 if (ret == 0)
72 return;
73 if (!FD_ISSET(STDIN_FILENO, &s->rfds))
74 return;
75 sz = btr_pool_get_buffer(sit->btrp, &buf);
76 if (sz == 0)
77 return;
78 /*
79 * Do not use the maximal size to avoid having only a single buffer
80 * reference for the whole pool. This is bad because if that single
81 * reference can not be freed, we're stuck.
82 */
83 sz = PARA_MIN(sz, btr_pool_size(sit->btrp) / 2);
84 ret = read(STDIN_FILENO, buf, sz);
85 if (ret < 0)
86 ret = -ERRNO_TO_PARA_ERROR(errno);
87 if (ret == 0)
88 ret = -E_STDIN_EOF;
89 if (ret < 0)
90 goto err;
91 btr_add_output_pool(sit->btrp, ret, sit->btrn);
92 return;
93 err:
94 btr_remove_node(sit->btrn);
95 //btr_pool_free(sit->btrp);
96 t->error = ret;
97 }
98
99 /**
100 * Initialize a stdin task structure with default values.
101 *
102 * \param sit The stdin task structure.
103 *
104 * This fills in the pre/post select function pointers of the task structure
105 * given by \a sit. Moreover, the stdin file desctiptor is set to nonblocking
106 * mode, and a buffer tree is created.
107 */
108 void stdin_set_defaults(struct stdin_task *sit)
109 {
110 int ret;
111
112 sit->task.pre_select = stdin_pre_select;
113 sit->task.post_select = stdin_post_select;
114 sit->btrp = btr_pool_new("stdin", 64 * 1024);
115 sprintf(sit->task.status, "stdin reader");
116 ret = mark_fd_nonblocking(STDIN_FILENO);
117 if (ret >= 0)
118 return;
119 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
120 exit(EXIT_FAILURE);
121 }