From 78e1242601d064cc96996a7534a04004e9824d48 Mon Sep 17 00:00:00 2001 From: mutlusun Date: Wed, 25 Jan 2017 17:47:33 +0100 Subject: [PATCH] helpers: add sysctl helper for freebsd --- helpers.lua | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/helpers.lua b/helpers.lua index 0555e95..3288e64 100644 --- a/helpers.lua +++ b/helpers.lua @@ -196,6 +196,44 @@ function helpers.scroll(text, maxlen, widget) end -- }}} +-- {{{ Return result from one sysctl variable as string +function helpers.sysctl(path) + local fd = io.popen("sysctl -n " .. helpers.shellquote(path)) + + if not fd then + return + end + + local ret = fd:read() + + fd:close() + + return ret +end +-- }}} + +-- {{{ Return result from multiple sysctl variables as table +function helpers.sysctl_table(syspath) + return setmetatable({ _path = syspath }, + { __index = function(table, index) + local path = "sysctl -n " .. helpers.shellquote(table._path .. "." .. index) + local f = io.popen(path) + if f then + local s = f:read("*all") + f:close() + if select(2, s:gsub("\n", "\n")) > 1 then + local o = { _path = path} + setmetatable(o, getmetatable(table)) + return o + else + return s + end + end + end + }) +end +-- }}} + return helpers -- }}}