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" |
1742
|
35 #include "pager.h" |
1352
|
36 #include "oct-obj.h" |
|
37 #include "utils.h" |
2370
|
38 #include "ov.h" |
|
39 #include "variables.h" |
1
|
40 |
2174
|
41 // TRUE means that Octave will try to beep obnoxiously before printing |
|
42 // error messages. |
|
43 static bool Vbeep_on_error; |
|
44 |
143
|
45 // Current error state. |
672
|
46 int error_state = 0; |
|
47 |
3489
|
48 // Current warning state. |
|
49 int warning_state = 0; |
|
50 |
1489
|
51 // Tell the error handler whether to print messages, or just store |
|
52 // them for later. Used for handling errors in eval() and |
|
53 // the `unwind_protect' statement. |
3136
|
54 bool buffer_error_messages = false; |
1489
|
55 |
|
56 // The message buffer |
3523
|
57 std::ostrstream *error_message_buffer = 0; |
143
|
58 |
3491
|
59 // Warning messages are never buffered. |
|
60 // XXX FIXME XXX -- we should provide another way to turn them off... |
|
61 |
|
62 static void |
|
63 vwarning (const char *name, const char *fmt, va_list args) |
|
64 { |
|
65 flush_octave_stdout (); |
|
66 |
3523
|
67 std::ostrstream output_buf; |
3491
|
68 |
|
69 if (name) |
|
70 { |
|
71 octave_diary << name << ": "; |
3531
|
72 std::cerr << name << ": "; |
3491
|
73 } |
|
74 |
3620
|
75 octave_vformat (octave_diary, fmt, args); |
|
76 octave_vformat (std::cerr, fmt, args); |
3491
|
77 |
3538
|
78 octave_diary << std::endl; |
|
79 std::cerr << std::endl; |
3491
|
80 } |
|
81 |
1
|
82 static void |
|
83 verror (const char *name, const char *fmt, va_list args) |
|
84 { |
3585
|
85 if (! buffer_error_messages) |
|
86 flush_octave_stdout (); |
914
|
87 |
2174
|
88 bool to_beep_or_not_to_beep_p = Vbeep_on_error && ! error_state; |
1423
|
89 |
3523
|
90 std::ostrstream output_buf; |
1
|
91 |
2174
|
92 if (to_beep_or_not_to_beep_p) |
1423
|
93 output_buf << "\a"; |
3620
|
94 |
599
|
95 if (name) |
|
96 output_buf << name << ": "; |
3620
|
97 |
|
98 octave_vformat (output_buf, fmt, args); |
|
99 |
3538
|
100 output_buf << std::endl << std::ends; |
581
|
101 |
|
102 char *msg = output_buf.str (); |
|
103 |
1489
|
104 if (buffer_error_messages) |
|
105 { |
|
106 char *ptr = msg; |
|
107 |
|
108 if (! error_message_buffer) |
|
109 { |
3548
|
110 error_message_buffer = new std::ostrstream; |
1489
|
111 |
|
112 // XXX FIXME XXX -- this is ugly, but it prevents |
|
113 // |
|
114 // eval ("error (\"msg\")", "error (__error_text__)"); |
|
115 // |
|
116 // from printing `error: ' twice. Assumes that the NAME we |
|
117 // have been given doesn't contain `:'. |
|
118 |
3162
|
119 ptr = strchr (msg, ':'); |
|
120 |
|
121 if (ptr) |
|
122 { |
|
123 if (*++ptr != '\0') |
|
124 ++ptr; |
|
125 } |
|
126 else |
|
127 ptr = msg; |
1489
|
128 } |
|
129 |
|
130 *error_message_buffer << ptr; |
|
131 } |
|
132 else |
|
133 { |
2095
|
134 octave_diary << msg; |
3531
|
135 std::cerr << msg; |
1489
|
136 } |
581
|
137 |
|
138 delete [] msg; |
1
|
139 } |
|
140 |
1266
|
141 // Note that we don't actually print any message if the error string |
|
142 // is just "" or "\n". This allows error ("") and error ("\n") to |
|
143 // just set the error state. |
|
144 |
1005
|
145 static void |
|
146 error_1 (const char *name, const char *fmt, va_list args) |
|
147 { |
|
148 if (error_state != -2) |
|
149 { |
1489
|
150 if (fmt) |
1005
|
151 { |
1489
|
152 if (*fmt) |
1005
|
153 { |
1489
|
154 int len = strlen (fmt); |
|
155 if (fmt[len - 1] == '\n') |
1266
|
156 { |
1489
|
157 if (len > 1) |
1266
|
158 { |
1489
|
159 char *tmp_fmt = strsave (fmt); |
|
160 tmp_fmt[len - 1] = '\0'; |
|
161 verror (name, tmp_fmt, args); |
|
162 delete [] tmp_fmt; |
|
163 } |
1423
|
164 |
1489
|
165 error_state = -2; |
1266
|
166 } |
1489
|
167 else |
|
168 verror (name, fmt, args); |
1005
|
169 } |
|
170 } |
1489
|
171 else |
|
172 panic ("error_1: invalid format"); |
1423
|
173 |
|
174 if (! error_state) |
|
175 error_state = 1; |
1005
|
176 } |
|
177 } |
|
178 |
1
|
179 void |
|
180 message (const char *name, const char *fmt, ...) |
|
181 { |
|
182 va_list args; |
|
183 va_start (args, fmt); |
|
184 verror (name, fmt, args); |
|
185 va_end (args); |
|
186 } |
|
187 |
|
188 void |
|
189 usage (const char *fmt, ...) |
|
190 { |
|
191 va_list args; |
|
192 va_start (args, fmt); |
905
|
193 error_state = -1; |
1
|
194 verror ("usage", fmt, args); |
|
195 va_end (args); |
|
196 } |
|
197 |
|
198 void |
|
199 warning (const char *fmt, ...) |
|
200 { |
|
201 va_list args; |
|
202 va_start (args, fmt); |
3489
|
203 warning_state = 1; |
3491
|
204 vwarning ("warning", fmt, args); |
1
|
205 va_end (args); |
|
206 } |
|
207 |
|
208 void |
|
209 error (const char *fmt, ...) |
|
210 { |
|
211 va_list args; |
|
212 va_start (args, fmt); |
1005
|
213 error_1 ("error", fmt, args); |
|
214 va_end (args); |
|
215 } |
436
|
216 |
1005
|
217 void |
|
218 parse_error (const char *fmt, ...) |
|
219 { |
|
220 va_list args; |
|
221 va_start (args, fmt); |
|
222 error_1 (0, fmt, args); |
1
|
223 va_end (args); |
|
224 } |
|
225 |
189
|
226 void |
1
|
227 panic (const char *fmt, ...) |
|
228 { |
|
229 va_list args; |
|
230 va_start (args, fmt); |
3585
|
231 buffer_error_messages = false; |
1
|
232 verror ("panic", fmt, args); |
|
233 va_end (args); |
|
234 abort (); |
|
235 } |
|
236 |
1489
|
237 typedef void (*error_fun)(const char *, ...); |
|
238 |
2086
|
239 extern octave_value_list Fsprintf (const octave_value_list&, int); |
1489
|
240 |
2086
|
241 static octave_value_list |
|
242 handle_message (error_fun f, const char *msg, const octave_value_list& args) |
528
|
243 { |
2086
|
244 octave_value_list retval; |
528
|
245 |
3523
|
246 std::string tstr; |
1728
|
247 |
528
|
248 int nargin = args.length (); |
|
249 |
2745
|
250 if (nargin > 0) |
528
|
251 { |
3066
|
252 octave_value arg; |
|
253 |
|
254 if (nargin > 1) |
|
255 { |
|
256 octave_value_list tmp = Fsprintf (args, 1); |
|
257 arg = tmp(0); |
|
258 } |
|
259 else |
|
260 arg = args(0); |
2745
|
261 |
|
262 if (arg.is_defined ()) |
528
|
263 { |
2745
|
264 if (arg.is_string ()) |
|
265 { |
|
266 tstr = arg.string_value (); |
|
267 msg = tstr.c_str (); |
|
268 |
|
269 if (! msg) |
|
270 return retval; |
|
271 } |
|
272 else if (arg.is_empty ()) |
528
|
273 return retval; |
|
274 } |
|
275 } |
|
276 |
1489
|
277 // Ugh. |
|
278 |
|
279 int len = strlen (msg); |
|
280 if (msg[len - 1] == '\n') |
|
281 { |
|
282 if (len > 1) |
|
283 { |
|
284 char *tmp_msg = strsave (msg); |
|
285 tmp_msg[len - 1] = '\0'; |
|
286 f ("%s\n", tmp_msg); |
|
287 delete [] tmp_msg; |
|
288 } |
|
289 } |
|
290 else |
|
291 f ("%s", msg); |
528
|
292 |
|
293 return retval; |
|
294 } |
|
295 |
1957
|
296 DEFUN (error, args, , |
3373
|
297 "-*- texinfo -*-\n\ |
|
298 @deftypefn {Built-in Function} {} error (@var{template}, @dots{})\n\ |
|
299 The @code{error} function formats the optional arguments under the\n\ |
|
300 control of the template string @var{template} using the same rules as\n\ |
|
301 the @code{printf} family of functions (@pxref{Formatted Output}).\n\ |
|
302 The resulting message is prefixed by the string @samp{error: } and\n\ |
|
303 printed on the @code{stderr} stream.\n\ |
|
304 \n\ |
|
305 Calling @code{error} also sets Octave's internal error state such that\n\ |
|
306 control will return to the top level without evaluating any more\n\ |
|
307 commands. This is useful for aborting from functions or scripts.\n\ |
897
|
308 \n\ |
3373
|
309 If the error message does not end with a new line character, Octave will\n\ |
|
310 print a traceback of all the function calls leading to the error. For\n\ |
|
311 example, given the following function definitions:\n\ |
|
312 \n\ |
|
313 @example\n\ |
|
314 @group\n\ |
|
315 function f () g () end\n\ |
|
316 function g () h () end\n\ |
|
317 function h () nargin == 1 || error (\"nargin != 1\"); end\n\ |
|
318 @end group\n\ |
|
319 @end example\n\ |
1489
|
320 \n\ |
3373
|
321 @noindent\n\ |
|
322 calling the function @code{f} will result in a list of messages that\n\ |
|
323 can help you to quickly locate the exact location of the error:\n\ |
1489
|
324 \n\ |
3373
|
325 @example\n\ |
|
326 @group\n\ |
|
327 f ()\n\ |
|
328 error: nargin != 1\n\ |
|
329 error: evaluating index expression near line 1, column 30\n\ |
|
330 error: evaluating binary operator `||' near line 1, column 27\n\ |
|
331 error: called from `h'\n\ |
|
332 error: called from `g'\n\ |
|
333 error: called from `f'\n\ |
|
334 @end group\n\ |
|
335 @end example\n\ |
|
336 \n\ |
|
337 If the error message ends in a new line character, Octave will print the\n\ |
|
338 message but will not display any traceback messages as it returns\n\ |
|
339 control to the top level. For example, modifying the error message\n\ |
|
340 in the previous example to end in a new line causes Octave to only print\n\ |
|
341 a single message:\n\ |
|
342 \n\ |
|
343 @example\n\ |
|
344 @group\n\ |
|
345 function h () nargin == 1 || error (\"nargin != 1\\n\"); end\n\ |
|
346 f ()\n\ |
|
347 error: nargin != 1\n\ |
|
348 @end group\n\ |
|
349 @end example\n\ |
|
350 @end deftypefn") |
897
|
351 { |
1489
|
352 return handle_message (error, "unspecified error", args); |
|
353 } |
897
|
354 |
1957
|
355 DEFUN (warning, args, , |
3373
|
356 "-*- texinfo -*-\n\ |
|
357 @deftypefn {Built-in Function} {} warning (@var{msg})\n\ |
|
358 Print a warning message @var{msg} prefixed by the string @samp{warning: }. \n\ |
|
359 After printing the warning message, Octave will continue to execute\n\ |
3600
|
360 commands. You should use this function when you want to notify the user\n\ |
|
361 of an unusual condition, but only when it makes sense for your program\n\ |
|
362 to go on.\n\ |
3373
|
363 @end deftypefn") |
1489
|
364 { |
|
365 return handle_message (warning, "unspecified warning", args); |
897
|
366 } |
|
367 |
1957
|
368 DEFUN (usage, args, , |
3373
|
369 "-*- texinfo -*-\n\ |
|
370 @deftypefn {Built-in Function} {} usage (@var{msg})\n\ |
|
371 Print the message @var{msg}, prefixed by the string @samp{usage: }, and\n\ |
|
372 set Octave's internal error state such that control will return to the\n\ |
|
373 top level without evaluating any more commands. This is useful for\n\ |
|
374 aborting from functions.\n\ |
|
375 \n\ |
|
376 After @code{usage} is evaluated, Octave will print a traceback of all\n\ |
|
377 the function calls leading to the usage message.\n\ |
899
|
378 \n\ |
3373
|
379 You should use this function for reporting problems errors that result\n\ |
|
380 from an improper call to a function, such as calling a function with an\n\ |
|
381 incorrect number of arguments, or with arguments of the wrong type. For\n\ |
|
382 example, most functions distributed with Octave begin with code like\n\ |
|
383 this\n\ |
|
384 \n\ |
|
385 @example\n\ |
|
386 @group\n\ |
|
387 if (nargin != 2)\n\ |
|
388 usage (\"foo (a, b)\");\n\ |
|
389 endif\n\ |
|
390 @end group\n\ |
|
391 @end example\n\ |
|
392 \n\ |
|
393 @noindent\n\ |
|
394 to check for the proper number of arguments.\n\ |
|
395 @end deftypefn") |
899
|
396 { |
1489
|
397 return handle_message (usage, "unknown", args); |
899
|
398 } |
|
399 |
3018
|
400 void |
|
401 bind_global_error_variable (void) |
|
402 { |
3490
|
403 if (error_message_buffer) |
|
404 { |
3538
|
405 *error_message_buffer << std::ends; |
3018
|
406 |
3490
|
407 char *error_text = error_message_buffer->str (); |
3018
|
408 |
3490
|
409 bind_builtin_constant ("__error_text__", error_text, true); |
3018
|
410 |
3490
|
411 delete [] error_text; |
|
412 |
|
413 delete error_message_buffer; |
3018
|
414 |
3490
|
415 error_message_buffer = 0; |
|
416 } |
|
417 else |
|
418 bind_builtin_constant ("__error_text__", "", true); |
3018
|
419 } |
|
420 |
|
421 void |
|
422 clear_global_error_variable (void *) |
|
423 { |
|
424 delete error_message_buffer; |
|
425 error_message_buffer = 0; |
|
426 |
3259
|
427 bind_builtin_constant ("__error_text__", "", true); |
3018
|
428 } |
|
429 |
2174
|
430 static int |
|
431 beep_on_error (void) |
|
432 { |
|
433 Vbeep_on_error = check_preference ("beep_on_error"); |
|
434 |
|
435 return 0; |
|
436 } |
|
437 |
|
438 void |
|
439 symbols_of_error (void) |
|
440 { |
3258
|
441 DEFVAR (beep_on_error, 0.0, beep_on_error, |
3373
|
442 "-*- texinfo -*-\n\ |
|
443 @defvr {Built-in Variable} beep_on_error\n\ |
|
444 If the value of @code{beep_on_error} is nonzero, Octave will try\n\ |
|
445 to ring your terminal's bell before printing an error message. The\n\ |
|
446 default value is 0.\n\ |
|
447 @end defvr"); |
3018
|
448 |
3141
|
449 DEFCONST (error_text, "", |
3373
|
450 "-*- texinfo -*-\n\ |
|
451 @defvr {Built-in Variable} error_text\n\ |
|
452 This variable contains the text of error messages that would have\n\ |
|
453 been printed in the body of the most recent @code{unwind_protect} or\n\ |
|
454 @code{try} statement or the @var{try} part of the most recent call to\n\ |
|
455 the @code{eval} function. Outside of the @code{unwind_protect} and\n\ |
|
456 @code{try} statements or the @code{eval} function, or if no error has\n\ |
|
457 occurred within them, the value of @code{error_text} is guaranteed to be\n\ |
|
458 the empty string.\n\ |
|
459 \n\ |
|
460 Note that the message does not include the first @samp{error: } prefix,\n\ |
|
461 so that it may easily be passed to the @code{error} function without\n\ |
|
462 additional processing@footnote{Yes, it's a kluge, but it seems to be a\n\ |
|
463 reasonably useful one.}.\n\ |
|
464 \n\ |
3402
|
465 @xref{The try Statement}, and @ref{The unwind_protect Statement}.\n\ |
3373
|
466 @end defvr"); |
2174
|
467 } |
|
468 |
1
|
469 /* |
|
470 ;;; Local Variables: *** |
|
471 ;;; mode: C++ *** |
|
472 ;;; End: *** |
|
473 */ |