etc-hosts: new vocab.

char-rename
John Benediktsson 2016-08-14 11:43:02 -07:00
parent a36c0cb5ac
commit e3f6999519
3 changed files with 31 additions and 0 deletions

View File

@ -0,0 +1 @@
John Benediktsson

View File

@ -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 ;

View File

@ -0,0 +1 @@
Parsing the /etc/hosts file