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 static int Lget_nameservers(lua_State *L) {
23 char stack_buffer[1024]; // stack allocated buffer
24 IP4_ARRAY* ips = (IP4_ARRAY*) stack_buffer;
25 DWORD len = sizeof(stack_buffer);
28 status = DnsQueryConfig(DnsConfigDnsServerList, FALSE, NULL, NULL, ips, &len);
31 lua_createtable(L, ips->AddrCount, 0);
32 for (i = 0; i < ips->AddrCount; i++) {
33 DWORD ip = ips->AddrArray[i];
35 sprintf_s(ip_str, sizeof(ip_str), "%d.%d.%d.%d", (ip >> 0) & 255, (ip >> 8) & 255, (ip >> 16) & 255, (ip >> 24) & 255);
36 lua_pushstring(L, ip_str);
37 lua_rawseti(L, -2, i+1);
42 lua_pushfstring(L, "DnsQueryConfig returned %d", status);
47 static int lerror(lua_State *L, char* string) {
49 lua_pushfstring(L, "%s: %d", string, GetLastError());
53 static int Lget_consolecolor(lua_State *L) {
54 HWND console = GetStdHandle(STD_OUTPUT_HANDLE);
55 WORD color; DWORD read_len;
57 CONSOLE_SCREEN_BUFFER_INFO info;
59 if (console == INVALID_HANDLE_VALUE) return lerror(L, "GetStdHandle");
60 if (!GetConsoleScreenBufferInfo(console, &info)) return lerror(L, "GetConsoleScreenBufferInfo");
61 if (!ReadConsoleOutputAttribute(console, &color, 1, info.dwCursorPosition, &read_len)) return lerror(L, "ReadConsoleOutputAttribute");
63 lua_pushnumber(L, color);
66 static int Lset_consolecolor(lua_State *L) {
67 int color = luaL_checkint(L, 1);
68 HWND console = GetStdHandle(STD_OUTPUT_HANDLE);
69 if (console == INVALID_HANDLE_VALUE) return lerror(L, "GetStdHandle");
70 if (!SetConsoleTextAttribute(console, color)) return lerror(L, "SetConsoleTextAttribute");
71 lua_pushboolean(L, 1);
75 static const luaL_Reg Reg[] =
77 { "get_nameservers", Lget_nameservers },
78 { "get_consolecolor", Lget_consolecolor },
79 { "set_consolecolor", Lset_consolecolor },
83 LUALIB_API int luaopen_util_windows(lua_State *L) {
85 luaL_register(L, NULL, Reg);
86 lua_pushliteral(L, "version"); /** version */
87 lua_pushliteral(L, "-3.14");