6 * io - simple library for asynchronous io handling.
8 * io provides a mechanism to write I/O servers with multiple
9 * connections. Each callback indicates what I/O they plan next
10 * (eg. read, write). It is also possible to write custom I/O
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>
26 * size_t start, end, rlen, wlen;
30 * static void finish(struct io_conn *c, struct buffer *b)
32 * // Mark us finished.
34 * // Wake writer just in case it's asleep.
38 * static struct io_plan *read_in(struct io_conn *c, struct buffer *b)
40 * // Add what we just read.
42 * assert(b->end <= sizeof(b->buf));
44 * // If we just read something, wake writer.
48 * // If buffer is empty, return to start.
49 * if (b->start == b->end)
50 * b->start = b->end = 0;
52 * // No room? Wait for writer
53 * if (b->end == sizeof(b->buf))
54 * return io_wait(c, b, read_in, b);
56 * return io_read_partial(c, b->buf + b->end, sizeof(b->buf) - b->end,
57 * &b->rlen, read_in, b);
60 * static struct io_plan *write_out(struct io_conn *c, struct buffer *b)
62 * // Remove what we just wrote.
63 * b->start += b->wlen;
64 * assert(b->start <= sizeof(b->buf));
66 * // If we wrote something, wake writer.
70 * // Nothing to write? Wait for reader.
71 * if (b->end == b->start) {
74 * return io_wait(c, b, write_out, b);
77 * return io_write_partial(c, b->buf + b->start, b->end - b->start,
78 * &b->wlen, write_out, b);
81 * // Feed a program our stdin, gather its stdout, print that at end.
82 * int main(int argc, char *argv[])
84 * int tochild[2], fromchild[2];
85 * struct buffer to, from;
87 * struct io_conn *reader;
90 * errx(1, "Usage: runner <cmdline>...");
92 * if (pipe(tochild) != 0 || pipe(fromchild) != 0)
93 * err(1, "Creating pipes");
96 * // Child runs command.
98 * close(fromchild[0]);
100 * dup2(tochild[0], STDIN_FILENO);
101 * dup2(fromchild[1], STDOUT_FILENO);
102 * execvp(argv[1], argv + 1);
107 * close(fromchild[1]);
108 * signal(SIGPIPE, SIG_IGN);
110 * // Read from stdin, write to child.
111 * memset(&to, 0, sizeof(to));
112 * reader = io_new_conn(NULL, STDIN_FILENO, read_in, &to);
113 * io_set_finish(reader, finish, &to);
114 * io_new_conn(NULL, tochild[1], write_out, &to);
116 * // Read from child, write to stdout.
117 * reader = io_new_conn(NULL, fromchild[0], read_in, &from);
118 * io_set_finish(reader, finish, &from);
119 * io_new_conn(NULL, STDOUT_FILENO, write_out, &from);
121 * io_loop(NULL, NULL);
124 * return WIFEXITED(status) ? WEXITSTATUS(status) : 2;
127 * License: LGPL (v2.1 or any later version)
128 * Author: Rusty Russell <rusty@rustcorp.com.au>
130 int main(int argc, char *argv[])
135 if (strcmp(argv[1], "depends") == 0) {
136 printf("ccan/container_of\n");
137 printf("ccan/list\n");
138 printf("ccan/tal\n");
139 printf("ccan/time\n");
140 printf("ccan/timer\n");
141 printf("ccan/typesafe_cb\n");