network_interface_file.anubis 6.52 KB
/*
 * Created by PyramIDE.
 * User: Alexis
 * 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/kero_loggers.anubis
read tools/streams.anubis
read calexium_lib/net_services_protocols/logger_service.anubis

read kero_constants.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
  )=
  if interface_entries is
  {
    []       then so_far,
    [h . t]  then
      _to_String(t, so_far + to_String(h))
  }.

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+"\n"),
      section(net_type, params) then  to_String(t, so_far+"auto "+net_type.name+"\n")+
                                        to_String(net_type)+"\n"+
                                        _to_String( params, so_far)
    }
  }.
  
  
define Maybe(Network_Parameter)
  extract_value
  (
    String  name,
    String  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 success(conf_nv(name, value)),
          success(comment)  then success(conf_nvc(name, value, comment))
        }
      }
  }.
  
define Maybe(Network_Parameter)
  get_network_parameter
  (
    List(String)  params
  )=
  if params is
  {
    []          then failure,
    [ name . t] then 
      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,
    [h . t] then
      if h = "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 get_network_parameter(split_by_token(line, ' ')) is
      {
        failure         then reverse(so_far),
        success(param)  then parse_section_body(s,[param . 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
            //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
                  if get_type(type_line, name) is
                  {
                    failure             then blank,
                    success(type_entry) then
                      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, 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, trim(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 String
  dump_interface_file
  =
  to_String(read_interface(etc_directory+"/network/interfaces"),"").
  
public define Maybe(One)
  write_interface
  (
    String  interface_file
  )=
  if (Maybe(RWStream))file(etc_directory+"/network/interfaces_bis", new) is
  {
    failure     then failure,
    success(f)  then success(forget(reliable_write(f, to_byte_array(dump_interface_file))))
  }.




global define One
 test_file
 (
  List(String)  args
 )=
 if write_interface(dump_interface_file) is
 {
  failure     then  unique,
  success(_)  then unique
 }
 .