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