2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
12 * Windows support functions for Lua
22 #if (LUA_VERSION_NUM == 502)
23 #define luaL_register(L, N, R) luaL_setfuncs(L, R, 0)
26 static int Lget_nameservers(lua_State* L) {
27 char stack_buffer[1024]; // stack allocated buffer
28 IP4_ARRAY* ips = (IP4_ARRAY*) stack_buffer;
29 DWORD len = sizeof(stack_buffer);
32 status = DnsQueryConfig(DnsConfigDnsServerList, FALSE, NULL, NULL, ips, &len);
36 lua_createtable(L, ips->AddrCount, 0);
38 for(i = 0; i < ips->AddrCount; i++) {
39 DWORD ip = ips->AddrArray[i];
41 sprintf_s(ip_str, sizeof(ip_str), "%d.%d.%d.%d", (ip >> 0) & 255, (ip >> 8) & 255, (ip >> 16) & 255, (ip >> 24) & 255);
42 lua_pushstring(L, ip_str);
43 lua_rawseti(L, -2, i + 1);
49 lua_pushfstring(L, "DnsQueryConfig returned %d", status);
54 static int lerror(lua_State* L, char* string) {
56 lua_pushfstring(L, "%s: %d", string, GetLastError());
60 static int Lget_consolecolor(lua_State* L) {
61 HWND console = GetStdHandle(STD_OUTPUT_HANDLE);
65 CONSOLE_SCREEN_BUFFER_INFO info;
67 if(console == INVALID_HANDLE_VALUE) {
68 return lerror(L, "GetStdHandle");
71 if(!GetConsoleScreenBufferInfo(console, &info)) {
72 return lerror(L, "GetConsoleScreenBufferInfo");
75 if(!ReadConsoleOutputAttribute(console, &color, 1, info.dwCursorPosition, &read_len)) {
76 return lerror(L, "ReadConsoleOutputAttribute");
79 lua_pushnumber(L, color);
82 static int Lset_consolecolor(lua_State* L) {
83 int color = luaL_checkint(L, 1);
84 HWND console = GetStdHandle(STD_OUTPUT_HANDLE);
86 if(console == INVALID_HANDLE_VALUE) {
87 return lerror(L, "GetStdHandle");
90 if(!SetConsoleTextAttribute(console, color)) {
91 return lerror(L, "SetConsoleTextAttribute");
94 lua_pushboolean(L, 1);
98 static const luaL_Reg Reg[] = {
99 { "get_nameservers", Lget_nameservers },
100 { "get_consolecolor", Lget_consolecolor },
101 { "set_consolecolor", Lset_consolecolor },
105 LUALIB_API int luaopen_util_windows(lua_State* L) {
107 luaL_register(L, NULL, Reg);
108 lua_pushliteral(L, "-3.14");
109 lua_setfield(L, -2, "version");