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