| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
if os.type == "unix" then |
| 21 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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") |
| 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 |
| 129 |
|
| 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 |
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 189 |
if CLUTTEX_VERBOSITY >= 3 then |
| 190 |
local err = GetLastError() |
| 191 |
|
| 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 |
|
| 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 |
} |