etc-hosts: new vocab.
parent
a36c0cb5ac
commit
e3f6999519
|
@ -0,0 +1 @@
|
||||||
|
John Benediktsson
|
|
@ -0,0 +1,29 @@
|
||||||
|
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 ;
|
|
@ -0,0 +1 @@
|
||||||
|
Parsing the /etc/hosts file
|
Loading…
Reference in New Issue