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; }; }; fmt::fatalf("{}: key not found", args[1]); }; 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"); };