4836bcbb380874e1fc2c41c77015ea0ca2815d78
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 cleanGetState(int *offset
, int *num
, int *flags
){
60 *offset
= history_get_history_state()->offset
;
61 *num
= history_get_history_state()->length
;
62 *flags
= history_get_history_state()->flags
;
65 void cleanSetState(int offset
, int num
, int flags
){
69 void cleanGetHistoryItem(int num
, CleanString
*line
, CleanString
*timestamp
){
70 char *cs_line
= history_get_history_state()->entries
[num
]->line
;
71 char *cs_stamp
= history_get_history_state()->entries
[num
]->timestamp
;
73 CleanStringVariable(cleanLine
, strlen(cs_line
));
74 *line
= (CleanString
) cleanLine
;
75 memcpy(CleanStringCharacters(cleanLine
), cs_line
, strlen(cs_line
));
76 CleanStringLength(cleanLine
) = strlen(cs_line
);
78 CleanStringVariable(cleanTimestamp
, strlen(cs_stamp
));
79 *timestamp
= (CleanString
) cleanTimestamp
;
80 memcpy(CleanStringCharacters(cleanTimestamp
), cs_stamp
, strlen(cs_stamp
));
81 CleanStringLength(cleanTimestamp
) = strlen(cs_stamp
);
84 void cleanAddHistory(CleanString entry
){
85 char *cs_entry
= cleanStringToCString(entry
);
86 add_history(cs_entry
);
90 void cleanClearHistory(){
94 int cleanHistorySearch(CleanString s
, int dir
){
95 char *cs_s
= cleanStringToCString(s
);
96 int ret
= history_search(cs_s
, dir
);
101 int cleanHistorySearchPrefix(CleanString s
, int dir
){
102 char *cs_s
= cleanStringToCString(s
);
103 int ret
= history_search_prefix(cs_s
, dir
);
108 int cleanHistorySearchPos(CleanString s
, int dir
, int pos
){
109 char *cs_s
= cleanStringToCString(s
);
110 int ret
= history_search_pos(cs_s
, dir
, pos
);
115 int cleanReadHistory(CleanString path
)
117 char *cs_path
= cleanStringToCString(path
);
118 int errno
= read_history(cs_path
);
123 int cleanReadHistoryRange(CleanString path
, int from
, int to
)
125 char *cs_path
= cleanStringToCString(path
);
126 int errno
= read_history_range(cs_path
, from
, to
);
131 int cleanWriteHistory(CleanString path
)
133 char *cs_path
= cleanStringToCString(path
);
134 int errno
= write_history(cs_path
);
139 int cleanAppendHistory(int n
, CleanString path
)
141 char *cs_path
= cleanStringToCString(path
);
142 int errno
= append_history(n
, cs_path
);
147 int cleanHistoryTruncateFile(CleanString path
, int nlines
)
149 char *cs_path
= cleanStringToCString(path
);
150 int errno
= history_truncate_file(cs_path
, nlines
);