e6a2c53cd02ba442ae00ba914c285af639670900
4 #include <readline/readline.h>
5 #include <readline/history.h>
9 static char *cs_answer
= (char *)NULL
;
10 HISTORY_STATE
*history_state
= NULL
;
12 char *cleanStringToCString(CleanString s
){
13 unsigned long len
= CleanStringLength(s
);
14 char *cs
= (char *)malloc(len
+1);
16 printf("malloc failed...\n");
19 memcpy(cs
, CleanStringCharacters(s
), len
);
24 void cleanReadLine(CleanString prompt
, int history
, CleanString
*result
)
26 char *cs_prompt
= cleanStringToCString(prompt
);
28 //Get the answer and add to history if not empty
31 cs_answer
= (char *)NULL
;
33 cs_answer
= readline(cs_prompt
);
35 if(cs_answer
&& *cs_answer
&& history
){
36 add_history(cs_answer
);
39 if(!cs_answer
){ //In case of an EOF
40 CleanStringVariable(answer
, 1);
41 *result
= (CleanString
) answer
;
42 memcpy(CleanStringCharacters(answer
), "", 1);
43 CleanStringLength(answer
) = 1;
44 } else { //In case of a proper response
45 CleanStringVariable(answer
, strlen(cs_answer
));
46 *result
= (CleanString
) answer
;
47 memcpy(CleanStringCharacters(answer
), cs_answer
, strlen(cs_answer
));
48 CleanStringLength(answer
) = strlen(cs_answer
);
52 void cleanSetReadLineName(CleanString name
){
53 rl_readline_name
= cleanStringToCString(name
);
56 void cleanUsingHistory(){
60 void cleanGetState(int *offset
, int *num
, int *flags
){
61 *offset
= history_get_history_state()->offset
;
62 *num
= history_get_history_state()->length
;
63 *flags
= history_get_history_state()->flags
;
66 void cleanGetHistoryItem(int num
, CleanString
*line
, CleanString
*timestamp
){
67 char *cs_line
= history_get_history_state()->entries
[num
]->line
;
68 char *cs_stamp
= history_get_history_state()->entries
[num
]->timestamp
;
70 CleanStringVariable(cleanLine
, strlen(cs_line
));
71 *line
= (CleanString
) cleanLine
;
72 memcpy(CleanStringCharacters(cleanLine
), cs_line
, strlen(cs_line
));
73 CleanStringLength(cleanLine
) = strlen(cs_line
);
75 CleanStringVariable(cleanTimestamp
, strlen(cs_stamp
));
76 *timestamp
= (CleanString
) cleanTimestamp
;
77 memcpy(CleanStringCharacters(cleanTimestamp
), cs_stamp
, strlen(cs_stamp
));
78 CleanStringLength(cleanTimestamp
) = strlen(cs_stamp
);
81 void cleanInitNewHistoryState(int offset
, int flags
, int num
){
82 if(history_state
!= NULL
){
85 history_state
= (HISTORY_STATE
*)malloc(sizeof(HISTORY_STATE
));
86 if(history_state
== NULL
){
87 printf("Not enough memory...\n");
90 HIST_ENTRY
**entries
= (HIST_ENTRY
**)malloc(sizeof(HIST_ENTRY
*)*num
);
92 printf("Not enough memory...\n");
95 for(int i
= 0; i
<num
; i
++){
96 entries
[i
] = (HIST_ENTRY
*)malloc(sizeof(HIST_ENTRY
));
98 history_state
->offset
= offset
;
99 history_state
->entries
= entries
;
100 history_state
->length
= num
;
101 history_state
->size
= num
;
102 history_state
->flags
= 0;
105 void cleanSetNewHistoryEntry(int i
, CleanString line
, CleanString timestamp
){
106 char *cs_line
= cleanStringToCString(line
);
107 char *cs_timestamp
= cleanStringToCString(timestamp
);
108 history_state
->entries
[i
]->line
= cs_line
;
109 history_state
->entries
[i
]->timestamp
= cs_timestamp
;
112 void cleanCommitSetHistory(void){
113 history_set_history_state(history_state
);
116 void cleanAddHistory(CleanString entry
){
117 char *cs_entry
= cleanStringToCString(entry
);
118 add_history(cs_entry
);
122 void cleanClearHistory(){
126 int cleanHistorySearch(CleanString s
, int dir
){
127 char *cs_s
= cleanStringToCString(s
);
128 int ret
= history_search(cs_s
, dir
);
133 int cleanHistorySearchPrefix(CleanString s
, int dir
){
134 char *cs_s
= cleanStringToCString(s
);
135 int ret
= history_search_prefix(cs_s
, dir
);
140 int cleanHistorySearchPos(CleanString s
, int dir
, int pos
){
141 char *cs_s
= cleanStringToCString(s
);
142 int ret
= history_search_pos(cs_s
, dir
, pos
);
147 int cleanReadHistory(CleanString path
)
149 char *cs_path
= cleanStringToCString(path
);
150 int errno
= read_history(cs_path
);
155 int cleanReadHistoryRange(CleanString path
, int from
, int to
)
157 char *cs_path
= cleanStringToCString(path
);
158 int errno
= read_history_range(cs_path
, from
, to
);
163 int cleanWriteHistory(CleanString path
)
165 char *cs_path
= cleanStringToCString(path
);
166 int errno
= write_history(cs_path
);
171 int cleanAppendHistory(int n
, CleanString path
)
173 char *cs_path
= cleanStringToCString(path
);
174 int errno
= append_history(n
, cs_path
);
179 int cleanHistoryTruncateFile(CleanString path
, int nlines
)
181 char *cs_path
= cleanStringToCString(path
);
182 int errno
= history_truncate_file(cs_path
, nlines
);