libs/sys: protect iwinfo loading and return stub if module is not present
authorJo-Philipp Wich <jow@openwrt.org>
Sat, 27 Nov 2010 16:57:39 +0000 (16:57 +0000)
committerJo-Philipp Wich <jow@openwrt.org>
Sat, 27 Nov 2010 16:57:39 +0000 (16:57 +0000)
libs/sys/luasrc/sys.lua

index d1137b76a707804a05304d141234f17abc097ced..61da6f1cb41873c96b40a21e2fc0794147c18c8f 100644 (file)
@@ -624,7 +624,7 @@ wifi = {}
 -- @param ifname        String containing the interface name
 -- @return              A wrapped iwinfo object instance
 function wifi.getiwinfo(ifname)
-       local iwinfo = require "iwinfo"
+       local stat, iwinfo = pcall(require, "iwinfo")
 
        if ifname then
                local c = 0
@@ -652,19 +652,17 @@ function wifi.getiwinfo(ifname)
                                end)
                end
 
-               local t = iwinfo.type(ifname)
-               if t then
-                       local x = iwinfo[t]
-                       return setmetatable({}, {
-                               __index = function(t, k)
-                                       if k == "ifname" then
-                                               return ifname
-                                       elseif x[k] then
-                                               return x[k](ifname)
-                                       end
+               local t = stat and iwinfo.type(ifname)
+               local x = t and iwinfo[t] or { }
+               return setmetatable({}, {
+                       __index = function(t, k)
+                               if k == "ifname" then
+                                       return ifname
+                               elseif x[k] then
+                                       return x[k](ifname)
                                end
-                       })
-               end
+                       end
+               })
        end
 end