Skip to content

Commit bcc2b13

Browse files
committed
added paths.findprogram and adjust gnuplot.lua
1 parent 2b51999 commit bcc2b13

File tree

3 files changed

+119
-11
lines changed

3 files changed

+119
-11
lines changed

‎dok/index.dok‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,8 @@ This directory is used to build variable ''package.cpath''.
242242
The home directory of the current user.
243243

244244

245-
===== Operating system info =====
246-
{{anchor:paths.osinfo.dok}}
245+
===== Miscellaneous =====
246+
{{anchor:paths.misc.dok}}
247247

248248

249249
==== paths.uname() ====
@@ -264,4 +264,13 @@ Returns true if the operating system is Microsoft Windows.
264264

265265
Returns true if the operating system is Mac OS X.
266266

267+
==== paths.getregistryvalue(key,subkey,value) ====
267268

269+
Query a value in the Windows registry value.
270+
Causes an error on other systems.
271+
272+
==== paths.findprogram(progname) ====
273+
274+
Finds an executable program and returns its full path.
275+
All the directories specified by the PATH variable are searched.
276+
Under windows, this also searches the "App Path" registry entries.

‎init.lua.in‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,28 @@ function rmall(d, more)
116116
else
117117
return nil, "not a file or directory", d
118118
end
119+
end
120+
121+
function findprogram(exe)
122+
if is_win() then
123+
local k = 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\' .. exe;
124+
local x = getregistryvalue('HKEY_LOCAL_MACHINE', k, '')
125+
if type(x) == 'string' then return x end
126+
x = getregistryvalue('HKEY_LOCAL_MACHINE', k .. '.exe', '')
127+
if type(x) == 'string' then return x end
128+
local path = os.getenv("PATH") or "."
129+
for dir in path:gmatch('[^;]+') do
130+
x = concat(dir, exe)
131+
if filep(x) then return x end
132+
x = x .. '.exe'
133+
if filep(x) then return x end
134+
end
135+
else
136+
local path = os.getenv("PATH") or "."
137+
for dir in path:gmatch('[^:]+') do
138+
local x = concat(dir, exe)
139+
if filep(x) then return x end
140+
end
141+
end
142+
return nil
119143
end

‎paths.c‎

Lines changed: 84 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,6 @@ filep(lua_State *L, int i)
129129
}
130130

131131

132-
static int
133-
concat_fname(lua_State *L, const char *fname);
134-
135132
static int
136133
dirp(lua_State *L, int i)
137134
{
@@ -802,7 +799,8 @@ lua_rmdir(lua_State *L)
802799
/* uname */
803800

804801

805-
static int lua_uname(lua_State *L)
802+
static int
803+
lua_uname(lua_State *L)
806804
{
807805
#if defined(LUA_WIN)
808806
const char *name;
@@ -839,7 +837,87 @@ static int lua_uname(lua_State *L)
839837
#endif
840838
}
841839

842-
840+
static int
841+
lua_getregistryvalue(lua_State *L)
842+
{
843+
#ifdef LUA_WIN
844+
static char *keynames[] = {
845+
"HKEY_CLASSES_ROOT",
846+
"HKEY_CURRENT_CONFIG",
847+
"HKEY_CURRENT_USER",
848+
"HKEY_LOCAL_MACHINE",
849+
"HKEY_USERS",
850+
NULL };
851+
static HKEY keys[] = {
852+
HKEY_CLASSES_ROOT,
853+
HKEY_CURRENT_CONFIG,
854+
HKEY_CURRENT_USER,
855+
HKEY_LOCAL_MACHINE,
856+
HKEY_USERS
857+
};
858+
859+
HKEY rkey = keys[ luaL_checkoption(L, 1, NULL, keynames) ];
860+
const char *subkey = luaL_checkstring(L, 2);
861+
const char *value = luaL_checkstring(L, 3);
862+
HKEY skey;
863+
DWORD type;
864+
DWORD len = 0;
865+
char *data = "";
866+
LONG res;
867+
res = RegOpenKeyExA(rkey, subkey, 0, KEY_READ, &skey);
868+
if (res != ERROR_SUCCESS)
869+
{
870+
lua_pushnil(L);
871+
lua_pushinteger(L, res);
872+
if (res == ERROR_FILE_NOT_FOUND)
873+
lua_pushstring(L, "subkey not found");
874+
if (res == ERROR_ACCESS_DENIED)
875+
lua_pushstring(L, "subkey access denied");
876+
else
877+
return 2;
878+
return 3;
879+
}
880+
res = RegQueryValueExA(skey, value, NULL, &type, (LPBYTE)data, &len);
881+
if (res == ERROR_MORE_DATA)
882+
{
883+
len += 8;
884+
if ((data = (char*)malloc(len)))
885+
res = RegQueryValueExA(skey, value, NULL, &type, (LPBYTE)data, &len);
886+
}
887+
if (res != ERROR_SUCCESS)
888+
{
889+
RegCloseKey(skey);
890+
lua_pushnil(L);
891+
lua_pushinteger(L, res);
892+
if (res == ERROR_FILE_NOT_FOUND)
893+
lua_pushstring(L, "value not found");
894+
if (res == ERROR_ACCESS_DENIED)
895+
lua_pushstring(L, "value access denied");
896+
else
897+
return 2;
898+
return 3;
899+
}
900+
switch(type)
901+
{
902+
case REG_SZ:
903+
if (((const char*)data)[len-1] == 0) len -= 1;
904+
case REG_BINARY:
905+
lua_pushlstring(L, (const char*)data, (int)len);
906+
return 1;
907+
case REG_DWORD:
908+
lua_pushinteger(L, (lua_Integer)*(const DWORD*)data);
909+
return 1;
910+
default:
911+
lua_pushnil(L);
912+
lua_pushinteger(L, res);
913+
lua_pushfstring(L, "getting registry type %d not implemented", type);
914+
return 3;
915+
}
916+
#else
917+
luaL_error(L, "This function exists only on windows");
918+
return 0;
919+
#endif
920+
}
843921

844922
/* ------------------------------------------------------ */
845923
/* require (with global flag) */
@@ -969,10 +1047,6 @@ path_require(lua_State *L)
9691047

9701048

9711049

972-
/* ------------------------------------------------------ */
973-
/* uname */
974-
975-
9761050

9771051
/* ------------------------------------------------------ */
9781052
/* register */
@@ -991,6 +1065,7 @@ static const struct luaL_Reg paths__ [] = {
9911065
{"mkdir", lua_mkdir},
9921066
{"rmdir", lua_rmdir},
9931067
{"uname", lua_uname},
1068+
{"getregistryvalue", lua_getregistryvalue},
9941069
{"require", path_require},
9951070
{NULL, NULL}
9961071
};

0 commit comments

Comments
 (0)