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
|
/* See LICENSE file for copyright and license details. */
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#if defined(__clang__)
# pragma clang diagnostic ignored "-Wunsafe-buffer-usage" /* broken */
# pragma clang diagnostic ignored "-Wimplicit-void-ptr-cast" /* C++ */
# pragma clang diagnostic ignored "-Wpre-c11-compat" /* C++ */
# pragma clang diagnostic ignored "-Wdisabled-macro-expansion" /* libc */
#endif
#define ULTIMATE_CODEPOINT 0x10FFFFul
static const char *argv0;
static void parse_line(char *text, size_t lineno);
static int output(void);
static size_t
parse(char *buf, size_t len)
{
static int prev_was_cr = 0;
static size_t lineno = 1u;
size_t ret = 0;
size_t off = 0;
size_t she;
beginning:
if (prev_was_cr && off < len && buf[off] == '\n')
off++;
while (off < len && buf[off] != '\n' && buf[off] != '\r' && isspace(buf[off]))
off++;
ret = off;
if (off == len)
return ret;
if (buf[off] == '#') {
while (off < len && buf[off] != '\n' && buf[off] != '\r')
off++;
if (off == len)
return ret;
goto newline;
} else if (buf[off] == '\n' || buf[off] == '\r') {
newline:
prev_was_cr = buf[off] == '\r';
off++;
lineno++;
goto beginning;
}
for (; off < len; off++) {
if (buf[off] == '\0') {
fprintf(stderr, "%s: NUL byte found in <stdin> on line %zu\n", argv0, lineno);
exit(1);
} else if (buf[off] == '\n' || buf[off] == '\r' || buf[off] == '#') {
break;
}
}
if (off == len)
return ret;
if (buf[off] == '\n' || buf[off] == '\r') {
prev_was_cr = buf[off] == '\r';
buf[off++] = '\0';
} else if (buf[off] == '#') {
she = off++;
while (off < len && buf[off] != '\n' && buf[off] != '\r')
off++;
if (off == len)
return ret;
buf[she] = '\0';
prev_was_cr = buf[off++] == '\r';
} else {
abort();
}
parse_line(&buf[ret], lineno);
lineno++;
goto beginning;
}
int
main(int argc, char *argv[])
{
char *buf = NULL;
size_t bufsize = 0;
size_t len = 0;
size_t parsed;
ssize_t r;
argv0 = argv[0];
(void) argc;
for (;;) {
if (len == bufsize) {
buf = realloc(buf, bufsize += 8192u);
if (!buf) {
fprintf(stderr, "%s: realloc %zu: %s\n", argv0, bufsize, strerror(errno));
exit(1);
}
}
r = read(STDIN_FILENO, &buf[len], bufsize - len);
if (r <= 0) {
if (!r)
break;
if (errno == EINTR)
continue;
fprintf(stderr, "%s: read <stdin>: %s\n", argv0, strerror(errno));
exit(1);
}
len += (size_t)r;
parsed = parse(buf, len);
memmove(&buf[0], &buf[parsed], len -= parsed);
}
buf[len++] = '\n';
parse(buf, len);
free(buf);
if (output() < 0 || fflush(stdout)) {
fprintf(stderr, "%s: failed to write to <stdout>\n", argv0);
exit(1);
}
return 0;
}
|