Debian Codegraph @

texmf-dist/doc/support/cluttex/src/texrunner/isatty.lua

raw symbols SCIP
1 --[[
2 Copyright 2018 ARATA Mizuki
3
4 This file is part of ClutTeX.
5
6 ClutTeX is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 ClutTeX is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with ClutTeX. If not, see <http://www.gnu.org/licenses/>.
18 ]]
19
20 if os.type == "unix" then
21 -- Try LuaJIT-like FFI
22 local succ, M = pcall(function()
23 local ffi = require "ffi"
24 assert(ffi.os ~= "" and ffi.arch ~= "", "ffi library is stub")
25 ffi.cdef[[
26 int isatty(int fd);
27 int fileno(void *stream);
28 ]]
29 local isatty = assert(ffi.C.isatty, "isatty not found")
30 local fileno = assert(ffi.C.fileno, "fileno not found")
31 return {
32 isatty = function(file)
33 -- LuaJIT converts Lua's file handles into FILE* (void*)
34 return isatty(fileno(file)) ~= 0
35 end
36 }
37 end)
38 if succ then
39 if CLUTTEX_VERBOSITY >= 3 then
40 io.stderr:write("ClutTeX: isatty found via FFI (Unix)\n")
41 end
42 return M
43 else
44 if CLUTTEX_VERBOSITY >= 3 then
45 io.stderr:write("ClutTeX: FFI (Unix) not found: ", M, "\n")
46 end
47 end
48
49 -- Try luaposix
50 local succ, M = pcall(function()
51 local isatty = require "posix.unistd".isatty
52 local fileno = require "posix.stdio".fileno
53 return {
54 isatty = function(file)
55 return isatty(fileno(file)) == 1
56 end,
57 }
58 end)
59 if succ then
60 if CLUTTEX_VERBOSITY >= 3 then
61 io.stderr:write("ClutTeX: isatty found via luaposix\n")
62 end
63 return M
64 else
65 if CLUTTEX_VERBOSITY >= 3 then
66 io.stderr:write("ClutTeX: luaposix not found: ", M, "\n")
67 end
68 end
69
70 -- Fallback using system command
71 return {
72 isatty = function(file)
73 local fd
74 if file == io.stdin then
75 fd = 0
76 elseif file == io.stdout then
77 fd = 1
78 elseif file == io.stderr then
79 fd = 2
80 else
81 return false
82 end
83 local result = os.execute(string.format("test -t %d", fd))
84 return result == true or result == 0
85 end,
86 }
87
88 else
89 -- Try LuaJIT
90 local succ, M = pcall(function()
91 local ffi = require "ffi"
92 local bitlib = assert(bit32 or bit, "Neither bit32 (Lua 5.2) nor bit (LuaJIT) found") -- Lua 5.2 or LuaJIT
93 ffi.cdef[[
94 int _isatty(int fd);
95 int _fileno(void *stream);
96 void *_get_osfhandle(int fd); // should return intptr_t
97 typedef int BOOL;
98 typedef uint32_t DWORD;
99 typedef int FILE_INFO_BY_HANDLE_CLASS; // ???
100 typedef struct _FILE_NAME_INFO {
101 DWORD FileNameLength;
102 uint16_t FileName[?];
103 } FILE_NAME_INFO;
104 DWORD GetFileType(void *hFile);
105 BOOL GetFileInformationByHandleEx(void *hFile, FILE_INFO_BY_HANDLE_CLASS fic, void *fileinfo, DWORD dwBufferSize);
106 BOOL GetConsoleMode(void *hConsoleHandle, DWORD* lpMode);
107 BOOL SetConsoleMode(void *hConsoleHandle, DWORD dwMode);
108 DWORD GetLastError();
109 ]]
110 local isatty = assert(ffi.C._isatty, "_isatty not found")
111 local fileno = assert(ffi.C._fileno, "_fileno not found")
112 local get_osfhandle = assert(ffi.C._get_osfhandle, "_get_osfhandle not found")
113 local GetFileType = assert(ffi.C.GetFileType, "GetFileType not found")
114 local GetFileInformationByHandleEx = assert(ffi.C.GetFileInformationByHandleEx, "GetFileInformationByHandleEx not found")
115 local GetConsoleMode = assert(ffi.C.GetConsoleMode, "GetConsoleMode not found")
116 local SetConsoleMode = assert(ffi.C.SetConsoleMode, "SetConsoleMode not found")
117 local GetLastError = assert(ffi.C.GetLastError, "GetLastError not found")
118 local function wide_to_narrow(array, length)
119 local t = {}
120 for i = 0, length - 1 do
121 table.insert(t, string.char(math.min(array[i], 0xff)))
122 end
123 return table.concat(t, "")
124 end
125 local function is_mintty(fd)
126 local handle = get_osfhandle(fd)
127 local filetype = GetFileType(handle)
128 if filetype ~= 0x0003 then -- not FILE_TYPE_PIPE (0x0003)
129 -- mintty must be a pipe
130 if CLUTTEX_VERBOSITY >= 4 then
131 io.stderr:write("ClutTeX: is_mintty: not a pipe\n")
132 end
133 return false
134 end
135 local nameinfo = ffi.new("FILE_NAME_INFO", 32768)
136 local FileNameInfo = 2 -- : FILE_INFO_BY_HANDLE_CLASS
137 if GetFileInformationByHandleEx(handle, FileNameInfo, nameinfo, ffi.sizeof("FILE_NAME_INFO", 32768)) ~= 0 then
138 local filename = wide_to_narrow(nameinfo.FileName, math.floor(nameinfo.FileNameLength / 2))
139 -- \(cygwin|msys)-<hex digits>-pty<N>-(from|to)-master
140 if CLUTTEX_VERBOSITY >= 4 then
141 io.stderr:write("ClutTeX: is_mintty: GetFileInformationByHandleEx returned ", filename, "\n")
142 end
143 local a, b = string.match(filename, "^\\(%w+)%-%x+%-pty%d+%-(%w+)%-master$")
144 return (a == "cygwin" or a == "msys") and (b == "from" or b == "to")
145 else
146 if CLUTTEX_VERBOSITY >= 4 then
147 io.stderr:write("ClutTeX: is_mintty: GetFileInformationByHandleEx failed\n")
148 end
149 return false
150 end
151 end
152 return {
153 isatty = function(file)
154 -- LuaJIT converts Lua's file handles into FILE* (void*)
155 local fd = fileno(file)
156 return isatty(fd) ~= 0 or is_mintty(fd)
157 end,
158 enable_virtual_terminal = function(file)
159 local fd = fileno(file)
160 if is_mintty(fd) then
161 -- MinTTY
162 if CLUTTEX_VERBOSITY >= 4 then
163 io.stderr:write("ClutTeX: Detected MinTTY\n")
164 end
165 return true
166 elseif isatty(fd) ~= 0 then
167 -- Check for ConEmu or ansicon
168 if os.getenv("ConEmuANSI") == "ON" or os.getenv("ANSICON") then
169 if CLUTTEX_VERBOSITY >= 4 then
170 io.stderr:write("ClutTeX: Detected ConEmu or ansicon\n")
171 end
172 return true
173 else
174 -- Try native VT support on recent Windows
175 local handle = get_osfhandle(fd)
176 local modePtr = ffi.new("DWORD[1]")
177 local result = GetConsoleMode(handle, modePtr)
178 if result == 0 then
179 if CLUTTEX_VERBOSITY >= 3 then
180 local err = GetLastError()
181 io.stderr:write(string.format("ClutTeX: GetConsoleMode failed (0x%08X)\n", err))
182 end
183 return false
184 end
185 local ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004
186 result = SetConsoleMode(handle, bitlib.bor(modePtr[0], ENABLE_VIRTUAL_TERMINAL_PROCESSING))
187 if result == 0 then
188 -- SetConsoleMode failed: Command Prompt on older Windows
189 if CLUTTEX_VERBOSITY >= 3 then
190 local err = GetLastError()
191 -- Typical error code: ERROR_INVALID_PARAMETER (0x57)
192 io.stderr:write(string.format("ClutTeX: SetConsoleMode failed (0x%08X)\n", err))
193 end
194 return false
195 end
196 if CLUTTEX_VERBOSITY >= 4 then
197 io.stderr:write("ClutTeX: Detected recent Command Prompt\n")
198 end
199 return true
200 end
201 else
202 -- Not a TTY
203 return false
204 end
205 end,
206 }
207 end)
208 if succ then
209 if CLUTTEX_VERBOSITY >= 3 then
210 io.stderr:write("ClutTeX: isatty found via FFI (Windows)\n")
211 end
212 return M
213 else
214 if CLUTTEX_VERBOSITY >= 3 then
215 io.stderr:write("ClutTeX: FFI (Windows) not found: ", M, "\n")
216 end
217 end
218 end
219
220 return {
221 isatty = function(file)
222 return false
223 end,
224 }