diff --git a/docs/develop/porting.rst b/docs/develop/porting.rst index 3d0553205c..0511f5d276 100644 --- a/docs/develop/porting.rst +++ b/docs/develop/porting.rst @@ -174,9 +174,6 @@ The following is an example of an ``mpconfigport.h`` file: #define MP_STATE_PORT MP_STATE_VM - #define MICROPY_PORT_ROOT_POINTERS \ - const char *readline_hist[8]; - This configuration file contains machine-specific configurations including aspects like if different MicroPython features should be enabled e.g. ``#define MICROPY_ENABLE_GC (1)``. Making this Setting ``(0)`` disables the feature. diff --git a/examples/embedding/mpconfigport_minimal.h b/examples/embedding/mpconfigport_minimal.h index 6cc84dcbc1..91b194bc7b 100644 --- a/examples/embedding/mpconfigport_minimal.h +++ b/examples/embedding/mpconfigport_minimal.h @@ -87,8 +87,6 @@ #define MICROPY_PY_UHASHLIB (0) #define MICROPY_PY_UBINASCII (0) -#define MICROPY_PORT_ROOT_POINTERS \ - ////////////////////////////////////////// // Do not change anything beyond this line ////////////////////////////////////////// diff --git a/ports/cc3200/mpconfigport.h b/ports/cc3200/mpconfigport.h index 58ec80a9f5..ce708cb3f4 100644 --- a/ports/cc3200/mpconfigport.h +++ b/ports/cc3200/mpconfigport.h @@ -142,7 +142,6 @@ // vm state and root pointers for the gc #define MP_STATE_PORT MP_STATE_VM #define MICROPY_PORT_ROOT_POINTERS \ - const char *readline_hist[8]; \ mp_obj_t mp_const_user_interrupt; \ mp_obj_t machine_config_main; \ mp_obj_list_t pyb_sleep_obj_list; \ diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h index 1877a01437..a374d81203 100644 --- a/ports/esp32/mpconfigport.h +++ b/ports/esp32/mpconfigport.h @@ -146,7 +146,6 @@ struct mp_bluetooth_nimble_root_pointers_t; #endif #define MICROPY_PORT_ROOT_POINTERS \ - const char *readline_hist[8]; \ mp_obj_t machine_pin_irq_handler[40]; \ struct _machine_timer_obj_t *machine_timer_obj_head; \ struct _machine_i2s_obj_t *machine_i2s_obj[I2S_NUM_MAX]; \ diff --git a/ports/esp8266/mpconfigport.h b/ports/esp8266/mpconfigport.h index 7727f7845a..3f1db723a5 100644 --- a/ports/esp8266/mpconfigport.h +++ b/ports/esp8266/mpconfigport.h @@ -148,7 +148,6 @@ extern const struct _mp_print_t mp_debug_print; #define MP_STATE_PORT MP_STATE_VM #define MICROPY_PORT_ROOT_POINTERS \ - const char *readline_hist[8]; \ mp_obj_t pin_irq_handler[16]; \ byte *uart0_rxbuf; \ diff --git a/ports/javascript/Makefile b/ports/javascript/Makefile index aea9d6ec37..046d303336 100644 --- a/ports/javascript/Makefile +++ b/ports/javascript/Makefile @@ -30,7 +30,7 @@ SRC_C = \ mphalport.c \ modutime.c \ -SRC_QSTR += $(SRC_C) +SRC_QSTR += $(SRC_C) $(SRC_SHARED) OBJ += $(PY_O) OBJ += $(addprefix $(BUILD)/, $(SRC_SHARED:.c=.o)) diff --git a/ports/javascript/mpconfigport.h b/ports/javascript/mpconfigport.h index 4a6cb45883..01a61e391d 100644 --- a/ports/javascript/mpconfigport.h +++ b/ports/javascript/mpconfigport.h @@ -182,6 +182,3 @@ typedef long mp_off_t; #define MICROPY_HW_MCU_NAME "Emscripten" #define MP_STATE_PORT MP_STATE_VM - -#define MICROPY_PORT_ROOT_POINTERS \ - const char *readline_hist[8]; diff --git a/ports/mimxrt/Makefile b/ports/mimxrt/Makefile index 7ae3f91837..be1c95ba95 100644 --- a/ports/mimxrt/Makefile +++ b/ports/mimxrt/Makefile @@ -290,6 +290,7 @@ SRC_QSTR += \ modmimxrt.c \ modutime.c \ pin.c \ + shared/readline/readline.c \ shared/runtime/mpirq.c \ shared/runtime/sys_stdio_mphal.c \ $(GEN_PINS_SRC) diff --git a/ports/mimxrt/mpconfigport.h b/ports/mimxrt/mpconfigport.h index 1dcaccad47..3bff3e474d 100644 --- a/ports/mimxrt/mpconfigport.h +++ b/ports/mimxrt/mpconfigport.h @@ -242,7 +242,6 @@ extern const struct _mp_obj_type_t network_lan_type; #endif #define MICROPY_PORT_ROOT_POINTERS \ - const char *readline_hist[8]; \ struct _machine_timer_obj_t *timer_table[MICROPY_HW_PIT_NUM_CHANNELS]; \ void *machine_pin_irq_objects[MICROPY_HW_NUM_PIN_IRQS]; \ /* list of registered NICs */ \ diff --git a/ports/minimal/Makefile b/ports/minimal/Makefile index fc3730e700..ac74925b57 100644 --- a/ports/minimal/Makefile +++ b/ports/minimal/Makefile @@ -68,6 +68,7 @@ SRC_C += $(SRC_MOD) SRC_CXX += $(SRC_MOD_CXX) +SRC_QSTR += shared/readline/readline.c SRC_QSTR += $(SRC_MOD) $(SRC_MOD_CXX) OBJ += $(PY_CORE_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) diff --git a/ports/minimal/mpconfigport.h b/ports/minimal/mpconfigport.h index 7d6a3adedc..02312e2e40 100644 --- a/ports/minimal/mpconfigport.h +++ b/ports/minimal/mpconfigport.h @@ -41,6 +41,3 @@ typedef long mp_off_t; #endif #define MP_STATE_PORT MP_STATE_VM - -#define MICROPY_PORT_ROOT_POINTERS \ - const char *readline_hist[8]; diff --git a/ports/nrf/mpconfigport.h b/ports/nrf/mpconfigport.h index 0698a52cef..fcbe71c5a0 100644 --- a/ports/nrf/mpconfigport.h +++ b/ports/nrf/mpconfigport.h @@ -332,7 +332,6 @@ typedef long mp_off_t; #endif #define MICROPY_PORT_ROOT_POINTERS \ - const char *readline_hist[8]; \ mp_obj_t pin_class_mapper; \ mp_obj_t pin_class_map_dict; \ mp_obj_t pin_irq_handlers[NUM_OF_PINS]; \ diff --git a/ports/pic16bit/mpconfigport.h b/ports/pic16bit/mpconfigport.h index e97a465028..e03fb81a6a 100644 --- a/ports/pic16bit/mpconfigport.h +++ b/ports/pic16bit/mpconfigport.h @@ -92,7 +92,6 @@ typedef int mp_off_t; #define MP_STATE_PORT MP_STATE_VM #define MICROPY_PORT_ROOT_POINTERS \ - char *readline_hist[8]; \ mp_obj_t keyboard_interrupt_obj; \ #define MICROPY_MPHALPORT_H "pic16bit_mphal.h" diff --git a/ports/powerpc/Makefile b/ports/powerpc/Makefile index a1e6fa4dac..f1ebd6764c 100644 --- a/ports/powerpc/Makefile +++ b/ports/powerpc/Makefile @@ -42,6 +42,8 @@ SRC_C = \ shared/runtime/stdout_helpers.c \ $(BUILD)/_frozen_mpy.c \ +SRC_QSTR += shared/readline/readline.c + OBJ = $(PY_CORE_O) OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) OBJ += $(BUILD)/head.o diff --git a/ports/powerpc/mpconfigport.h b/ports/powerpc/mpconfigport.h index 0b868e3daf..084a461a4a 100644 --- a/ports/powerpc/mpconfigport.h +++ b/ports/powerpc/mpconfigport.h @@ -112,9 +112,6 @@ typedef long mp_off_t; #define MP_STATE_PORT MP_STATE_VM -#define MICROPY_PORT_ROOT_POINTERS \ - const char *readline_hist[8]; - // powerpc64 gcc doesn't seem to define these // These are pointers, so make them 64 bit types typedef long intptr_t; diff --git a/ports/renesas-ra/mpconfigport.h b/ports/renesas-ra/mpconfigport.h index 64caa69fd2..11b106999a 100644 --- a/ports/renesas-ra/mpconfigport.h +++ b/ports/renesas-ra/mpconfigport.h @@ -168,8 +168,6 @@ #endif #define MICROPY_PORT_ROOT_POINTERS \ - const char *readline_hist[8]; \ - \ mp_obj_t pyb_hid_report_desc; \ \ mp_obj_t pyb_config_main; \ diff --git a/ports/rp2/CMakeLists.txt b/ports/rp2/CMakeLists.txt index 202261f9c1..8f7895459e 100644 --- a/ports/rp2/CMakeLists.txt +++ b/ports/rp2/CMakeLists.txt @@ -130,6 +130,7 @@ set(MICROPY_SOURCE_PORT set(MICROPY_SOURCE_QSTR ${MICROPY_SOURCE_PY} + ${MICROPY_DIR}/shared/readline/readline.c ${MICROPY_DIR}/shared/runtime/mpirq.c ${MICROPY_DIR}/shared/runtime/sys_stdio_mphal.c ${PROJECT_SOURCE_DIR}/machine_adc.c diff --git a/ports/rp2/mpconfigport.h b/ports/rp2/mpconfigport.h index 1a63476c7c..1e85104844 100644 --- a/ports/rp2/mpconfigport.h +++ b/ports/rp2/mpconfigport.h @@ -231,7 +231,6 @@ extern const struct _mod_network_nic_type_t mod_network_nic_type_wiznet5k; #endif #define MICROPY_PORT_ROOT_POINTERS \ - const char *readline_hist[8]; \ void *machine_pin_irq_obj[30]; \ void *rp2_pio_irq_obj[2]; \ void *rp2_state_machine_irq_obj[8]; \ diff --git a/ports/samd/Makefile b/ports/samd/Makefile index 17dd811690..b3d6d8b228 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -127,6 +127,7 @@ SRC_QSTR += \ machine_led.c \ modsamd.c \ samd_flash.c \ + shared/readline/readline.c \ SRC_QSTR += $(SRC_MOD) $(SRC_CXX) diff --git a/ports/samd/mpconfigport.h b/ports/samd/mpconfigport.h index 1625496a2e..96d34bf4da 100644 --- a/ports/samd/mpconfigport.h +++ b/ports/samd/mpconfigport.h @@ -99,9 +99,6 @@ #define mp_type_fileio mp_type_vfs_lfs1_fileio #define mp_type_textio mp_type_vfs_lfs1_textio -#define MICROPY_PORT_ROOT_POINTERS \ - const char *readline_hist[8]; - #define MP_STATE_PORT MP_STATE_VM // Miscellaneous settings diff --git a/ports/stm32/mpconfigport.h b/ports/stm32/mpconfigport.h index a87fe3768f..4afad339bd 100644 --- a/ports/stm32/mpconfigport.h +++ b/ports/stm32/mpconfigport.h @@ -258,8 +258,6 @@ struct _mp_bluetooth_btstack_root_pointers_t; #endif #define MICROPY_PORT_ROOT_POINTERS \ - const char *readline_hist[8]; \ - \ mp_obj_t pyb_hid_report_desc; \ \ mp_obj_t pyb_config_main; \ diff --git a/ports/teensy/mpconfigport.h b/ports/teensy/mpconfigport.h index db1f56757a..b870263dfc 100644 --- a/ports/teensy/mpconfigport.h +++ b/ports/teensy/mpconfigport.h @@ -42,7 +42,6 @@ extern const struct _mp_obj_module_t pyb_module; #define MP_STATE_PORT MP_STATE_VM #define MICROPY_PORT_ROOT_POINTERS \ - const char *readline_hist[8]; \ mp_obj_t pin_class_mapper; \ mp_obj_t pin_class_map_dict; \ struct _pyb_uart_obj_t *pyb_stdio_uart; \ diff --git a/ports/unix/mpconfigport.h b/ports/unix/mpconfigport.h index 232bfb857c..4594b8be9d 100644 --- a/ports/unix/mpconfigport.h +++ b/ports/unix/mpconfigport.h @@ -129,6 +129,9 @@ #define MICROPY_READER_POSIX (1) #define MICROPY_READER_VFS (1) #define MICROPY_USE_READLINE_HISTORY (1) +#ifndef MICROPY_READLINE_HISTORY_SIZE +#define MICROPY_READLINE_HISTORY_SIZE 50 +#endif #define MICROPY_HELPER_LEXER_UNIX (1) #ifndef MICROPY_FLOAT_IMPL #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_DOUBLE) @@ -302,7 +305,6 @@ struct _mp_bluetooth_nimble_malloc_t; #endif #define MICROPY_PORT_ROOT_POINTERS \ - const char *readline_hist[50]; \ void *mmap_region_head; \ MICROPY_BLUETOOTH_ROOT_POINTERS \ diff --git a/ports/windows/mpconfigport.h b/ports/windows/mpconfigport.h index 47292b3bb6..39f8c0059c 100644 --- a/ports/windows/mpconfigport.h +++ b/ports/windows/mpconfigport.h @@ -33,6 +33,12 @@ #ifndef MICROPY_USE_READLINE #define MICROPY_USE_READLINE (1) #endif +#ifndef MICROPY_USE_READLINE_HISTORY +#define MICROPY_USE_READLINE_HISTORY (1) +#endif +#ifndef MICROPY_READLINE_HISTORY_SIZE +#define MICROPY_READLINE_HISTORY_SIZE (50) +#endif #define MICROPY_ALLOC_PATH_MAX (260) // see minwindef.h for msvc or limits.h for mingw #define MICROPY_PERSISTENT_CODE_LOAD (1) @@ -52,7 +58,6 @@ #define MICROPY_DEBUG_PRINTERS (1) #define MICROPY_READER_POSIX (1) #define MICROPY_READER_VFS (1) -#define MICROPY_USE_READLINE_HISTORY (1) #define MICROPY_HELPER_REPL (1) #define MICROPY_REPL_EMACS_KEYS (1) #define MICROPY_REPL_AUTO_INDENT (1) @@ -210,11 +215,6 @@ typedef long long mp_off_t; typedef long mp_off_t; #endif -#if MICROPY_USE_READLINE == 1 -#define MICROPY_PORT_ROOT_POINTERS \ - char *readline_hist[50]; -#endif - #define MP_STATE_PORT MP_STATE_VM #define MICROPY_MPHALPORT_H "windows_mphal.h" diff --git a/ports/zephyr/mpconfigport.h b/ports/zephyr/mpconfigport.h index 26610ccced..1be755baab 100644 --- a/ports/zephyr/mpconfigport.h +++ b/ports/zephyr/mpconfigport.h @@ -134,7 +134,6 @@ typedef long mp_off_t; #define MP_STATE_PORT MP_STATE_VM #define MICROPY_PORT_ROOT_POINTERS \ - const char *readline_hist[8]; \ void *machine_pin_irq_list; /* Linked list of pin irq objects */ \ struct _mp_bluetooth_zephyr_root_pointers_t *bluetooth_zephyr_root_pointers; diff --git a/ports/zephyr/mpconfigport_minimal.h b/ports/zephyr/mpconfigport_minimal.h index f0e57d7566..e819d5cf41 100644 --- a/ports/zephyr/mpconfigport_minimal.h +++ b/ports/zephyr/mpconfigport_minimal.h @@ -83,6 +83,3 @@ typedef unsigned mp_uint_t; // must be pointer size typedef long mp_off_t; #define MP_STATE_PORT MP_STATE_VM - -#define MICROPY_PORT_ROOT_POINTERS \ - const char *readline_hist[8]; diff --git a/py/mpconfig.h b/py/mpconfig.h index d70d39ae97..adaa3ec1fb 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -699,6 +699,11 @@ #define MICROPY_REPL_EVENT_DRIVEN (0) #endif +// The number of items to keep in the readline history. +#ifndef MICROPY_READLINE_HISTORY_SIZE +#define MICROPY_READLINE_HISTORY_SIZE (8) +#endif + // Whether to include lexer helper function for unix #ifndef MICROPY_HELPER_LEXER_UNIX #define MICROPY_HELPER_LEXER_UNIX (0) diff --git a/shared/readline/readline.c b/shared/readline/readline.c index 9b8961712d..b85bbd5bea 100644 --- a/shared/readline/readline.c +++ b/shared/readline/readline.c @@ -40,16 +40,19 @@ #define DEBUG_printf(...) (void)0 #endif -#define READLINE_HIST_SIZE (MP_ARRAY_SIZE(MP_STATE_PORT(readline_hist))) - // flags for readline_t.auto_indent_state #define AUTO_INDENT_ENABLED (0x01) #define AUTO_INDENT_JUST_ADDED (0x02) enum { ESEQ_NONE, ESEQ_ESC, ESEQ_ESC_BRACKET, ESEQ_ESC_BRACKET_DIGIT, ESEQ_ESC_O }; +#ifdef _MSC_VER +// work around MSVC compiler bug: https://stackoverflow.com/q/62259834/1976323 +#pragma warning(disable : 4090) +#endif + void readline_init0(void) { - memset(MP_STATE_PORT(readline_hist), 0, READLINE_HIST_SIZE * sizeof(const char*)); + memset(MP_STATE_PORT(readline_hist), 0, MICROPY_READLINE_HISTORY_SIZE * sizeof(const char*)); } STATIC char *str_dup_maybe(const char *str) { @@ -334,7 +337,7 @@ backward_kill_word: up_arrow_key: #endif // up arrow - if (rl.hist_cur + 1 < (int)READLINE_HIST_SIZE && MP_STATE_PORT(readline_hist)[rl.hist_cur + 1] != NULL) { + if (rl.hist_cur + 1 < MICROPY_READLINE_HISTORY_SIZE && MP_STATE_PORT(readline_hist)[rl.hist_cur + 1] != NULL) { // increase hist num rl.hist_cur += 1; // set line to history @@ -570,10 +573,12 @@ void readline_push_history(const char *line) { // so update the history char *most_recent_hist = str_dup_maybe(line); if (most_recent_hist != NULL) { - for (int i = READLINE_HIST_SIZE - 1; i > 0; i--) { + for (int i = MICROPY_READLINE_HISTORY_SIZE - 1; i > 0; i--) { MP_STATE_PORT(readline_hist)[i] = MP_STATE_PORT(readline_hist)[i - 1]; } MP_STATE_PORT(readline_hist)[0] = most_recent_hist; } } } + +MP_REGISTER_ROOT_POINTER(const char *readline_hist[MICROPY_READLINE_HISTORY_SIZE]);