Home | History | Annotate | Line # | Download | only in uvtee
main.c revision 1.1
      1  1.1  christos #include <stdio.h>
      2  1.1  christos #include <fcntl.h>
      3  1.1  christos #include <unistd.h>
      4  1.1  christos #include <string.h>
      5  1.1  christos #include <stdlib.h>
      6  1.1  christos 
      7  1.1  christos #include <uv.h>
      8  1.1  christos 
      9  1.1  christos typedef struct {
     10  1.1  christos     uv_write_t req;
     11  1.1  christos     uv_buf_t buf;
     12  1.1  christos } write_req_t;
     13  1.1  christos 
     14  1.1  christos uv_loop_t *loop;
     15  1.1  christos uv_pipe_t stdin_pipe;
     16  1.1  christos uv_pipe_t stdout_pipe;
     17  1.1  christos uv_pipe_t file_pipe;
     18  1.1  christos 
     19  1.1  christos void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) {
     20  1.1  christos     *buf = uv_buf_init((char*) malloc(suggested_size), suggested_size);
     21  1.1  christos }
     22  1.1  christos 
     23  1.1  christos void free_write_req(uv_write_t *req) {
     24  1.1  christos     write_req_t *wr = (write_req_t*) req;
     25  1.1  christos     free(wr->buf.base);
     26  1.1  christos     free(wr);
     27  1.1  christos }
     28  1.1  christos 
     29  1.1  christos void on_stdout_write(uv_write_t *req, int status) {
     30  1.1  christos     free_write_req(req);
     31  1.1  christos }
     32  1.1  christos 
     33  1.1  christos void on_file_write(uv_write_t *req, int status) {
     34  1.1  christos     free_write_req(req);
     35  1.1  christos }
     36  1.1  christos 
     37  1.1  christos void write_data(uv_stream_t *dest, size_t size, uv_buf_t buf, uv_write_cb cb) {
     38  1.1  christos     write_req_t *req = (write_req_t*) malloc(sizeof(write_req_t));
     39  1.1  christos     req->buf = uv_buf_init((char*) malloc(size), size);
     40  1.1  christos     memcpy(req->buf.base, buf.base, size);
     41  1.1  christos     uv_write((uv_write_t*) req, (uv_stream_t*)dest, &req->buf, 1, cb);
     42  1.1  christos }
     43  1.1  christos 
     44  1.1  christos void read_stdin(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) {
     45  1.1  christos     if (nread < 0){
     46  1.1  christos         if (nread == UV_EOF){
     47  1.1  christos             // end of file
     48  1.1  christos             uv_close((uv_handle_t *)&stdin_pipe, NULL);
     49  1.1  christos             uv_close((uv_handle_t *)&stdout_pipe, NULL);
     50  1.1  christos             uv_close((uv_handle_t *)&file_pipe, NULL);
     51  1.1  christos         }
     52  1.1  christos     } else if (nread > 0) {
     53  1.1  christos         write_data((uv_stream_t *)&stdout_pipe, nread, *buf, on_stdout_write);
     54  1.1  christos         write_data((uv_stream_t *)&file_pipe, nread, *buf, on_file_write);
     55  1.1  christos     }
     56  1.1  christos 
     57  1.1  christos     // OK to free buffer as write_data copies it.
     58  1.1  christos     if (buf->base)
     59  1.1  christos         free(buf->base);
     60  1.1  christos }
     61  1.1  christos 
     62  1.1  christos int main(int argc, char **argv) {
     63  1.1  christos     loop = uv_default_loop();
     64  1.1  christos 
     65  1.1  christos     uv_pipe_init(loop, &stdin_pipe, 0);
     66  1.1  christos     uv_pipe_open(&stdin_pipe, 0);
     67  1.1  christos 
     68  1.1  christos     uv_pipe_init(loop, &stdout_pipe, 0);
     69  1.1  christos     uv_pipe_open(&stdout_pipe, 1);
     70  1.1  christos 
     71  1.1  christos     uv_fs_t file_req;
     72  1.1  christos     int fd = uv_fs_open(loop, &file_req, argv[1], O_CREAT | O_RDWR, 0644, NULL);
     73  1.1  christos     uv_pipe_init(loop, &file_pipe, 0);
     74  1.1  christos     uv_pipe_open(&file_pipe, fd);
     75  1.1  christos 
     76  1.1  christos     uv_read_start((uv_stream_t*)&stdin_pipe, alloc_buffer, read_stdin);
     77  1.1  christos 
     78  1.1  christos     uv_run(loop, UV_RUN_DEFAULT);
     79  1.1  christos     return 0;
     80  1.1  christos }
     81