This commit is contained in:
swrup 2025-11-11 02:07:51 +01:00
parent aa2ff7b2f0
commit 2f3113f55d
11742 changed files with 1223940 additions and 0 deletions

View file

@ -0,0 +1,14 @@
let parse buf =
try
Resolvconf_state.reset ();
let buf =
if String.(get buf (pred (length buf))) = '\n' then buf else buf ^ "\n"
in
let lexbuf = Lexing.from_string buf in
Ok (Resolvconf_parser.resolvconf Resolvconf_lexer.lex lexbuf)
with
| Parsing.Parse_error ->
Error (`Msg (Fmt.str "parse error at line %d" Resolvconf_state.(state.lineno)))
| exn ->
Error (`Msg (Fmt.str "error at line %d: %s" Resolvconf_state.(state.lineno)
(Printexc.to_string exn)))

View file

@ -0,0 +1 @@
val parse : string -> ([ `Nameserver of Ipaddr.t ] list, [> `Msg of string ]) result

View file

@ -0,0 +1,9 @@
(library
(name dns_resolvconv)
(public_name dns-client.resolvconf)
(private_modules resolvconf_lexer resolvconf_parser resolvconf_state)
(libraries ipaddr fmt)
(wrapped false))
(ocamlyacc resolvconf_parser)
(ocamllex resolvconf_lexer)

View file

@ -0,0 +1,30 @@
{
open Resolvconf_state
open Resolvconf_parser
}
let ipv4 = (['0'-'9']+ '.' ['0'-'9']+ '.' ['0'-'9']+ '.' ['0'-'9']+) as contents
let ipv6 = (['0'-'9' 'a'-'f' 'A'-'F' ':']+) as contents
let zone_id = (['0'-'9' 'a'-'z' 'A'-'Z' '.' ]+) as contents
(* inspired by https://github.com/tailhook/resolv-conf/blob/master/src/grammar.rs *)
rule lex = parse
| "nameserver" { SNAMESERVER }
| "options" ([^'\n']*) '\n' { state.lineno <- state.lineno + 1 ; EOL }
| "search" ([^'\n']*) '\n' { state.lineno <- state.lineno + 1 ; EOL }
| "domain" ([^'\n']*) '\n' { state.lineno <- state.lineno + 1 ; EOL }
| "sortlist" ([^'\n']*) '\n' { state.lineno <- state.lineno + 1 ; EOL }
| "lookup" ([^'\n']*) '\n' { state.lineno <- state.lineno + 1 ; EOL }
| "family" ([^'\n']*) '\n' { state.lineno <- state.lineno + 1 ; EOL }
| [' ' '\t']+ { SPACE }
| ipv4 { IPV4 contents }
| ipv6 { IPV6 contents }
| '.' { DOT }
| ':' { COLON }
| '%' { PERCENT }
| [' ' '\t']* ('#' [^'\n']*)? '\n' { state.lineno <- state.lineno + 1 ; EOL }
| [' ' '\t']* (';' [^'\n']*)? '\n' { state.lineno <- state.lineno + 1 ; EOL }
| zone_id { ZONE_ID contents }
| eof { EOF }

View file

@ -0,0 +1,37 @@
%{
%}
%token EOF
%token EOL
%token SPACE
%token SNAMESERVER
%token DOT
%token COLON
%token PERCENT
%token <string> IPV4
%token <string> IPV6
%token <string> ZONE_ID
%start resolvconf
%type <[ `Nameserver of Ipaddr.t ] list> resolvconf
%%
resolvconf: lines EOF { List.rev $1 }
lines:
/* nothing */ { [] }
| lines EOL { $1 }
| lines nameserver EOL { $2 :: $1 }
s: SPACE {} | s SPACE {}
ipv4: IPV4 { Ipaddr.V4.of_string_exn $1 }
ipv6:
IPV6 { Ipaddr.V6.of_string_exn $1 }
| IPV6 PERCENT ZONE_ID { Ipaddr.V6.of_string_exn $1 }
nameserver:
SNAMESERVER s ipv4 { `Nameserver (Ipaddr.V4 $3) }
| SNAMESERVER s ipv6 { `Nameserver (Ipaddr.V6 $3) }

View file

@ -0,0 +1,11 @@
(* State variables for the parser & lexer *)
type parserstate = {
mutable lineno : int ;
}
let state = {
lineno = 1 ;
}
let reset () =
state.lineno <- 1