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