1 ! Copyright (C) 2010 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors combinators constructors io.encodings.utf8
4 io.files kernel math math.parser sequences splitting
8 TUPLE: network ip netmask ;
9 CONSTRUCTOR: network ( ip netmask -- network ) ;
13 { ndots integer initial: 1 }
14 { timeout integer initial: 5 }
15 { attempts integer initial: 2 }
16 rotate? no-check-names? inet6? ;
18 CONSTRUCTOR: options ( -- options ) ;
20 TUPLE: resolv.conf nameserver domain search sortlist options ;
22 CONSTRUCTOR: resolv.conf ( -- resolv.conf )
23 V{ } clone >>nameserver
31 : trim-blanks ( string -- string' ) [ blank? ] trim ;
33 : parse-nameserver ( resolv.conf string -- resolv.conf )
35 [ trim-blanks ] map harvest over nameserver>> push-all ;
37 : parse-domain ( resolv.conf string -- resolv.conf )
39 [ trim-blanks ] map harvest over domain>> push-all ;
41 : parse-search ( resolv.conf string -- resolv.conf )
43 [ trim-blanks ] map harvest over search>> push-all ;
45 : parse-sortlist ( resolv.conf string -- resolv.conf )
47 [ trim-blanks "/" split1 <network> ] map >>sortlist ;
49 ERROR: unsupported-resolv.conf-option string ;
51 : parse-integer ( string -- n )
52 trim-blanks ":" ?head drop trim-blanks string>number ;
54 : parse-option ( resolv.conf string -- resolv.conf )
55 [ dup options>> ] dip trim-blanks {
56 { [ "debug" ?head ] [ drop t >>debug? ] }
57 { [ "ndots:" ?head ] [ parse-integer >>ndots ] }
58 { [ "timeout" ?head ] [ parse-integer >>timeout ] }
59 { [ "attempts" ?head ] [ parse-integer >>attempts ] }
60 { [ "rotate" ?head ] [ drop t >>rotate? ] }
61 { [ "no-check-names" ?head ] [ drop t >>no-check-names? ] }
62 { [ "inet6" ?head ] [ drop t >>inet6? ] }
63 [ unsupported-resolv.conf-option ]
66 ERROR: unsupported-resolv.conf-line string ;
68 : parse-resolv.conf-line ( resolv.conf string -- resolv.conf )
70 { [ "nameserver" ?head ] [ parse-nameserver ] }
71 { [ "domain" ?head ] [ parse-domain ] }
72 { [ "search" ?head ] [ parse-search ] }
73 { [ "sortlist" ?head ] [ parse-sortlist ] }
74 { [ "options" ?head ] [ parse-option ] }
75 [ unsupported-resolv.conf-line ]
80 : parse-resolve.conf ( path -- resolv.conf )
83 [ [ blank? ] trim ] map harvest
84 [ "#" head? not ] filter
85 [ parse-resolv.conf-line ] each ;
87 : default-resolv.conf ( -- resolv.conf )
88 "/etc/resolv.conf" parse-resolve.conf ;