libs/core: Added target parameter to luci.sys.httpget
[project/luci.git] / libs / sys / luasrc / sys.lua
index 970dcff..557a7ce 100644 (file)
@@ -24,14 +24,26 @@ limitations under the License.
 
 ]]--
 
+
+local io    = require "io"
+local os    = require "os"
+local posix = require "posix"
+local table = require "table"
+
+local luci  = {}
+luci.util   = require "luci.util"
+luci.fs     = require "luci.fs"
+luci.ip     = require "luci.ip"
+
+local tonumber, ipairs, pairs = tonumber, ipairs, pairs
+
+
 --- LuCI Linux and POSIX system utilities.
-module("luci.sys", package.seeall)
-require("posix")
-require("luci.util")
-require("luci.fs")
-require("luci.ip")
+module "luci.sys"
+
 
 --- Invoke the luci-flash executable to write an image to the flash memory.
+-- @param image                Local path or URL to image file
 -- @param kpattern     Pattern of files to keep over flash process
 -- @return                     Return value of os.execute()
 function flash(image, kpattern)
@@ -94,10 +106,16 @@ end
 --- Returns the contents of a documented referred by an URL.
 -- @param url   The URL to retrieve
 -- @param stream Return a stream instead of a buffer
+-- @param target Directly write to target file name
 -- @return             String containing the contents of given the URL
-function httpget(url, stream)
-       local source = stream and io.open or luci.util.exec
-       return source("wget -qO- '"..url:gsub("'", "").."'")
+function httpget(url, stream, target)
+       if not target then
+               local source = stream and io.open or luci.util.exec
+               return source("wget -qO- '"..url:gsub("'", "").."'")
+       else
+               return os.execute("wget -qO '%s' '%s'" %
+                       {target:gsub("'", ""), url:gsub("'", "")})
+       end
 end
 
 --- Returns the system load average values.
@@ -432,8 +450,9 @@ end
 
 --- Get iwlist scan output from all wireless devices.
 -- @return     Table of tables contaiing all scan results
-function wifi.iwscan()
-       local cnt = luci.util.exec("iwlist scan 2>/dev/null")
+function wifi.iwscan(iface)
+       local siface = iface or ""
+       local cnt = luci.util.exec("iwlist "..siface.." scan 2>/dev/null")
        local iws = {}
 
        for i, l in pairs(luci.util.split(luci.util.trim(cnt), "\n\n")) do
@@ -451,7 +470,7 @@ function wifi.iwscan()
                end
        end
 
-       return iws
+       return iface and (iws[iface] or {}) or iws
 end