-
Notifications
You must be signed in to change notification settings - Fork 64
/
parser.go
310 lines (259 loc) · 5.86 KB
/
parser.go
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// SPDX-License-Identifier: Apache-2.0
// Copyright 2020 syzkaller project authors. All rights reserved.
// Copyright 2022 Unikraft GmbH. All rights reserved.
// Licensed under the Apache-2.0 License (the "License").
// You may not use this file except in compliance with the License.
package kconfig
import (
"bufio"
"bytes"
"fmt"
"os/exec"
"strings"
)
// parser is a helper for parsing simple text protocols tailored for kconfig syntax.
type parser struct {
data []byte
file string
current string
col int
line int
err error
env KeyValueMap
}
func newParser(data []byte, file string, env KeyValueMap) *parser {
return &parser{
data: data,
file: file,
env: env,
}
}
// nextLine resets the parser to the next line.
// Automatically concatenates lines split with \ at the end.
func (p *parser) nextLine() bool {
if p.err != nil || len(p.data) == 0 {
return false
}
p.col = 0
p.line++
p.current = p.readNextLine()
for p.current != "" && p.current[len(p.current)-1] == '\\' && len(p.data) != 0 {
p.current = p.current[:len(p.current)-1] + p.readNextLine()
p.line++
}
p.skipSpaces()
return true
}
func (p *parser) readNextLine() string {
line := ""
nextLine := bytes.IndexByte(p.data, '\n')
if nextLine != -1 {
line = string(p.data[:nextLine])
p.data = p.data[nextLine+1:]
} else {
line = string(p.data)
p.data = nil
}
return line
}
func (p *parser) skipSpaces() {
for p.col < len(p.current) && (p.current[p.col] == ' ' || p.current[p.col] == '\t') {
p.col++
}
}
func (p *parser) identLevel() int {
level := 0
for i := 0; i < p.col; i++ {
level++
if p.current[i] == '\t' {
level = (level + 7) & ^7
}
}
return level
}
func (p *parser) failf(msg string, args ...interface{}) {
if p.err == nil {
p.err = fmt.Errorf("%v:%v:%v: %v\n%v", p.file, p.line, p.col, fmt.Sprintf(msg, args...), p.current)
}
}
func (p *parser) eol() bool {
return p.col == len(p.current)
}
func (p *parser) char() byte {
if p.err != nil {
return 0
}
if p.eol() {
p.failf("unexpected end of line")
return 0
}
v := p.current[p.col]
p.col++
return v
}
func (p *parser) peek() byte {
if p.err != nil || p.eol() {
return 0
}
return p.current[p.col]
}
func (p *parser) ConsumeLine() string {
res := p.current[p.col:]
p.col = len(p.current)
return res
}
func (p *parser) TryConsume(what string) bool {
if !strings.HasPrefix(p.current[p.col:], what) {
return false
}
p.col += len(what)
p.skipSpaces()
return true
}
func (p *parser) MustConsume(what string) {
if !p.TryConsume(what) {
p.failf("expected %q", what)
}
}
func (p *parser) interpolate(b []byte) string {
var vars []string
var v []byte
parsing := false
str := string(b)
for i := 0; i < len(b); i++ {
// Test if we're parsing a variable which starts with `$(` and not a shell
// invocation which starts with `$(shell `.
if i+1 <= len(b) && b[i] == '$' && b[i+1] == '(' &&
(i+8 <= len(b) && string(b[i+2:i+7]) != "shell") {
i += 2 // skip the '$('
parsing = true
// If we're busy accepting a variable name and come to an end `)` we can
// stop and save.
} else if parsing && b[i] == ')' {
parsing = false
vars = append(vars, string(v))
v = []byte{}
}
// Save the next character to the variable name if we're parsing.
if parsing {
v = append(v, b[i])
}
}
for _, v := range vars {
if v == "" {
continue
}
// Evaluate known variables found from the provided environment
if !strings.HasPrefix("shell", v) {
replace, ok := p.env[v]
if !ok {
// Try with the prefix `CONFIG_`
replace, ok = p.env["CONFIG_"+v]
if !ok {
continue
}
}
str = strings.ReplaceAll(str, fmt.Sprintf("$(%s)", v), replace.Value)
}
}
// Evaluate shell executions
if len(str) > 7 && str[0:8] == "$(shell," {
line := strings.TrimPrefix(str, "$(shell,")
line = strings.TrimSuffix(line, ")")
quoted := false
args := strings.FieldsFunc(line, func(r rune) bool {
if r == '\'' || r == '"' {
quoted = !quoted
}
return !quoted && r == ' '
})
for i := range args {
args[i] = strings.TrimSpace(args[i])
args[i] = strings.TrimPrefix(args[i], "'")
args[i] = strings.TrimSuffix(args[i], "'")
}
var buf bytes.Buffer
cmd := exec.Command(args[0], args[1:]...)
cmd.Stdout = bufio.NewWriter(&buf)
if err := cmd.Run(); err != nil {
p.failf("could not execute shell: %s", err.Error())
return ""
}
str = strings.TrimSpace(buf.String())
}
return str
}
func (p *parser) QuotedString() string {
var str []byte
quote := p.char()
if quote != '"' && quote != '\'' {
p.failf("expect quoted string")
}
for ch := p.char(); ch != quote; ch = p.char() {
if ch == 0 {
p.failf("unterminated quoted string")
break
}
if ch == '\\' {
ch = p.char()
switch ch {
case '\'', '"', '\\':
str = append(str, ch)
default:
p.failf("bad quoted character")
}
continue
}
str = append(str, ch)
if ch == '$' && p.peek() == '(' {
str = append(str, p.Shell()...)
}
}
p.skipSpaces()
return p.interpolate(str)
}
func (p *parser) TryQuotedString() (string, bool) {
if ch := p.peek(); ch == '"' || ch == '\'' {
return p.QuotedString(), true
}
return "", false
}
func (p *parser) Ident() string {
var str []byte
for !p.eol() {
ch := p.peek()
if ch >= 'a' && ch <= 'z' ||
ch >= 'A' && ch <= 'Z' ||
ch >= '0' && ch <= '9' ||
ch == '_' || ch == '-' {
str = append(str, ch)
p.col++
continue
}
break
}
if len(str) == 0 {
p.failf("expected an identifier")
}
p.skipSpaces()
return string(str)
}
func (p *parser) Shell() string {
start := p.col
p.MustConsume("(")
for !p.eol() && p.peek() != ')' {
if p.peek() == '"' {
p.QuotedString()
} else if p.peek() == '(' {
p.Shell()
} else {
p.col++
}
}
if ch := p.char(); ch != ')' {
p.failf("shell expression is not terminated")
}
res := p.current[start:p.col]
p.skipSpaces()
return res
}