network_interface_file.anubis
7.94 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
* Created by PyramIDE.
* User: Alexis & Totoro
* Date: 03/09/2012
* Time: 16:05
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
read system/logger.anubis
read system/string.anubis
read tools/basis.anubis
read tools/streams.anubis
delete_entry
find_entry
replace_entry
add_entry
public type Network_Parameter:
comment(String),
conf_nv(String name, String value),
conf_nvc(String name, String value, String comment).
public type Network_type:
iface(String name, Network_Parameter param).
public type Network_entry:
blank,
comment(String comment),
section(Network_type net_type, List(Network_Parameter) params).
define String
to_String
(
Network_Parameter i_entry,
)=
if i_entry is
{
comment(s) then s,
conf_nv(n,v) then n +" "+v,
conf_nvc(n,v,c) then n+" "+v+" "+c
}.
define String
to_String
(
Network_type n_type
)=
if n_type is
{
iface(name, param) then "iface "+name+" "+to_String(param)
}
.
define String
_to_String
(
List(Network_Parameter) interface_entries,
String so_far //this is so far of the section, not the general file
)=
if interface_entries is
{
[] then so_far,
[h . t] then
_to_String(t, so_far + "\t"+to_String(h)+"\n")
}.
public define String
to_String
(
List(Network_entry) network_entries,
String so_far
)=
if network_entries is
{
[] then so_far,
[h . t] then
if h is
{
blank then to_String(t, so_far+"\n"),
comment(comment) then to_String(t, so_far + comment),
section(net_type, params) then to_String(t, so_far+"auto "+net_type.name+"\n"+
to_String(net_type)+"\n"+
_to_String( params, ""))
}
}.
define Maybe(Network_Parameter)
extract_value
(
String name,
String left_line
)=
//println(" Extract value for name ["+name+"] in ["+left_line+"]");
if find_char(left_line, '#', 0) is
{
failure then success(conf_nv(name, left_line)),
success(idx) then
if sub_string(left_line, 0, idx) is
{
failure then failure,
success(value) then
if sub_string(left_line, idx+1, (length(left_line) - (idx+1))) is
{
failure then
//println("conf_nv found name="+name+" value="+value);
success(conf_nv(name, value)),
success(comment) then
//println("conf_nvc found name="+name+" value="+value+" comment="+comment);
success(conf_nvc(name, value, comment))
}
}
}.
define Maybe(Network_Parameter)
get_network_parameter
(
List(String) params
)=
if params is
{
[] then failure,
[ name . t] then
//println("Get network parameter for ["+name+"]");
if nth((Int)0, name) is
{
failure then failure,
success(c) then
if c = '#' then
success(comment(join(" ",params)))
else
extract_value(name, join(" ",t))
}
}.
define Maybe(Network_type)
get_type
(
String line,
String name
)=
if split_by_token(line, ' ') is
{
[ ] then failure,
[type . t] then
if type = "iface" then
if t is
{
[] then
//println("WARNING unamed iface section ["+name+"]");
failure,
[h . t] then
//check if auto name is equal to iface name
if h = name then
if get_network_parameter(t) is
{
failure then failure,
success(net_param) then success(iface(name, net_param))
}
else
failure
}
else failure
}.
define List(Network_Parameter)
parse_section_body
(
Stream s,
List(Network_Parameter) so_far
)=
if read_line(s) is
{
failure then reverse(so_far),
success(line) then
if nth((Int)0, line) is
{
failure then reverse(so_far),
success(c) then
if c = ' ' | c='\t' then
//it seem to be a a real section body entry we try it
//println("parse section line ["+trim(line)+"]");
if get_network_parameter(split_by_token(trim(line), ' ')) is
{
failure then reverse(so_far),
success(param) then parse_section_body(s,[param . so_far])
}
else
// itsn't part of section body, so we stop here and unput the line read
//and return the found Network_Parameter
//println("Not body part ["+trim(line)+"]");
unput_line(s, line);
reverse(so_far)
}
}.
define Network_entry
parse_section
(
Stream s,
String line
) =
if split_by_token(line, ' ') is
{
[ ] then blank,
[h . t] then
if h = "auto" then
if t is
{
[] then
//println("WARNING unamed section ["+h+"]");
blank,
[ name . t ] then
//println("auto found with name ["+name+"]");
//read the second line of the section where the type is located i.e. iface
if read_line(s) is
{
failure then blank,
success(type_line) then
//println("get type line for "+name+"["+trim(type_line)+"]");
if get_type(trim(type_line), name) is
{
failure then
//println("type not found for "+name);
blank,
success(type_entry) then
//println("type found, parse for section");
section(type_entry, parse_section_body(s,[]))
}
}
}
else
blank
}.
define Network_entry
read_interface_entry
(
Stream s,
String line
)=
//check for comment
if nth((Int)0,line) is
{
failure then blank,
success(c) then
if c = '#' then
comment(line)
else
parse_section(s, trim(line))
}.
define List(Network_entry)
read_interface_entries
(
Stream s,
List(Network_entry) so_far
)=
if read_line(s) is
{
failure then reverse(so_far),
success(line) then
//println("["+line+"]");
read_interface_entries(s, [read_interface_entry(s, line) . so_far])
}.
public define List(Network_entry)
read_interface
(
String interface_file
) =
if (Maybe(RStream))file(interface_file, read) is
{
failure then [], //nothing to read
success(f) then //parse file for each entry
read_interface_entries(make_stream(f), [])
}.
public define Maybe(One)
write_interface
(
String file_name,
List(Network_entry) entries
)=
if (Maybe(RWStream))file(file_name, new) is
{
failure then failure,
success(f) then success(forget(reliable_write(f, to_byte_array(to_String(entries, "")))))
}.
/** here tool for testing
* anbexec parse_interface <given_file>
* will produce a dump of given_file into file named as original with ".dump" as an extension
*/
global define One
parse_interface
(
List(String) args
)=
if args is
{
[] then print("usage : parse_interface <file>"),
[ file_name . t ] then
forget(write_interface(file_name+".dump",read_interface(file_name)))
}
.