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 |
|
214 pr_where_1 (const char *fmt, ...) |
|
215 { |
|
216 va_list args; |
|
217 va_start (args, fmt); |
|
218 error_1 (0, fmt, args); |
|
219 va_end (args); |
|
220 } |
|
221 |
|
222 static void |
3708
|
223 pr_where (const char *name) |
3707
|
224 { |
|
225 if (curr_statement) |
|
226 { |
3708
|
227 const char *f_nm = 0; |
3707
|
228 |
3708
|
229 if (curr_function) |
|
230 { |
|
231 std::string fcn_name = curr_function->function_name (); |
|
232 std::string file_name = curr_function->fcn_file_name (); |
|
233 |
|
234 f_nm = file_name.empty () ? fcn_name.c_str () : file_name.c_str (); |
|
235 } |
3707
|
236 |
|
237 int l = curr_statement->line (); |
|
238 int c = curr_statement->column (); |
|
239 |
3708
|
240 if (f_nm) |
|
241 pr_where_1 ("%s: in %s near line %d, column %d:", name, f_nm, l, c); |
|
242 else |
|
243 pr_where_1 ("%s: near line %d, column %d:", name, l, c); |
3707
|
244 |
3708
|
245 // XXX FIXME XXX -- Note that the column number is probably not |
|
246 // going to mean much here since the code is being reproduced |
|
247 // from the parse tree, and we are only showing one statement |
|
248 // even if there were multiple statements on the original source |
|
249 // line. |
3707
|
250 |
|
251 std::ostrstream output_buf; |
|
252 |
3708
|
253 output_buf << "\n"; |
|
254 |
|
255 tree_print_code tpc (output_buf, ">>> "); |
3707
|
256 |
|
257 curr_statement->accept (tpc); |
|
258 |
3708
|
259 output_buf << "\n" << ends; |
3707
|
260 |
|
261 char *msg = output_buf.str (); |
|
262 |
|
263 pr_where_1 (msg); |
|
264 |
|
265 delete [] msg; |
|
266 } |
|
267 } |
|
268 |
1
|
269 void |
|
270 warning (const char *fmt, ...) |
|
271 { |
|
272 va_list args; |
|
273 va_start (args, fmt); |
3489
|
274 warning_state = 1; |
3491
|
275 vwarning ("warning", fmt, args); |
1
|
276 va_end (args); |
3707
|
277 |
3708
|
278 pr_where ("warning"); |
|
279 |
3707
|
280 if ((interactive || forced_interactive) |
|
281 && Vdebug_on_warning && curr_function) |
|
282 { |
|
283 unwind_protect_bool (Vdebug_on_warning); |
|
284 Vdebug_on_warning = false; |
|
285 |
|
286 do_keyboard (octave_value_list ()); |
|
287 |
|
288 unwind_protect::run (); |
|
289 } |
1
|
290 } |
|
291 |
|
292 void |
|
293 error (const char *fmt, ...) |
|
294 { |
3707
|
295 int init_state = error_state; |
|
296 |
1
|
297 va_list args; |
|
298 va_start (args, fmt); |
1005
|
299 error_1 ("error", fmt, args); |
|
300 va_end (args); |
3707
|
301 |
|
302 if ((interactive || forced_interactive) |
|
303 && Vdebug_on_error && init_state == 0 && curr_function) |
|
304 { |
|
305 unwind_protect_bool (Vdebug_on_error); |
|
306 Vdebug_on_error = false; |
|
307 |
3708
|
308 pr_where ("error"); |
3707
|
309 |
|
310 error_state = 0; |
|
311 |
|
312 do_keyboard (octave_value_list ()); |
|
313 |
|
314 unwind_protect::run (); |
|
315 } |
1005
|
316 } |
436
|
317 |
1005
|
318 void |
|
319 parse_error (const char *fmt, ...) |
|
320 { |
|
321 va_list args; |
|
322 va_start (args, fmt); |
|
323 error_1 (0, fmt, args); |
1
|
324 va_end (args); |
|
325 } |
|
326 |
189
|
327 void |
1
|
328 panic (const char *fmt, ...) |
|
329 { |
|
330 va_list args; |
|
331 va_start (args, fmt); |
3585
|
332 buffer_error_messages = false; |
1
|
333 verror ("panic", fmt, args); |
|
334 va_end (args); |
|
335 abort (); |
|
336 } |
|
337 |
1489
|
338 typedef void (*error_fun)(const char *, ...); |
|
339 |
2086
|
340 extern octave_value_list Fsprintf (const octave_value_list&, int); |
1489
|
341 |
2086
|
342 static octave_value_list |
|
343 handle_message (error_fun f, const char *msg, const octave_value_list& args) |
528
|
344 { |
2086
|
345 octave_value_list retval; |
528
|
346 |
3523
|
347 std::string tstr; |
1728
|
348 |
528
|
349 int nargin = args.length (); |
|
350 |
2745
|
351 if (nargin > 0) |
528
|
352 { |
3066
|
353 octave_value arg; |
|
354 |
|
355 if (nargin > 1) |
|
356 { |
|
357 octave_value_list tmp = Fsprintf (args, 1); |
|
358 arg = tmp(0); |
|
359 } |
|
360 else |
|
361 arg = args(0); |
2745
|
362 |
|
363 if (arg.is_defined ()) |
528
|
364 { |
2745
|
365 if (arg.is_string ()) |
|
366 { |
|
367 tstr = arg.string_value (); |
|
368 msg = tstr.c_str (); |
|
369 |
|
370 if (! msg) |
|
371 return retval; |
|
372 } |
|
373 else if (arg.is_empty ()) |
528
|
374 return retval; |
|
375 } |
|
376 } |
|
377 |
1489
|
378 // Ugh. |
|
379 |
|
380 int len = strlen (msg); |
|
381 if (msg[len - 1] == '\n') |
|
382 { |
|
383 if (len > 1) |
|
384 { |
|
385 char *tmp_msg = strsave (msg); |
|
386 tmp_msg[len - 1] = '\0'; |
|
387 f ("%s\n", tmp_msg); |
|
388 delete [] tmp_msg; |
|
389 } |
|
390 } |
|
391 else |
|
392 f ("%s", msg); |
528
|
393 |
|
394 return retval; |
|
395 } |
|
396 |
1957
|
397 DEFUN (error, args, , |
3373
|
398 "-*- texinfo -*-\n\ |
|
399 @deftypefn {Built-in Function} {} error (@var{template}, @dots{})\n\ |
|
400 The @code{error} function formats the optional arguments under the\n\ |
|
401 control of the template string @var{template} using the same rules as\n\ |
|
402 the @code{printf} family of functions (@pxref{Formatted Output}).\n\ |
|
403 The resulting message is prefixed by the string @samp{error: } and\n\ |
|
404 printed on the @code{stderr} stream.\n\ |
|
405 \n\ |
|
406 Calling @code{error} also sets Octave's internal error state such that\n\ |
|
407 control will return to the top level without evaluating any more\n\ |
|
408 commands. This is useful for aborting from functions or scripts.\n\ |
897
|
409 \n\ |
3373
|
410 If the error message does not end with a new line character, Octave will\n\ |
|
411 print a traceback of all the function calls leading to the error. For\n\ |
|
412 example, given the following function definitions:\n\ |
|
413 \n\ |
|
414 @example\n\ |
|
415 @group\n\ |
|
416 function f () g () end\n\ |
|
417 function g () h () end\n\ |
|
418 function h () nargin == 1 || error (\"nargin != 1\"); end\n\ |
|
419 @end group\n\ |
|
420 @end example\n\ |
1489
|
421 \n\ |
3373
|
422 @noindent\n\ |
|
423 calling the function @code{f} will result in a list of messages that\n\ |
|
424 can help you to quickly locate the exact location of the error:\n\ |
1489
|
425 \n\ |
3373
|
426 @example\n\ |
|
427 @group\n\ |
|
428 f ()\n\ |
|
429 error: nargin != 1\n\ |
|
430 error: evaluating index expression near line 1, column 30\n\ |
|
431 error: evaluating binary operator `||' near line 1, column 27\n\ |
|
432 error: called from `h'\n\ |
|
433 error: called from `g'\n\ |
|
434 error: called from `f'\n\ |
|
435 @end group\n\ |
|
436 @end example\n\ |
|
437 \n\ |
|
438 If the error message ends in a new line character, Octave will print the\n\ |
|
439 message but will not display any traceback messages as it returns\n\ |
|
440 control to the top level. For example, modifying the error message\n\ |
|
441 in the previous example to end in a new line causes Octave to only print\n\ |
|
442 a single message:\n\ |
|
443 \n\ |
|
444 @example\n\ |
|
445 @group\n\ |
|
446 function h () nargin == 1 || error (\"nargin != 1\\n\"); end\n\ |
|
447 f ()\n\ |
|
448 error: nargin != 1\n\ |
|
449 @end group\n\ |
|
450 @end example\n\ |
|
451 @end deftypefn") |
897
|
452 { |
1489
|
453 return handle_message (error, "unspecified error", args); |
|
454 } |
897
|
455 |
1957
|
456 DEFUN (warning, args, , |
3373
|
457 "-*- texinfo -*-\n\ |
|
458 @deftypefn {Built-in Function} {} warning (@var{msg})\n\ |
|
459 Print a warning message @var{msg} prefixed by the string @samp{warning: }. \n\ |
|
460 After printing the warning message, Octave will continue to execute\n\ |
3600
|
461 commands. You should use this function when you want to notify the user\n\ |
|
462 of an unusual condition, but only when it makes sense for your program\n\ |
|
463 to go on.\n\ |
3373
|
464 @end deftypefn") |
1489
|
465 { |
|
466 return handle_message (warning, "unspecified warning", args); |
897
|
467 } |
|
468 |
1957
|
469 DEFUN (usage, args, , |
3373
|
470 "-*- texinfo -*-\n\ |
|
471 @deftypefn {Built-in Function} {} usage (@var{msg})\n\ |
|
472 Print the message @var{msg}, prefixed by the string @samp{usage: }, and\n\ |
|
473 set Octave's internal error state such that control will return to the\n\ |
|
474 top level without evaluating any more commands. This is useful for\n\ |
|
475 aborting from functions.\n\ |
|
476 \n\ |
|
477 After @code{usage} is evaluated, Octave will print a traceback of all\n\ |
|
478 the function calls leading to the usage message.\n\ |
899
|
479 \n\ |
3373
|
480 You should use this function for reporting problems errors that result\n\ |
|
481 from an improper call to a function, such as calling a function with an\n\ |
|
482 incorrect number of arguments, or with arguments of the wrong type. For\n\ |
|
483 example, most functions distributed with Octave begin with code like\n\ |
|
484 this\n\ |
|
485 \n\ |
|
486 @example\n\ |
|
487 @group\n\ |
|
488 if (nargin != 2)\n\ |
|
489 usage (\"foo (a, b)\");\n\ |
|
490 endif\n\ |
|
491 @end group\n\ |
|
492 @end example\n\ |
|
493 \n\ |
|
494 @noindent\n\ |
|
495 to check for the proper number of arguments.\n\ |
|
496 @end deftypefn") |
899
|
497 { |
1489
|
498 return handle_message (usage, "unknown", args); |
899
|
499 } |
|
500 |
3018
|
501 void |
|
502 bind_global_error_variable (void) |
|
503 { |
3490
|
504 if (error_message_buffer) |
|
505 { |
3538
|
506 *error_message_buffer << std::ends; |
3018
|
507 |
3490
|
508 char *error_text = error_message_buffer->str (); |
3018
|
509 |
3490
|
510 bind_builtin_constant ("__error_text__", error_text, true); |
3018
|
511 |
3490
|
512 delete [] error_text; |
|
513 |
|
514 delete error_message_buffer; |
3018
|
515 |
3490
|
516 error_message_buffer = 0; |
|
517 } |
|
518 else |
|
519 bind_builtin_constant ("__error_text__", "", true); |
3018
|
520 } |
|
521 |
|
522 void |
|
523 clear_global_error_variable (void *) |
|
524 { |
|
525 delete error_message_buffer; |
|
526 error_message_buffer = 0; |
|
527 |
3259
|
528 bind_builtin_constant ("__error_text__", "", true); |
3018
|
529 } |
|
530 |
2174
|
531 static int |
|
532 beep_on_error (void) |
|
533 { |
|
534 Vbeep_on_error = check_preference ("beep_on_error"); |
|
535 |
|
536 return 0; |
|
537 } |
|
538 |
3707
|
539 static int |
|
540 debug_on_error (void) |
|
541 { |
|
542 Vdebug_on_error = check_preference ("debug_on_error"); |
|
543 |
|
544 return 0; |
|
545 } |
|
546 |
|
547 static int |
|
548 debug_on_warning (void) |
|
549 { |
|
550 Vdebug_on_warning = check_preference ("debug_on_warning"); |
|
551 |
|
552 return 0; |
|
553 } |
|
554 |
2174
|
555 void |
|
556 symbols_of_error (void) |
|
557 { |
3258
|
558 DEFVAR (beep_on_error, 0.0, beep_on_error, |
3373
|
559 "-*- texinfo -*-\n\ |
|
560 @defvr {Built-in Variable} beep_on_error\n\ |
|
561 If the value of @code{beep_on_error} is nonzero, Octave will try\n\ |
|
562 to ring your terminal's bell before printing an error message. The\n\ |
|
563 default value is 0.\n\ |
|
564 @end defvr"); |
3018
|
565 |
3707
|
566 DEFVAR (debug_on_error, 0.0, debug_on_error, |
|
567 "-*- texinfo -*-\n\ |
|
568 @defvr {Built-in Variable} debug_on_error\n\ |
|
569 If the value of @code{debug_on_error} is nonzero, Octave will try\n\ |
|
570 to enter the debugger when an error is encountered. This will also\n\ |
|
571 inhibit printing of the normal traceback message (you will only see\n\ |
|
572 the top-level error message). The default value is 0.\n\ |
|
573 @end defvr"); |
|
574 |
|
575 DEFVAR (debug_on_warning, 0.0, debug_on_warning, |
|
576 "-*- texinfo -*-\n\ |
|
577 @defvr {Built-in Variable} debug_on_warning\n\ |
|
578 If the value of @code{debug_on_warning} is nonzero, Octave will try\n\ |
|
579 to enter the debugger when a warning is encountered. The default\n\ |
|
580 value is 0.\n\ |
|
581 @end defvr"); |
|
582 |
3141
|
583 DEFCONST (error_text, "", |
3373
|
584 "-*- texinfo -*-\n\ |
|
585 @defvr {Built-in Variable} error_text\n\ |
|
586 This variable contains the text of error messages that would have\n\ |
|
587 been printed in the body of the most recent @code{unwind_protect} or\n\ |
|
588 @code{try} statement or the @var{try} part of the most recent call to\n\ |
|
589 the @code{eval} function. Outside of the @code{unwind_protect} and\n\ |
|
590 @code{try} statements or the @code{eval} function, or if no error has\n\ |
|
591 occurred within them, the value of @code{error_text} is guaranteed to be\n\ |
|
592 the empty string.\n\ |
|
593 \n\ |
|
594 Note that the message does not include the first @samp{error: } prefix,\n\ |
|
595 so that it may easily be passed to the @code{error} function without\n\ |
|
596 additional processing@footnote{Yes, it's a kluge, but it seems to be a\n\ |
|
597 reasonably useful one.}.\n\ |
|
598 \n\ |
3402
|
599 @xref{The try Statement}, and @ref{The unwind_protect Statement}.\n\ |
3373
|
600 @end defvr"); |
2174
|
601 } |
|
602 |
1
|
603 /* |
|
604 ;;; Local Variables: *** |
|
605 ;;; mode: C++ *** |
|
606 ;;; End: *** |
|
607 */ |