forked from blakesmith/go-grok
/
grok.go
191 lines (146 loc) · 3.44 KB
/
grok.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
package grok
/*
#cgo LDFLAGS: -lgrok
#include <grok.h>
*/
import "C"
import (
"errors"
"fmt"
"unsafe"
)
const (
GROK_OK = iota
GROK_ERROR_FILE_NOT_ACCESSIBLE
GROK_ERROR_PATTERN_NOT_FOUND
GROK_ERROR_UNEXPECTED_READ_SIZE
GROK_ERROR_COMPILE_FAILED
GROK_ERROR_UNINITIALIZED
GROK_ERROR_PCRE_ERROR
GROK_ERROR_NOMATCH
)
type Grok struct {
g *C.grok_t
}
type Match struct {
gm C.grok_match_t
}
type Pile struct {
Patterns map[string]string
PatternFiles []string
Groks []*Grok
}
func New() *Grok {
grok := new(Grok)
grok.g = C.grok_new()
if grok.g == nil {
return nil
}
return grok
}
func (grok *Grok) AddPattern(name, pattern string) {
cname := C.CString(name)
cpattern := C.CString(pattern)
defer C.free(unsafe.Pointer(cname))
defer C.free(unsafe.Pointer(cpattern))
C.grok_pattern_add(grok.g, cname, C.strlen(cname), cpattern, C.strlen(cpattern))
}
func (grok *Grok) AddPatternsFromFile(path string) error {
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
ret := C.grok_patterns_import_from_file(grok.g, cpath)
if ret != GROK_OK {
return errors.New(fmt.Sprintf("Failed to add path %s", path))
}
return nil
}
func (grok *Grok) Compile(pattern string) error {
p := C.CString(pattern)
defer C.free(unsafe.Pointer(p))
ret := C.grok_compile(grok.g, p)
if ret != GROK_OK {
return errors.New(fmt.Sprintf("Failed to compile: %s", C.GoString(grok.g.errstr)))
}
return nil
}
func (grok *Grok) Match(text string) *Match {
t := C.CString(text)
var cmatch C.grok_match_t
ret := C.grok_exec(grok.g, t, &cmatch)
if ret != GROK_OK {
return nil
}
match := new(Match)
match.gm = cmatch
return match
}
func (grok *Grok) Discover(text string) string {
ctext := C.CString(text)
defer C.free(unsafe.Pointer(ctext))
gdt := C.grok_discover_new(grok.g)
var discovery *C.char
var discoverylen C.int
C.grok_discover(gdt, ctext, &discovery, &discoverylen)
return C.GoStringN(discovery, discoverylen)
}
func (grok *Grok) Free() {
C.grok_free(grok.g)
}
func NewPile() *Pile {
pile := new(Pile)
pile.Patterns = make(map[string]string)
pile.PatternFiles = make([]string, 0)
pile.Groks = make([]*Grok, 0)
return pile
}
func (pile *Pile) Free() {
for _, grok := range pile.Groks {
grok.Free()
}
}
func (pile *Pile) AddPattern(name, str string) {
pile.Patterns[name] = str
}
func (pile *Pile) Compile(pattern string) error {
grok := New()
if grok == nil {
return errors.New("Unable to initialize grok!")
}
for name, value := range pile.Patterns {
grok.AddPattern(name, value)
}
for _, path := range pile.PatternFiles {
grok.AddPatternsFromFile(path)
}
grok.Compile(pattern)
pile.Groks = append(pile.Groks, grok)
return nil
}
func (pile *Pile) Match(str string) (*Grok, *Match) {
for _, grok := range pile.Groks {
match := grok.Match(str)
if match != nil {
return grok, match
}
}
return nil, nil
}
func (match *Match) Captures() map[string][]string {
captures := make(map[string][]string)
var name, substring *C.char
var namelen, sublen C.int
C.grok_match_walk_init(&match.gm)
for C.grok_match_walk_next(&match.gm, &name, &namelen, &substring, &sublen) == GROK_OK {
var substrings []string
gname := C.GoStringN(name, namelen)
gsubstring := C.GoStringN(substring, sublen)
if val := captures[gname]; val == nil {
substrings = make([]string, 0)
} else {
substrings = val
}
captures[gname] = append(substrings, gsubstring)
}
C.grok_match_walk_end(&match.gm)
return captures
}