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