| 1 | /* Prosody IM |
| 2 | -- Copyright (C) 2008-2010 Matthew Wild |
| 3 | -- Copyright (C) 2008-2010 Waqas Hussain |
| 4 | -- |
| 5 | -- This project is MIT/X11 licensed. Please see the |
| 6 | -- COPYING file in the source package for more information. |
| 7 | -- |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * windows.c |
| 12 | * Windows support functions for Lua |
| 13 | */ |
| 14 | |
| 15 | #include <stdio.h> |
| 16 | #include <windows.h> |
| 17 | #include <windns.h> |
| 18 | |
| 19 | #include "lua.h" |
| 20 | #include "lauxlib.h" |
| 21 | |
| 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); |
| 26 | DNS_STATUS status; |
| 27 | |
| 28 | status = DnsQueryConfig(DnsConfigDnsServerList, FALSE, NULL, NULL, ips, &len); |
| 29 | if (status == 0) { |
| 30 | DWORD i; |
| 31 | lua_createtable(L, ips->AddrCount, 0); |
| 32 | for (i = 0; i < ips->AddrCount; i++) { |
| 33 | DWORD ip = ips->AddrArray[i]; |
| 34 | char ip_str[16] = ""; |
| 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); |
| 38 | } |
| 39 | return 1; |
| 40 | } else { |
| 41 | luaL_error(L, "DnsQueryConfig returned %d", status); |
| 42 | return 0; // unreachable, but prevents a compiler warning |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | static void lassert(lua_State *L, BOOL test, char* string) { |
| 47 | if (!test) { |
| 48 | luaL_error(L, "%s: %d", string, GetLastError()); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | static int Lget_consolecolor(lua_State *L) { |
| 53 | HWND console = GetStdHandle(STD_OUTPUT_HANDLE); |
| 54 | WORD color; DWORD read_len; |
| 55 | |
| 56 | CONSOLE_SCREEN_BUFFER_INFO info; |
| 57 | |
| 58 | lassert(L, console != INVALID_HANDLE_VALUE, "GetStdHandle"); |
| 59 | lassert(L, GetConsoleScreenBufferInfo(console, &info), "GetConsoleScreenBufferInfo"); |
| 60 | lassert(L, ReadConsoleOutputAttribute(console, &color, sizeof(WORD), info.dwCursorPosition, &read_len), "ReadConsoleOutputAttribute"); |
| 61 | |
| 62 | lua_pushnumber(L, color); |
| 63 | return 1; |
| 64 | } |
| 65 | static int Lset_consolecolor(lua_State *L) { |
| 66 | int color = luaL_checkint(L, 1); |
| 67 | HWND console = GetStdHandle(STD_OUTPUT_HANDLE); |
| 68 | lassert(L, console != INVALID_HANDLE_VALUE, "GetStdHandle"); |
| 69 | lassert(L, SetConsoleTextAttribute(console, color), "SetConsoleTextAttribute"); |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | static const luaL_Reg Reg[] = |
| 74 | { |
| 75 | { "get_nameservers", Lget_nameservers }, |
| 76 | { "get_consolecolor", Lget_consolecolor }, |
| 77 | { "set_consolecolor", Lset_consolecolor }, |
| 78 | { NULL, NULL } |
| 79 | }; |
| 80 | |
| 81 | LUALIB_API int luaopen_util_windows(lua_State *L) { |
| 82 | luaL_register(L, "windows", Reg); |
| 83 | lua_pushliteral(L, "version"); /** version */ |
| 84 | lua_pushliteral(L, "-3.14"); |
| 85 | lua_settable(L,-3); |
| 86 | return 1; |
| 87 | } |