From 7d38b19361a7e779461f6f85ad975b048478ef31 Mon Sep 17 00:00:00 2001 From: XingZhe-Li Date: Fri, 10 Jul 2026 13:49:12 +0800 Subject: [PATCH] use file mode other than REPL Mode when stdin is not a tty --- src/interpreter/vmx.c | 23 +--------------- src2/main.c | 63 ++++++++++++++++++++++++++++++++----------- 2 files changed, 48 insertions(+), 38 deletions(-) diff --git a/src/interpreter/vmx.c b/src/interpreter/vmx.c index 2137011e..28d19c5f 100644 --- a/src/interpreter/vmx.c +++ b/src/interpreter/vmx.c @@ -1,13 +1,6 @@ #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; @@ -73,25 +66,12 @@ bool pk_wrapper__self(int argc, py_Ref argv) { return true; } -// macro for tty-sensitiveness -#if defined(_WIN32) || defined(_WIN64) -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 isatty(0) -#else -#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) printf(">>> "); + printf(">>> "); while(true) { int c = pk_current_vm->callbacks.getchr(); @@ -127,7 +107,6 @@ 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 2d111791..eab03764 100644 --- a/src2/main.c +++ b/src2/main.c @@ -97,10 +97,41 @@ int main(int argc, char** argv) { const char* filename = arg1; if(filename == NULL) { - if(profile) printf("Warning: --profile is ignored in REPL mode.\n"); - if(debug) printf("Warning: --debug is ignored in REPL mode.\n"); + if(!istty) { + // File mode: read stdin and execute as a script + if(profile) printf("Warning: --profile is ignored in stdin mode.\n"); + if(debug) printf("Warning: --debug is ignored in stdin mode.\n"); + + size_t cap = 4096; + size_t total = 0; + char* data = PK_MALLOC(cap); + if(!data) { fprintf(stderr, "Error: out of memory\n"); py_finalize(); return 1; } + + int n; + while((n = fread(data + total, 1, cap - total - 1, stdin)) > 0) { + total += n; + if(total >= cap - 1) { + cap *= 2; + char* tmp = PK_REALLOC(data, cap); + if(!tmp) { PK_FREE(data); fprintf(stderr, "Error: out of memory\n"); py_finalize(); return 1; } + data = tmp; + } + } + data[total] = '\0'; + + if(total > 0) { + py_StackRef p0 = py_peek(0); + if(!py_exec(data, "", EXEC_MODE, NULL)) { + py_printexc(); + py_clearexc(p0); + } + } + PK_FREE(data); + } else { + // REPL mode + if(profile) printf("Warning: --profile is ignored in REPL mode.\n"); + if(debug) printf("Warning: --debug is ignored in REPL mode.\n"); - if (istty) { printf("pocketpy " PK_VERSION " (" __DATE__ ", " __TIME__ ") "); printf("[%d bit] on %s", (int)(sizeof(void*) * 8), PY_SYS_PLATFORM_STRING); #ifndef NDEBUG @@ -109,20 +140,20 @@ int main(int argc, char** argv) { 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) - if (istty) printf("\n"); - break; - } - assert(size < sizeof(buf)); - if(size >= 0) { - py_StackRef p0 = py_peek(0); - if(!py_exec(buf, "", SINGLE_MODE, NULL)) { - py_printexc(); - py_clearexc(p0); + while(true) { + int size = py_replinput(buf, sizeof(buf)); + if(size == -1) { // Ctrl-D (i.e. EOF) + printf("\n"); + break; + } + assert(size < sizeof(buf)); + if(size >= 0) { + py_StackRef p0 = py_peek(0); + if(!py_exec(buf, "", SINGLE_MODE, NULL)) { + py_printexc(); + py_clearexc(p0); + } } } }