-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinereader.h
141 lines (122 loc) · 3.07 KB
/
linereader.h
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
/* Easy to use line reader
*
* This Works is placed under the terms of the Copyright Less License,
* see file COPYRIGHT.CLL. USE AT OWN RISK, ABSOLUTELY NO WARRANTY.
*/
#include "oops.h"
#ifndef LINEREADER_MAX
#define LINEREADER_MAX BUFSIZ
#endif
struct linereader
{
int fd, pos, fill, eof, err;
int linenr;
char buf[LINEREADER_MAX], *line;
const char *name;
};
static void
linereader_init(struct linereader *l, const char *name)
{
memset(l, 0, sizeof *l);
l->fd = -1;
l->name = name;
}
/* Close the linereader and get the error state */
static int
linereader_end(struct linereader *l)
{
if (l->fd >= 0)
{
if (closex(l->fd))
l->err = errno;
l->fd = -1;
}
if (l->err)
errno = l->err;
return l->err;
}
/* struct linereader l;
* linereader_init(&l, "FILENAME");
* while (linereader(&l))
* process(l.line, l.linenr);
* if (linereader_end(&l))
* error(errno);
*
* This no more OOPSes now.
* Instead it ERROR()s and sets l->err and returns EOF on following:
* - Stray NUL
* - Line too long
* - too many loops (EINTR or broken reads)
*/
static char *
linereader(struct linereader *l)
{
int i, loop;
if (l->fd<0 && (l->fd=open(l->name, O_RDONLY))<0)
{
l->err = errno;
return 0;
}
for (loop=MAXLOOPS; --loop>=0; )
{
int tmp;
if (l->pos >= l->fill)
{
l->pos = 0;
l->fill = 0;
}
FATAL(l->pos<0 || l->fill<0 || l->pos>sizeof l->buf || l->fill>sizeof l->buf);
/* hunt for complete line */
for (i=l->pos; i<l->fill; i++)
switch (l->buf[i])
{
case '\n':
tmp = l->pos;
l->pos = i+1;
l->buf[i] = 0; /* NUL terminated line */
l->linenr++;
return l->line = l->buf+tmp; /* return line */
case 0:
STDERR(l->name, OOPS_I, l->linenr+1, "contains stray NUL", NULL);
l->err = EILSEQ; /* Illegal byte sequence */
return 0;
}
/* free no more needed buffer space */
if (l->pos)
{
/* l->pos < l->fill, see above */
memmove(l->buf, l->buf + l->pos, l->fill - l->pos);
l->fill -= l->pos;
l->pos = 0;
}
/* l->pos == 0 */
if (l->fill >= sizeof l->buf)
{
STDERR(l->name, OOPS_I, l->linenr+1, "line too long", NULL);
l->err = EOVERFLOW;
return 0;
}
if (l->eof)
{
/* l->fill < sizeof l->buf */
l->buf[l->pos = l->fill] = 0;
return l->fill ? l->buf : 0; /* return last line without LF, or EOF */
}
/* read more data */
/* l->fill < sizeof l->buf */
tmp = read(l->fd, l->buf + l->fill, (sizeof l->buf) - l->fill);
if (tmp<0)
{
if (errno==EINTR)
continue; /* ignore EINTR */
l->err = errno;
return 0;
}
l->fill += tmp;
if (!tmp)
l->eof = 1;
}
STDERR(l->name, OOPS_I, l->linenr+1, "too many interrupts", NULL);
l->err = EINTR;
return 0;
}