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