summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Hurst <ark@mansus.space>2025-10-19 11:08:03 +0200
committerJulian Hurst <ark@mansus.space>2025-10-19 11:08:28 +0200
commit4671163d0854e9acc281885f6aec76d3df174168 (patch)
treeaaa11360a1226c5f6c05096317571e83dd0f3f51
downloadreadkey-4671163d0854e9acc281885f6aec76d3df174168.tar.gz
Initial commit
-rw-r--r--readkey.ha65
1 files changed, 65 insertions, 0 deletions
diff --git a/readkey.ha b/readkey.ha
new file mode 100644
index 0000000..16581ce
--- /dev/null
+++ b/readkey.ha
@@ -0,0 +1,65 @@
+use fmt;
+use os;
+use io;
+use format::ini;
+use fs;
+use dirs;
+use path;
+
+def READKEY_ENV_FILE = "READKEY_ENV_FILE";
+
+export fn main() void = {
+ let path = match (os::getenv(READKEY_ENV_FILE)) {
+ case void =>
+ let buf = match (path::init()) {
+ case let b: path::buffer =>
+ yield b;
+ case let e: path::error =>
+ fmt::fatal(path::strerror(e));
+ };
+ yield match (path::set(&buf, dirs::config("readkey"), "env")) {
+ case let s: str =>
+ yield s;
+ case let e: path::error =>
+ fmt::fatal(path::strerror(e));
+ };
+ case let s: str =>
+ yield s;
+ };
+
+ if (!os::exists(path)) {
+ fmt::fatalf("{}: File not found", path);
+ };
+
+ let args = os::args;
+ if (len(args) != 2) {
+ fmt::fatalf("USAGE: {} key", args[0]);
+ };
+
+ let envfile = match (os::open(path)) {
+ case let f: io::file =>
+ yield f;
+ case let e: fs::error =>
+ fmt::fatal(fs::strerror(e));
+ };
+ defer io::close(envfile)!;
+
+ let sc = ini::scan(envfile);
+ defer ini::finish(&sc);
+
+ for (let entry => next(&sc)) {
+ if (entry.1 == args[1]) {
+ fmt::println(entry.2)!;
+ return;
+ };
+ };
+};
+
+fn next(sc: *ini::scanner) (ini::entry | io::EOF) = match (ini::next(sc)) {
+case let e: (ini::entry | io::EOF) =>
+ return e;
+case let e: ini::error =>
+ fmt::fatal(ini::strerror(e));
+case nomem =>
+ fmt::fatal("No memory left");
+};