3 LuCI - Configuration Bind Interface - Datatype Tests
4 (c) 2010 Jo-Philipp Wich <xm@subsignal.org>
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
16 local fs = require "nixio.fs"
17 local ip = require "luci.ip"
18 local math = require "math"
19 local util = require "luci.util"
20 local tonumber, type = tonumber, type
23 module "luci.cbi.datatypes"
27 if val == "1" or val == "yes" or val == "on" or val == "true" then
29 elseif val == "0" or val == "no" or val == "off" or val == "false" then
31 elseif val == "" or val == nil then
38 function uinteger(val)
39 local n = tonumber(val)
40 if n ~= nil and math.floor(n) == n and n >= 0 then
48 local n = tonumber(val)
49 if n ~= nil and math.floor(n) == n then
57 local n = tonumber(val)
58 return ( n ~= nil and n >= 0 )
62 return ( tonumber(val) ~= nil )
66 return ip4addr(val) or ip6addr(val)
69 function neg_ipaddr(v)
70 if type(v) == "string" then
71 v = v:gsub("^%s*!", "")
73 return v and ipaddr(v)
78 return ip.IPv4(val) and true or false
84 function neg_ip4addr(v)
85 if type(v) == "string" then
86 v = v:gsub("^%s*!", "")
88 return v and ip4addr(v)
91 function ip4prefix(val)
93 return ( val and val >= 0 and val <= 32 )
98 return ip.IPv6(val) and true or false
104 function ip6prefix(val)
106 return ( val and val >= 0 and val <= 128 )
111 return ( val and val >= 0 and val <= 65535 )
114 function portrange(val)
115 local p1, p2 = val:match("^(%d+)%-(%d+)$")
116 if p1 and p2 and port(p1) and port(p2) then
123 function macaddr(val)
124 if val and val:match(
125 "^[a-fA-F0-9]+:[a-fA-F0-9]+:[a-fA-F0-9]+:" ..
126 "[a-fA-F0-9]+:[a-fA-F0-9]+:[a-fA-F0-9]+$"
128 local parts = util.split( val, ":" )
131 parts[i] = tonumber( parts[i], 16 )
132 if parts[i] < 0 or parts[i] > 255 then
143 function hostname(val)
144 if val and (#val < 254) and (
145 val:match("^[a-zA-Z]+$") or
146 (val:match("^[a-zA-Z0-9][a-zA-Z0-9%-%.]*[a-zA-Z0-9]$") and
147 val:match("[^0-9%.]"))
155 return hostname(val) or ipaddr(val)
158 function network(val)
159 return uciname(val) or host(val)
164 return (val:match("^[a-fA-F0-9]+$") ~= nil)
166 return (#val >= 8) and (#val <= 63)
171 if val:sub(1, 2) == "s:" then
175 if (#val == 10) or (#val == 26) then
176 return (val:match("^[a-fA-F0-9]+$") ~= nil)
178 return (#val == 5) or (#val == 13)
183 return true -- Everything qualifies as valid string
186 function directory( val, seen )
187 local s = fs.stat(val)
190 if s and not seen[s.ino] then
192 if s.type == "dir" then
194 elseif s.type == "lnk" then
195 return directory( fs.readlink(val), seen )
202 function file( val, seen )
203 local s = fs.stat(val)
206 if s and not seen[s.ino] then
208 if s.type == "reg" then
210 elseif s.type == "lnk" then
211 return file( fs.readlink(val), seen )
218 function device( val, seen )
219 local s = fs.stat(val)
222 if s and not seen[s.ino] then
224 if s.type == "chr" or s.type == "blk" then
226 elseif s.type == "lnk" then
227 return device( fs.readlink(val), seen )
234 function uciname(val)
235 return (val:match("^[a-zA-Z0-9_]+$") ~= nil)
238 function neg_network_ip4addr(val)
239 if type(v) == "string" then
240 v = v:gsub("^%s*!", "")
241 return (uciname(v) or ip4addr(v))
245 function range(val, min, max)
250 if val ~= nil and min ~= nil and max ~= nil then
251 return ((val >= min) and (val <= max))
257 function min(val, min)
261 if val ~= nil and min ~= nil then
268 function max(val, max)
272 if val ~= nil and max ~= nil then
279 function neg(val, what)
280 if what and type(_M[what]) == "function" then
281 return _M[what](val:gsub("^%s*!%s*", ""))
287 function list(val, what, ...)
288 if type(val) == "string" and what and type(_M[what]) == "function" then
289 for val in val:gmatch("%S+") do
290 if not _M[what](val, ...) then