1
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
1
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
1
|
26 #endif |
|
27 |
1343
|
28 #include <cstdarg> |
1633
|
29 #include <cstring> |
1343
|
30 |
1728
|
31 #include <string> |
|
32 |
4051
|
33 #include "lo-sstream.h" |
|
34 |
1352
|
35 #include "defun.h" |
1
|
36 #include "error.h" |
3707
|
37 #include "input.h" |
1742
|
38 #include "pager.h" |
1352
|
39 #include "oct-obj.h" |
|
40 #include "utils.h" |
2370
|
41 #include "ov.h" |
3707
|
42 #include "ov-usr-fcn.h" |
|
43 #include "pt-pr-code.h" |
|
44 #include "pt-stmt.h" |
|
45 #include "toplev.h" |
|
46 #include "unwind-prot.h" |
2370
|
47 #include "variables.h" |
1
|
48 |
2174
|
49 // TRUE means that Octave will try to beep obnoxiously before printing |
|
50 // error messages. |
|
51 static bool Vbeep_on_error; |
|
52 |
3707
|
53 // TRUE means that Octave will try to enter the debugger when an error |
|
54 // is encountered. This will also inhibit printing of the normal |
|
55 // traceback message (you will only see the top-level error message). |
|
56 static bool Vdebug_on_error; |
|
57 |
|
58 // TRUE means that Octave will try to enter the debugger when a warning |
|
59 // is encountered. |
|
60 static bool Vdebug_on_warning; |
|
61 |
3935
|
62 // The text of the last error message. |
|
63 static std::string Vlast_error_message; |
|
64 |
3934
|
65 // The text of the last warning message. |
|
66 static std::string Vlast_warning_message; |
|
67 |
|
68 // The warning frequency for Matlab handle graphics backwards |
|
69 // compatibility warnings (currently not used). |
|
70 static std::string Vwarning_frequency = "once"; |
|
71 |
|
72 // The current warning state. Valid values are "on", "off", |
|
73 // "backtrace", or "debug". |
3935
|
74 std::string Vwarning_option = "backtrace"; |
3934
|
75 |
143
|
76 // Current error state. |
3935
|
77 // |
|
78 // Valid values: |
|
79 // |
|
80 // -2: an error has occurred, but don't print any messages. |
|
81 // -1: an error has occurred, we are printing a traceback |
|
82 // 0: no error |
|
83 // 1: an error has occurred |
|
84 // |
672
|
85 int error_state = 0; |
|
86 |
3489
|
87 // Current warning state. |
3935
|
88 // |
|
89 // Valid values: |
|
90 // |
|
91 // 0: no warning |
|
92 // 1: a warning has occurred |
|
93 // |
3489
|
94 int warning_state = 0; |
|
95 |
1489
|
96 // Tell the error handler whether to print messages, or just store |
|
97 // them for later. Used for handling errors in eval() and |
|
98 // the `unwind_protect' statement. |
4699
|
99 int buffer_error_messages = 0; |
1489
|
100 |
3815
|
101 // TRUE means error messages are turned off. |
|
102 bool discard_error_messages = false; |
|
103 |
4452
|
104 // TRUE means warning messages are turned off. |
|
105 bool discard_warning_messages = false; |
|
106 |
3811
|
107 // The message buffer. |
4051
|
108 static OSSTREAM *error_message_buffer = 0; |
143
|
109 |
4318
|
110 void |
|
111 reset_error_handler (void) |
|
112 { |
|
113 error_state = 0; |
|
114 warning_state = 0; |
4699
|
115 buffer_error_messages = 0; |
4318
|
116 discard_error_messages = false; |
|
117 } |
|
118 |
3491
|
119 // Warning messages are never buffered. |
|
120 |
|
121 static void |
|
122 vwarning (const char *name, const char *fmt, va_list args) |
|
123 { |
4452
|
124 if (discard_warning_messages) |
|
125 return; |
|
126 |
3491
|
127 flush_octave_stdout (); |
|
128 |
4051
|
129 OSSTREAM output_buf; |
3491
|
130 |
|
131 if (name) |
3761
|
132 output_buf << name << ": "; |
|
133 |
|
134 octave_vformat (output_buf, fmt, args); |
|
135 |
4051
|
136 output_buf << std::endl << OSSTREAM_ENDS; |
3491
|
137 |
3935
|
138 // XXX FIXME XXX -- we really want to capture the message before it |
|
139 // has all the formatting goop attached to it. We probably also |
|
140 // want just the message, not the traceback information. |
|
141 |
4051
|
142 std::string msg_string = OSSTREAM_STR (output_buf); |
3761
|
143 |
4051
|
144 OSSTREAM_FREEZE (output_buf); |
3934
|
145 |
3935
|
146 if (! warning_state) |
|
147 { |
|
148 // This is the first warning in a possible series. |
|
149 Vlast_warning_message = msg_string; |
|
150 } |
3934
|
151 |
3935
|
152 octave_diary << msg_string; |
|
153 |
|
154 std::cerr << msg_string; |
3491
|
155 } |
|
156 |
1
|
157 static void |
4732
|
158 verror (bool save_last_error, std::ostream& os, |
|
159 const char *name, const char *fmt, va_list args) |
1
|
160 { |
3815
|
161 if (discard_error_messages) |
|
162 return; |
|
163 |
3585
|
164 if (! buffer_error_messages) |
|
165 flush_octave_stdout (); |
914
|
166 |
2174
|
167 bool to_beep_or_not_to_beep_p = Vbeep_on_error && ! error_state; |
1423
|
168 |
4051
|
169 OSSTREAM output_buf; |
1
|
170 |
2174
|
171 if (to_beep_or_not_to_beep_p) |
1423
|
172 output_buf << "\a"; |
3620
|
173 |
599
|
174 if (name) |
|
175 output_buf << name << ": "; |
3620
|
176 |
|
177 octave_vformat (output_buf, fmt, args); |
|
178 |
4051
|
179 output_buf << std::endl << OSSTREAM_ENDS; |
581
|
180 |
3935
|
181 // XXX FIXME XXX -- we really want to capture the message before it |
|
182 // has all the formatting goop attached to it. We probably also |
|
183 // want just the message, not the traceback information. |
|
184 |
4051
|
185 std::string msg_string = OSSTREAM_STR (output_buf); |
3935
|
186 |
4051
|
187 OSSTREAM_FREEZE (output_buf); |
3935
|
188 |
4731
|
189 if (! error_state && save_last_error) |
3935
|
190 { |
|
191 // This is the first error in a possible series. |
|
192 Vlast_error_message = msg_string; |
|
193 } |
|
194 |
1489
|
195 if (buffer_error_messages) |
|
196 { |
3941
|
197 std::string tmp = msg_string; |
1489
|
198 |
|
199 if (! error_message_buffer) |
|
200 { |
4051
|
201 error_message_buffer = new OSSTREAM; |
1489
|
202 |
|
203 // XXX FIXME XXX -- this is ugly, but it prevents |
|
204 // |
4699
|
205 // eval ("error (\"msg\")", "error (lasterr ())"); |
1489
|
206 // |
|
207 // from printing `error: ' twice. Assumes that the NAME we |
|
208 // have been given doesn't contain `:'. |
|
209 |
3935
|
210 size_t pos = msg_string.find (':'); |
3162
|
211 |
3935
|
212 if (pos != NPOS && pos < Vlast_error_message.length () - 2) |
|
213 tmp = msg_string.substr (pos+2); |
1489
|
214 } |
|
215 |
3935
|
216 *error_message_buffer << tmp; |
1489
|
217 } |
|
218 else |
|
219 { |
3935
|
220 octave_diary << msg_string; |
4732
|
221 os << msg_string; |
1489
|
222 } |
1
|
223 } |
|
224 |
1266
|
225 // Note that we don't actually print any message if the error string |
|
226 // is just "" or "\n". This allows error ("") and error ("\n") to |
|
227 // just set the error state. |
|
228 |
1005
|
229 static void |
4732
|
230 error_1 (std::ostream& os, const char *name, const char *fmt, va_list args) |
1005
|
231 { |
|
232 if (error_state != -2) |
|
233 { |
1489
|
234 if (fmt) |
1005
|
235 { |
1489
|
236 if (*fmt) |
1005
|
237 { |
1489
|
238 int len = strlen (fmt); |
|
239 if (fmt[len - 1] == '\n') |
1266
|
240 { |
1489
|
241 if (len > 1) |
1266
|
242 { |
1489
|
243 char *tmp_fmt = strsave (fmt); |
|
244 tmp_fmt[len - 1] = '\0'; |
4732
|
245 verror (true, os, name, tmp_fmt, args); |
1489
|
246 delete [] tmp_fmt; |
|
247 } |
1423
|
248 |
1489
|
249 error_state = -2; |
1266
|
250 } |
1489
|
251 else |
4732
|
252 verror (true, os, name, fmt, args); |
1005
|
253 } |
|
254 } |
1489
|
255 else |
|
256 panic ("error_1: invalid format"); |
1423
|
257 |
|
258 if (! error_state) |
|
259 error_state = 1; |
1005
|
260 } |
|
261 } |
|
262 |
1
|
263 void |
|
264 message (const char *name, const char *fmt, ...) |
|
265 { |
|
266 va_list args; |
|
267 va_start (args, fmt); |
4732
|
268 verror (false, std::cerr, name, fmt, args); |
1
|
269 va_end (args); |
|
270 } |
|
271 |
|
272 void |
|
273 usage (const char *fmt, ...) |
|
274 { |
|
275 va_list args; |
|
276 va_start (args, fmt); |
4732
|
277 verror (true, std::cerr, "usage", fmt, args); |
905
|
278 error_state = -1; |
1
|
279 va_end (args); |
|
280 } |
|
281 |
3707
|
282 static void |
3719
|
283 pr_where_2 (const char *fmt, va_list args) |
|
284 { |
|
285 if (fmt) |
|
286 { |
|
287 if (*fmt) |
|
288 { |
|
289 int len = strlen (fmt); |
|
290 if (fmt[len - 1] == '\n') |
|
291 { |
|
292 if (len > 1) |
|
293 { |
|
294 char *tmp_fmt = strsave (fmt); |
|
295 tmp_fmt[len - 1] = '\0'; |
4732
|
296 verror (false, std::cerr, 0, tmp_fmt, args); |
3719
|
297 delete [] tmp_fmt; |
|
298 } |
|
299 } |
|
300 else |
4732
|
301 verror (false, std::cerr, 0, fmt, args); |
3719
|
302 } |
|
303 } |
|
304 else |
|
305 panic ("pr_where_2: invalid format"); |
|
306 } |
|
307 |
|
308 static void |
3707
|
309 pr_where_1 (const char *fmt, ...) |
|
310 { |
|
311 va_list args; |
|
312 va_start (args, fmt); |
3719
|
313 pr_where_2 (fmt, args); |
3707
|
314 va_end (args); |
|
315 } |
|
316 |
|
317 static void |
4719
|
318 pr_where (const char *name, bool print_code = true) |
3707
|
319 { |
|
320 if (curr_statement) |
|
321 { |
4976
|
322 std::string nm; |
3707
|
323 |
4976
|
324 int l = -1; |
|
325 int c = -1; |
|
326 |
|
327 octave_function *fcn = curr_function; |
|
328 |
|
329 if (fcn) |
3708
|
330 { |
4976
|
331 nm = fcn->name (); |
|
332 |
|
333 if (nm == "error" || nm == "warning") |
|
334 fcn = curr_caller_function; |
|
335 |
|
336 if (fcn) |
|
337 { |
|
338 nm = fcn->fcn_file_name (); |
3708
|
339 |
4976
|
340 if (nm.empty ()) |
|
341 nm = fcn->name (); |
|
342 |
|
343 if (curr_statement) |
|
344 { |
|
345 l = curr_statement->line (); |
|
346 c = curr_statement->column (); |
|
347 } |
|
348 } |
3708
|
349 } |
3707
|
350 |
4976
|
351 if (nm.empty ()) |
|
352 { |
|
353 if (l > 0 && c > 0) |
|
354 pr_where_1 ("%s: near line %d, column %d:", name, l, c); |
|
355 } |
3708
|
356 else |
4976
|
357 { |
|
358 if (l > 0 && c > 0) |
|
359 pr_where_1 ("%s: in %s near line %d, column %d:", |
|
360 name, nm.c_str (), l, c); |
|
361 else |
|
362 pr_where_1 ("%s: in %s", name, nm.c_str ()); |
|
363 } |
3707
|
364 |
4719
|
365 if (print_code) |
|
366 { |
|
367 // XXX FIXME XXX -- Note that the column number is probably |
|
368 // not going to mean much here since the code is being |
|
369 // reproduced from the parse tree, and we are only showing |
|
370 // one statement even if there were multiple statements on |
|
371 // the original source line. |
3707
|
372 |
4719
|
373 OSSTREAM output_buf; |
3708
|
374 |
4719
|
375 output_buf << std::endl; |
3707
|
376 |
4719
|
377 tree_print_code tpc (output_buf, ">>> "); |
|
378 |
|
379 curr_statement->accept (tpc); |
3707
|
380 |
4719
|
381 output_buf << std::endl << OSSTREAM_ENDS; |
3707
|
382 |
4719
|
383 pr_where_1 ("%s", OSSTREAM_C_STR (output_buf)); |
3707
|
384 |
4719
|
385 OSSTREAM_FREEZE (output_buf); |
|
386 } |
3707
|
387 } |
|
388 } |
|
389 |
1
|
390 void |
|
391 warning (const char *fmt, ...) |
|
392 { |
3934
|
393 if (Vwarning_option != "off") |
|
394 { |
4014
|
395 if (curr_sym_tab != top_level_sym_tab |
|
396 && Vwarning_option == "backtrace" |
4452
|
397 && ! warning_state |
|
398 && ! discard_warning_messages) |
4719
|
399 pr_where ("warning", false); |
3986
|
400 |
3934
|
401 va_list args; |
|
402 va_start (args, fmt); |
|
403 vwarning ("warning", fmt, args); |
|
404 va_end (args); |
3707
|
405 |
3935
|
406 warning_state = 1; |
|
407 |
3934
|
408 if ((interactive || forced_interactive) |
|
409 && Vdebug_on_warning && curr_function) |
|
410 { |
|
411 unwind_protect_bool (Vdebug_on_warning); |
|
412 Vdebug_on_warning = false; |
3707
|
413 |
3934
|
414 do_keyboard (octave_value_list ()); |
3707
|
415 |
3934
|
416 unwind_protect::run (); |
|
417 } |
3707
|
418 } |
1
|
419 } |
|
420 |
|
421 void |
|
422 error (const char *fmt, ...) |
|
423 { |
3707
|
424 int init_state = error_state; |
|
425 |
1
|
426 va_list args; |
|
427 va_start (args, fmt); |
4732
|
428 error_1 (std::cerr, "error", fmt, args); |
1005
|
429 va_end (args); |
3707
|
430 |
|
431 if ((interactive || forced_interactive) |
|
432 && Vdebug_on_error && init_state == 0 && curr_function) |
|
433 { |
|
434 unwind_protect_bool (Vdebug_on_error); |
|
435 Vdebug_on_error = false; |
|
436 |
3708
|
437 pr_where ("error"); |
3707
|
438 |
|
439 error_state = 0; |
|
440 |
|
441 do_keyboard (octave_value_list ()); |
|
442 |
|
443 unwind_protect::run (); |
|
444 } |
1005
|
445 } |
436
|
446 |
1005
|
447 void |
|
448 parse_error (const char *fmt, ...) |
|
449 { |
|
450 va_list args; |
|
451 va_start (args, fmt); |
4732
|
452 error_1 (std::cerr, 0, fmt, args); |
1
|
453 va_end (args); |
|
454 } |
|
455 |
189
|
456 void |
1
|
457 panic (const char *fmt, ...) |
|
458 { |
|
459 va_list args; |
|
460 va_start (args, fmt); |
4699
|
461 buffer_error_messages = 0; |
3815
|
462 discard_error_messages = false; |
4732
|
463 verror (false, std::cerr, "panic", fmt, args); |
1
|
464 va_end (args); |
|
465 abort (); |
|
466 } |
|
467 |
4732
|
468 static void |
|
469 defun_usage_message_1 (const char *fmt, ...) |
|
470 { |
|
471 va_list args; |
|
472 va_start (args, fmt); |
|
473 error_1 (octave_stdout, 0, fmt, args); |
|
474 va_end (args); |
|
475 } |
|
476 |
|
477 void |
|
478 defun_usage_message (const std::string& msg) |
|
479 { |
|
480 defun_usage_message_1 ("%s", msg.c_str ()); |
|
481 } |
|
482 |
1489
|
483 typedef void (*error_fun)(const char *, ...); |
|
484 |
2086
|
485 extern octave_value_list Fsprintf (const octave_value_list&, int); |
1489
|
486 |
3934
|
487 static std::string |
2086
|
488 handle_message (error_fun f, const char *msg, const octave_value_list& args) |
528
|
489 { |
3934
|
490 std::string retval; |
528
|
491 |
3523
|
492 std::string tstr; |
1728
|
493 |
528
|
494 int nargin = args.length (); |
|
495 |
2745
|
496 if (nargin > 0) |
528
|
497 { |
3066
|
498 octave_value arg; |
|
499 |
|
500 if (nargin > 1) |
|
501 { |
|
502 octave_value_list tmp = Fsprintf (args, 1); |
|
503 arg = tmp(0); |
|
504 } |
|
505 else |
|
506 arg = args(0); |
2745
|
507 |
|
508 if (arg.is_defined ()) |
528
|
509 { |
2745
|
510 if (arg.is_string ()) |
|
511 { |
|
512 tstr = arg.string_value (); |
|
513 msg = tstr.c_str (); |
|
514 |
|
515 if (! msg) |
|
516 return retval; |
|
517 } |
|
518 else if (arg.is_empty ()) |
528
|
519 return retval; |
|
520 } |
|
521 } |
|
522 |
1489
|
523 // Ugh. |
|
524 |
|
525 int len = strlen (msg); |
|
526 if (msg[len - 1] == '\n') |
|
527 { |
|
528 if (len > 1) |
|
529 { |
|
530 char *tmp_msg = strsave (msg); |
|
531 tmp_msg[len - 1] = '\0'; |
|
532 f ("%s\n", tmp_msg); |
3934
|
533 retval = tmp_msg; |
1489
|
534 delete [] tmp_msg; |
|
535 } |
|
536 } |
|
537 else |
3934
|
538 { |
|
539 f ("%s", msg); |
|
540 retval = msg; |
|
541 } |
528
|
542 |
|
543 return retval; |
|
544 } |
|
545 |
1957
|
546 DEFUN (error, args, , |
3373
|
547 "-*- texinfo -*-\n\ |
|
548 @deftypefn {Built-in Function} {} error (@var{template}, @dots{})\n\ |
|
549 The @code{error} function formats the optional arguments under the\n\ |
|
550 control of the template string @var{template} using the same rules as\n\ |
|
551 the @code{printf} family of functions (@pxref{Formatted Output}).\n\ |
|
552 The resulting message is prefixed by the string @samp{error: } and\n\ |
|
553 printed on the @code{stderr} stream.\n\ |
|
554 \n\ |
|
555 Calling @code{error} also sets Octave's internal error state such that\n\ |
|
556 control will return to the top level without evaluating any more\n\ |
|
557 commands. This is useful for aborting from functions or scripts.\n\ |
897
|
558 \n\ |
3373
|
559 If the error message does not end with a new line character, Octave will\n\ |
|
560 print a traceback of all the function calls leading to the error. For\n\ |
|
561 example, given the following function definitions:\n\ |
|
562 \n\ |
|
563 @example\n\ |
|
564 @group\n\ |
|
565 function f () g () end\n\ |
|
566 function g () h () end\n\ |
|
567 function h () nargin == 1 || error (\"nargin != 1\"); end\n\ |
|
568 @end group\n\ |
|
569 @end example\n\ |
1489
|
570 \n\ |
3373
|
571 @noindent\n\ |
|
572 calling the function @code{f} will result in a list of messages that\n\ |
|
573 can help you to quickly locate the exact location of the error:\n\ |
1489
|
574 \n\ |
3373
|
575 @example\n\ |
|
576 @group\n\ |
|
577 f ()\n\ |
|
578 error: nargin != 1\n\ |
|
579 error: evaluating index expression near line 1, column 30\n\ |
|
580 error: evaluating binary operator `||' near line 1, column 27\n\ |
|
581 error: called from `h'\n\ |
|
582 error: called from `g'\n\ |
|
583 error: called from `f'\n\ |
|
584 @end group\n\ |
|
585 @end example\n\ |
|
586 \n\ |
|
587 If the error message ends in a new line character, Octave will print the\n\ |
|
588 message but will not display any traceback messages as it returns\n\ |
|
589 control to the top level. For example, modifying the error message\n\ |
|
590 in the previous example to end in a new line causes Octave to only print\n\ |
|
591 a single message:\n\ |
|
592 \n\ |
|
593 @example\n\ |
|
594 @group\n\ |
|
595 function h () nargin == 1 || error (\"nargin != 1\\n\"); end\n\ |
|
596 f ()\n\ |
|
597 error: nargin != 1\n\ |
|
598 @end group\n\ |
|
599 @end example\n\ |
|
600 @end deftypefn") |
897
|
601 { |
3934
|
602 octave_value_list retval; |
|
603 handle_message (error, "unspecified error", args); |
|
604 return retval; |
1489
|
605 } |
897
|
606 |
3934
|
607 static inline octave_value_list |
|
608 set_warning_option (const std::string& state, |
3935
|
609 const std::string& frequency, int nargout) |
3934
|
610 { |
|
611 octave_value_list retval; |
|
612 |
|
613 if (nargout > 1) |
|
614 retval(1) = Vwarning_frequency; |
|
615 |
3935
|
616 if (nargout >= 0) |
3934
|
617 retval(0) = Vwarning_option; |
|
618 |
|
619 if (! state.empty ()) |
|
620 Vwarning_option = state; |
|
621 |
|
622 if (! frequency.empty ()) |
|
623 Vwarning_frequency = frequency; |
|
624 |
|
625 return retval; |
|
626 } |
|
627 |
3935
|
628 DEFUN (warning, args, nargout, |
3373
|
629 "-*- texinfo -*-\n\ |
|
630 @deftypefn {Built-in Function} {} warning (@var{msg})\n\ |
|
631 Print a warning message @var{msg} prefixed by the string @samp{warning: }. \n\ |
|
632 After printing the warning message, Octave will continue to execute\n\ |
3600
|
633 commands. You should use this function when you want to notify the user\n\ |
|
634 of an unusual condition, but only when it makes sense for your program\n\ |
|
635 to go on.\n\ |
3373
|
636 @end deftypefn") |
1489
|
637 { |
3934
|
638 octave_value_list retval; |
|
639 |
|
640 int argc = args.length () + 1; |
|
641 |
3935
|
642 bool done = false; |
3934
|
643 |
3935
|
644 if (args.all_strings_p ()) |
|
645 { |
|
646 string_vector argv = args.make_argv ("warning"); |
|
647 |
|
648 if (! error_state) |
3934
|
649 { |
3935
|
650 if (argc == 1) |
3934
|
651 { |
|
652 retval = set_warning_option ("", "", nargout); |
|
653 done = true; |
|
654 } |
3935
|
655 else if (argc == 2) |
|
656 { |
|
657 std::string arg = argv(1); |
3934
|
658 |
3935
|
659 if (arg == "on" || arg == "off" || arg == "backtrace") |
|
660 { |
|
661 retval = set_warning_option (arg, "", nargout); |
|
662 done = true; |
|
663 } |
|
664 else if (arg == "once" || arg == "always") |
|
665 { |
|
666 retval = set_warning_option ("", arg, nargout); |
|
667 done = true; |
|
668 } |
|
669 else if (arg == "debug") |
|
670 { |
4324
|
671 bind_builtin_variable ("debug_on_warning", true); |
3935
|
672 retval = set_warning_option ("", "", nargout); |
|
673 done = true; |
|
674 } |
|
675 } |
3934
|
676 } |
|
677 } |
|
678 |
3935
|
679 if (! done) |
|
680 { |
|
681 std::string prev_msg = Vlast_warning_message; |
|
682 |
|
683 std::string curr_msg |
|
684 = handle_message (warning, "unspecified warning", args); |
|
685 |
|
686 if (nargout > 0) |
|
687 retval(0) = Vlast_warning_message; |
|
688 } |
|
689 |
3934
|
690 return retval; |
|
691 } |
|
692 |
3935
|
693 DEFUN (lasterr, args, , |
|
694 "-*- texinfo -*-\n\ |
|
695 @deftypefn {Built-in Function} {} lasterr ()\n\ |
|
696 @deftypefnx {Built-in Function} {} lasterr (@var{msg})\n\ |
|
697 Without any arguments, return the last error message. With one\n\ |
5016
|
698 argument, set the last error message to @var{msg}.\n\ |
3935
|
699 @end deftypefn") |
|
700 { |
|
701 octave_value_list retval; |
|
702 |
|
703 int argc = args.length () + 1; |
|
704 |
5335
|
705 if (argc == 1 || argc == 2) |
|
706 { |
|
707 string_vector argv = args.make_argv ("lasterr"); |
3935
|
708 |
5335
|
709 if (! error_state) |
|
710 { |
|
711 if (argc == 1) |
|
712 retval(0) = Vlast_error_message; |
|
713 else |
|
714 Vlast_error_message = argv(1); |
|
715 } |
|
716 } |
3935
|
717 else |
|
718 print_usage ("lasterr"); |
|
719 |
|
720 return retval; |
|
721 } |
|
722 |
4699
|
723 // For backward compatibility. |
|
724 DEFALIAS (error_text, lasterr); |
|
725 DEFALIAS (__error_text__, lasterr); |
|
726 |
3934
|
727 DEFUN (lastwarn, args, , |
|
728 "-*- texinfo -*-\n\ |
|
729 @deftypefn {Built-in Function} {} lastwarn ()\n\ |
|
730 @deftypefnx {Built-in Function} {} lastwarn (@var{msg})\n\ |
3935
|
731 Without any arguments, return the last warning message. With one\n\ |
5016
|
732 argument, set the last warning message to @var{msg}.\n\ |
3934
|
733 @end deftypefn") |
|
734 { |
|
735 octave_value_list retval; |
|
736 |
|
737 int argc = args.length () + 1; |
|
738 |
|
739 string_vector argv = args.make_argv ("lastwarn"); |
|
740 |
|
741 if (argc == 1) |
|
742 retval(0) = Vlast_warning_message; |
|
743 else if (argc == 2) |
|
744 Vlast_warning_message = argv(1); |
|
745 else |
|
746 print_usage ("lastwarn"); |
|
747 |
|
748 return retval; |
897
|
749 } |
|
750 |
1957
|
751 DEFUN (usage, args, , |
3373
|
752 "-*- texinfo -*-\n\ |
|
753 @deftypefn {Built-in Function} {} usage (@var{msg})\n\ |
|
754 Print the message @var{msg}, prefixed by the string @samp{usage: }, and\n\ |
|
755 set Octave's internal error state such that control will return to the\n\ |
|
756 top level without evaluating any more commands. This is useful for\n\ |
|
757 aborting from functions.\n\ |
|
758 \n\ |
|
759 After @code{usage} is evaluated, Octave will print a traceback of all\n\ |
|
760 the function calls leading to the usage message.\n\ |
899
|
761 \n\ |
3373
|
762 You should use this function for reporting problems errors that result\n\ |
|
763 from an improper call to a function, such as calling a function with an\n\ |
|
764 incorrect number of arguments, or with arguments of the wrong type. For\n\ |
|
765 example, most functions distributed with Octave begin with code like\n\ |
|
766 this\n\ |
|
767 \n\ |
|
768 @example\n\ |
|
769 @group\n\ |
|
770 if (nargin != 2)\n\ |
|
771 usage (\"foo (a, b)\");\n\ |
|
772 endif\n\ |
|
773 @end group\n\ |
|
774 @end example\n\ |
|
775 \n\ |
|
776 @noindent\n\ |
|
777 to check for the proper number of arguments.\n\ |
|
778 @end deftypefn") |
899
|
779 { |
3934
|
780 octave_value_list retval; |
|
781 handle_message (usage, "unknown", args); |
|
782 return retval; |
899
|
783 } |
|
784 |
2174
|
785 static int |
|
786 beep_on_error (void) |
|
787 { |
|
788 Vbeep_on_error = check_preference ("beep_on_error"); |
|
789 |
|
790 return 0; |
|
791 } |
|
792 |
3707
|
793 static int |
|
794 debug_on_error (void) |
|
795 { |
|
796 Vdebug_on_error = check_preference ("debug_on_error"); |
|
797 |
|
798 return 0; |
|
799 } |
|
800 |
|
801 static int |
|
802 debug_on_warning (void) |
|
803 { |
|
804 Vdebug_on_warning = check_preference ("debug_on_warning"); |
|
805 |
|
806 return 0; |
|
807 } |
|
808 |
2174
|
809 void |
|
810 symbols_of_error (void) |
|
811 { |
4233
|
812 DEFVAR (beep_on_error, false, beep_on_error, |
3373
|
813 "-*- texinfo -*-\n\ |
|
814 @defvr {Built-in Variable} beep_on_error\n\ |
|
815 If the value of @code{beep_on_error} is nonzero, Octave will try\n\ |
|
816 to ring your terminal's bell before printing an error message. The\n\ |
|
817 default value is 0.\n\ |
|
818 @end defvr"); |
3018
|
819 |
4233
|
820 DEFVAR (debug_on_error, false, debug_on_error, |
3707
|
821 "-*- texinfo -*-\n\ |
|
822 @defvr {Built-in Variable} debug_on_error\n\ |
|
823 If the value of @code{debug_on_error} is nonzero, Octave will try\n\ |
|
824 to enter the debugger when an error is encountered. This will also\n\ |
|
825 inhibit printing of the normal traceback message (you will only see\n\ |
|
826 the top-level error message). The default value is 0.\n\ |
|
827 @end defvr"); |
|
828 |
4233
|
829 DEFVAR (debug_on_warning, false, debug_on_warning, |
3707
|
830 "-*- texinfo -*-\n\ |
|
831 @defvr {Built-in Variable} debug_on_warning\n\ |
|
832 If the value of @code{debug_on_warning} is nonzero, Octave will try\n\ |
|
833 to enter the debugger when a warning is encountered. The default\n\ |
|
834 value is 0.\n\ |
|
835 @end defvr"); |
2174
|
836 } |
|
837 |
1
|
838 /* |
|
839 ;;; Local Variables: *** |
|
840 ;;; mode: C++ *** |
|
841 ;;; End: *** |
|
842 */ |