Mercurial > hg > octave-thorsten
changeset 3020:f491f232cb09
[project @ 1997-06-03 22:15:08 by jwe]
author | jwe |
---|---|
date | Tue, 03 Jun 1997 22:15:26 +0000 |
parents | 92aa3d651723 |
children | 18d64612e67a |
files | src/toplev.cc src/variables.cc src/variables.h |
diffstat | 3 files changed, 37 insertions(+), 827 deletions(-) [+] |
line wrap: on
line diff
--- a/src/toplev.cc +++ b/src/toplev.cc @@ -48,6 +48,7 @@ #include "file-ops.h" #include "lo-error.h" #include "lo-mappers.h" +#include "oct-env.h" #include "str-vec.h" #include <defaults.h> @@ -78,18 +79,8 @@ #include "variables.h" #include <version.h> -// Nonzero means we print -static bool Vdefault_eval_print_flag = true; - -// Nonzero means we are using readline. -// (--no-line-editing) -int line_editing = 1; - -// Nonzero means we printed messages about reading startup files. -int reading_startup_message_printed = 0; - -// Nonzero means we are exiting via the builtin exit or quit functions. -int quitting_gracefully = 0; +// TRUE means we are exiting via the builtin exit or quit functions. +static bool quitting_gracefully = false; // Current command to execute. tree_statement_list *global_command = 0; @@ -97,126 +88,9 @@ // Pointer to function that is currently being evaluated. octave_user_function *curr_function = 0; -// Nonzero means input is coming from startup file. -int input_from_startup_file = 0; - -// Nonzero means that input is coming from a file that was named on -// the command line. -int input_from_command_line_file = 1; - // Top level context (?) jmp_buf toplevel; -void -parse_and_execute (FILE *f) -{ - unwind_protect::begin_frame ("parse_and_execute"); - - YY_BUFFER_STATE old_buf = current_buffer (); - YY_BUFFER_STATE new_buf = create_buffer (f); - - unwind_protect::add (restore_input_buffer, old_buf); - unwind_protect::add (delete_input_buffer, new_buf); - - switch_to_buffer (new_buf); - - unwind_protect_int (line_editing); - unwind_protect_int (input_from_command_line_file); - - line_editing = 0; - input_from_command_line_file = 0; - - unwind_protect_ptr (curr_sym_tab); - - int retval; - do - { - reset_parser (); - - retval = yyparse (); - - if (retval == 0 && global_command) - { - global_command->eval (); - - delete global_command; - - global_command = 0; - - bool quit = (tree_return_command::returning - || tree_break_command::breaking); - - if (tree_return_command::returning) - tree_return_command::returning = 0; - - if (tree_break_command::breaking) - tree_break_command::breaking--; - - if (error_state) - { - error ("near line %d of file `%s'", input_line_number, - curr_fcn_file_full_name.c_str ()); - - break; - } - - if (quit) - break; - } - } - while (retval == 0); - - unwind_protect::run_frame ("parse_and_execute"); -} - -static void -safe_fclose (void *f) -{ - if (f) - fclose (static_cast<FILE *> (f)); -} - -void -parse_and_execute (const string& s, bool verbose, const char *warn_for) -{ - unwind_protect::begin_frame ("parse_and_execute_2"); - - unwind_protect_int (reading_script_file); - unwind_protect_str (curr_fcn_file_full_name); - - reading_script_file = 1; - curr_fcn_file_full_name = s; - - FILE *f = get_input_from_file (s, 0); - - if (f) - { - unwind_protect::add (safe_fclose, f); - - unwind_protect_int (input_line_number); - unwind_protect_int (current_input_column); - - input_line_number = 0; - current_input_column = 1; - - if (verbose) - { - cout << "reading commands from " << s << " ... "; - reading_startup_message_printed = 1; - cout.flush (); - } - - parse_and_execute (f); - - if (verbose) - cout << "done." << endl; - } - else if (warn_for) - error ("%s: unable to open file `%s'", warn_for, s.c_str ()); - - unwind_protect::run_frame ("parse_and_execute_2"); -} - int main_loop (void) { @@ -233,7 +107,7 @@ octave_restore_signal_mask (); } - can_interrupt = 1; + can_interrupt = true; octave_catch_interrupts (); @@ -290,38 +164,6 @@ return retval; } -DEFUN (source, args, , - "source (FILE)\n\ -\n\ -Parse and execute the contents of FILE. Like executing commands in a\n\ -script file but without requiring the file to be named `FILE.m'.") -{ - octave_value_list retval; - - int nargin = args.length (); - - if (nargin == 1) - { - string file = args(0).string_value (); - - if (! error_state) - { - file = file_ops::tilde_expand (file); - - parse_and_execute (file, false, "source"); - - if (error_state) - error ("source: error sourcing file `%s'", file.c_str ()); - } - else - error ("source: expecting file name as argument"); - } - else - print_usage ("source"); - - return retval; -} - // Fix up things before exiting. void @@ -412,7 +254,7 @@ int exit_status = 0; - quitting_gracefully = 1; + quitting_gracefully = true; int nargin = args.length (); @@ -457,180 +299,6 @@ return retval; } -// XXX FIXME XXX -- this may not be the best place for these... - -static octave_value_list -feval (const octave_value_list& args, int nargout) -{ - octave_value_list retval; - - octave_function *fcn = is_valid_function (args(0), "feval", 1); - - if (fcn) - { - string_vector arg_names = args.name_tags (); - - int tmp_nargin = args.length () - 1; - - octave_value_list tmp_args (tmp_nargin, octave_value ()); - - string_vector tmp_arg_names (tmp_nargin); - - for (int i = 0; i < tmp_nargin; i++) - { - tmp_args(i) = args(i+1); - tmp_arg_names(i) = arg_names(i+1); - } - - tmp_args.stash_name_tags (tmp_arg_names); - - retval = fcn->do_index_op (nargout, tmp_args); - } - - return retval; -} - -DEFUN (feval, args, nargout, - "feval (NAME, ARGS, ...)\n\ -\n\ -evaluate NAME as a function, passing ARGS as its arguments") -{ - octave_value_list retval; - - int nargin = args.length (); - - if (nargin > 0) - retval = feval (args, nargout); - else - print_usage ("feval"); - - return retval; -} - -static octave_value_list -eval_string (const string& s, bool silent, int& parse_status, int nargout) -{ - unwind_protect::begin_frame ("eval_string"); - - unwind_protect_int (get_input_from_eval_string); - unwind_protect_int (input_from_command_line_file); - unwind_protect_ptr (global_command); - unwind_protect_str (current_eval_string); - - get_input_from_eval_string = 1; - input_from_command_line_file = 0; - current_eval_string = s; - - YY_BUFFER_STATE old_buf = current_buffer (); - YY_BUFFER_STATE new_buf = create_buffer (0); - - unwind_protect::add (restore_input_buffer, old_buf); - unwind_protect::add (delete_input_buffer, new_buf); - - switch_to_buffer (new_buf); - - unwind_protect_ptr (curr_sym_tab); - - reset_parser (); - - parse_status = yyparse (); - - // Important to reset the idea of where input is coming from before - // trying to eval the command we just parsed -- it might contain the - // name of an function file that still needs to be parsed! - - tree_statement_list *command = global_command; - - unwind_protect::run_frame ("eval_string"); - - octave_value_list retval; - - if (parse_status == 0 && command) - { - retval = command->eval (silent, nargout); - delete command; - } - - return retval; -} - -octave_value -eval_string (const string& s, bool silent, int& parse_status) -{ - octave_value retval; - - octave_value_list tmp = eval_string (s, silent, parse_status, 1); - - if (! tmp.empty ()) - retval = tmp(0); - - return retval; -} - -static octave_value_list -eval_string (const octave_value& arg, bool silent, int& parse_status, - int nargout) -{ - string s = arg.string_value (); - - if (error_state) - { - error ("eval: expecting string argument"); - return -1.0; - } - - return eval_string (s, silent, parse_status, nargout); -} - -DEFUN (eval, args, nargout, - "eval (TRY, CATCH)\n\ -\n\ -Evaluate the string TRY as octave code. If that fails, evaluate the\n\ -string CATCH.") -{ - octave_value_list retval; - - int nargin = args.length (); - - if (nargin > 0) - { - unwind_protect::begin_frame ("Feval"); - - if (nargin > 1) - { - unwind_protect_int (buffer_error_messages); - buffer_error_messages = 1; - } - - int parse_status = 0; - - retval = eval_string (args(0), ! Vdefault_eval_print_flag, - parse_status, nargout); - - if (nargin > 1 && (parse_status != 0 || error_state)) - { - error_state = 0; - - // Set up for letting the user print any messages from - // errors that occurred in the first part of this eval(). - - buffer_error_messages = 0; - bind_global_error_variable (); - unwind_protect::add (clear_global_error_variable, 0); - - eval_string (args(1), 0, parse_status, nargout); - - retval = octave_value_list (); - } - - unwind_protect::run_frame ("Feval"); - } - else - print_usage ("eval"); - - return retval; -} - // Execute a shell command. static void @@ -951,21 +619,19 @@ #endif -static int -default_eval_print_flag (void) -{ - Vdefault_eval_print_flag = check_preference ("default_eval_print_flag"); - - return 0; -} - void symbols_of_toplev (void) { - DEFVAR (default_eval_print_flag, 1.0, 0, default_eval_print_flag, - "If the value of this variable is nonzero, Octave will print the\n\ -results of commands executed by eval() that do not end with semicolons."); + DEFCONST (argv, , 0, 0, + "the command line arguments this program was invoked with"); + DEFCONST (program_invocation_name, + octave_env::get_program_invocation_name (), 0, 0, + "the full name of the current program or script, including the\n\ +directory specification"); + + DEFCONST (program_name, octave_env::get_program_name (), 0, 0, + "the name of the current program or script"); } /*
--- a/src/variables.cc +++ b/src/variables.cc @@ -24,28 +24,12 @@ #include <config.h> #endif -#include <cfloat> -#include <cmath> #include <cstdio> #include <cstring> #include <string> -#include <iostream.h> -#include <strstream.h> - -#ifdef HAVE_UNISTD_H -#ifdef HAVE_SYS_TYPES_H -#include <sys/types.h> -#endif -#include <unistd.h> -#endif - -#include "cmd-edit.h" -#include "cmd-hist.h" -#include "file-ops.h" #include "file-stat.h" -#include "lo-mappers.h" #include "oct-env.h" #include "glob-match.h" #include "str-vec.h" @@ -53,38 +37,20 @@ #include <defaults.h> #include "defun.h" #include "dirfns.h" -#include "dynamic-ld.h" #include "error.h" -#include "fn-cache.h" #include "gripes.h" #include "help.h" -#include "input.h" #include "lex.h" -#include "sysdep.h" -#include "oct-hist.h" #include "oct-map.h" -#include "ov-mapper.h" #include "oct-obj.h" #include "ov.h" #include "pager.h" #include "parse.h" -#include "pt-id.h" -#include "pt-indir.h" #include "symtab.h" #include "toplev.h" #include "unwind-prot.h" #include "utils.h" #include "variables.h" -#include <version.h> - -// 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 Vecho_executing_commands; // Should Octave always check to see if function files have changed // since they were last compiled? @@ -549,339 +515,6 @@ return retval; } -static bool -looks_like_octave_copyright (const string& s) -{ - bool retval = false; - - string t = s.substr (0, 15); - - if (t == " Copyright (C) ") - { - size_t pos = s.find ('\n'); - - if (pos != NPOS) - { - pos = s.find ('\n', pos + 1); - - if (pos != NPOS) - { - pos++; - - t = s.substr (pos, 29); - - if (t == " This file is part of Octave." - || t == " This program is free softwar") - retval = true; - } - } - } - - return retval; -} - -// Eat whitespace and comments from FFILE, returning the text of the -// comments read if it doesn't look like a copyright notice. If -// IN_PARTS, consider each block of comments separately; otherwise, -// grab them all at once. If UPDATE_POS is TRUE, line and column -// number information is updated. - -// XXX FIXME XXX -- grab_help_text() in lex.l duplicates some of this -// code! - -static string -gobble_leading_white_space (FILE *ffile, bool in_parts, bool update_pos) -{ - string help_txt; - - bool first_comments_seen = false; - bool begin_comment = false; - bool have_help_text = false; - bool in_comment = false; - int c; - - while ((c = getc (ffile)) != EOF) - { - if (update_pos) - current_input_column++; - - if (begin_comment) - { - if (c == '%' || c == '#') - continue; - else - begin_comment = false; - } - - if (in_comment) - { - if (! have_help_text) - { - first_comments_seen = true; - help_txt += (char) c; - } - - if (c == '\n') - { - if (update_pos) - { - input_line_number++; - current_input_column = 0; - } - in_comment = false; - - if (in_parts) - { - if ((c = getc (ffile)) != EOF) - { - if (update_pos) - current_input_column--; - ungetc (c, ffile); - if (c == '\n') - break; - } - else - break; - } - } - } - else - { - switch (c) - { - case ' ': - case '\t': - if (first_comments_seen) - have_help_text = true; - break; - - case '\n': - if (first_comments_seen) - have_help_text = true; - if (update_pos) - { - input_line_number++; - current_input_column = 0; - } - continue; - - case '%': - case '#': - begin_comment = true; - in_comment = true; - break; - - default: - if (update_pos) - current_input_column--; - ungetc (c, ffile); - goto done; - } - } - } - - done: - - if (! help_txt.empty ()) - { - if (looks_like_octave_copyright (help_txt)) - help_txt.resize (0); - - if (in_parts && help_txt.empty ()) - help_txt = gobble_leading_white_space (ffile, in_parts, update_pos); - } - - return help_txt; -} - -static int -is_function_file (FILE *ffile) -{ - int status = 0; - - long pos = ftell (ffile); - - gobble_leading_white_space (ffile, false, false); - - char buf [10]; - fgets (buf, 10, ffile); - int len = strlen (buf); - if (len > 8 && strncmp (buf, "function", 8) == 0 - && ! (isalnum (buf[8]) || buf[8] == '_')) - status = 1; - - fseek (ffile, pos, SEEK_SET); - - return status; -} - -static void -restore_command_history (void *) -{ - command_history::ignore_entries (! Vsaving_history); -} - -static void -safe_fclose (void *f) -{ - if (f) - fclose (static_cast<FILE *> (f)); -} - -static void -restore_input_stream (void *f) -{ - command_editor::set_input_stream (static_cast<FILE *> (f)); -} - -static int -parse_fcn_file (bool exec_script, const string& ff) -{ - unwind_protect::begin_frame ("parse_fcn_file"); - - int script_file_executed = 0; - - // Open function file and parse. - - int old_reading_fcn_file_state = reading_fcn_file; - - FILE *in_stream = command_editor::get_input_stream (); - - unwind_protect::add (restore_input_stream, in_stream); - - unwind_protect_ptr (ff_instream); - - unwind_protect_int (line_editing); - unwind_protect_int (input_line_number); - unwind_protect_int (current_input_column); - unwind_protect_int (reading_fcn_file); - - line_editing = 0; - reading_fcn_file = 1; - input_line_number = 0; - current_input_column = 1; - - FILE *ffile = get_input_from_file (ff, 0); - - unwind_protect::add (safe_fclose, ffile); - - if (ffile) - { - // Check to see if this file defines a function or is just a - // list of commands. - - if (is_function_file (ffile)) - { - // XXX FIXME XXX -- we shouldn't need both the - // command_history object and the - // Vsaving_history variable... - command_history::ignore_entries (); - - unwind_protect::add (restore_command_history, 0); - - unwind_protect_int (Vecho_executing_commands); - unwind_protect_int (Vsaving_history); - unwind_protect_int (reading_fcn_file); - unwind_protect_int (input_from_command_line_file); - - Vecho_executing_commands = ECHO_OFF; - Vsaving_history = 0; - reading_fcn_file = 1; - input_from_command_line_file = 0; - - YY_BUFFER_STATE old_buf = current_buffer (); - YY_BUFFER_STATE new_buf = create_buffer (ffile); - - unwind_protect::add (restore_input_buffer, (void *) old_buf); - unwind_protect::add (delete_input_buffer, (void *) new_buf); - - switch_to_buffer (new_buf); - - unwind_protect_ptr (curr_sym_tab); - - reset_parser (); - - help_buf = gobble_leading_white_space (ffile, true, true); - - // XXX FIXME XXX -- this should not be necessary. - gobble_leading_white_space (ffile, false, true); - - int status = yyparse (); - - if (status != 0) - { - error ("parse error while reading function file %s", - ff.c_str ()); - global_sym_tab->clear (curr_fcn_file_name); - } - } - else if (exec_script) - { - // The value of `reading_fcn_file' will be restored to the - // proper value when we unwind from this frame. - reading_fcn_file = old_reading_fcn_file_state; - - // XXX FIXME XXX -- we shouldn't need both the - // command_history object and the - // Vsaving_history variable... - command_history::ignore_entries (); - - unwind_protect::add (restore_command_history, 0); - - unwind_protect_int (Vsaving_history); - unwind_protect_int (reading_script_file); - - Vsaving_history = 0; - reading_script_file = 1; - - parse_and_execute (ffile); - - script_file_executed = 1; - } - } - - unwind_protect::run_frame ("parse_fcn_file"); - - return script_file_executed; -} - -static bool -load_fcn_from_file (symbol_record *sym_rec, bool exec_script) -{ - bool script_file_executed = false; - - string nm = sym_rec->name (); - - if (octave_dynamic_loader::load_fcn_from_dot_oct_file (nm)) - { - force_link_to_function (nm); - } - else - { - string ff = fcn_file_in_path (nm); - - // These are needed by yyparse. - - unwind_protect::begin_frame ("load_fcn_from_file"); - - unwind_protect_str (curr_fcn_file_name); - unwind_protect_str (curr_fcn_file_full_name); - - curr_fcn_file_name = nm; - curr_fcn_file_full_name = ff; - - if (ff.length () > 0) - script_file_executed = parse_fcn_file (exec_script, ff); - - if (! (error_state || script_file_executed)) - force_link_to_function (nm); - - unwind_protect::run_frame ("load_fcn_from_file"); - } - - return script_file_executed; -} - bool lookup (symbol_record *sym_rec, bool exec_script) { @@ -955,28 +588,6 @@ panic_impossible (); } -string -get_help_from_file (const string& path) -{ - string retval; - - if (! path.empty ()) - { - FILE *fptr = fopen (path.c_str (), "r"); - - if (fptr) - { - unwind_protect::add (safe_fclose, (void *) fptr); - - retval = gobble_leading_white_space (fptr, true, true); - - unwind_protect::run (); - } - } - - return retval; -} - // Variable values. // Look for the given name in the global symbol table. If it refers @@ -1327,31 +938,6 @@ } } -void -bind_global_error_variable (void) -{ - *error_message_buffer << ends; - - char *error_text = error_message_buffer->str (); - - bind_builtin_variable ("__error_text__", error_text, 1); - - delete [] error_text; - - delete error_message_buffer; - - error_message_buffer = 0; -} - -void -clear_global_error_variable (void *) -{ - delete error_message_buffer; - error_message_buffer = 0; - - bind_builtin_variable ("__error_text__", "", 1); -} - // Give a global variable a definition. This will insert the symbol // in the global table if necessary. @@ -1539,14 +1125,6 @@ // able to provide more meaningful warning or error messages. static int -echo_executing_commands (void) -{ - Vecho_executing_commands = check_preference ("echo_executing_commands"); - - return 0; -} - -static int ignore_function_time_stamp (void) { int pref = 0; @@ -1575,31 +1153,9 @@ DEFVAR (ans, , 0, 0, ""); - DEFCONST (argv, , 0, 0, - "the command line arguments this program was invoked with"); - - DEFVAR (echo_executing_commands, static_cast<double> (ECHO_OFF), 0, - echo_executing_commands, - "echo commands as they are executed"); - - DEFCONST (error_text, "", 0, 0, - "the text of error messages that would have been printed in the\n\ -body of the most recent unwind_protect statement or the TRY part of\n\ -the most recent eval() command. Outside of unwind_protect and\n\ -eval(), or if no error has ocurred within them, the value of\n\ -__error_text__ is guaranteed to be the empty string."); - DEFVAR (ignore_function_time_stamp, "system", 0, ignore_function_time_stamp, "don't check to see if function files have changed since they were\n\ - last compiled. Possible values are \"system\" and \"all\""); - - DEFCONST (program_invocation_name, - octave_env::get_program_invocation_name (), 0, 0, - "the full name of the current program or script, including the\n\ -directory specification"); - - DEFCONST (program_name, octave_env::get_program_name (), 0, 0, - "the name of the current program or script"); +last compiled. Possible values are \"system\" and \"all\""); } /*
--- a/src/variables.h +++ b/src/variables.h @@ -43,27 +43,6 @@ extern void initialize_symbol_tables (void); -extern bool lookup (symbol_record *s, bool exec_script = true); - -extern symbol_record *lookup_by_name (const string& nm, - bool exec_script = true); - -extern octave_value get_global_value (const string& nm); - -extern void set_global_value (const string& nm, const octave_value& val); - -extern string get_help_from_file (const string& f); - -extern string builtin_string_variable (const string&); -extern int builtin_real_scalar_variable (const string&, double&); -extern octave_value builtin_any_variable (const string&); - -extern void link_to_global_variable (symbol_record *sr); -extern void link_to_builtin_variable (symbol_record *sr); -extern void link_to_builtin_or_function (symbol_record *sr); - -extern void force_link_to_function (const string&); - extern bool is_builtin_variable (const string&); extern bool is_text_function_name (const string&); extern bool is_mapper_function_name (const string&); @@ -81,12 +60,31 @@ extern string_vector get_struct_elts (const string& text); +extern string_vector +generate_struct_completions (const string& text, string& prefix, + string& hint); + extern bool looks_like_struct (const string& text); -extern string_vector -generate_struct_completions (const string& text, string& prefix, - string& hint); +extern bool lookup (symbol_record *s, bool exec_script = true); + +extern symbol_record * +lookup_by_name (const string& nm, bool exec_script = true); + +extern octave_value get_global_value (const string& nm); + +extern void set_global_value (const string& nm, const octave_value& val); + +extern string builtin_string_variable (const string&); +extern int builtin_real_scalar_variable (const string&, double&); +extern octave_value builtin_any_variable (const string&); + +extern void link_to_global_variable (symbol_record *sr); +extern void link_to_builtin_variable (symbol_record *sr); +extern void link_to_builtin_or_function (symbol_record *sr); + +extern void force_link_to_function (const string&); extern void bind_ans (const octave_value& val, bool print); @@ -109,16 +107,6 @@ // Symbol table for global symbols. extern symbol_table *global_sym_tab; -enum echo_state -{ - ECHO_OFF = 0, - ECHO_SCRIPTS = 1, - ECHO_FUNCTIONS = 2, - ECHO_CMD_LINE = 4 -}; - -extern int Vecho_executing_commands; - #endif /*