-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathvariables.go
93 lines (80 loc) · 1.74 KB
/
variables.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
package utils
import (
"fmt"
"strings"
"sync"
)
var (
SysVarPrintFormatKey string = "sys.printfmt"
)
var (
_varMutex sync.RWMutex
_globalVariables = make(map[string][]byte)
_builtinVars = [][]string{
{`head`, "\x00"},
}
_globalSysVariables = make(map[string]string)
_builtinSysVars = [][]string{
{SysVarPrintFormatKey, "table"},
}
)
func VarGet(varname string) ([]byte, bool) {
_varMutex.RLock()
defer _varMutex.RUnlock()
val, ok := _globalVariables[varname]
return val, ok
}
func VarSet(varname string, val []byte) {
_varMutex.Lock()
defer _varMutex.Unlock()
_globalVariables[varname] = append([]byte{}, val...)
}
func IsVar(s string) bool {
return strings.HasPrefix(s, "$")
}
func InitBuiltinVaribles() {
for _, item := range _builtinVars {
VarSet(item[0], []byte(item[1]))
}
for _, item := range _builtinSysVars {
SysVarSet(item[0], item[1])
}
}
func PrintGlobalVaribles() {
_varMutex.RLock()
defer _varMutex.RUnlock()
if len(_globalVariables) > 0 {
var data = [][]string{
{"Var Name", "Value"},
}
for k, v := range _globalVariables {
vv := fmt.Sprintf("h'%s'", Bytes2hex(v))
data = append(data, []string{k, vv})
}
PrintTable(data)
}
}
func SysVarGet(varname string) (string, bool) {
_varMutex.RLock()
defer _varMutex.RUnlock()
val, ok := _globalSysVariables[varname]
return val, ok
}
func SysVarSet(varname, val string) {
_varMutex.Lock()
defer _varMutex.Unlock()
_globalSysVariables[varname] = val
}
func PrintSysVaribles() {
_varMutex.RLock()
defer _varMutex.RUnlock()
if len(_globalSysVariables) > 0 {
var data = [][]string{
{"System Varibles Name", "Value"},
}
for k, v := range _globalSysVariables {
data = append(data, []string{k, string(v)})
}
PrintTable(data)
}
}