Moved luci.sys.libpath to luci.util
[project/luci.git] / libs / core / luasrc / sys.lua
index 217a36b..14a2f29 100644 (file)
@@ -24,52 +24,13 @@ limitations under the License.
 
 ]]--
 
---- LuCI system utilities.
+--- LuCI Linux and POSIX system utilities.
 module("luci.sys", package.seeall)
 require("posix")
 require("luci.bits")
 require("luci.util")
 require("luci.fs")
 
---- Test wheather the current system is operating in big endian mode.
--- @return     Boolean value indicating wheather system is big endian
-function bigendian()
-       local fp = io.open("/bin/sh")
-       fp:seek("set", 5)
-       local be = (fp:read(1):byte() ~= 1)
-       fp:close()
-       return be
-end
-
---- Execute given commandline and gather stdout.
--- @param command      String containing command to execute
--- @return                     String containing the command's stdout
-function exec(command)
-       local pp   = io.popen(command)
-       local data = pp:read("*a")
-       pp:close()
-
-       return data
-end
-
---- Execute given commandline and gather stdout.
--- @param command      String containing the command to execute
--- @return                     Table containing the command's stdout splitted up in lines
-function execl(command)
-       local pp   = io.popen(command)
-       local line = ""
-       local data = {}
-
-       while true do
-               line = pp:read()
-               if (line == nil) then break end
-               table.insert(data, line)
-       end
-       pp:close()
-
-       return data
-end
-
 --- Invoke the luci-flash executable to write an image to the flash memory.
 -- @param kpattern     Pattern of files to keep over flash process
 -- @return                     Return value of os.execute()
@@ -101,16 +62,12 @@ function hostname()
 end
 
 --- Returns the contents of a documented referred by an URL.
--- @param url  The URL to retrieve
+-- @param url   The URL to retrieve
+-- @param stream Return a stream instead of a buffer
 -- @return             String containing the contents of given the URL
-function httpget(url)
-       return exec("wget -qO- '"..url:gsub("'", "").."'")
-end
-
---- Returns the absolute path to LuCI base directory.
--- @return             String containing the directory path
-function libpath()
-       return luci.fs.dirname(require("luci.debug").__file__)
+function httpget(url, stream)
+       local source = stream and io.open or luci.util.exec
+       return source("wget -qO- '"..url:gsub("'", "").."'")
 end
 
 --- Returns the system load average values.
@@ -150,21 +107,21 @@ function sysinfo()
        local c7 = "cat /proc/meminfo|grep MemFree|awk {' print $2 '} 2>/dev/null"
        local c8 = "cat /proc/meminfo|grep Buffers|awk {' print $2 '} 2>/dev/null"
 
-       local system = luci.util.trim(exec(c1))
+       local system = luci.util.trim(luci.util.exec(c1))
        local model = ""
-       local memtotal = luci.util.trim(exec(c5))
-       local memcached = luci.util.trim(exec(c6))
-       local memfree = luci.util.trim(exec(c7))
-       local membuffers = luci.util.trim(exec(c8))
+       local memtotal = luci.util.trim(luci.util.exec(c5))
+       local memcached = luci.util.trim(luci.util.exec(c6))
+       local memfree = luci.util.trim(luci.util.exec(c7))
+       local membuffers = luci.util.trim(luci.util.exec(c8))
        local perc_memfree = math.floor((memfree/memtotal)*100)
        local perc_membuffers = math.floor((membuffers/memtotal)*100)
        local perc_memcached = math.floor((memcached/memtotal)*100)
 
        if system == "" then
-               system = luci.util.trim(exec(c2))
-               model = luci.util.trim(exec(c3))
+               system = luci.util.trim(luci.util.exec(c2))
+               model = luci.util.trim(luci.util.exec(c3))
        else
-               model = luci.util.trim(exec(c4))
+               model = luci.util.trim(luci.util.exec(c4))
        end
 
        return system, model, memtotal, memcached, membuffers, memfree, perc_memfree, perc_membuffers, perc_memcached
@@ -173,7 +130,7 @@ end
 --- Retrieves the output of the "logread" command.
 -- @return     String containing the current log buffer
 function syslog()
-       return exec("logread")
+       return luci.util.exec("logread")
 end
 
 --- Generates a random id with specified length.
@@ -309,7 +266,7 @@ function net.hexip4(hex, be)
                return nil
        end
 
-       be = be or bigendian()
+       be = be or luci.util.bigendian()
 
        local hexdec = luci.bits.Hex2Dec
 
@@ -446,7 +403,7 @@ wifi = {}
 --- Get iwconfig output for all wireless devices.
 -- @return     Table of tables containing the iwconfing output for each wifi device
 function wifi.getiwconfig()
-       local cnt = exec("/usr/sbin/iwconfig 2>/dev/null")
+       local cnt = luci.util.exec("/usr/sbin/iwconfig 2>/dev/null")
        local iwc = {}
 
        for i, l in pairs(luci.util.split(luci.util.trim(cnt), "\n\n")) do
@@ -463,7 +420,7 @@ end
 --- Get iwlist scan output from all wireless devices.
 -- @return     Table of tables contaiing all scan results
 function wifi.iwscan()
-       local cnt = exec("iwlist scan 2>/dev/null")
+       local cnt = luci.util.exec("iwlist scan 2>/dev/null")
        local iws = {}
 
        for i, l in pairs(luci.util.split(luci.util.trim(cnt), "\n\n")) do