--- /dev/null
+John Benediktsson
--- /dev/null
+USING: arrays assocs environment hashtables io.encodings.utf8
+io.files io.pathnames kernel memoize sequences splitting system
+unicode ;
+
+IN: etc-hosts
+
+HOOK: hosts-path os ( -- path )
+
+M: windows hosts-path
+ "SystemRoot" os-env "System32/drivers/etc/hosts" append-path ;
+
+M: unix hosts-path "/etc/hosts" ;
+
+: parse-hosts ( path -- hosts )
+ utf8 file-lines
+ [ [ blank? ] trim ] map harvest
+ [ "#" head? ] reject
+ [
+ [ blank? ] split1-when
+ [ blank? ] split-when harvest
+ ] H{ } map>assoc ;
+
+MEMO: system-hosts ( -- hosts ) hosts-path parse-hosts ;
+
+: host>ips ( host -- ips )
+ system-hosts [ member? nip ] with assoc-filter keys ;
+
+: ip>hosts ( ip -- hosts )
+ system-hosts at ;
--- /dev/null
+Parsing the /etc/hosts file
--- /dev/null
+file formats
--- /dev/null
+Doug Coleman
--- /dev/null
+! Copyright (C) 2019 Doug Coleman.
+! See http://factorcode.org/license.txt for BSD license.
+USING: tools.test resolv-conf ;
+IN: resolv-conf.tests
+{
+ T{ resolv.conf
+ { nameserver V{ "127.0.0.53" } }
+ { domain V{ } }
+ { lookup V{ } }
+ { search V{ "localdomain" } }
+ { sortlist V{ } }
+ { options T{ options { edns0? t } } }
+ }
+} [
+ "nameserver 127.0.0.53
+ options edns0
+ search localdomain" string>resolv.conf
+] unit-test
\ No newline at end of file
--- /dev/null
+! Copyright (C) 2010 Doug Coleman.
+! See http://factorcode.org/license.txt for BSD license.
+USING: accessors combinators constructors io.encodings.utf8
+io.files kernel math math.parser sequences splitting
+unicode ;
+IN: resolv-conf
+
+TUPLE: network ip netmask ;
+CONSTRUCTOR: <network> network ( ip netmask -- network ) ;
+
+TUPLE: options
+debug?
+edns0?
+insecure1?
+insecure2?
+{ ndots integer initial: 1 }
+{ timeout integer initial: 5 }
+{ attempts integer initial: 2 }
+rotate? no-check-names? inet6? tcp? ;
+
+CONSTRUCTOR: <options> options ( -- options ) ;
+
+TUPLE: resolv.conf nameserver domain lookup search sortlist options ;
+
+CONSTRUCTOR: <resolv.conf> resolv.conf ( -- resolv.conf )
+ V{ } clone >>nameserver
+ V{ } clone >>domain
+ V{ } clone >>search
+ V{ } clone >>sortlist
+ V{ } clone >>lookup
+ <options> >>options ;
+
+<PRIVATE
+
+: trim-blanks ( string -- string' ) [ blank? ] trim ;
+
+: split-line ( resolv.conf string -- resolv.conf seq resolv.conf )
+ trim-blanks " " split
+ [ trim-blanks ] map harvest over ;
+
+: parse-nameserver ( resolv.conf string -- resolv.conf )
+ split-line nameserver>> push-all ;
+
+: parse-domain ( resolv.conf string -- resolv.conf )
+ split-line domain>> push-all ;
+
+: parse-lookup ( resolv.conf string -- resolv.conf )
+ split-line lookup>> push-all ;
+
+: parse-search ( resolv.conf string -- resolv.conf )
+ split-line search>> push-all ;
+
+: parse-sortlist ( resolv.conf string -- resolv.conf )
+ trim-blanks " " split
+ [ trim-blanks "/" split1 <network> ] map >>sortlist ;
+
+ERROR: unsupported-resolv.conf-option string ;
+
+: parse-integer ( string -- n )
+ trim-blanks ":" ?head drop trim-blanks string>number ;
+
+: parse-option ( resolv.conf string -- resolv.conf )
+ [ dup options>> ] dip trim-blanks {
+ { [ "debug" ?head ] [ drop t >>debug? ] }
+ { [ "ndots:" ?head ] [ parse-integer >>ndots ] }
+ { [ "timeout" ?head ] [ parse-integer >>timeout ] }
+ { [ "attempts" ?head ] [ parse-integer >>attempts ] }
+ { [ "rotate" ?head ] [ drop t >>rotate? ] }
+ { [ "no-check-names" ?head ] [ drop t >>no-check-names? ] }
+ { [ "inet6" ?head ] [ drop t >>inet6? ] }
+ { [ "edns0" ?head ] [ drop t >>edns0? ] }
+ [ unsupported-resolv.conf-option ]
+ } cond drop ;
+
+ERROR: unsupported-resolv.conf-line string ;
+
+: parse-resolv.conf-line ( resolv.conf string -- resolv.conf )
+ {
+ { [ "nameserver" ?head ] [ parse-nameserver ] }
+ { [ "domain" ?head ] [ parse-domain ] }
+ { [ "lookup" ?head ] [ parse-lookup ] }
+ { [ "search" ?head ] [ parse-search ] }
+ { [ "sortlist" ?head ] [ parse-sortlist ] }
+ { [ "options" ?head ] [ parse-option ] }
+ [ unsupported-resolv.conf-line ]
+ } cond ;
+
+PRIVATE>
+
+: lines>resolv.conf ( lines -- resolv.conf )
+ [ <resolv.conf> ] dip
+ [ [ blank? ] trim ] map harvest
+ [ "#" head? ] reject
+ [ parse-resolv.conf-line ] each ;
+
+: string>resolv.conf ( string -- resolv.conf )
+ string-lines lines>resolv.conf ;
+
+: path>resolv.conf ( path -- resolv.conf )
+ utf8 file-lines lines>resolv.conf ;
+
+: default-resolv.conf ( -- resolv.conf )
+ "/etc/resolv.conf" path>resolv.conf ;
--- /dev/null
+#
+# Mac OS X Notice
+#
+# This file is not used by the host name and address resolution
+# or the DNS query routing mechanisms used by most processes on
+# this Mac OS X system.
+ #
+ # This file is automatically generated.
+ #
+ nameserver 8.8.8.8
+ domain hmm.lol.com
+ search a.com b.com c.com
+
+sortlist 130.155.160.0/255.255.240.0 130.155.0.0 131.155.160.0/255.255.240.0 130.155.0.1
+
+ options debug
+ options ndots:10
+ options timeout:11
+ options attempts : 12
+ options rotate
+ options no-check-names
+ options inet6
+
+
+
+
+
+
--- /dev/null
+os
+parsing
+file formats
+++ /dev/null
-John Benediktsson
+++ /dev/null
-USING: arrays assocs environment hashtables io.encodings.utf8
-io.files io.pathnames kernel memoize sequences splitting system
-unicode ;
-
-IN: etc-hosts
-
-HOOK: hosts-path os ( -- path )
-
-M: windows hosts-path
- "SystemRoot" os-env "System32/drivers/etc/hosts" append-path ;
-
-M: unix hosts-path "/etc/hosts" ;
-
-: parse-hosts ( path -- hosts )
- utf8 file-lines
- [ [ blank? ] trim ] map harvest
- [ "#" head? ] reject
- [
- [ blank? ] split1-when
- [ blank? ] split-when harvest
- ] H{ } map>assoc ;
-
-MEMO: system-hosts ( -- hosts ) hosts-path parse-hosts ;
-
-: host>ips ( host -- ips )
- system-hosts [ member? nip ] with assoc-filter keys ;
-
-: ip>hosts ( ip -- hosts )
- system-hosts at ;
+++ /dev/null
-Parsing the /etc/hosts file
+++ /dev/null
-file formats
+++ /dev/null
-Doug Coleman
+++ /dev/null
-! Copyright (C) 2019 Doug Coleman.
-! See http://factorcode.org/license.txt for BSD license.
-USING: tools.test resolv-conf ;
-IN: resolv-conf.tests
-{
- T{ resolv.conf
- { nameserver V{ "127.0.0.53" } }
- { domain V{ } }
- { lookup V{ } }
- { search V{ "localdomain" } }
- { sortlist V{ } }
- { options T{ options { edns0? t } } }
- }
-} [
- "nameserver 127.0.0.53
- options edns0
- search localdomain" string>resolv.conf
-] unit-test
\ No newline at end of file
+++ /dev/null
-! Copyright (C) 2010 Doug Coleman.
-! See http://factorcode.org/license.txt for BSD license.
-USING: accessors combinators constructors io.encodings.utf8
-io.files kernel math math.parser sequences splitting
-unicode ;
-IN: resolv-conf
-
-TUPLE: network ip netmask ;
-CONSTRUCTOR: <network> network ( ip netmask -- network ) ;
-
-TUPLE: options
-debug?
-edns0?
-insecure1?
-insecure2?
-{ ndots integer initial: 1 }
-{ timeout integer initial: 5 }
-{ attempts integer initial: 2 }
-rotate? no-check-names? inet6? tcp? ;
-
-CONSTRUCTOR: <options> options ( -- options ) ;
-
-TUPLE: resolv.conf nameserver domain lookup search sortlist options ;
-
-CONSTRUCTOR: <resolv.conf> resolv.conf ( -- resolv.conf )
- V{ } clone >>nameserver
- V{ } clone >>domain
- V{ } clone >>search
- V{ } clone >>sortlist
- V{ } clone >>lookup
- <options> >>options ;
-
-<PRIVATE
-
-: trim-blanks ( string -- string' ) [ blank? ] trim ;
-
-: split-line ( resolv.conf string -- resolv.conf seq resolv.conf )
- trim-blanks " " split
- [ trim-blanks ] map harvest over ;
-
-: parse-nameserver ( resolv.conf string -- resolv.conf )
- split-line nameserver>> push-all ;
-
-: parse-domain ( resolv.conf string -- resolv.conf )
- split-line domain>> push-all ;
-
-: parse-lookup ( resolv.conf string -- resolv.conf )
- split-line lookup>> push-all ;
-
-: parse-search ( resolv.conf string -- resolv.conf )
- split-line search>> push-all ;
-
-: parse-sortlist ( resolv.conf string -- resolv.conf )
- trim-blanks " " split
- [ trim-blanks "/" split1 <network> ] map >>sortlist ;
-
-ERROR: unsupported-resolv.conf-option string ;
-
-: parse-integer ( string -- n )
- trim-blanks ":" ?head drop trim-blanks string>number ;
-
-: parse-option ( resolv.conf string -- resolv.conf )
- [ dup options>> ] dip trim-blanks {
- { [ "debug" ?head ] [ drop t >>debug? ] }
- { [ "ndots:" ?head ] [ parse-integer >>ndots ] }
- { [ "timeout" ?head ] [ parse-integer >>timeout ] }
- { [ "attempts" ?head ] [ parse-integer >>attempts ] }
- { [ "rotate" ?head ] [ drop t >>rotate? ] }
- { [ "no-check-names" ?head ] [ drop t >>no-check-names? ] }
- { [ "inet6" ?head ] [ drop t >>inet6? ] }
- { [ "edns0" ?head ] [ drop t >>edns0? ] }
- [ unsupported-resolv.conf-option ]
- } cond drop ;
-
-ERROR: unsupported-resolv.conf-line string ;
-
-: parse-resolv.conf-line ( resolv.conf string -- resolv.conf )
- {
- { [ "nameserver" ?head ] [ parse-nameserver ] }
- { [ "domain" ?head ] [ parse-domain ] }
- { [ "lookup" ?head ] [ parse-lookup ] }
- { [ "search" ?head ] [ parse-search ] }
- { [ "sortlist" ?head ] [ parse-sortlist ] }
- { [ "options" ?head ] [ parse-option ] }
- [ unsupported-resolv.conf-line ]
- } cond ;
-
-PRIVATE>
-
-: lines>resolv.conf ( lines -- resolv.conf )
- [ <resolv.conf> ] dip
- [ [ blank? ] trim ] map harvest
- [ "#" head? ] reject
- [ parse-resolv.conf-line ] each ;
-
-: string>resolv.conf ( string -- resolv.conf )
- string-lines lines>resolv.conf ;
-
-: path>resolv.conf ( path -- resolv.conf )
- utf8 file-lines lines>resolv.conf ;
-
-: default-resolv.conf ( -- resolv.conf )
- "/etc/resolv.conf" path>resolv.conf ;
+++ /dev/null
-#
-# Mac OS X Notice
-#
-# This file is not used by the host name and address resolution
-# or the DNS query routing mechanisms used by most processes on
-# this Mac OS X system.
- #
- # This file is automatically generated.
- #
- nameserver 8.8.8.8
- domain hmm.lol.com
- search a.com b.com c.com
-
-sortlist 130.155.160.0/255.255.240.0 130.155.0.0 131.155.160.0/255.255.240.0 130.155.0.1
-
- options debug
- options ndots:10
- options timeout:11
- options attempts : 12
- options rotate
- options no-check-names
- options inet6
-
-
-
-
-
-
+++ /dev/null
-os
-parsing
-file formats