fix arg_getBool, support REPL echo config

This commit is contained in:
Lyon 2023-07-11 16:36:28 +08:00
parent 813495e6bb
commit 0e4d927a5d
11 changed files with 65 additions and 25 deletions

View File

@ -15,5 +15,9 @@ class MemChecker:
def resetMax(self): ...
class REPL:
def setEcho(self, echo: bool): ...
class SysObj:
pass

View File

@ -1,4 +1,9 @@
#include "PikaStdLib_SysObj.h"
#include "PikaStdData_FILEIO.h"
#include "PikaStdLib_REPL.h"
#include "PikaVM.h"
#include "dataStrs.h"
void PikaStdLib_REPL_setEcho(PikaObj* self, pika_bool echo) {
pikaShellSetEcho(echo);
}

View File

@ -15,5 +15,9 @@ class MemChecker:
def resetMax(self): ...
class REPL:
def setEcho(self, echo: bool): ...
class SysObj:
pass

View File

@ -1,4 +1,9 @@
#include "PikaStdLib_SysObj.h"
#include "PikaStdData_FILEIO.h"
#include "PikaStdLib_REPL.h"
#include "PikaVM.h"
#include "dataStrs.h"
void PikaStdLib_REPL_setEcho(PikaObj* self, pika_bool echo) {
pikaShellSetEcho(echo);
}

View File

@ -4523,19 +4523,19 @@ HEAP_RETURN:
LBL(6)
LBL(7)
LBL(8)
LBL(9) LBL(10) LBL(11) LBL(12) LBL(13) LBL(14) LBL(15) LBL(17) LBL(19)
LBL(24) LBL(25) LBL(26) LBL(27) LBL(29) LBL(31) LBL(33) LBL(35)
LBL(43) LBL(47) LBL(48) LBL(49) LBL(50) LBL(51) LBL(52) LBL(53)
LBL(54)
LBL(9)
LBL(10) LBL(11) LBL(12) LBL(13) LBL(14) LBL(15) LBL(17) LBL(19) LBL(24)
LBL(25) LBL(26) LBL(27) LBL(29) LBL(31) LBL(33) LBL(35) LBL(43)
LBL(47) LBL(48) LBL(49) LBL(50) LBL(51) LBL(52) LBL(53) LBL(54)
#ifdef SUPPORT_UTF8
LBL(16) LBL(18) LBL(20) LBL(21) LBL(22) LBL(23) LBL(28)
LBL(30) LBL(32) LBL(34) LBL(42) LBL(46)
LBL(16) LBL(18) LBL(20) LBL(21) LBL(22) LBL(23) LBL(28)
LBL(30) LBL(32) LBL(34) LBL(42) LBL(46)
#ifdef SUPPORT_UCP
LBL(36) LBL(37) LBL(38) LBL(39) LBL(40) LBL(41)
LBL(44) LBL(45)
LBL(36) LBL(37) LBL(38) LBL(39) LBL(40) LBL(41)
LBL(44) LBL(45)
#endif /* SUPPORT_UCP */
#endif /* SUPPORT_UTF8 */
default
default
: DPRINTF(("jump error in pcre match: label %d non-existent\n",
frame->Xwhere));
return PCRE_ERROR_INTERNAL;

View File

@ -56,7 +56,7 @@ volatile PikaObjState g_PikaObjState = {
#endif
};
static volatile ShellConfig g_repl_shell;
static volatile ShellConfig g_REPL;
PikaObj* New_PikaStdData_Dict(Args* args);
PikaObj* New_PikaStdData_dict_keys(Args* args);
@ -207,7 +207,7 @@ int32_t obj_deinit(PikaObj* self) {
pikaGC_unlock();
if (bisRoot) {
pikaGC_markSweep();
shConfig_deinit((ShellConfig*)&g_repl_shell);
shConfig_deinit((ShellConfig*)&g_REPL);
#if __linux
disable_raw_mode();
#endif
@ -1598,11 +1598,13 @@ enum shellCTRL _inner_do_obj_runChar(PikaObj* self,
ShellConfig* shell) {
char* input_line = NULL;
enum shellCTRL ctrl = SHELL_CTRL_CONTINUE;
if (g_REPL.no_echo == pika_false) {
#if __linux
printf("%c", inputChar);
printf("%c", inputChar);
#elif !(defined(_WIN32))
pika_platform_printf("%c", inputChar);
pika_platform_printf("%c", inputChar);
#endif
}
if (inputChar == '\n' && shell->lastChar == '\r') {
ctrl = SHELL_CTRL_CONTINUE;
goto __exit;
@ -2008,18 +2010,19 @@ static enum shellCTRL __obj_shellLineHandler_REPL(PikaObj* self,
return SHELL_CTRL_CONTINUE;
}
static volatile ShellConfig g_repl_shell = {
static volatile ShellConfig g_REPL = {
.handler = __obj_shellLineHandler_REPL,
.prefix = ">>> ",
.blockBuffName = "@sh0",
#if PIKA_SHELL_HISTORY_ENABLE
.history = NULL,
#endif
.no_echo = PIKA_SHELL_NO_ECHO,
};
void pikaScriptShell_withGetchar(PikaObj* self, sh_getchar getchar_fn) {
g_repl_shell.fn_getchar = getchar_fn;
_do_pikaScriptShell(self, (ShellConfig*)&g_repl_shell);
g_REPL.fn_getchar = getchar_fn;
_do_pikaScriptShell(self, (ShellConfig*)&g_REPL);
}
int shConfig_deinit(ShellConfig* self) {
@ -2032,10 +2035,18 @@ int shConfig_deinit(ShellConfig* self) {
return 0;
}
void pikaScriptShell(PikaObj* self) {
void pikaPythonShell(PikaObj* self) {
pikaScriptShell_withGetchar(self, pika_platform_getchar);
}
void pikaShellSetEcho(pika_bool enable_echo) {
if (enable_echo) {
g_REPL.no_echo = pika_false;
} else {
g_REPL.no_echo = pika_true;
}
}
void obj_setErrorCode(PikaObj* self, int32_t errCode) {
obj_setInt(self, "__errCode", errCode);
}

View File

@ -336,7 +336,8 @@ int64_t fast_atoi(char* src);
char* fast_itoa(char* buf, uint32_t val);
/* shell */
void pikaScriptShell(PikaObj* self);
void pikaPythonShell(PikaObj* self);
void pikaShellSetEcho(pika_bool enable_echo);
enum shellCTRL { SHELL_CTRL_CONTINUE, SHELL_CTRL_EXIT };
typedef struct ShellConfig ShellConfig;
@ -401,6 +402,7 @@ struct ShellConfig {
#if PIKA_SHELL_HISTORY_ENABLE
ShellHistory* history;
#endif
pika_bool no_echo;
};
#if PIKA_SHELL_HISTORY_ENABLE

View File

@ -477,7 +477,17 @@ pika_bool arg_getBool(Arg* self) {
if (NULL == arg_getContent(self)) {
return _PIKA_BOOL_ERR;
}
return *(pika_bool*)arg_getContent(self);
ArgType type = arg_getType(self);
if (type == ARG_TYPE_BOOL) {
return *(pika_bool*)arg_getContent(self);
}
if (type == ARG_TYPE_INT) {
return (pika_bool)arg_getInt(self);
}
if (type == ARG_TYPE_FLOAT) {
return (pika_bool)arg_getFloat(self);
}
return _PIKA_BOOL_ERR;
}
void* arg_getPtr(Arg* self) {

View File

@ -207,11 +207,7 @@ pika_bool args_getBool(Args* self, char* name) {
if (NULL == arg) {
return _PIKA_BOOL_ERR;
}
ArgType arg_type = arg_getType(arg);
if (arg_type == ARG_TYPE_BOOL) {
return arg_getBool(arg);
}
return _PIKA_BOOL_ERR;
return arg_getBool(arg);
}
int32_t args_getSize(Args* self) {

View File

@ -104,7 +104,6 @@ extern "C" {
#define PIKA_FALSE pika_false
#define pikaScriptInit pikaPythonInit
#define pikaScriptShell pikaPythonShell
#endif
#endif

View File

@ -487,6 +487,10 @@ extern "C" {
#define PIKA_UNBUFFERED_ENABLE 0
#endif
#ifndef PIKA_SHELL_NO_ECHO
#define PIKA_SHELL_NO_ECHO 0
#endif
/* configuration validation */
#endif