3Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 John W. Eaton
6This file is part of Octave.
8Octave is free software; you can redistribute it and/or modify it
9under the terms of the GNU General Public License as published by the
10Free Software Foundation; either version 3 of the License, or (at your
11option) any later version.
13Octave is distributed in the hope that it will be useful, but WITHOUT
14ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18You should have received a copy of the GNU General Public License
19along with Octave; see the file COPYING. If not, see
20<http://www.gnu.org/licenses/>.
43#include "ov-usr-fcn.h"
44#include "pt-pr-code.h"
47#include "unwind-prot.h"
50// TRUE means that Octave will try to beep obnoxiously before printing
52static bool Vbeep_on_error = false;
54// TRUE means that Octave will try to enter the debugger when an error
55// is encountered. This will also inhibit printing of the normal
56// traceback message (you will only see the top-level error message).
57bool Vdebug_on_error = false;
59// TRUE means that Octave will try to enter the debugger when a warning
61bool Vdebug_on_warning = false;
63// TRUE means that Octave will try to display a stack trace when a
64// warning is encountered.
65static bool Vbacktrace_on_warning = false;
67// TRUE means that Octave will print a verbose warning. Currently unused.
68static bool Vverbose_warning;
70// TRUE means that Octave will print no warnings, but lastwarn will be
72static bool Vquiet_warning = false;
74// A structure containing (most of) the current state of warnings.
75static Octave_map warning_options;
77// The text of the last error message.
78static std::string Vlast_error_message;
80// The text of the last warning message.
81static std::string Vlast_warning_message;
83// The last warning message id.
84static std::string Vlast_warning_id;
86// The last error message id.
87static std::string Vlast_error_id;
89// The last file in which an error occured
90static Octave_map Vlast_error_stack;
92// Current error state.
96// -2: an error has occurred, but don't print any messages.
97// -1: an error has occurred, we are printing a traceback
99// 1: an error has occurred
103// Current warning state.
108// 1: a warning has occurred
110int warning_state = 0;
112// Tell the error handler whether to print messages, or just store
113// them for later. Used for handling errors in eval() and
114// the `unwind_protect' statement.
115int buffer_error_messages = 0;
117// TRUE means error messages are turned off.
118bool discard_error_messages = false;
120// TRUE means warning messages are turned off.
121bool discard_warning_messages = false;
123// The message buffer.
124static std::ostringstream *error_message_buffer = 0;
127reset_error_handler (void)
131 buffer_error_messages = 0;
132 discard_error_messages = false;
136initialize_warning_options (const std::string& state)
138 warning_options.clear ();
140 warning_options.assign ("identifier", "all");
141 warning_options.assign ("state", state);
145initialize_last_error_stack (void)
147 static bool initialized = false;
149 static string_vector sv (4);
161 return Octave_map (dim_vector (0, 1), sv);
164// Warning messages are never buffered.
167vwarning (const char *name, const char *id, const char *fmt, va_list args)
169 if (discard_warning_messages)
172 flush_octave_stdout ();
174 std::ostringstream output_buf;
177 output_buf << name << ": ";
179 octave_vformat (output_buf, fmt, args);
181 output_buf << std::endl;
183 // FIXME -- we really want to capture the message before it
184 // has all the formatting goop attached to it. We probably also
185 // want just the message, not the traceback information.
187 std::string msg_string = output_buf.str ();
191 // This is the first warning in a possible series.
193 Vlast_warning_id = id;
194 Vlast_warning_message = msg_string;
197 if (! Vquiet_warning)
199 octave_diary << msg_string;
201 std::cerr << msg_string;
206verror (bool save_last_error, std::ostream& os,
207 const char *name, const char *id, const char *fmt, va_list args,
208 bool with_cfn = false)
210 if (discard_error_messages)
213 if (! buffer_error_messages)
214 flush_octave_stdout ();
216 // FIXME -- we really want to capture the message before it
217 // has all the formatting goop attached to it. We probably also
218 // want just the message, not the traceback information.
220 std::ostringstream output_buf;
222 octave_vformat (output_buf, fmt, args);
224 std::string base_msg = output_buf.str ();
226 bool to_beep_or_not_to_beep_p = Vbeep_on_error && ! error_state;
228 std::string msg_string;
230 if (to_beep_or_not_to_beep_p)
234 msg_string += std::string (name) + ": ";
236 // If with_fcn is specified, we'll attempt to prefix the message with the name
237 // of the current executing function. But we'll do so only if:
238 // 1. the name is not empty (anonymous function)
239 // 2. it is not already there (including the following colon)
242 octave_function *curfcn = octave_call_stack::current ();
245 std::string cfn = curfcn->name ();
249 if (cfn.length () > base_msg.length ()
250 || base_msg.compare (0, cfn.length (), cfn) != 0)
252 msg_string += cfn + ' ';
258 msg_string += base_msg + "\n";
260 if (! error_state && save_last_error)
262 // This is the first error in a possible series.
265 Vlast_error_message = base_msg;
267 octave_user_code *fcn = octave_call_stack::caller_user_code ();
271 octave_idx_type curr_frame = -1;
273 Vlast_error_stack = octave_call_stack::backtrace (0, curr_frame);
276 Vlast_error_stack = initialize_last_error_stack ();
279 if (buffer_error_messages)
281 if (error_message_buffer)
282 msg_string = "error: " + msg_string;
284 error_message_buffer = new std::ostringstream ();
286 *error_message_buffer << msg_string;
290 octave_diary << msg_string;
295// Note that we don't actually print any message if the error string
296// is just "" or "\n". This allows error ("") and error ("\n") to
297// just set the error state.
300error_1 (std::ostream& os, const char *name, const char *id,
301 const char *fmt, va_list args, bool with_cfn = false)
303 if (error_state != -2)
309 size_t len = strlen (fmt);
313 if (fmt[len - 1] == '\n')
317 char *tmp_fmt = strsave (fmt);
318 tmp_fmt[len - 1] = '\0';
319 verror (true, os, name, id, tmp_fmt, args, with_cfn);
327 verror (true, os, name, id, fmt, args, with_cfn);
336 panic ("error_1: invalid format");
341vmessage (const char *name, const char *fmt, va_list args)
343 verror (false, std::cerr, name, "", fmt, args);
347message (const char *name, const char *fmt, ...)
350 va_start (args, fmt);
351 vmessage (name, fmt, args);
356vmessage_with_id (const char *name, const char *id, const char *fmt,
359 verror (false, std::cerr, name, id, fmt, args);
363message_with_id (const char *name, const char *id, const char *fmt, ...)
366 va_start (args, fmt);
367 vmessage_with_id (name, id, fmt, args);
372usage_1 (const char *id, const char *fmt, va_list args)
374 verror (true, std::cerr, "usage", id, fmt, args);
379vusage (const char *fmt, va_list args)
381 usage_1 ("", fmt, args);
385usage (const char *fmt, ...)
388 va_start (args, fmt);
394vusage_with_id (const char *id, const char *fmt, va_list args)
396 usage_1 (id, fmt, args);
400usage_with_id (const char *id, const char *fmt, ...)
403 va_start (args, fmt);
404 vusage_with_id (id, fmt, args);
409pr_where_2 (const char *fmt, va_list args)
415 size_t len = strlen (fmt);
419 if (fmt[len - 1] == '\n')
423 char *tmp_fmt = strsave (fmt);
424 tmp_fmt[len - 1] = '\0';
425 verror (false, std::cerr, 0, "", tmp_fmt, args);
430 verror (false, std::cerr, 0, "", fmt, args);
435 panic ("pr_where_2: invalid format");
439pr_where_1 (const char *fmt, ...)
442 va_start (args, fmt);
443 pr_where_2 (fmt, args);
448pr_where (const char *who)
450 octave_idx_type curr_frame = -1;
452 Octave_map stk = octave_call_stack::backtrace (0, curr_frame);
454 octave_idx_type nframes_to_display = stk.numel ();
456 if (nframes_to_display > 0)
458 pr_where_1 ("%s: called from\n", who);
460 Cell names = stk.contents ("name");
461 Cell lines = stk.contents ("line");
462 Cell columns = stk.contents ("column");
464 for (octave_idx_type i = 0; i < nframes_to_display; i++)
466 octave_value name = names(i);
467 octave_value line = lines(i);
468 octave_value column = columns(i);
470 std::string nm = name.string_value ();
472 pr_where_1 (" %s at line %d column %d\n", nm.c_str (),
473 line.int_value (), column.int_value ());
479error_2 (const char *id, const char *fmt, va_list args, bool with_cfn = false)
481 int init_state = error_state;
483 error_1 (std::cerr, "error", id, fmt, args, with_cfn);
485 if ((interactive || forced_interactive)
486 && Vdebug_on_error && init_state == 0
487 && octave_call_stack::caller_user_code ())
489 unwind_protect frame;
490 frame.protect_var (Vdebug_on_error);
491 Vdebug_on_error = false;
497 do_keyboard (octave_value_list ());
502verror (const char *fmt, va_list args)
504 error_2 ("", fmt, args);
508error (const char *fmt, ...)
511 va_start (args, fmt);
517verror_with_cfn (const char *fmt, va_list args)
519 error_2 ("", fmt, args, true);
523error_with_cfn (const char *fmt, ...)
526 va_start (args, fmt);
527 verror_with_cfn (fmt, args);
532verror_with_id (const char *id, const char *fmt, va_list args)
534 error_2 (id, fmt, args);
538error_with_id (const char *id, const char *fmt, ...)
541 va_start (args, fmt);
542 verror_with_id (id, fmt, args);
547verror_with_id_cfn (const char *id, const char *fmt, va_list args)
549 error_2 (id, fmt, args, true);
553error_with_id_cfn (const char *id, const char *fmt, ...)
556 va_start (args, fmt);
557 verror_with_id_cfn (id, fmt, args);
562check_state (const std::string& state)
571 else if (state == "on")
573 else if (state == "error")
579// For given warning ID, return 0 if warnings are disabled, 1 if
580// enabled, and 2 if this ID should be an error instead of a warning.
583warning_enabled (const std::string& id)
590 octave_idx_type nel = warning_options.numel ();
594 Cell identifier = warning_options.contents ("identifier");
595 Cell state = warning_options.contents ("state");
597 bool all_found = false;
598 bool id_found = false;
600 for (octave_idx_type i = 0; i < nel; i++)
602 octave_value ov = identifier(i);
603 std::string ovs = ov.string_value ();
605 if (! all_found && ovs == "all")
607 all_state = check_state (state(i).string_value ());
613 if (! id_found && ovs == id)
615 id_state = check_state (state(i).string_value ());
621 if (all_found && id_found)
634 else if (all_state == 1)
636 if (id_state == 0 || id_state == 2)
641 else if (all_state == 2)
653warning_1 (const char *id, const char *fmt, va_list args)
655 int warn_opt = warning_enabled (id);
659 // Handle this warning as an error.
661 error_2 (id, fmt, args);
663 else if (warn_opt == 1)
665 vwarning ("warning", id, fmt, args);
667 if (! symbol_table::at_top_level ()
668 && Vbacktrace_on_warning
670 && ! discard_warning_messages)
671 pr_where ("warning");
675 if ((interactive || forced_interactive)
677 && octave_call_stack::caller_user_code ())
679 unwind_protect frame;
680 frame.protect_var (Vdebug_on_warning);
681 Vdebug_on_warning = false;
683 do_keyboard (octave_value_list ());
689vwarning (const char *fmt, va_list args)
691 warning_1 ("", fmt, args);
695warning (const char *fmt, ...)
698 va_start (args, fmt);
699 vwarning (fmt, args);
704vwarning_with_id (const char *id, const char *fmt, va_list args)
706 warning_1 (id, fmt, args);
710warning_with_id (const char *id, const char *fmt, ...)
713 va_start (args, fmt);
714 vwarning_with_id (id, fmt, args);
719vparse_error (const char *fmt, va_list args)
721 error_1 (std::cerr, 0, "", fmt, args);
725parse_error (const char *fmt, ...)
728 va_start (args, fmt);
729 vparse_error (fmt, args);
734vparse_error_with_id (const char *id, const char *fmt, va_list args)
736 error_1 (std::cerr, 0, id, fmt, args);
740parse_error_with_id (const char *id, const char *fmt, ...)
743 va_start (args, fmt);
744 vparse_error_with_id (id, fmt, args);
749rethrow_error (const char *id, const char *fmt, ...)
752 va_start (args, fmt);
753 error_1 (std::cerr, 0, id, fmt, args);
758panic (const char *fmt, ...)
761 va_start (args, fmt);
762 buffer_error_messages = 0;
763 discard_error_messages = false;
764 verror (false, std::cerr, "panic", "", fmt, args);
770defun_usage_message_1 (const char *fmt, ...)
773 va_start (args, fmt);
774 error_1 (octave_stdout, 0, "", fmt, args);
779defun_usage_message (const std::string& msg)
781 defun_usage_message_1 ("%s", msg.c_str ());
784typedef void (*error_fun)(const char *, const char *, ...);
786extern octave_value_list Fsprintf (const octave_value_list&, int);
789handle_message (error_fun f, const char *id, const char *msg,
790 const octave_value_list& args)
796 int nargin = args.length ();
804 octave_value_list tmp = Fsprintf (args, 1);
810 if (arg.is_defined ())
812 if (arg.is_string ())
814 tstr = arg.string_value ();
820 else if (arg.is_empty ())
827 size_t len = strlen (msg);
831 if (msg[len - 1] == '\n')
835 char *tmp_msg = strsave (msg);
836 tmp_msg[len - 1] = '\0';
837 f (id, "%s\n", tmp_msg);
852DEFUN (rethrow, args, ,
854@deftypefn {Built-in Function} {} rethrow (@var{err})\n\
855Reissues a previous error as defined by @var{err}. @var{err} is a structure\n\
856that must contain at least the 'message' and 'identifier' fields. @var{err}\n\
857can also contain a field 'stack' that gives information on the assumed\n\
858location of the error. Typically @var{err} is returned from\n\
860@seealso{lasterror, lasterr, error}\n\
864 int nargin = args.length();
870 Octave_map err = args(0).map_value ();
874 if (err.contains ("message") && err.contains ("identifier"))
876 std::string msg = err.contents("message")(0).string_value ();
877 std::string id = err.contents("identifier")(0).string_value ();
878 int len = msg.length();
885 Octave_map err_stack = initialize_last_error_stack ();
887 if (err.contains ("stack"))
889 err_stack = err.contents("stack")(0).map_value ();
891 if (err_stack.numel () > 0)
893 if (err_stack.contains ("file"))
894 file = err_stack.contents("file")(0).string_value ();
896 if (err_stack.contains ("name"))
897 nm = err_stack.contents("name")(0).string_value ();
899 if (err_stack.contains ("line"))
900 l = err_stack.contents("line")(0).nint_value ();
902 if (err_stack.contains ("column"))
903 c = err_stack.contents("column")(0).nint_value ();
908 char *tmp_msg = strsave (msg.c_str ());
909 if (tmp_msg[len-1] == '\n')
913 tmp_msg[len - 1] = '\0';
914 rethrow_error (id.c_str (), "%s\n", tmp_msg);
918 rethrow_error (id.c_str (), "%s", tmp_msg);
921 // FIXME -- is this the right thing to do for
922 // Vlast_error_stack? Should it be saved and restored
923 // with unwind_protect?
925 Vlast_error_stack = err_stack;
927 if (err.contains ("stack"))
936 pr_where_1 ("error: near line %d, column %d",
939 pr_where_1 ("error: near line %d", l);
947 pr_where_1 ("error: called from `%s' near line %d, column %d",
950 pr_where_1 ("error: called from `%d' near line %d", nm.c_str (), l);
961 pr_where_1 ("error: in file %s near line %d, column %d",
962 file.c_str (), l, c);
964 pr_where_1 ("error: in file %s near line %d", file.c_str (), l);
972 pr_where_1 ("error: called from `%s' in file %s near line %d, column %d",
973 nm.c_str (), file.c_str (), l, c);
975 pr_where_1 ("error: called from `%d' in file %s near line %d", nm.c_str (), file.c_str (), l);
982 error ("rethrow: structure must contain the fields 'message and 'identifier'");
990@deftypefn {Built-in Function} {} error (@var{template}, @dots{})\n\
991@deftypefnx {Built-in Function} {} error (@var{id}, @var{template}, @dots{})\n\
992Format the optional arguments under the control of the template string\n\
993@var{template} using the same rules as the @code{printf} family of\n\
994functions (@pxref{Formatted Output}) and print the resulting message\n\
995on the @code{stderr} stream. The message is prefixed by the character\n\
996string @samp{error: }.\n\
998Calling @code{error} also sets Octave's internal error state such that\n\
999control will return to the top level without evaluating any more\n\
1000commands. This is useful for aborting from functions or scripts.\n\
1002If the error message does not end with a new line character, Octave will\n\
1003print a traceback of all the function calls leading to the error. For\n\
1004example, given the following function definitions:\n\
1008function f () g (); end\n\
1009function g () h (); end\n\
1010function h () nargin == 1 || error (\"nargin != 1\"); end\n\
1015calling the function @code{f} will result in a list of messages that\n\
1016can help you to quickly locate the exact location of the error:\n\
1021error: nargin != 1\n\
1022error: called from:\n\
1023error: error at line -1, column -1\n\
1024error: h at line 1, column 27\n\
1025error: g at line 1, column 15\n\
1026error: f at line 1, column 15\n\
1030If the error message ends in a new line character, Octave will print the\n\
1031message but will not display any traceback messages as it returns\n\
1032control to the top level. For example, modifying the error message\n\
1033in the previous example to end in a new line causes Octave to only print\n\
1038function h () nargin == 1 || error (\"nargin != 1\\n\"); end\n\
1040error: nargin != 1\n\
1045 octave_value retval;
1047 int nargin = args.length ();
1049 octave_value_list nargs = args;
1059 std::string arg1 = args(0).string_value ();
1063 if (arg1.find ('%') == std::string::npos)
1067 nargs.resize (nargin-1);
1069 for (int i = 1; i < nargin; i++)
1070 nargs(i-1) = args(i);
1076 else if (nargin == 1 && args(0).is_map ())
1078 octave_value_list tmp;
1080 Octave_map m = args(0).map_value ();
1082 if (m.numel () == 1)
1084 if (m.contains ("message"))
1086 Cell c = m.contents ("message");
1088 if (! c.is_empty () && c(0).is_string ())
1089 nargs(0) = c(0).string_value ();
1092 if (m.contains ("identifier"))
1094 Cell c = m.contents ("identifier");
1096 if (! c.is_empty () && c(0).is_string ())
1097 id = c(0).string_value ();
1100 // FIXME -- also need to handle "stack" field in error
1101 // structure, but that will require some more significant
1102 // surgery on handle_message, error_with_id, etc.
1106 handle_message (error_with_id, id.c_str (), "unspecified error", nargs);
1112DEFUN (warning, args, nargout,
1114@deftypefn {Built-in Function} {} warning (@var{template}, @dots{})\n\
1115@deftypefnx {Built-in Function} {} warning (@var{id}, @var{template}, @dots{})\n\
1116Format the optional arguments under the control of the template string\n\
1117@var{template} using the same rules as the @code{printf} family of\n\
1118functions (@pxref{Formatted Output}) and print the resulting message\n\
1119on the @code{stderr} stream. The message is prefixed by the character\n\
1120string @samp{warning: }.\n\
1121You should use this function when you want to notify the user\n\
1122of an unusual condition, but only when it makes sense for your program\n\
1125The optional message identifier allows users to enable or disable\n\
1126warnings tagged by @var{id}. The special identifier @samp{\"all\"} may\n\
1127be used to set the state of all warnings.\n\
1129@deftypefnx {Built-in Function} {} warning (\"on\", @var{id})\n\
1130@deftypefnx {Built-in Function} {} warning (\"off\", @var{id})\n\
1131@deftypefnx {Built-in Function} {} warning (\"error\", @var{id})\n\
1132@deftypefnx {Built-in Function} {} warning (\"query\", @var{id})\n\
1133Set or query the state of a particular warning using the identifier\n\
1134@var{id}. If the identifier is omitted, a value of @samp{\"all\"} is\n\
1135assumed. If you set the state of a warning to @samp{\"error\"}, the\n\
1136warning named by @var{id} is handled as if it were an error instead.\n\
1137@seealso{warning_ids}\n\
1140 octave_value retval;
1142 int nargin = args.length ();
1143 int argc = nargin + 1;
1147 if (argc > 1 && args.all_strings_p ())
1149 string_vector argv = args.make_argv ("warning");
1153 std::string arg1 = argv(1);
1154 std::string arg2 = "all";
1159 if (arg1 == "on" || arg1 == "off" || arg1 == "error")
1161 Octave_map old_warning_options = warning_options;
1173 // Since internal Octave functions are not
1174 // compatible, turning all warnings into errors
1175 // should leave the state of
1176 // Octave:matlab-incompatible alone.
1179 && warning_options.contains ("identifier"))
1181 octave_idx_type n = 1;
1183 Cell tid = warning_options.contents ("identifier");
1184 Cell tst = warning_options.contents ("state");
1186 for (octave_idx_type i = 0; i < tid.numel (); i++)
1188 octave_value vid = tid(i);
1190 if (vid.is_string ())
1192 std::string key = vid.string_value ();
1194 if (key == "Octave:matlab-incompatible"
1195 || key == "Octave:single-quote-string")
1197 id.resize (dim_vector (1, n+1));
1198 st.resize (dim_vector (1, n+1));
1209 tmp.assign ("identifier", id);
1210 tmp.assign ("state", st);
1212 warning_options = tmp;
1216 else if (arg2 == "backtrace")
1218 if (arg1 != "error")
1220 Vbacktrace_on_warning = (arg1 == "on");
1224 else if (arg2 == "debug")
1226 if (arg1 != "error")
1228 Vdebug_on_warning = (arg1 == "on");
1232 else if (arg2 == "verbose")
1234 if (arg1 != "error")
1236 Vverbose_warning = (arg1 == "on");
1240 else if (arg2 == "quiet")
1242 if (arg1 != "error")
1244 Vquiet_warning = (arg1 == "on");
1251 arg2 = Vlast_warning_id;
1254 initialize_warning_options (arg1);
1257 Cell ident = warning_options.contents ("identifier");
1258 Cell state = warning_options.contents ("state");
1260 octave_idx_type nel = ident.numel ();
1264 for (octave_idx_type i = 0; i < nel; i++)
1266 if (ident(i).string_value () == arg2)
1268 // FIXME -- if state for "all" is
1269 // same as arg1, we can simply remove the
1270 // item from the list.
1273 warning_options.assign ("state", state);
1281 // FIXME -- if state for "all" is
1282 // same as arg1, we don't need to do anything.
1284 ident.resize (dim_vector (1, nel+1));
1285 state.resize (dim_vector (1, nel+1));
1290 warning_options.clear ();
1292 warning_options.assign ("identifier", ident);
1293 warning_options.assign ("state", state);
1300 if (done && nargout > 0)
1301 retval = old_warning_options;
1303 else if (arg1 == "query")
1306 retval = warning_options;
1307 else if (arg2 == "backtrace" || arg2 == "debug"
1308 || arg2 == "verbose" || arg2 == "quiet")
1311 tmp.assign ("identifier", arg2);
1312 if (arg2 == "backtrace")
1313 tmp.assign ("state", Vbacktrace_on_warning ? "on" : "off");
1314 else if (arg2 == "debug")
1315 tmp.assign ("state", Vdebug_on_warning ? "on" : "off");
1316 else if (arg2 == "verbose")
1317 tmp.assign ("state", Vverbose_warning ? "on" : "off");
1319 tmp.assign ("state", Vquiet_warning ? "on" : "off");
1326 arg2 = Vlast_warning_id;
1328 Cell ident = warning_options.contents ("identifier");
1329 Cell state = warning_options.contents ("state");
1331 octave_idx_type nel = ident.numel ();
1337 for (octave_idx_type i = 0; i < nel; i++)
1339 if (ident(i).string_value () == arg2)
1341 val = state(i).string_value ();
1349 for (octave_idx_type i = 0; i < nel; i++)
1351 if (ident(i).string_value () == "all")
1353 val = state(i).string_value ();
1364 tmp.assign ("identifier", arg2);
1365 tmp.assign ("state", val);
1370 error ("warning: unable to find default warning state!");
1379 retval = warning_options;
1385 octave_value arg = args(0);
1387 Octave_map old_warning_options = warning_options;
1391 Octave_map m = arg.map_value ();
1393 if (m.contains ("identifier") && m.contains ("state"))
1394 warning_options = m;
1396 error ("warning: expecting structure with fields `identifier' and `state'");
1401 retval = old_warning_options;
1405 if (! (error_state || done))
1407 octave_value_list nargs = args;
1413 std::string arg1 = args(0).string_value ();
1417 if (arg1.find ('%') == std::string::npos)
1421 nargs.resize (nargin-1);
1423 for (int i = 1; i < nargin; i++)
1424 nargs(i-1) = args(i);
1431 std::string prev_msg = Vlast_warning_message;
1433 std::string curr_msg = handle_message (warning_with_id, id.c_str (),
1434 "unspecified warning", nargs);
1444disable_warning (const std::string& id)
1446 octave_value_list args;
1455initialize_default_warning_state (void)
1457 initialize_warning_options ("on");
1459 // Most people will want to have the following disabled.
1461 disable_warning ("Octave:array-to-scalar");
1462 disable_warning ("Octave:array-to-vector");
1463 disable_warning ("Octave:empty-list-elements");
1464 disable_warning ("Octave:fortran-indexing");
1465 disable_warning ("Octave:imag-to-real");
1466 disable_warning ("Octave:matlab-incompatible");
1467 disable_warning ("Octave:missing-semicolon");
1468 disable_warning ("Octave:neg-dim-as-zero");
1469 disable_warning ("Octave:resize-on-range-error");
1470 disable_warning ("Octave:separator-insert");
1471 disable_warning ("Octave:single-quote-string");
1472 disable_warning ("Octave:str-to-num");
1473 disable_warning ("Octave:string-concat");
1474 disable_warning ("Octave:variable-switch-label");
1475 disable_warning ("Octave:int-convert-nan");
1476 disable_warning ("Octave:int-convert-non-int-val");
1477 disable_warning ("Octave:int-convert-overflow");
1478 disable_warning ("Octave:int-math-overflow");
1479 disable_warning ("Octave:complex-cmp-ops");
1482DEFUN (lasterror, args, ,
1484@deftypefn {Built-in Function} {@var{err} =} lasterror (@var{err})\n\
1485@deftypefnx {Built-in Function} {} lasterror ('reset')\n\
1486Returns or sets the last error message. Called without any arguments\n\
1487returns a structure containing the last error message, as well as other\n\
1488information related to this error. The elements of this structure are:\n\
1492The text of the last error message\n\
1493@item 'identifier'\n\
1494The message identifier of this error message\n\
1496A structure containing information on where the message occurred. This might\n\
1497be an empty structure if this in the case where this information cannot\n\
1498be obtained. The fields of this structure are:\n\
1502The name of the file where the error occurred\n\
1504The name of function in which the error occurred\n\
1506The line number at which the error occurred\n\
1508An optional field with the column number at which the error occurred\n\
1512The @var{err} structure may also be passed to @code{lasterror} to set the\n\
1513information about the last error. The only constraint on @var{err} in that\n\
1514case is that it is a scalar structure. Any fields of @var{err} that match\n\
1515the above are set to the value passed in @var{err}, while other fields are\n\
1516set to their default values.\n\
1518If @code{lasterror} is called with the argument 'reset', all values take\n\
1519their default values.\n\
1522 octave_value retval;
1523 int nargin = args.length();
1525 unwind_protect frame;
1527 frame.protect_var (error_state);
1534 err.assign ("message", Vlast_error_message);
1535 err.assign ("identifier", Vlast_error_id);
1537 err.assign ("stack", octave_value (Vlast_error_stack));
1541 if (args(0).is_string())
1543 if (args(0).string_value () == "reset")
1545 Vlast_error_message = std::string();
1546 Vlast_error_id = std::string();
1548 Vlast_error_stack = initialize_last_error_stack ();
1551 error("lasterror: unrecognized string argument");
1553 else if (args(0).is_map ())
1555 Octave_map new_err = args(0).map_value ();
1556 std::string new_error_message;
1557 std::string new_error_id;
1558 std::string new_error_file;
1559 std::string new_error_name;
1560 int new_error_line = -1;
1561 int new_error_column = -1;
1563 if (! error_state && new_err.contains ("message"))
1565 const std::string tmp =
1566 new_err.contents("message")(0).string_value ();
1567 new_error_message = tmp;
1570 if (! error_state && new_err.contains ("identifier"))
1572 const std::string tmp =
1573 new_err.contents("identifier")(0).string_value ();
1577 if (! error_state && new_err.contains ("stack"))
1579 Octave_map new_err_stack =
1580 new_err.contents("identifier")(0).map_value ();
1582 if (! error_state && new_err_stack.contains ("file"))
1584 const std::string tmp =
1585 new_err_stack.contents("file")(0).string_value ();
1586 new_error_file = tmp;
1589 if (! error_state && new_err_stack.contains ("name"))
1591 const std::string tmp =
1592 new_err_stack.contents("name")(0).string_value ();
1593 new_error_name = tmp;
1596 if (! error_state && new_err_stack.contains ("line"))
1599 new_err_stack.contents("line")(0).nint_value ();
1600 new_error_line = tmp;
1603 if (! error_state && new_err_stack.contains ("column"))
1606 new_err_stack.contents("column")(0).nint_value ();
1607 new_error_column = tmp;
1613 Vlast_error_message = new_error_message;
1614 Vlast_error_id = new_error_id;
1616 octave_idx_type curr_frame = -1;
1619 = octave_call_stack::backtrace (0, curr_frame);
1623 error ("lasterror: argument must be a structure or a string");
1635DEFUN (lasterr, args, nargout,
1637@deftypefn {Built-in Function} {[@var{msg}, @var{msgid}] =} lasterr (@var{msg}, @var{msgid})\n\
1638Without any arguments, return the last error message. With one\n\
1639argument, set the last error message to @var{msg}. With two arguments,\n\
1640also set the last message identifier.\n\
1643 octave_value_list retval;
1645 unwind_protect frame;
1647 frame.protect_var (error_state);
1650 int argc = args.length () + 1;
1654 string_vector argv = args.make_argv ("lasterr");
1658 std::string prev_error_id = Vlast_error_id;
1659 std::string prev_error_message = Vlast_error_message;
1662 Vlast_error_id = argv(2);
1665 Vlast_error_message = argv(1);
1667 if (argc == 1 || nargout > 0)
1669 retval(1) = prev_error_id;
1670 retval(0) = prev_error_message;
1674 error ("lasterr: expecting arguments to be character strings");
1682// For backward compatibility.
1683DEFALIAS (error_text, lasterr);
1684DEFALIAS (__error_text__, lasterr);
1686DEFUN (lastwarn, args, nargout,
1688@deftypefn {Built-in Function} {[@var{msg}, @var{msgid}] =} lastwarn (@var{msg}, @var{msgid})\n\
1689Without any arguments, return the last warning message. With one\n\
1690argument, set the last warning message to @var{msg}. With two arguments,\n\
1691also set the last message identifier.\n\
1694 octave_value_list retval;
1696 int argc = args.length () + 1;
1700 string_vector argv = args.make_argv ("lastwarn");
1704 std::string prev_warning_id = Vlast_warning_id;
1705 std::string prev_warning_message = Vlast_warning_message;
1708 Vlast_warning_id = argv(2);
1711 Vlast_warning_message = argv(1);
1713 if (argc == 1 || nargout > 0)
1716 retval(1) = prev_warning_id;
1717 retval(0) = prev_warning_message;
1721 error ("lastwarn: expecting arguments to be character strings");
1729DEFUN (usage, args, ,
1731@deftypefn {Built-in Function} {} usage (@var{msg})\n\
1732Print the message @var{msg}, prefixed by the string @samp{usage: }, and\n\
1733set Octave's internal error state such that control will return to the\n\
1734top level without evaluating any more commands. This is useful for\n\
1735aborting from functions.\n\
1737After @code{usage} is evaluated, Octave will print a traceback of all\n\
1738the function calls leading to the usage message.\n\
1740You should use this function for reporting problems errors that result\n\
1741from an improper call to a function, such as calling a function with an\n\
1742incorrect number of arguments, or with arguments of the wrong type. For\n\
1743example, most functions distributed with Octave begin with code like\n\
1749 usage (\"foo (a, b)\");\n\
1755to check for the proper number of arguments.\n\
1758 octave_value_list retval;
1759 handle_message (usage_with_id, "", "unknown", args);
1763DEFUN (beep_on_error, args, nargout,
1765@deftypefn {Built-in Function} {@var{val} =} beep_on_error ()\n\
1766@deftypefnx {Built-in Function} {@var{old_val} =} beep_on_error (@var{new_val})\n\
1767Query or set the internal variable that controls whether Octave will try\n\
1768to ring the terminal bell before printing an error message.\n\
1771 return SET_INTERNAL_VARIABLE (beep_on_error);
1774DEFUN (debug_on_error, args, nargout,
1776@deftypefn {Built-in Function} {@var{val} =} debug_on_error ()\n\
1777@deftypefnx {Built-in Function} {@var{old_val} =} debug_on_error (@var{new_val})\n\
1778Query or set the internal variable that controls whether Octave will try\n\
1779to enter the debugger when an error is encountered. This will also\n\
1780inhibit printing of the normal traceback message (you will only see\n\
1781the top-level error message).\n\
1784 return SET_INTERNAL_VARIABLE (debug_on_error);
1787DEFUN (debug_on_warning, args, nargout,
1789@deftypefn {Built-in Function} {@var{val} =} debug_on_warning ()\n\
1790@deftypefnx {Built-in Function} {@var{old_val} =} debug_on_warning (@var{new_val})\n\
1791Query or set the internal variable that controls whether Octave will try\n\
1792to enter the debugger when a warning is encountered.\n\
1795 return SET_INTERNAL_VARIABLE (debug_on_warning);
1799last_error_message (void)
1801 return Vlast_error_message;
1807 return Vlast_error_id;
1811last_warning_message (void)
1813 return Vlast_warning_message;
1817last_warning_id (void)
1819 return Vlast_warning_id;