changeset 1588:27f5ac98fc4a

[project @ 1995-10-31 06:04:47 by jwe]
author jwe
date Tue, 31 Oct 1995 06:04:47 +0000
parents dd087a402811
children 3b0b38ef2e2f
files src/help.cc src/input.cc src/input.h src/oct-hist.cc src/octave.cc src/parse.y src/pr-output.cc src/pt-base.cc src/pt-exp-base.cc src/pt-exp-base.h src/pt-misc.cc src/pt-misc.h src/user-prefs.cc src/user-prefs.h src/variables.cc
diffstat 15 files changed, 232 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/src/help.cc	Tue Oct 31 05:26:50 1995 +0000
+++ b/src/help.cc	Tue Oct 31 06:04:47 1995 +0000
@@ -52,6 +52,7 @@
 #include "tree-const.h"
 #include "tree-expr.h"
 #include "tree-expr.h"
+#include "unwind-prot.h"
 #include "user-prefs.h"
 #include "utils.h"
 #include "variables.h"
@@ -728,6 +729,11 @@
 {
   Octave_object retval;
 
+  begin_unwind_frame ("Ftype");
+
+  unwind_protect_ptr (user_pref.ps4);
+  user_pref.ps4 = "";
+
   DEFINE_ARGV("type");
 
   if (argc > 1)
@@ -859,6 +865,8 @@
 
   DELETE_ARGV;
 
+  run_unwind_frame ("Ftype");
+
   return retval;
 }
 
--- a/src/input.cc	Tue Oct 31 05:26:50 1995 +0000
+++ b/src/input.cc	Tue Oct 31 06:04:47 1995 +0000
@@ -140,10 +140,6 @@
 // Nonzero means we are using readline.
 int using_readline = 1;
 
-// Nonzero means commands are echoed as they are executed.
-// (--echo-commands; -x).
-int echo_input = 0;
-
 // Nonzero means this is an interactive shell.
 int interactive = 0;
 
@@ -441,7 +437,11 @@
 static void
 do_input_echo (const char *input_string)
 {
-  if (echo_input)
+  int do_echo = reading_script_file ?
+    (user_pref.echo_executing_commands & ECHO_SCRIPTS)
+      : (user_pref.echo_executing_commands & ECHO_CMD_LINE);
+
+  if (do_echo)
     {
       ostrstream buf;
 
@@ -1103,6 +1103,68 @@
   return retval;
 }
 
+DEFUN_TEXT("echo", Fecho, Secho, 10,
+  "echo [options]\n\
+\n\
+  echo [on|off]         -- enable or disable echoing of commands as\n\
+                           they are executed in script files\n\
+\n\
+  echo [on all|off all] -- enable or disable echoing of commands as they\n\
+                           are executed in script files and functions\n\
+\n\
+Without any arguments, toggle the current echo state.")
+{
+  Octave_object retval;
+
+  DEFINE_ARGV ("echo");
+
+  switch (argc)
+    {
+    case 1:
+      {
+	int echo_cmds = user_pref.echo_executing_commands;
+	if ((echo_cmds & ECHO_SCRIPTS) || (echo_cmds & ECHO_FUNCTIONS))
+	  bind_builtin_variable ("echo_executing_commands", ECHO_OFF);
+	else
+	  bind_builtin_variable ("echo_executing_commands", ECHO_SCRIPTS);
+      }
+      break;
+
+    case 2:
+      {
+	char *arg = argv[1];
+	if (strcmp (arg, "on") == 0)
+	  bind_builtin_variable ("echo_executing_commands", ECHO_SCRIPTS);
+	else if (strcmp (arg, "on") == 0)
+	  bind_builtin_variable ("echo_executing_commands", ECHO_OFF);
+	else
+	  print_usage ("echo");
+      }
+      break;
+
+    case 3:
+      {
+	char *arg = argv[1];
+	if (strcmp (arg, "on") == 0 && strcmp (argv[2], "all") == 0)
+	  bind_builtin_variable ("echo_executing_commands",
+				 (ECHO_SCRIPTS | ECHO_FUNCTIONS));
+	else if (strcmp (arg, "off") == 0 && strcmp (argv[2], "all") == 0)
+	  bind_builtin_variable ("echo_executing_commands", ECHO_OFF);
+	else
+	  print_usage ("echo");
+      }
+      break;
+
+    default:
+      print_usage ("echo");
+      break;
+    }
+
+  DELETE_ARGV;
+
+  return retval;
+}
+
 /*
 ;;; Local Variables: ***
 ;;; mode: C++ ***
--- a/src/input.h	Tue Oct 31 05:26:50 1995 +0000
+++ b/src/input.h	Tue Oct 31 06:04:47 1995 +0000
@@ -54,9 +54,6 @@
 // Nonzero means we are using readline.
 extern int using_readline;
 
-// Nonzero means commands are echoed as they are executed (-x).
-extern int echo_input;
-
 // Nonzero means this is an interactive shell.
 extern int interactive;
 
--- a/src/oct-hist.cc	Tue Oct 31 05:26:50 1995 +0000
+++ b/src/oct-hist.cc	Tue Oct 31 06:04:47 1995 +0000
@@ -578,9 +578,9 @@
   // sense.
 
   begin_unwind_frame ("do_edit_history");
-  unwind_protect_int (echo_input);
+  unwind_protect_int (user_pref.echo_executing_commands);
   unwind_protect_int (input_from_tmp_history_file);
-  echo_input = 1;
+  user_pref.echo_executing_commands = ECHO_CMD_LINE;
   input_from_tmp_history_file = 1;
 
   parse_and_execute (name, 1);
@@ -607,9 +607,9 @@
   // sense.
 
   begin_unwind_frame ("do_run_history");
-  unwind_protect_int (echo_input);
+  unwind_protect_int (user_pref.echo_executing_commands);
   unwind_protect_int (input_from_tmp_history_file);
-  echo_input = 1;
+  user_pref.echo_executing_commands = ECHO_CMD_LINE;
   input_from_tmp_history_file = 1;
 
   parse_and_execute (name, 1);
--- a/src/octave.cc	Tue Oct 31 05:26:50 1995 +0000
+++ b/src/octave.cc	Tue Oct 31 06:04:47 1995 +0000
@@ -323,12 +323,10 @@
 
   switch_to_buffer (new_buf);
 
-  unwind_protect_int (echo_input);
   unwind_protect_int (using_readline);
   unwind_protect_int (saving_history);
   unwind_protect_int (input_from_command_line_file);
 
-  echo_input = 0;
   using_readline = 0;
   saving_history = 0;
   input_from_command_line_file = 0;
@@ -367,11 +365,9 @@
     {
       unwind_protect_int (input_line_number);
       unwind_protect_int (current_input_column);
-      unwind_protect_int (echo_input);
 
       input_line_number = 0;
       current_input_column = 1;
-      echo_input = 0;
 
       if (verbose)
 	{
@@ -398,7 +394,10 @@
 {
   begin_unwind_frame ("execute_startup_files");
 
+  unwind_protect_int (user_pref.echo_executing_commands);
   unwind_protect_int (input_from_startup_file);
+
+  user_pref.echo_executing_commands = ECHO_OFF;
   input_from_startup_file = 1;
 
   int verbose = (verbose_flag && ! inhibit_startup_message);
@@ -547,6 +546,8 @@
 int
 main (int argc, char **argv)
 {
+  int echo_commands = ECHO_OFF;
+
   // The order of these calls is important, and initialize_globals
   // must come before the options are processed because some command
   // line options override defaults.
@@ -597,7 +598,7 @@
 	  break;
 
 	case 'x':
-	  echo_input = 1;
+	  echo_commands = (ECHO_SCRIPTS | ECHO_FUNCTIONS | ECHO_CMD_LINE);
 	  break;
 
 	case 'v':
@@ -649,6 +650,9 @@
   if (traditional)
     maximum_braindamage ();
 
+  bind_builtin_variable ("echo_executing_commands",
+			 (double) echo_commands);
+
   if (read_init_files)
     {
       saving_history = 0;
@@ -713,7 +717,11 @@
   if (!interactive && forced_interactive)
     {
       rl_blink_matching_paren = 0;
-      echo_input = 1;
+
+      // XXX FIXME XXX -- is this the right thing to do?
+
+      bind_builtin_variable ("echo_executing_commands",
+			     (double) ECHO_CMD_LINE);
     }
 
   if (! interactive)
--- a/src/parse.y	Tue Oct 31 05:26:50 1995 +0000
+++ b/src/parse.y	Tue Oct 31 06:04:47 1995 +0000
@@ -1048,11 +1048,15 @@
 
 func_def3	: param_list optsep opt_list fcn_end_or_eof
 		  {
+		    $3->mark_as_function_body ();
 		    tree_function *fcn = new tree_function ($3, curr_sym_tab);
 		    $$ = fcn->define_param_list ($1);
 		  }
 		| optsep opt_list fcn_end_or_eof
-		  { $$ = new tree_function ($2, curr_sym_tab); }
+		  {
+		    $2->mark_as_function_body ();
+		    $$ = new tree_function ($2, curr_sym_tab);
+		  }
 		;
 
 fcn_end_or_eof	: END
--- a/src/pr-output.cc	Tue Oct 31 05:26:50 1995 +0000
+++ b/src/pr-output.cc	Tue Oct 31 06:04:47 1995 +0000
@@ -1465,15 +1465,23 @@
 
       for (int i = 0; i < nstr; i++)
 	{
+	  char *row = chm.row_as_string (i);
+
 	  if (pr_as_read_syntax)
 	    {
-	      os << "\"" << chm.row_as_string (i) << "\"";
+	      char *tmp = undo_string_escapes (row);
+
+	      os << "\"" << tmp << "\"";
+
+	      delete [] tmp;
 
 	      if (i < nstr - 1)
 		os << "; ";
 	    }
 	  else
-	    os << chm.row_as_string (i) << "\n";
+	    os << row << "\n";
+
+	  delete [] row;
 	}
 
       if (pr_as_read_syntax && nstr > 1)
--- a/src/pt-base.cc	Tue Oct 31 05:26:50 1995 +0000
+++ b/src/pt-base.cc	Tue Oct 31 06:04:47 1995 +0000
@@ -34,6 +34,7 @@
 #include <iostream.h>
 
 #include "tree-base.h"
+#include "user-prefs.h"
 
 // Current indentation.
 int tree_print_code::curr_print_indent_level = 0;
@@ -63,7 +64,7 @@
  
   if (beginning_of_line)
     {
-      os.form ("%*s", curr_print_indent_level, "");
+      os.form ("%s%*s", user_pref.ps4, curr_print_indent_level, "");
       beginning_of_line = 0;
     }
 }
--- a/src/pt-exp-base.cc	Tue Oct 31 05:26:50 1995 +0000
+++ b/src/pt-exp-base.cc	Tue Oct 31 06:04:47 1995 +0000
@@ -2818,14 +2818,21 @@
 
   {
     bind_nargin_and_nargout (nargin, nargout);
-      
-    // Evaluate the commands that make up the function.  Always turn
-    // on printing for commands inside functions.   Maybe this should
-    // be toggled by a user-leval variable?
+
+    int echo_commands
+      = (user_pref.echo_executing_commands & ECHO_FUNCTIONS);
+
+    if (echo_commands)
+      print_code_function_header ();
+
+    // Evaluate the commands that make up the function.
 
     int pf = ! user_pref.silent_functions;
     tree_constant last_computed_value = cmd_list->eval (pf);
 
+    if (echo_commands)
+      print_code_function_trailer ();
+
     if (returning)
       returning = 0;
 
@@ -2888,6 +2895,29 @@
 {
   print_code_reset ();
 
+  print_code_function_header (os);
+
+  if (cmd_list)
+    {
+      increment_indent_level ();
+      cmd_list->print_code (os);
+    }
+
+  print_code_function_trailer (os);
+}
+
+void
+tree_function::print_code_function_header (void)
+{
+  ostrstream output_buf;
+  print_code_function_header (output_buf);
+  output_buf << ends;
+  maybe_page_output (output_buf);
+}
+
+void
+tree_function::print_code_function_header (ostream& os)
+{
   print_code_indent (os);
 
   os << "function ";
@@ -2928,12 +2958,21 @@
       os << "()";
       print_code_new_line (os);
     }
-
-  if (cmd_list)
-    {
-      increment_indent_level ();
-      cmd_list->print_code (os);
-    }
+}
+
+void
+tree_function::print_code_function_trailer (void)
+{
+  ostrstream output_buf;
+  print_code_function_trailer (output_buf);
+  output_buf << ends;
+  maybe_page_output (output_buf);
+}
+
+void
+tree_function::print_code_function_trailer (ostream& os)
+{
+  print_code_indent (os);
 
   os << "endfunction";
 
--- a/src/pt-exp-base.h	Tue Oct 31 05:26:50 1995 +0000
+++ b/src/pt-exp-base.h	Tue Oct 31 06:04:47 1995 +0000
@@ -945,6 +945,12 @@
   tree_va_return_list *vr_list;
   symbol_record *nargin_sr;
   symbol_record *nargout_sr;
+
+  void print_code_function_header (void);
+  void print_code_function_header (ostream& os);
+
+  void print_code_function_trailer (void);
+  void print_code_function_trailer (ostream& os);
 };
 
 #endif
--- a/src/pt-misc.cc	Tue Oct 31 05:26:50 1995 +0000
+++ b/src/pt-misc.cc	Tue Oct 31 06:04:47 1995 +0000
@@ -30,6 +30,7 @@
 #endif
 
 #include <iostream.h>
+#include <strstream.h>
 
 #ifdef HAVE_UNISTD_H
 #include <sys/types.h>
@@ -39,6 +40,7 @@
 #include "error.h"
 #include "oct-obj.h"
 #include "octave.h"
+#include "pager.h"
 #include "tree-base.h"
 #include "tree-cmd.h"
 #include "tree-const.h"
@@ -64,6 +66,19 @@
 }
 
 void
+tree_statement::maybe_echo_code (int in_function_body)
+{
+  if (in_function_body
+      && (user_pref.echo_executing_commands & ECHO_FUNCTIONS))
+    {
+      ostrstream output_buf;
+      print_code (output_buf);
+      output_buf << ends;
+      maybe_page_output (output_buf);
+    }
+}
+
+void
 tree_statement::print_code (ostream& os)
 {
   if (command)
@@ -84,8 +99,6 @@
 
       expression->print_code_new_line (os);
     }
-
-
 }
 
 tree_constant
@@ -111,6 +124,8 @@
 
       if (cmd || expr)
 	{
+	  elt->maybe_echo_code (function_body);
+
 	  if (cmd)
 	    cmd->eval ();
 	  else
@@ -157,6 +172,8 @@
 
 	  if (cmd || expr)
 	    {
+	      elt->maybe_echo_code (function_body);
+
 	      if (cmd)
 		cmd->eval ();
 	      else
@@ -530,6 +547,7 @@
     expr->print_code (os);
 
   print_code_new_line (os);
+
   increment_indent_level ();
 
   if (list)
--- a/src/pt-misc.h	Tue Oct 31 05:26:50 1995 +0000
+++ b/src/pt-misc.h	Tue Oct 31 06:04:47 1995 +0000
@@ -108,6 +108,8 @@
 	  : (expression ? expression->column () : -1);
     }
 
+  void maybe_echo_code (int);
+
   void print_code (ostream& os);
 
 private:
@@ -121,11 +123,15 @@
 {
 public:
   tree_statement_list (void)
-    : SLList<tree_statement *> (), tree_print_code () { }
+    : SLList<tree_statement *> (), tree_print_code ()
+      { function_body = 0; }
 
   tree_statement_list (tree_statement *s)
     : SLList<tree_statement *> (), tree_print_code ()
-      { append (s); }
+      {
+	function_body = 0;
+	append (s);
+      }
 
   ~tree_statement_list (void)
     {
@@ -136,11 +142,16 @@
 	}
     }
 
+  void mark_as_function_body (void) { function_body = 1; }
+
   tree_constant eval (int print);
 
   Octave_object eval (int print, int nargout);
 
   void print_code (ostream& os);
+
+private:
+  int function_body;
 };
 
 // Argument lists.  Used to hold the list of expressions that are the
--- a/src/user-prefs.cc	Tue Oct 31 05:26:50 1995 +0000
+++ b/src/user-prefs.cc	Tue Oct 31 06:04:47 1995 +0000
@@ -185,6 +185,24 @@
 }
 
 
+// Echo commands as they are executed?
+//
+//   1  ==>  echo commands read from script files
+//   2  ==>  echo commands from functions
+//   4  ==>  echo commands read from command line
+//
+// more than one state can be active at once.
+
+int
+echo_executing_commands (void)
+{
+  user_pref.echo_executing_commands =
+    check_preference ("echo_executing_commands"); 
+
+  return 0;
+}
+
+
 // Should ignore empty elements in a matrix list (i.e., is an
 //  expression like `[[], 1]' ok?
 
--- a/src/user-prefs.h	Tue Oct 31 05:26:50 1995 +0000
+++ b/src/user-prefs.h	Tue Oct 31 06:04:47 1995 +0000
@@ -30,6 +30,7 @@
   int beep_on_error;
   int define_all_return_values;
   int do_fortran_indexing;
+  int echo_executing_commands;
   int empty_list_elements_ok;
   int gnuplot_has_multiplot;
   int ignore_function_time_stamp;
@@ -82,6 +83,7 @@
 extern int beep_on_error (void);
 extern int define_all_return_values (void);
 extern int do_fortran_indexing (void);
+extern int echo_executing_commands (void);
 extern int empty_list_elements_ok (void);
 extern int gnuplot_has_multiplot (void);
 extern int ignore_function_time_stamp (void);
@@ -126,6 +128,14 @@
 extern int sv_ps4 (void);
 extern int sv_pwd (void);
 
+enum echo_state
+{
+  ECHO_OFF = 0,
+  ECHO_SCRIPTS = 1,
+  ECHO_FUNCTIONS = 2,
+  ECHO_CMD_LINE = 4
+};
+
 #endif
 
 /*
--- a/src/variables.cc	Tue Oct 31 05:26:50 1995 +0000
+++ b/src/variables.cc	Tue Oct 31 06:04:47 1995 +0000
@@ -711,12 +711,12 @@
 
       if (is_function_file (ffile))
 	{
-	  unwind_protect_int (echo_input);
+	  unwind_protect_int (user_pref.echo_executing_commands);
 	  unwind_protect_int (saving_history);
 	  unwind_protect_int (reading_fcn_file);
 	  unwind_protect_int (input_from_command_line_file);
 
-	  echo_input = 0;
+	  user_pref.echo_executing_commands = ECHO_OFF;
 	  saving_history = 0;
 	  reading_fcn_file = 1;
 	  input_from_command_line_file = 0;
@@ -1668,6 +1668,10 @@
 	  do_fortran_indexing,
     "allow single indices for matrices");
 
+  DEFVAR ("echo_executing_commands", SBV_echo_executing_commands, 0.0, 0, 
+	  echo_executing_commands,
+    "echo commands as they are executed");
+
   DEFCONST ("e", SBV_e, exp (1.0), 0, 0,
     "exp (1)");