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_op *wake_writer(struct io_conn *, struct stdin_buffer *);
36 * // This writes the stdin buffer to the child.
37 * static struct io_op *write_to_child(struct io_conn *c,
38 * struct stdin_buffer *b);
39 * static struct io_op *read_stdin(struct io_conn *c, struct stdin_buffer *b)
41 * assert(c == b->reader);
42 * b->len = sizeof(b->inbuf);
43 * return io_read_partial(b->inbuf, &b->len,
44 * io_next(c, wake_writer, b));
47 * static struct io_op *wake_writer(struct io_conn *c, struct stdin_buffer *b)
49 * assert(c == b->reader);
50 * io_wake(b->writer, write_to_child, b);
54 * static void reader_exit(struct io_conn *c, struct stdin_buffer *b)
56 * assert(c == b->reader);
57 * io_wake(b->writer, write_to_child, b);
61 * static struct io_op *wake_reader(struct io_conn *c, struct stdin_buffer *b)
63 * assert(c == b->writer);
64 * io_wake(b->reader, read_stdin, b);
68 * static struct io_op *write_to_child(struct io_conn *conn,
69 * struct stdin_buffer *b)
71 * assert(conn == b->writer);
73 * return io_close(conn, NULL);
74 * return io_write(b->inbuf, b->len, io_next(conn, wake_reader, b));
77 * static struct io_op *start_writer(struct io_conn *conn,
78 * struct stdin_buffer *b)
80 * assert(conn == b->writer);
81 * return io_idle(conn);
84 * static void fail_child_write(struct io_conn *conn, struct stdin_buffer *b)
87 * err(1, "Failed writing to child.");
90 * // This reads from the child and saves it into buffer.
91 * static struct io_op *read_from_child(struct io_conn *conn,
96 * if (b->off == b->max) {
99 * else if (b->max >= 1024*1024)
100 * b->max += 1024*1024;
103 * b->buf = realloc(b->buf, b->max);
106 * b->rlen = b->max - b->off;
107 * return io_read_partial(b->buf + b->off, &b->rlen,
108 * io_next(conn, read_from_child, b));
111 * // Feed a program our stdin, gather its stdout, print that at end.
112 * int main(int argc, char *argv[])
114 * int tochild[2], fromchild[2];
115 * struct buffer out = { 0, 0, 0, NULL };
116 * struct stdin_buffer sbuf;
122 * errx(1, "Usage: runner <cmdline>...");
124 * if (pipe(tochild) != 0 || pipe(fromchild) != 0)
125 * err(1, "Creating pipes");
128 * // Child runs command.
130 * close(fromchild[0]);
132 * dup2(tochild[0], STDIN_FILENO);
133 * dup2(fromchild[1], STDOUT_FILENO);
134 * execvp(argv[1], argv + 1);
139 * close(fromchild[1]);
140 * signal(SIGPIPE, SIG_IGN);
142 * sbuf.reader = io_new_conn(STDIN_FILENO, read_stdin, reader_exit, &sbuf);
143 * sbuf.writer = io_new_conn(tochild[1], start_writer, fail_child_write,
145 * if (!sbuf.reader || !sbuf.writer
146 * || !io_new_conn(fromchild[0], read_from_child, NULL, &out))
147 * err(1, "Allocating connections");
152 * for (off = 0; off < out.off; off += ret) {
153 * ret = write(STDOUT_FILENO, out.buf+off, out.off-off);
155 * err(1, "Writing stdout");
159 * return WIFEXITED(status) ? WEXITSTATUS(status) : 2;
164 int main(int argc, char *argv[])
169 if (strcmp(argv[1], "depends") == 0) {