1
+
1
2
/*
2
3
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3
4
*
9
10
#include "freertos/semphr.h"
10
11
#include "esp_repl.h"
11
12
#include "esp_err.h"
12
-
13
+ #include "esp_commands.h"
14
+ #include "esp_linenoise.h"
13
15
typedef enum {
14
16
ESP_REPL_STATE_RUNNING ,
15
17
ESP_REPL_STATE_STOPPED
@@ -32,10 +34,9 @@ typedef struct esp_repl_instance {
32
34
} \
33
35
} while(0)
34
36
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 )
36
38
{
37
- if ((config -> executor .func == NULL ) ||
38
- (config -> reader .func == NULL ) ||
39
+ if ((config -> linenoise_handle == NULL ) ||
39
40
(config -> max_cmd_line_size == 0 )) {
40
41
return ESP_ERR_INVALID_ARG ;
41
42
}
@@ -106,7 +107,21 @@ esp_err_t esp_repl_stop(esp_repl_instance_handle_t handle)
106
107
/* update the state to force the while loop in esp_repl to return */
107
108
state -> state = ESP_REPL_STATE_STOPPED ;
108
109
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 */
110
125
if (config -> on_stop .func != NULL ) {
111
126
config -> on_stop .func (config -> on_stop .ctx , handle );
112
127
}
@@ -117,7 +132,7 @@ esp_err_t esp_repl_stop(esp_repl_instance_handle_t handle)
117
132
/* give it back so destroy can also take/give symmetrically */
118
133
xSemaphoreGive (state -> mux );
119
134
120
- return ESP_OK ;
135
+ return ret_val ;
121
136
}
122
137
123
138
void esp_repl (esp_repl_instance_handle_t handle )
@@ -140,11 +155,22 @@ void esp_repl(esp_repl_instance_handle_t handle)
140
155
* function is called. */
141
156
xSemaphoreTake (state -> mux , portMAX_DELAY );
142
157
158
+ esp_linenoise_handle_t l_hdl = config -> linenoise_handle ;
159
+ esp_command_set_handle_t c_set = config -> command_set_handle ;
160
+
143
161
/* REPL loop */
144
162
while (state -> state == ESP_REPL_STATE_RUNNING ) {
145
163
146
164
/* 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
+ }
148
174
149
175
/* forward the raw command line to the pre executor callback (e.g., save in history).
150
176
* 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)
159
185
160
186
/* try to run the command */
161
187
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 );
163
189
164
190
/* forward the raw command line to the post executor callback (e.g., save in history).
165
191
* 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)
181
207
if (config -> on_exit .func != NULL ) {
182
208
config -> on_exit .func (config -> on_exit .ctx , handle );
183
209
}
210
+
211
+ printf ("returned from get_line\n" );
184
212
}
0 commit comments