Merge pull request #687 from tvandijck/os_regkey

Windows Registry
This commit is contained in:
Tom van Dijck 2017-01-31 13:11:21 -08:00 committed by GitHub
commit aae8917042
7 changed files with 194 additions and 3 deletions

View File

@ -67,7 +67,7 @@ linux: $(SRC)
windows-base: $(SRC)
if not exist build\bootstrap (mkdir build\bootstrap)
cl /Fo.\build\bootstrap\ /Fe.\build\bootstrap\premake_bootstrap.exe /DPREMAKE_NO_BUILTIN_SCRIPTS /I"$(LUA_DIR)" user32.lib ole32.lib $**
cl /Fo.\build\bootstrap\ /Fe.\build\bootstrap\premake_bootstrap.exe /DPREMAKE_NO_BUILTIN_SCRIPTS /I"$(LUA_DIR)" user32.lib ole32.lib advapi32.lib $**
.\build\bootstrap\premake_bootstrap.exe embed
.\build\bootstrap\premake_bootstrap --to=build/bootstrap $(MSDEV)

View File

@ -55,7 +55,7 @@
defines {"_CRT_SECURE_NO_DEPRECATE" }
configuration "windows"
links { "ole32" }
links { "ole32", "advapi32" }
configuration "linux or bsd or hurd"
defines { "LUA_USE_POSIX", "LUA_USE_DLOPEN" }

View File

@ -159,7 +159,7 @@
targetdir "bin/release"
filter "system:windows"
links { "ole32", "ws2_32" }
links { "ole32", "ws2_32", "advapi32" }
filter "system:linux or bsd or hurd"
defines { "LUA_USE_POSIX", "LUA_USE_DLOPEN" }

View File

@ -0,0 +1,144 @@
/**
* \file os_reg.c
* \brief Returns true if the given file exists on the file system.
* \author Copyright (c) 2002-2016 Jason Perkins and the Premake project
*/
#include "premake.h"
#if PLATFORM_WINDOWS
typedef struct RegKeyInfo
{
HKEY key;
HKEY subkey;
char * value;
} RegKeyInfo;
static HKEY get_key(const char **path)
{
if (_strnicmp(*path, "HKCU:", 5) == 0) {
*path += 5;
return HKEY_CURRENT_USER;
}
if (_strnicmp(*path, "HKLM:", 5) == 0) {
*path += 5;
return HKEY_LOCAL_MACHINE;
}
if (_strnicmp(*path, "HKCR:", 5) == 0) {
*path += 5;
return HKEY_CLASSES_ROOT;
}
if (_strnicmp(*path, "HKU:", 4) == 0) {
*path += 4;
return HKEY_USERS;
}
if (_strnicmp(*path, "HKCC:", 5) == 0) {
*path += 5;
return HKEY_CURRENT_CONFIG;
}
// unsupported or invalid key prefix
return NULL;
}
static HKEY get_subkey(HKEY key, const char **path)
{
HKEY subkey;
size_t length;
char *subpath;
const char *value;
char hasValue;
if (key == NULL)
return NULL;
// skip the initial path separator
if ((*path)[0] == '\\')
(*path)++;
// make a copy of the subkey path that excludes the value name (if present)
value = strrchr(*path, '\\');
hasValue = value ? value[1] : 0;
if (hasValue) {
length = (size_t)(value - *path);
subpath = (char *)malloc(length + 1);
strncpy(subpath, *path, length);
subpath[length] = 0;
}
// no value separator means we should check the default value
else {
subpath = (char *)*path;
length = strlen(subpath);
}
// open the key for reading
if (RegOpenKeyExA(key, subpath, 0, KEY_READ, &subkey) != ERROR_SUCCESS)
subkey = NULL;
// free the subpath if one was allocated
if (hasValue)
free(subpath);
*path += length;
return subkey;
}
static char * get_value(HKEY key, const char * name)
{
DWORD length;
char * value;
if (key == NULL || name == NULL)
return NULL;
// skip the initial path separator
if (name[0] == '\\')
name++;
// query length of value
if (RegQueryValueExA(key, name, NULL, NULL, NULL, &length) != ERROR_SUCCESS)
return NULL;
// allocate room for the value and fetch it
value = (char *)malloc((size_t)length + 1);
if (RegQueryValueExA(key, name, NULL, NULL, (LPBYTE)value, &length) != ERROR_SUCCESS) {
free(value);
return NULL;
}
value[length] = 0;
return value;
}
static void fetch_keyinfo(struct RegKeyInfo *info, const char *path)
{
info->key = get_key(&path);
info->subkey = get_subkey(info->key, &path);
info->value = get_value(info->subkey, path);
}
static void release_keyinfo(struct RegKeyInfo *info)
{
free(info->value);
RegCloseKey(info->subkey);
}
int os_getWindowsRegistry(lua_State *L)
{
RegKeyInfo info;
fetch_keyinfo(&info, luaL_checkstring(L, 1));
lua_pushstring(L, info.value);
release_keyinfo(&info);
return 1;
}
#else
int os_getWindowsRegistry(lua_State *L)
{
lua_pushnil(L);
return 1;
}
#endif

View File

@ -58,6 +58,7 @@ static const luaL_Reg os_functions[] = {
{ "isdir", os_isdir },
{ "getcwd", os_getcwd },
{ "getpass", os_getpass },
{ "getWindowsRegistry", os_getWindowsRegistry },
{ "getversion", os_getversion },
{ "isfile", os_isfile },
{ "islink", os_islink },

View File

@ -48,6 +48,7 @@
#if PLATFORM_WINDOWS
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdlib.h>
#else
#include <unistd.h>
#endif
@ -104,6 +105,7 @@ int os_chmod(lua_State* L);
int os_copyfile(lua_State* L);
int os_getcwd(lua_State* L);
int os_getpass(lua_State* L);
int os_getWindowsRegistry(lua_State* L);
int os_getversion(lua_State* L);
int os_is64bit(lua_State* L);
int os_isdir(lua_State* L);

View File

@ -220,3 +220,47 @@
function suite.translateCommand_windowsCopyNoQuotesSrc_ExtraSpace()
test.isequal('IF EXIST a\\ (xcopy /Q /E /Y /I a "b" > nul) ELSE (xcopy /Q /Y /I a "b" > nul)', os.translateCommands('{COPY} a "b" ', "windows"))
end
--
-- os.getWindowsRegistry windows tests
--
function suite.getreg_nonExistentValue()
if os.is("windows") then
local x = os.getWindowsRegistry("HKCU:Should\\Not\\Exist\\At\\All")
test.isequal(nil, x)
end
end
function suite.getreg_nonExistentDefaultValue()
if os.is("windows") then
local x = os.getWindowsRegistry("HKCU:Should\\Not\\Exist\\At\\All\\")
test.isequal(nil, x)
end
end
function suite.getreg_noSeparators()
if os.is("windows") then
os.getWindowsRegistry("HKCU:ShouldNotExistAtAll")
end
end
function suite.getreg_namedValue()
if os.is("windows") then
local x = os.getWindowsRegistry("HKCU:Environment\\PATH")
test.istrue(x ~= nil)
end
end
function suite.getreg_namedValueOptSeparator()
if os.is("windows") then
local x = os.getWindowsRegistry("HKCU:\\Environment\\PATH")
test.istrue(x ~= nil)
end
end
function suite.getreg_defaultValue()
if os.is("windows") then
local x = os.getWindowsRegistry("HKCU:AppEvents\\EventLabels\\.Default\\")
test.isequal("Default Beep", x)
end
end