|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local registry = {} |
|
|
local lua51 = rawget(_G,'setfenv') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local function execute (cmd) |
|
|
local res1,res2,res2 = os.execute(cmd) |
|
|
if lua51 then |
|
|
return res1==0,res1 |
|
|
else |
|
|
return res1,res2 |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local function executeex(cmd) |
|
|
local outfile = os.tmpname() |
|
|
local errfile = os.tmpname() |
|
|
os.remove(outfile) |
|
|
os.remove(errfile) |
|
|
|
|
|
outfile = os.getenv('TEMP')..outfile |
|
|
errfile = os.getenv('TEMP')..errfile |
|
|
cmd = cmd .. [[ >"]]..outfile..[[" 2>"]]..errfile..[["]] |
|
|
|
|
|
local success, retcode = execute(cmd) |
|
|
|
|
|
local outcontent, errcontent, fh |
|
|
|
|
|
fh = io.open(outfile) |
|
|
if fh then |
|
|
outcontent = fh:read("*a") |
|
|
fh:close() |
|
|
end |
|
|
os.remove(outfile) |
|
|
|
|
|
fh = io.open(errfile) |
|
|
if fh then |
|
|
errcontent = fh:read("*a") |
|
|
fh:close() |
|
|
end |
|
|
os.remove(errfile) |
|
|
|
|
|
return success, retcode, (outcontent or ""), (errcontent or "") |
|
|
end |
|
|
|
|
|
|
|
|
local split = function(str, pat) |
|
|
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 t |
|
|
end |
|
|
|
|
|
|
|
|
local dqwrap = function(str) |
|
|
assert(type(str)=="string", "Expected string, got "..type(str)) |
|
|
return '"'..str..'"' |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local function parsequery(output, i) |
|
|
assert(type(output) == "string" or type(output) == "table", "Expected string or table, got "..type(output)) |
|
|
local lines |
|
|
if type(output) == "string" then |
|
|
lines = split(output, "\n") |
|
|
else |
|
|
lines = output |
|
|
end |
|
|
local i = i or 1 |
|
|
local result = { values = {}, keys = {} } |
|
|
while i <= #lines do |
|
|
if lines[i] ~= "" then |
|
|
if result.key then |
|
|
|
|
|
if lines[i]:sub(1,1) == " " then |
|
|
|
|
|
local n, t, v = lines[i]:match("^%s%s%s%s(.+)%s%s%s%s(REG_.+)%s%(%d%d?%)%s%s%s%s(.*)$") |
|
|
result.values[n] = { ["type"] = t, value = v, name = n} |
|
|
elseif lines[i]:find(result.key,1,true) == 1 then |
|
|
|
|
|
local skey |
|
|
local name = lines[i]:sub(#result.key + 2, -1) |
|
|
skey, i = parsequery(lines, i) |
|
|
result.keys[name] = skey |
|
|
else |
|
|
|
|
|
return result, i-1 |
|
|
end |
|
|
else |
|
|
|
|
|
result.key = lines[i] |
|
|
end |
|
|
else |
|
|
if result.key then |
|
|
|
|
|
while lines[i] == "" and i <= #lines do i = i + 1 end |
|
|
if lines[i] then |
|
|
if lines[i]:find(result.key,1,true) ~= 1 then |
|
|
|
|
|
return result, i |
|
|
else |
|
|
i = i - 1 |
|
|
end |
|
|
end |
|
|
end |
|
|
end |
|
|
i = i + 1 |
|
|
end |
|
|
if result.key then |
|
|
return result, i |
|
|
else |
|
|
return nil |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function registry.getkey(key, recursive) |
|
|
assert(type(key)=="string", "Expected string, got "..type(key)) |
|
|
local options = " /z" |
|
|
if recursive then options = options.." /s" end |
|
|
local ok, ec, out, err = executeex([[reg.exe query ]]..dqwrap(key)..options) |
|
|
if not ok then |
|
|
return nil, (split(err,"\n"))[1] |
|
|
else |
|
|
local result = parsequery(out) |
|
|
if not recursive then |
|
|
|
|
|
for _, v in pairs(result.keys) do |
|
|
v.keys = nil |
|
|
v.values = nil |
|
|
end |
|
|
end |
|
|
return result |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function registry.createkey(key) |
|
|
local ok, ec, out, err = executeex([[reg.exe add ]]..dqwrap(key)..[[ /f]]) |
|
|
if not ok then |
|
|
return nil, (split(err,"\n"))[1] |
|
|
else |
|
|
return true |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function registry.deletekey(key) |
|
|
local ok, ec, out, err = executeex([[reg.exe delete ]]..dqwrap(key)..[[ /f]]) |
|
|
if not ok then |
|
|
if not registry.getkey(key) then return true end |
|
|
return nil, (split(err,"\n"))[1] |
|
|
else |
|
|
return true |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function registry.writevalue(key, name, vtype, value) |
|
|
local command |
|
|
if name == "(Default)" or name == nil then |
|
|
command = ("reg.exe add %s /ve /t %s /d %s /f"):format(dqwrap(key), vtype, dqwrap(value)) |
|
|
else |
|
|
command = ("reg.exe add %s /v %s /t %s /d %s /f"):format(dqwrap(key),dqwrap(name), vtype, dqwrap(value)) |
|
|
end |
|
|
local ok, ec, out, err = executeex(command) |
|
|
if not ok then |
|
|
return nil, (split(err,"\n"))[1] |
|
|
else |
|
|
return true |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function registry.deletevalue(key, name) |
|
|
local command |
|
|
if name == "(Default)" or name == nil then |
|
|
command = ("reg.exe delete %s /ve /f"):format(dqwrap(key)) |
|
|
else |
|
|
command = ("reg.exe delete %s /v %s /f"):format(dqwrap(key),dqwrap(name)) |
|
|
end |
|
|
local ok, ec, out, err = executeex(command) |
|
|
if not ok then |
|
|
if not registry.getvalue(key, name) then return true end |
|
|
return nil, (split(err,"\n"))[1] |
|
|
else |
|
|
return true |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function registry.getvalue(key, name) |
|
|
local keyt = registry.getkey(key) |
|
|
if keyt then |
|
|
if keyt[name] then |
|
|
|
|
|
return keyt[name].value, keyt[name].type |
|
|
end |
|
|
end |
|
|
return nil |
|
|
end |
|
|
|
|
|
return registry |