Skip to content

Commit f9e79db

Browse files
author
Vincent Blondeau
committed
Use the right functions to deal with WINAPI HANDLEs + refactoring
1 parent 702a540 commit f9e79db

File tree

2 files changed

+34
-38
lines changed

2 files changed

+34
-38
lines changed

platforms/win32/plugins/FilePlugin/sqWin32FilePrims.c

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,15 @@ sqConnectToFile(SQFile *sqFile, void *file, sqInt writeFlag)
286286
return 1;
287287
}
288288

289-
290-
289+
void
290+
sqFileStdioHandlesIntoFile_WithHandle_IsWritable(SQFile file, HANDLE handle, int isWritable) {
291+
file.sessionID = thisSession;
292+
file.file = handle;
293+
file.writable = isWritable;
294+
file.lastOp = 0; /* unused on win32 */
295+
file.isStdioStream = isFileHandleATTY(handle);
296+
AddHandleToTable(win32Files, handle);
297+
}
291298

292299
/*
293300
* Fill-in files with handles for stdin, stdout and seterr as available and
@@ -297,32 +304,15 @@ sqConnectToFile(SQFile *sqFile, void *file, sqInt writeFlag)
297304
sqInt
298305
sqFileStdioHandlesInto(SQFile files[3])
299306
{
300-
DWORD mode;
301-
302-
files[0].sessionID = thisSession;
303-
files[0].file = GetStdHandle(STD_INPUT_HANDLE);
304-
files[0].writable = false;
305-
files[0].lastOp = 0; /* unused on win32 */
306-
files[0].isStdioStream = isFileHandleATTY(STD_INPUT_HANDLE);
307-
AddHandleToTable(win32Files, files[0].file);
308-
309-
files[1].sessionID = thisSession;
310-
files[1].file = GetStdHandle(STD_OUTPUT_HANDLE);
311-
files[1].writable = true;
312-
files[1].lastOp = 0; /* unused on win32 */
313-
files[1].isStdioStream = isFileHandleATTY(STD_OUTPUT_HANDLE);
314-
AddHandleToTable(win32Files, files[1].file);
315-
316-
files[2].sessionID = thisSession;
317-
files[2].file = GetStdHandle(STD_ERROR_HANDLE);
318-
files[2].writable = true;
319-
files[2].lastOp = 0; /* unused on win32 */
320-
files[2].isStdioStream = isFileHandleATTY(STD_ERROR_HANDLE);
321-
AddHandleToTable(win32Files, files[2].file);
307+
sqFileStdioHandlesIntoFile_WithHandle_IsWritable(files[0], GetStdHandle(STD_INPUT_HANDLE), false);
308+
sqFileStdioHandlesIntoFile_WithHandle_IsWritable(files[1], GetStdHandle(STD_OUTPUT_HANDLE), true);
309+
sqFileStdioHandlesIntoFile_WithHandle_IsWritable(files[2], GetStdHandle(STD_ERROR_HANDLE), true);
322310

323311
return 7;
324312
}
325313

314+
315+
326316
/*
327317
* Allow to test if the standard input/output files are from a console or not
328318
* 1 if stdio is redirected to a console pipe, else 0 (and in this case, a file should be created)

platforms/win32/vm/sqWin32Main.c

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -876,13 +876,24 @@ getVersionInfo(int verbose)
876876
* Inspired of: https://fossies.org/linux/misc/vim-8.0.tar.bz2/vim80/src/iscygpty.c?m=t
877877
*/
878878
sqInt isFileHandleATTY(HANDLE fdHandle) {
879-
//In case of Windows Shell case
880-
int res = _isatty(_fileno(fdHandle));
881-
if (res != 0)
882-
return res < 0;
883-
if (errno == EBADF)
879+
if (fdHandle == INVALID_HANDLE_VALUE) {
884880
return 0;
885-
//In case of Unix emulator, we parse the name of the pipe
881+
}
882+
883+
/* In case of Windows Shell case */
884+
DWORD fileType = GetFileType(fdHandle);
885+
if (fileType == FILE_TYPE_CHAR)
886+
/* The specified file is a character file, typically an LPT device or a console. */
887+
/* https://msdn.microsoft.com/en-us/library/windows/desktop/aa364960(v=vs.85).aspx */
888+
return 1;
889+
890+
/* In case of Unix emulator, we need to parse the name of the pipe */
891+
892+
/* Cygwin/msys's pty is a pipe. */
893+
if (fileType != FILE_TYPE_PIPE) {
894+
return 0;
895+
}
896+
886897
int size = sizeof(FILE_NAME_INFO) + sizeof(WCHAR) * MAX_PATH;
887898
FILE_NAME_INFO *nameinfo;
888899
WCHAR *p = NULL;
@@ -900,13 +911,7 @@ sqInt isFileHandleATTY(HANDLE fdHandle) {
900911
if (!pGetFileInformationByHandleEx)
901912
return 0;
902913
}
903-
if (fdHandle == INVALID_HANDLE_VALUE) {
904-
return 0;
905-
}
906-
/* Cygwin/msys's pty is a pipe. */
907-
if (GetFileType(fdHandle) != FILE_TYPE_PIPE) {
908-
return 0;
909-
}
914+
910915
nameinfo = malloc(size);
911916
if (nameinfo == NULL) {
912917
return 0;
@@ -932,7 +937,8 @@ sqInt isFileHandleATTY(HANDLE fdHandle) {
932937
* 1 if one of the stdio is redirected to a console pipe, else 0 (and in this case, a file should be created)
933938
*/
934939
sqInt isOneStdioDescriptorATTY() {
935-
return isFileHandleATTY(STD_INPUT_HANDLE) || isFileHandleATTY(STD_OUTPUT_HANDLE) || isFileHandleATTY(STD_ERROR_HANDLE);
940+
return isFileHandleATTY(GetStdHandle(STD_INPUT_HANDLE)) ||
941+
isFileHandleATTY(GetStdHandle(STD_OUTPUT_HANDLE)) || isFileHandleATTY(GetStdHandle(STD_ERROR_HANDLE));
936942
}
937943

938944
static void

0 commit comments

Comments
 (0)