-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathinput_buffer.c
53 lines (43 loc) · 1.35 KB
/
input_buffer.c
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
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "input_buffer.h"
struct user_iobuf *create_userbuf() {
struct user_iobuf *b;
b = malloc(sizeof(struct user_iobuf));
if (!b) return NULL;
b->buf = malloc(USERBUF_SIZE + 1);
if (!b->buf) {
free(b);
return NULL;
}
b->cur = 0;
bzero(b->buf, USERBUF_SIZE+1);
return b;
}
void process_user_input(int fd, struct user_iobuf *userbuf,
void (*handle_line)(char *, void *), void *cbdata)
{
int nread;
char *ret;
assert(userbuf != NULL);
assert(userbuf->buf != NULL);
/* A real program would propagate this error back to the select loop or
* implement some other form of error handling */
if (userbuf->cur >= (USERBUF_SIZE - 1)) {
fprintf(stderr, "process_user_input error: buffer full; line too long!\n");
exit(-1);
}
nread = read(fd, userbuf->buf + userbuf->cur,
(USERBUF_SIZE - userbuf->cur));
if (nread > 0) {
userbuf->cur += nread;
}
while ((ret = strchr(userbuf->buf, '\n')) != NULL) {
*ret = '\0';
handle_line(userbuf->buf, cbdata);
/* Shift the remaining contents of the buffer forward */
memmove(userbuf->buf, ret + 1, USERBUF_SIZE - (ret - userbuf->buf));
userbuf->cur -= (ret - userbuf->buf + 1);
}
}