mirror of
https://github.com/pocketpy/pocketpy
synced 2026-08-05 21:57:11 +08:00
use file mode other than REPL Mode when stdin is not a tty
This commit is contained in:
parent
4d74b66ffc
commit
7d38b19361
@ -1,13 +1,6 @@
|
||||
#include "pocketpy/interpreter/vm.h"
|
||||
#include <assert.h>
|
||||
|
||||
// importing io.h for tty detection in replinput
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
#include <windows.h>
|
||||
#elif defined(__linux__) || defined(__APPLE__) || defined(__ANDROID__)
|
||||
#include <unistd.h>
|
||||
#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);
|
||||
|
||||
63
src2/main.c
63
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, "<stdin>", 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, "<stdin>", 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, "<stdin>", SINGLE_MODE, NULL)) {
|
||||
py_printexc();
|
||||
py_clearexc(p0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user