Annotate

util-src/windows.c @ 4416:03e1295d599a

Merge with Zash
author Matthew Wild <mwild1@gmail.com>
date Fri, 18 Nov 2011 16:05:40 +0000
parent 4128:b6d072a3668d
child 5864:22b1d18eb919
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2924
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
1 /* Prosody IM
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
4 --
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
6 -- COPYING file in the source package for more information.
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
7 --
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
8 */
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
9
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
10 /*
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
11 * windows.c
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
12 * Windows support functions for Lua
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
13 */
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
14
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
15 #include <stdio.h>
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
16 #include <windows.h>
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
17 #include <windns.h>
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
18
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
19 #include "lua.h"
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
20 #include "lauxlib.h"
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
21
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
22 static int Lget_nameservers(lua_State *L) {
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
23 char stack_buffer[1024]; // stack allocated buffer
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
24 IP4_ARRAY* ips = (IP4_ARRAY*) stack_buffer;
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
25 DWORD len = sizeof(stack_buffer);
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
26 DNS_STATUS status;
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
27
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
28 status = DnsQueryConfig(DnsConfigDnsServerList, FALSE, NULL, NULL, ips, &len);
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
29 if (status == 0) {
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
30 DWORD i;
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
31 lua_createtable(L, ips->AddrCount, 0);
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
32 for (i = 0; i < ips->AddrCount; i++) {
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
33 DWORD ip = ips->AddrArray[i];
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
34 char ip_str[16] = "";
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
35 sprintf_s(ip_str, sizeof(ip_str), "%d.%d.%d.%d", (ip >> 0) & 255, (ip >> 8) & 255, (ip >> 16) & 255, (ip >> 24) & 255);
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
36 lua_pushstring(L, ip_str);
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
37 lua_rawseti(L, -2, i+1);
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
38 }
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
39 return 1;
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
40 } else {
4128
b6d072a3668d windows.c: Return nil,err from functions instead of throwing errors.
Waqas Hussain <waqas20@gmail.com>
parents: 3868
diff changeset
41 lua_pushnil(L);
b6d072a3668d windows.c: Return nil,err from functions instead of throwing errors.
Waqas Hussain <waqas20@gmail.com>
parents: 3868
diff changeset
42 lua_pushfstring(L, "DnsQueryConfig returned %d", status);
b6d072a3668d windows.c: Return nil,err from functions instead of throwing errors.
Waqas Hussain <waqas20@gmail.com>
parents: 3868
diff changeset
43 return 2;
2924
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
44 }
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
45 }
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
46
4128
b6d072a3668d windows.c: Return nil,err from functions instead of throwing errors.
Waqas Hussain <waqas20@gmail.com>
parents: 3868
diff changeset
47 static int lerror(lua_State *L, char* string) {
b6d072a3668d windows.c: Return nil,err from functions instead of throwing errors.
Waqas Hussain <waqas20@gmail.com>
parents: 3868
diff changeset
48 lua_pushnil(L);
b6d072a3668d windows.c: Return nil,err from functions instead of throwing errors.
Waqas Hussain <waqas20@gmail.com>
parents: 3868
diff changeset
49 lua_pushfstring(L, "%s: %d", string, GetLastError());
b6d072a3668d windows.c: Return nil,err from functions instead of throwing errors.
Waqas Hussain <waqas20@gmail.com>
parents: 3868
diff changeset
50 return 2;
3868
72d68f996f45 util-src/windows.c: Added get_consolecolor, set_consolecolor.
Waqas Hussain <waqas20@gmail.com>
parents: 2924
diff changeset
51 }
72d68f996f45 util-src/windows.c: Added get_consolecolor, set_consolecolor.
Waqas Hussain <waqas20@gmail.com>
parents: 2924
diff changeset
52
72d68f996f45 util-src/windows.c: Added get_consolecolor, set_consolecolor.
Waqas Hussain <waqas20@gmail.com>
parents: 2924
diff changeset
53 static int Lget_consolecolor(lua_State *L) {
72d68f996f45 util-src/windows.c: Added get_consolecolor, set_consolecolor.
Waqas Hussain <waqas20@gmail.com>
parents: 2924
diff changeset
54 HWND console = GetStdHandle(STD_OUTPUT_HANDLE);
72d68f996f45 util-src/windows.c: Added get_consolecolor, set_consolecolor.
Waqas Hussain <waqas20@gmail.com>
parents: 2924
diff changeset
55 WORD color; DWORD read_len;
72d68f996f45 util-src/windows.c: Added get_consolecolor, set_consolecolor.
Waqas Hussain <waqas20@gmail.com>
parents: 2924
diff changeset
56
72d68f996f45 util-src/windows.c: Added get_consolecolor, set_consolecolor.
Waqas Hussain <waqas20@gmail.com>
parents: 2924
diff changeset
57 CONSOLE_SCREEN_BUFFER_INFO info;
72d68f996f45 util-src/windows.c: Added get_consolecolor, set_consolecolor.
Waqas Hussain <waqas20@gmail.com>
parents: 2924
diff changeset
58
4128
b6d072a3668d windows.c: Return nil,err from functions instead of throwing errors.
Waqas Hussain <waqas20@gmail.com>
parents: 3868
diff changeset
59 if (console == INVALID_HANDLE_VALUE) return lerror(L, "GetStdHandle");
b6d072a3668d windows.c: Return nil,err from functions instead of throwing errors.
Waqas Hussain <waqas20@gmail.com>
parents: 3868
diff changeset
60 if (!GetConsoleScreenBufferInfo(console, &info)) return lerror(L, "GetConsoleScreenBufferInfo");
b6d072a3668d windows.c: Return nil,err from functions instead of throwing errors.
Waqas Hussain <waqas20@gmail.com>
parents: 3868
diff changeset
61 if (!ReadConsoleOutputAttribute(console, &color, sizeof(WORD), info.dwCursorPosition, &read_len)) return lerror(L, "ReadConsoleOutputAttribute");
3868
72d68f996f45 util-src/windows.c: Added get_consolecolor, set_consolecolor.
Waqas Hussain <waqas20@gmail.com>
parents: 2924
diff changeset
62
72d68f996f45 util-src/windows.c: Added get_consolecolor, set_consolecolor.
Waqas Hussain <waqas20@gmail.com>
parents: 2924
diff changeset
63 lua_pushnumber(L, color);
72d68f996f45 util-src/windows.c: Added get_consolecolor, set_consolecolor.
Waqas Hussain <waqas20@gmail.com>
parents: 2924
diff changeset
64 return 1;
72d68f996f45 util-src/windows.c: Added get_consolecolor, set_consolecolor.
Waqas Hussain <waqas20@gmail.com>
parents: 2924
diff changeset
65 }
72d68f996f45 util-src/windows.c: Added get_consolecolor, set_consolecolor.
Waqas Hussain <waqas20@gmail.com>
parents: 2924
diff changeset
66 static int Lset_consolecolor(lua_State *L) {
72d68f996f45 util-src/windows.c: Added get_consolecolor, set_consolecolor.
Waqas Hussain <waqas20@gmail.com>
parents: 2924
diff changeset
67 int color = luaL_checkint(L, 1);
72d68f996f45 util-src/windows.c: Added get_consolecolor, set_consolecolor.
Waqas Hussain <waqas20@gmail.com>
parents: 2924
diff changeset
68 HWND console = GetStdHandle(STD_OUTPUT_HANDLE);
4128
b6d072a3668d windows.c: Return nil,err from functions instead of throwing errors.
Waqas Hussain <waqas20@gmail.com>
parents: 3868
diff changeset
69 if (console == INVALID_HANDLE_VALUE) return lerror(L, "GetStdHandle");
b6d072a3668d windows.c: Return nil,err from functions instead of throwing errors.
Waqas Hussain <waqas20@gmail.com>
parents: 3868
diff changeset
70 if (!SetConsoleTextAttribute(console, color)) return lerror(L, "SetConsoleTextAttribute");
b6d072a3668d windows.c: Return nil,err from functions instead of throwing errors.
Waqas Hussain <waqas20@gmail.com>
parents: 3868
diff changeset
71 lua_pushboolean(L, 1);
b6d072a3668d windows.c: Return nil,err from functions instead of throwing errors.
Waqas Hussain <waqas20@gmail.com>
parents: 3868
diff changeset
72 return 1;
3868
72d68f996f45 util-src/windows.c: Added get_consolecolor, set_consolecolor.
Waqas Hussain <waqas20@gmail.com>
parents: 2924
diff changeset
73 }
72d68f996f45 util-src/windows.c: Added get_consolecolor, set_consolecolor.
Waqas Hussain <waqas20@gmail.com>
parents: 2924
diff changeset
74
2924
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
75 static const luaL_Reg Reg[] =
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
76 {
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
77 { "get_nameservers", Lget_nameservers },
3868
72d68f996f45 util-src/windows.c: Added get_consolecolor, set_consolecolor.
Waqas Hussain <waqas20@gmail.com>
parents: 2924
diff changeset
78 { "get_consolecolor", Lget_consolecolor },
72d68f996f45 util-src/windows.c: Added get_consolecolor, set_consolecolor.
Waqas Hussain <waqas20@gmail.com>
parents: 2924
diff changeset
79 { "set_consolecolor", Lset_consolecolor },
2924
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
80 { NULL, NULL }
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
81 };
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
82
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
83 LUALIB_API int luaopen_util_windows(lua_State *L) {
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
84 luaL_register(L, "windows", Reg);
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
85 lua_pushliteral(L, "version"); /** version */
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
86 lua_pushliteral(L, "-3.14");
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
87 lua_settable(L,-3);
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
88 return 1;
8dc4e2e00129 util.windows: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
89 }