⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fslib.lua.svn-base

📁 嵌入式无线路由系统openwrt的web配置工具
💻 SVN-BASE
字号:
-- Copyright (C) 2008 OpenRB.com---- FlashSYS function library-- Uses json and uci lua libraries--require('json')require('uci')-- gets data from enviromental variable and parses it using JSON if it's no nilfunction datajson(k)  k = k or 'FORM_data'  local env = os.getenv(k)  if (env ~= nil) then    return json.decode(env)  else    return nil  end end-- executes given proc and returns it's output either in raw format or JSON-decodedfunction proc(proc, raw)  if (proc == nil) then    return ''  end  -- open proc and read all at once  local f = io.popen(proc)  if (f == nil) then    return nil  end  local r = f:read('*all')  f:close()  -- raw must be equal to true if raw output is required  return (raw == true and r or json.decode(r))end-- checks if given ifname is wireless, returns nil or parent namefunction checkwifi(ifname)  -- if there's no %parent file, interface probably has no wext  local f = io.open('/proc/sys/net/' .. ifname .. '/%parent')  if (f == nil) then    return nil  end    -- read one line  local p = f:read('*line')  f:close()    return p end-- get wifi country code and madwifi config file namefunction wifigetcc()  local ccode, fname, fproc, fcont, s, e  fproc = io.popen('ls -1 /etc/modules.d/*-madwifi*')  if (fproc ~= nil) then    fname = fproc:read('*line')    fproc:close()    if (fname) then      fcont = readfile(fname)      s, e = fcont:find('countrycode=%d+')      if (s ~= nil) then        ccode = fcont:sub(s, e):gsub('countrycode=', '')      else        ccode = '0'      end    end  end  return ccode, fnameend-- convert ifname to config name using ucifunction iftocfg(ifname)  uci.load('network')  -- special case: bridge has no ifname in config file  if ifname:find('^br-') then    local cfgname = string.gsub(ifname, '^br--', '')    return uci.get('network', cfgname) ~= nil and cfgname or nil  end  local network = uci.get_all('network')  for cfgname, options in pairs(network) do    if (options['ifname'] == ifname) then      return cfgname    end  end    return nilend-- convert ifname to config name using rulesfunction iftocfgr(ifname)  ifname = string.gsub(ifname, '^br--', '')  ifname = string.gsub(ifname, '^eth', 'lan')  ifname = string.gsub(ifname, '^ath', 'wifi')  return ifname == 'lan0' and 'lan' or ifnameend-- uci load / get_all wrapper, takes care of config files that don't existfunction uci.init(file, section, type, force)  -- check config file  local fStat = uci.load(file)  local sStat = nil  force = force == true    if (fStat == false) then    -- file not found, create it    os.execute('touch /etc/config/' .. file)  else    -- found, check sectione existance    if (section ~= nil) then      sStat = uci.get_all(file, section)    else      sStat = uci.get_all(file)    end  end  -- section not found, create it  if ((section ~= nil and type ~= nil and sStat == nil) or force) then    uci.set(file, section, type)  end  return sStatend-- add new section and return idfunction uci.addsection(file, type)  local fStat = uci.load(file)  local section  if (fStat == false) then    os.execute('touch /etc/config/' .. file)  end  section = uci.add(file, type)  uci.revert(file)  uci.set(file, section, type)  uci.commit(file)  return sectionend-- uci won't set empty value, so we need to delete itfunction uci.setvalue(file, section, option, value)  if (value ~= nil and value ~= '') then    uci.set(file, section, option, value)  else    uci.delete(file, section, option)  endend-- return array of config datafunction uci.get_array(cfg, filter)  local i, t  t = {}  i = function(d)    table.insert(t, d)  end  uci.foreach(cfg, filter, i)  return tend-- return given string or empty string if nil was givenfunction toString(v)  return (v == nil and '' or v .. '')end-- /etc/init.d/service action wrapperfunction service(name, action)  action = action or 'restart'  os.execute('/etc/init.d/' .. name .. ' ' .. action)end-- split string by given patternfunction split(str, pat)  str = toString(str)  -- pattern not specified or is empty  if (pat == nil or pat == '') then    return {}  end  local t = {}   local fpat = "(.-)" .. pat  local last_end = 1  local s, e, cap = str:find(fpat, 1)  while (s) do    if s ~= 1 or cap ~= "" then      table.insert(t, cap)    end        last_end = e + 1    s, e, cap = str:find(fpat, last_end)  end  if (last_end <= #str) then    cap = str:sub(last_end)    table.insert(t, cap)  end    return tend-- /var/state/... search wrapperfunction findstate(file, matches, ret)  local f = io.open('/var/state/' .. file)  if (f == nil) then    return nil  end  local match = nil  while (true) do    local p = f:read('*line')    -- finished reading, found nothing    if (p == nil) then      break    end        local matched = true    local values = split(p, '[\.=]')        for pos, key in pairs(matches) do      if (key ~= nil and values[ pos ] ~= key) then        matched = false      end    end        if (matched == true) then      match = values[ ret ]      break    end  end    f:close()  return matchendfunction writefile(file, contents)  local f = io.open(file, 'w')  if (f == nil) then    return false  end  f:write(contents)  f:close()  return trueendfunction readfile(file)  local f = io.open(file, 'r')  if (f == nil) then    return false  end  local contents = f:read("*all")  f:close()  return contentsendfunction readlines(file, skip, proc)  local f = proc and io.popen(file, 'r') or io.open(file, 'r')  local t = {}  skip = skip and skip or 0  if (f ~= nil) then    while true do      line = f:read('*line')      if not line then break end      if (skip > 0) then        skip = skip - 1      else        table.insert(t, line)      end    end    f:close()  end   return tend-- check if given array contain given item function table.contains(t, item)  local i, x  for i, x in pairs(t) do    if item == x then      return true    end  end  return falseend-- remove duplicate item from given arrayfunction table.unique(t)  local n, i, x  n = {}  for i, x in ipairs(t) do    if (not table.contains(n, x)) then      table.insert(n, x)    end  end  return nend

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -