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 |
143
|
60 // Current error state. |
672
|
61 int error_state = 0; |
|
62 |
3489
|
63 // Current warning state. |
|
64 int warning_state = 0; |
|
65 |
1489
|
66 // Tell the error handler whether to print messages, or just store |
|
67 // them for later. Used for handling errors in eval() and |
|
68 // the `unwind_protect' statement. |
3136
|
69 bool buffer_error_messages = false; |
1489
|
70 |
3815
|
71 // TRUE means error messages are turned off. |
|
72 bool discard_error_messages = false; |
|
73 |
3811
|
74 // The message buffer. |
|
75 static std::ostrstream *error_message_buffer = 0; |
143
|
76 |
3491
|
77 // Warning messages are never buffered. |
|
78 // XXX FIXME XXX -- we should provide another way to turn them off... |
|
79 |
|
80 static void |
|
81 vwarning (const char *name, const char *fmt, va_list args) |
|
82 { |
|
83 flush_octave_stdout (); |
|
84 |
3523
|
85 std::ostrstream output_buf; |
3491
|
86 |
|
87 if (name) |
3761
|
88 output_buf << name << ": "; |
|
89 |
|
90 octave_vformat (output_buf, fmt, args); |
|
91 |
|
92 output_buf << std::endl << std::ends; |
3491
|
93 |
3761
|
94 char *msg = output_buf.str (); |
3491
|
95 |
3761
|
96 octave_diary << msg; |
|
97 std::cerr << msg; |
|
98 |
|
99 delete [] msg; |
3491
|
100 } |
|
101 |
1
|
102 static void |
|
103 verror (const char *name, const char *fmt, va_list args) |
|
104 { |
3815
|
105 if (discard_error_messages) |
|
106 return; |
|
107 |
3585
|
108 if (! buffer_error_messages) |
|
109 flush_octave_stdout (); |
914
|
110 |
2174
|
111 bool to_beep_or_not_to_beep_p = Vbeep_on_error && ! error_state; |
1423
|
112 |
3523
|
113 std::ostrstream output_buf; |
1
|
114 |
2174
|
115 if (to_beep_or_not_to_beep_p) |
1423
|
116 output_buf << "\a"; |
3620
|
117 |
599
|
118 if (name) |
|
119 output_buf << name << ": "; |
3620
|
120 |
|
121 octave_vformat (output_buf, fmt, args); |
|
122 |
3538
|
123 output_buf << std::endl << std::ends; |
581
|
124 |
|
125 char *msg = output_buf.str (); |
|
126 |
1489
|
127 if (buffer_error_messages) |
|
128 { |
|
129 char *ptr = msg; |
|
130 |
|
131 if (! error_message_buffer) |
|
132 { |
3548
|
133 error_message_buffer = new std::ostrstream; |
1489
|
134 |
|
135 // XXX FIXME XXX -- this is ugly, but it prevents |
|
136 // |
|
137 // eval ("error (\"msg\")", "error (__error_text__)"); |
|
138 // |
|
139 // from printing `error: ' twice. Assumes that the NAME we |
|
140 // have been given doesn't contain `:'. |
|
141 |
3162
|
142 ptr = strchr (msg, ':'); |
|
143 |
|
144 if (ptr) |
|
145 { |
|
146 if (*++ptr != '\0') |
|
147 ++ptr; |
|
148 } |
|
149 else |
|
150 ptr = msg; |
1489
|
151 } |
|
152 |
|
153 *error_message_buffer << ptr; |
|
154 } |
|
155 else |
|
156 { |
2095
|
157 octave_diary << msg; |
3531
|
158 std::cerr << msg; |
1489
|
159 } |
581
|
160 |
|
161 delete [] msg; |
1
|
162 } |
|
163 |
1266
|
164 // Note that we don't actually print any message if the error string |
|
165 // is just "" or "\n". This allows error ("") and error ("\n") to |
|
166 // just set the error state. |
|
167 |
1005
|
168 static void |
|
169 error_1 (const char *name, const char *fmt, va_list args) |
|
170 { |
|
171 if (error_state != -2) |
|
172 { |
1489
|
173 if (fmt) |
1005
|
174 { |
1489
|
175 if (*fmt) |
1005
|
176 { |
1489
|
177 int len = strlen (fmt); |
|
178 if (fmt[len - 1] == '\n') |
1266
|
179 { |
1489
|
180 if (len > 1) |
1266
|
181 { |
1489
|
182 char *tmp_fmt = strsave (fmt); |
|
183 tmp_fmt[len - 1] = '\0'; |
|
184 verror (name, tmp_fmt, args); |
|
185 delete [] tmp_fmt; |
|
186 } |
1423
|
187 |
1489
|
188 error_state = -2; |
1266
|
189 } |
1489
|
190 else |
|
191 verror (name, fmt, args); |
1005
|
192 } |
|
193 } |
1489
|
194 else |
|
195 panic ("error_1: invalid format"); |
1423
|
196 |
|
197 if (! error_state) |
|
198 error_state = 1; |
1005
|
199 } |
|
200 } |
|
201 |
1
|
202 void |
|
203 message (const char *name, const char *fmt, ...) |
|
204 { |
|
205 va_list args; |
|
206 va_start (args, fmt); |
|
207 verror (name, fmt, args); |
|
208 va_end (args); |
|
209 } |
|
210 |
|
211 void |
|
212 usage (const char *fmt, ...) |
|
213 { |
|
214 va_list args; |
|
215 va_start (args, fmt); |
905
|
216 error_state = -1; |
1
|
217 verror ("usage", fmt, args); |
|
218 va_end (args); |
|
219 } |
|
220 |
3707
|
221 static void |
3719
|
222 pr_where_2 (const char *fmt, va_list args) |
|
223 { |
|
224 if (fmt) |
|
225 { |
|
226 if (*fmt) |
|
227 { |
|
228 int len = strlen (fmt); |
|
229 if (fmt[len - 1] == '\n') |
|
230 { |
|
231 if (len > 1) |
|
232 { |
|
233 char *tmp_fmt = strsave (fmt); |
|
234 tmp_fmt[len - 1] = '\0'; |
|
235 verror (0, tmp_fmt, args); |
|
236 delete [] tmp_fmt; |
|
237 } |
|
238 } |
|
239 else |
|
240 verror (0, fmt, args); |
|
241 } |
|
242 } |
|
243 else |
|
244 panic ("pr_where_2: invalid format"); |
|
245 } |
|
246 |
|
247 static void |
3707
|
248 pr_where_1 (const char *fmt, ...) |
|
249 { |
|
250 va_list args; |
|
251 va_start (args, fmt); |
3719
|
252 pr_where_2 (fmt, args); |
3707
|
253 va_end (args); |
|
254 } |
|
255 |
|
256 static void |
3708
|
257 pr_where (const char *name) |
3707
|
258 { |
|
259 if (curr_statement) |
|
260 { |
3708
|
261 const char *f_nm = 0; |
3707
|
262 |
3708
|
263 if (curr_function) |
|
264 { |
|
265 std::string fcn_name = curr_function->function_name (); |
|
266 std::string file_name = curr_function->fcn_file_name (); |
|
267 |
|
268 f_nm = file_name.empty () ? fcn_name.c_str () : file_name.c_str (); |
|
269 } |
3707
|
270 |
|
271 int l = curr_statement->line (); |
|
272 int c = curr_statement->column (); |
|
273 |
3708
|
274 if (f_nm) |
|
275 pr_where_1 ("%s: in %s near line %d, column %d:", name, f_nm, l, c); |
|
276 else |
|
277 pr_where_1 ("%s: near line %d, column %d:", name, l, c); |
3707
|
278 |
3708
|
279 // XXX FIXME XXX -- Note that the column number is probably not |
|
280 // going to mean much here since the code is being reproduced |
|
281 // from the parse tree, and we are only showing one statement |
|
282 // even if there were multiple statements on the original source |
|
283 // line. |
3707
|
284 |
|
285 std::ostrstream output_buf; |
|
286 |
3764
|
287 output_buf << std::endl; |
3708
|
288 |
|
289 tree_print_code tpc (output_buf, ">>> "); |
3707
|
290 |
|
291 curr_statement->accept (tpc); |
|
292 |
3764
|
293 output_buf << std::endl << std::ends; |
3707
|
294 |
|
295 char *msg = output_buf.str (); |
|
296 |
3764
|
297 pr_where_1 ("%s", msg); |
3707
|
298 |
|
299 delete [] msg; |
|
300 } |
|
301 } |
|
302 |
1
|
303 void |
|
304 warning (const char *fmt, ...) |
|
305 { |
|
306 va_list args; |
|
307 va_start (args, fmt); |
3489
|
308 warning_state = 1; |
3491
|
309 vwarning ("warning", fmt, args); |
1
|
310 va_end (args); |
3707
|
311 |
3708
|
312 pr_where ("warning"); |
|
313 |
3707
|
314 if ((interactive || forced_interactive) |
|
315 && Vdebug_on_warning && curr_function) |
|
316 { |
|
317 unwind_protect_bool (Vdebug_on_warning); |
|
318 Vdebug_on_warning = false; |
|
319 |
|
320 do_keyboard (octave_value_list ()); |
|
321 |
|
322 unwind_protect::run (); |
|
323 } |
1
|
324 } |
|
325 |
|
326 void |
|
327 error (const char *fmt, ...) |
|
328 { |
3707
|
329 int init_state = error_state; |
|
330 |
1
|
331 va_list args; |
|
332 va_start (args, fmt); |
1005
|
333 error_1 ("error", fmt, args); |
|
334 va_end (args); |
3707
|
335 |
|
336 if ((interactive || forced_interactive) |
|
337 && Vdebug_on_error && init_state == 0 && curr_function) |
|
338 { |
|
339 unwind_protect_bool (Vdebug_on_error); |
|
340 Vdebug_on_error = false; |
|
341 |
3708
|
342 pr_where ("error"); |
3707
|
343 |
|
344 error_state = 0; |
|
345 |
|
346 do_keyboard (octave_value_list ()); |
|
347 |
|
348 unwind_protect::run (); |
|
349 } |
1005
|
350 } |
436
|
351 |
1005
|
352 void |
|
353 parse_error (const char *fmt, ...) |
|
354 { |
|
355 va_list args; |
|
356 va_start (args, fmt); |
|
357 error_1 (0, fmt, args); |
1
|
358 va_end (args); |
|
359 } |
|
360 |
189
|
361 void |
1
|
362 panic (const char *fmt, ...) |
|
363 { |
|
364 va_list args; |
|
365 va_start (args, fmt); |
3585
|
366 buffer_error_messages = false; |
3815
|
367 discard_error_messages = false; |
1
|
368 verror ("panic", fmt, args); |
|
369 va_end (args); |
|
370 abort (); |
|
371 } |
|
372 |
1489
|
373 typedef void (*error_fun)(const char *, ...); |
|
374 |
2086
|
375 extern octave_value_list Fsprintf (const octave_value_list&, int); |
1489
|
376 |
2086
|
377 static octave_value_list |
|
378 handle_message (error_fun f, const char *msg, const octave_value_list& args) |
528
|
379 { |
2086
|
380 octave_value_list retval; |
528
|
381 |
3523
|
382 std::string tstr; |
1728
|
383 |
528
|
384 int nargin = args.length (); |
|
385 |
2745
|
386 if (nargin > 0) |
528
|
387 { |
3066
|
388 octave_value arg; |
|
389 |
|
390 if (nargin > 1) |
|
391 { |
|
392 octave_value_list tmp = Fsprintf (args, 1); |
|
393 arg = tmp(0); |
|
394 } |
|
395 else |
|
396 arg = args(0); |
2745
|
397 |
|
398 if (arg.is_defined ()) |
528
|
399 { |
2745
|
400 if (arg.is_string ()) |
|
401 { |
|
402 tstr = arg.string_value (); |
|
403 msg = tstr.c_str (); |
|
404 |
|
405 if (! msg) |
|
406 return retval; |
|
407 } |
|
408 else if (arg.is_empty ()) |
528
|
409 return retval; |
|
410 } |
|
411 } |
|
412 |
1489
|
413 // Ugh. |
|
414 |
|
415 int len = strlen (msg); |
|
416 if (msg[len - 1] == '\n') |
|
417 { |
|
418 if (len > 1) |
|
419 { |
|
420 char *tmp_msg = strsave (msg); |
|
421 tmp_msg[len - 1] = '\0'; |
|
422 f ("%s\n", tmp_msg); |
|
423 delete [] tmp_msg; |
|
424 } |
|
425 } |
|
426 else |
|
427 f ("%s", msg); |
528
|
428 |
|
429 return retval; |
|
430 } |
|
431 |
1957
|
432 DEFUN (error, args, , |
3373
|
433 "-*- texinfo -*-\n\ |
|
434 @deftypefn {Built-in Function} {} error (@var{template}, @dots{})\n\ |
|
435 The @code{error} function formats the optional arguments under the\n\ |
|
436 control of the template string @var{template} using the same rules as\n\ |
|
437 the @code{printf} family of functions (@pxref{Formatted Output}).\n\ |
|
438 The resulting message is prefixed by the string @samp{error: } and\n\ |
|
439 printed on the @code{stderr} stream.\n\ |
|
440 \n\ |
|
441 Calling @code{error} also sets Octave's internal error state such that\n\ |
|
442 control will return to the top level without evaluating any more\n\ |
|
443 commands. This is useful for aborting from functions or scripts.\n\ |
897
|
444 \n\ |
3373
|
445 If the error message does not end with a new line character, Octave will\n\ |
|
446 print a traceback of all the function calls leading to the error. For\n\ |
|
447 example, given the following function definitions:\n\ |
|
448 \n\ |
|
449 @example\n\ |
|
450 @group\n\ |
|
451 function f () g () end\n\ |
|
452 function g () h () end\n\ |
|
453 function h () nargin == 1 || error (\"nargin != 1\"); end\n\ |
|
454 @end group\n\ |
|
455 @end example\n\ |
1489
|
456 \n\ |
3373
|
457 @noindent\n\ |
|
458 calling the function @code{f} will result in a list of messages that\n\ |
|
459 can help you to quickly locate the exact location of the error:\n\ |
1489
|
460 \n\ |
3373
|
461 @example\n\ |
|
462 @group\n\ |
|
463 f ()\n\ |
|
464 error: nargin != 1\n\ |
|
465 error: evaluating index expression near line 1, column 30\n\ |
|
466 error: evaluating binary operator `||' near line 1, column 27\n\ |
|
467 error: called from `h'\n\ |
|
468 error: called from `g'\n\ |
|
469 error: called from `f'\n\ |
|
470 @end group\n\ |
|
471 @end example\n\ |
|
472 \n\ |
|
473 If the error message ends in a new line character, Octave will print the\n\ |
|
474 message but will not display any traceback messages as it returns\n\ |
|
475 control to the top level. For example, modifying the error message\n\ |
|
476 in the previous example to end in a new line causes Octave to only print\n\ |
|
477 a single message:\n\ |
|
478 \n\ |
|
479 @example\n\ |
|
480 @group\n\ |
|
481 function h () nargin == 1 || error (\"nargin != 1\\n\"); end\n\ |
|
482 f ()\n\ |
|
483 error: nargin != 1\n\ |
|
484 @end group\n\ |
|
485 @end example\n\ |
|
486 @end deftypefn") |
897
|
487 { |
1489
|
488 return handle_message (error, "unspecified error", args); |
|
489 } |
897
|
490 |
1957
|
491 DEFUN (warning, args, , |
3373
|
492 "-*- texinfo -*-\n\ |
|
493 @deftypefn {Built-in Function} {} warning (@var{msg})\n\ |
|
494 Print a warning message @var{msg} prefixed by the string @samp{warning: }. \n\ |
|
495 After printing the warning message, Octave will continue to execute\n\ |
3600
|
496 commands. You should use this function when you want to notify the user\n\ |
|
497 of an unusual condition, but only when it makes sense for your program\n\ |
|
498 to go on.\n\ |
3373
|
499 @end deftypefn") |
1489
|
500 { |
|
501 return handle_message (warning, "unspecified warning", args); |
897
|
502 } |
|
503 |
1957
|
504 DEFUN (usage, args, , |
3373
|
505 "-*- texinfo -*-\n\ |
|
506 @deftypefn {Built-in Function} {} usage (@var{msg})\n\ |
|
507 Print the message @var{msg}, prefixed by the string @samp{usage: }, and\n\ |
|
508 set Octave's internal error state such that control will return to the\n\ |
|
509 top level without evaluating any more commands. This is useful for\n\ |
|
510 aborting from functions.\n\ |
|
511 \n\ |
|
512 After @code{usage} is evaluated, Octave will print a traceback of all\n\ |
|
513 the function calls leading to the usage message.\n\ |
899
|
514 \n\ |
3373
|
515 You should use this function for reporting problems errors that result\n\ |
|
516 from an improper call to a function, such as calling a function with an\n\ |
|
517 incorrect number of arguments, or with arguments of the wrong type. For\n\ |
|
518 example, most functions distributed with Octave begin with code like\n\ |
|
519 this\n\ |
|
520 \n\ |
|
521 @example\n\ |
|
522 @group\n\ |
|
523 if (nargin != 2)\n\ |
|
524 usage (\"foo (a, b)\");\n\ |
|
525 endif\n\ |
|
526 @end group\n\ |
|
527 @end example\n\ |
|
528 \n\ |
|
529 @noindent\n\ |
|
530 to check for the proper number of arguments.\n\ |
|
531 @end deftypefn") |
899
|
532 { |
1489
|
533 return handle_message (usage, "unknown", args); |
899
|
534 } |
|
535 |
3018
|
536 void |
|
537 bind_global_error_variable (void) |
|
538 { |
3490
|
539 if (error_message_buffer) |
|
540 { |
3538
|
541 *error_message_buffer << std::ends; |
3018
|
542 |
3490
|
543 char *error_text = error_message_buffer->str (); |
3018
|
544 |
3811
|
545 bind_builtin_variable ("__error_text__", error_text, true); |
3018
|
546 |
3490
|
547 delete [] error_text; |
|
548 |
|
549 delete error_message_buffer; |
3018
|
550 |
3490
|
551 error_message_buffer = 0; |
|
552 } |
|
553 else |
3811
|
554 bind_builtin_variable ("__error_text__", "", true); |
3018
|
555 } |
|
556 |
|
557 void |
|
558 clear_global_error_variable (void *) |
|
559 { |
|
560 delete error_message_buffer; |
3811
|
561 |
3018
|
562 error_message_buffer = 0; |
|
563 |
3811
|
564 bind_builtin_variable ("__error_text__", "", true); |
3018
|
565 } |
|
566 |
2174
|
567 static int |
|
568 beep_on_error (void) |
|
569 { |
|
570 Vbeep_on_error = check_preference ("beep_on_error"); |
|
571 |
|
572 return 0; |
|
573 } |
|
574 |
3707
|
575 static int |
|
576 debug_on_error (void) |
|
577 { |
|
578 Vdebug_on_error = check_preference ("debug_on_error"); |
|
579 |
|
580 return 0; |
|
581 } |
|
582 |
|
583 static int |
|
584 debug_on_warning (void) |
|
585 { |
|
586 Vdebug_on_warning = check_preference ("debug_on_warning"); |
|
587 |
|
588 return 0; |
|
589 } |
|
590 |
2174
|
591 void |
|
592 symbols_of_error (void) |
|
593 { |
3258
|
594 DEFVAR (beep_on_error, 0.0, beep_on_error, |
3373
|
595 "-*- texinfo -*-\n\ |
|
596 @defvr {Built-in Variable} beep_on_error\n\ |
|
597 If the value of @code{beep_on_error} is nonzero, Octave will try\n\ |
|
598 to ring your terminal's bell before printing an error message. The\n\ |
|
599 default value is 0.\n\ |
|
600 @end defvr"); |
3018
|
601 |
3707
|
602 DEFVAR (debug_on_error, 0.0, debug_on_error, |
|
603 "-*- texinfo -*-\n\ |
|
604 @defvr {Built-in Variable} debug_on_error\n\ |
|
605 If the value of @code{debug_on_error} is nonzero, Octave will try\n\ |
|
606 to enter the debugger when an error is encountered. This will also\n\ |
|
607 inhibit printing of the normal traceback message (you will only see\n\ |
|
608 the top-level error message). The default value is 0.\n\ |
|
609 @end defvr"); |
|
610 |
|
611 DEFVAR (debug_on_warning, 0.0, debug_on_warning, |
|
612 "-*- texinfo -*-\n\ |
|
613 @defvr {Built-in Variable} debug_on_warning\n\ |
|
614 If the value of @code{debug_on_warning} is nonzero, Octave will try\n\ |
|
615 to enter the debugger when a warning is encountered. The default\n\ |
|
616 value is 0.\n\ |
|
617 @end defvr"); |
|
618 |
3141
|
619 DEFCONST (error_text, "", |
3373
|
620 "-*- texinfo -*-\n\ |
|
621 @defvr {Built-in Variable} error_text\n\ |
|
622 This variable contains the text of error messages that would have\n\ |
|
623 been printed in the body of the most recent @code{unwind_protect} or\n\ |
|
624 @code{try} statement or the @var{try} part of the most recent call to\n\ |
|
625 the @code{eval} function. Outside of the @code{unwind_protect} and\n\ |
|
626 @code{try} statements or the @code{eval} function, or if no error has\n\ |
|
627 occurred within them, the value of @code{error_text} is guaranteed to be\n\ |
|
628 the empty string.\n\ |
|
629 \n\ |
|
630 Note that the message does not include the first @samp{error: } prefix,\n\ |
|
631 so that it may easily be passed to the @code{error} function without\n\ |
|
632 additional processing@footnote{Yes, it's a kluge, but it seems to be a\n\ |
|
633 reasonably useful one.}.\n\ |
|
634 \n\ |
3402
|
635 @xref{The try Statement}, and @ref{The unwind_protect Statement}.\n\ |
3373
|
636 @end defvr"); |
2174
|
637 } |
|
638 |
1
|
639 /* |
|
640 ;;; Local Variables: *** |
|
641 ;;; mode: C++ *** |
|
642 ;;; End: *** |
|
643 */ |