1+
12/*
23 * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
34 *
910#include "freertos/semphr.h"
1011#include "esp_repl.h"
1112#include "esp_err.h"
12-
13+ #include "esp_commands.h"
14+ #include "esp_linenoise.h"
1315typedef enum {
1416 ESP_REPL_STATE_RUNNING ,
1517 ESP_REPL_STATE_STOPPED
@@ -32,10 +34,9 @@ typedef struct esp_repl_instance {
3234 } \
3335} while(0)
3436
35- esp_err_t esp_repl_create (esp_repl_instance_handle_t * handle , const esp_repl_config_t * config )
37+ esp_err_t esp_repl_create (esp_repl_instance_handle_t * handle , const esp_repl_config_t * config )
3638{
37- if ((config -> executor .func == NULL ) ||
38- (config -> reader .func == NULL ) ||
39+ if ((config -> linenoise_handle == NULL ) ||
3940 (config -> max_cmd_line_size == 0 )) {
4041 return ESP_ERR_INVALID_ARG ;
4142 }
@@ -106,7 +107,21 @@ esp_err_t esp_repl_stop(esp_repl_instance_handle_t handle)
106107 /* update the state to force the while loop in esp_repl to return */
107108 state -> state = ESP_REPL_STATE_STOPPED ;
108109
109- /* Call the on_stop callback to let the user unblock reader.func, if provided */
110+ /* Call the abort function from esp_linenoise to force esp_linenoise_get_line to return.
111+ * This function is expected to return ESP_OK only if the user has registered a custom
112+ * read to the esp_linenoise instance and if the abort function succeeds.
113+ * ESP_ERR_INVALID_STATE is expected to be returned by esp_linenoise_abort if it is called
114+ * when the user has registered a custom read to the esp_linenoise instance. From the point
115+ * of view of esp_repl_stop, this return value if indicating that the user will have to take
116+ * care of returning from its own custom read by himself through the call of the on_stop callback,
117+ * therefore set the return value of esp_repl_stop to ESP_OK. */
118+ esp_err_t ret_val = esp_linenoise_abort (config -> linenoise_handle );
119+ if (ret_val == ESP_ERR_INVALID_STATE ) {
120+ ret_val = ESP_OK ;
121+ }
122+
123+ /* Call the on_stop callback to let the user unblock esp_linenoise
124+ * if a custom read is provided */
110125 if (config -> on_stop .func != NULL ) {
111126 config -> on_stop .func (config -> on_stop .ctx , handle );
112127 }
@@ -117,7 +132,7 @@ esp_err_t esp_repl_stop(esp_repl_instance_handle_t handle)
117132 /* give it back so destroy can also take/give symmetrically */
118133 xSemaphoreGive (state -> mux );
119134
120- return ESP_OK ;
135+ return ret_val ;
121136}
122137
123138void esp_repl (esp_repl_instance_handle_t handle )
@@ -140,11 +155,22 @@ void esp_repl(esp_repl_instance_handle_t handle)
140155 * function is called. */
141156 xSemaphoreTake (state -> mux , portMAX_DELAY );
142157
158+ esp_linenoise_handle_t l_hdl = config -> linenoise_handle ;
159+ esp_command_set_handle_t c_set = config -> command_set_handle ;
160+
143161 /* REPL loop */
144162 while (state -> state == ESP_REPL_STATE_RUNNING ) {
145163
146164 /* try to read a command line */
147- const esp_err_t read_ret = config -> reader .func (config -> reader .ctx , cmd_line , cmd_line_size );
165+ const esp_err_t read_ret = esp_linenoise_get_line (l_hdl , cmd_line , cmd_line_size );
166+
167+ /* Add the command to the history */
168+ esp_linenoise_history_add (l_hdl , cmd_line );
169+
170+ /* Save command history to filesystem */
171+ if (config -> history_save_path ) {
172+ esp_linenoise_history_save (l_hdl , config -> history_save_path );
173+ }
148174
149175 /* forward the raw command line to the pre executor callback (e.g., save in history).
150176 * this callback is not necessary for the user to register, continue if it isn't */
@@ -159,7 +185,7 @@ void esp_repl(esp_repl_instance_handle_t handle)
159185
160186 /* try to run the command */
161187 int cmd_func_ret ;
162- const esp_err_t exec_ret = config -> executor . func ( config -> executor . ctx , cmd_line , & cmd_func_ret );
188+ const esp_err_t exec_ret = esp_commands_execute ( c_set , cmd_line , & cmd_func_ret );
163189
164190 /* forward the raw command line to the post executor callback (e.g., save in history).
165191 * this callback is not necessary for the user to register, continue if it isn't */
@@ -181,4 +207,6 @@ void esp_repl(esp_repl_instance_handle_t handle)
181207 if (config -> on_exit .func != NULL ) {
182208 config -> on_exit .func (config -> on_exit .ctx , handle );
183209 }
210+
211+ printf ("returned from get_line\n" );
184212}
0 commit comments