summaryrefslogtreecommitdiff
path: root/cmd/statrep/generic.ha
blob: fb61ab06bc57ced3c050da829958193ec2fbf11e (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
use os::exec;
use shlex;
use strings;
use io;
use os;

type generic = struct {
	section: section,
	cmd: str,
	value: str,
};

fn buildgeneric(cmd: str) generic = {
	let gen = generic {
		section = section {
			label = "generic: ",
		},
		cmd = cmd,
		...
	};
	updategeneric(&gen)!;
	return gen;
};

fn updategeneric(gen: *generic) (void | shlex::syntaxerr | exec::error) = {
	const spl = shlex::split(gen.cmd)?;
	const cmd = exec::cmd(spl[0], spl[1..]...)?;
	let pipe = exec::pipe();
	exec::addfile(&cmd, os::stdout_file, pipe.1);
	let proc = exec::start(&cmd)?;
	io::close(pipe.1)!;
	let data = io::drain(pipe.0)!;
	defer free(data);
	io::close(pipe.0)!;
	exec::wait(&proc)!;
	gen.value = strings::dup(strings::trim(strings::fromutf8(data)!));
};

fn finishgeneric(gen: *generic) void = {
	free(gen.value);
};