diff --git a/src/interpreter/vmx.c b/src/interpreter/vmx.c index 8beb0240..2137011e 100644 --- a/src/interpreter/vmx.c +++ b/src/interpreter/vmx.c @@ -3,7 +3,7 @@ // importing io.h for tty detection in replinput #if defined(_WIN32) || defined(_WIN64) -#include +#include #elif defined(__linux__) || defined(__APPLE__) || defined(__ANDROID__) #include #endif @@ -75,18 +75,23 @@ bool pk_wrapper__self(int argc, py_Ref argv) { // macro for tty-sensitiveness #if defined(_WIN32) || defined(_WIN64) -#define istty(fd) _isatty(_fileno(fd)) +static inline int isatty_win(DWORD std_handle_id) { + HANDLE handle = GetStdHandle(std_handle_id); DWORD mode; + if (handle == INVALID_HANDLE_VALUE || handle == NULL) return 0; + return GetConsoleMode(handle, &mode) != 0; +} +#define istty isatty_win(STD_INPUT_HANDLE) #elif defined(__linux__) || defined(__APPLE__) || defined(__ANDROID__) -#define istty(fd) isatty(fileno(fd)) +#define istty isatty(0) #else -#define istty(fd) 1 +#define istty 1 #endif int py_replinput(char* buf, int max_size) { buf[0] = '\0'; // reset first char because we check '@' at the beginning int size = 0; bool multiline = false; - if (istty(stdin)) printf(">>> "); + if (istty) printf(">>> "); while(true) { int c = pk_current_vm->callbacks.getchr(); diff --git a/src2/main.c b/src2/main.c index 453585b7..2d111791 100644 --- a/src2/main.c +++ b/src2/main.c @@ -10,13 +10,17 @@ #endif #if defined(_WIN32) || defined(_WIN64) -#include -#define istty(fd) _isatty(_fileno(fd)) +static inline int isatty_win(DWORD std_handle_id) { + HANDLE handle = GetStdHandle(std_handle_id); DWORD mode; + if (handle == INVALID_HANDLE_VALUE || handle == NULL) return 0; + return GetConsoleMode(handle, &mode) != 0; +} +#define istty isatty_win(STD_INPUT_HANDLE) #elif defined(__linux__) || defined(__APPLE__) || defined(__ANDROID__) #include -#define istty(fd) isatty(fileno(fd)) +#define istty isatty(STDIN_FILENO) #else -#define istty(fd) 1 +#define istty 1 #endif static char* readfile(const char* path, int* data_size) { @@ -96,7 +100,7 @@ int main(int argc, char** argv) { if(profile) printf("Warning: --profile is ignored in REPL mode.\n"); if(debug) printf("Warning: --debug is ignored in REPL mode.\n"); - if (istty(stdin)) { + if (istty) { printf("pocketpy " PK_VERSION " (" __DATE__ ", " __TIME__ ") "); printf("[%d bit] on %s", (int)(sizeof(void*) * 8), PY_SYS_PLATFORM_STRING); #ifndef NDEBUG @@ -110,7 +114,7 @@ int main(int argc, char** argv) { while(true) { int size = py_replinput(buf, sizeof(buf)); if(size == -1) { // Ctrl-D (i.e. EOF) - if (istty(stdin)) printf("\n"); + if (istty) printf("\n"); break; } assert(size < sizeof(buf)); @@ -172,4 +176,4 @@ int main(int argc, char** argv) { if(debug) py_debugger_exit(code); return code; } -#undef istty \ No newline at end of file +#undef istty