-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftab.go
More file actions
311 lines (250 loc) · 6.83 KB
/
conftab.go
File metadata and controls
311 lines (250 loc) · 6.83 KB
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
311
// Copyright (C) 2017, 2018 Damon Revoe. All rights reserved.
// Use of this source code is governed by the MIT
// license, which can be found in the LICENSE file.
package main
import (
"bufio"
"fmt"
"os"
"regexp"
"strings"
"unicode"
)
type optTypeType int
const (
optFeat optTypeType = iota // --enable-FEATURE type of options
optPkg // --with-PACKAGE type of options
optOther // all other options
)
type optionKey struct {
optType optTypeType
optName string
}
// ConftabSection contains a multiline plain text definition
// of the conftab section for the given package.
type ConftabSection struct {
PkgName string // "package" or "" if global section
Definition string // verbatim text including newlines
options map[optionKey]string // "--opt=value" or "" if commented
}
// Conftab contains definitions as well as an index of all conftab sections.
type Conftab struct {
GlobalSection *ConftabSection
PackageSections []*ConftabSection
sectionByPackageName map[string]*ConftabSection
}
func newSection(pkgName, definition string) *ConftabSection {
return &ConftabSection{pkgName, definition,
make(map[optionKey]string)}
}
type conftabReader struct {
filename string
scanner *bufio.Scanner
lineNumber int
optRegexp *regexp.Regexp
optClassifier optClassifier
}
func (reader *conftabReader) Err(message string) error {
return fmt.Errorf("%s:%d: %s", reader.filename,
reader.lineNumber, message)
}
type optClassifier struct {
optTypeRegexp *regexp.Regexp
}
func createOptClassifier() optClassifier {
return optClassifier{regexp.MustCompile(
`^((enable|disable)|(with|without))-(.+)$`)}
}
func (classifier *optClassifier) classify(option string) (key optionKey) {
matches := classifier.optTypeRegexp.FindStringSubmatch(option)
if len(matches) < 5 {
key.optType = optOther
key.optName = option
} else {
if matches[2] != "" {
key.optType = optFeat
} else {
key.optType = optPkg
}
key.optName = matches[4]
}
return
}
func (reader *conftabReader) readSection(pkgName string) (*ConftabSection,
string, error) {
section := newSection(pkgName, "")
for reader.scanner.Scan() {
reader.lineNumber++
line := strings.TrimSpace(reader.scanner.Text())
if line == "" {
section.Definition += "\n"
continue
}
if line[0] == '[' {
if line[len(line)-1] != ']' {
return nil, "", reader.Err(
"invalid section title format")
}
line = line[1 : len(line)-1]
return section, strings.TrimSpace(line), nil
}
section.Definition += line + "\n"
var optDefinition string
if line[0] == '#' {
line = strings.TrimLeft(line, "#")
line = strings.TrimLeftFunc(line, unicode.IsSpace)
} else if line[0] != '-' {
return nil, "", reader.Err("invalid option format " +
"(must start with a dash)")
} else {
optDefinition = line
}
matches := reader.optRegexp.FindStringSubmatch(line)
if len(matches) > 1 {
option := matches[1]
key := reader.optClassifier.classify(option)
section.options[key] = optDefinition
}
}
return section, "", nil
}
func readConftab(pathname string) (conftab *Conftab, err error) {
conftabFile, err := os.Open(pathname)
if err != nil {
return
}
defer func() {
closeErr := conftabFile.Close()
if err == nil {
err = closeErr
}
}()
conftabScanner := bufio.NewScanner(conftabFile)
reader := conftabReader{pathname, conftabScanner, 0,
regexp.MustCompile(`^--([^\s\[=]+)`),
createOptClassifier()}
section, nextPkgName, err := reader.readSection("")
if err != nil {
return
}
conftab = &Conftab{section, nil, make(map[string]*ConftabSection)}
for nextPkgName != "" {
pkgName := nextPkgName
section, nextPkgName, err = reader.readSection(pkgName)
if err != nil {
return
}
conftab.PackageSections = append(conftab.PackageSections,
section)
conftab.sectionByPackageName[pkgName] = section
}
return
}
func newConftab() *Conftab {
globalSection := newSection("", "\n")
globalSection.addOption(&optDescription{optionKey{optFeat, "shared"},
"Global defaults go here.", "--disable-shared"})
return &Conftab{globalSection,
nil, make(map[string]*ConftabSection)}
}
type optDescription struct {
key optionKey
description string
definition string
}
func (section *ConftabSection) addOption(opt *optDescription) {
// Novel options are commented out.
section.options[opt.key] = ""
section.Definition = "# " + opt.description + "\n#" +
opt.definition + "\n\n" + section.Definition
}
func (conftab *Conftab) addOption(pkgName string,
opt *optDescription) bool {
section, found := conftab.sectionByPackageName[pkgName]
if found {
if _, found = section.options[opt.key]; found {
return false
}
} else {
section = newSection(pkgName, "\n")
conftab.PackageSections = append(conftab.PackageSections,
section)
conftab.sectionByPackageName[pkgName] = section
}
section.addOption(opt)
return true
}
func (conftab *Conftab) getConfigureArgs(pkgName string) []string {
var args []string
section, found := conftab.sectionByPackageName[pkgName]
if !found {
return args
}
for key, val := range section.options {
if val != "" {
args = append(args, val)
} else if val = conftab.GlobalSection.options[key]; val != "" {
args = append(args, val)
}
}
return args
}
type sectionChange struct {
deleted, added string
}
func (conftab *Conftab) diff(otherConftab *Conftab) (
deletedSections []string,
changedSections map[string][]sectionChange,
addedSections []string) {
for pkgName, origSection := range conftab.sectionByPackageName {
section := otherConftab.sectionByPackageName[pkgName]
if section == nil {
deletedSections = append(deletedSections,
origSection.PkgName)
}
}
changedSections = make(map[string][]sectionChange)
for pkgName, section := range otherConftab.sectionByPackageName {
origSection := conftab.sectionByPackageName[pkgName]
if origSection == nil {
addedSections = append(addedSections, section.PkgName)
continue
}
changes := changedSections[section.PkgName]
for key, val := range origSection.options {
if val == "" {
val = conftab.GlobalSection.options[key]
if val == "" {
continue
}
}
if section.options[key] == "" && otherConftab.
GlobalSection.options[key] == "" {
changes = append(changes,
sectionChange{deleted: val})
}
}
for key, val := range section.options {
if val == "" {
val = otherConftab.GlobalSection.options[key]
// Deletions are discovered
// in the previous loop.
if val == "" {
continue
}
}
origVal := origSection.options[key]
if origVal == "" {
origVal = conftab.GlobalSection.options[key]
}
if val != origVal {
changes = append(changes,
sectionChange{origVal, val})
}
}
if len(changes) > 0 {
changedSections[section.PkgName] = changes
}
}
return
}