Commit 899f1ab0f3b0e73bf645c48084016d2fca6989b0
1 parent
48805b33
--no commit message
Showing
1 changed file
with
288 additions
and
0 deletions
Show diff stats
os_tools/linux/config/network_interface_file.anubis
0 → 100644
| 1 | +/* | ||
| 2 | + * Created by PyramIDE. | ||
| 3 | + * User: Alexis | ||
| 4 | + * Date: 03/09/2012 | ||
| 5 | + * Time: 16:05 | ||
| 6 | + * | ||
| 7 | + * To change this template use Tools | Options | Coding | Edit Standard Headers. | ||
| 8 | + */ | ||
| 9 | + | ||
| 10 | + | ||
| 11 | +read system/logger.anubis | ||
| 12 | +read system/string.anubis | ||
| 13 | +read tools/basis.anubis | ||
| 14 | +read tools/kero_loggers.anubis | ||
| 15 | +read tools/streams.anubis | ||
| 16 | +read calexium_lib/net_services_protocols/logger_service.anubis | ||
| 17 | + | ||
| 18 | +read kero_constants.anubis | ||
| 19 | + | ||
| 20 | +delete_entry | ||
| 21 | +find_entry | ||
| 22 | +replace_entry | ||
| 23 | +add_entry | ||
| 24 | + | ||
| 25 | + | ||
| 26 | +public type Network_Parameter: | ||
| 27 | + comment(String), | ||
| 28 | + conf_nv(String name, String value), | ||
| 29 | + conf_nvc(String name, String value, String comment). | ||
| 30 | + | ||
| 31 | + | ||
| 32 | +public type Network_type: | ||
| 33 | + iface(String name, Network_Parameter param). | ||
| 34 | + | ||
| 35 | +public type Network_entry: | ||
| 36 | + blank, | ||
| 37 | + comment(String comment), | ||
| 38 | + section(Network_type net_type, List(Network_Parameter) params). | ||
| 39 | + | ||
| 40 | +define String | ||
| 41 | + to_String | ||
| 42 | + ( | ||
| 43 | + Network_Parameter i_entry, | ||
| 44 | + )= | ||
| 45 | + if i_entry is | ||
| 46 | + { | ||
| 47 | + comment(s) then s, | ||
| 48 | + conf_nv(n,v) then n +" "+v, | ||
| 49 | + conf_nvc(n,v,c) then n+" "+v+" "+c | ||
| 50 | + }. | ||
| 51 | + | ||
| 52 | +define String | ||
| 53 | + to_String | ||
| 54 | + ( | ||
| 55 | + Network_type n_type | ||
| 56 | + )= | ||
| 57 | + if n_type is | ||
| 58 | + { | ||
| 59 | + iface(name, param) then "iface "+name+" "+to_String(param) | ||
| 60 | + } | ||
| 61 | + . | ||
| 62 | + | ||
| 63 | +define String | ||
| 64 | + _to_String | ||
| 65 | + ( | ||
| 66 | + List(Network_Parameter) interface_entries, | ||
| 67 | + String so_far | ||
| 68 | + )= | ||
| 69 | + if interface_entries is | ||
| 70 | + { | ||
| 71 | + [] then so_far, | ||
| 72 | + [h . t] then | ||
| 73 | + _to_String(t, so_far + to_String(h)) | ||
| 74 | + }. | ||
| 75 | + | ||
| 76 | +public define String | ||
| 77 | + to_String | ||
| 78 | + ( | ||
| 79 | + List(Network_entry) network_entries, | ||
| 80 | + String so_far | ||
| 81 | + )= | ||
| 82 | + if network_entries is | ||
| 83 | + { | ||
| 84 | + [] then so_far, | ||
| 85 | + [h . t] then | ||
| 86 | + if h is | ||
| 87 | + { | ||
| 88 | + blank then to_String(t, so_far+"\n"), | ||
| 89 | + comment(comment) then to_String(t, so_far + comment+"\n"), | ||
| 90 | + section(net_type, params) then to_String(t, so_far+"auto "+net_type.name+"\n")+ | ||
| 91 | + to_String(net_type)+"\n"+ | ||
| 92 | + _to_String( params, so_far) | ||
| 93 | + } | ||
| 94 | + }. | ||
| 95 | + | ||
| 96 | + | ||
| 97 | +define Maybe(Network_Parameter) | ||
| 98 | + extract_value | ||
| 99 | + ( | ||
| 100 | + String name, | ||
| 101 | + String left_line | ||
| 102 | + )= | ||
| 103 | + if find_char(left_line, '#', 0) is | ||
| 104 | + { | ||
| 105 | + failure then success(conf_nv(name, left_line)), | ||
| 106 | + success(idx) then | ||
| 107 | + if sub_string(left_line, 0, idx) is | ||
| 108 | + { | ||
| 109 | + failure then failure, | ||
| 110 | + success(value) then | ||
| 111 | + if sub_string(left_line, idx+1, (length(left_line) - (idx+1))) is | ||
| 112 | + { | ||
| 113 | + failure then success(conf_nv(name, value)), | ||
| 114 | + success(comment) then success(conf_nvc(name, value, comment)) | ||
| 115 | + } | ||
| 116 | + } | ||
| 117 | + }. | ||
| 118 | + | ||
| 119 | +define Maybe(Network_Parameter) | ||
| 120 | + get_network_parameter | ||
| 121 | + ( | ||
| 122 | + List(String) params | ||
| 123 | + )= | ||
| 124 | + if params is | ||
| 125 | + { | ||
| 126 | + [] then failure, | ||
| 127 | + [ name . t] then | ||
| 128 | + if nth((Int)0, name) is | ||
| 129 | + { | ||
| 130 | + failure then failure, | ||
| 131 | + success(c) then | ||
| 132 | + if c = '#' then | ||
| 133 | + success(comment(join(" ",params))) | ||
| 134 | + else | ||
| 135 | + extract_value(name, join(" ",t)) | ||
| 136 | + } | ||
| 137 | + }. | ||
| 138 | + | ||
| 139 | +define Maybe(Network_type) | ||
| 140 | + get_type | ||
| 141 | + ( | ||
| 142 | + String line, | ||
| 143 | + String name | ||
| 144 | + )= | ||
| 145 | + if split_by_token(line, ' ') is | ||
| 146 | + { | ||
| 147 | + [ ] then failure, | ||
| 148 | + [h . t] then | ||
| 149 | + if h = "iface" then | ||
| 150 | + if t is | ||
| 151 | + { | ||
| 152 | + [] then println("WARNING unamed iface section ["+name+"]");failure, | ||
| 153 | + [h . t] then | ||
| 154 | + //check if auto name is equal to iface name | ||
| 155 | + if h = name then | ||
| 156 | + if get_network_parameter(t) is | ||
| 157 | + { | ||
| 158 | + failure then failure, | ||
| 159 | + success(net_param) then success(iface(name, net_param)) | ||
| 160 | + } | ||
| 161 | + else | ||
| 162 | + failure | ||
| 163 | + } | ||
| 164 | + else failure | ||
| 165 | + }. | ||
| 166 | + | ||
| 167 | +define List(Network_Parameter) | ||
| 168 | + parse_section_body | ||
| 169 | + ( | ||
| 170 | + Stream s, | ||
| 171 | + List(Network_Parameter) so_far | ||
| 172 | + )= | ||
| 173 | + if read_line(s) is | ||
| 174 | + { | ||
| 175 | + failure then reverse(so_far), | ||
| 176 | + success(line) then | ||
| 177 | + if get_network_parameter(split_by_token(line, ' ')) is | ||
| 178 | + { | ||
| 179 | + failure then reverse(so_far), | ||
| 180 | + success(param) then parse_section_body(s,[param . so_far]) | ||
| 181 | + } | ||
| 182 | + }. | ||
| 183 | + | ||
| 184 | +define Network_entry | ||
| 185 | + parse_section | ||
| 186 | + ( | ||
| 187 | + Stream s, | ||
| 188 | + String line | ||
| 189 | + ) = | ||
| 190 | + if split_by_token(line, ' ') is | ||
| 191 | + { | ||
| 192 | + [ ] then blank, | ||
| 193 | + [h . t] then | ||
| 194 | + if h = "auto" then | ||
| 195 | + if t is | ||
| 196 | + { | ||
| 197 | + [] then println("WARNING unamed section ["+h+"]");blank, | ||
| 198 | + [ name . t ] then | ||
| 199 | + //read the second line of the section where the type is located i.e. iface | ||
| 200 | + if read_line(s) is | ||
| 201 | + { | ||
| 202 | + failure then blank, | ||
| 203 | + success(type_line) then | ||
| 204 | + if get_type(type_line, name) is | ||
| 205 | + { | ||
| 206 | + failure then blank, | ||
| 207 | + success(type_entry) then | ||
| 208 | + section(type_entry, parse_section_body(s,[])) | ||
| 209 | + } | ||
| 210 | + } | ||
| 211 | + } | ||
| 212 | + else | ||
| 213 | + blank | ||
| 214 | + }. | ||
| 215 | + | ||
| 216 | + | ||
| 217 | +define Network_entry | ||
| 218 | + read_interface_entry | ||
| 219 | + ( | ||
| 220 | + Stream s, | ||
| 221 | + String line | ||
| 222 | + )= | ||
| 223 | + //check for comment | ||
| 224 | + if nth((Int)0,line) is | ||
| 225 | + { | ||
| 226 | + failure then blank, | ||
| 227 | + success(c) then | ||
| 228 | + if c = '#' then | ||
| 229 | + comment(line) | ||
| 230 | + else | ||
| 231 | + parse_section(s, line) | ||
| 232 | + }. | ||
| 233 | + | ||
| 234 | +define List(Network_entry) | ||
| 235 | + read_interface_entries | ||
| 236 | + ( | ||
| 237 | + Stream s, | ||
| 238 | + List(Network_entry) so_far | ||
| 239 | + )= | ||
| 240 | + if read_line(s) is | ||
| 241 | + { | ||
| 242 | + failure then reverse(so_far), | ||
| 243 | + success(line) then println(line);read_interface_entries(s, [read_interface_entry(s, trim(line)) . so_far]) | ||
| 244 | + }. | ||
| 245 | + | ||
| 246 | +public define List(Network_entry) | ||
| 247 | + read_interface | ||
| 248 | + ( | ||
| 249 | + String interface_file | ||
| 250 | + ) = | ||
| 251 | + if (Maybe(RStream))file(interface_file, read) is | ||
| 252 | + { | ||
| 253 | + failure then [], //nothing to read | ||
| 254 | + success(f) then //parse file for each entry | ||
| 255 | + read_interface_entries(make_stream(f), []) | ||
| 256 | + }. | ||
| 257 | + | ||
| 258 | +public define String | ||
| 259 | + dump_interface_file | ||
| 260 | + = | ||
| 261 | + to_String(read_interface(etc_directory+"/network/interfaces"),""). | ||
| 262 | + | ||
| 263 | +public define Maybe(One) | ||
| 264 | + write_interface | ||
| 265 | + ( | ||
| 266 | + String interface_file | ||
| 267 | + )= | ||
| 268 | + if (Maybe(RWStream))file(etc_directory+"/network/interfaces_bis", new) is | ||
| 269 | + { | ||
| 270 | + failure then failure, | ||
| 271 | + success(f) then success(forget(reliable_write(f, to_byte_array(dump_interface_file)))) | ||
| 272 | + }. | ||
| 273 | + | ||
| 274 | + | ||
| 275 | + | ||
| 276 | + | ||
| 277 | +global define One | ||
| 278 | + test_file | ||
| 279 | + ( | ||
| 280 | + List(String) args | ||
| 281 | + )= | ||
| 282 | + if write_interface(dump_interface_file) is | ||
| 283 | + { | ||
| 284 | + failure then unique, | ||
| 285 | + success(_) then unique | ||
| 286 | + } | ||
| 287 | + . | ||
| 288 | + |