diff options
Diffstat (limited to 'readkey.ha')
| -rw-r--r-- | readkey.ha | 65 |
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"); +}; |
