summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDaniil Baturin <daniil@baturin.org>2019-06-30 04:25:48 +0700
committerDaniil Baturin <daniil@baturin.org>2019-06-30 04:25:48 +0700
commit49aa04970e4afe28552f6e6679f2abacd3851bc0 (patch)
treeb60c6e07cdfb65d240fdf9ea264b1efa10c24bf4 /lib
parent083214cb040c447d55851dfbd3e81769513f0752 (diff)
downloadlibvyosconfig-49aa04970e4afe28552f6e6679f2abacd3851bc0.tar.gz
libvyosconfig-49aa04970e4afe28552f6e6679f2abacd3851bc0.zip
Switch from linking with vyconf plus adhocery to dedicated vyos1x-config package.
Diffstat (limited to 'lib')
-rw-r--r--lib/bindings.ml36
-rw-r--r--lib/vyos1x_renderer.ml93
2 files changed, 23 insertions, 106 deletions
diff --git a/lib/bindings.ml b/lib/bindings.ml
index 49f8ef7..1734a52 100644
--- a/lib/bindings.ml
+++ b/lib/bindings.ml
@@ -1,11 +1,18 @@
open Ctypes
open Foreign
-module CT = Config_tree
+open Vyos1x
+module CT = Config_tree
let error_message = ref ""
+let make_syntax_error pos err =
+ match pos with
+ | None -> Printf.sprintf "Syntax error: %s" err
+ | Some (l, c) ->
+ Printf.sprintf "Syntax error on line %d, character %d: %s" l c err
+
let to_json_str = fun s -> `String s
let make_config_tree name = Ctypes.Root.create (CT.make name)
@@ -14,21 +21,24 @@ let destroy c_ptr =
Root.release c_ptr
let from_string s =
- try
- error_message := "";
- let config = Vyos1x_parser.config Vyos1x_lexer.token (Lexing.from_string s) in
- Ctypes.Root.create config
- with
- | Failure s | Vyos1x_lexer.Error s -> error_message := s; Ctypes.null
+ try
+ error_message := "";
+ let config = Parser.from_string s in
+ Ctypes.Root.create config
+ with
+ | Failure s -> error_message := s; Ctypes.null
+ | Util.Syntax_error (pos, err) ->
+ let msg = make_syntax_error pos err in
+ error_message := msg; Ctypes.null
| _ -> error_message := "Parse error"; Ctypes.null
let get_error () = !error_message
-let render c_ptr =
- Vyos1x_renderer.render (Root.get c_ptr)
+let render_config c_ptr =
+ CT.render_config (Root.get c_ptr)
let render_commands c_ptr =
- CT.render_commands ~alwayssort:true ~sortchildren:true (Root.get c_ptr) []
+ CT.render_commands (Root.get c_ptr) []
let set_add_value c_ptr path value =
let ct = Root.get c_ptr in
@@ -84,14 +94,14 @@ let set_tag c_ptr path =
let ct = Root.get c_ptr in
let path = Pcre.split ~rex:(Pcre.regexp "\\s+") path in
try
- Root.set c_ptr (CT.set_ephemeral ct path true);
+ Root.set c_ptr (CT.set_tag ct path true);
0 (* return 0 *)
with _ -> 1
let is_tag c_ptr path =
let ct = Root.get c_ptr in
let path = Pcre.split ~rex:(Pcre.regexp "\\s+") path in
- if (CT.is_ephemeral ct path) then 1 else 0
+ if (CT.is_tag ct path) then 1 else 0
let exists c_ptr path =
let ct = Root.get c_ptr in
@@ -144,7 +154,7 @@ struct
let () = I.internal "destroy" ((ptr void) @-> returning void) destroy
let () = I.internal "from_string" (string @-> returning (ptr void)) from_string
let () = I.internal "get_error" (void @-> returning string) get_error
- let () = I.internal "to_string" ((ptr void) @-> returning string) render
+ let () = I.internal "to_string" ((ptr void) @-> returning string) render_config
let () = I.internal "to_commands" ((ptr void) @-> returning string) render_commands
let () = I.internal "set_add_value" ((ptr void) @-> string @-> string @-> returning int) set_add_value
let () = I.internal "set_replace_value" ((ptr void) @-> string @-> string @-> returning int) set_replace_value
diff --git a/lib/vyos1x_renderer.ml b/lib/vyos1x_renderer.ml
deleted file mode 100644
index 7ff4c4c..0000000
--- a/lib/vyos1x_renderer.ml
+++ /dev/null
@@ -1,93 +0,0 @@
-(* The renderer makes two assumptions about the invariants of the config files:
- that top level nodes are never tag or leaf nodes,
- and that immediate children of tag nodes are never themselves tag nodes.
-
- They are true in all existing VyOS configs and configs with those invariant
- broken will never load, so these assumptions are safe to make when
- processing existing configs. In configs built from scratch, the user is
- responsible for its validness.
-
- The original loader behaviour with tag nodes is strange: deep down, after
- config loading, they are indistinguishable from any other nodes, but at load
- time, they fail to validate unless they are formatted as tag nodes in the
- config file, that is, "ethernet eth0 { ..." as opposed to "ethernet { eth0 { ...".
-
- Since Vyconf makes no distinction between normal nodes and tag nodes other than
- at set command validation and formatting time, I reused the ephemeral flag
- which is never used in VyOS 1.x for marking nodes as tag nodes at parsing time.
-
- Note that if non-leaf nodes have values, they will be rendered in _some_ way.
- Such a config would never appear in a live VyOS and cannot load, so this case
- is considered undefined behaviour.
-
- *)
-
-module CT = Config_tree
-module VT = Vytree
-
-let make_indent indent level = String.make (level * indent) ' '
-
-let render_values indent_str name values =
- match values with
- | [] -> Printf.sprintf "%s%s { }\n" indent_str name
- | [v] -> Printf.sprintf "%s%s \"%s\"\n" indent_str name (String.escaped v)
- | _ ->
- let rendered = List.map (fun s -> Printf.sprintf "%s%s \"%s\"" indent_str name (String.escaped s)) values in
- let rendered = String.concat "\n" rendered in
- Printf.sprintf "%s\n" rendered
-
-let render_comment indent c =
- match c with
- | None -> ""
- | Some c -> Printf.sprintf "%s/* %s */\n" indent c
-
-let rec render_node indent level node =
- let open CT in
- let indent_str = make_indent indent level in
- let name = VT.name_of_node node in
- let data = VT.data_of_node node in
- let is_tag = data.ephemeral (* sic! look in the parser *) in
- let comment = render_comment indent_str data.comment in
- let values = render_values indent_str name data.values in
- let children = VT.children_of_node node in
- match children with
- | [] -> Printf.sprintf "%s%s" comment values
- | _ ->
- if is_tag then
- begin
- let inner = List.map (render_tag_node_child indent level name) children in
- String.concat "" inner
- end
- else
- begin
- let inner = List.map (render_node indent (level + 1)) children in
- let inner = String.concat "" inner in
- Printf.sprintf "%s%s%s {\n%s%s}\n" comment indent_str name inner indent_str
- end
-
-
-and render_tag_node_child indent level parent node =
- let open CT in
- let indent_str = make_indent indent level in
- let name = VT.name_of_node node in
- let data = VT.data_of_node node in
- let comment = render_comment indent_str data.comment in
- let values = render_values indent_str name data.values in
- let children = VT.children_of_node node in
- match children with
- (* This produces too much whitespace due to indent_str from values,
- but the issue is cosmetic *)
- | [] -> Printf.sprintf "%s%s%s %s" comment indent_str parent values
- | _ ->
- (* Exploiting the fact that immediate children of tag nodes are
- never themselves tag nodes *)
- let inner = List.map (render_node indent (level + 1)) children in
- let inner = String.concat "" inner in
- Printf.sprintf "%s%s%s %s {\n%s%s}\n" comment indent_str parent name inner indent_str
-
-let render node =
- let children = Vytree.children_of_node node in
- let child_configs = List.map (render_node 4 0) children in
- String.concat "" child_configs
-
-