summaryrefslogtreecommitdiffstats
path: root/src/ioline.c
blob: ada9cee07e24ebecd48cb95740dc5ad8caab9e3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <errno.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>

#include "ioline.h"

#define BUFFER_LIMIT (64*1024)
#define READ_SIZE (1024)

struct ioline {
    struct iochannel *io;
    int dead;

    char *wbuf;
    size_t wbuf_length, wbuf_index, wbuf_valid_length;

    char *rbuf;
    size_t rbuf_length, rbuf_index, rbuf_valid_length;

    void (*callback)(struct ioline*io, const char *s, void *userdata);
    void *userdata;
};

static void io_callback(struct iochannel*io, void *userdata);
static int do_write(struct ioline *l);

struct ioline* ioline_new(struct iochannel *io) {
    struct ioline *l;
    assert(io);
    
    l = malloc(sizeof(struct ioline));
    assert(l);
    l->io = io;
    l->dead = 0;

    l->wbuf = NULL;
    l->wbuf_length = l->wbuf_index = l->wbuf_valid_length = 0;

    l->rbuf = NULL;
    l->rbuf_length = l->rbuf_index = l->rbuf_valid_length = 0;

    l->callback = NULL;
    l->userdata = NULL;

    iochannel_set_callback(io, io_callback, l);
    
    return l;
}

void ioline_free(struct ioline *l) {
    assert(l);
    iochannel_free(l->io);
    free(l->wbuf);
    free(l->rbuf);
    free(l);
}

void ioline_puts(struct ioline *l, const char *c) {
    size_t len;
    assert(l && c);
    
    len = strlen(c);
    if (len > BUFFER_LIMIT - l->wbuf_valid_length)
        len = BUFFER_LIMIT - l->wbuf_valid_length;

    if (!len)
        return;
    
    if (len > l->wbuf_length - l->wbuf_valid_length) {
        size_t n = l->wbuf_valid_length+len;
        char *new = malloc(n);
        if (l->wbuf) {
            memcpy(new, l->wbuf+l->wbuf_index, l->wbuf_valid_length);
            free(l->wbuf);
        }
        l->wbuf = new;
        l->wbuf_length = n;
        l->wbuf_index = 0;
    } else if (len > l->wbuf_length - l->wbuf_valid_length - l->wbuf_index) {
        memmove(l->wbuf, l->wbuf+l->wbuf_index, l->wbuf_valid_length);
        l->wbuf_index = 0;
    }

    memcpy(l->wbuf+l->wbuf_index+l->wbuf_valid_length, c, len);
    l->wbuf_valid_length += len;

    do_write(l);
}

void ioline_set_callback(struct ioline*l, void (*callback)(struct ioline*io, const char *s, void *userdata), void *userdata) {
    assert(l && callback);
    l->callback = callback;
    l->userdata = userdata;
}

static int do_read(struct ioline *l) {
    ssize_t r;
    size_t m, len;
    char *e;
    assert(l);

    if (!iochannel_is_readable(l->io))
        return 0;

    len = l->rbuf_length - l->rbuf_index - l->rbuf_valid_length;

    if (len < READ_SIZE) {
        size_t n = l->rbuf_valid_length+READ_SIZE;

        if (n >= BUFFER_LIMIT)
            n = BUFFER_LIMIT;
        
        if (l->rbuf_length >= n) {
            if (l->rbuf_valid_length)
                memmove(l->rbuf, l->rbuf+l->rbuf_index, l->rbuf_valid_length);
        } else {
            char *new = malloc(n);
            if (l->rbuf_valid_length)
                memcpy(new, l->rbuf+l->rbuf_index, l->rbuf_valid_length);
            free(l->rbuf);
            l->rbuf = new;
            l->rbuf_length = n;
        }
        
        l->rbuf_index = 0;
    }

    len = l->rbuf_length - l->rbuf_index - l->rbuf_valid_length;
    
    if ((r = iochannel_read(l->io, l->rbuf+l->rbuf_index+l->rbuf_valid_length, len)) <= 0)
        return -1;

    e = memchr(l->rbuf+l->rbuf_index+l->rbuf_valid_length, '\n', r);
    l->rbuf_valid_length += r;

    if (!e &&l->rbuf_valid_length >= BUFFER_LIMIT)
        e = l->rbuf+BUFFER_LIMIT-1;

    if (e) {
        char *p;
        
        *e = 0;
    
        p = l->rbuf+l->rbuf_index;
        m = strlen(p);

        if (l->callback)
            l->callback(l, p, l->userdata);

        l->rbuf_index += m+1;
        l->rbuf_valid_length -= m+1;

        if (l->rbuf_valid_length == 0)
            l->rbuf_index = 0;
    }

    return 0;
}

static int do_write(struct ioline *l) {
    ssize_t r;
    assert(l);

    if (!l->wbuf_valid_length || !iochannel_is_writable(l->io))
        return 0;
    
    if ((r = iochannel_write(l->io, l->wbuf+l->wbuf_index, l->wbuf_valid_length)) < 0)
        return -1;

    l->wbuf_valid_length -= r;
    if (l->wbuf_valid_length == 0)
        l->wbuf_index = 0;

    return 0;
}

static void io_callback(struct iochannel*io, void *userdata) {
    struct ioline *l = userdata;
    assert(io && l);
    
    if (!l->dead && do_read(l) < 0)
        goto fail;
    
    if (!l->dead && do_write(l) < 0)
        goto fail;
    
    return;

fail:
    l->dead = 1;
    if (l->callback)
        l->callback(l, NULL, l->userdata);
}