summaryrefslogtreecommitdiff
path: root/lib/std/env+plan9.myr
diff options
context:
space:
mode:
Diffstat (limited to 'lib/std/env+plan9.myr')
-rw-r--r--lib/std/env+plan9.myr56
1 files changed, 56 insertions, 0 deletions
diff --git a/lib/std/env+plan9.myr b/lib/std/env+plan9.myr
new file mode 100644
index 0000000..4a95ed7
--- /dev/null
+++ b/lib/std/env+plan9.myr
@@ -0,0 +1,56 @@
+use sys
+
+use "alloc.use"
+use "die.use"
+use "extremum.use"
+use "fmt.use"
+use "option.use"
+use "result.use"
+use "slcp.use"
+use "sldup.use"
+use "sleq.use"
+use "slpush.use"
+use "slurp.use"
+
+pkg std =
+ const getenv : (name : byte[:] -> option(byte[:]))
+ const getenvv : (name : byte[:], default : byte[:] -> byte[:])
+;;
+
+var envkey : byte[:][:]
+var envval : byte[:][:]
+
+const envfind = {key
+ var i
+ for i = 0; i < envkey.len; i++
+ if std.sleq(envkey[i], key)
+ -> `Some envval[i]
+ ;;
+ ;;
+ -> `None
+}
+
+const getenv = {name
+ var buf : byte[128]
+ var s
+
+ match envfind(name)
+ | `Some val: -> `Some val
+ | `None:
+ s = bfmt(buf[:], "/env/{}", name)
+ match std.slurp(s)
+ | `Fail m: -> `None
+ | `Ok data:
+ envkey = slpush(envkey, sldup(name))
+ envval = slpush(envval, data)
+ -> `Some data
+ ;;
+ ;;
+}
+
+const getenvv = {name, default
+ match getenv(name)
+ | `Some val: -> val
+ | `None: -> default
+ ;;
+}