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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
1
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
1
|
26 #endif |
|
27 |
1343
|
28 #include <cstdarg> |
1633
|
29 #include <cstring> |
1343
|
30 |
1728
|
31 #include <string> |
|
32 |
4051
|
33 #include "lo-sstream.h" |
|
34 |
1352
|
35 #include "defun.h" |
1
|
36 #include "error.h" |
3707
|
37 #include "input.h" |
1742
|
38 #include "pager.h" |
1352
|
39 #include "oct-obj.h" |
5567
|
40 #include "oct-map.h" |
1352
|
41 #include "utils.h" |
2370
|
42 #include "ov.h" |
3707
|
43 #include "ov-usr-fcn.h" |
|
44 #include "pt-pr-code.h" |
|
45 #include "pt-stmt.h" |
|
46 #include "toplev.h" |
|
47 #include "unwind-prot.h" |
2370
|
48 #include "variables.h" |
1
|
49 |
2174
|
50 // TRUE means that Octave will try to beep obnoxiously before printing |
|
51 // error messages. |
|
52 static bool Vbeep_on_error; |
|
53 |
3707
|
54 // TRUE means that Octave will try to enter the debugger when an error |
|
55 // is encountered. This will also inhibit printing of the normal |
|
56 // traceback message (you will only see the top-level error message). |
|
57 static bool Vdebug_on_error; |
|
58 |
|
59 // TRUE means that Octave will try to enter the debugger when a warning |
|
60 // is encountered. |
|
61 static bool Vdebug_on_warning; |
|
62 |
5567
|
63 // TRUE means that Octave will try to display a stack trace when a |
|
64 // warning is encountered. |
|
65 static bool Vbacktrace_on_warning = false; |
|
66 |
|
67 // TRUE means that Octave will print a verbose warning. Currently unused. |
|
68 static bool Vverbose_warning; |
|
69 |
|
70 // A structure containing (most of) the current state of warnings. |
|
71 static Octave_map warning_options; |
|
72 |
3935
|
73 // The text of the last error message. |
|
74 static std::string Vlast_error_message; |
|
75 |
3934
|
76 // The text of the last warning message. |
|
77 static std::string Vlast_warning_message; |
|
78 |
5567
|
79 // The last warning message id. |
|
80 static std::string Vlast_warning_id; |
3934
|
81 |
5567
|
82 // The last error message id. |
|
83 static std::string Vlast_error_id; |
3934
|
84 |
143
|
85 // Current error state. |
3935
|
86 // |
|
87 // Valid values: |
|
88 // |
|
89 // -2: an error has occurred, but don't print any messages. |
|
90 // -1: an error has occurred, we are printing a traceback |
|
91 // 0: no error |
|
92 // 1: an error has occurred |
|
93 // |
672
|
94 int error_state = 0; |
|
95 |
3489
|
96 // Current warning state. |
3935
|
97 // |
|
98 // Valid values: |
|
99 // |
|
100 // 0: no warning |
|
101 // 1: a warning has occurred |
|
102 // |
3489
|
103 int warning_state = 0; |
|
104 |
1489
|
105 // Tell the error handler whether to print messages, or just store |
|
106 // them for later. Used for handling errors in eval() and |
|
107 // the `unwind_protect' statement. |
4699
|
108 int buffer_error_messages = 0; |
1489
|
109 |
3815
|
110 // TRUE means error messages are turned off. |
|
111 bool discard_error_messages = false; |
|
112 |
4452
|
113 // TRUE means warning messages are turned off. |
|
114 bool discard_warning_messages = false; |
|
115 |
3811
|
116 // The message buffer. |
4051
|
117 static OSSTREAM *error_message_buffer = 0; |
143
|
118 |
4318
|
119 void |
|
120 reset_error_handler (void) |
|
121 { |
|
122 error_state = 0; |
|
123 warning_state = 0; |
4699
|
124 buffer_error_messages = 0; |
4318
|
125 discard_error_messages = false; |
|
126 } |
|
127 |
5567
|
128 static void |
|
129 init_warning_options (const std::string& state = "on") |
|
130 { |
|
131 warning_options.clear (); |
|
132 |
|
133 warning_options.assign ("identifier", "all"); |
|
134 warning_options.assign ("state", state); |
|
135 } |
|
136 |
3491
|
137 // Warning messages are never buffered. |
|
138 |
|
139 static void |
5567
|
140 vwarning (const char *name, const char *id, const char *fmt, va_list args) |
3491
|
141 { |
4452
|
142 if (discard_warning_messages) |
|
143 return; |
|
144 |
3491
|
145 flush_octave_stdout (); |
|
146 |
4051
|
147 OSSTREAM output_buf; |
3491
|
148 |
|
149 if (name) |
3761
|
150 output_buf << name << ": "; |
|
151 |
|
152 octave_vformat (output_buf, fmt, args); |
|
153 |
4051
|
154 output_buf << std::endl << OSSTREAM_ENDS; |
3491
|
155 |
3935
|
156 // XXX FIXME XXX -- we really want to capture the message before it |
|
157 // has all the formatting goop attached to it. We probably also |
|
158 // want just the message, not the traceback information. |
|
159 |
4051
|
160 std::string msg_string = OSSTREAM_STR (output_buf); |
3761
|
161 |
4051
|
162 OSSTREAM_FREEZE (output_buf); |
3934
|
163 |
3935
|
164 if (! warning_state) |
|
165 { |
|
166 // This is the first warning in a possible series. |
5567
|
167 |
|
168 Vlast_warning_id = id; |
3935
|
169 Vlast_warning_message = msg_string; |
|
170 } |
3934
|
171 |
3935
|
172 octave_diary << msg_string; |
|
173 |
|
174 std::cerr << msg_string; |
3491
|
175 } |
|
176 |
1
|
177 static void |
4732
|
178 verror (bool save_last_error, std::ostream& os, |
5567
|
179 const char *name, const char *id, const char *fmt, va_list args) |
1
|
180 { |
3815
|
181 if (discard_error_messages) |
|
182 return; |
|
183 |
3585
|
184 if (! buffer_error_messages) |
|
185 flush_octave_stdout (); |
914
|
186 |
2174
|
187 bool to_beep_or_not_to_beep_p = Vbeep_on_error && ! error_state; |
1423
|
188 |
4051
|
189 OSSTREAM output_buf; |
1
|
190 |
2174
|
191 if (to_beep_or_not_to_beep_p) |
1423
|
192 output_buf << "\a"; |
3620
|
193 |
599
|
194 if (name) |
|
195 output_buf << name << ": "; |
3620
|
196 |
|
197 octave_vformat (output_buf, fmt, args); |
|
198 |
4051
|
199 output_buf << std::endl << OSSTREAM_ENDS; |
581
|
200 |
3935
|
201 // XXX FIXME XXX -- we really want to capture the message before it |
|
202 // has all the formatting goop attached to it. We probably also |
|
203 // want just the message, not the traceback information. |
|
204 |
4051
|
205 std::string msg_string = OSSTREAM_STR (output_buf); |
3935
|
206 |
4051
|
207 OSSTREAM_FREEZE (output_buf); |
3935
|
208 |
4731
|
209 if (! error_state && save_last_error) |
3935
|
210 { |
|
211 // This is the first error in a possible series. |
5567
|
212 |
|
213 Vlast_error_id = id; |
3935
|
214 Vlast_error_message = msg_string; |
|
215 } |
|
216 |
1489
|
217 if (buffer_error_messages) |
|
218 { |
3941
|
219 std::string tmp = msg_string; |
1489
|
220 |
|
221 if (! error_message_buffer) |
|
222 { |
4051
|
223 error_message_buffer = new OSSTREAM; |
1489
|
224 |
|
225 // XXX FIXME XXX -- this is ugly, but it prevents |
|
226 // |
4699
|
227 // eval ("error (\"msg\")", "error (lasterr ())"); |
1489
|
228 // |
|
229 // from printing `error: ' twice. Assumes that the NAME we |
|
230 // have been given doesn't contain `:'. |
|
231 |
3935
|
232 size_t pos = msg_string.find (':'); |
3162
|
233 |
3935
|
234 if (pos != NPOS && pos < Vlast_error_message.length () - 2) |
|
235 tmp = msg_string.substr (pos+2); |
1489
|
236 } |
|
237 |
3935
|
238 *error_message_buffer << tmp; |
1489
|
239 } |
|
240 else |
|
241 { |
3935
|
242 octave_diary << msg_string; |
4732
|
243 os << msg_string; |
1489
|
244 } |
1
|
245 } |
|
246 |
1266
|
247 // Note that we don't actually print any message if the error string |
|
248 // is just "" or "\n". This allows error ("") and error ("\n") to |
|
249 // just set the error state. |
|
250 |
1005
|
251 static void |
5567
|
252 error_1 (std::ostream& os, const char *name, const char *id, |
|
253 const char *fmt, va_list args) |
1005
|
254 { |
|
255 if (error_state != -2) |
|
256 { |
1489
|
257 if (fmt) |
1005
|
258 { |
1489
|
259 if (*fmt) |
1005
|
260 { |
1489
|
261 int len = strlen (fmt); |
|
262 if (fmt[len - 1] == '\n') |
1266
|
263 { |
1489
|
264 if (len > 1) |
1266
|
265 { |
1489
|
266 char *tmp_fmt = strsave (fmt); |
|
267 tmp_fmt[len - 1] = '\0'; |
5567
|
268 verror (true, os, name, id, tmp_fmt, args); |
1489
|
269 delete [] tmp_fmt; |
|
270 } |
1423
|
271 |
1489
|
272 error_state = -2; |
1266
|
273 } |
1489
|
274 else |
5567
|
275 verror (true, os, name, id, fmt, args); |
1005
|
276 } |
|
277 } |
1489
|
278 else |
|
279 panic ("error_1: invalid format"); |
1423
|
280 |
|
281 if (! error_state) |
|
282 error_state = 1; |
1005
|
283 } |
|
284 } |
|
285 |
1
|
286 void |
|
287 message (const char *name, const char *fmt, ...) |
|
288 { |
|
289 va_list args; |
|
290 va_start (args, fmt); |
5567
|
291 verror (false, std::cerr, name, "", fmt, args); |
1
|
292 va_end (args); |
|
293 } |
|
294 |
|
295 void |
5567
|
296 message_with_id (const char *name, const char *id, const char *fmt, ...) |
|
297 { |
|
298 va_list args; |
|
299 va_start (args, fmt); |
|
300 verror (false, std::cerr, name, id, fmt, args); |
|
301 va_end (args); |
|
302 } |
|
303 |
|
304 void |
|
305 usage_1 (const char *id, const char *fmt, va_list args) |
|
306 { |
|
307 verror (true, std::cerr, "usage", id, fmt, args); |
|
308 error_state = -1; |
|
309 } |
|
310 |
|
311 void |
1
|
312 usage (const char *fmt, ...) |
|
313 { |
|
314 va_list args; |
|
315 va_start (args, fmt); |
5567
|
316 usage_1 ("", fmt, args); |
|
317 va_end (args); |
|
318 } |
|
319 |
|
320 void |
|
321 usage_with_id (const char *id, const char *fmt, ...) |
|
322 { |
|
323 va_list args; |
|
324 va_start (args, fmt); |
|
325 usage_1 (id, fmt, args); |
1
|
326 va_end (args); |
|
327 } |
|
328 |
3707
|
329 static void |
3719
|
330 pr_where_2 (const char *fmt, va_list args) |
|
331 { |
|
332 if (fmt) |
|
333 { |
|
334 if (*fmt) |
|
335 { |
|
336 int len = strlen (fmt); |
|
337 if (fmt[len - 1] == '\n') |
|
338 { |
|
339 if (len > 1) |
|
340 { |
|
341 char *tmp_fmt = strsave (fmt); |
|
342 tmp_fmt[len - 1] = '\0'; |
5567
|
343 verror (false, std::cerr, 0, "", tmp_fmt, args); |
3719
|
344 delete [] tmp_fmt; |
|
345 } |
|
346 } |
|
347 else |
5567
|
348 verror (false, std::cerr, 0, "", fmt, args); |
3719
|
349 } |
|
350 } |
|
351 else |
|
352 panic ("pr_where_2: invalid format"); |
|
353 } |
|
354 |
|
355 static void |
3707
|
356 pr_where_1 (const char *fmt, ...) |
|
357 { |
|
358 va_list args; |
|
359 va_start (args, fmt); |
3719
|
360 pr_where_2 (fmt, args); |
3707
|
361 va_end (args); |
|
362 } |
|
363 |
|
364 static void |
4719
|
365 pr_where (const char *name, bool print_code = true) |
3707
|
366 { |
|
367 if (curr_statement) |
|
368 { |
4976
|
369 std::string nm; |
3707
|
370 |
4976
|
371 int l = -1; |
|
372 int c = -1; |
|
373 |
|
374 octave_function *fcn = curr_function; |
|
375 |
|
376 if (fcn) |
3708
|
377 { |
4976
|
378 nm = fcn->name (); |
|
379 |
|
380 if (nm == "error" || nm == "warning") |
|
381 fcn = curr_caller_function; |
|
382 |
|
383 if (fcn) |
|
384 { |
|
385 nm = fcn->fcn_file_name (); |
3708
|
386 |
4976
|
387 if (nm.empty ()) |
|
388 nm = fcn->name (); |
|
389 |
|
390 if (curr_statement) |
|
391 { |
|
392 l = curr_statement->line (); |
|
393 c = curr_statement->column (); |
|
394 } |
|
395 } |
3708
|
396 } |
3707
|
397 |
4976
|
398 if (nm.empty ()) |
|
399 { |
|
400 if (l > 0 && c > 0) |
|
401 pr_where_1 ("%s: near line %d, column %d:", name, l, c); |
|
402 } |
3708
|
403 else |
4976
|
404 { |
|
405 if (l > 0 && c > 0) |
|
406 pr_where_1 ("%s: in %s near line %d, column %d:", |
|
407 name, nm.c_str (), l, c); |
|
408 else |
|
409 pr_where_1 ("%s: in %s", name, nm.c_str ()); |
|
410 } |
3707
|
411 |
4719
|
412 if (print_code) |
|
413 { |
|
414 // XXX FIXME XXX -- Note that the column number is probably |
|
415 // not going to mean much here since the code is being |
|
416 // reproduced from the parse tree, and we are only showing |
|
417 // one statement even if there were multiple statements on |
|
418 // the original source line. |
3707
|
419 |
4719
|
420 OSSTREAM output_buf; |
3708
|
421 |
4719
|
422 output_buf << std::endl; |
3707
|
423 |
4719
|
424 tree_print_code tpc (output_buf, ">>> "); |
|
425 |
|
426 curr_statement->accept (tpc); |
3707
|
427 |
4719
|
428 output_buf << std::endl << OSSTREAM_ENDS; |
3707
|
429 |
4719
|
430 pr_where_1 ("%s", OSSTREAM_C_STR (output_buf)); |
3707
|
431 |
4719
|
432 OSSTREAM_FREEZE (output_buf); |
|
433 } |
3707
|
434 } |
|
435 } |
|
436 |
5567
|
437 static int |
|
438 check_state (const std::string& state) |
|
439 { |
|
440 // -1: not found |
|
441 // 0: found, "off" |
|
442 // 1: found, "on" |
|
443 // 2: found, "error" |
|
444 |
|
445 if (state == "off") |
|
446 return 0; |
|
447 else if (state == "on") |
|
448 return 1; |
|
449 else if (state == "error") |
|
450 return 2; |
|
451 else |
|
452 return -1; |
|
453 } |
|
454 |
|
455 // For given warning ID, return 0 if warnings are disabled, 1 if |
|
456 // enabled, and 2 if this ID should be an error instead of a warning. |
|
457 |
|
458 static int |
|
459 warning_enabled (const std::string& id) |
1
|
460 { |
5567
|
461 int retval = 0; |
|
462 |
|
463 int all_state = -1; |
|
464 int id_state = -1; |
|
465 |
|
466 octave_idx_type nel = warning_options.numel (); |
|
467 |
|
468 if (nel > 0) |
|
469 { |
|
470 Cell identifier = warning_options.contents ("identifier"); |
|
471 Cell state = warning_options.contents ("state"); |
|
472 |
|
473 bool all_found = false; |
|
474 bool id_found = false; |
|
475 |
|
476 for (octave_idx_type i = 0; i < nel; i++) |
|
477 { |
|
478 octave_value ov = identifier(i); |
|
479 std::string ovs = ov.string_value (); |
|
480 |
|
481 if (! all_found && ovs == "all") |
|
482 { |
|
483 all_state = check_state (state(i).string_value ()); |
|
484 |
|
485 if (all_state >= 0) |
|
486 all_found = true; |
|
487 } |
|
488 |
|
489 if (! id_found && ovs == id) |
|
490 { |
|
491 id_state = check_state (state(i).string_value ()); |
|
492 |
|
493 if (id_state >= 0) |
|
494 id_found = true; |
|
495 } |
|
496 |
|
497 if (all_found && id_found) |
|
498 break; |
|
499 } |
|
500 |
|
501 } |
|
502 |
|
503 if (all_state == -1) |
|
504 panic_impossible (); |
|
505 |
|
506 if (all_state == 0) |
|
507 { |
|
508 if (id_state >= 0) |
|
509 retval = id_state; |
|
510 } |
|
511 else if (all_state == 1) |
|
512 { |
|
513 if (id_state == 0 || id_state == 2) |
|
514 retval = id_state; |
|
515 else |
|
516 retval = all_state; |
|
517 } |
|
518 else if (all_state == 2) |
|
519 retval = 2; |
|
520 |
|
521 return retval; |
|
522 } |
|
523 |
|
524 static void |
|
525 warning_1 (const char *id, const char *fmt, va_list args) |
|
526 { |
|
527 int warn_opt = warning_enabled (id); |
|
528 |
|
529 if (warn_opt == 2) |
|
530 { |
|
531 // Handle this warning as an error. |
|
532 |
|
533 error (id, fmt, args); |
|
534 } |
|
535 else if (warn_opt == 1) |
3934
|
536 { |
4014
|
537 if (curr_sym_tab != top_level_sym_tab |
5567
|
538 && Vbacktrace_on_warning |
4452
|
539 && ! warning_state |
|
540 && ! discard_warning_messages) |
4719
|
541 pr_where ("warning", false); |
3986
|
542 |
5567
|
543 vwarning ("warning", id, fmt, args); |
3707
|
544 |
3935
|
545 warning_state = 1; |
|
546 |
3934
|
547 if ((interactive || forced_interactive) |
|
548 && Vdebug_on_warning && curr_function) |
|
549 { |
|
550 unwind_protect_bool (Vdebug_on_warning); |
|
551 Vdebug_on_warning = false; |
3707
|
552 |
3934
|
553 do_keyboard (octave_value_list ()); |
3707
|
554 |
3934
|
555 unwind_protect::run (); |
|
556 } |
3707
|
557 } |
1
|
558 } |
|
559 |
|
560 void |
5567
|
561 warning (const char *fmt, ...) |
|
562 { |
|
563 va_list args; |
|
564 va_start (args, fmt); |
|
565 warning_1 ("", fmt, args); |
|
566 va_end (args); |
|
567 } |
|
568 |
|
569 void |
|
570 warning_with_id (const char *id, const char *fmt, ...) |
|
571 { |
|
572 va_list args; |
|
573 va_start (args, fmt); |
|
574 warning_1 (id, fmt, args); |
|
575 va_end (args); |
|
576 } |
|
577 |
|
578 static void |
|
579 error_2 (const char *id, const char *fmt, va_list args) |
1
|
580 { |
3707
|
581 int init_state = error_state; |
|
582 |
5567
|
583 error_1 (std::cerr, "error", id, fmt, args); |
3707
|
584 |
|
585 if ((interactive || forced_interactive) |
|
586 && Vdebug_on_error && init_state == 0 && curr_function) |
|
587 { |
|
588 unwind_protect_bool (Vdebug_on_error); |
|
589 Vdebug_on_error = false; |
|
590 |
3708
|
591 pr_where ("error"); |
3707
|
592 |
|
593 error_state = 0; |
|
594 |
|
595 do_keyboard (octave_value_list ()); |
|
596 |
|
597 unwind_protect::run (); |
|
598 } |
1005
|
599 } |
436
|
600 |
1005
|
601 void |
5567
|
602 error (const char *fmt, ...) |
|
603 { |
|
604 va_list args; |
|
605 va_start (args, fmt); |
|
606 error_2 ("", fmt, args); |
|
607 va_end (args); |
|
608 } |
|
609 |
|
610 void |
|
611 error_with_id (const char *id, const char *fmt, ...) |
|
612 { |
|
613 va_list args; |
|
614 va_start (args, fmt); |
|
615 error_2 (id, fmt, args); |
|
616 va_end (args); |
|
617 } |
|
618 |
|
619 void |
1005
|
620 parse_error (const char *fmt, ...) |
|
621 { |
|
622 va_list args; |
|
623 va_start (args, fmt); |
5567
|
624 error_1 (std::cerr, 0, "", fmt, args); |
|
625 va_end (args); |
|
626 } |
|
627 |
|
628 void |
|
629 parse_error_with_id (const char *id, const char *fmt, ...) |
|
630 { |
|
631 va_list args; |
|
632 va_start (args, fmt); |
|
633 error_1 (std::cerr, 0, id, fmt, args); |
1
|
634 va_end (args); |
|
635 } |
|
636 |
189
|
637 void |
1
|
638 panic (const char *fmt, ...) |
|
639 { |
|
640 va_list args; |
|
641 va_start (args, fmt); |
4699
|
642 buffer_error_messages = 0; |
3815
|
643 discard_error_messages = false; |
5567
|
644 verror (false, std::cerr, "panic", "", fmt, args); |
1
|
645 va_end (args); |
|
646 abort (); |
|
647 } |
|
648 |
4732
|
649 static void |
|
650 defun_usage_message_1 (const char *fmt, ...) |
|
651 { |
|
652 va_list args; |
|
653 va_start (args, fmt); |
5567
|
654 error_1 (octave_stdout, 0, "", fmt, args); |
4732
|
655 va_end (args); |
|
656 } |
|
657 |
|
658 void |
|
659 defun_usage_message (const std::string& msg) |
|
660 { |
|
661 defun_usage_message_1 ("%s", msg.c_str ()); |
|
662 } |
|
663 |
5567
|
664 typedef void (*error_fun)(const char *, const char *, ...); |
1489
|
665 |
2086
|
666 extern octave_value_list Fsprintf (const octave_value_list&, int); |
1489
|
667 |
3934
|
668 static std::string |
5567
|
669 handle_message (error_fun f, const char *id, const char *msg, |
|
670 const octave_value_list& args) |
528
|
671 { |
3934
|
672 std::string retval; |
528
|
673 |
3523
|
674 std::string tstr; |
1728
|
675 |
528
|
676 int nargin = args.length (); |
|
677 |
2745
|
678 if (nargin > 0) |
528
|
679 { |
3066
|
680 octave_value arg; |
|
681 |
|
682 if (nargin > 1) |
|
683 { |
|
684 octave_value_list tmp = Fsprintf (args, 1); |
|
685 arg = tmp(0); |
|
686 } |
|
687 else |
|
688 arg = args(0); |
2745
|
689 |
|
690 if (arg.is_defined ()) |
528
|
691 { |
2745
|
692 if (arg.is_string ()) |
|
693 { |
|
694 tstr = arg.string_value (); |
|
695 msg = tstr.c_str (); |
|
696 |
|
697 if (! msg) |
|
698 return retval; |
|
699 } |
|
700 else if (arg.is_empty ()) |
528
|
701 return retval; |
|
702 } |
|
703 } |
|
704 |
1489
|
705 // Ugh. |
|
706 |
|
707 int len = strlen (msg); |
|
708 if (msg[len - 1] == '\n') |
|
709 { |
|
710 if (len > 1) |
|
711 { |
|
712 char *tmp_msg = strsave (msg); |
|
713 tmp_msg[len - 1] = '\0'; |
5567
|
714 f (id, "%s\n", tmp_msg); |
3934
|
715 retval = tmp_msg; |
1489
|
716 delete [] tmp_msg; |
|
717 } |
|
718 } |
|
719 else |
3934
|
720 { |
5567
|
721 f (id, "%s", msg); |
3934
|
722 retval = msg; |
|
723 } |
528
|
724 |
|
725 return retval; |
|
726 } |
|
727 |
1957
|
728 DEFUN (error, args, , |
3373
|
729 "-*- texinfo -*-\n\ |
|
730 @deftypefn {Built-in Function} {} error (@var{template}, @dots{})\n\ |
|
731 The @code{error} function formats the optional arguments under the\n\ |
|
732 control of the template string @var{template} using the same rules as\n\ |
|
733 the @code{printf} family of functions (@pxref{Formatted Output}).\n\ |
|
734 The resulting message is prefixed by the string @samp{error: } and\n\ |
|
735 printed on the @code{stderr} stream.\n\ |
|
736 \n\ |
|
737 Calling @code{error} also sets Octave's internal error state such that\n\ |
|
738 control will return to the top level without evaluating any more\n\ |
|
739 commands. This is useful for aborting from functions or scripts.\n\ |
897
|
740 \n\ |
3373
|
741 If the error message does not end with a new line character, Octave will\n\ |
|
742 print a traceback of all the function calls leading to the error. For\n\ |
|
743 example, given the following function definitions:\n\ |
|
744 \n\ |
|
745 @example\n\ |
|
746 @group\n\ |
|
747 function f () g () end\n\ |
|
748 function g () h () end\n\ |
|
749 function h () nargin == 1 || error (\"nargin != 1\"); end\n\ |
|
750 @end group\n\ |
|
751 @end example\n\ |
1489
|
752 \n\ |
3373
|
753 @noindent\n\ |
|
754 calling the function @code{f} will result in a list of messages that\n\ |
|
755 can help you to quickly locate the exact location of the error:\n\ |
1489
|
756 \n\ |
3373
|
757 @example\n\ |
|
758 @group\n\ |
|
759 f ()\n\ |
|
760 error: nargin != 1\n\ |
|
761 error: evaluating index expression near line 1, column 30\n\ |
|
762 error: evaluating binary operator `||' near line 1, column 27\n\ |
|
763 error: called from `h'\n\ |
|
764 error: called from `g'\n\ |
|
765 error: called from `f'\n\ |
|
766 @end group\n\ |
|
767 @end example\n\ |
|
768 \n\ |
|
769 If the error message ends in a new line character, Octave will print the\n\ |
|
770 message but will not display any traceback messages as it returns\n\ |
|
771 control to the top level. For example, modifying the error message\n\ |
|
772 in the previous example to end in a new line causes Octave to only print\n\ |
|
773 a single message:\n\ |
|
774 \n\ |
|
775 @example\n\ |
|
776 @group\n\ |
|
777 function h () nargin == 1 || error (\"nargin != 1\\n\"); end\n\ |
|
778 f ()\n\ |
|
779 error: nargin != 1\n\ |
|
780 @end group\n\ |
|
781 @end example\n\ |
|
782 @end deftypefn") |
897
|
783 { |
5567
|
784 // XXX FIXME XXX -- need to extract and pass message id to |
|
785 // handle_message. |
|
786 |
3934
|
787 octave_value_list retval; |
5567
|
788 handle_message (error_with_id, "", "unspecified error", args); |
3934
|
789 return retval; |
1489
|
790 } |
897
|
791 |
5567
|
792 DEFCMD (warning, args, nargout, |
3373
|
793 "-*- texinfo -*-\n\ |
|
794 @deftypefn {Built-in Function} {} warning (@var{msg})\n\ |
|
795 Print a warning message @var{msg} prefixed by the string @samp{warning: }. \n\ |
|
796 After printing the warning message, Octave will continue to execute\n\ |
3600
|
797 commands. You should use this function when you want to notify the user\n\ |
|
798 of an unusual condition, but only when it makes sense for your program\n\ |
|
799 to go on.\n\ |
3373
|
800 @end deftypefn") |
1489
|
801 { |
5567
|
802 octave_value retval; |
3934
|
803 |
5567
|
804 int nargin = args.length (); |
|
805 int argc = nargin + 1; |
3934
|
806 |
3935
|
807 bool done = false; |
3934
|
808 |
5567
|
809 if (argc > 1 && args.all_strings_p ()) |
3935
|
810 { |
|
811 string_vector argv = args.make_argv ("warning"); |
|
812 |
|
813 if (! error_state) |
3934
|
814 { |
5567
|
815 std::string arg1 = argv(1); |
|
816 std::string arg2 = "all"; |
|
817 |
|
818 if (argc == 3) |
|
819 arg2 = argv(2); |
|
820 |
|
821 if (arg1 == "on" || arg1 == "off" || arg1 == "error") |
3934
|
822 { |
5567
|
823 Octave_map old_warning_options = warning_options; |
|
824 |
|
825 if (arg2 == "all") |
|
826 { |
|
827 Octave_map tmp; |
3934
|
828 |
5567
|
829 tmp.assign ("identifier", arg2); |
|
830 tmp.assign ("state", arg1); |
|
831 |
|
832 warning_options = tmp; |
|
833 |
3935
|
834 done = true; |
|
835 } |
5567
|
836 else if (arg2 == "backtrace") |
|
837 { |
|
838 if (arg1 != "error") |
|
839 { |
|
840 Vbacktrace_on_warning = (arg1 == "on"); |
|
841 done = true; |
|
842 } |
|
843 } |
|
844 else if (arg2 == "debug") |
|
845 { |
|
846 if (arg1 != "error") |
|
847 { |
|
848 bind_builtin_variable ("debug_on_warning", arg1 == "on"); |
|
849 done = true; |
|
850 } |
|
851 } |
|
852 else if (arg2 == "verbose") |
|
853 { |
|
854 if (arg1 != "error") |
|
855 { |
|
856 Vverbose_warning = (arg1 == "on"); |
|
857 done = true; |
|
858 } |
|
859 } |
|
860 else |
3935
|
861 { |
5567
|
862 if (arg2 == "last") |
|
863 arg2 = Vlast_warning_id; |
|
864 |
|
865 if (arg2 == "all") |
|
866 init_warning_options (arg1); |
|
867 else |
|
868 { |
|
869 Cell ident = warning_options.contents ("identifier"); |
|
870 Cell state = warning_options.contents ("state"); |
|
871 |
|
872 octave_idx_type nel = ident.numel (); |
|
873 |
|
874 bool found = false; |
|
875 |
|
876 for (octave_idx_type i = 0; i < nel; i++) |
|
877 { |
|
878 if (ident(i).string_value () == arg2) |
|
879 { |
|
880 // XXX FIXME XXX -- if state for "all" is |
|
881 // same as arg1, we can simply remove the |
|
882 // item from the list. |
|
883 |
|
884 state(i) = arg1; |
|
885 warning_options.assign ("state", state); |
|
886 found = true; |
|
887 break; |
|
888 } |
|
889 } |
|
890 |
|
891 if (! found) |
|
892 { |
|
893 // XXX FIXME XXX -- if state for "all" is |
|
894 // same as arg1, we don't need to do anything. |
|
895 |
|
896 ident.resize (dim_vector (1, nel+1)); |
|
897 state.resize (dim_vector (1, nel+1)); |
|
898 |
|
899 ident(nel) = arg2; |
|
900 state(nel) = arg1; |
|
901 |
|
902 warning_options.clear (); |
|
903 |
|
904 warning_options.assign ("identifier", ident); |
|
905 warning_options.assign ("state", state); |
|
906 } |
|
907 } |
|
908 |
3935
|
909 done = true; |
|
910 } |
5567
|
911 |
|
912 if (done && nargout > 0) |
|
913 retval = warning_options; |
|
914 } |
|
915 else if (arg1 == "query") |
|
916 { |
|
917 if (arg2 == "all") |
|
918 retval = warning_options; |
|
919 else if (arg2 == "backtrace" || arg2 == "debug" |
|
920 || arg2 == "verbose") |
|
921 { |
|
922 Octave_map tmp; |
|
923 tmp.assign ("identifier", arg2); |
|
924 if (arg2 == "backtrace") |
|
925 tmp.assign ("state", Vbacktrace_on_warning ? "on" : "off"); |
|
926 else if (arg2 == "debug") |
|
927 tmp.assign ("state", Vdebug_on_warning ? "on" : "off"); |
|
928 else |
|
929 tmp.assign ("state", Vverbose_warning ? "on" : "off"); |
|
930 } |
|
931 else |
3935
|
932 { |
5567
|
933 if (arg2 == "last") |
|
934 arg2 = Vlast_warning_id; |
|
935 |
|
936 Cell ident = warning_options.contents ("identifier"); |
|
937 Cell state = warning_options.contents ("state"); |
|
938 |
|
939 octave_idx_type nel = ident.numel (); |
|
940 |
|
941 bool found = false; |
|
942 |
|
943 std::string val; |
|
944 |
|
945 for (octave_idx_type i = 0; i < nel; i++) |
|
946 { |
|
947 if (ident(i).string_value () == arg2) |
|
948 { |
|
949 val = state(i).string_value (); |
|
950 found = true; |
|
951 break; |
|
952 } |
|
953 } |
|
954 |
|
955 if (found) |
|
956 { |
|
957 Octave_map tmp; |
|
958 |
|
959 tmp.assign ("identifier", arg2); |
|
960 tmp.assign ("state", val); |
|
961 |
|
962 retval = tmp; |
|
963 } |
|
964 else |
|
965 error ("warning: invalid warning tag `%s'", arg2.c_str ()); |
3935
|
966 } |
5567
|
967 |
|
968 done = true; |
3935
|
969 } |
3934
|
970 } |
|
971 } |
5567
|
972 else if (argc == 1) |
|
973 { |
|
974 retval = warning_options; |
3934
|
975 |
5567
|
976 done = true; |
|
977 } |
|
978 else if (argc == 2) |
|
979 { |
|
980 octave_value arg = args(0); |
|
981 |
|
982 Octave_map old_warning_options = warning_options; |
|
983 |
|
984 if (arg.is_map ()) |
|
985 { |
|
986 Octave_map m = arg.map_value (); |
|
987 |
|
988 if (m.contains ("identifier") && m.contains ("state")) |
|
989 warning ("warning: setting state with structure not implemented"); |
|
990 else |
|
991 error ("warning: expecting structure with fields `identifier' and `state'"); |
|
992 |
|
993 done = true; |
|
994 |
|
995 if (nargout > 0) |
|
996 retval = old_warning_options; |
|
997 } |
|
998 } |
|
999 |
|
1000 if (! (error_state || done)) |
3935
|
1001 { |
5567
|
1002 octave_value_list nargs = args; |
|
1003 |
|
1004 std::string id; |
|
1005 |
|
1006 if (nargin > 1) |
|
1007 { |
|
1008 std::string arg1 = args(0).string_value (); |
|
1009 |
|
1010 if (! error_state) |
|
1011 { |
|
1012 if (arg1.find ('%') == NPOS) |
|
1013 { |
|
1014 id = arg1; |
|
1015 |
|
1016 nargs.resize (nargin-1); |
|
1017 |
|
1018 for (int i = 1; i < nargin; i++) |
|
1019 nargs(i-1) = args(i); |
|
1020 } |
|
1021 } |
|
1022 else |
|
1023 return retval; |
|
1024 } |
|
1025 |
|
1026 // handle_message. |
|
1027 |
3935
|
1028 std::string prev_msg = Vlast_warning_message; |
|
1029 |
5567
|
1030 std::string curr_msg = handle_message (warning_with_id, id.c_str (), |
|
1031 "unspecified warning", nargs); |
3935
|
1032 |
|
1033 if (nargout > 0) |
5567
|
1034 retval = prev_msg; |
3935
|
1035 } |
|
1036 |
3934
|
1037 return retval; |
|
1038 } |
|
1039 |
5567
|
1040 DEFUN (lasterr, args, nargout, |
3935
|
1041 "-*- texinfo -*-\n\ |
5567
|
1042 @deftypefn {Built-in Function} {[@var{msg}, @var{msgid}] =} lasterr (@var{msg}, @var{msgid})\n\ |
3935
|
1043 Without any arguments, return the last error message. With one\n\ |
5567
|
1044 argument, set the last error message to @var{msg}. With two arguments,\n\ |
|
1045 also set the last message identifier.\n\ |
3935
|
1046 @end deftypefn") |
|
1047 { |
|
1048 octave_value_list retval; |
|
1049 |
|
1050 int argc = args.length () + 1; |
|
1051 |
5567
|
1052 if (argc < 4) |
5335
|
1053 { |
|
1054 string_vector argv = args.make_argv ("lasterr"); |
3935
|
1055 |
5335
|
1056 if (! error_state) |
|
1057 { |
5567
|
1058 std::string prev_error_id = Vlast_error_id; |
|
1059 std::string prev_error_message = Vlast_error_message; |
|
1060 |
|
1061 if (argc > 2) |
|
1062 Vlast_error_id = argv(2); |
|
1063 |
|
1064 if (argc > 1) |
5335
|
1065 Vlast_error_message = argv(1); |
5567
|
1066 |
|
1067 if (argc == 1 || nargout > 0) |
|
1068 { |
|
1069 retval(1) = prev_error_id; |
|
1070 retval(0) = prev_error_message; |
|
1071 } |
5335
|
1072 } |
5567
|
1073 else |
|
1074 error ("lastwarn: expecting arguments to be character strings"); |
5335
|
1075 } |
3935
|
1076 else |
|
1077 print_usage ("lasterr"); |
|
1078 |
|
1079 return retval; |
|
1080 } |
|
1081 |
4699
|
1082 // For backward compatibility. |
|
1083 DEFALIAS (error_text, lasterr); |
|
1084 DEFALIAS (__error_text__, lasterr); |
|
1085 |
5567
|
1086 DEFUN (lastwarn, args, nargout, |
3934
|
1087 "-*- texinfo -*-\n\ |
5567
|
1088 @deftypefn {Built-in Function} {[@var{msg}, @var{msgid}] =} lastwarn (@var{msg}, @var{msgid})\n\ |
3935
|
1089 Without any arguments, return the last warning message. With one\n\ |
5567
|
1090 argument, set the last warning message to @var{msg}. With two arguments,\n\ |
|
1091 also set the last message identifier.\n\ |
3934
|
1092 @end deftypefn") |
|
1093 { |
|
1094 octave_value_list retval; |
|
1095 |
|
1096 int argc = args.length () + 1; |
|
1097 |
5567
|
1098 if (argc < 4) |
|
1099 { |
|
1100 string_vector argv = args.make_argv ("lastwarn"); |
|
1101 |
|
1102 if (! error_state) |
|
1103 { |
|
1104 std::string prev_warning_id = Vlast_warning_id; |
|
1105 std::string prev_warning_message = Vlast_warning_message; |
|
1106 |
|
1107 if (argc > 2) |
|
1108 Vlast_warning_id = argv(2); |
3934
|
1109 |
5567
|
1110 if (argc > 1) |
|
1111 Vlast_warning_message = argv(1); |
|
1112 |
|
1113 if (argc == 1 || nargout > 0) |
|
1114 { |
|
1115 retval(1) = prev_warning_id; |
|
1116 retval(0) = prev_warning_message; |
|
1117 } |
|
1118 } |
|
1119 else |
|
1120 error ("lastwarn: expecting arguments to be character strings"); |
|
1121 } |
3934
|
1122 else |
|
1123 print_usage ("lastwarn"); |
|
1124 |
|
1125 return retval; |
897
|
1126 } |
|
1127 |
1957
|
1128 DEFUN (usage, args, , |
3373
|
1129 "-*- texinfo -*-\n\ |
|
1130 @deftypefn {Built-in Function} {} usage (@var{msg})\n\ |
|
1131 Print the message @var{msg}, prefixed by the string @samp{usage: }, and\n\ |
|
1132 set Octave's internal error state such that control will return to the\n\ |
|
1133 top level without evaluating any more commands. This is useful for\n\ |
|
1134 aborting from functions.\n\ |
|
1135 \n\ |
|
1136 After @code{usage} is evaluated, Octave will print a traceback of all\n\ |
|
1137 the function calls leading to the usage message.\n\ |
899
|
1138 \n\ |
3373
|
1139 You should use this function for reporting problems errors that result\n\ |
|
1140 from an improper call to a function, such as calling a function with an\n\ |
|
1141 incorrect number of arguments, or with arguments of the wrong type. For\n\ |
|
1142 example, most functions distributed with Octave begin with code like\n\ |
|
1143 this\n\ |
|
1144 \n\ |
|
1145 @example\n\ |
|
1146 @group\n\ |
|
1147 if (nargin != 2)\n\ |
|
1148 usage (\"foo (a, b)\");\n\ |
|
1149 endif\n\ |
|
1150 @end group\n\ |
|
1151 @end example\n\ |
|
1152 \n\ |
|
1153 @noindent\n\ |
|
1154 to check for the proper number of arguments.\n\ |
|
1155 @end deftypefn") |
899
|
1156 { |
3934
|
1157 octave_value_list retval; |
5567
|
1158 handle_message (usage_with_id, "", "unknown", args); |
3934
|
1159 return retval; |
899
|
1160 } |
|
1161 |
2174
|
1162 static int |
|
1163 beep_on_error (void) |
|
1164 { |
|
1165 Vbeep_on_error = check_preference ("beep_on_error"); |
|
1166 |
|
1167 return 0; |
|
1168 } |
|
1169 |
3707
|
1170 static int |
|
1171 debug_on_error (void) |
|
1172 { |
|
1173 Vdebug_on_error = check_preference ("debug_on_error"); |
|
1174 |
|
1175 return 0; |
|
1176 } |
|
1177 |
|
1178 static int |
|
1179 debug_on_warning (void) |
|
1180 { |
|
1181 Vdebug_on_warning = check_preference ("debug_on_warning"); |
|
1182 |
|
1183 return 0; |
|
1184 } |
|
1185 |
2174
|
1186 void |
|
1187 symbols_of_error (void) |
|
1188 { |
5567
|
1189 init_warning_options (); |
|
1190 |
4233
|
1191 DEFVAR (beep_on_error, false, beep_on_error, |
3373
|
1192 "-*- texinfo -*-\n\ |
|
1193 @defvr {Built-in Variable} beep_on_error\n\ |
|
1194 If the value of @code{beep_on_error} is nonzero, Octave will try\n\ |
|
1195 to ring your terminal's bell before printing an error message. The\n\ |
|
1196 default value is 0.\n\ |
|
1197 @end defvr"); |
3018
|
1198 |
4233
|
1199 DEFVAR (debug_on_error, false, debug_on_error, |
3707
|
1200 "-*- texinfo -*-\n\ |
|
1201 @defvr {Built-in Variable} debug_on_error\n\ |
|
1202 If the value of @code{debug_on_error} is nonzero, Octave will try\n\ |
|
1203 to enter the debugger when an error is encountered. This will also\n\ |
|
1204 inhibit printing of the normal traceback message (you will only see\n\ |
|
1205 the top-level error message). The default value is 0.\n\ |
|
1206 @end defvr"); |
|
1207 |
4233
|
1208 DEFVAR (debug_on_warning, false, debug_on_warning, |
3707
|
1209 "-*- texinfo -*-\n\ |
|
1210 @defvr {Built-in Variable} debug_on_warning\n\ |
|
1211 If the value of @code{debug_on_warning} is nonzero, Octave will try\n\ |
|
1212 to enter the debugger when a warning is encountered. The default\n\ |
|
1213 value is 0.\n\ |
|
1214 @end defvr"); |
2174
|
1215 } |
|
1216 |
1
|
1217 /* |
|
1218 ;;; Local Variables: *** |
|
1219 ;;; mode: C++ *** |
|
1220 ;;; End: *** |
|
1221 */ |