2828#include <stdlib.h>
2929#include <string.h>
3030
31- #include "py/nlr.h"
32- #include "py/obj.h"
31+ #include "py/mpstate.h"
3332#include "input.h"
3433
35- #if MICROPY_USE_READLINE
34+ #if MICROPY_USE_READLINE == 1
35+ #include MICROPY_HAL_H
36+ #include "lib/mp-readline/readline.h"
37+ #elif MICROPY_USE_READLINE == 2
3638#include <readline/readline.h>
3739#include <readline/history.h>
3840#include <readline/tilde.h>
39- #else
40- #undef MICROPY_USE_READLINE_HISTORY
41- #define MICROPY_USE_READLINE_HISTORY (0)
4241#endif
4342
4443char * prompt (char * p ) {
45- #if MICROPY_USE_READLINE
44+ #if MICROPY_USE_READLINE == 1
45+ // MicroPython supplied readline
46+ vstr_t vstr ;
47+ vstr_init (& vstr , 16 );
48+ mp_hal_stdio_mode_raw ();
49+ int ret = readline (& vstr , p );
50+ mp_hal_stdio_mode_orig ();
51+ if (ret != 0 ) {
52+ vstr_clear (& vstr );
53+ if (ret == CHAR_CTRL_D ) {
54+ // EOF
55+ return NULL ;
56+ } else {
57+ printf ("\n" );
58+ char * line = malloc (1 );
59+ line [0 ] = '\0' ;
60+ return line ;
61+ }
62+ }
63+ vstr_null_terminated_str (& vstr );
64+ char * line = malloc (vstr .len + 1 );
65+ memcpy (line , vstr .buf , vstr .len + 1 );
66+ vstr_clear (& vstr );
67+ #elif MICROPY_USE_READLINE == 2
68+ // GNU readline
4669 char * line = readline (p );
4770 if (line ) {
4871 add_history (line );
4972 }
5073#else
74+ // simple read string
5175 static char buf [256 ];
5276 fputs (p , stdout );
5377 char * s = fgets (buf , sizeof (buf ), stdin );
@@ -68,13 +92,61 @@ char *prompt(char *p) {
6892
6993void prompt_read_history (void ) {
7094#if MICROPY_USE_READLINE_HISTORY
95+ #if MICROPY_USE_READLINE == 1
96+ readline_init0 (); // will clear history pointers
97+ char * home = getenv ("HOME" );
98+ if (home != NULL ) {
99+ vstr_t vstr ;
100+ vstr_init (& vstr , 50 );
101+ vstr_printf (& vstr , "%s/.micropython.history" , home );
102+ FILE * fp = fopen (vstr_null_terminated_str (& vstr ), "r" );
103+ if (fp != NULL ) {
104+ vstr_reset (& vstr );
105+ for (;;) {
106+ int c = fgetc (fp );
107+ if (c == EOF || c == '\n' ) {
108+ readline_push_history (vstr_null_terminated_str (& vstr ));
109+ if (c == EOF ) {
110+ break ;
111+ }
112+ vstr_reset (& vstr );
113+ } else {
114+ vstr_add_byte (& vstr , c );
115+ }
116+ }
117+ fclose (fp );
118+ }
119+ vstr_clear (& vstr );
120+ }
121+ #elif MICROPY_USE_READLINE == 2
71122 read_history (tilde_expand ("~/.micropython.history" ));
123+ #endif
72124#endif
73125}
74126
75127void prompt_write_history (void ) {
76128#if MICROPY_USE_READLINE_HISTORY
129+ #if MICROPY_USE_READLINE == 1
130+ char * home = getenv ("HOME" );
131+ if (home != NULL ) {
132+ vstr_t vstr ;
133+ vstr_init (& vstr , 50 );
134+ vstr_printf (& vstr , "%s/.micropython.history" , home );
135+ FILE * fp = fopen (vstr_null_terminated_str (& vstr ), "w" );
136+ if (fp != NULL ) {
137+ for (int i = MP_ARRAY_SIZE (MP_STATE_PORT (readline_hist )) - 1 ; i >= 0 ; i -- ) {
138+ const char * line = MP_STATE_PORT (readline_hist )[i ];
139+ if (line != NULL ) {
140+ fwrite (line , 1 , strlen (line ), fp );
141+ fputc ('\n' , fp );
142+ }
143+ }
144+ fclose (fp );
145+ }
146+ }
147+ #elif MICROPY_USE_READLINE == 2
77148 write_history (tilde_expand ("~/.micropython.history" ));
149+ #endif
78150#endif
79151}
80152
0 commit comments