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