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 |
2658
|
182 command_history::read (bool must_exist) |
|
183 { |
|
184 read (xfile, must_exist); |
|
185 } |
|
186 |
|
187 void |
|
188 command_history::read (const string& f_arg, bool must_exist) |
1797
|
189 { |
|
190 string f = f_arg; |
|
191 |
|
192 if (f.empty ()) |
|
193 f = xfile; |
|
194 |
|
195 if (! f.empty ()) |
|
196 { |
2470
|
197 int status = ::read_history (f.c_str ()); |
1797
|
198 |
2658
|
199 if (status != 0 && must_exist) |
1797
|
200 error (status); |
|
201 else |
2658
|
202 { |
|
203 lines_in_file = where (); |
|
204 |
|
205 ::using_history (); |
|
206 } |
1797
|
207 } |
|
208 else |
|
209 error ("command_history::read: missing file name"); |
|
210 } |
|
211 |
2658
|
212 command_history::read_range (int from, int to, bool must_exist) |
|
213 { |
|
214 read_range (xfile, from, to, must_exist); |
|
215 } |
|
216 |
1797
|
217 void |
2658
|
218 command_history::read_range (const string& f_arg, int from, int to, |
|
219 bool must_exist) |
1797
|
220 { |
|
221 if (from < 0) |
|
222 from = lines_in_file; |
|
223 |
|
224 string f = f_arg; |
|
225 |
|
226 if (f.empty ()) |
|
227 f = xfile; |
|
228 |
|
229 if (! f.empty ()) |
|
230 { |
2470
|
231 int status = ::read_history_range (f.c_str (), from, to); |
1797
|
232 |
2658
|
233 if (status != 0 && must_exist) |
1797
|
234 error (status); |
|
235 else |
|
236 { |
2658
|
237 lines_in_file = where (); |
|
238 |
2470
|
239 ::using_history (); |
1797
|
240 } |
|
241 } |
|
242 else |
|
243 error ("command_history::read_range: missing file name"); |
|
244 } |
|
245 |
|
246 void |
|
247 command_history::write (const string& f_arg) |
|
248 { |
|
249 string f = f_arg; |
|
250 |
|
251 if (f.empty ()) |
|
252 f = xfile; |
|
253 |
|
254 if (! f.empty ()) |
|
255 { |
2470
|
256 int status = ::write_history (f.c_str ()); |
1797
|
257 |
|
258 if (status != 0) |
|
259 error (status); |
|
260 } |
|
261 else |
|
262 error ("command_history::write: missing file name"); |
|
263 } |
|
264 |
|
265 void |
|
266 command_history::append (const string& f_arg) |
|
267 { |
|
268 if (lines_this_session) |
|
269 { |
|
270 if (lines_this_session < where ()) |
|
271 { |
|
272 // Create file if it doesn't already exist. |
|
273 |
|
274 string f = f_arg; |
|
275 |
|
276 if (f.empty ()) |
|
277 f = xfile; |
|
278 |
|
279 if (! f.empty ()) |
|
280 { |
|
281 file_stat fs (f); |
|
282 |
|
283 if (! fs) |
|
284 { |
|
285 int tem; |
|
286 |
|
287 tem = open (f.c_str (), O_CREAT, 0666); |
|
288 close (tem); |
|
289 } |
|
290 |
2470
|
291 int status = ::append_history (lines_this_session, f.c_str ()); |
1797
|
292 |
|
293 if (status != 0) |
|
294 error (status); |
|
295 else |
|
296 lines_in_file += lines_this_session; |
|
297 |
|
298 lines_this_session = 0; |
|
299 } |
|
300 else |
|
301 error ("comman_history::append: missing file name"); |
|
302 } |
|
303 } |
|
304 } |
|
305 |
|
306 void |
|
307 command_history::truncate_file (const string& f_arg, int n) |
|
308 { |
|
309 string f = f_arg; |
|
310 |
|
311 if (f.empty ()) |
|
312 f = xfile; |
|
313 |
|
314 if (! f.empty ()) |
2470
|
315 ::history_truncate_file (f.c_str (), n); |
1797
|
316 else |
|
317 error ("command_history::truncate_file: missing file name"); |
|
318 } |
|
319 |
|
320 string_vector |
|
321 command_history::list (int limit, int number_lines) |
|
322 { |
|
323 string_vector retval; |
|
324 |
|
325 if (limit) |
|
326 { |
2470
|
327 HIST_ENTRY **hlist = ::history_list (); |
1797
|
328 |
|
329 if (hlist) |
|
330 { |
|
331 int end = 0; |
|
332 while (hlist[end]) |
|
333 end++; |
|
334 |
|
335 int beg = (limit < 0 || end < limit) ? 0 : (end - limit); |
|
336 |
|
337 retval.resize (end - beg); |
|
338 |
|
339 int k = 0; |
|
340 for (int i = beg; i < end; i++) |
|
341 { |
|
342 ostrstream output_buf; |
|
343 |
|
344 if (number_lines) |
2470
|
345 output_buf.form ("%5d%c", i + ::history_base, |
1797
|
346 hlist[i]->data ? '*' : ' '); |
|
347 |
|
348 output_buf << hlist[i]->line << ends; |
|
349 |
|
350 const char *tmp = output_buf.str (); |
|
351 |
|
352 retval[k++] = tmp; |
|
353 |
|
354 delete [] tmp; |
|
355 } |
|
356 } |
|
357 } |
|
358 |
|
359 return retval; |
|
360 } |
|
361 |
|
362 string |
|
363 command_history::get_entry (int n) |
|
364 { |
|
365 string retval; |
|
366 |
2470
|
367 HIST_ENTRY *entry = ::history_get (::history_base + n); |
1797
|
368 |
|
369 if (entry && entry->line) |
|
370 retval = entry->line; |
|
371 |
|
372 return retval; |
|
373 } |
|
374 |
|
375 void |
|
376 command_history::replace_entry (int which, const string& line) |
|
377 { |
2470
|
378 HIST_ENTRY *discard = ::replace_history_entry (which, line.c_str (), 0); |
1797
|
379 |
|
380 if (discard) |
|
381 { |
|
382 if (discard->line) |
2470
|
383 ::free (discard->line); |
1797
|
384 |
2470
|
385 ::free (discard); |
1797
|
386 } |
|
387 } |
|
388 |
|
389 void |
|
390 command_history::clean_up_and_save (const string& f_arg, int n) |
|
391 { |
|
392 string f = f_arg; |
|
393 |
|
394 if (f.empty ()) |
|
395 f = xfile; |
|
396 |
|
397 if (! f.empty ()) |
|
398 { |
|
399 if (n < 0) |
|
400 n = xsize; |
|
401 |
|
402 stifle (n); |
|
403 |
2470
|
404 ::write_history (f.c_str ()); |
1797
|
405 } |
|
406 else |
|
407 error ("command_history::clean_up_and_save: missing file name"); |
|
408 } |
|
409 |
|
410 void |
2470
|
411 command_history::error (int err_num) |
1797
|
412 { |
2470
|
413 (*current_liboctave_error_handler) ("%s", strerror (err_num)); |
1797
|
414 } |
|
415 |
|
416 void |
|
417 command_history::error (const string& s) |
|
418 { |
|
419 (*current_liboctave_error_handler) ("%s", s.c_str ()); |
|
420 } |
|
421 |
|
422 /* |
|
423 ;;; Local Variables: *** |
|
424 ;;; mode: C++ *** |
|
425 ;;; End: *** |
|
426 */ |