]> gitweb.factorcode.org Git - factor.git/commitdiff
etc-hosts: new vocab.
authorJohn Benediktsson <mrjbq7@gmail.com>
Sun, 14 Aug 2016 18:43:02 +0000 (11:43 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Sun, 14 Aug 2016 18:43:20 +0000 (11:43 -0700)
extra/etc-hosts/authors.txt [new file with mode: 0644]
extra/etc-hosts/etc-hosts.factor [new file with mode: 0644]
extra/etc-hosts/summary.txt [new file with mode: 0644]

diff --git a/extra/etc-hosts/authors.txt b/extra/etc-hosts/authors.txt
new file mode 100644 (file)
index 0000000..e091bb8
--- /dev/null
@@ -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 (file)
index 0000000..6bdd6f8
--- /dev/null
@@ -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 (file)
index 0000000..0a595a2
--- /dev/null
@@ -0,0 +1 @@
+Parsing the /etc/hosts file