diff --git a/src/interpreter/vmx.c b/src/interpreter/vmx.c index 28d19c5f..8beb0240 100644 --- a/src/interpreter/vmx.c +++ b/src/interpreter/vmx.c @@ -1,6 +1,13 @@ #include "pocketpy/interpreter/vm.h" #include +// importing io.h for tty detection in replinput +#if defined(_WIN32) || defined(_WIN64) +#include +#elif defined(__linux__) || defined(__APPLE__) || defined(__ANDROID__) +#include +#endif + void pk_print_stack(VM* self, py_Frame* frame, Bytecode byte) { return; if(frame == NULL || !self->main || py_isnil(self->main)) return; @@ -66,12 +73,20 @@ bool pk_wrapper__self(int argc, py_Ref argv) { return true; } +// macro for tty-sensitiveness +#if defined(_WIN32) || defined(_WIN64) +#define istty(fd) _isatty(_fileno(fd)) +#elif defined(__linux__) || defined(__APPLE__) || defined(__ANDROID__) +#define istty(fd) isatty(fileno(fd)) +#else +#define istty(fd) 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; - printf(">>> "); + if (istty(stdin)) printf(">>> "); while(true) { int c = pk_current_vm->callbacks.getchr(); @@ -107,6 +122,7 @@ int py_replinput(char* buf, int max_size) { buf[size] = '\0'; return size; } +#undef istty py_Ref py_name2ref(py_Name name) { assert(name != NULL); diff --git a/src2/main.c b/src2/main.c index 4212293b..453585b7 100644 --- a/src2/main.c +++ b/src2/main.c @@ -9,6 +9,16 @@ #include #endif +#if defined(_WIN32) || defined(_WIN64) +#include +#define istty(fd) _isatty(_fileno(fd)) +#elif defined(__linux__) || defined(__APPLE__) || defined(__ANDROID__) +#include +#define istty(fd) isatty(fileno(fd)) +#else +#define istty(fd) 1 +#endif + static char* readfile(const char* path, int* data_size) { FILE* f = fopen(path, "rb"); if(f == NULL) return NULL; @@ -86,19 +96,21 @@ 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"); - printf("pocketpy " PK_VERSION " (" __DATE__ ", " __TIME__ ") "); - printf("[%d bit] on %s", (int)(sizeof(void*) * 8), PY_SYS_PLATFORM_STRING); + if (istty(stdin)) { + printf("pocketpy " PK_VERSION " (" __DATE__ ", " __TIME__ ") "); + printf("[%d bit] on %s", (int)(sizeof(void*) * 8), PY_SYS_PLATFORM_STRING); #ifndef NDEBUG - printf(" (DEBUG)"); + printf(" (DEBUG)"); #endif - printf("\n"); - printf("https://github.com/pocketpy/pocketpy\n"); - printf("Type \"exit()\" to exit.\n"); + printf("\n"); + printf("https://github.com/pocketpy/pocketpy\n"); + printf("Type \"exit()\" to exit.\n"); + } while(true) { int size = py_replinput(buf, sizeof(buf)); if(size == -1) { // Ctrl-D (i.e. EOF) - printf("\n"); + if (istty(stdin)) printf("\n"); break; } assert(size < sizeof(buf)); @@ -160,3 +172,4 @@ int main(int argc, char** argv) { if(debug) py_debugger_exit(code); return code; } +#undef istty \ No newline at end of file