Skip to content

Commit

Permalink
add isatty/write_console
Browse files Browse the repository at this point in the history
  • Loading branch information
actboy168 committed Jan 20, 2024
1 parent 7c124f4 commit ae43ad6
Showing 1 changed file with 45 additions and 11 deletions.
56 changes: 45 additions & 11 deletions binding/port/lua_windows.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <Windows.h>
#include <bee/lua/binding.h>
#include <bee/platform/win/unicode.h>
#include <binding/binding.h>
Expand All @@ -21,26 +22,59 @@ namespace bee::lua_windows {
static int filemode(lua_State* L) {
luaL_Stream* p = lua::tofile(L, 1);
auto mode = lua::checkstrview(L, 2);
if (p && p->closef && p->f) {
int ok = _setmode(_fileno(p->f), mode[0] == 'b' ? _O_BINARY : _O_TEXT);
if (ok == -1) {
lua_pushnil(L);
lua_pushstring(L, make_crterror("_setmode").c_str());
return 2;
}
lua_pushboolean(L, 1);
if (!p || !p->closef || !p->f) {
lua_pushnil(L);
lua_pushstring(L, make_error(std::make_error_code(std::errc::bad_file_descriptor), "filemode").c_str());
return 2;
}
int ok = _setmode(_fileno(p->f), mode[0] == 'b' ? _O_BINARY : _O_TEXT);
if (ok == -1) {
lua_pushnil(L);
lua_pushstring(L, make_crterror("filemode").c_str());
return 2;
}
lua_pushboolean(L, 1);
return 1;
}

static int isatty(lua_State* L) {
luaL_Stream* p = lua::tofile(L, 1);
if (!p || !p->closef || !p->f) {
lua_pushboolean(L, 0);
return 1;
}
lua_pushnil(L);
lua_pushstring(L, make_error(std::make_error_code(std::errc::bad_file_descriptor), "_setmode").c_str());
return 2;
HANDLE handle = (HANDLE)_get_osfhandle(_fileno(p->f));
lua_pushboolean(L, FILE_TYPE_CHAR == GetFileType(handle));
return 1;
}

static int write_console(lua_State* L) {
luaL_Stream* p = lua::tofile(L, 1);
auto msg = win::u2w(lua::checkstrview(L, 2));
if (!p || !p->closef || !p->f) {
lua_pushnil(L);
lua_pushstring(L, make_error(std::make_error_code(std::errc::bad_file_descriptor), "write_console").c_str());
return 2;
}
HANDLE handle = (HANDLE)_get_osfhandle(_fileno(p->f));
DWORD written = 0;
BOOL ok = WriteConsoleW(handle, (void*)msg.c_str(), (DWORD)msg.size(), &written, NULL);
if (!ok) {
lua_pushnil(L);
lua_pushstring(L, make_syserror("write_console").c_str());
return 2;
}
lua_pushinteger(L, written);
return 1;
}

static int luaopen(lua_State* L) {
luaL_Reg lib[] = {
{ "u2a", lu2a },
{ "a2u", la2u },
{ "filemode", filemode },
{ "isatty", isatty },
{ "write_console", write_console },
{ NULL, NULL }
};
luaL_newlibtable(L, lib);
Expand Down

0 comments on commit ae43ad6

Please sign in to comment.