From aa5a442284b2caf09cfbad0bf79756da757e5179 Mon Sep 17 00:00:00 2001 From: Julian Hurst Date: Tue, 22 Oct 2024 18:37:21 +0200 Subject: Initial commit --- cmd/statrep/generic.ha | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 cmd/statrep/generic.ha (limited to 'cmd/statrep/generic.ha') diff --git a/cmd/statrep/generic.ha b/cmd/statrep/generic.ha new file mode 100644 index 0000000..fb61ab0 --- /dev/null +++ b/cmd/statrep/generic.ha @@ -0,0 +1,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); +}; -- cgit v1.2.3