1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | |
22 | |
23 | #ifdef HAVE_CONFIG_H1 |
24 | #include <config.h> |
25 | #endif |
26 | |
27 | #include "oct-rl-hist.h" |
28 | |
29 | #if defined (USE_READLINE1) |
30 | |
31 | #include <stdio.h> |
32 | #include <stdlib.h> |
33 | #include <string.h> |
34 | |
35 | #include <readline/history.h> |
36 | |
37 | |
38 | |
39 | |
40 | |
41 | |
42 | static int |
43 | check_history_control (const char *line, int history_control) |
44 | { |
45 | HIST_ENTRY *temp; |
46 | int r; |
47 | |
48 | if (history_control == 0) |
49 | return 1; |
50 | |
51 | |
52 | if ((history_control & HC_IGNSPACE) && *line == ' ') |
53 | return 0; |
54 | |
55 | |
56 | if (history_control & HC_IGNDUPS) |
57 | { |
58 | using_history (); |
59 | temp = previous_history (); |
60 | |
61 | r = (temp == 0 || strcmp (temp->line, line)); |
62 | |
63 | using_history (); |
64 | |
65 | if (r == 0) |
66 | return r; |
67 | } |
68 | |
69 | return 1; |
70 | } |
71 | |
72 | |
73 | |
74 | |
75 | static void |
76 | hc_erasedups (const char *line) |
77 | { |
78 | HIST_ENTRY *temp; |
79 | int r; |
80 | |
81 | using_history (); |
82 | while ((temp = previous_history ())) |
83 | { |
84 | if (! strcmp (temp->line, line)) |
85 | { |
86 | r = where_history (); |
87 | remove_history (r); |
88 | } |
89 | } |
90 | using_history (); |
91 | } |
92 | |
93 | |
94 | |
95 | |
96 | int |
97 | octave_add_history (const char *line, int history_control) |
98 | { |
99 | if (check_history_control (line, history_control)) |
100 | { |
101 | |
102 | |
103 | |
104 | if (history_control & HC_ERASEDUPS) |
105 | hc_erasedups (line); |
106 | |
107 | add_history (line); |
108 | |
109 | return 1; |
110 | } |
111 | |
112 | return 0; |
113 | } |
114 | |
115 | int |
116 | octave_where_history (void) |
117 | { |
118 | return where_history (); |
119 | } |
120 | |
121 | int |
122 | octave_history_length (void) |
123 | { |
124 | return history_length; |
125 | } |
126 | |
127 | int |
128 | octave_max_input_history (void) |
129 | { |
130 | return max_input_history; |
131 | } |
132 | |
133 | int |
134 | octave_history_base (void) |
135 | { |
136 | return history_base; |
137 | } |
138 | |
139 | void |
140 | octave_stifle_history (int n) |
141 | { |
142 | stifle_history (n); |
143 | } |
144 | |
145 | int |
146 | octave_unstifle_history (void) |
147 | { |
148 | return unstifle_history (); |
149 | } |
150 | |
151 | int |
152 | octave_history_is_stifled (void) |
153 | { |
154 | return history_is_stifled (); |
155 | } |
156 | |
157 | int |
158 | octave_history_set_pos (int n) |
159 | { |
160 | return history_set_pos (n); |
161 | } |
162 | |
163 | int |
164 | octave_read_history (const char *f) |
165 | { |
166 | return read_history (f); |
167 | } |
168 | |
169 | void |
170 | octave_using_history (void) |
171 | { |
172 | using_history (); |
173 | } |
174 | |
175 | int |
176 | octave_read_history_range (const char *f, int b, int e) |
177 | { |
178 | return read_history_range (f, b, e); |
179 | } |
180 | |
181 | int |
182 | octave_write_history (const char *f) |
183 | { |
184 | return write_history (f); |
185 | } |
186 | |
187 | int |
188 | octave_append_history (int n, const char *f) |
189 | { |
190 | return append_history (n, f); |
191 | } |
192 | |
193 | int |
194 | octave_history_truncate_file (const char *f, int n) |
195 | { |
196 | return history_truncate_file (f, n); |
197 | } |
198 | |
199 | void |
200 | octave_remove_history (int n) |
201 | { |
202 | HIST_ENTRY *discard = remove_history (n); |
203 | |
204 | if (discard) |
205 | { |
206 | if (discard->line) |
207 | free (discard->line); |
208 | |
209 | free (discard); |
210 | } |
211 | } |
212 | |
213 | void |
214 | octave_clear_history (void) |
215 | { |
216 | clear_history (); |
217 | } |
218 | |
219 | char * |
220 | octave_history_goto_mark (int n) |
221 | { |
222 | HIST_ENTRY *h; |
223 | |
224 | char *retval = 0; |
225 | |
226 | if (history_set_pos (n)) |
227 | { |
228 | h = current_history (); |
229 | |
230 | if (h) |
231 | retval = h->line; |
232 | } |
233 | |
234 | return retval; |
235 | } |
236 | |
237 | char * |
238 | octave_history_get (int n) |
239 | { |
240 | char *retval = 0; |
241 | |
242 | HIST_ENTRY *h = history_get (n); |
243 | |
244 | if (h) |
245 | retval = h->line; |
246 | |
247 | return retval; |
248 | } |
249 | |
250 | char ** |
251 | octave_history_list (int limit, int number_lines) |
252 | { |
253 | static char **retval = 0; |
254 | |
255 | HIST_ENTRY **hlist = 0; |
256 | |
257 | if (retval) |
258 | { |
259 | char **p = retval; |
260 | |
261 | while (*p) |
262 | free (*p++); |
263 | |
264 | free (retval); |
265 | |
266 | retval = 0; |
267 | } |
268 | |
269 | hlist = history_list (); |
270 | |
271 | if (hlist) |
272 | { |
273 | int i, k; |
274 | |
275 | int beg = 0; |
276 | int end = 0; |
277 | while (hlist[end]) |
278 | end++; |
279 | |
280 | beg = (limit < 0 || end < limit) ? 0 : (end - limit); |
281 | |
282 | retval = malloc ((end - beg + 1) * sizeof (char **)); |
| Result of 'malloc' is converted to a pointer of type 'char *', which is incompatible with sizeof operand type 'char **' |
283 | |
284 | k = 0; |
285 | for (i = beg; i < end; i++) |
286 | { |
287 | char *line = hlist[i]->line; |
288 | int len = line ? strlen (line) : 0; |
289 | char *tmp = malloc (len + 64); |
290 | |
291 | if (number_lines) |
292 | sprintf (tmp, "%5d %s", i + history_base, |
293 | line ? line : ""); |
294 | else |
295 | strcpy (tmp, line ? line : ""); |
296 | |
297 | retval[k++] = tmp; |
298 | } |
299 | |
300 | retval[k] = 0; |
301 | } |
302 | |
303 | return retval; |
304 | } |
305 | |
306 | void |
307 | octave_replace_history_entry (int which, const char *line) |
308 | { |
309 | HIST_ENTRY *discard = replace_history_entry (which, line, 0); |
310 | |
311 | if (discard) |
312 | { |
313 | if (discard->line) |
314 | free (discard->line); |
315 | |
316 | free (discard); |
317 | } |
318 | } |
319 | |
320 | #endif |