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