@@ -134,6 +134,9 @@ STATIC int execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind,
134134 }
135135}
136136
137+ #if MICROPY_USE_READLINE == 1
138+ #include "lib/mp-readline/readline.h"
139+ #else
137140STATIC char * strjoin (const char * s1 , int sep_char , const char * s2 ) {
138141 int l1 = strlen (s1 );
139142 int l2 = strlen (s2 );
@@ -147,10 +150,63 @@ STATIC char *strjoin(const char *s1, int sep_char, const char *s2) {
147150 s [l1 + l2 ] = 0 ;
148151 return s ;
149152}
153+ #endif
150154
151155STATIC int do_repl (void ) {
152156 mp_hal_stdout_tx_str ("Micro Python " MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE "; " MICROPY_PY_SYS_PLATFORM " version\n" );
153157
158+ #if MICROPY_USE_READLINE == 1
159+
160+ // use MicroPython supplied readline
161+
162+ vstr_t line ;
163+ vstr_init (& line , 16 );
164+ for (;;) {
165+ input_restart :
166+ vstr_reset (& line );
167+ mp_hal_stdio_mode_raw ();
168+ int ret = readline (& line , ">>> " );
169+
170+ if (ret == CHAR_CTRL_D ) {
171+ // EOF
172+ printf ("\n" );
173+ mp_hal_stdio_mode_orig ();
174+ vstr_clear (& line );
175+ return 0 ;
176+ } else if (line .len == 0 ) {
177+ if (ret != 0 ) {
178+ printf ("\n" );
179+ }
180+ mp_hal_stdio_mode_orig ();
181+ continue ;
182+ }
183+
184+ while (mp_repl_continue_with_input (vstr_null_terminated_str (& line ))) {
185+ vstr_add_byte (& line , '\n' );
186+ ret = readline (& line , "... " );
187+ if (ret == CHAR_CTRL_C ) {
188+ // cancel everything
189+ printf ("\n" );
190+ mp_hal_stdio_mode_orig ();
191+ goto input_restart ;
192+ } else if (ret == CHAR_CTRL_D ) {
193+ // stop entering compound statement
194+ break ;
195+ }
196+ }
197+ mp_hal_stdio_mode_orig ();
198+
199+ mp_lexer_t * lex = mp_lexer_new_from_str_len (MP_QSTR__lt_stdin_gt_ , line .buf , line .len , false);
200+ ret = execute_from_lexer (lex , MP_PARSE_SINGLE_INPUT , true);
201+ if (ret & FORCED_EXIT ) {
202+ return ret ;
203+ }
204+ }
205+
206+ #else
207+
208+ // use GNU or simple readline
209+
154210 for (;;) {
155211 char * line = prompt (">>> " );
156212 if (line == NULL ) {
@@ -175,6 +231,8 @@ STATIC int do_repl(void) {
175231 }
176232 free (line );
177233 }
234+
235+ #endif
178236}
179237
180238STATIC int do_file (const char * file ) {
0 commit comments