-
Notifications
You must be signed in to change notification settings - Fork 27
/
common.tcl
executable file
·192 lines (144 loc) · 3.82 KB
/
common.tcl
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
192
#!/bin/tclsh
source once.tcl
sourceOnce cgi.tcl
array set TYPE_MAP {
"BOOL" "bool"
"ENUM" "int"
"INTEGER" "int"
"FLOAT" "double"
"STRING" "string"
"ACTION" "bool"
}
#source cgi.tcl
loadOnce tclrpc.so
proc decr {x {cnt 1}} {
upvar $x xx
set xx [expr $xx - $cnt]
}
proc max {x y} { expr { $x > $y ? $x : $y } }
proc min {x y} { expr { $x > $y ? $y : $x } }
proc cgi_cgi {args} {return $args}
proc in {list element} {expr [lsearch -exact $list $element] >= 0}
proc array_clear {name} {
upvar $name arr
foreach key [array names arr] {
unset arr($key)
}
}
proc array_copy {src_name dst_name} {
global $src_name $dst_name
array_clear $dst_name
array set $dst_name [array get $src_name]
}
#Liest eine Datei ein mit dem Format
#Key=Value
#Key=Value
#...
#<.
#und speichert die Werte in einem übergebenen Array ab.
proc read_assignment_file {filename value_array} {
upvar $value_array arr
set ret -1
if { ! [catch {open $filename RDONLY} f] } then {
while {1} {
gets $f zeile
# Weiche EOF-Marke
if { [eof $f] || [string equal $zeile "<."] } break
set data [split $zeile =]
if {$zeile == "" || [lindex $data 0] == ""} then {continue}
set arr([lindex $data 0]) [lindex $data 1]
}
close $f
}
return $ret
}
#Schreibt ein Array in eine Datei. Format, siehe read_assignment_file
proc write_assignment_file {filename value_array {soft_eof 1}} {
upvar $value_array arr
set ret -1
if { ! [catch {open $filename w} f] } then {
foreach key [array names arr] {
puts $f "$key=$arr($key)"
}
#Weiche EOF-Marke
if {$soft_eof} then { puts $f "<." }
close $f
set ret 1
}
return $ret
}
#Abgewandelt von http://wiki.tcl.tk/1017
#proc verbose_eval
#15.02.2007
proc eval_script {script} {
set cmd ""
set ret -1
foreach line [split $script \n] {
if {$line == ""} {continue}
append cmd $line\n
if { [info complete $cmd] } {
#puts -nonewline $cmd
set ret [uplevel 1 $cmd]
set cmd ""
}
}
return $ret
}
proc BitsSet {b_testee b_set} {
if {$b_testee == "" || $b_set == "" || $b_testee == 0 || $b_set == 0} then { return 0 }
return [expr [expr $b_set & $b_testee] == $b_testee]
}
proc putimage {img_path} {
set in [open $img_path]
catch {fconfigure stdout -translation binary}
catch {fconfigure stdout -encoding binary}
catch {fconfigure $in -translation binary}
catch {fconfigure $in -encoding binary}
puts -nonewline stdout [read $in]
close $in
}
proc uniq {liste} {
set u_list ""
set last_e ""
foreach e [lsort $liste] {
if {$e != $last_e} then {
lappend u_list $e
set last_e $e
}
}
return $u_list
}
set INTERFACES_FILE "/etc/config/InterfacesList.xml"
array set interfaces ""
array set interface_descriptions ""
proc read_interfaces {} {
global interfaces interface_descriptions INTERFACES_FILE env
set retval 1
if { [ info exist env(BIDCOS_SERVICE) ] } {
set interfaces(default) "$env(BIDCOS_SERVICE)"
set interface_descriptions(default) "Default BidCoS Interface"
} else {
set fd -1
catch {set fd [open $INTERFACES_FILE r]}
if { $fd >=0 } {
set contents [read $fd]
while { [regexp -indices {</ipc[^>]*>} $contents range] } {
set section [string range $contents 0 [lindex $range 1]]
set contents [string range $contents [expr [lindex $range 1] + 1] end]
if {
[regexp {<name[^>]*>([^<]+)</name} $section dummy name] &&
[regexp {<url[^>]*>([^<]+)</url} $section dummy url] &&
[regexp {<info[^>]*>([^<]+)</info} $section dummy description ]
} {
array set interfaces [list $name $url]
array set interface_descriptions [list $name $description]
}
}
close $fd
} else {
puts "Could not open interface file"
set retval 0
}
}
return $retval
}