summaryrefslogtreecommitdiff
path: root/readkey.ha
blob: 16581ce22d4c9231e34e94e0de700a372290fd94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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");
};