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::protect_var (Vdebug_on_error);
490 Vdebug_on_error = false;
496 do_keyboard (octave_value_list ());
498 unwind_protect::run ();
503verror (const char *fmt, va_list args)
505 error_2 ("", fmt, args);
509error (const char *fmt, ...)
512 va_start (args, fmt);
518verror_with_cfn (const char *fmt, va_list args)
520 error_2 ("", fmt, args, true);
524error_with_cfn (const char *fmt, ...)
527 va_start (args, fmt);
528 verror_with_cfn (fmt, args);
533verror_with_id (const char *id, const char *fmt, va_list args)
535 error_2 (id, fmt, args);
539error_with_id (const char *id, const char *fmt, ...)
542 va_start (args, fmt);
543 verror_with_id (id, fmt, args);
548verror_with_id_cfn (const char *id, const char *fmt, va_list args)
550 error_2 (id, fmt, args, true);
554error_with_id_cfn (const char *id, const char *fmt, ...)
557 va_start (args, fmt);
558 verror_with_id_cfn (id, fmt, args);
563check_state (const std::string& state)
572 else if (state == "on")
574 else if (state == "error")
580// For given warning ID, return 0 if warnings are disabled, 1 if
581// enabled, and 2 if this ID should be an error instead of a warning.
584warning_enabled (const std::string& id)
591 octave_idx_type nel = warning_options.numel ();
595 Cell identifier = warning_options.contents ("identifier");
596 Cell state = warning_options.contents ("state");
598 bool all_found = false;
599 bool id_found = false;
601 for (octave_idx_type i = 0; i < nel; i++)
603 octave_value ov = identifier(i);
604 std::string ovs = ov.string_value ();
606 if (! all_found && ovs == "all")
608 all_state = check_state (state(i).string_value ());
614 if (! id_found && ovs == id)
616 id_state = check_state (state(i).string_value ());
622 if (all_found && id_found)
635 else if (all_state == 1)
637 if (id_state == 0 || id_state == 2)
642 else if (all_state == 2)
654warning_1 (const char *id, const char *fmt, va_list args)
656 int warn_opt = warning_enabled (id);
660 // Handle this warning as an error.
662 error_2 (id, fmt, args);
664 else if (warn_opt == 1)
666 vwarning ("warning", id, fmt, args);
668 if (! symbol_table::at_top_level ()
669 && Vbacktrace_on_warning
671 && ! discard_warning_messages)
672 pr_where ("warning");
676 if ((interactive || forced_interactive)
678 && octave_call_stack::caller_user_code ())
680 unwind_protect::protect_var (Vdebug_on_warning);
681 Vdebug_on_warning = false;
683 do_keyboard (octave_value_list ());
685 unwind_protect::run ();
691vwarning (const char *fmt, va_list args)
693 warning_1 ("", fmt, args);
697warning (const char *fmt, ...)
700 va_start (args, fmt);
701 vwarning (fmt, args);
706vwarning_with_id (const char *id, const char *fmt, va_list args)
708 warning_1 (id, fmt, args);
712warning_with_id (const char *id, const char *fmt, ...)
715 va_start (args, fmt);
716 vwarning_with_id (id, fmt, args);
721vparse_error (const char *fmt, va_list args)
723 error_1 (std::cerr, 0, "", fmt, args);
727parse_error (const char *fmt, ...)
730 va_start (args, fmt);
731 vparse_error (fmt, args);
736vparse_error_with_id (const char *id, const char *fmt, va_list args)
738 error_1 (std::cerr, 0, id, fmt, args);
742parse_error_with_id (const char *id, const char *fmt, ...)
745 va_start (args, fmt);
746 vparse_error_with_id (id, fmt, args);
751rethrow_error (const char *id, const char *fmt, ...)
754 va_start (args, fmt);
755 error_1 (std::cerr, 0, id, fmt, args);
760panic (const char *fmt, ...)
763 va_start (args, fmt);
764 buffer_error_messages = 0;
765 discard_error_messages = false;
766 verror (false, std::cerr, "panic", "", fmt, args);
772defun_usage_message_1 (const char *fmt, ...)
775 va_start (args, fmt);
776 error_1 (octave_stdout, 0, "", fmt, args);
781defun_usage_message (const std::string& msg)
783 defun_usage_message_1 ("%s", msg.c_str ());
786typedef void (*error_fun)(const char *, const char *, ...);
788extern octave_value_list Fsprintf (const octave_value_list&, int);
791handle_message (error_fun f, const char *id, const char *msg,
792 const octave_value_list& args)
798 int nargin = args.length ();
806 octave_value_list tmp = Fsprintf (args, 1);
812 if (arg.is_defined ())
814 if (arg.is_string ())
816 tstr = arg.string_value ();
822 else if (arg.is_empty ())
829 size_t len = strlen (msg);
833 if (msg[len - 1] == '\n')
837 char *tmp_msg = strsave (msg);
838 tmp_msg[len - 1] = '\0';
839 f (id, "%s\n", tmp_msg);
854DEFUN (rethrow, args, ,
856@deftypefn {Built-in Function} {} rethrow (@var{err})\n\
857Reissues a previous error as defined by @var{err}. @var{err} is a structure\n\
858that must contain at least the 'message' and 'identifier' fields. @var{err}\n\
859can also contain a field 'stack' that gives information on the assumed\n\
860location of the error. Typically @var{err} is returned from\n\
862@seealso{lasterror, lasterr, error}\n\
866 int nargin = args.length();
872 Octave_map err = args(0).map_value ();
876 if (err.contains ("message") && err.contains ("identifier"))
878 std::string msg = err.contents("message")(0).string_value ();
879 std::string id = err.contents("identifier")(0).string_value ();
880 int len = msg.length();
887 Octave_map err_stack = initialize_last_error_stack ();
889 if (err.contains ("stack"))
891 err_stack = err.contents("stack")(0).map_value ();
893 if (err_stack.numel () > 0)
895 if (err_stack.contains ("file"))
896 file = err_stack.contents("file")(0).string_value ();
898 if (err_stack.contains ("name"))
899 nm = err_stack.contents("name")(0).string_value ();
901 if (err_stack.contains ("line"))
902 l = err_stack.contents("line")(0).nint_value ();
904 if (err_stack.contains ("column"))
905 c = err_stack.contents("column")(0).nint_value ();
910 char *tmp_msg = strsave (msg.c_str ());
911 if (tmp_msg[len-1] == '\n')
915 tmp_msg[len - 1] = '\0';
916 rethrow_error (id.c_str (), "%s\n", tmp_msg);
920 rethrow_error (id.c_str (), "%s", tmp_msg);
923 // FIXME -- is this the right thing to do for
924 // Vlast_error_stack? Should it be saved and restored
925 // with unwind_protect?
927 Vlast_error_stack = err_stack;
929 if (err.contains ("stack"))
938 pr_where_1 ("error: near line %d, column %d",
941 pr_where_1 ("error: near line %d", l);
949 pr_where_1 ("error: called from `%s' near line %d, column %d",
952 pr_where_1 ("error: called from `%d' near line %d", nm.c_str (), l);
963 pr_where_1 ("error: in file %s near line %d, column %d",
964 file.c_str (), l, c);
966 pr_where_1 ("error: in file %s near line %d", file.c_str (), l);
974 pr_where_1 ("error: called from `%s' in file %s near line %d, column %d",
975 nm.c_str (), file.c_str (), l, c);
977 pr_where_1 ("error: called from `%d' in file %s near line %d", nm.c_str (), file.c_str (), l);
984 error ("rethrow: structure must contain the fields 'message and 'identifier'");
992@deftypefn {Built-in Function} {} error (@var{template}, @dots{})\n\
993@deftypefnx {Built-in Function} {} error (@var{id}, @var{template}, @dots{})\n\
994Format the optional arguments under the control of the template string\n\
995@var{template} using the same rules as the @code{printf} family of\n\
996functions (@pxref{Formatted Output}) and print the resulting message\n\
997on the @code{stderr} stream. The message is prefixed by the character\n\
998string @samp{error: }.\n\
1000Calling @code{error} also sets Octave's internal error state such that\n\
1001control will return to the top level without evaluating any more\n\
1002commands. This is useful for aborting from functions or scripts.\n\
1004If the error message does not end with a new line character, Octave will\n\
1005print a traceback of all the function calls leading to the error. For\n\
1006example, given the following function definitions:\n\
1010function f () g (); end\n\
1011function g () h (); end\n\
1012function h () nargin == 1 || error (\"nargin != 1\"); end\n\
1017calling the function @code{f} will result in a list of messages that\n\
1018can help you to quickly locate the exact location of the error:\n\
1023error: nargin != 1\n\
1024error: called from:\n\
1025error: error at line -1, column -1\n\
1026error: h at line 1, column 27\n\
1027error: g at line 1, column 15\n\
1028error: f at line 1, column 15\n\
1032If the error message ends in a new line character, Octave will print the\n\
1033message but will not display any traceback messages as it returns\n\
1034control to the top level. For example, modifying the error message\n\
1035in the previous example to end in a new line causes Octave to only print\n\
1040function h () nargin == 1 || error (\"nargin != 1\\n\"); end\n\
1042error: nargin != 1\n\
1047 octave_value retval;
1049 int nargin = args.length ();
1051 octave_value_list nargs = args;
1061 std::string arg1 = args(0).string_value ();
1065 if (arg1.find ('%') == std::string::npos)
1069 nargs.resize (nargin-1);
1071 for (int i = 1; i < nargin; i++)
1072 nargs(i-1) = args(i);
1078 else if (nargin == 1 && args(0).is_map ())
1080 octave_value_list tmp;
1082 Octave_map m = args(0).map_value ();
1084 if (m.numel () == 1)
1086 if (m.contains ("message"))
1088 Cell c = m.contents ("message");
1090 if (! c.is_empty () && c(0).is_string ())
1091 nargs(0) = c(0).string_value ();
1094 if (m.contains ("identifier"))
1096 Cell c = m.contents ("identifier");
1098 if (! c.is_empty () && c(0).is_string ())
1099 id = c(0).string_value ();
1102 // FIXME -- also need to handle "stack" field in error
1103 // structure, but that will require some more significant
1104 // surgery on handle_message, error_with_id, etc.
1108 handle_message (error_with_id, id.c_str (), "unspecified error", nargs);
1114DEFUN (warning, args, nargout,
1116@deftypefn {Built-in Function} {} warning (@var{template}, @dots{})\n\
1117@deftypefnx {Built-in Function} {} warning (@var{id}, @var{template}, @dots{})\n\
1118Format the optional arguments under the control of the template string\n\
1119@var{template} using the same rules as the @code{printf} family of\n\
1120functions (@pxref{Formatted Output}) and print the resulting message\n\
1121on the @code{stderr} stream. The message is prefixed by the character\n\
1122string @samp{warning: }.\n\
1123You should use this function when you want to notify the user\n\
1124of an unusual condition, but only when it makes sense for your program\n\
1127The optional message identifier allows users to enable or disable\n\
1128warnings tagged by @var{id}. The special identifier @samp{\"all\"} may\n\
1129be used to set the state of all warnings.\n\
1131@deftypefnx {Built-in Function} {} warning (\"on\", @var{id})\n\
1132@deftypefnx {Built-in Function} {} warning (\"off\", @var{id})\n\
1133@deftypefnx {Built-in Function} {} warning (\"error\", @var{id})\n\
1134@deftypefnx {Built-in Function} {} warning (\"query\", @var{id})\n\
1135Set or query the state of a particular warning using the identifier\n\
1136@var{id}. If the identifier is omitted, a value of @samp{\"all\"} is\n\
1137assumed. If you set the state of a warning to @samp{\"error\"}, the\n\
1138warning named by @var{id} is handled as if it were an error instead.\n\
1139@seealso{warning_ids}\n\
1142 octave_value retval;
1144 int nargin = args.length ();
1145 int argc = nargin + 1;
1149 if (argc > 1 && args.all_strings_p ())
1151 string_vector argv = args.make_argv ("warning");
1155 std::string arg1 = argv(1);
1156 std::string arg2 = "all";
1161 if (arg1 == "on" || arg1 == "off" || arg1 == "error")
1163 Octave_map old_warning_options = warning_options;
1175 // Since internal Octave functions are not
1176 // compatible, turning all warnings into errors
1177 // should leave the state of
1178 // Octave:matlab-incompatible alone.
1181 && warning_options.contains ("identifier"))
1183 octave_idx_type n = 1;
1185 Cell tid = warning_options.contents ("identifier");
1186 Cell tst = warning_options.contents ("state");
1188 for (octave_idx_type i = 0; i < tid.numel (); i++)
1190 octave_value vid = tid(i);
1192 if (vid.is_string ())
1194 std::string key = vid.string_value ();
1196 if (key == "Octave:matlab-incompatible"
1197 || key == "Octave:single-quote-string")
1199 id.resize (dim_vector (1, n+1));
1200 st.resize (dim_vector (1, n+1));
1211 tmp.assign ("identifier", id);
1212 tmp.assign ("state", st);
1214 warning_options = tmp;
1218 else if (arg2 == "backtrace")
1220 if (arg1 != "error")
1222 Vbacktrace_on_warning = (arg1 == "on");
1226 else if (arg2 == "debug")
1228 if (arg1 != "error")
1230 Vdebug_on_warning = (arg1 == "on");
1234 else if (arg2 == "verbose")
1236 if (arg1 != "error")
1238 Vverbose_warning = (arg1 == "on");
1242 else if (arg2 == "quiet")
1244 if (arg1 != "error")
1246 Vquiet_warning = (arg1 == "on");
1253 arg2 = Vlast_warning_id;
1256 initialize_warning_options (arg1);
1259 Cell ident = warning_options.contents ("identifier");
1260 Cell state = warning_options.contents ("state");
1262 octave_idx_type nel = ident.numel ();
1266 for (octave_idx_type i = 0; i < nel; i++)
1268 if (ident(i).string_value () == arg2)
1270 // FIXME -- if state for "all" is
1271 // same as arg1, we can simply remove the
1272 // item from the list.
1275 warning_options.assign ("state", state);
1283 // FIXME -- if state for "all" is
1284 // same as arg1, we don't need to do anything.
1286 ident.resize (dim_vector (1, nel+1));
1287 state.resize (dim_vector (1, nel+1));
1292 warning_options.clear ();
1294 warning_options.assign ("identifier", ident);
1295 warning_options.assign ("state", state);
1302 if (done && nargout > 0)
1303 retval = old_warning_options;
1305 else if (arg1 == "query")
1308 retval = warning_options;
1309 else if (arg2 == "backtrace" || arg2 == "debug"
1310 || arg2 == "verbose" || arg2 == "quiet")
1313 tmp.assign ("identifier", arg2);
1314 if (arg2 == "backtrace")
1315 tmp.assign ("state", Vbacktrace_on_warning ? "on" : "off");
1316 else if (arg2 == "debug")
1317 tmp.assign ("state", Vdebug_on_warning ? "on" : "off");
1318 else if (arg2 == "verbose")
1319 tmp.assign ("state", Vverbose_warning ? "on" : "off");
1321 tmp.assign ("state", Vquiet_warning ? "on" : "off");
1328 arg2 = Vlast_warning_id;
1330 Cell ident = warning_options.contents ("identifier");
1331 Cell state = warning_options.contents ("state");
1333 octave_idx_type nel = ident.numel ();
1339 for (octave_idx_type i = 0; i < nel; i++)
1341 if (ident(i).string_value () == arg2)
1343 val = state(i).string_value ();
1351 for (octave_idx_type i = 0; i < nel; i++)
1353 if (ident(i).string_value () == "all")
1355 val = state(i).string_value ();
1366 tmp.assign ("identifier", arg2);
1367 tmp.assign ("state", val);
1372 error ("warning: unable to find default warning state!");
1381 retval = warning_options;
1387 octave_value arg = args(0);
1389 Octave_map old_warning_options = warning_options;
1393 Octave_map m = arg.map_value ();
1395 if (m.contains ("identifier") && m.contains ("state"))
1396 warning_options = m;
1398 error ("warning: expecting structure with fields `identifier' and `state'");
1403 retval = old_warning_options;
1407 if (! (error_state || done))
1409 octave_value_list nargs = args;
1415 std::string arg1 = args(0).string_value ();
1419 if (arg1.find ('%') == std::string::npos)
1423 nargs.resize (nargin-1);
1425 for (int i = 1; i < nargin; i++)
1426 nargs(i-1) = args(i);
1433 std::string prev_msg = Vlast_warning_message;
1435 std::string curr_msg = handle_message (warning_with_id, id.c_str (),
1436 "unspecified warning", nargs);
1446disable_warning (const std::string& id)
1448 octave_value_list args;
1457initialize_default_warning_state (void)
1459 initialize_warning_options ("on");
1461 // Most people will want to have the following disabled.
1463 disable_warning ("Octave:array-to-scalar");
1464 disable_warning ("Octave:array-to-vector");
1465 disable_warning ("Octave:empty-list-elements");
1466 disable_warning ("Octave:fortran-indexing");
1467 disable_warning ("Octave:imag-to-real");
1468 disable_warning ("Octave:matlab-incompatible");
1469 disable_warning ("Octave:missing-semicolon");
1470 disable_warning ("Octave:neg-dim-as-zero");
1471 disable_warning ("Octave:resize-on-range-error");
1472 disable_warning ("Octave:separator-insert");
1473 disable_warning ("Octave:single-quote-string");
1474 disable_warning ("Octave:str-to-num");
1475 disable_warning ("Octave:string-concat");
1476 disable_warning ("Octave:variable-switch-label");
1477 disable_warning ("Octave:int-convert-nan");
1478 disable_warning ("Octave:int-convert-non-int-val");
1479 disable_warning ("Octave:int-convert-overflow");
1480 disable_warning ("Octave:int-math-overflow");
1481 disable_warning ("Octave:complex-cmp-ops");
1484DEFUN (lasterror, args, ,
1486@deftypefn {Built-in Function} {@var{err} =} lasterror (@var{err})\n\
1487@deftypefnx {Built-in Function} {} lasterror ('reset')\n\
1488Returns or sets the last error message. Called without any arguments\n\
1489returns a structure containing the last error message, as well as other\n\
1490information related to this error. The elements of this structure are:\n\
1494The text of the last error message\n\
1495@item 'identifier'\n\
1496The message identifier of this error message\n\
1498A structure containing information on where the message occurred. This might\n\
1499be an empty structure if this in the case where this information cannot\n\
1500be obtained. The fields of this structure are:\n\
1504The name of the file where the error occurred\n\
1506The name of function in which the error occurred\n\
1508The line number at which the error occurred\n\
1510An optional field with the column number at which the error occurred\n\
1514The @var{err} structure may also be passed to @code{lasterror} to set the\n\
1515information about the last error. The only constraint on @var{err} in that\n\
1516case is that it is a scalar structure. Any fields of @var{err} that match\n\
1517the above are set to the value passed in @var{err}, while other fields are\n\
1518set to their default values.\n\
1520If @code{lasterror} is called with the argument 'reset', all values take\n\
1521their default values.\n\
1524 octave_value retval;
1525 int nargin = args.length();
1527 unwind_protect::frame_id_t uwp_frame = unwind_protect::begin_frame ();
1529 unwind_protect::protect_var (error_state);
1536 err.assign ("message", Vlast_error_message);
1537 err.assign ("identifier", Vlast_error_id);
1539 err.assign ("stack", octave_value (Vlast_error_stack));
1543 if (args(0).is_string())
1545 if (args(0).string_value () == "reset")
1547 Vlast_error_message = std::string();
1548 Vlast_error_id = std::string();
1550 Vlast_error_stack = initialize_last_error_stack ();
1553 error("lasterror: unrecognized string argument");
1555 else if (args(0).is_map ())
1557 Octave_map new_err = args(0).map_value ();
1558 std::string new_error_message;
1559 std::string new_error_id;
1560 std::string new_error_file;
1561 std::string new_error_name;
1562 int new_error_line = -1;
1563 int new_error_column = -1;
1565 if (! error_state && new_err.contains ("message"))
1567 const std::string tmp =
1568 new_err.contents("message")(0).string_value ();
1569 new_error_message = tmp;
1572 if (! error_state && new_err.contains ("identifier"))
1574 const std::string tmp =
1575 new_err.contents("identifier")(0).string_value ();
1579 if (! error_state && new_err.contains ("stack"))
1581 Octave_map new_err_stack =
1582 new_err.contents("identifier")(0).map_value ();
1584 if (! error_state && new_err_stack.contains ("file"))
1586 const std::string tmp =
1587 new_err_stack.contents("file")(0).string_value ();
1588 new_error_file = tmp;
1591 if (! error_state && new_err_stack.contains ("name"))
1593 const std::string tmp =
1594 new_err_stack.contents("name")(0).string_value ();
1595 new_error_name = tmp;
1598 if (! error_state && new_err_stack.contains ("line"))
1601 new_err_stack.contents("line")(0).nint_value ();
1602 new_error_line = tmp;
1605 if (! error_state && new_err_stack.contains ("column"))
1608 new_err_stack.contents("column")(0).nint_value ();
1609 new_error_column = tmp;
1615 Vlast_error_message = new_error_message;
1616 Vlast_error_id = new_error_id;
1618 octave_idx_type curr_frame = -1;
1621 = octave_call_stack::backtrace (0, curr_frame);
1625 error ("lasterror: argument must be a structure or a string");
1634 unwind_protect::run_frame (uwp_frame);
1639DEFUN (lasterr, args, nargout,
1641@deftypefn {Built-in Function} {[@var{msg}, @var{msgid}] =} lasterr (@var{msg}, @var{msgid})\n\
1642Without any arguments, return the last error message. With one\n\
1643argument, set the last error message to @var{msg}. With two arguments,\n\
1644also set the last message identifier.\n\
1647 octave_value_list retval;
1649 unwind_protect::frame_id_t uwp_frame = unwind_protect::begin_frame ();
1651 unwind_protect::protect_var (error_state);
1654 int argc = args.length () + 1;
1658 string_vector argv = args.make_argv ("lasterr");
1662 std::string prev_error_id = Vlast_error_id;
1663 std::string prev_error_message = Vlast_error_message;
1666 Vlast_error_id = argv(2);
1669 Vlast_error_message = argv(1);
1671 if (argc == 1 || nargout > 0)
1673 retval(1) = prev_error_id;
1674 retval(0) = prev_error_message;
1678 error ("lasterr: expecting arguments to be character strings");
1683 unwind_protect::run_frame (uwp_frame);
1688// For backward compatibility.
1689DEFALIAS (error_text, lasterr);
1690DEFALIAS (__error_text__, lasterr);
1692DEFUN (lastwarn, args, nargout,
1694@deftypefn {Built-in Function} {[@var{msg}, @var{msgid}] =} lastwarn (@var{msg}, @var{msgid})\n\
1695Without any arguments, return the last warning message. With one\n\
1696argument, set the last warning message to @var{msg}. With two arguments,\n\
1697also set the last message identifier.\n\
1700 octave_value_list retval;
1702 int argc = args.length () + 1;
1706 string_vector argv = args.make_argv ("lastwarn");
1710 std::string prev_warning_id = Vlast_warning_id;
1711 std::string prev_warning_message = Vlast_warning_message;
1714 Vlast_warning_id = argv(2);
1717 Vlast_warning_message = argv(1);
1719 if (argc == 1 || nargout > 0)
1722 retval(1) = prev_warning_id;
1723 retval(0) = prev_warning_message;
1727 error ("lastwarn: expecting arguments to be character strings");
1735DEFUN (usage, args, ,
1737@deftypefn {Built-in Function} {} usage (@var{msg})\n\
1738Print the message @var{msg}, prefixed by the string @samp{usage: }, and\n\
1739set Octave's internal error state such that control will return to the\n\
1740top level without evaluating any more commands. This is useful for\n\
1741aborting from functions.\n\
1743After @code{usage} is evaluated, Octave will print a traceback of all\n\
1744the function calls leading to the usage message.\n\
1746You should use this function for reporting problems errors that result\n\
1747from an improper call to a function, such as calling a function with an\n\
1748incorrect number of arguments, or with arguments of the wrong type. For\n\
1749example, most functions distributed with Octave begin with code like\n\
1755 usage (\"foo (a, b)\");\n\
1761to check for the proper number of arguments.\n\
1764 octave_value_list retval;
1765 handle_message (usage_with_id, "", "unknown", args);
1769DEFUN (beep_on_error, args, nargout,
1771@deftypefn {Built-in Function} {@var{val} =} beep_on_error ()\n\
1772@deftypefnx {Built-in Function} {@var{old_val} =} beep_on_error (@var{new_val})\n\
1773Query or set the internal variable that controls whether Octave will try\n\
1774to ring the terminal bell before printing an error message.\n\
1777 return SET_INTERNAL_VARIABLE (beep_on_error);
1780DEFUN (debug_on_error, args, nargout,
1782@deftypefn {Built-in Function} {@var{val} =} debug_on_error ()\n\
1783@deftypefnx {Built-in Function} {@var{old_val} =} debug_on_error (@var{new_val})\n\
1784Query or set the internal variable that controls whether Octave will try\n\
1785to enter the debugger when an error is encountered. This will also\n\
1786inhibit printing of the normal traceback message (you will only see\n\
1787the top-level error message).\n\
1790 return SET_INTERNAL_VARIABLE (debug_on_error);
1793DEFUN (debug_on_warning, args, nargout,
1795@deftypefn {Built-in Function} {@var{val} =} debug_on_warning ()\n\
1796@deftypefnx {Built-in Function} {@var{old_val} =} debug_on_warning (@var{new_val})\n\
1797Query or set the internal variable that controls whether Octave will try\n\
1798to enter the debugger when a warning is encountered.\n\
1801 return SET_INTERNAL_VARIABLE (debug_on_warning);
1805last_error_message (void)
1807 return Vlast_error_message;
1813 return Vlast_error_id;
1817last_warning_message (void)
1819 return Vlast_warning_message;
1823last_warning_id (void)
1825 return Vlast_warning_id;
1829;;; Local Variables: ***