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