6 * io - simple library for stateful io handling.
8 * io provides a simple mechanism to write I/O servers with multiple
9 * connections. Handling of connections is multiplexed, and function
10 * indicate what they want written or read, and what follow-on
11 * function to call on success (or failure).
14 * // Given tr A-Z a-z outputs tr a-z a-z
15 * #include <ccan/io/io.h>
16 * #include <ccan/err/err.h>
20 * #include <sys/types.h>
21 * #include <sys/wait.h>
24 * size_t max, off, rlen;
28 * struct stdin_buffer {
29 * struct io_conn *reader, *writer;
34 * // This reads from stdin.
35 * static struct io_plan wake_writer(struct io_conn *, struct stdin_buffer *);
36 * // This writes the stdin buffer to the child.
37 * static struct io_plan wake_reader(struct io_conn *, struct stdin_buffer *);
39 * static struct io_plan wake_writer(struct io_conn *c, struct stdin_buffer *b)
41 * assert(c == b->reader);
42 * io_wake(b->writer, io_write(b->inbuf, b->len, wake_reader, b));
46 * static void reader_exit(struct io_conn *c, struct stdin_buffer *b)
48 * assert(c == b->reader);
49 * io_wake(b->writer, io_close());
53 * static struct io_plan wake_reader(struct io_conn *c, struct stdin_buffer *b)
55 * assert(c == b->writer);
58 * b->len = sizeof(b->inbuf);
59 * io_wake(b->reader, io_read_partial(b->inbuf, &b->len, wake_writer, b));
63 * static void fail_child_write(struct io_conn *conn, struct stdin_buffer *b)
66 * err(1, "Failed writing to child.");
69 * // This reads from the child and saves it into buffer.
70 * static struct io_plan read_from_child(struct io_conn *conn,
75 * if (b->off == b->max)
76 * b->buf = realloc(b->buf, b->max *= 2);
78 * b->rlen = b->max - b->off;
79 * return io_read_partial(b->buf + b->off, &b->rlen, read_from_child, b);
82 * // Feed a program our stdin, gather its stdout, print that at end.
83 * int main(int argc, char *argv[])
85 * int tochild[2], fromchild[2];
87 * struct stdin_buffer sbuf;
91 * struct io_conn *from_child;
94 * errx(1, "Usage: runner <cmdline>...");
96 * if (pipe(tochild) != 0 || pipe(fromchild) != 0)
97 * err(1, "Creating pipes");
100 * // Child runs command.
102 * close(fromchild[0]);
104 * dup2(tochild[0], STDIN_FILENO);
105 * dup2(fromchild[1], STDOUT_FILENO);
106 * execvp(argv[1], argv + 1);
111 * close(fromchild[1]);
112 * signal(SIGPIPE, SIG_IGN);
114 * sbuf.len = sizeof(sbuf.inbuf);
115 * sbuf.reader = io_new_conn(STDIN_FILENO,
116 * io_read_partial(sbuf.inbuf, &sbuf.len,
117 * wake_writer, &sbuf));
118 * sbuf.writer = io_new_conn(tochild[1], io_idle());
123 * out.buf = malloc(out.max);
124 * from_child = io_new_conn(fromchild[0],
125 * io_read_partial(out.buf, &out.rlen,
126 * read_from_child, &out));
127 * if (!sbuf.reader || !sbuf.writer || !from_child)
128 * err(1, "Allocating connections");
130 * io_set_finish(sbuf.reader, reader_exit, &sbuf);
131 * io_set_finish(sbuf.writer, fail_child_write, &sbuf);
136 * for (off = 0; off < out.off; off += ret) {
137 * ret = write(STDOUT_FILENO, out.buf+off, out.off-off);
139 * err(1, "Writing stdout");
143 * return WIFEXITED(status) ? WEXITSTATUS(status) : 2;
146 * License: LGPL (v2.1 or any later version)
147 * Author: Rusty Russell <rusty@rustcorp.com.au>
149 int main(int argc, char *argv[])
154 if (strcmp(argv[1], "depends") == 0) {
155 printf("ccan/time\n");
156 printf("ccan/timer\n");