Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/windirstat/lua-winreg.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormoteus <mimir@newmail.ru>2012-09-05 16:09:46 +0400
committermoteus <mimir@newmail.ru>2012-09-05 16:09:46 +0400
commit46a94cbb43975e06bdb66f47974d09d7b3b36a22 (patch)
treeee7e36b7ec56d3f7de6367122d7b7f6ec72eb13f
parente52ac7f6beaf32bdebb7e3af33448e79df5cc6b6 (diff)
Support Lua 5.2
Add tests from http://scite-ru.googlecode.com/ Add lakefile
-rw-r--r--lakefile74
-rw-r--r--src/l52util.c119
-rw-r--r--src/l52util.h46
-rw-r--r--src/lua_mtutil.c4
-rw-r--r--src/luawin_dllerror.c3
-rw-r--r--src/winreg.c9
-rw-r--r--test/test_1.lua10
-rw-r--r--test/test_5_1_10.lua8
-rw-r--r--test/test_5_1_13.lua7
-rw-r--r--test/test_5_1_5.lua8
-rw-r--r--test/test_5_1_6.lua20
-rw-r--r--test/test_5_1_8.lua9
-rw-r--r--test/test_5_1_9.lua7
13 files changed, 315 insertions, 9 deletions
diff --git a/lakefile b/lakefile
new file mode 100644
index 0000000..98c1945
--- /dev/null
+++ b/lakefile
@@ -0,0 +1,74 @@
+if not WINDOWS then quit('This is only windwos module!') end
+
+if LUA_VER == '5.2' then
+ LUA_NEED = 'lua52'
+ LUA_DIR = ENV.LUA_DIR_5_2 or ENV.LUA_DIR
+else
+ LUA_NEED = 'lua'
+ LUA_DIR = ENV.LUA_DIR
+end
+
+lake.define_need('unicode',function()return{
+ defines = {"UNICODE"; "_UNICODE"};
+ flags = "/UMBS /U_MBS";
+} end)
+
+INSTALL_DIR = INSTALL_DIR or J(LUA_DIR,'libs','winreg')
+
+winreg = c.shared{'winreg',
+ base = 'src';
+ src = {"lua_int64","lua_mtutil","lua_tstring","luawin_dllerror",
+ "win_privileges","win_registry","win_trace","winreg","l52util"
+ };
+ defines = L(
+ {'WIN32','_WIN32','_WINDOWS'},
+ {"WIN32_LEAN_AND_MEAN"; "WINDLL"; "USRDLL"},
+ IF(DEBUG, {'_DEBUG', 'DEBUG'},'NDEBUG'),
+ IF(MSVC, {'_CRT_SECURE_NO_WARNINGS'}),
+ IF(MSVC, {'_WIN32_WINNT=0x0400','WINVER=0x0400', '_WIN32_IE=0x0300'}),
+ IF(MSVC, {'CRTAPI1=_cdecl','CRTAPI2=_cdecl','_X86_=1'}),
+ 'WINREG_EXPORTS', 'WINREG_API=__declspec(dllexport)'
+ );
+ flags = IF(MSVC,
+ IF(DEBUG, {'-Z7' , '-Od', --[['-MLd']]}, {'-O2', --[['-ML']]}),
+ '-Os -DNDEBUG -s'
+ );
+ libflags = IF(MSVC,IF(DEBUG, '-debug:full -debugtype:cv', '-DEBUG -OPT:REF -OPT:ICF'));
+ needs = {LUA_NEED,IF(UNICODE, 'unicode')};
+ libs = {"advapi32", "kernel32", "user32"};
+ optimize = false;
+}
+
+target('build',{winreg})
+
+install = target('install', {
+ file.group{odir=J(INSTALL_DIR, 'test');
+ src = J('test','*.*');recurse=true;
+ };
+ file.group{odir=J(INSTALL_DIR, 'doc');
+ src = J('doc','*.*');recurse=true;
+ };
+ target(J(INSTALL_DIR, 'bin', winreg.target), winreg, CP );
+})
+
+target('test', install, function()
+ if TESTING then
+ lake.chdir('test')
+ for file in path.mask('*.lua') do
+ print("run " .. file)
+ end
+ lake.chdir('<')
+ end
+ if not TESTING then
+ lake.chdir(J(INSTALL_DIR,'test'))
+ for file in path.mask('*.lua') do
+ print("run " .. file)
+ if not utils.execute( LUA_EXE .. ' ' .. file ) then
+ quit("FAIL!")
+ end
+ end
+ lake.chdir('<')
+ end
+end)
+
+default('build')
diff --git a/src/l52util.c b/src/l52util.c
new file mode 100644
index 0000000..5675114
--- /dev/null
+++ b/src/l52util.c
@@ -0,0 +1,119 @@
+#include "l52util.h"
+
+#include <memory.h>
+#include <assert.h>
+
+#if LUA_VERSION_NUM >= 502
+
+int luaL_typerror (lua_State *L, int narg, const char *tname) {
+ const char *msg = lua_pushfstring(L, "%s expected, got %s", tname,
+ luaL_typename(L, narg));
+ return luaL_argerror(L, narg, msg);
+}
+
+void luaL_register (lua_State *L, const char *libname, const luaL_Reg *l){
+ if(libname) lua_newtable(L);
+ luaL_setfuncs(L, l, 0);
+}
+
+#else
+
+void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup){
+ luaL_checkstack(L, nup, "too many upvalues");
+ for (; l->name != NULL; l++) { /* fill the table with given functions */
+ int i;
+ for (i = 0; i < nup; i++) /* copy upvalues to the top */
+ lua_pushvalue(L, -nup);
+ lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
+ lua_setfield(L, -(nup + 2), l->name);
+ }
+ lua_pop(L, nup); /* remove upvalues */
+}
+
+void lua_rawgetp(lua_State *L, int index, const void *p){
+ index = lua_absindex(L, index);
+ lua_pushlightuserdata(L, (void *)p);
+ lua_rawget(L, index);
+}
+
+void lua_rawsetp (lua_State *L, int index, const void *p){
+ index = lua_absindex(L, index);
+ lua_pushlightuserdata(L, (void *)p);
+ lua_insert(L, -2);
+ lua_rawset(L, index);
+}
+
+#endif
+
+int lutil_newmetatablep (lua_State *L, const void *p) {
+ lua_rawgetp(L, LUA_REGISTRYINDEX, p);
+ if (!lua_isnil(L, -1)) /* name already in use? */
+ return 0; /* leave previous value on top, but return 0 */
+ lua_pop(L, 1);
+
+ lua_newtable(L); /* create metatable */
+ lua_pushvalue(L, -1); /* duplicate metatable to set*/
+ lua_rawsetp(L, LUA_REGISTRYINDEX, p);
+
+ return 1;
+}
+
+void lutil_getmetatablep (lua_State *L, const void *p) {
+ lua_rawgetp(L, LUA_REGISTRYINDEX, p);
+}
+
+void lutil_setmetatablep (lua_State *L, const void *p) {
+ lutil_getmetatablep(L, p);
+ assert(lua_istable(L,-1));
+ lua_setmetatable (L, -2);
+}
+
+int lutil_isudatap (lua_State *L, int ud, const void *p) {
+ if (lua_isuserdata(L, ud)){
+ if (lua_getmetatable(L, ud)) { /* does it have a metatable? */
+ int res;
+ lutil_getmetatablep(L,p); /* get correct metatable */
+ res = lua_rawequal(L, -1, -2); /* does it have the correct mt? */
+ lua_pop(L, 2); /* remove both metatables */
+ return res;
+ }
+ }
+ return 0;
+}
+
+void *lutil_checkudatap (lua_State *L, int ud, const void *p) {
+ void *up = lua_touserdata(L, ud);
+ if (up != NULL) { /* value is a userdata? */
+ if (lua_getmetatable(L, ud)) { /* does it have a metatable? */
+ lutil_getmetatablep(L,p); /* get correct metatable */
+ if (lua_rawequal(L, -1, -2)) { /* does it have the correct mt? */
+ lua_pop(L, 2); /* remove both metatables */
+ return up;
+ }
+ }
+ }
+ luaL_typerror(L, ud, p); /* else error */
+ return NULL; /* to avoid warnings */
+}
+
+int lutil_createmetap (lua_State *L, const void *p, const luaL_Reg *methods) {
+ if (!lutil_newmetatablep(L, p))
+ return 0;
+
+ /* define methods */
+ luaL_setfuncs (L, methods, 0);
+
+ /* define metamethods */
+ lua_pushliteral (L, "__index");
+ lua_pushvalue (L, -2);
+ lua_settable (L, -3);
+
+ return 1;
+}
+
+void *lutil_newudatap_impl(lua_State *L, size_t size, const void *p){
+ void *obj = lua_newuserdata (L, size);
+ memset(obj, 0, size);
+ lutil_setmetatablep(L, p);
+ return obj;
+}
diff --git a/src/l52util.h b/src/l52util.h
new file mode 100644
index 0000000..6391307
--- /dev/null
+++ b/src/l52util.h
@@ -0,0 +1,46 @@
+#ifndef _LZUTILS_H_
+#define _LZUTILS_H_
+
+#include "lua.h"
+#include "lauxlib.h"
+
+#if LUA_VERSION_NUM >= 502 // lua 5.2
+
+// lua_rawgetp
+// lua_rawsetp
+// luaL_setfuncs
+// lua_absindex
+
+
+#define lua_objlen lua_rawlen
+
+int luaL_typerror (lua_State *L, int narg, const char *tname);
+
+void luaL_register (lua_State *L, const char *libname, const luaL_Reg *l);
+
+#else // lua 5.1
+
+// functions form lua 5.2
+
+# define lua_absindex(L, i) (((i)>0)?(i):((i)<=LUA_REGISTRYINDEX?(i):(lua_gettop(L)+(i)+1)))
+# define lua_rawlen lua_objlen
+
+void lua_rawgetp (lua_State *L, int index, const void *p);
+void lua_rawsetp (lua_State *L, int index, const void *p);
+void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup);
+
+#endif
+
+int lutil_newmetatablep (lua_State *L, const void *p);
+void lutil_getmetatablep (lua_State *L, const void *p);
+void lutil_setmetatablep (lua_State *L, const void *p);
+
+#define lutil_newudatap(L, TTYPE, TNAME) (TTYPE *)lutil_newudatap_impl(L, sizeof(TTYPE), TNAME)
+int lutil_isudatap (lua_State *L, int ud, const void *p);
+void *lutil_checkudatap (lua_State *L, int ud, const void *p);
+int lutil_createmetap (lua_State *L, const void *p, const luaL_Reg *methods);
+
+void *lutil_newudatap_impl (lua_State *L, size_t size, const void *p);
+
+#endif
+
diff --git a/src/lua_mtutil.c b/src/lua_mtutil.c
index 5f6bea2..b7a927f 100644
--- a/src/lua_mtutil.c
+++ b/src/lua_mtutil.c
@@ -1,12 +1,14 @@
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
+#include "l52util.h"
+
/* push metatable to stack, create if not yet reg, the metatable of the table is its self */
int lua_opentablemt(lua_State *L, const char * libname, const luaL_reg * reg, int upval){
/*STACK:...<upvals>#*/
if(luaL_newmetatable(L, libname)){
/*STACK:...<upvals><tablemt>#*/
- luaL_openlib(L, NULL, reg, upval);
+ luaL_setfuncs(L, reg, upval);
lua_pushliteral(L, "__index");
/*STACK:...<tablemt><"__index">#*/
lua_pushvalue(L, -2);
diff --git a/src/luawin_dllerror.c b/src/luawin_dllerror.c
index 68f0db0..d661083 100644
--- a/src/luawin_dllerror.c
+++ b/src/luawin_dllerror.c
@@ -12,8 +12,7 @@ void lua_dllerror(lua_State *L, DWORD dwErr){
/*
if lgk_dllerror is present in global env and equal to false dont raise error!
*/
- lua_pushliteral(L, lgk_dllerror);
- lua_rawget(L, LUA_GLOBALSINDEX);
+ lua_getglobal(L, lgk_dllerror);
if(lua_isrealfalse(L, -1)){
WIN_TRACEA("lua_dllerror:false");
diff --git a/src/winreg.c b/src/winreg.c
index e3aafb3..5257166 100644
--- a/src/winreg.c
+++ b/src/winreg.c
@@ -5,13 +5,10 @@
#include <assert.h>
#define lua_assert assert
-#ifndef uintptr_t
- typedef unsigned uintptr_t;
-#endif
-
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
+#include "l52util.h"
#include "stdmacro.h"
#include "luamacro.h"
@@ -138,7 +135,7 @@ HKEY reg_aux_strtohkey(lua_State *L, const char * psz){
INT64 x;
if(atoINT64(psz, &x)){
WIN_TRACEA("DIGIT ROOTKEY %s", psz);
- return (HKEY)(uintptr_t)x;
+ return (HKEY)(size_t)x;
}else{
for(pph = ph; pph->name && stricmp(psz, pph->name); pph++);
if(!pph->data)luaL_error(L, "invalid prefix key '%s'", psz);
@@ -283,7 +280,7 @@ BOOL reg_aux_setvalue(lua_State *L, HKEY hKey, const TCHAR * pszVal, int type, i
luaL_buffinit(L, &B);
if(lua_istable(L, i)){
int n;
- int last = luaL_getn(L, i);
+ int last = lua_objlen(L, i);
for (n = 1; n <= last; n++) {
lua_rawgeti(L, i, n);
luaL_addstring(&B, lua_checkstring(L, -1));
diff --git a/test/test_1.lua b/test/test_1.lua
new file mode 100644
index 0000000..1dc0170
--- /dev/null
+++ b/test/test_1.lua
@@ -0,0 +1,10 @@
+local winreg = require"winreg"
+
+-- prints all the special folders
+hkey = winreg.openkey[[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion]]
+
+skey = hkey:openkey([[Explorer\Shell Folders]])
+for name in skey:enumvalue() do
+ print("\nname: " .. name
+ .. "\npath: " .. skey:getvalue(name))
+end
diff --git a/test/test_5_1_10.lua b/test/test_5_1_10.lua
new file mode 100644
index 0000000..7e9a2ce
--- /dev/null
+++ b/test/test_5_1_10.lua
@@ -0,0 +1,8 @@
+local winreg = require"winreg"
+rkey = "HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"
+hkey = winreg.openkey(rkey)
+for name, kind in hkey:enumvalue() do
+ print("\nname: " .. name
+ .. "\ntype: " .. hkey:getvaltype(name))
+ assert(kind == hkey:getvaltype(name))
+end
diff --git a/test/test_5_1_13.lua b/test/test_5_1_13.lua
new file mode 100644
index 0000000..308ec78
--- /dev/null
+++ b/test/test_5_1_13.lua
@@ -0,0 +1,7 @@
+local winreg = require"winreg"
+-- Enumerate shell folders
+hkey = winreg.openkey([[HKCU\Software\Microsoft\Windows\CurrentVersion]])
+skey = hkey:openkey("Explorer\\Shell Folders")
+for name, kind in skey:enumvalue(true) do
+ print("\nname: " .. name .. " (type " .. kind .. ")")
+end
diff --git a/test/test_5_1_5.lua b/test/test_5_1_5.lua
new file mode 100644
index 0000000..4af3188
--- /dev/null
+++ b/test/test_5_1_5.lua
@@ -0,0 +1,8 @@
+local winreg = require"winreg"
+-- Enumerate Application paths
+rkey = [[HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths]]
+hkey = winreg.openkey(rkey)
+for k in hkey:enumkey() do
+ print(k,(hkey:openkey(k):getvalue()))
+ collectgarbage()
+end
diff --git a/test/test_5_1_6.lua b/test/test_5_1_6.lua
new file mode 100644
index 0000000..615720f
--- /dev/null
+++ b/test/test_5_1_6.lua
@@ -0,0 +1,20 @@
+local winreg = require"winreg"
+-- Enumerate start up programs
+
+rkey = "HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"
+hkey = winreg.openkey(rkey)
+
+print('\n'..rkey..":")
+for name, kind in hkey:enumvalue() do
+ print("\nname: " .. name
+ .. "\ncommand: " .. hkey:getvalue(name))
+end
+
+rkey = "HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce"
+hkey = winreg.openkey(rkey)
+
+print('\n'..rkey..":")
+for name, kind in hkey:enumvalue() do
+ print("\nname: " .. name
+ .. "\ncommand: " .. hkey:getvalue(name))
+end
diff --git a/test/test_5_1_8.lua b/test/test_5_1_8.lua
new file mode 100644
index 0000000..212d4c0
--- /dev/null
+++ b/test/test_5_1_8.lua
@@ -0,0 +1,9 @@
+local winreg = require"winreg"
+rkey = [[HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer]]
+info = winreg.openkey(rkey):getinfo()
+print("number of subkeys : '" .. info.subkeys .. "'")
+print("longest subkey name length : '" .. info.maxsubkeylen .. "'")
+print("longest class string length : '" .. info.maxclasslen .. "'")
+print("number of value entries : '" .. info.values .. "'")
+print("longest value name length : '" .. info.maxvaluelen .. "'")
+print("class string : '" .. info.class .. "'")
diff --git a/test/test_5_1_9.lua b/test/test_5_1_9.lua
new file mode 100644
index 0000000..18a59b2
--- /dev/null
+++ b/test/test_5_1_9.lua
@@ -0,0 +1,7 @@
+local winreg = require"winreg"
+rkey = "HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"
+hkey = winreg.openkey(rkey)
+for name, kind in hkey:enumvalue() do
+ print("\nname: " .. name
+ .. "\nvalue: " .. hkey:getstrval(name))
+end