nothing is inline anymore
[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
11 char *cleanStringToCString(CleanString s){
12 unsigned long len = CleanStringLength(s);
13 char *cs = (char *)malloc(len+1);
14 if(cs == NULL){
15 printf("malloc failed...\n");
16 exit(1);
17 }
18 memcpy(cs, CleanStringCharacters(s), len);
19 cs[len] = '\0';
20 return cs;
21 }
22
23 void cleanReadLine(CleanString prompt, int history, CleanString *result)
24 {
25 char *cs_prompt = cleanStringToCString(prompt);
26
27 //Get the answer and add to history if not empty
28 if(cs_answer){
29 free(cs_answer);
30 cs_answer = (char *)NULL;
31 }
32 cs_answer = readline(cs_prompt);
33 free(cs_prompt);
34 if(cs_answer && *cs_answer && history){
35 add_history(cs_answer);
36 }
37
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);
48 }
49 }
50
51 void cleanSetReadLineName(CleanString name){
52 rl_readline_name = cleanStringToCString(name);
53 }
54
55 void cleanUsingHistory(){
56 using_history();
57 }
58
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;
63 }
64
65 void cleanSetState(int offset, int num, int flags){
66 ;
67 }
68
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;
72
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);
77
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);
82 }
83
84 void cleanAddHistory(CleanString entry){
85 char *cs_entry = cleanStringToCString(entry);
86 add_history(cs_entry);
87 free(cs_entry);
88 }
89
90 void cleanClearHistory(){
91 clear_history();
92 }
93
94 int cleanHistorySearch(CleanString s, int dir){
95 char *cs_s = cleanStringToCString(s);
96 int ret = history_search(cs_s, dir);
97 free(cs_s);
98 return ret;
99 }
100
101 int cleanHistorySearchPrefix(CleanString s, int dir){
102 char *cs_s = cleanStringToCString(s);
103 int ret = history_search_prefix(cs_s, dir);
104 free(cs_s);
105 return ret;
106 }
107
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);
111 free(cs_s);
112 return ret;
113 }
114
115 int cleanReadHistory(CleanString path)
116 {
117 char *cs_path = cleanStringToCString(path);
118 int errno = read_history(cs_path);
119 free(cs_path);
120 return errno;
121 }
122
123 int cleanReadHistoryRange(CleanString path, int from, int to)
124 {
125 char *cs_path = cleanStringToCString(path);
126 int errno = read_history_range(cs_path, from, to);
127 free(cs_path);
128 return errno;
129 }
130
131 int cleanWriteHistory(CleanString path)
132 {
133 char *cs_path = cleanStringToCString(path);
134 int errno = write_history(cs_path);
135 free(cs_path);
136 return errno;
137 }
138
139 int cleanAppendHistory(int n, CleanString path)
140 {
141 char *cs_path = cleanStringToCString(path);
142 int errno = append_history(n, cs_path);
143 free(cs_path);
144 return errno;
145 }
146
147 int cleanHistoryTruncateFile(CleanString path, int nlines)
148 {
149 char *cs_path = cleanStringToCString(path);
150 int errno = history_truncate_file(cs_path, nlines);
151 free(cs_path);
152 return errno;
153 }
154