1797
|
1 /* |
|
2 |
|
3 Copyright (C) 1996 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <cstring> |
|
28 |
|
29 #include <strstream.h> |
|
30 |
2444
|
31 #ifdef HAVE_FCNTL_H |
1797
|
32 #include <fcntl.h> |
2444
|
33 #endif |
1797
|
34 |
|
35 #ifdef HAVE_UNISTD_H |
2443
|
36 #ifdef HAVE_SYS_TYPES_H |
1797
|
37 #include <sys/types.h> |
2443
|
38 #endif |
1797
|
39 #include <unistd.h> |
|
40 #endif |
|
41 |
|
42 #include <readline/history.h> |
|
43 |
|
44 #include "file-ops.h" |
|
45 #include "lo-error.h" |
|
46 #include "cmd-hist.h" |
|
47 |
|
48 bool command_history::initialized = false; |
|
49 |
|
50 command_history::command_history (const string& f, int n) |
|
51 { |
|
52 if (initialized) |
|
53 error ("only one history object can be active at once"); |
|
54 else |
|
55 { |
|
56 ignoring_additions = false; |
|
57 |
|
58 lines_in_file = 0; |
|
59 lines_this_session = 0; |
|
60 |
|
61 xsize = -1; |
|
62 |
|
63 if (! f.empty ()) |
|
64 { |
|
65 xfile = f; |
|
66 |
2470
|
67 ::read_history (f.c_str ()); |
1797
|
68 |
|
69 lines_in_file = where (); |
|
70 |
2470
|
71 ::using_history (); |
1797
|
72 } |
|
73 |
|
74 if (n > 0) |
|
75 xsize = n; |
|
76 |
|
77 initialized = true; |
|
78 } |
|
79 } |
|
80 |
|
81 void |
|
82 command_history::set_file (const string& f) |
|
83 { |
|
84 xfile = f; |
|
85 } |
|
86 |
|
87 string |
|
88 command_history::file (void) |
|
89 { |
|
90 return xfile; |
|
91 } |
|
92 |
|
93 void |
|
94 command_history::set_size (int n) |
|
95 { |
|
96 xsize = n; |
|
97 } |
|
98 |
|
99 int |
|
100 command_history::size (void) |
|
101 { |
|
102 return xsize; |
|
103 } |
|
104 |
|
105 void |
1888
|
106 command_history::ignore_entries (bool flag) |
1797
|
107 { |
|
108 ignoring_additions = flag; |
|
109 } |
|
110 |
|
111 bool |
|
112 command_history::ignoring_entries (void) |
|
113 { |
|
114 return ignoring_additions; |
|
115 } |
|
116 |
|
117 void |
|
118 command_history::add (const string& s) |
|
119 { |
|
120 if (! ignoring_entries ()) |
|
121 { |
2512
|
122 if (s.empty () |
|
123 || (s.length () == 1 && (s[0] == '\r' || s[0] == '\n'))) |
|
124 return; |
|
125 |
2470
|
126 ::add_history (s.c_str ()); |
1797
|
127 lines_this_session++; |
|
128 } |
|
129 } |
|
130 |
|
131 void |
|
132 command_history::remove (int n) |
|
133 { |
2470
|
134 HIST_ENTRY *discard = ::remove_history (n); |
1797
|
135 |
|
136 if (discard) |
|
137 { |
|
138 if (discard->line) |
2470
|
139 ::free (discard->line); |
1797
|
140 |
2470
|
141 ::free (discard); |
1797
|
142 } |
|
143 } |
|
144 |
|
145 int |
|
146 command_history::where (void) |
|
147 { |
2470
|
148 return ::where_history (); |
1797
|
149 } |
|
150 |
|
151 int |
|
152 command_history::base (void) |
|
153 { |
2470
|
154 return ::history_base; |
1797
|
155 } |
|
156 |
|
157 int |
|
158 command_history::current_number (void) |
|
159 { |
|
160 return (xsize > 0) ? base () + where () : -1; |
|
161 } |
|
162 |
|
163 void |
|
164 command_history::stifle (int n) |
|
165 { |
2470
|
166 ::stifle_history (n); |
1797
|
167 } |
|
168 |
|
169 int |
|
170 command_history::unstifle (void) |
|
171 { |
2470
|
172 return ::unstifle_history (); |
1797
|
173 } |
|
174 |
|
175 int |
|
176 command_history::is_stifled (void) |
|
177 { |
2470
|
178 return ::history_is_stifled (); |
1797
|
179 } |
|
180 |
|
181 void |
|
182 command_history::read (const string& f_arg) |
|
183 { |
|
184 string f = f_arg; |
|
185 |
|
186 if (f.empty ()) |
|
187 f = xfile; |
|
188 |
|
189 if (! f.empty ()) |
|
190 { |
2470
|
191 int status = ::read_history (f.c_str ()); |
1797
|
192 |
|
193 if (status != 0) |
|
194 error (status); |
|
195 else |
2470
|
196 ::using_history (); |
1797
|
197 } |
|
198 else |
|
199 error ("command_history::read: missing file name"); |
|
200 } |
|
201 |
|
202 void |
|
203 command_history::read_range (const string& f_arg, int from, int to) |
|
204 { |
|
205 if (from < 0) |
|
206 from = lines_in_file; |
|
207 |
|
208 string f = f_arg; |
|
209 |
|
210 if (f.empty ()) |
|
211 f = xfile; |
|
212 |
|
213 if (! f.empty ()) |
|
214 { |
2470
|
215 int status = ::read_history_range (f.c_str (), from, to); |
1797
|
216 |
|
217 if (status != 0) |
|
218 error (status); |
|
219 else |
|
220 { |
2470
|
221 ::using_history (); |
1797
|
222 lines_in_file = where (); |
|
223 } |
|
224 } |
|
225 else |
|
226 error ("command_history::read_range: missing file name"); |
|
227 } |
|
228 |
|
229 void |
|
230 command_history::write (const string& f_arg) |
|
231 { |
|
232 string f = f_arg; |
|
233 |
|
234 if (f.empty ()) |
|
235 f = xfile; |
|
236 |
|
237 if (! f.empty ()) |
|
238 { |
2470
|
239 int status = ::write_history (f.c_str ()); |
1797
|
240 |
|
241 if (status != 0) |
|
242 error (status); |
|
243 } |
|
244 else |
|
245 error ("command_history::write: missing file name"); |
|
246 } |
|
247 |
|
248 void |
|
249 command_history::append (const string& f_arg) |
|
250 { |
|
251 if (lines_this_session) |
|
252 { |
|
253 if (lines_this_session < where ()) |
|
254 { |
|
255 // Create file if it doesn't already exist. |
|
256 |
|
257 string f = f_arg; |
|
258 |
|
259 if (f.empty ()) |
|
260 f = xfile; |
|
261 |
|
262 if (! f.empty ()) |
|
263 { |
|
264 file_stat fs (f); |
|
265 |
|
266 if (! fs) |
|
267 { |
|
268 int tem; |
|
269 |
|
270 tem = open (f.c_str (), O_CREAT, 0666); |
|
271 close (tem); |
|
272 } |
|
273 |
2470
|
274 int status = ::append_history (lines_this_session, f.c_str ()); |
1797
|
275 |
|
276 if (status != 0) |
|
277 error (status); |
|
278 else |
|
279 lines_in_file += lines_this_session; |
|
280 |
|
281 lines_this_session = 0; |
|
282 } |
|
283 else |
|
284 error ("comman_history::append: missing file name"); |
|
285 } |
|
286 } |
|
287 } |
|
288 |
|
289 void |
|
290 command_history::truncate_file (const string& f_arg, int n) |
|
291 { |
|
292 string f = f_arg; |
|
293 |
|
294 if (f.empty ()) |
|
295 f = xfile; |
|
296 |
|
297 if (! f.empty ()) |
2470
|
298 ::history_truncate_file (f.c_str (), n); |
1797
|
299 else |
|
300 error ("command_history::truncate_file: missing file name"); |
|
301 } |
|
302 |
|
303 string_vector |
|
304 command_history::list (int limit, int number_lines) |
|
305 { |
|
306 string_vector retval; |
|
307 |
|
308 if (limit) |
|
309 { |
2470
|
310 HIST_ENTRY **hlist = ::history_list (); |
1797
|
311 |
|
312 if (hlist) |
|
313 { |
|
314 int end = 0; |
|
315 while (hlist[end]) |
|
316 end++; |
|
317 |
|
318 int beg = (limit < 0 || end < limit) ? 0 : (end - limit); |
|
319 |
|
320 retval.resize (end - beg); |
|
321 |
|
322 int k = 0; |
|
323 for (int i = beg; i < end; i++) |
|
324 { |
|
325 ostrstream output_buf; |
|
326 |
|
327 if (number_lines) |
2470
|
328 output_buf.form ("%5d%c", i + ::history_base, |
1797
|
329 hlist[i]->data ? '*' : ' '); |
|
330 |
|
331 output_buf << hlist[i]->line << ends; |
|
332 |
|
333 const char *tmp = output_buf.str (); |
|
334 |
|
335 retval[k++] = tmp; |
|
336 |
|
337 delete [] tmp; |
|
338 } |
|
339 } |
|
340 } |
|
341 |
|
342 return retval; |
|
343 } |
|
344 |
|
345 string |
|
346 command_history::get_entry (int n) |
|
347 { |
|
348 string retval; |
|
349 |
2470
|
350 HIST_ENTRY *entry = ::history_get (::history_base + n); |
1797
|
351 |
|
352 if (entry && entry->line) |
|
353 retval = entry->line; |
|
354 |
|
355 return retval; |
|
356 } |
|
357 |
|
358 void |
|
359 command_history::replace_entry (int which, const string& line) |
|
360 { |
2470
|
361 HIST_ENTRY *discard = ::replace_history_entry (which, line.c_str (), 0); |
1797
|
362 |
|
363 if (discard) |
|
364 { |
|
365 if (discard->line) |
2470
|
366 ::free (discard->line); |
1797
|
367 |
2470
|
368 ::free (discard); |
1797
|
369 } |
|
370 } |
|
371 |
|
372 void |
|
373 command_history::clean_up_and_save (const string& f_arg, int n) |
|
374 { |
|
375 string f = f_arg; |
|
376 |
|
377 if (f.empty ()) |
|
378 f = xfile; |
|
379 |
|
380 if (! f.empty ()) |
|
381 { |
|
382 if (n < 0) |
|
383 n = xsize; |
|
384 |
|
385 stifle (n); |
|
386 |
2470
|
387 ::write_history (f.c_str ()); |
1797
|
388 } |
|
389 else |
|
390 error ("command_history::clean_up_and_save: missing file name"); |
|
391 } |
|
392 |
|
393 void |
2470
|
394 command_history::error (int err_num) |
1797
|
395 { |
2470
|
396 (*current_liboctave_error_handler) ("%s", strerror (err_num)); |
1797
|
397 } |
|
398 |
|
399 void |
|
400 command_history::error (const string& s) |
|
401 { |
|
402 (*current_liboctave_error_handler) ("%s", s.c_str ()); |
|
403 } |
|
404 |
|
405 /* |
|
406 ;;; Local Variables: *** |
|
407 ;;; mode: C++ *** |
|
408 ;;; End: *** |
|
409 */ |