From e3f69995194b93c4db8a64a2c01a0e8957d24fa9 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Sun, 14 Aug 2016 11:43:02 -0700 Subject: [PATCH] etc-hosts: new vocab. --- extra/etc-hosts/authors.txt | 1 + extra/etc-hosts/etc-hosts.factor | 29 +++++++++++++++++++++++++++++ extra/etc-hosts/summary.txt | 1 + 3 files changed, 31 insertions(+) create mode 100644 extra/etc-hosts/authors.txt create mode 100644 extra/etc-hosts/etc-hosts.factor create mode 100644 extra/etc-hosts/summary.txt diff --git a/extra/etc-hosts/authors.txt b/extra/etc-hosts/authors.txt new file mode 100644 index 0000000000..e091bb8164 --- /dev/null +++ b/extra/etc-hosts/authors.txt @@ -0,0 +1 @@ +John Benediktsson diff --git a/extra/etc-hosts/etc-hosts.factor b/extra/etc-hosts/etc-hosts.factor new file mode 100644 index 0000000000..6bdd6f8060 --- /dev/null +++ b/extra/etc-hosts/etc-hosts.factor @@ -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 ; diff --git a/extra/etc-hosts/summary.txt b/extra/etc-hosts/summary.txt new file mode 100644 index 0000000000..0a595a2873 --- /dev/null +++ b/extra/etc-hosts/summary.txt @@ -0,0 +1 @@ +Parsing the /etc/hosts file