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