@@ -1091,9 +1091,9 @@ int esp_linenoise_probe(esp_linenoise_instance_t *instance)
10911091 return 0 ;
10921092}
10931093
1094- #define esp_LINENOISE_CHECK_INSTANCE (handle ) \
1095- if(( handle == NULL) || ((esp_linenoise_instance_t*)handle->self != (esp_linenoise_instance_t*)handle)) { \
1096- return ESP_ERR_INVALID_ARG; \
1094+ #define ESP_LINENOISE_CHECK_INSTANCE (handle ) \
1095+ if(handle == NULL) { \
1096+ return ESP_ERR_INVALID_ARG; \
10971097}
10981098
10991099void esp_linenoise_get_instance_config_default (esp_linenoise_config_t * config )
@@ -1158,7 +1158,7 @@ esp_err_t esp_linenoise_create_instance(const esp_linenoise_config_t *config, es
11581158 }
11591159 if ((instance -> config .read_bytes_cb == NULL ) ||
11601160 (instance -> config .read_bytes_cb == esp_linenoise_default_read_bytes )) {
1161- /* since we are using the default read function, make sure
1161+ /* since we are using the default read function, make sure
11621162 * blocking read are set */
11631163 int flags = fcntl (instance -> config .in_fd , F_GETFL , 0 );
11641164 flags &= ~O_NONBLOCK ;
@@ -1195,16 +1195,14 @@ esp_err_t esp_linenoise_create_instance(const esp_linenoise_config_t *config, es
11951195 instance -> config .write_bytes_cb (instance -> config .out_fd , buf , len );
11961196 }
11971197
1198- /* set the self value to the handle of instance and assign the instance to out_handle */
1199- instance -> self = instance ;
12001198 * out_handle = (esp_linenoise_handle_t )instance ;
12011199
12021200 return ESP_OK ;
12031201}
12041202
12051203esp_err_t esp_linenoise_delete_instance (esp_linenoise_handle_t handle )
12061204{
1207- esp_LINENOISE_CHECK_INSTANCE (handle );
1205+ ESP_LINENOISE_CHECK_INSTANCE (handle );
12081206
12091207 esp_linenoise_instance_t * instance = (esp_linenoise_instance_t * )handle ;
12101208
@@ -1216,13 +1214,13 @@ esp_err_t esp_linenoise_delete_instance(esp_linenoise_handle_t handle)
12161214
12171215 // delete the mutex in the state and close the eventfd
12181216 // if it was created
1219- if (esp_linenoise_remove_event_fd != NULL ) {
1217+ if ((instance -> config .write_bytes_cb == esp_linenoise_default_write_bytes ) &&
1218+ (esp_linenoise_remove_event_fd != NULL )) {
12201219 ret_val = esp_linenoise_remove_event_fd (instance );
12211220 if (ret_val != ESP_OK ) {
12221221 return ret_val ;
12231222 }
12241223 }
1225-
12261224 // reset the memory
12271225 memset (instance , 0x00 , sizeof (esp_linenoise_instance_t ));
12281226
@@ -1234,7 +1232,7 @@ esp_err_t esp_linenoise_delete_instance(esp_linenoise_handle_t handle)
12341232
12351233esp_err_t esp_linenoise_get_line (esp_linenoise_handle_t handle , char * cmd_line_buffer , size_t cmd_line_length )
12361234{
1237- esp_LINENOISE_CHECK_INSTANCE (handle );
1235+ ESP_LINENOISE_CHECK_INSTANCE (handle );
12381236
12391237 if (cmd_line_buffer == NULL ) {
12401238 return ESP_ERR_INVALID_ARG ;
@@ -1280,6 +1278,10 @@ esp_err_t esp_linenoise_get_line(esp_linenoise_handle_t handle, char *cmd_line_b
12801278
12811279void esp_linenoise_add_completion (void * ctx , const char * str )
12821280{
1281+ if ((ctx == NULL ) || str == NULL ) {
1282+ return ;
1283+ }
1284+
12831285 esp_linenoise_completions_t * lc = (esp_linenoise_completions_t * )ctx ;
12841286
12851287 size_t len = strlen (str );
@@ -1301,7 +1303,11 @@ void esp_linenoise_add_completion(void *ctx, const char *str)
13011303
13021304esp_err_t esp_linenoise_history_add (esp_linenoise_handle_t handle , const char * line )
13031305{
1304- esp_LINENOISE_CHECK_INSTANCE (handle );
1306+ ESP_LINENOISE_CHECK_INSTANCE (handle );
1307+
1308+ if (line == NULL ) {
1309+ return ESP_ERR_INVALID_ARG ;
1310+ }
13051311
13061312 esp_linenoise_config_t * config = & ((esp_linenoise_instance_t * )handle )-> config ;
13071313 esp_linenoise_state_t * state = & ((esp_linenoise_instance_t * )handle )-> state ;
@@ -1345,10 +1351,13 @@ esp_err_t esp_linenoise_history_add(esp_linenoise_handle_t handle, const char *l
13451351
13461352esp_err_t esp_linenoise_history_save (esp_linenoise_handle_t handle , const char * filename )
13471353{
1348- esp_LINENOISE_CHECK_INSTANCE (handle );
1354+ ESP_LINENOISE_CHECK_INSTANCE (handle );
13491355
1350- esp_linenoise_instance_t * instance = (esp_linenoise_instance_t * )handle ;
1356+ if (filename == NULL ) {
1357+ return ESP_ERR_INVALID_ARG ;
1358+ }
13511359
1360+ esp_linenoise_instance_t * instance = (esp_linenoise_instance_t * )handle ;
13521361 FILE * fp ;
13531362 int j ;
13541363
@@ -1367,7 +1376,11 @@ esp_err_t esp_linenoise_history_save(esp_linenoise_handle_t handle, const char *
13671376
13681377esp_err_t esp_linenoise_history_load (esp_linenoise_handle_t handle , const char * filename )
13691378{
1370- esp_LINENOISE_CHECK_INSTANCE (handle );
1379+ ESP_LINENOISE_CHECK_INSTANCE (handle );
1380+
1381+ if (filename == NULL ) {
1382+ return ESP_ERR_INVALID_ARG ;
1383+ }
13711384
13721385 esp_linenoise_instance_t * instance = (esp_linenoise_instance_t * )handle ;
13731386
@@ -1408,7 +1421,7 @@ esp_err_t esp_linenoise_history_load(esp_linenoise_handle_t handle, const char *
14081421
14091422esp_err_t esp_linenoise_history_set_max_len (esp_linenoise_handle_t handle , int new_length )
14101423{
1411- esp_LINENOISE_CHECK_INSTANCE (handle );
1424+ ESP_LINENOISE_CHECK_INSTANCE (handle );
14121425
14131426 esp_linenoise_config_t * config = & ((esp_linenoise_instance_t * )handle )-> config ;
14141427 esp_linenoise_state_t * state = & ((esp_linenoise_instance_t * )handle )-> state ;
@@ -1456,7 +1469,7 @@ esp_err_t esp_linenoise_history_set_max_len(esp_linenoise_handle_t handle, int n
14561469
14571470esp_err_t esp_linenoise_history_free (esp_linenoise_handle_t handle )
14581471{
1459- esp_LINENOISE_CHECK_INSTANCE (handle );
1472+ ESP_LINENOISE_CHECK_INSTANCE (handle );
14601473 esp_linenoise_instance_t * instance = (esp_linenoise_instance_t * )handle ;
14611474
14621475 if (instance -> config .history ) {
@@ -1473,7 +1486,7 @@ esp_err_t esp_linenoise_history_free(esp_linenoise_handle_t handle)
14731486
14741487esp_err_t esp_linenoise_clear_screen (esp_linenoise_handle_t handle )
14751488{
1476- esp_LINENOISE_CHECK_INSTANCE (handle );
1489+ ESP_LINENOISE_CHECK_INSTANCE (handle );
14771490 esp_linenoise_instance_t * instance = (esp_linenoise_instance_t * )handle ;
14781491 esp_linenoise_config_t * config = & instance -> config ;
14791492
@@ -1489,49 +1502,64 @@ esp_err_t esp_linenoise_clear_screen(esp_linenoise_handle_t handle)
14891502
14901503esp_err_t esp_linenoise_set_empty_line (esp_linenoise_handle_t handle , bool empty_line )
14911504{
1492- esp_LINENOISE_CHECK_INSTANCE (handle );
1505+ ESP_LINENOISE_CHECK_INSTANCE (handle );
14931506 ((esp_linenoise_instance_t * )handle )-> config .allow_empty_line = empty_line ;
14941507 return ESP_OK ;
14951508}
14961509
14971510esp_err_t esp_linenoise_is_empty_line (esp_linenoise_handle_t handle , bool * is_empty_line )
14981511{
1499- esp_LINENOISE_CHECK_INSTANCE (handle );
1512+ ESP_LINENOISE_CHECK_INSTANCE (handle );
1513+
1514+ if (is_empty_line == NULL ) {
1515+ return ESP_ERR_INVALID_ARG ;
1516+ }
1517+
15001518 * is_empty_line = ((esp_linenoise_instance_t * )handle )-> config .allow_empty_line ;
15011519 return ESP_OK ;
15021520}
15031521
15041522esp_err_t esp_linenoise_set_multi_line (esp_linenoise_handle_t handle , bool multi_line )
15051523{
1506- esp_LINENOISE_CHECK_INSTANCE (handle );
1524+ ESP_LINENOISE_CHECK_INSTANCE (handle );
15071525 ((esp_linenoise_instance_t * )handle )-> config .allow_multi_line = multi_line ;
15081526 return ESP_OK ;
15091527}
15101528
15111529esp_err_t esp_linenoise_is_multi_line (esp_linenoise_handle_t handle , bool * is_multi_line )
15121530{
1513- esp_LINENOISE_CHECK_INSTANCE (handle );
1531+ ESP_LINENOISE_CHECK_INSTANCE (handle );
1532+
1533+ if (is_multi_line == NULL ) {
1534+ return ESP_ERR_INVALID_ARG ;
1535+ }
1536+
15141537 * is_multi_line = ((esp_linenoise_instance_t * )handle )-> config .allow_multi_line ;
15151538 return ESP_OK ;
15161539}
15171540
15181541esp_err_t esp_linenoise_set_dumb_mode (esp_linenoise_handle_t handle , bool dumb_mode )
15191542{
1520- esp_LINENOISE_CHECK_INSTANCE (handle );
1543+ ESP_LINENOISE_CHECK_INSTANCE (handle );
15211544 ((esp_linenoise_instance_t * )handle )-> config .allow_dumb_mode = dumb_mode ;
15221545 return ESP_OK ;
15231546}
15241547
15251548esp_err_t esp_linenoise_is_dumb_mode (esp_linenoise_handle_t handle , bool * is_dumb_mode )
15261549{
1527- esp_LINENOISE_CHECK_INSTANCE (handle );
1550+ ESP_LINENOISE_CHECK_INSTANCE (handle );
1551+
1552+ if (is_dumb_mode == NULL ) {
1553+ return ESP_ERR_INVALID_ARG ;
1554+ }
1555+
15281556 * is_dumb_mode = ((esp_linenoise_instance_t * )handle )-> config .allow_dumb_mode ;
15291557 return ESP_OK ;
15301558}
15311559
15321560esp_err_t esp_linenoise_set_max_cmd_line_length (esp_linenoise_handle_t handle , size_t length )
15331561{
1534- esp_LINENOISE_CHECK_INSTANCE (handle );
1562+ ESP_LINENOISE_CHECK_INSTANCE (handle );
15351563 if (length >= ESP_LINENOISE_MINIMAL_MAX_LINE ) {
15361564 ((esp_linenoise_instance_t * )handle )-> config .max_cmd_line_length = length ;
15371565 } else {
@@ -1542,7 +1570,60 @@ esp_err_t esp_linenoise_set_max_cmd_line_length(esp_linenoise_handle_t handle, s
15421570
15431571esp_err_t esp_linenoise_get_max_cmd_line_length (esp_linenoise_handle_t handle , size_t * max_cmd_line_length )
15441572{
1545- esp_LINENOISE_CHECK_INSTANCE (handle );
1573+ ESP_LINENOISE_CHECK_INSTANCE (handle );
1574+
1575+ if (max_cmd_line_length == NULL ) {
1576+ return ESP_ERR_INVALID_ARG ;
1577+ }
1578+
15461579 * max_cmd_line_length = ((esp_linenoise_instance_t * )handle )-> config .max_cmd_line_length ;
15471580 return ESP_OK ;
15481581}
1582+
1583+ esp_err_t esp_linenoise_get_out_fd (esp_linenoise_handle_t handle , int * fd )
1584+ {
1585+ ESP_LINENOISE_CHECK_INSTANCE (handle );
1586+
1587+ if (fd == NULL ) {
1588+ return ESP_ERR_INVALID_ARG ;
1589+ }
1590+
1591+ * fd = handle -> config .out_fd ;
1592+ return ESP_OK ;
1593+ }
1594+
1595+ esp_err_t esp_linenoise_get_in_fd (esp_linenoise_handle_t handle , int * fd )
1596+ {
1597+ ESP_LINENOISE_CHECK_INSTANCE (handle );
1598+
1599+ if (fd == NULL ) {
1600+ return ESP_ERR_INVALID_ARG ;
1601+ }
1602+
1603+ * fd = handle -> config .in_fd ;
1604+ return ESP_OK ;
1605+ }
1606+
1607+ esp_err_t esp_linenoise_get_read (esp_linenoise_handle_t handle , esp_linenoise_read_bytes_t * read_func )
1608+ {
1609+ ESP_LINENOISE_CHECK_INSTANCE (handle );
1610+
1611+ if (read_func == NULL ) {
1612+ return ESP_ERR_INVALID_ARG ;
1613+ }
1614+
1615+ * read_func = handle -> config .read_bytes_cb ;
1616+ return ESP_OK ;
1617+ }
1618+
1619+ esp_err_t esp_linenoise_get_write (esp_linenoise_handle_t handle , esp_linenoise_write_bytes_t * write_func )
1620+ {
1621+ ESP_LINENOISE_CHECK_INSTANCE (handle );
1622+
1623+ if (write_func == NULL ) {
1624+ return ESP_ERR_INVALID_ARG ;
1625+ }
1626+
1627+ * write_func = handle -> config .write_bytes_cb ;
1628+ return ESP_OK ;
1629+ }
0 commit comments