Comparison

util-src/windows.c @ 6615:8e4572a642cb

util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
author Kim Alvefur <zash@zash.se>
date Fri, 03 Apr 2015 19:52:48 +0200
parent 6413:a552f4170aed
child 6789:6b180e77c97a
comparison
equal deleted inserted replaced
6613:2aae36312eb9 6615:8e4572a642cb
1 /* Prosody IM 1 /* Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild 2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain 3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- 4 --
5 -- This project is MIT/X11 licensed. Please see the 5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information. 6 -- COPYING file in the source package for more information.
7 -- 7 --
8 */ 8 */
9 9
21 21
22 #if (LUA_VERSION_NUM == 502) 22 #if (LUA_VERSION_NUM == 502)
23 #define luaL_register(L, N, R) luaL_setfuncs(L, R, 0) 23 #define luaL_register(L, N, R) luaL_setfuncs(L, R, 0)
24 #endif 24 #endif
25 25
26 static int Lget_nameservers(lua_State *L) { 26 static int Lget_nameservers(lua_State* L) {
27 char stack_buffer[1024]; // stack allocated buffer 27 char stack_buffer[1024]; // stack allocated buffer
28 IP4_ARRAY* ips = (IP4_ARRAY*) stack_buffer; 28 IP4_ARRAY* ips = (IP4_ARRAY*) stack_buffer;
29 DWORD len = sizeof(stack_buffer); 29 DWORD len = sizeof(stack_buffer);
30 DNS_STATUS status; 30 DNS_STATUS status;
31 31
32 status = DnsQueryConfig(DnsConfigDnsServerList, FALSE, NULL, NULL, ips, &len); 32 status = DnsQueryConfig(DnsConfigDnsServerList, FALSE, NULL, NULL, ips, &len);
33 if (status == 0) { 33
34 if(status == 0) {
34 DWORD i; 35 DWORD i;
35 lua_createtable(L, ips->AddrCount, 0); 36 lua_createtable(L, ips->AddrCount, 0);
36 for (i = 0; i < ips->AddrCount; i++) { 37
38 for(i = 0; i < ips->AddrCount; i++) {
37 DWORD ip = ips->AddrArray[i]; 39 DWORD ip = ips->AddrArray[i];
38 char ip_str[16] = ""; 40 char ip_str[16] = "";
39 sprintf_s(ip_str, sizeof(ip_str), "%d.%d.%d.%d", (ip >> 0) & 255, (ip >> 8) & 255, (ip >> 16) & 255, (ip >> 24) & 255); 41 sprintf_s(ip_str, sizeof(ip_str), "%d.%d.%d.%d", (ip >> 0) & 255, (ip >> 8) & 255, (ip >> 16) & 255, (ip >> 24) & 255);
40 lua_pushstring(L, ip_str); 42 lua_pushstring(L, ip_str);
41 lua_rawseti(L, -2, i+1); 43 lua_rawseti(L, -2, i + 1);
42 } 44 }
45
43 return 1; 46 return 1;
44 } else { 47 } else {
45 lua_pushnil(L); 48 lua_pushnil(L);
46 lua_pushfstring(L, "DnsQueryConfig returned %d", status); 49 lua_pushfstring(L, "DnsQueryConfig returned %d", status);
47 return 2; 50 return 2;
48 } 51 }
49 } 52 }
50 53
51 static int lerror(lua_State *L, char* string) { 54 static int lerror(lua_State* L, char* string) {
52 lua_pushnil(L); 55 lua_pushnil(L);
53 lua_pushfstring(L, "%s: %d", string, GetLastError()); 56 lua_pushfstring(L, "%s: %d", string, GetLastError());
54 return 2; 57 return 2;
55 } 58 }
56 59
57 static int Lget_consolecolor(lua_State *L) { 60 static int Lget_consolecolor(lua_State* L) {
58 HWND console = GetStdHandle(STD_OUTPUT_HANDLE); 61 HWND console = GetStdHandle(STD_OUTPUT_HANDLE);
59 WORD color; DWORD read_len; 62 WORD color;
60 63 DWORD read_len;
64
61 CONSOLE_SCREEN_BUFFER_INFO info; 65 CONSOLE_SCREEN_BUFFER_INFO info;
62 66
63 if (console == INVALID_HANDLE_VALUE) return lerror(L, "GetStdHandle"); 67 if(console == INVALID_HANDLE_VALUE) {
64 if (!GetConsoleScreenBufferInfo(console, &info)) return lerror(L, "GetConsoleScreenBufferInfo"); 68 return lerror(L, "GetStdHandle");
65 if (!ReadConsoleOutputAttribute(console, &color, 1, info.dwCursorPosition, &read_len)) return lerror(L, "ReadConsoleOutputAttribute"); 69 }
70
71 if(!GetConsoleScreenBufferInfo(console, &info)) {
72 return lerror(L, "GetConsoleScreenBufferInfo");
73 }
74
75 if(!ReadConsoleOutputAttribute(console, &color, 1, info.dwCursorPosition, &read_len)) {
76 return lerror(L, "ReadConsoleOutputAttribute");
77 }
66 78
67 lua_pushnumber(L, color); 79 lua_pushnumber(L, color);
68 return 1; 80 return 1;
69 } 81 }
70 static int Lset_consolecolor(lua_State *L) { 82 static int Lset_consolecolor(lua_State* L) {
71 int color = luaL_checkint(L, 1); 83 int color = luaL_checkint(L, 1);
72 HWND console = GetStdHandle(STD_OUTPUT_HANDLE); 84 HWND console = GetStdHandle(STD_OUTPUT_HANDLE);
73 if (console == INVALID_HANDLE_VALUE) return lerror(L, "GetStdHandle"); 85
74 if (!SetConsoleTextAttribute(console, color)) return lerror(L, "SetConsoleTextAttribute"); 86 if(console == INVALID_HANDLE_VALUE) {
87 return lerror(L, "GetStdHandle");
88 }
89
90 if(!SetConsoleTextAttribute(console, color)) {
91 return lerror(L, "SetConsoleTextAttribute");
92 }
93
75 lua_pushboolean(L, 1); 94 lua_pushboolean(L, 1);
76 return 1; 95 return 1;
77 } 96 }
78 97
79 static const luaL_Reg Reg[] = 98 static const luaL_Reg Reg[] = {
80 {
81 { "get_nameservers", Lget_nameservers }, 99 { "get_nameservers", Lget_nameservers },
82 { "get_consolecolor", Lget_consolecolor }, 100 { "get_consolecolor", Lget_consolecolor },
83 { "set_consolecolor", Lset_consolecolor }, 101 { "set_consolecolor", Lset_consolecolor },
84 { NULL, NULL } 102 { NULL, NULL }
85 }; 103 };
86 104
87 LUALIB_API int luaopen_util_windows(lua_State *L) { 105 LUALIB_API int luaopen_util_windows(lua_State* L) {
88 lua_newtable(L); 106 lua_newtable(L);
89 luaL_register(L, NULL, Reg); 107 luaL_register(L, NULL, Reg);
90 lua_pushliteral(L, "-3.14"); 108 lua_pushliteral(L, "-3.14");
91 lua_setfield(L, -2, "version"); 109 lua_setfield(L, -2, "version");
92 return 1; 110 return 1;