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