comparison src/oct-hist.cc @ 529:7ea224e713cd

[project @ 1994-07-20 18:54:27 by jwe]
author jwe
date Wed, 20 Jul 1994 19:19:08 +0000
parents 3c23b8ea9099
children bc813f5eb025
comparison
equal deleted inserted replaced
528:e1e6e33e26f8 529:7ea224e713cd
47 #include "statdefs.h" 47 #include "statdefs.h"
48 #include "utils.h" 48 #include "utils.h"
49 #include "error.h" 49 #include "error.h"
50 #include "input.h" 50 #include "input.h"
51 #include "octave.h" 51 #include "octave.h"
52 #include "oct-obj.h"
52 #include "user-prefs.h" 53 #include "user-prefs.h"
53 #include "unwind-prot.h" 54 #include "unwind-prot.h"
54 #include "octave-hist.h" 55 #include "octave-hist.h"
55 #include "sighandlers.h" 56 #include "sighandlers.h"
57 #include "defun.h"
56 58
57 extern "C" 59 extern "C"
58 { 60 {
59 #include <readline/history.h> 61 #include <readline/history.h>
60 } 62 }
84 static int 86 static int
85 default_history_size (void) 87 default_history_size (void)
86 { 88 {
87 int size = 1024; 89 int size = 1024;
88 char *env_size = getenv ("OCTAVE_HISTSIZE"); 90 char *env_size = getenv ("OCTAVE_HISTSIZE");
89 if (env_size != (char *) NULL) 91 if (env_size)
90 { 92 {
91 int val; 93 int val;
92 if (sscanf (env_size, "%d", &val) == 1) 94 if (sscanf (env_size, "%d", &val) == 1)
93 size = val > 0 ? val : 0; 95 size = val > 0 ? val : 0;
94 } 96 }
96 } 98 }
97 99
98 static char * 100 static char *
99 default_history_file (void) 101 default_history_file (void)
100 { 102 {
101 char *file = (char *) NULL;; 103 char *file = 0;
102 104
103 char *env_file = getenv ("OCTAVE_HISTFILE"); 105 char *env_file = getenv ("OCTAVE_HISTFILE");
104 if (env_file != (char *) NULL) 106 if (env_file)
105 { 107 {
106 fstream f (env_file, (ios::in | ios::out)); 108 fstream f (env_file, (ios::in | ios::out));
107 if (f != 0) 109 if (f)
108 { 110 {
109 file = strsave (env_file); 111 file = strsave (env_file);
110 f.close (); 112 f.close ();
111 } 113 }
112 } 114 }
113 115
114 if (file == (char *) NULL) 116 if (! file && home_directory)
115 { 117 file = strconcat (home_directory, "/.octave_hist");
116 if (home_directory != NULL)
117 file = strconcat (home_directory, "/.octave_hist");
118 }
119 118
120 return file; 119 return file;
121 } 120 }
122 121
123 /* 122 /*
258 257
259 hlist = history_list (); 258 hlist = history_list ();
260 259
261 if (hlist) 260 if (hlist)
262 { 261 {
263 for (int i = 0; hlist[i] != (HIST_ENTRY *) NULL; i++) 262 for (int i = 0; hlist[i]; i++)
264 ; // Do nothing. 263 ; // Do nothing.
265 264
266 if (limit < 0) 265 if (limit < 0)
267 limit = -limit; 266 limit = -limit;
268 267
319 } 318 }
320 319
321 if (! lindex) 320 if (! lindex)
322 { 321 {
323 delete [] line; 322 delete [] line;
324 return (char *) NULL; 323 return 0;
325 } 324 }
326 325
327 if (lindex + 2 >= line_len) 326 if (lindex + 2 >= line_len)
328 { 327 {
329 char *tmp_line = new char [lindex+3]; 328 char *tmp_line = new char [lindex+3];
352 * your heart's content. 351 * your heart's content.
353 */ 352 */
354 static void 353 static void
355 edit_history_repl_hist (char *command) 354 edit_history_repl_hist (char *command)
356 { 355 {
357 if (command == (char *) NULL || *command == '\0') 356 if (! command || ! *command)
358 return; 357 return;
359 358
360 HIST_ENTRY **hlist = history_list (); 359 HIST_ENTRY **hlist = history_list ();
361 360
362 if (hlist == (HIST_ENTRY **) NULL) 361 if (! hlist)
363 return; 362 return;
364 363
365 for (int i = 0; hlist[i]; i++) 364 for (int i = 0; hlist[i]; i++)
366 ; // Count 'em. 365 ; // Count 'em.
367 i--; 366 i--;
369 /* History_get () takes a parameter that should be 368 /* History_get () takes a parameter that should be
370 offset by history_base. */ 369 offset by history_base. */
371 370
372 // Don't free this. 371 // Don't free this.
373 HIST_ENTRY *histent = history_get (history_base + i); 372 HIST_ENTRY *histent = history_get (history_base + i);
374 if (histent == (HIST_ENTRY *) NULL) 373 if (! histent)
375 return; 374 return;
376 375
377 char *data = (char *) NULL; 376 char *data = 0;
378 if (histent->data != (char *) NULL) 377 if (histent->data)
379 { 378 {
380 int len = strlen (histent->data); 379 int len = strlen (histent->data);
381 data = (char *) malloc (len); 380 data = (char *) malloc (len);
382 strcpy (data, histent->data); 381 strcpy (data, histent->data);
383 } 382 }
385 int n = strlen (command); 384 int n = strlen (command);
386 385
387 if (command[n - 1] == '\n') 386 if (command[n - 1] == '\n')
388 command[n - 1] = '\0'; 387 command[n - 1] = '\0';
389 388
390 if (command != (char *) NULL && *command != '\0') 389 if (command && *command)
391 { 390 {
392 HIST_ENTRY *discard = replace_history_entry (i, command, data); 391 HIST_ENTRY *discard = replace_history_entry (i, command, data);
393 if (discard != (HIST_ENTRY *) NULL) 392 if (discard)
394 { 393 {
395 if (discard->line != (char *) NULL) 394 if (discard->line)
396 free (discard->line); 395 free (discard->line);
397 396
398 free ((char *) discard); 397 free ((char *) discard);
399 } 398 }
400 } 399 }
401 } 400 }
402 401
403 static void 402 static void
404 edit_history_add_hist (char *line) 403 edit_history_add_hist (char *line)
405 { 404 {
406 if (line != (char *) NULL) 405 if (line)
407 { 406 {
408 int len = strlen (line); 407 int len = strlen (line);
409 if (len > 0 && line[len-1] == '\n') 408 if (len > 0 && line[len-1] == '\n')
410 line[len-1] = '\0'; 409 line[len-1] = '\0';
411 410
423 422
424 hlist = history_list (); 423 hlist = history_list ();
425 424
426 int hist_count = 0; 425 int hist_count = 0;
427 426
428 while (hlist[hist_count++] != (HIST_ENTRY *) NULL) 427 while (hlist[hist_count++])
429 ; // Find the number of items in the history list. 428 ; // Find the number of items in the history list.
430 429
431 // The current command line is already part of the history list by the 430 // The current command line is already part of the history list by the
432 // time we get to this point. Delete it from the list. 431 // time we get to this point. Delete it from the list.
433 432
472 471
473 if (hist_beg < 0 || hist_end < 0 || hist_beg > hist_count 472 if (hist_beg < 0 || hist_end < 0 || hist_beg > hist_count
474 || hist_end > hist_count) 473 || hist_end > hist_count)
475 { 474 {
476 error ("%s: history specification out of range", warn_for); 475 error ("%s: history specification out of range", warn_for);
477 return (char *) NULL; 476 return 0;
478 } 477 }
479 478
480 if (usage_error) 479 if (usage_error)
481 { 480 {
482 usage ("%s [first] [last]", warn_for); 481 usage ("%s [first] [last]", warn_for);
483 return (char *) NULL; 482 return 0;
484 } 483 }
485 484
486 if (hist_end < hist_beg) 485 if (hist_end < hist_beg)
487 { 486 {
488 int t = hist_end; 487 int t = hist_end;
489 hist_end = hist_beg; 488 hist_end = hist_beg;
490 hist_beg = t; 489 hist_beg = t;
491 reverse = 1; 490 reverse = 1;
492 } 491 }
493 492
494 char *name = tmpnam ((char *) NULL); 493 char *name = tmpnam (0);
495 494
496 fstream file (name, ios::out); 495 fstream file (name, ios::out);
497 496
498 if (! file) 497 if (! file)
499 { 498 {
500 error ("%s: couldn't open temporary file `%s'", warn_for, name); 499 error ("%s: couldn't open temporary file `%s'", warn_for, name);
501 return (char *) NULL; 500 return 0;
502 } 501 }
503 502
504 if (reverse) 503 if (reverse)
505 { 504 {
506 for (int i = hist_end; i >= hist_beg; i--) 505 for (int i = hist_end; i >= hist_beg; i--)
520 void 519 void
521 do_edit_history (int argc, char **argv) 520 do_edit_history (int argc, char **argv)
522 { 521 {
523 char *name = mk_tmp_hist_file (argc, argv, 0, "edit_history"); 522 char *name = mk_tmp_hist_file (argc, argv, 0, "edit_history");
524 523
525 if (name == (char *) NULL) 524 if (! name)
526 return; 525 return;
527 526
528 // Call up our favorite editor on the file of commands. 527 // Call up our favorite editor on the file of commands.
529 528
530 ostrstream buf; 529 ostrstream buf;
543 542
544 fstream file (name, ios::in); 543 fstream file (name, ios::in);
545 544
546 char *line; 545 char *line;
547 int first = 1; 546 int first = 1;
548 while ((line = edit_history_readline (file)) != NULL) 547 while ((line = edit_history_readline (file)) != 0)
549 { 548 {
550 549
551 // Skip blank lines 550 // Skip blank lines
552 551
553 if (line[0] == '\n') 552 if (line[0] == '\n')
590 void 589 void
591 do_run_history (int argc, char **argv) 590 do_run_history (int argc, char **argv)
592 { 591 {
593 char *name = mk_tmp_hist_file (argc, argv, 1, "run_history"); 592 char *name = mk_tmp_hist_file (argc, argv, 1, "run_history");
594 593
595 if (name == (char *) NULL) 594 if (! name)
596 return; 595 return;
597 596
598 // Turn on command echo, so the output from this will make better sense. 597 // Turn on command echo, so the output from this will make better sense.
599 598
600 begin_unwind_frame ("do_run_history"); 599 begin_unwind_frame ("do_run_history");
623 if (octave_hist_size > 0) 622 if (octave_hist_size > 0)
624 return history_base + where_history (); 623 return history_base + where_history ();
625 else 624 else
626 return -1; 625 return -1;
627 626
627 }
628
629 DEFUN_TEXT ("edit_history", Fedit_history, Sedit_history, -1, 1,
630 "edit_history [first] [last]\n\
631 \n\
632 edit commands from the history list")
633 {
634 Octave_object retval;
635
636 DEFINE_ARGV("edit_history");
637
638 do_edit_history (argc, argv);
639
640 DELETE_ARGV;
641
642 return retval;
643 }
644
645 DEFUN_TEXT ("history", Fhistory, Shistory, -1, 1,
646 "history [N] [-w file] [-r file] [-q]\n\
647 \n\
648 display, save, or load command history")
649 {
650 Octave_object retval;
651
652 DEFINE_ARGV("history");
653
654 do_history (argc, argv);
655
656 DELETE_ARGV;
657
658 return retval;
659 }
660
661 DEFUN_TEXT ("run_history", Frun_history, Srun_history, -1, 1,
662 "run_history [first] [last]\n\
663 \n\
664 run commands from the history list")
665 {
666 Octave_object retval;
667
668 DEFINE_ARGV("run_history");
669
670 do_run_history (argc, argv);
671
672 DELETE_ARGV;
673
674 return retval;
628 } 675 }
629 676
630 /* 677 /*
631 ;;; Local Variables: *** 678 ;;; Local Variables: ***
632 ;;; mode: C++ *** 679 ;;; mode: C++ ***