X-Git-Url: https://git.archive.openwrt.org/?a=blobdiff_plain;f=libs%2Fuvl%2Fluasrc%2Fuvl%2Fdatatypes.lua;h=c6a5de39888af222035996eea21f32e4c04bb2cc;hb=800a2630497e1ec2c61199b3bc645d778c714529;hp=3dfe37dbef82c9252eaddfc9d1625fc22f84956a;hpb=2b767fc45c564b94df4153d99d5501bb208e6fe5;p=project%2Fluci.git diff --git a/libs/uvl/luasrc/uvl/datatypes.lua b/libs/uvl/luasrc/uvl/datatypes.lua index 3dfe37dbe..c6a5de398 100644 --- a/libs/uvl/luasrc/uvl/datatypes.lua +++ b/libs/uvl/luasrc/uvl/datatypes.lua @@ -14,17 +14,22 @@ $Id$ ]]-- -module( "luci.uvl.datatypes", package.seeall ) +local fs = require "nixio.fs" +local ip = require "luci.ip" +local math = require "math" +local util = require "luci.util" -require("luci.fs") -require("luci.ip") -require("luci.util") +local tonumber = tonumber + +module "luci.uvl.datatypes" function boolean( val ) - if val == "1" or val == "yes" or val == "on" then + if val == "1" or val == "yes" or val == "on" or val == "true" then + return true + elseif val == "0" or val == "no" or val == "off" or val == "false" then return true - elseif val == "0" or val == "no" or val == "off" then + elseif val == "" or val == nil then return true end @@ -59,7 +64,7 @@ end function ip4addr( val ) if val then - return luci.ip.IPv4(val) and true or false + return ip.IPv4(val) and true or false end return false @@ -72,7 +77,7 @@ end function ip6addr( val ) if val then - return luci.ip.IPv6(val) and true or false + return ip.IPv6(val) and true or false end return false @@ -83,12 +88,26 @@ function ip6prefix( val ) return ( val and val >= 0 and val <= 128 ) end +function port( val ) + val = tonumber(val) + return ( val and val >= 1 and val <= 65535 ) +end + +function portrange( val ) + local p1, p2 = val:match("^(%d+)%-(%d+)$") + if p1 and p2 and port(p1) and port(p2) then + return true + else + return port(val) + end +end + function macaddr( val ) if val and val:match( "^[a-fA-F0-9]+:[a-fA-F0-9]+:[a-fA-F0-9]+:" .. "[a-fA-F0-9]+:[a-fA-F0-9]+:[a-fA-F0-9]+$" ) then - local parts = luci.util.split( val, ":" ) + local parts = util.split( val, ":" ) for i = 1,6 do parts[i] = tonumber( parts[i], 16 ) @@ -111,20 +130,24 @@ function hostname( val ) return false end +function host( val ) + return hostname(val) or ipaddr(val) +end + function string( val ) return true -- Everything qualifies as valid string end function directory( val, seen ) - local s = luci.fs.stat( val ) + local s = fs.stat( val ) seen = seen or { } if s and not seen[s.ino] then seen[s.ino] = true - if s.type == "directory" then + if s.type == "dir" then return true - elseif s.type == "link" then - return directory( luci.fs.readlink(val), seen ) + elseif s.type == "lnk" then + return directory( fs.readlink(val), seen ) end end @@ -132,15 +155,31 @@ function directory( val, seen ) end function file( val, seen ) - local s = luci.fs.stat( val ) + local s = fs.stat( val ) + seen = seen or { } + + if s and not seen[s.ino] then + seen[s.ino] = true + if s.type == "reg" then + return true + elseif s.type == "lnk" then + return file( fs.readlink(val), seen ) + end + end + + return false +end + +function device( val, seen ) + local s = fs.stat( val ) seen = seen or { } if s and not seen[s.ino] then seen[s.ino] = true - if s.type == "regular" then + if s.type == "chr" or s.type == "blk" then return true - elseif s.type == "link" then - return file( luci.fs.readlink(val), seen ) + elseif s.type == "lnk" then + return device( fs.readlink(val), seen ) end end