diff options
Diffstat (limited to 'Master/texmf-dist/doc/support/cluttex/src/texrunner/isatty.lua')
-rw-r--r-- | Master/texmf-dist/doc/support/cluttex/src/texrunner/isatty.lua | 101 |
1 files changed, 64 insertions, 37 deletions
diff --git a/Master/texmf-dist/doc/support/cluttex/src/texrunner/isatty.lua b/Master/texmf-dist/doc/support/cluttex/src/texrunner/isatty.lua index 9969f691636..000e2510b78 100644 --- a/Master/texmf-dist/doc/support/cluttex/src/texrunner/isatty.lua +++ b/Master/texmf-dist/doc/support/cluttex/src/texrunner/isatty.lua @@ -18,51 +18,51 @@ ]] if os.type == "unix" then - -- Try luaposix + -- Try LuaJIT-like FFI local succ, M = pcall(function() - local isatty = require "posix.unistd".isatty - local fileno = require "posix.stdio".fileno + local ffi = require "ffi" + ffi.cdef[[ +int isatty(int fd); +int fileno(void *stream); +]] + local isatty = assert(ffi.C.isatty, "isatty not found") + local fileno = assert(ffi.C.fileno, "fileno not found") return { isatty = function(file) - return isatty(fileno(file)) == 1 - end, + -- LuaJIT converts Lua's file handles into FILE* (void*) + return isatty(fileno(file)) ~= 0 + end } end) if succ then if CLUTTEX_VERBOSITY >= 3 then - io.stderr:write("ClutTeX: isatty found via luaposix\n") + io.stderr:write("ClutTeX: isatty found via FFI (Unix)\n") end return M else if CLUTTEX_VERBOSITY >= 3 then - io.stderr:write("ClutTeX: luaposix not found: ", M, "\n") + io.stderr:write("ClutTeX: FFI (Unix) not found: ", M, "\n") end end - -- Try LuaJIT-like FFI + -- Try luaposix local succ, M = pcall(function() - local ffi = require "ffi" - ffi.cdef[[ -int isatty(int fd); -int fileno(void *stream); -]] - local isatty = assert(ffi.C.isatty, "isatty not found") - local fileno = assert(ffi.C.fileno, "fileno not found") + local isatty = require "posix.unistd".isatty + local fileno = require "posix.stdio".fileno return { isatty = function(file) - -- LuaJIT converts Lua's file handles into FILE* (void*) - return isatty(fileno(file)) ~= 0 - end + return isatty(fileno(file)) == 1 + end, } end) if succ then if CLUTTEX_VERBOSITY >= 3 then - io.stderr:write("ClutTeX: isatty found via FFI (Unix)\n") + io.stderr:write("ClutTeX: isatty found via luaposix\n") end return M else if CLUTTEX_VERBOSITY >= 3 then - io.stderr:write("ClutTeX: FFI (Unix) not found: ", M, "\n") + io.stderr:write("ClutTeX: luaposix not found: ", M, "\n") end end @@ -87,6 +87,7 @@ DWORD GetFileType(void *hFile); BOOL GetFileInformationByHandleEx(void *hFile, FILE_INFO_BY_HANDLE_CLASS fic, void *fileinfo, DWORD dwBufferSize); BOOL GetConsoleMode(void *hConsoleHandle, DWORD* lpMode); BOOL SetConsoleMode(void *hConsoleHandle, DWORD dwMode); +DWORD GetLastError(); ]] local isatty = assert(ffi.C._isatty, "_isatty not found") local fileno = assert(ffi.C._fileno, "_fileno not found") @@ -95,6 +96,7 @@ BOOL SetConsoleMode(void *hConsoleHandle, DWORD dwMode); local GetFileInformationByHandleEx = assert(ffi.C.GetFileInformationByHandleEx, "GetFileInformationByHandleEx not found") local GetConsoleMode = assert(ffi.C.GetConsoleMode, "GetConsoleMode not found") local SetConsoleMode = assert(ffi.C.SetConsoleMode, "SetConsoleMode not found") + local GetLastError = assert(ffi.C.GetLastError, "GetLastError not found") local function wide_to_narrow(array, length) local t = {} for i = 0, length - 1 do @@ -108,7 +110,7 @@ BOOL SetConsoleMode(void *hConsoleHandle, DWORD dwMode); if filetype ~= 0x0003 then -- not FILE_TYPE_PIPE (0x0003) -- mintty must be a pipe if CLUTTEX_VERBOSITY >= 4 then - io.stderr:write("ClutTeX: not a pipe\n") + io.stderr:write("ClutTeX: is_mintty: not a pipe\n") end return false end @@ -118,18 +120,16 @@ BOOL SetConsoleMode(void *hConsoleHandle, DWORD dwMode); local filename = wide_to_narrow(nameinfo.FileName, math.floor(nameinfo.FileNameLength / 2)) -- \(cygwin|msys)-<hex digits>-pty<N>-(from|to)-master if CLUTTEX_VERBOSITY >= 4 then - io.stderr:write("ClutTeX: GetFileInformationByHandleEx returned ", filename, "\n") + io.stderr:write("ClutTeX: is_mintty: GetFileInformationByHandleEx returned ", filename, "\n") end local a, b = string.match(filename, "^\\(%w+)%-%x+%-pty%d+%-(%w+)%-master$") - if (a == "cygwin" or a == "msys") and (b == "from" or b == "to") then - return true - end + return (a == "cygwin" or a == "msys") and (b == "from" or b == "to") else if CLUTTEX_VERBOSITY >= 4 then - io.stderr:write("ClutTeX: GetFileInformationByHandleEx failed\n") + io.stderr:write("ClutTeX: is_mintty: GetFileInformationByHandleEx failed\n") end + return false end - return false end return { isatty = function(file) @@ -137,25 +137,52 @@ BOOL SetConsoleMode(void *hConsoleHandle, DWORD dwMode); local fd = fileno(file) return isatty(fd) ~= 0 or is_mintty(fd) end, - enable_console_colors = function(file) + enable_virtual_terminal = function(file) local fd = fileno(file) - if isatty(fd) ~= 0 then - local handle = get_osfhandle(fd) - local modePtr = ffi.new("DWORD[1]") - local result = GetConsoleMode(handle, modePtr) - if result ~= 0 then + if is_mintty(fd) then + -- MinTTY + if CLUTTEX_VERBOSITY >= 4 then + io.stderr:write("ClutTeX: Detected MinTTY\n") + end + return true + elseif isatty(fd) ~= 0 then + -- Check for ConEmu or ansicon + if os.getenv("ConEmuANSI") == "ON" or os.getenv("ANSICON") then + if CLUTTEX_VERBOSITY >= 4 then + io.stderr:write("ClutTeX: Detected ConEmu or ansicon\n") + end + return true + else + -- Try native VT support on recent Windows + local handle = get_osfhandle(fd) + local modePtr = ffi.new("DWORD[1]") + local result = GetConsoleMode(handle, modePtr) + if result == 0 then + if CLUTTEX_VERBOSITY >= 3 then + local err = GetLastError() + io.stderr:write(string.format("ClutTeX: GetConsoleMode failed (0x%08X)\n", err)) + end + return false + end local ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004 result = SetConsoleMode(handle, bitlib.bor(modePtr[0], ENABLE_VIRTUAL_TERMINAL_PROCESSING)) if result == 0 then + -- SetConsoleMode failed: Command Prompt on older Windows if CLUTTEX_VERBOSITY >= 3 then - io.stderr:write("ClutTeX: SetConsoleMode failed\n") + local err = GetLastError() + -- Typical error code: ERROR_INVALID_PARAMETER (0x57) + io.stderr:write(string.format("ClutTeX: SetConsoleMode failed (0x%08X)\n", err)) end + return false end - else - if CLUTTEX_VERBOSITY >= 3 then - io.stderr:write("ClutTeX: GetConsoleMode failed\n") + if CLUTTEX_VERBOSITY >= 4 then + io.stderr:write("ClutTeX: Detected recent Command Prompt\n") end + return true end + else + -- Not a TTY + return false end end, } |