summaryrefslogtreecommitdiffstats
path: root/tailburst.c
blob: b5b919e17bd7f8e6ee19e94ca2a70a43fb5eae69 (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
#include <stdio.h>
#include <fcntl.h>
#include <poll.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>

#define BUFLEN 1024
#define DEBUG 0
#define pr_debug(fmt, args...) if (DEBUG) printf(fmt, ##args)

struct getline_states {
	char *base;
	char *cur;
	size_t size;
	size_t read;
	char *next;
};

ssize_t fdgetline(int fd, struct getline_states *line) {
	char *str;
	ssize_t nread;

	if (line->base == NULL) {
		/* First pass: init state */
		line->size = 120;
		line->base = malloc(line->size);
		if (line->base == NULL)
			return -ENOMEM;
		line->base[0] = '\0';
		line->read = 0;
		line->next = line->base;
		line->cur = NULL;
	}

	/* Do we already have a full line in buffer? */
	if (line->read > 0 && (str = strchr(line->next, '\n'))) {
		line->cur = line->next;
		line->next = str+1;
		str[0] = '\0'; 
		pr_debug("next already buffered, %p %p: %s\n", str, line->cur, line->cur);
		return 1;
	}

	/* No such luck, move current to base and read what we can */
	if (line->cur && line->cur != line->base) {
		pr_debug("moving cur: %p, base: %p, read: %zd, moving %zd\n",
			 line->cur, line->base, line->read, line->cur - line->base);
		line->read -= line->cur - line->base;
		line->next -= line->cur - line->base;
		memmove(line->base, line->cur, line->read + 1); /* also move trailing \0 */
		line->cur = line->base;
	}

	/* search from here */
	while (1) {
		errno = 0;
		nread = read(fd, line->base + line->read, line->size - line->read - 1);
		if (nread < 0) {
			if (errno == EAGAIN)
				return 0;
			break;
		}
		if (nread == 0) {
			/* EOF */
			return -1;
		}
		pr_debug("read %zd\n", nread);
		line->read += nread;
		line->base[line->read] = '\0';

		/* Check if we have a full line now */
		if ((str = strchr(line->next, '\n'))) {
			line->cur = line->next;
			line->next = str + 1;
			str[0] = '\0'; 
			pr_debug("found stuff, %p %p %p: %s\n", str, line->base, line->cur, line->cur);
			return 1;
		}

		/* realloc bigger buffer and try again if sensible */
		if (line->size - line->read > 100)
			continue;
		if (line->size > 1024) {
			/* pretend this was a full line.. */
			line->cur = line->next;
			line->next = line->base + line->read;
			return 1;
		}
		line->size *= 2;
		pr_debug("growing buffer to %zd\n", line->size);

		char *newbase = realloc(line->base, line->size);
		if (newbase == NULL)
			return -ENOMEM;
		if (line->cur) {
			pr_debug("base-cur was %p-%p, new %p-%p\n", line->base, line->cur, newbase, newbase + (line->cur - line->base));
			line->cur = newbase + (line->cur - line->base);
		}
		line->next = newbase + (line->next - line->base);
		line->base = newbase;
	}
	pr_debug("read failed? %zd %d\n", nread, errno);

	if (errno)
		return -errno;
	
	return -1; /* unknown error? */
}


int main(int argc, char **argv)
{
	int flags, fd = 0;
	ssize_t nread;
	struct pollfd pollfd;
	struct getline_states line = { 0 };

	/* non-buffered output */
	setlinebuf(stdout);

	/* make stream non-block */
	flags = fcntl(fd, F_GETFL);
	flags |= O_NONBLOCK;
	fcntl(fd, F_SETFL, flags);

	pollfd.fd = fd;
	pollfd.events = POLLIN;
	while (poll(&pollfd, 1, -1) > 0) {
		line.cur = NULL;
		while ((nread = fdgetline(fd, &line)) > 0);

		if (line.cur)
			printf("%s\n", line.cur);

		/* eof or error */
		if (nread < 0)
			break;

		/* throttle in case something tries to wake us up every few ms... */
		sleep(1);
	}

	return 0;
}