-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuci.go
More file actions
177 lines (162 loc) · 3.95 KB
/
Copy pathuci.go
File metadata and controls
177 lines (162 loc) · 3.95 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
// Copyright (c) 2026 Artronah1
// SPDX-License-Identifier: MulanPubL-2.0
package main
// uci.go — tiny reader for OpenWrt UCI config files.
//
// We parse /etc/config/* directly instead of shelling out to `uci`. Reasons:
// - no exec, no dependency on the uci binary being present/working
// - deterministic: `uci show` output quoting has changed across releases
// - ksd only ever *reads* config, so a read-only parser is enough
//
// Supported grammar (the subset UCI actually emits):
//
// config <type> ['<name>']
// option <key> '<value>'
// list <key> '<value>'
// # comments
import (
"bufio"
"os"
"strings"
)
// Section is one `config` block. Options holds scalars, Lists holds repeated
// values (UCI `list` and repeated `option` with the same key both land here).
type Section struct {
Type string
Name string
Options map[string]string
Lists map[string][]string
}
// UCIFile is a parsed config file: indexed by "type" and "type.name".
type UCIFile struct {
Sections []*Section
byKey map[string]*Section
}
func (u *UCIFile) Get(typeName string) *Section {
if u == nil {
return nil
}
return u.byKey[typeName]
}
// Lookup finds a section by type, or by type.name.
func (u *UCIFile) Lookup(typeName, name string) *Section {
if u == nil {
return nil
}
if name != "" {
if s, ok := u.byKey[typeName+"."+name]; ok {
return s
}
}
return u.byKey[typeName]
}
func LoadUCI(path string) (*UCIFile, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
u := &UCIFile{byKey: map[string]*Section{}}
var cur *Section
sc := bufio.NewScanner(f)
sc.Buffer(make([]byte, 0, 64*1024), 1024*1024)
for sc.Scan() {
line := strings.TrimSpace(sc.Text())
if line == "" || strings.HasPrefix(line, "#") {
continue
}
fields := splitFields(line)
if len(fields) == 0 {
continue
}
switch fields[0] {
case "config":
if len(fields) < 2 {
continue
}
cur = &Section{
Type: unquote(fields[1]),
Options: map[string]string{},
Lists: map[string][]string{},
}
if len(fields) > 2 {
cur.Name = unquote(fields[2])
}
u.Sections = append(u.Sections, cur)
// First section of a given type wins for Get(type); anonymous
// sections ("@defaults[0]") have no name, so type alone is the key.
if _, exists := u.byKey[cur.Type]; !exists {
u.byKey[cur.Type] = cur
}
if cur.Name != "" {
key := cur.Type + "." + cur.Name
if _, exists := u.byKey[key]; !exists {
u.byKey[key] = cur
}
}
case "option", "list":
if cur == nil || len(fields) < 3 {
continue
}
k := unquote(fields[1])
v := unquote(strings.Join(fields[2:], " "))
if fields[0] == "list" {
cur.Lists[k] = append(cur.Lists[k], v)
} else {
// A repeated `option` with the same key is treated as a list
// entry as well — matches how `uci show` renders UCI lists.
if prev, seen := cur.Options[k]; seen {
delete(cur.Options, k)
cur.Lists[k] = append(cur.Lists[k], prev, v)
} else {
cur.Options[k] = v
}
}
}
}
return u, sc.Err()
}
// unquote strips one layer of '...' or "...".
func unquote(s string) string {
if len(s) >= 2 {
if (s[0] == '\'' && s[len(s)-1] == '\'') || (s[0] == '"' && s[len(s)-1] == '"') {
return s[1 : len(s)-1]
}
}
return s
}
// splitFields splits on whitespace but keeps quoted spans intact, so that
// option ntp_servers 'a b c' parses as a single value.
func splitFields(s string) []string {
var out []string
var cur strings.Builder
inQ := byte(0)
started := false
for i := 0; i < len(s); i++ {
c := s[i]
switch {
case inQ != 0:
cur.WriteByte(c)
if c == inQ {
inQ = 0
}
case c == '\'' || c == '"':
inQ = c
cur.WriteByte(c)
started = true
case c == ' ' || c == '\t':
if started {
out = append(out, cur.String())
cur.Reset()
started = false
}
default:
cur.WriteByte(c)
started = true
}
}
if started {
out = append(out, cur.String())
}
return out
}