Commit 2bb9ce7510f6c29e1e75ae3b17737d00928e18a5
1 parent
899f1ab0
Fix read and write network interface file for Linux environment.
Showing
1 changed file
with
77 additions
and
50 deletions
Show diff stats
os_tools/linux/config/network_interface_file.anubis
| 1 | /* | 1 | /* |
| 2 | * Created by PyramIDE. | 2 | * Created by PyramIDE. |
| 3 | - * User: Alexis | 3 | + * User: Alexis & Totoro |
| 4 | * Date: 03/09/2012 | 4 | * Date: 03/09/2012 |
| 5 | * Time: 16:05 | 5 | * Time: 16:05 |
| 6 | * | 6 | * |
| 7 | * To change this template use Tools | Options | Coding | Edit Standard Headers. | 7 | * To change this template use Tools | Options | Coding | Edit Standard Headers. |
| 8 | */ | 8 | */ |
| 9 | 9 | ||
| 10 | - | ||
| 11 | read system/logger.anubis | 10 | read system/logger.anubis |
| 12 | read system/string.anubis | 11 | read system/string.anubis |
| 13 | read tools/basis.anubis | 12 | read tools/basis.anubis |
| 14 | -read tools/kero_loggers.anubis | ||
| 15 | read tools/streams.anubis | 13 | read tools/streams.anubis |
| 16 | -read calexium_lib/net_services_protocols/logger_service.anubis | ||
| 17 | - | ||
| 18 | -read kero_constants.anubis | ||
| 19 | 14 | ||
| 20 | delete_entry | 15 | delete_entry |
| 21 | find_entry | 16 | find_entry |
| @@ -64,20 +59,20 @@ define String | @@ -64,20 +59,20 @@ define String | ||
| 64 | _to_String | 59 | _to_String |
| 65 | ( | 60 | ( |
| 66 | List(Network_Parameter) interface_entries, | 61 | List(Network_Parameter) interface_entries, |
| 67 | - String so_far | 62 | + String so_far //this is so far of the section, not the general file |
| 68 | )= | 63 | )= |
| 69 | if interface_entries is | 64 | if interface_entries is |
| 70 | { | 65 | { |
| 71 | [] then so_far, | 66 | [] then so_far, |
| 72 | [h . t] then | 67 | [h . t] then |
| 73 | - _to_String(t, so_far + to_String(h)) | 68 | + _to_String(t, so_far + "\t"+to_String(h)+"\n") |
| 74 | }. | 69 | }. |
| 75 | 70 | ||
| 76 | public define String | 71 | public define String |
| 77 | to_String | 72 | to_String |
| 78 | ( | 73 | ( |
| 79 | List(Network_entry) network_entries, | 74 | List(Network_entry) network_entries, |
| 80 | - String so_far | 75 | + String so_far |
| 81 | )= | 76 | )= |
| 82 | if network_entries is | 77 | if network_entries is |
| 83 | { | 78 | { |
| @@ -86,10 +81,10 @@ public define String | @@ -86,10 +81,10 @@ public define String | ||
| 86 | if h is | 81 | if h is |
| 87 | { | 82 | { |
| 88 | blank then to_String(t, so_far+"\n"), | 83 | 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")+ | 84 | + comment(comment) then to_String(t, so_far + comment), |
| 85 | + section(net_type, params) then to_String(t, so_far+"auto "+net_type.name+"\n"+ | ||
| 91 | to_String(net_type)+"\n"+ | 86 | to_String(net_type)+"\n"+ |
| 92 | - _to_String( params, so_far) | 87 | + _to_String( params, "")) |
| 93 | } | 88 | } |
| 94 | }. | 89 | }. |
| 95 | 90 | ||
| @@ -100,6 +95,7 @@ define Maybe(Network_Parameter) | @@ -100,6 +95,7 @@ define Maybe(Network_Parameter) | ||
| 100 | String name, | 95 | String name, |
| 101 | String left_line | 96 | String left_line |
| 102 | )= | 97 | )= |
| 98 | + //println(" Extract value for name ["+name+"] in ["+left_line+"]"); | ||
| 103 | if find_char(left_line, '#', 0) is | 99 | if find_char(left_line, '#', 0) is |
| 104 | { | 100 | { |
| 105 | failure then success(conf_nv(name, left_line)), | 101 | failure then success(conf_nv(name, left_line)), |
| @@ -110,8 +106,13 @@ define Maybe(Network_Parameter) | @@ -110,8 +106,13 @@ define Maybe(Network_Parameter) | ||
| 110 | success(value) then | 106 | success(value) then |
| 111 | if sub_string(left_line, idx+1, (length(left_line) - (idx+1))) is | 107 | if sub_string(left_line, idx+1, (length(left_line) - (idx+1))) is |
| 112 | { | 108 | { |
| 113 | - failure then success(conf_nv(name, value)), | ||
| 114 | - success(comment) then success(conf_nvc(name, value, comment)) | 109 | + failure then |
| 110 | + //println("conf_nv found name="+name+" value="+value); | ||
| 111 | + success(conf_nv(name, value)), | ||
| 112 | + | ||
| 113 | + success(comment) then | ||
| 114 | + //println("conf_nvc found name="+name+" value="+value+" comment="+comment); | ||
| 115 | + success(conf_nvc(name, value, comment)) | ||
| 115 | } | 116 | } |
| 116 | } | 117 | } |
| 117 | }. | 118 | }. |
| @@ -125,6 +126,7 @@ define Maybe(Network_Parameter) | @@ -125,6 +126,7 @@ define Maybe(Network_Parameter) | ||
| 125 | { | 126 | { |
| 126 | [] then failure, | 127 | [] then failure, |
| 127 | [ name . t] then | 128 | [ name . t] then |
| 129 | + //println("Get network parameter for ["+name+"]"); | ||
| 128 | if nth((Int)0, name) is | 130 | if nth((Int)0, name) is |
| 129 | { | 131 | { |
| 130 | failure then failure, | 132 | failure then failure, |
| @@ -145,17 +147,19 @@ define Maybe(Network_type) | @@ -145,17 +147,19 @@ define Maybe(Network_type) | ||
| 145 | if split_by_token(line, ' ') is | 147 | if split_by_token(line, ' ') is |
| 146 | { | 148 | { |
| 147 | [ ] then failure, | 149 | [ ] then failure, |
| 148 | - [h . t] then | ||
| 149 | - if h = "iface" then | 150 | + [type . t] then |
| 151 | + if type = "iface" then | ||
| 150 | if t is | 152 | if t is |
| 151 | { | 153 | { |
| 152 | - [] then println("WARNING unamed iface section ["+name+"]");failure, | 154 | + [] then |
| 155 | + //println("WARNING unamed iface section ["+name+"]"); | ||
| 156 | + failure, | ||
| 153 | [h . t] then | 157 | [h . t] then |
| 154 | //check if auto name is equal to iface name | 158 | //check if auto name is equal to iface name |
| 155 | if h = name then | 159 | if h = name then |
| 156 | if get_network_parameter(t) is | 160 | if get_network_parameter(t) is |
| 157 | { | 161 | { |
| 158 | - failure then failure, | 162 | + failure then failure, |
| 159 | success(net_param) then success(iface(name, net_param)) | 163 | success(net_param) then success(iface(name, net_param)) |
| 160 | } | 164 | } |
| 161 | else | 165 | else |
| @@ -170,16 +174,30 @@ define List(Network_Parameter) | @@ -170,16 +174,30 @@ define List(Network_Parameter) | ||
| 170 | Stream s, | 174 | Stream s, |
| 171 | List(Network_Parameter) so_far | 175 | List(Network_Parameter) so_far |
| 172 | )= | 176 | )= |
| 173 | - if read_line(s) is | ||
| 174 | - { | 177 | + if read_line(s) is |
| 178 | + { | ||
| 175 | failure then reverse(so_far), | 179 | failure then reverse(so_far), |
| 176 | success(line) then | 180 | 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 | - }. | 181 | + if nth((Int)0, line) is |
| 182 | + { | ||
| 183 | + failure then reverse(so_far), | ||
| 184 | + success(c) then | ||
| 185 | + if c = ' ' | c='\t' then | ||
| 186 | + //it seem to be a a real section body entry we try it | ||
| 187 | + //println("parse section line ["+trim(line)+"]"); | ||
| 188 | + if get_network_parameter(split_by_token(trim(line), ' ')) is | ||
| 189 | + { | ||
| 190 | + failure then reverse(so_far), | ||
| 191 | + success(param) then parse_section_body(s,[param . so_far]) | ||
| 192 | + } | ||
| 193 | + else | ||
| 194 | + // itsn't part of section body, so we stop here and unput the line read | ||
| 195 | + //and return the found Network_Parameter | ||
| 196 | + //println("Not body part ["+trim(line)+"]"); | ||
| 197 | + unput_line(s, line); | ||
| 198 | + reverse(so_far) | ||
| 199 | + } | ||
| 200 | + }. | ||
| 183 | 201 | ||
| 184 | define Network_entry | 202 | define Network_entry |
| 185 | parse_section | 203 | parse_section |
| @@ -194,17 +212,24 @@ define Network_entry | @@ -194,17 +212,24 @@ define Network_entry | ||
| 194 | if h = "auto" then | 212 | if h = "auto" then |
| 195 | if t is | 213 | if t is |
| 196 | { | 214 | { |
| 197 | - [] then println("WARNING unamed section ["+h+"]");blank, | 215 | + [] then |
| 216 | + //println("WARNING unamed section ["+h+"]"); | ||
| 217 | + blank, | ||
| 198 | [ name . t ] then | 218 | [ name . t ] then |
| 219 | + //println("auto found with name ["+name+"]"); | ||
| 199 | //read the second line of the section where the type is located i.e. iface | 220 | //read the second line of the section where the type is located i.e. iface |
| 200 | if read_line(s) is | 221 | if read_line(s) is |
| 201 | { | 222 | { |
| 202 | failure then blank, | 223 | failure then blank, |
| 203 | success(type_line) then | 224 | success(type_line) then |
| 204 | - if get_type(type_line, name) is | 225 | + //println("get type line for "+name+"["+trim(type_line)+"]"); |
| 226 | + if get_type(trim(type_line), name) is | ||
| 205 | { | 227 | { |
| 206 | - failure then blank, | 228 | + failure then |
| 229 | + //println("type not found for "+name); | ||
| 230 | + blank, | ||
| 207 | success(type_entry) then | 231 | success(type_entry) then |
| 232 | + //println("type found, parse for section"); | ||
| 208 | section(type_entry, parse_section_body(s,[])) | 233 | section(type_entry, parse_section_body(s,[])) |
| 209 | } | 234 | } |
| 210 | } | 235 | } |
| @@ -228,7 +253,7 @@ define Network_entry | @@ -228,7 +253,7 @@ define Network_entry | ||
| 228 | if c = '#' then | 253 | if c = '#' then |
| 229 | comment(line) | 254 | comment(line) |
| 230 | else | 255 | else |
| 231 | - parse_section(s, line) | 256 | + parse_section(s, trim(line)) |
| 232 | }. | 257 | }. |
| 233 | 258 | ||
| 234 | define List(Network_entry) | 259 | define List(Network_entry) |
| @@ -240,7 +265,9 @@ define List(Network_entry) | @@ -240,7 +265,9 @@ define List(Network_entry) | ||
| 240 | if read_line(s) is | 265 | if read_line(s) is |
| 241 | { | 266 | { |
| 242 | failure then reverse(so_far), | 267 | failure then reverse(so_far), |
| 243 | - success(line) then println(line);read_interface_entries(s, [read_interface_entry(s, trim(line)) . so_far]) | 268 | + success(line) then |
| 269 | + //println("["+line+"]"); | ||
| 270 | + read_interface_entries(s, [read_interface_entry(s, line) . so_far]) | ||
| 244 | }. | 271 | }. |
| 245 | 272 | ||
| 246 | public define List(Network_entry) | 273 | public define List(Network_entry) |
| @@ -255,34 +282,34 @@ public define List(Network_entry) | @@ -255,34 +282,34 @@ public define List(Network_entry) | ||
| 255 | read_interface_entries(make_stream(f), []) | 282 | read_interface_entries(make_stream(f), []) |
| 256 | }. | 283 | }. |
| 257 | 284 | ||
| 258 | -public define String | ||
| 259 | - dump_interface_file | ||
| 260 | - = | ||
| 261 | - to_String(read_interface(etc_directory+"/network/interfaces"),""). | ||
| 262 | - | ||
| 263 | public define Maybe(One) | 285 | public define Maybe(One) |
| 264 | write_interface | 286 | write_interface |
| 265 | ( | 287 | ( |
| 266 | - String interface_file | 288 | + String file_name, |
| 289 | + List(Network_entry) entries | ||
| 267 | )= | 290 | )= |
| 268 | - if (Maybe(RWStream))file(etc_directory+"/network/interfaces_bis", new) is | 291 | + if (Maybe(RWStream))file(file_name, new) is |
| 269 | { | 292 | { |
| 270 | failure then failure, | 293 | failure then failure, |
| 271 | - success(f) then success(forget(reliable_write(f, to_byte_array(dump_interface_file)))) | 294 | + success(f) then success(forget(reliable_write(f, to_byte_array(to_String(entries, ""))))) |
| 272 | }. | 295 | }. |
| 273 | 296 | ||
| 274 | 297 | ||
| 275 | - | ||
| 276 | - | 298 | + /** here tool for testing |
| 299 | + * anbexec parse_interface <given_file> | ||
| 300 | + * will produce a dump of given_file into file named as original with ".dump" as an extension | ||
| 301 | + */ | ||
| 302 | + | ||
| 277 | global define One | 303 | 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 | - } | 304 | + parse_interface |
| 305 | + ( | ||
| 306 | + List(String) args | ||
| 307 | + )= | ||
| 308 | + if args is | ||
| 309 | + { | ||
| 310 | + [] then print("usage : parse_interface <file>"), | ||
| 311 | + [ file_name . t ] then | ||
| 312 | + forget(write_interface(file_name+".dump",read_interface(file_name))) | ||
| 313 | + } | ||
| 287 | . | 314 | . |
| 288 | 315 |