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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
/* ISC license. */
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <skalibs/uint64.h>
#include <skalibs/sgetopt.h>
#include <skalibs/buffer.h>
#include <skalibs/strerr2.h>
#include <skalibs/stralloc.h>
#include <skalibs/genalloc.h>
#include <skalibs/direntry.h>
#include <skalibs/djbunix.h>
#include <skalibs/skamisc.h>
#include <skalibs/avltree.h>
#define USAGE "s6-frontend-config-preprocess file"
#define dieusage() strerr_dieusage(100, USAGE)
#define dienomem() strerr_diefu1sys(111, "stralloc_catb") ;
#define MAXDEPTH 100
static stralloc sa = STRALLOC_ZERO ;
static unsigned int depth = 0 ;
/* Name storage */
static stralloc namesa = STRALLOC_ZERO ;
static void *name_dtok (uint32_t pos, void *aux)
{
(void)aux ;
return namesa.s + pos ;
}
static int name_cmp (void const *a, void const *b, void *aux)
{
(void)aux ;
return strcmp((char const *)a, (char const *)b) ;
}
static avltree namemap = AVLTREE_INIT(8, 3, 8, &name_dtok, &name_cmp, 0) ;
/* Directory sorting */
static char *dname_cmp_base ;
static int dname_cmp (void const *a, void const *b)
{
return strcmp(dname_cmp_base + *(size_t *)a, dname_cmp_base + *(size_t *)b) ;
}
/* Recursive inclusion functions */
static int includefromhere (char const *, int) ;
static inline void includecwd (int once)
{
genalloc ga = GENALLOC_ZERO ; /* size_t */
DIR *dir ;
size_t sabase = sa.len ;
if (sagetcwd(&sa) < 0 || !stralloc_0(&sa)) dienomem() ;
dir = opendir(".") ;
if (!dir) strerr_diefu2sys(111, "opendir ", sa.s + sabase) ;
for (;;)
{
direntry *d ;
errno = 0 ;
d = readdir(dir) ;
if (!d) break ;
if (d->d_name[0] == '.') continue ;
if (!genalloc_catb(size_t, &ga, &sa.len, 1)) break ;
if (!stralloc_catb(&sa, d->d_name, strlen(d->d_name)+1)) break ;
}
dir_close(dir) ;
if (errno) strerr_diefu2sys(111, "readdir ", sa.s + sabase) ;
dname_cmp_base = sa.s ;
qsort(genalloc_s(size_t, &ga), genalloc_len(size_t, &ga), sizeof(size_t), &dname_cmp) ;
for (size_t i = 0 ; i < genalloc_len(size_t, &ga) ; i++)
if (!includefromhere(sa.s + genalloc_s(size_t, &ga)[i], once))
strerr_dief4sys(1, "in ", sa.s + sabase, ": unable to include ", sa.s + genalloc_s(size_t, &ga)[i]) ;
genalloc_free(size_t, &ga) ;
sa.len = sabase ;
}
static int include (char const *file, int once)
{
size_t sabase = sa.len ;
size_t filelen = strlen(file) ;
if (!sadirname(&sa, file, filelen) || !stralloc_0(&sa)) dienomem() ;
if (chdir(sa.s + sabase) < 0) return 0 ;
sa.len = sabase ;
if (!sabasename(&sa, file, filelen)) dienomem() ;
{
char fn[sa.len + 1 - sabase] ;
memcpy(fn, sa.s + sabase, sa.len - sabase) ;
fn[sa.len - sabase] = 0 ;
sa.len = sabase ;
return includefromhere(fn, once) ;
}
}
static inline int idcmd (char const *s)
{
static char const *commands[] =
{
"include",
"includeonce",
"includedir",
"includedironce",
0
} ;
for (char const **p = commands ; *p ; p++)
if (!strcmp(s, *p)) return p - commands ;
return -1 ;
}
static inline unsigned char cclass (char c)
{
static unsigned char const classtable[34] = "0444444443144344444444444444444432" ;
return (unsigned char)c < 34 ? classtable[(unsigned char)c] - '0' : 4 ;
}
static int includefromhere (char const *file, int once)
{
size_t sabase = sa.len ;
if (sarealpath(&sa, file) < 0 || !stralloc_0(&sa)) return 0 ;
if (once)
{
uint32_t dummy ;
if (avltree_search(&namemap, sa.s + sabase, &dummy)) goto end ;
{
size_t cur = namesa.len ;
size_t filelen = strlen(file) ;
if (cur + filelen + 2 < cur || cur + filelen + 2 > UINT32_MAX)
strerr_dief3x(3, "in ", sa.s + sabase, ": too much nesting") ;
if (!stralloc_catb(&namesa, sa.s + sabase, sa.len - sabase)) dienomem() ;
if (!avltree_insert(&namemap, cur)) dienomem() ;
}
}
if (depth++ > MAXDEPTH)
strerr_dief3x(3, "in ", sa.s + sabase, ": too much nesting") ;
{
static unsigned char const table[8][5] =
{
{ 0x08, 0x10, 0x02, 0x10, 0x10 },
{ 0x08, 0x10, 0x11, 0x11, 0x11 },
{ 0x08, 0x00, 0x03, 0x04, 0x25 },
{ 0x08, 0x00, 0x03, 0x03, 0x03 },
{ 0x09, 0x09, 0x09, 0x04, 0x25 },
{ 0x09, 0x09, 0x09, 0x46, 0x25 },
{ 0x0a, 0x0a, 0x07, 0x06, 0x27 },
{ 0x88, 0x80, 0x27, 0x27, 0x27 }
} ;
stralloc localsa = STRALLOC_ZERO ;
uint64_t line = 1 ;
size_t sacur = sa.len ;
int cmd = -1 ;
buffer b ;
char buf[4096] ;
unsigned char state = 0 ;
int fd = open_readb(file) ;
if (fd < 0) strerr_diefu2sys(111, "open ", sa.s + sabase) ;
buffer_init(&b, &buffer_read, fd, buf, 4096) ;
if (buffer_put(buffer_1, "! 0 ", 4) < 4
|| buffer_put(buffer_1, sa.s + sabase, sacur - 1 - sabase) < sacur - 1 - sabase
|| buffer_put(buffer_1, "\n", 1) < 1)
strerr_diefu1sys(111, "write to stdout") ;
while (state < 8)
{
uint16_t what ;
char c = 0 ;
if (buffer_get(&b, &c, 1) < 0)
strerr_diefu2sys(111, "read from ", sa.s + sabase) ;
what = table[state][cclass((unsigned char)c)] ;
state = what & 0x000f ;
if (what & 0x0010)
if (buffer_put(buffer_1, &c, 1) < 1)
strerr_diefu1sys(111, "write to stdout") ;
if (what & 0x0020)
if (!stralloc_catb(&localsa, &c, 1)) dienomem() ;
if (what & 0x0040)
{
if (!stralloc_0(&localsa)) dienomem() ;
cmd = idcmd(localsa.s) ;
if (cmd < 0)
{
char fmt[UINT64_FMT] ;
fmt[uint64_fmt(fmt, line)] = 0 ;
strerr_dief6x(2, "in ", sa.s + sabase, " line ", fmt, ": unrecognized directive: ", localsa.s) ;
}
localsa.len = 0 ;
}
if (what & 0x0080)
{
int fdhere = open(".", O_RDONLY | O_DIRECTORY) ;
if (fdhere < 0)
strerr_dief3sys(111, "in ", sa.s + sabase, ": unable to open base directory: ") ;
if (!stralloc_0(&localsa)) dienomem() ;
if (cmd & 2)
{
if (chdir(localsa.s) < 0)
{
char fmt[UINT64_FMT] ;
fmt[uint64_fmt(fmt, line)] = 0 ;
strerr_dief6sys(111, "in ", sa.s + sabase, " line ", fmt, ": unable to chdir to ", localsa.s) ;
}
includecwd(cmd & 1) ;
}
else if (!include(localsa.s, cmd & 1))
{
char fmt[UINT64_FMT] ;
fmt[uint64_fmt(fmt, line)] = 0 ;
strerr_dief6sys(111, "in ", sa.s + sabase, " line ", fmt, ": unable to include ", localsa.s) ;
}
if (fchdir(fdhere) < 0)
strerr_dief3sys(111, "in ", sa.s + sabase, ": unable to fchdir back") ;
fd_close(fdhere) ;
localsa.len = 0 ;
{
char fmt[UINT64_FMT] ;
size_t fmtlen = uint64_fmt(fmt, line) ;
fmt[fmtlen++] = ' ' ;
if (buffer_put(buffer_1, "! ", 2) < 2
|| buffer_put(buffer_1, fmt, fmtlen) < fmtlen
|| buffer_put(buffer_1, sa.s + sabase, sacur - 1 - sabase) < sacur - 1 - sabase
|| buffer_put(buffer_1, "\n", 1) < 1)
strerr_diefu1sys(111, "write to stdout") ;
}
}
if (c == '\n' && state <= 8) line++ ;
}
if (state > 8)
{
char fmt[UINT64_FMT] ;
fmt[uint64_fmt(fmt, line)] = 0 ;
strerr_dief5x(2, "in ", sa.s + sabase, " line ", fmt, ": syntax error: invalid ! line") ;
}
fd_close(fd) ;
stralloc_free(&localsa) ;
}
depth-- ;
end:
sa.len = sabase ;
return 1 ;
}
int main (int argc, char const *const *argv, char const *const *envp)
{
PROG = "s6-frontend-config-preprocess" ;
{
subgetopt l = SUBGETOPT_ZERO ;
for (;;)
{
int opt = subgetopt_r(argc, argv, "", &l) ;
if (opt == -1) break ;
switch (opt)
{
default : dieusage() ;
}
}
argc -= l.ind ; argv += l.ind ;
}
if (!argc) dieusage() ;
if (!include(argv[0], 1)) strerr_diefu2sys(1, "preprocess ", argv[0]) ;
if (!buffer_flush(buffer_1))
strerr_diefu1sys(111, "write to stdout") ;
return 0 ;
}
|