removed debug printf
[CleanReadLine.git] / Clean System Files / readLine.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include <readline/readline.h>
5 #include <readline/history.h>
6
7 #include "Clean.h"
8
9 static char *cs_answer = (char *)NULL;
10 HISTORY_STATE *history_state = NULL;
11
12 char *cleanStringToCString(CleanString s){
13 unsigned long len = CleanStringLength(s);
14 char *cs = (char *)malloc(len+1);
15 if(cs == NULL){
16 printf("malloc failed...\n");
17 exit(1);
18 }
19 memcpy(cs, CleanStringCharacters(s), len);
20 cs[len] = '\0';
21 return cs;
22 }
23
24 void cleanReadLine(CleanString prompt, int history, CleanString *result)
25 {
26 char *cs_prompt = cleanStringToCString(prompt);
27
28 //Get the answer and add to history if not empty
29 if(cs_answer){
30 free(cs_answer);
31 cs_answer = (char *)NULL;
32 }
33 cs_answer = readline(cs_prompt);
34 free(cs_prompt);
35 if(cs_answer && *cs_answer && history){
36 add_history(cs_answer);
37 }
38
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);
49 }
50 }
51
52 void cleanSetReadLineName(CleanString name){
53 rl_readline_name = cleanStringToCString(name);
54 }
55
56 void cleanUsingHistory(){
57 using_history();
58 }
59
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;
64 }
65
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;
69
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);
74
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);
79 }
80
81 void cleanInitNewHistoryState(int offset, int flags, int num){
82 if(history_state != NULL){
83 free(history_state);
84 }
85 history_state = (HISTORY_STATE *)malloc(sizeof(HISTORY_STATE));
86 if(history_state == NULL){
87 printf("Not enough memory...\n");
88 exit(1);
89 }
90 HIST_ENTRY **entries = (HIST_ENTRY **)malloc(sizeof(HIST_ENTRY*)*num);
91 if(entries == NULL){
92 printf("Not enough memory...\n");
93 exit(1);
94 }
95 for(int i = 0; i<num; i++){
96 entries[i] = (HIST_ENTRY *)malloc(sizeof(HIST_ENTRY));
97 }
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;
103 }
104
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;
110 }
111
112 void cleanCommitSetHistory(void){
113 history_set_history_state(history_state);
114 }
115
116 void cleanAddHistory(CleanString entry){
117 char *cs_entry = cleanStringToCString(entry);
118 add_history(cs_entry);
119 free(cs_entry);
120 }
121
122 void cleanClearHistory(){
123 clear_history();
124 }
125
126 int cleanHistorySearch(CleanString s, int dir){
127 char *cs_s = cleanStringToCString(s);
128 int ret = history_search(cs_s, dir);
129 free(cs_s);
130 return ret;
131 }
132
133 int cleanHistorySearchPrefix(CleanString s, int dir){
134 char *cs_s = cleanStringToCString(s);
135 int ret = history_search_prefix(cs_s, dir);
136 free(cs_s);
137 return ret;
138 }
139
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);
143 free(cs_s);
144 return ret;
145 }
146
147 int cleanReadHistory(CleanString path)
148 {
149 char *cs_path = cleanStringToCString(path);
150 int errno = read_history(cs_path);
151 free(cs_path);
152 return errno;
153 }
154
155 int cleanReadHistoryRange(CleanString path, int from, int to)
156 {
157 char *cs_path = cleanStringToCString(path);
158 int errno = read_history_range(cs_path, from, to);
159 free(cs_path);
160 return errno;
161 }
162
163 int cleanWriteHistory(CleanString path)
164 {
165 char *cs_path = cleanStringToCString(path);
166 int errno = write_history(cs_path);
167 free(cs_path);
168 return errno;
169 }
170
171 int cleanAppendHistory(int n, CleanString path)
172 {
173 char *cs_path = cleanStringToCString(path);
174 int errno = append_history(n, cs_path);
175 free(cs_path);
176 return errno;
177 }
178
179 int cleanHistoryTruncateFile(CleanString path, int nlines)
180 {
181 char *cs_path = cleanStringToCString(path);
182 int errno = history_truncate_file(cs_path, nlines);
183 free(cs_path);
184 return errno;
185 }
186