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