31
31
#include <assert.h>
32
32
#include <sys/queue.h>
33
33
#include <termios.h>
34
+ #include <errno.h>
34
35
35
36
#include <editline/readline.h>
36
37
@@ -71,7 +72,7 @@ struct icli {
71
72
struct icli_command * curr_cmd ;
72
73
char * curr_prompt ;
73
74
const char * prompt ;
74
-
75
+ const char * hist_file ;
75
76
int rows ;
76
77
int cols ;
77
78
int curr_row ;
@@ -936,6 +937,7 @@ int icli_register_command(struct icli_command_params *params, struct icli_comman
936
937
int icli_init (struct icli_params * params )
937
938
{
938
939
memset (& icli , 0 , sizeof (icli ));
940
+ int ret = 0 ;
939
941
940
942
icli .root_cmd = calloc (1 , sizeof (struct icli_command ));
941
943
if (!icli .root_cmd ) {
@@ -951,20 +953,48 @@ int icli_init(struct icli_params *params)
951
953
icli .user_data = params -> user_data ;
952
954
953
955
icli .prompt = strdup (params -> prompt );
956
+ if (!icli .prompt ) {
957
+ icli_api_printf ("Unable to allocate memory for prompt\n" );
958
+ ret = -1 ;
959
+ goto err ;
960
+ }
961
+
962
+ if (params -> hist_file ) {
963
+ icli .hist_file = strdup (params -> hist_file );
964
+ if (!icli .hist_file ) {
965
+ icli_api_printf ("Unable to allocate memory for hist_file\n" );
966
+ ret = -1 ;
967
+ goto err ;
968
+ }
969
+ }
954
970
955
971
icli .cmd_hook = params -> cmd_hook ;
956
972
icli .out_hook = params -> out_hook ;
957
973
icli .err_hook = params -> err_hook ;
958
974
959
975
/* Allow conditional parsing of the ~/.inputrc file. */
960
976
rl_readline_name = strdup (params -> app_name );
977
+ if (!rl_readline_name ) {
978
+ icli_api_printf ("Unable to allocate memory for rl_readline_name\n" );
979
+ ret = -1 ;
980
+ goto err ;
981
+ }
961
982
962
983
/* Tell the completer that we want a crack first. */
963
984
rl_attempted_completion_function = icli_completion ;
964
985
965
986
using_history ();
966
987
stifle_history (params -> history_size );
967
988
989
+ if (icli .hist_file ) {
990
+ ret = read_history (icli .hist_file );
991
+ if (ret && ret != ENOENT ) {
992
+ icli_api_printf ("Unable to read history from %s (%d)\n" , icli .hist_file , ret );
993
+ ret = -1 ;
994
+ goto err ;
995
+ }
996
+ }
997
+
968
998
rl_get_screen_size (& icli .rows , & icli .cols );
969
999
970
1000
icli_build_prompt (icli .curr_cmd );
@@ -977,7 +1007,7 @@ int icli_init(struct icli_params *params)
977
1007
.argc = 1 ,
978
1008
.argv = execute_args }};
979
1009
struct icli_command * commands [array_len (cmd_params )] = {};
980
- int ret = icli_register_commands (cmd_params , commands , array_len (cmd_params ));
1010
+ ret = icli_register_commands (cmd_params , commands , array_len (cmd_params ));
981
1011
if (ret ) {
982
1012
goto err ;
983
1013
}
@@ -1001,8 +1031,6 @@ void icli_cleanup(void)
1001
1031
{
1002
1032
icli_clean_command (icli .root_cmd );
1003
1033
1004
- rl_callback_handler_remove ();
1005
-
1006
1034
HISTORY_STATE * hist_state = history_get_history_state ();
1007
1035
HIST_ENTRY * * mylist = history_list ();
1008
1036
@@ -1012,13 +1040,24 @@ void icli_cleanup(void)
1012
1040
free (mylist );
1013
1041
free (hist_state );
1014
1042
1043
+ if (icli .hist_file ) {
1044
+ int ret = write_history (icli .hist_file );
1045
+ if (ret )
1046
+ icli_api_printf ("Unable to save history to %s (%d)\n" , icli .hist_file , ret );
1047
+ }
1048
+
1015
1049
clear_history ();
1016
1050
1051
+ rl_callback_handler_remove ();
1052
+
1017
1053
free (rl_readline_name );
1018
1054
rl_readline_name = "" ;
1019
1055
1020
1056
free ((void * )icli .prompt );
1021
1057
free ((void * )icli .curr_prompt );
1058
+ free ((void * )icli .hist_file );
1059
+
1060
+ memset (& icli , 0 , sizeof (icli ));
1022
1061
}
1023
1062
1024
1063
void icli_run (void )
0 commit comments