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