-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
156 lines (145 loc) · 3.19 KB
/
main.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
/*
Copyright 2012 Philip Silva
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"bufio"
"errors"
"fmt"
"io"
"log"
"os"
"strings"
)
var (
ErrNotFound = errors.New("pc file could not be found")
PKG_CONFIG_PATH = os.Getenv("PKG_CONFIG_PATH")
)
func parseConfig(filename string) (cfg map[string]string, err error) {
f, err := os.Open(filename)
if err != nil {
return
}
defer f.Close()
cfg = make(map[string]string)
vars := make(map[string]string)
r := bufio.NewReader(f)
prelude := true
for {
lb, _, err := r.ReadLine()
if err == io.EOF {
break
} else if err != nil {
return nil, err
}
l := string(lb)
if l == "" || strings.HasPrefix(l, "#") {
continue
}
if strings.Contains(l, ":") {
prelude = false
}
if prelude {
sl := strings.Split(l, "=")
if len(sl) < 2 {
continue
}
name := sl[0]
content := sl[1]
from := strings.Index(content, "${")
to := strings.Index(content, "}")
if from >= 0 && to >= 0 {
a := content[:from]
if from+2 < to {
b := vars[content[from+2:to]]
c := content[to+1:]
vars[name] = a + b + c
}
} else {
vars[name] = content
}
} else {
sl := strings.Split(l, ": ")
if len(sl) < 2 {
continue
}
name := sl[0]
content := sl[1]
from := strings.Index(content, "${")
to := strings.Index(content, "}")
if from >= 0 && to >= 0 {
cfg[strings.ToLower(name)] = content[:from] + vars[content[from+2:to]] + content[to+1:]
} else {
cfg[strings.ToLower(name)] = content
}
}
}
return cfg, nil
}
func locatePC(name string) (fullPath string, err error) {
configPaths := strings.Split(PKG_CONFIG_PATH, ":")
for _, p := range configPaths {
fullPath, err = find(p, name+".pc")
if err == nil {
return
}
}
return "", ErrNotFound
}
func find(path string, filename string) (targetPath string, err error) {
dir, err := os.Open(path)
if err != nil {
return
}
defer dir.Close()
fis, err := dir.Readdir(-1)
if err != nil {
return
}
for _, fi := range fis {
if fi.Name() == filename {
return path + "/" + filename, nil
} else if fi.IsDir() {
targetPath, err = find(path+"/"+fi.Name(), filename)
if err == nil {
return
}
}
}
return "", ErrNotFound
}
func main() {
var target string
params := make([]string, 0, 3)
for _, arg := range os.Args[1:] {
if strings.HasPrefix(arg, "--") {
params = append(params, arg[2:])
} else {
target = arg
}
}
if target == "" {
log.Fatalf("no target specified")
}
fn, err := locatePC(target)
if err != nil {
log.Fatalf("locate pc: %v", err)
}
cfg, err := parseConfig(fn)
if err != nil {
log.Fatalf("parsing: %v", err)
}
for _, p := range params {
fmt.Print(cfg[p] + " ")
}
fmt.Printf("\n")
}