b72d31a5e8cf75a8cbd6a8b85bb72378cb177d6f
4 #include <readline/readline.h>
5 #include <readline/history.h>
9 static char *cs_answer
= (char *)NULL
;
11 char *cleanStringToCString(CleanString s
){
12 unsigned long len
= CleanStringLength(s
);
13 char *cs
= (char *)malloc(len
+1);
15 printf("malloc failed...\n");
18 memcpy(cs
, CleanStringCharacters(s
), len
);
23 void cleanReadLine(CleanString prompt
, int history
, CleanString
*result
)
25 char *cs_prompt
= cleanStringToCString(prompt
);
27 //Get the answer and add to history if not empty
30 cs_answer
= (char *)NULL
;
32 cs_answer
= readline(cs_prompt
);
34 if(cs_answer
&& *cs_answer
&& history
){
35 add_history(cs_answer
);
38 if(!cs_answer
){ //In case of an EOF
39 CleanStringVariable(answer
, 1);
40 *result
= (CleanString
) answer
;
41 memcpy(CleanStringCharacters(answer
), "", 1);
42 CleanStringLength(answer
) = 1;
43 } else { //In case of a proper response
44 CleanStringVariable(answer
, strlen(cs_answer
));
45 *result
= (CleanString
) answer
;
46 memcpy(CleanStringCharacters(answer
), cs_answer
, strlen(cs_answer
));
47 CleanStringLength(answer
) = strlen(cs_answer
);
51 void cleanSetReadLineName(CleanString name
){
52 rl_readline_name
= cleanStringToCString(name
);
55 void cleanUsingHistory(){
59 void cleanAddHistory(CleanString entry
){
60 char *cs_entry
= cleanStringToCString(entry
);
61 add_history(cs_entry
);
65 void cleanClearHistory(){
69 int cleanHistorySearch(CleanString s
, int dir
){
70 char *cs_s
= cleanStringToCString(s
);
71 int ret
= history_search(cs_s
, dir
);
76 int cleanHistorySearchPrefix(CleanString s
, int dir
){
77 char *cs_s
= cleanStringToCString(s
);
78 int ret
= history_search_prefix(cs_s
, dir
);
83 int cleanHistorySearchPos(CleanString s
, int dir
, int pos
){
84 char *cs_s
= cleanStringToCString(s
);
85 int ret
= history_search_pos(cs_s
, dir
, pos
);
90 int cleanReadHistory(CleanString path
)
92 char *cs_path
= cleanStringToCString(path
);
93 int errno
= read_history(cs_path
);
98 int cleanReadHistoryRange(CleanString path
, int from
, int to
)
100 char *cs_path
= cleanStringToCString(path
);
101 int errno
= read_history_range(cs_path
, from
, to
);
106 int cleanWriteHistory(CleanString path
)
108 char *cs_path
= cleanStringToCString(path
);
109 int errno
= write_history(cs_path
);
114 int cleanAppendHistory(int n
, CleanString path
)
116 char *cs_path
= cleanStringToCString(path
);
117 int errno
= append_history(n
, cs_path
);
122 int cleanHistoryTruncateFile(CleanString path
, int nlines
)
124 char *cs_path
= cleanStringToCString(path
);
125 int errno
= history_truncate_file(cs_path
, nlines
);