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