-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathparse_config.go
83 lines (75 loc) · 2.1 KB
/
parse_config.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
/*
* Copyright (c) 2022 Red Hat, Inc.
* SPDX-License-Identifier: GPL-2.0-or-later
*/
package main
import (
"fmt"
"regexp"
"strconv"
"strings"
)
// Kernel version item
type kversion struct {
Version int64
Patchlevel int64
Sublevel int64
Extraversion string
}
// Given a config autogenerated file into the kernel source tree, returns
// the list of kernel config options
func parse_config(kconfig []string) map[string]string {
res := make(map[string]string)
for _, line := range kconfig {
if strings.Contains(line, "#define CONFIG_") {
tmp := strings.ReplaceAll(line, "#define CONFIG_", "")
items := strings.Split(tmp, " ")
res[items[0]] = items[1]
}
}
return res
}
// Parses a makefile and returns the current kernel version
func get_version(makefile []string) (kversion, error) {
var state int = 0
var v kversion
for _, line := range makefile {
if match, _ := regexp.MatchString("VERSION[ \t]*=[ \t]*[0-9]+", line); match && state == 0 {
re := regexp.MustCompile(`VERSION[ \t]*=[ \t]*([0-9]+)`)
tmp, err := strconv.ParseInt(re.ReplaceAllString(line, "$1"), 10, 64)
if err != nil {
panic(err)
}
v.Version = tmp
state = 1
}
if match, _ := regexp.MatchString("PATCHLEVEL[ \t]*=[ \t]*[0-9]+", line); match && state == 1 {
re := regexp.MustCompile(`PATCHLEVEL[ \t]*=[ \t]*([0-9]+)`)
tmp, err := strconv.ParseInt(re.ReplaceAllString(line, "$1"), 10, 64)
if err != nil {
panic(err)
}
v.Patchlevel = tmp
state = 2
}
if match, _ := regexp.MatchString("SUBLEVEL[ \t]*=[ \t]*[0-9]+", line); match && state == 2 {
re := regexp.MustCompile(`SUBLEVEL[ \t]*=[ \t]*([0-9]+)`)
tmp, err := strconv.ParseInt(re.ReplaceAllString(line, "$1"), 10, 64)
if err != nil {
panic(err)
}
v.Sublevel = tmp
state = 3
}
if match, _ := regexp.MatchString("EXTRAVERSION[ \t]*=[ \t]*.*", line); match && state == 3 {
re := regexp.MustCompile(`EXTRAVERSION[ \t]*=[ \t]*(.*)`)
v.Extraversion = re.ReplaceAllString(line, "$1")
state = 4
break
}
}
if state == 4 {
return v, nil
}
return v, fmt.Errorf("can't parse makefile (%d)", state)
}