Mercurial > hg > octave-nkf
annotate src/error.cc @ 7948:af10baa63915 ss-3-1-50
3.1.50 snapshot
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 18 Jul 2008 17:42:48 -0400 |
parents | 4816cec8b1ca |
children | 736124a4fa3d |
rev | line source |
---|---|
1 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, |
7353 | 4 2002, 2003, 2004, 2005, 2006, 2007, 2008 John W. Eaton |
1 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
1 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
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 |
5765 | 31 #include <sstream> |
1728 | 32 #include <string> |
33 | |
1352 | 34 #include "defun.h" |
1 | 35 #include "error.h" |
3707 | 36 #include "input.h" |
1742 | 37 #include "pager.h" |
1352 | 38 #include "oct-obj.h" |
5567 | 39 #include "oct-map.h" |
1352 | 40 #include "utils.h" |
2370 | 41 #include "ov.h" |
3707 | 42 #include "ov-usr-fcn.h" |
43 #include "pt-pr-code.h" | |
44 #include "pt-stmt.h" | |
45 #include "toplev.h" | |
46 #include "unwind-prot.h" | |
2370 | 47 #include "variables.h" |
1 | 48 |
2174 | 49 // TRUE means that Octave will try to beep obnoxiously before printing |
50 // error messages. | |
5794 | 51 static bool Vbeep_on_error = false; |
2174 | 52 |
3707 | 53 // TRUE means that Octave will try to enter the debugger when an error |
54 // is encountered. This will also inhibit printing of the normal | |
55 // traceback message (you will only see the top-level error message). | |
7353 | 56 bool Vdebug_on_error = false; |
3707 | 57 |
58 // TRUE means that Octave will try to enter the debugger when a warning | |
59 // is encountered. | |
7353 | 60 bool Vdebug_on_warning = false; |
3707 | 61 |
5567 | 62 // TRUE means that Octave will try to display a stack trace when a |
63 // warning is encountered. | |
64 static bool Vbacktrace_on_warning = false; | |
65 | |
66 // TRUE means that Octave will print a verbose warning. Currently unused. | |
67 static bool Vverbose_warning; | |
68 | |
5582 | 69 // TRUE means that Octave will print no warnings, but lastwarn will be |
70 //updated | |
71 static bool Vquiet_warning = false; | |
72 | |
5567 | 73 // A structure containing (most of) the current state of warnings. |
74 static Octave_map warning_options; | |
75 | |
3935 | 76 // The text of the last error message. |
77 static std::string Vlast_error_message; | |
78 | |
3934 | 79 // The text of the last warning message. |
80 static std::string Vlast_warning_message; | |
81 | |
5567 | 82 // The last warning message id. |
83 static std::string Vlast_warning_id; | |
3934 | 84 |
5567 | 85 // The last error message id. |
86 static std::string Vlast_error_id; | |
3934 | 87 |
6361 | 88 // The last file in which an error occured |
89 static std::string Vlast_error_file; | |
90 | |
91 // The last function in which an error occured | |
92 static std::string Vlast_error_name; | |
93 | |
94 // The last line in a function at which an error occured | |
95 static int Vlast_error_line = -1; | |
96 | |
97 // The last column in a function at which an error occured | |
98 static int Vlast_error_column = -1; | |
99 | |
143 | 100 // Current error state. |
3935 | 101 // |
102 // Valid values: | |
103 // | |
104 // -2: an error has occurred, but don't print any messages. | |
105 // -1: an error has occurred, we are printing a traceback | |
106 // 0: no error | |
107 // 1: an error has occurred | |
108 // | |
672 | 109 int error_state = 0; |
110 | |
3489 | 111 // Current warning state. |
3935 | 112 // |
113 // Valid values: | |
114 // | |
115 // 0: no warning | |
116 // 1: a warning has occurred | |
117 // | |
3489 | 118 int warning_state = 0; |
119 | |
1489 | 120 // Tell the error handler whether to print messages, or just store |
121 // them for later. Used for handling errors in eval() and | |
122 // the `unwind_protect' statement. | |
4699 | 123 int buffer_error_messages = 0; |
1489 | 124 |
3815 | 125 // TRUE means error messages are turned off. |
126 bool discard_error_messages = false; | |
127 | |
4452 | 128 // TRUE means warning messages are turned off. |
129 bool discard_warning_messages = false; | |
130 | |
3811 | 131 // The message buffer. |
5765 | 132 static std::ostringstream *error_message_buffer = 0; |
143 | 133 |
4318 | 134 void |
135 reset_error_handler (void) | |
136 { | |
137 error_state = 0; | |
138 warning_state = 0; | |
4699 | 139 buffer_error_messages = 0; |
4318 | 140 discard_error_messages = false; |
141 } | |
142 | |
5567 | 143 static void |
5794 | 144 initialize_warning_options (const std::string& state) |
5567 | 145 { |
146 warning_options.clear (); | |
147 | |
148 warning_options.assign ("identifier", "all"); | |
149 warning_options.assign ("state", state); | |
150 } | |
151 | |
3491 | 152 // Warning messages are never buffered. |
153 | |
154 static void | |
5567 | 155 vwarning (const char *name, const char *id, const char *fmt, va_list args) |
3491 | 156 { |
4452 | 157 if (discard_warning_messages) |
158 return; | |
159 | |
3491 | 160 flush_octave_stdout (); |
161 | |
5765 | 162 std::ostringstream output_buf; |
3491 | 163 |
164 if (name) | |
3761 | 165 output_buf << name << ": "; |
166 | |
167 octave_vformat (output_buf, fmt, args); | |
168 | |
5765 | 169 output_buf << std::endl; |
3491 | 170 |
5775 | 171 // FIXME -- we really want to capture the message before it |
3935 | 172 // has all the formatting goop attached to it. We probably also |
173 // want just the message, not the traceback information. | |
174 | |
5765 | 175 std::string msg_string = output_buf.str (); |
3934 | 176 |
3935 | 177 if (! warning_state) |
178 { | |
179 // This is the first warning in a possible series. | |
5567 | 180 |
181 Vlast_warning_id = id; | |
3935 | 182 Vlast_warning_message = msg_string; |
183 } | |
3934 | 184 |
5582 | 185 if (! Vquiet_warning) |
186 { | |
187 octave_diary << msg_string; | |
3935 | 188 |
5582 | 189 std::cerr << msg_string; |
190 } | |
3491 | 191 } |
192 | |
1 | 193 static void |
4732 | 194 verror (bool save_last_error, std::ostream& os, |
5567 | 195 const char *name, const char *id, const char *fmt, va_list args) |
1 | 196 { |
3815 | 197 if (discard_error_messages) |
198 return; | |
199 | |
3585 | 200 if (! buffer_error_messages) |
201 flush_octave_stdout (); | |
914 | 202 |
5775 | 203 // FIXME -- we really want to capture the message before it |
3935 | 204 // has all the formatting goop attached to it. We probably also |
205 // want just the message, not the traceback information. | |
206 | |
7877 | 207 std::ostringstream output_buf; |
208 | |
209 octave_vformat (output_buf, fmt, args); | |
210 | |
211 std::string base_msg = output_buf.str (); | |
212 | |
213 bool to_beep_or_not_to_beep_p = Vbeep_on_error && ! error_state; | |
214 | |
215 std::string msg_string; | |
216 | |
217 if (to_beep_or_not_to_beep_p) | |
218 msg_string = "\a"; | |
219 | |
220 if (name) | |
221 msg_string += std::string (name) + ": "; | |
222 | |
7880 | 223 msg_string += base_msg + "\n"; |
3935 | 224 |
4731 | 225 if (! error_state && save_last_error) |
3935 | 226 { |
227 // This is the first error in a possible series. | |
5567 | 228 |
229 Vlast_error_id = id; | |
7877 | 230 Vlast_error_message = base_msg; |
6361 | 231 |
232 Vlast_error_line = -1; | |
233 Vlast_error_column = -1; | |
234 Vlast_error_name = std::string (); | |
235 Vlast_error_file = std::string (); | |
236 | |
7877 | 237 octave_user_code *fcn = octave_call_stack::caller_user_code (); |
6361 | 238 |
7877 | 239 if (fcn) |
240 { | |
241 Vlast_error_file = fcn->fcn_file_name (); | |
242 Vlast_error_name = fcn->name (); | |
243 Vlast_error_line = octave_call_stack::caller_user_code_line (); | |
244 Vlast_error_column = octave_call_stack::caller_user_code_column (); | |
6361 | 245 } |
3935 | 246 } |
247 | |
1489 | 248 if (buffer_error_messages) |
249 { | |
7882 | 250 if (error_message_buffer) |
251 msg_string = "error: " + msg_string; | |
252 else | |
7880 | 253 error_message_buffer = new std::ostringstream (); |
1489 | 254 |
7880 | 255 *error_message_buffer << msg_string; |
1489 | 256 } |
257 else | |
258 { | |
3935 | 259 octave_diary << msg_string; |
4732 | 260 os << msg_string; |
1489 | 261 } |
1 | 262 } |
263 | |
1266 | 264 // Note that we don't actually print any message if the error string |
265 // is just "" or "\n". This allows error ("") and error ("\n") to | |
266 // just set the error state. | |
267 | |
1005 | 268 static void |
5567 | 269 error_1 (std::ostream& os, const char *name, const char *id, |
270 const char *fmt, va_list args) | |
1005 | 271 { |
272 if (error_state != -2) | |
273 { | |
1489 | 274 if (fmt) |
1005 | 275 { |
1489 | 276 if (*fmt) |
1005 | 277 { |
1489 | 278 int len = strlen (fmt); |
279 if (fmt[len - 1] == '\n') | |
1266 | 280 { |
1489 | 281 if (len > 1) |
1266 | 282 { |
1489 | 283 char *tmp_fmt = strsave (fmt); |
284 tmp_fmt[len - 1] = '\0'; | |
5567 | 285 verror (true, os, name, id, tmp_fmt, args); |
1489 | 286 delete [] tmp_fmt; |
287 } | |
1423 | 288 |
1489 | 289 error_state = -2; |
1266 | 290 } |
1489 | 291 else |
5567 | 292 verror (true, os, name, id, fmt, args); |
1005 | 293 } |
294 } | |
1489 | 295 else |
296 panic ("error_1: invalid format"); | |
1423 | 297 |
298 if (! error_state) | |
299 error_state = 1; | |
1005 | 300 } |
301 } | |
302 | |
1 | 303 void |
6338 | 304 vmessage (const char *name, const char *fmt, va_list args) |
305 { | |
306 verror (false, std::cerr, name, "", fmt, args); | |
307 } | |
308 | |
309 void | |
1 | 310 message (const char *name, const char *fmt, ...) |
311 { | |
312 va_list args; | |
313 va_start (args, fmt); | |
6338 | 314 vmessage (name, fmt, args); |
1 | 315 va_end (args); |
316 } | |
317 | |
318 void | |
6338 | 319 vmessage_with_id (const char *name, const char *id, const char *fmt, |
320 va_list args) | |
321 { | |
322 verror (false, std::cerr, name, id, fmt, args); | |
323 } | |
324 | |
325 void | |
5567 | 326 message_with_id (const char *name, const char *id, const char *fmt, ...) |
327 { | |
328 va_list args; | |
329 va_start (args, fmt); | |
6338 | 330 vmessage_with_id (name, id, fmt, args); |
5567 | 331 va_end (args); |
332 } | |
333 | |
334 void | |
335 usage_1 (const char *id, const char *fmt, va_list args) | |
336 { | |
337 verror (true, std::cerr, "usage", id, fmt, args); | |
338 error_state = -1; | |
339 } | |
340 | |
341 void | |
6338 | 342 vusage (const char *fmt, va_list args) |
343 { | |
344 usage_1 ("", fmt, args); | |
345 } | |
346 | |
347 void | |
1 | 348 usage (const char *fmt, ...) |
349 { | |
350 va_list args; | |
351 va_start (args, fmt); | |
6338 | 352 vusage (fmt, args); |
5567 | 353 va_end (args); |
354 } | |
355 | |
356 void | |
6338 | 357 vusage_with_id (const char *id, const char *fmt, va_list args) |
358 { | |
359 usage_1 (id, fmt, args); | |
360 } | |
361 | |
362 void | |
5567 | 363 usage_with_id (const char *id, const char *fmt, ...) |
364 { | |
365 va_list args; | |
366 va_start (args, fmt); | |
6338 | 367 vusage_with_id (id, fmt, args); |
1 | 368 va_end (args); |
369 } | |
370 | |
3707 | 371 static void |
3719 | 372 pr_where_2 (const char *fmt, va_list args) |
373 { | |
374 if (fmt) | |
375 { | |
376 if (*fmt) | |
377 { | |
378 int len = strlen (fmt); | |
379 if (fmt[len - 1] == '\n') | |
380 { | |
381 if (len > 1) | |
382 { | |
383 char *tmp_fmt = strsave (fmt); | |
384 tmp_fmt[len - 1] = '\0'; | |
5567 | 385 verror (false, std::cerr, 0, "", tmp_fmt, args); |
3719 | 386 delete [] tmp_fmt; |
387 } | |
388 } | |
389 else | |
5567 | 390 verror (false, std::cerr, 0, "", fmt, args); |
3719 | 391 } |
392 } | |
393 else | |
394 panic ("pr_where_2: invalid format"); | |
395 } | |
396 | |
397 static void | |
3707 | 398 pr_where_1 (const char *fmt, ...) |
399 { | |
400 va_list args; | |
401 va_start (args, fmt); | |
3719 | 402 pr_where_2 (fmt, args); |
3707 | 403 va_end (args); |
404 } | |
405 | |
406 static void | |
4719 | 407 pr_where (const char *name, bool print_code = true) |
3707 | 408 { |
7734
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7719
diff
changeset
|
409 if (octave_call_stack::current_statement ()) |
3707 | 410 { |
4976 | 411 std::string nm; |
3707 | 412 |
4976 | 413 int l = -1; |
414 int c = -1; | |
415 | |
7719
87eda1f8faaa
octave_user_code: new base class for octave_user_script and octave_user_function
John W. Eaton <jwe@octave.org>
parents:
7552
diff
changeset
|
416 octave_user_code *fcn = octave_call_stack::caller_user_code (); |
4976 | 417 |
418 if (fcn) | |
3708 | 419 { |
5743 | 420 nm = fcn->fcn_file_name (); |
4976 | 421 |
5743 | 422 if (nm.empty ()) |
423 nm = fcn->name (); | |
3708 | 424 |
7734
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7719
diff
changeset
|
425 l = octave_call_stack::current_line (); |
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7719
diff
changeset
|
426 c = octave_call_stack::current_column (); |
3708 | 427 } |
3707 | 428 |
4976 | 429 if (nm.empty ()) |
430 { | |
431 if (l > 0 && c > 0) | |
432 pr_where_1 ("%s: near line %d, column %d:", name, l, c); | |
433 } | |
3708 | 434 else |
4976 | 435 { |
436 if (l > 0 && c > 0) | |
437 pr_where_1 ("%s: in %s near line %d, column %d:", | |
438 name, nm.c_str (), l, c); | |
439 else | |
440 pr_where_1 ("%s: in %s", name, nm.c_str ()); | |
441 } | |
3707 | 442 |
4719 | 443 if (print_code) |
444 { | |
5775 | 445 // FIXME -- Note that the column number is probably |
4719 | 446 // not going to mean much here since the code is being |
447 // reproduced from the parse tree, and we are only showing | |
448 // one statement even if there were multiple statements on | |
449 // the original source line. | |
3707 | 450 |
5765 | 451 std::ostringstream output_buf; |
3708 | 452 |
4719 | 453 output_buf << std::endl; |
3707 | 454 |
4719 | 455 tree_print_code tpc (output_buf, ">>> "); |
456 | |
7734
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7719
diff
changeset
|
457 tree_statement *curr_stmt = octave_call_stack::current_statement (); |
7552
6070c3bd69c4
Arbitrary call stack access for external debuggers changeset
ryanru@PrinceHumperdinck
parents:
7353
diff
changeset
|
458 |
6070c3bd69c4
Arbitrary call stack access for external debuggers changeset
ryanru@PrinceHumperdinck
parents:
7353
diff
changeset
|
459 if (curr_stmt) |
6070c3bd69c4
Arbitrary call stack access for external debuggers changeset
ryanru@PrinceHumperdinck
parents:
7353
diff
changeset
|
460 curr_stmt->accept (tpc); |
3707 | 461 |
5765 | 462 output_buf << std::endl; |
3707 | 463 |
5765 | 464 std::string msg = output_buf.str (); |
3707 | 465 |
5765 | 466 pr_where_1 ("%s", msg.c_str ()); |
4719 | 467 } |
3707 | 468 } |
469 } | |
470 | |
6000 | 471 static void |
472 error_2 (const char *id, const char *fmt, va_list args) | |
473 { | |
474 int init_state = error_state; | |
475 | |
476 error_1 (std::cerr, "error", id, fmt, args); | |
477 | |
478 if ((interactive || forced_interactive) | |
479 && Vdebug_on_error && init_state == 0 | |
7719
87eda1f8faaa
octave_user_code: new base class for octave_user_script and octave_user_function
John W. Eaton <jwe@octave.org>
parents:
7552
diff
changeset
|
480 && octave_call_stack::caller_user_code ()) |
6000 | 481 { |
482 unwind_protect_bool (Vdebug_on_error); | |
483 Vdebug_on_error = false; | |
484 | |
485 pr_where ("error"); | |
486 | |
487 error_state = 0; | |
488 | |
489 do_keyboard (octave_value_list ()); | |
490 | |
491 unwind_protect::run (); | |
492 } | |
493 } | |
494 | |
495 void | |
6338 | 496 verror (const char *fmt, va_list args) |
497 { | |
498 error_2 ("", fmt, args); | |
499 } | |
500 | |
501 void | |
6000 | 502 error (const char *fmt, ...) |
503 { | |
504 va_list args; | |
505 va_start (args, fmt); | |
6338 | 506 verror (fmt, args); |
6000 | 507 va_end (args); |
508 } | |
509 | |
510 void | |
6338 | 511 verror_with_id (const char *id, const char *fmt, va_list args) |
512 { | |
513 error_2 (id, fmt, args); | |
514 } | |
515 | |
516 void | |
6000 | 517 error_with_id (const char *id, const char *fmt, ...) |
518 { | |
519 va_list args; | |
520 va_start (args, fmt); | |
6338 | 521 verror_with_id (id, fmt, args); |
6000 | 522 va_end (args); |
523 } | |
524 | |
5567 | 525 static int |
526 check_state (const std::string& state) | |
527 { | |
528 // -1: not found | |
529 // 0: found, "off" | |
530 // 1: found, "on" | |
531 // 2: found, "error" | |
532 | |
533 if (state == "off") | |
534 return 0; | |
535 else if (state == "on") | |
536 return 1; | |
537 else if (state == "error") | |
538 return 2; | |
539 else | |
540 return -1; | |
541 } | |
542 | |
543 // For given warning ID, return 0 if warnings are disabled, 1 if | |
544 // enabled, and 2 if this ID should be an error instead of a warning. | |
545 | |
5781 | 546 int |
5567 | 547 warning_enabled (const std::string& id) |
1 | 548 { |
5567 | 549 int retval = 0; |
550 | |
551 int all_state = -1; | |
552 int id_state = -1; | |
553 | |
554 octave_idx_type nel = warning_options.numel (); | |
555 | |
556 if (nel > 0) | |
557 { | |
558 Cell identifier = warning_options.contents ("identifier"); | |
559 Cell state = warning_options.contents ("state"); | |
560 | |
561 bool all_found = false; | |
562 bool id_found = false; | |
563 | |
564 for (octave_idx_type i = 0; i < nel; i++) | |
565 { | |
566 octave_value ov = identifier(i); | |
567 std::string ovs = ov.string_value (); | |
568 | |
569 if (! all_found && ovs == "all") | |
570 { | |
571 all_state = check_state (state(i).string_value ()); | |
572 | |
573 if (all_state >= 0) | |
574 all_found = true; | |
575 } | |
576 | |
577 if (! id_found && ovs == id) | |
578 { | |
579 id_state = check_state (state(i).string_value ()); | |
580 | |
581 if (id_state >= 0) | |
582 id_found = true; | |
583 } | |
584 | |
585 if (all_found && id_found) | |
586 break; | |
587 } | |
588 } | |
589 | |
590 if (all_state == -1) | |
591 panic_impossible (); | |
592 | |
593 if (all_state == 0) | |
594 { | |
595 if (id_state >= 0) | |
596 retval = id_state; | |
597 } | |
598 else if (all_state == 1) | |
599 { | |
600 if (id_state == 0 || id_state == 2) | |
601 retval = id_state; | |
602 else | |
603 retval = all_state; | |
604 } | |
605 else if (all_state == 2) | |
7206 | 606 { |
607 if (id_state == 0) | |
608 retval= id_state; | |
609 else | |
610 retval = all_state; | |
611 } | |
5567 | 612 |
613 return retval; | |
614 } | |
615 | |
616 static void | |
617 warning_1 (const char *id, const char *fmt, va_list args) | |
618 { | |
619 int warn_opt = warning_enabled (id); | |
620 | |
621 if (warn_opt == 2) | |
622 { | |
623 // Handle this warning as an error. | |
624 | |
5998 | 625 error_2 (id, fmt, args); |
5567 | 626 } |
627 else if (warn_opt == 1) | |
3934 | 628 { |
7336 | 629 if (symbol_table::at_top_level () |
5567 | 630 && Vbacktrace_on_warning |
4452 | 631 && ! warning_state |
632 && ! discard_warning_messages) | |
4719 | 633 pr_where ("warning", false); |
3986 | 634 |
5567 | 635 vwarning ("warning", id, fmt, args); |
3707 | 636 |
3935 | 637 warning_state = 1; |
638 | |
3934 | 639 if ((interactive || forced_interactive) |
5743 | 640 && Vdebug_on_warning |
7719
87eda1f8faaa
octave_user_code: new base class for octave_user_script and octave_user_function
John W. Eaton <jwe@octave.org>
parents:
7552
diff
changeset
|
641 && octave_call_stack::caller_user_code ()) |
3934 | 642 { |
643 unwind_protect_bool (Vdebug_on_warning); | |
644 Vdebug_on_warning = false; | |
3707 | 645 |
3934 | 646 do_keyboard (octave_value_list ()); |
3707 | 647 |
3934 | 648 unwind_protect::run (); |
649 } | |
3707 | 650 } |
1 | 651 } |
652 | |
653 void | |
6338 | 654 vwarning (const char *fmt, va_list args) |
655 { | |
656 warning_1 ("", fmt, args); | |
657 } | |
658 | |
659 void | |
5567 | 660 warning (const char *fmt, ...) |
661 { | |
662 va_list args; | |
663 va_start (args, fmt); | |
6338 | 664 vwarning (fmt, args); |
5567 | 665 va_end (args); |
666 } | |
667 | |
668 void | |
6338 | 669 vwarning_with_id (const char *id, const char *fmt, va_list args) |
670 { | |
671 warning_1 (id, fmt, args); | |
672 } | |
673 | |
674 void | |
5567 | 675 warning_with_id (const char *id, const char *fmt, ...) |
676 { | |
677 va_list args; | |
678 va_start (args, fmt); | |
6338 | 679 vwarning_with_id (id, fmt, args); |
5567 | 680 va_end (args); |
681 } | |
682 | |
683 void | |
6338 | 684 vparse_error (const char *fmt, va_list args) |
685 { | |
686 error_1 (std::cerr, 0, "", fmt, args); | |
687 } | |
688 | |
689 void | |
1005 | 690 parse_error (const char *fmt, ...) |
691 { | |
692 va_list args; | |
693 va_start (args, fmt); | |
6338 | 694 vparse_error (fmt, args); |
5567 | 695 va_end (args); |
696 } | |
697 | |
698 void | |
6338 | 699 vparse_error_with_id (const char *id, const char *fmt, va_list args) |
700 { | |
701 error_1 (std::cerr, 0, id, fmt, args); | |
702 } | |
703 | |
704 void | |
5567 | 705 parse_error_with_id (const char *id, const char *fmt, ...) |
706 { | |
707 va_list args; | |
708 va_start (args, fmt); | |
6338 | 709 vparse_error_with_id (id, fmt, args); |
1 | 710 va_end (args); |
711 } | |
712 | |
189 | 713 void |
6361 | 714 rethrow_error (const char *id, const char *fmt, ...) |
715 { | |
716 va_list args; | |
717 va_start (args, fmt); | |
6640 | 718 error_1 (std::cerr, 0, id, fmt, args); |
6361 | 719 va_end (args); |
720 } | |
721 | |
722 void | |
1 | 723 panic (const char *fmt, ...) |
724 { | |
725 va_list args; | |
726 va_start (args, fmt); | |
4699 | 727 buffer_error_messages = 0; |
3815 | 728 discard_error_messages = false; |
5567 | 729 verror (false, std::cerr, "panic", "", fmt, args); |
1 | 730 va_end (args); |
731 abort (); | |
732 } | |
733 | |
4732 | 734 static void |
735 defun_usage_message_1 (const char *fmt, ...) | |
736 { | |
737 va_list args; | |
738 va_start (args, fmt); | |
5567 | 739 error_1 (octave_stdout, 0, "", fmt, args); |
4732 | 740 va_end (args); |
741 } | |
742 | |
743 void | |
744 defun_usage_message (const std::string& msg) | |
745 { | |
746 defun_usage_message_1 ("%s", msg.c_str ()); | |
747 } | |
748 | |
5567 | 749 typedef void (*error_fun)(const char *, const char *, ...); |
1489 | 750 |
2086 | 751 extern octave_value_list Fsprintf (const octave_value_list&, int); |
1489 | 752 |
3934 | 753 static std::string |
5567 | 754 handle_message (error_fun f, const char *id, const char *msg, |
755 const octave_value_list& args) | |
528 | 756 { |
3934 | 757 std::string retval; |
528 | 758 |
3523 | 759 std::string tstr; |
1728 | 760 |
528 | 761 int nargin = args.length (); |
762 | |
2745 | 763 if (nargin > 0) |
528 | 764 { |
3066 | 765 octave_value arg; |
766 | |
767 if (nargin > 1) | |
768 { | |
769 octave_value_list tmp = Fsprintf (args, 1); | |
770 arg = tmp(0); | |
771 } | |
772 else | |
773 arg = args(0); | |
2745 | 774 |
775 if (arg.is_defined ()) | |
528 | 776 { |
2745 | 777 if (arg.is_string ()) |
778 { | |
779 tstr = arg.string_value (); | |
780 msg = tstr.c_str (); | |
781 | |
782 if (! msg) | |
783 return retval; | |
784 } | |
785 else if (arg.is_empty ()) | |
528 | 786 return retval; |
787 } | |
788 } | |
789 | |
1489 | 790 // Ugh. |
791 | |
792 int len = strlen (msg); | |
793 if (msg[len - 1] == '\n') | |
794 { | |
795 if (len > 1) | |
796 { | |
797 char *tmp_msg = strsave (msg); | |
798 tmp_msg[len - 1] = '\0'; | |
5567 | 799 f (id, "%s\n", tmp_msg); |
3934 | 800 retval = tmp_msg; |
1489 | 801 delete [] tmp_msg; |
802 } | |
803 } | |
804 else | |
3934 | 805 { |
5567 | 806 f (id, "%s", msg); |
3934 | 807 retval = msg; |
808 } | |
528 | 809 |
810 return retval; | |
811 } | |
812 | |
6361 | 813 DEFUN (rethrow, args, , |
814 "-*- texinfo -*-\n\ | |
815 @deftypefn {Built-in Function} {} rethrow (@var{err})\n\ | |
816 Reissues a previous error as defined by @var{err}. @var{err} is a structure\n\ | |
817 that must contain at least the 'message' and 'identifier' fields. @var{err}\n\ | |
818 can also contain a field 'stack' that gives information on the assumed\n\ | |
819 location of the error. Typically @var{err} is returned from\n\ | |
820 @code{lasterror}.\n\ | |
821 @seealso{lasterror, lasterr, error}\n\ | |
822 @end deftypefn") | |
823 { | |
824 octave_value retval; | |
825 int nargin = args.length(); | |
826 | |
827 if (nargin != 1) | |
6959 | 828 print_usage (); |
6361 | 829 else |
830 { | |
6483 | 831 Octave_map err = args(0).map_value (); |
6361 | 832 |
6483 | 833 if (! error_state) |
6361 | 834 { |
6483 | 835 if (err.contains ("message") && err.contains ("identifier")) |
6361 | 836 { |
6483 | 837 std::string msg = err.contents("message")(0).string_value (); |
838 std::string id = err.contents("identifier")(0).string_value (); | |
6361 | 839 int len = msg.length(); |
840 std::string file; | |
841 std::string nm; | |
842 int l = -1; | |
843 int c = -1; | |
844 | |
6483 | 845 if (err.contains ("stack")) |
6361 | 846 { |
6483 | 847 Octave_map err_stack = err.contents("stack")(0).map_value (); |
848 | |
6640 | 849 if (err_stack.numel () > 0) |
850 { | |
851 if (err_stack.contains ("file")) | |
852 file = err_stack.contents("file")(0).string_value (); | |
6483 | 853 |
6640 | 854 if (err_stack.contains ("name")) |
855 nm = err_stack.contents("name")(0).string_value (); | |
6483 | 856 |
6640 | 857 if (err_stack.contains ("line")) |
858 l = err_stack.contents("line")(0).nint_value (); | |
859 | |
860 if (err_stack.contains ("column")) | |
861 c = err_stack.contents("column")(0).nint_value (); | |
862 } | |
6361 | 863 } |
864 | |
865 // Ugh. | |
6483 | 866 char *tmp_msg = strsave (msg.c_str ()); |
6361 | 867 if (tmp_msg[len-1] == '\n') |
868 { | |
869 if (len > 1) | |
870 { | |
871 tmp_msg[len - 1] = '\0'; | |
6483 | 872 rethrow_error (id.c_str (), "%s\n", tmp_msg); |
6361 | 873 } |
874 } | |
875 else | |
6483 | 876 rethrow_error (id.c_str (), "%s", tmp_msg); |
6361 | 877 delete [] tmp_msg; |
878 | |
879 // FIXME: Need to restore the stack as rethrow_error sets it? | |
880 Vlast_error_file = file; | |
881 Vlast_error_name = nm; | |
882 Vlast_error_line = l; | |
883 Vlast_error_column = c; | |
884 | |
6483 | 885 if (err.contains ("stack")) |
6361 | 886 { |
887 if (file.empty ()) | |
888 { | |
889 if (nm.empty ()) | |
890 { | |
891 if (l > 0) | |
6483 | 892 { |
893 if (c > 0) | |
894 pr_where_1 ("error: near line %d, column %d", | |
895 l, c); | |
896 else | |
897 pr_where_1 ("error: near line %d", l); | |
898 } | |
6361 | 899 } |
900 else | |
901 { | |
902 if (l > 0) | |
6483 | 903 { |
904 if (c > 0) | |
905 pr_where_1 ("error: called from `%s' near line %d, column %d", | |
906 nm.c_str (), l, c); | |
907 else | |
908 pr_where_1 ("error: called from `%d' near line %d", nm.c_str (), l); | |
909 } | |
6361 | 910 } |
911 } | |
912 else | |
913 { | |
914 if (nm.empty ()) | |
915 { | |
916 if (l > 0) | |
6483 | 917 { |
918 if (c > 0) | |
919 pr_where_1 ("error: in file %s near line %d, column %d", | |
920 file.c_str (), l, c); | |
921 else | |
922 pr_where_1 ("error: in file %s near line %d", file.c_str (), l); | |
923 } | |
6361 | 924 } |
925 else | |
926 { | |
927 if (l > 0) | |
6483 | 928 { |
929 if (c > 0) | |
930 pr_where_1 ("error: called from `%s' in file %s near line %d, column %d", | |
931 nm.c_str (), file.c_str (), l, c); | |
932 else | |
933 pr_where_1 ("error: called from `%d' in file %s near line %d", nm.c_str (), file.c_str (), l); | |
934 } | |
6361 | 935 } |
936 } | |
937 } | |
938 } | |
939 else | |
940 error ("rethrow: structure must contain the fields 'message and 'identifier'"); | |
941 } | |
942 } | |
943 return retval; | |
944 } | |
945 | |
1957 | 946 DEFUN (error, args, , |
3373 | 947 "-*- texinfo -*-\n\ |
948 @deftypefn {Built-in Function} {} error (@var{template}, @dots{})\n\ | |
7252 | 949 @deftypefnx {Built-in Function} {} error (@var{id}, @var{template}, @dots{})\n\ |
5781 | 950 Format the optional arguments under the control of the template string\n\ |
951 @var{template} using the same rules as the @code{printf} family of\n\ | |
952 functions (@pxref{Formatted Output}) and print the resulting message\n\ | |
953 on the @code{stderr} stream. The message is prefixed by the character\n\ | |
954 string @samp{error: }.\n\ | |
3373 | 955 \n\ |
956 Calling @code{error} also sets Octave's internal error state such that\n\ | |
957 control will return to the top level without evaluating any more\n\ | |
958 commands. This is useful for aborting from functions or scripts.\n\ | |
897 | 959 \n\ |
3373 | 960 If the error message does not end with a new line character, Octave will\n\ |
961 print a traceback of all the function calls leading to the error. For\n\ | |
962 example, given the following function definitions:\n\ | |
963 \n\ | |
964 @example\n\ | |
965 @group\n\ | |
6671 | 966 function f () g (); end\n\ |
967 function g () h (); end\n\ | |
3373 | 968 function h () nargin == 1 || error (\"nargin != 1\"); end\n\ |
969 @end group\n\ | |
970 @end example\n\ | |
1489 | 971 \n\ |
3373 | 972 @noindent\n\ |
973 calling the function @code{f} will result in a list of messages that\n\ | |
974 can help you to quickly locate the exact location of the error:\n\ | |
1489 | 975 \n\ |
7031 | 976 @smallexample\n\ |
3373 | 977 @group\n\ |
978 f ()\n\ | |
979 error: nargin != 1\n\ | |
980 error: evaluating index expression near line 1, column 30\n\ | |
981 error: evaluating binary operator `||' near line 1, column 27\n\ | |
982 error: called from `h'\n\ | |
983 error: called from `g'\n\ | |
984 error: called from `f'\n\ | |
985 @end group\n\ | |
7031 | 986 @end smallexample\n\ |
3373 | 987 \n\ |
988 If the error message ends in a new line character, Octave will print the\n\ | |
989 message but will not display any traceback messages as it returns\n\ | |
990 control to the top level. For example, modifying the error message\n\ | |
991 in the previous example to end in a new line causes Octave to only print\n\ | |
992 a single message:\n\ | |
993 \n\ | |
994 @example\n\ | |
995 @group\n\ | |
996 function h () nargin == 1 || error (\"nargin != 1\\n\"); end\n\ | |
997 f ()\n\ | |
998 error: nargin != 1\n\ | |
999 @end group\n\ | |
1000 @end example\n\ | |
1001 @end deftypefn") | |
897 | 1002 { |
7252 | 1003 octave_value retval; |
1004 | |
1005 int nargin = args.length (); | |
1006 | |
1007 octave_value_list nargs = args; | |
1008 | |
1009 std::string id; | |
1010 | |
1011 if (nargin > 1) | |
1012 { | |
1013 std::string arg1 = args(0).string_value (); | |
5567 | 1014 |
7252 | 1015 if (! error_state) |
1016 { | |
1017 if (arg1.find ('%') == NPOS) | |
1018 { | |
1019 id = arg1; | |
1020 | |
1021 nargs.resize (nargin-1); | |
1022 | |
1023 for (int i = 1; i < nargin; i++) | |
1024 nargs(i-1) = args(i); | |
1025 } | |
1026 } | |
1027 else | |
1028 return retval; | |
1029 } | |
1030 | |
1031 handle_message (error_with_id, id.c_str (), "unspecified error", nargs); | |
1032 | |
3934 | 1033 return retval; |
1489 | 1034 } |
897 | 1035 |
5567 | 1036 DEFCMD (warning, args, nargout, |
3373 | 1037 "-*- texinfo -*-\n\ |
5781 | 1038 @deftypefn {Built-in Function} {} warning (@var{template}, @dots{})\n\ |
7252 | 1039 @deftypefnx {Built-in Function} {} warning (@var{id}, @var{template}, @dots{})\n\ |
5781 | 1040 Format the optional arguments under the control of the template string\n\ |
1041 @var{template} using the same rules as the @code{printf} family of\n\ | |
1042 functions (@pxref{Formatted Output}) and print the resulting message\n\ | |
1043 on the @code{stderr} stream. The message is prefixed by the character\n\ | |
1044 string @samp{warning: }.\n\ | |
1045 You should use this function when you want to notify the user\n\ | |
3600 | 1046 of an unusual condition, but only when it makes sense for your program\n\ |
1047 to go on.\n\ | |
5781 | 1048 \n\ |
1049 The optional message identifier allows users to enable or disable\n\ | |
1050 warnings tagged by @var{id}. The special identifier @samp{\"all\"} may\n\ | |
1051 be used to set the state of all warnings.\n\ | |
1052 \n\ | |
1053 @deftypefnx {Built-in Function} {} warning (\"on\", @var{id})\n\ | |
1054 @deftypefnx {Built-in Function} {} warning (\"off\", @var{id})\n\ | |
1055 @deftypefnx {Built-in Function} {} warning (\"error\", @var{id})\n\ | |
1056 @deftypefnx {Built-in Function} {} warning (\"query\", @var{id})\n\ | |
6653 | 1057 Set or query the state of a particular warning using the identifier\n\ |
5781 | 1058 @var{id}. If the identifier is omitted, a value of @samp{\"all\"} is\n\ |
1059 assumed. If you set the state of a warning to @samp{\"error\"}, the\n\ | |
1060 warning named by @var{id} is handled as if it were an error instead.\n\ | |
5783 | 1061 @seealso{warning_ids}\n\ |
3373 | 1062 @end deftypefn") |
1489 | 1063 { |
5567 | 1064 octave_value retval; |
3934 | 1065 |
5567 | 1066 int nargin = args.length (); |
1067 int argc = nargin + 1; | |
3934 | 1068 |
3935 | 1069 bool done = false; |
3934 | 1070 |
5567 | 1071 if (argc > 1 && args.all_strings_p ()) |
3935 | 1072 { |
1073 string_vector argv = args.make_argv ("warning"); | |
1074 | |
1075 if (! error_state) | |
3934 | 1076 { |
5567 | 1077 std::string arg1 = argv(1); |
1078 std::string arg2 = "all"; | |
1079 | |
1080 if (argc == 3) | |
1081 arg2 = argv(2); | |
1082 | |
1083 if (arg1 == "on" || arg1 == "off" || arg1 == "error") | |
3934 | 1084 { |
5567 | 1085 Octave_map old_warning_options = warning_options; |
1086 | |
1087 if (arg2 == "all") | |
1088 { | |
1089 Octave_map tmp; | |
3934 | 1090 |
7202 | 1091 Cell id (1, 1); |
1092 Cell st (1, 1); | |
1093 | |
1094 id(0) = arg2; | |
1095 st(0) = arg1; | |
1096 | |
1097 // Since internal Octave functions are not | |
1098 // compatible, turning all warnings into errors | |
1099 // should leave the state of | |
1100 // Octave:matlab-incompatible alone. | |
1101 | |
1102 if (arg1 == "error" | |
1103 && warning_options.contains ("identifier")) | |
1104 { | |
7206 | 1105 octave_idx_type n = 1; |
1106 | |
7202 | 1107 Cell tid = warning_options.contents ("identifier"); |
1108 Cell tst = warning_options.contents ("state"); | |
1109 | |
1110 for (octave_idx_type i = 0; i < tid.numel (); i++) | |
1111 { | |
1112 octave_value vid = tid(i); | |
1113 | |
7206 | 1114 if (vid.is_string ()) |
7202 | 1115 { |
7206 | 1116 std::string key = vid.string_value (); |
7202 | 1117 |
7206 | 1118 if (key == "Octave:matlab-incompatible" |
1119 || key == "Octave:single-quote-string") | |
1120 { | |
1121 id.resize (dim_vector (1, n+1)); | |
1122 st.resize (dim_vector (1, n+1)); | |
7202 | 1123 |
7206 | 1124 id(n) = tid(i); |
1125 st(n) = tst(i); | |
1126 | |
1127 n++; | |
1128 } | |
7202 | 1129 } |
1130 } | |
1131 } | |
1132 | |
1133 tmp.assign ("identifier", id); | |
1134 tmp.assign ("state", st); | |
5567 | 1135 |
1136 warning_options = tmp; | |
1137 | |
3935 | 1138 done = true; |
1139 } | |
5567 | 1140 else if (arg2 == "backtrace") |
1141 { | |
1142 if (arg1 != "error") | |
1143 { | |
1144 Vbacktrace_on_warning = (arg1 == "on"); | |
1145 done = true; | |
1146 } | |
1147 } | |
1148 else if (arg2 == "debug") | |
1149 { | |
1150 if (arg1 != "error") | |
1151 { | |
5794 | 1152 Vdebug_on_warning = (arg1 == "on"); |
5567 | 1153 done = true; |
1154 } | |
1155 } | |
1156 else if (arg2 == "verbose") | |
1157 { | |
1158 if (arg1 != "error") | |
1159 { | |
1160 Vverbose_warning = (arg1 == "on"); | |
1161 done = true; | |
1162 } | |
1163 } | |
5582 | 1164 else if (arg2 == "quiet") |
1165 { | |
1166 if (arg1 != "error") | |
1167 { | |
1168 Vquiet_warning = (arg1 == "on"); | |
1169 done = true; | |
1170 } | |
1171 } | |
5567 | 1172 else |
3935 | 1173 { |
5567 | 1174 if (arg2 == "last") |
1175 arg2 = Vlast_warning_id; | |
1176 | |
1177 if (arg2 == "all") | |
5794 | 1178 initialize_warning_options (arg1); |
5567 | 1179 else |
1180 { | |
1181 Cell ident = warning_options.contents ("identifier"); | |
1182 Cell state = warning_options.contents ("state"); | |
1183 | |
1184 octave_idx_type nel = ident.numel (); | |
1185 | |
1186 bool found = false; | |
1187 | |
1188 for (octave_idx_type i = 0; i < nel; i++) | |
1189 { | |
1190 if (ident(i).string_value () == arg2) | |
1191 { | |
5775 | 1192 // FIXME -- if state for "all" is |
5567 | 1193 // same as arg1, we can simply remove the |
1194 // item from the list. | |
1195 | |
1196 state(i) = arg1; | |
1197 warning_options.assign ("state", state); | |
1198 found = true; | |
1199 break; | |
1200 } | |
1201 } | |
1202 | |
1203 if (! found) | |
1204 { | |
5775 | 1205 // FIXME -- if state for "all" is |
5567 | 1206 // same as arg1, we don't need to do anything. |
1207 | |
1208 ident.resize (dim_vector (1, nel+1)); | |
1209 state.resize (dim_vector (1, nel+1)); | |
1210 | |
1211 ident(nel) = arg2; | |
1212 state(nel) = arg1; | |
1213 | |
1214 warning_options.clear (); | |
1215 | |
1216 warning_options.assign ("identifier", ident); | |
1217 warning_options.assign ("state", state); | |
1218 } | |
1219 } | |
1220 | |
3935 | 1221 done = true; |
1222 } | |
5567 | 1223 |
1224 if (done && nargout > 0) | |
6427 | 1225 retval = old_warning_options; |
5567 | 1226 } |
1227 else if (arg1 == "query") | |
1228 { | |
1229 if (arg2 == "all") | |
1230 retval = warning_options; | |
1231 else if (arg2 == "backtrace" || arg2 == "debug" | |
5582 | 1232 || arg2 == "verbose" || arg2 == "quiet") |
5567 | 1233 { |
1234 Octave_map tmp; | |
1235 tmp.assign ("identifier", arg2); | |
1236 if (arg2 == "backtrace") | |
1237 tmp.assign ("state", Vbacktrace_on_warning ? "on" : "off"); | |
1238 else if (arg2 == "debug") | |
1239 tmp.assign ("state", Vdebug_on_warning ? "on" : "off"); | |
5582 | 1240 else if (arg2 == "verbose") |
1241 tmp.assign ("state", Vverbose_warning ? "on" : "off"); | |
5567 | 1242 else |
5582 | 1243 tmp.assign ("state", Vquiet_warning ? "on" : "off"); |
1244 | |
1245 retval = tmp; | |
5567 | 1246 } |
1247 else | |
3935 | 1248 { |
5567 | 1249 if (arg2 == "last") |
1250 arg2 = Vlast_warning_id; | |
1251 | |
1252 Cell ident = warning_options.contents ("identifier"); | |
1253 Cell state = warning_options.contents ("state"); | |
1254 | |
1255 octave_idx_type nel = ident.numel (); | |
1256 | |
1257 bool found = false; | |
1258 | |
1259 std::string val; | |
1260 | |
1261 for (octave_idx_type i = 0; i < nel; i++) | |
1262 { | |
1263 if (ident(i).string_value () == arg2) | |
1264 { | |
1265 val = state(i).string_value (); | |
1266 found = true; | |
1267 break; | |
1268 } | |
1269 } | |
1270 | |
5781 | 1271 if (! found) |
1272 { | |
1273 for (octave_idx_type i = 0; i < nel; i++) | |
1274 { | |
1275 if (ident(i).string_value () == "all") | |
1276 { | |
1277 val = state(i).string_value (); | |
1278 found = true; | |
1279 break; | |
1280 } | |
1281 } | |
1282 } | |
1283 | |
5567 | 1284 if (found) |
1285 { | |
1286 Octave_map tmp; | |
1287 | |
1288 tmp.assign ("identifier", arg2); | |
1289 tmp.assign ("state", val); | |
1290 | |
1291 retval = tmp; | |
1292 } | |
1293 else | |
5781 | 1294 error ("warning: unable to find default warning state!"); |
3935 | 1295 } |
5567 | 1296 |
1297 done = true; | |
3935 | 1298 } |
3934 | 1299 } |
1300 } | |
5567 | 1301 else if (argc == 1) |
1302 { | |
1303 retval = warning_options; | |
3934 | 1304 |
5567 | 1305 done = true; |
1306 } | |
1307 else if (argc == 2) | |
1308 { | |
1309 octave_value arg = args(0); | |
1310 | |
1311 Octave_map old_warning_options = warning_options; | |
1312 | |
1313 if (arg.is_map ()) | |
1314 { | |
1315 Octave_map m = arg.map_value (); | |
1316 | |
1317 if (m.contains ("identifier") && m.contains ("state")) | |
6427 | 1318 warning_options = m; |
5567 | 1319 else |
1320 error ("warning: expecting structure with fields `identifier' and `state'"); | |
1321 | |
1322 done = true; | |
1323 | |
1324 if (nargout > 0) | |
1325 retval = old_warning_options; | |
1326 } | |
1327 } | |
1328 | |
1329 if (! (error_state || done)) | |
3935 | 1330 { |
5567 | 1331 octave_value_list nargs = args; |
1332 | |
1333 std::string id; | |
1334 | |
1335 if (nargin > 1) | |
1336 { | |
1337 std::string arg1 = args(0).string_value (); | |
1338 | |
1339 if (! error_state) | |
1340 { | |
1341 if (arg1.find ('%') == NPOS) | |
1342 { | |
1343 id = arg1; | |
1344 | |
1345 nargs.resize (nargin-1); | |
1346 | |
1347 for (int i = 1; i < nargin; i++) | |
1348 nargs(i-1) = args(i); | |
1349 } | |
1350 } | |
1351 else | |
1352 return retval; | |
1353 } | |
1354 | |
3935 | 1355 std::string prev_msg = Vlast_warning_message; |
1356 | |
5567 | 1357 std::string curr_msg = handle_message (warning_with_id, id.c_str (), |
1358 "unspecified warning", nargs); | |
3935 | 1359 |
1360 if (nargout > 0) | |
5567 | 1361 retval = prev_msg; |
3935 | 1362 } |
1363 | |
3934 | 1364 return retval; |
1365 } | |
1366 | |
5904 | 1367 void |
5794 | 1368 disable_warning (const std::string& id) |
1369 { | |
1370 octave_value_list args; | |
1371 | |
1372 args(1) = id; | |
1373 args(0) = "off"; | |
1374 | |
1375 Fwarning (args, 0); | |
1376 } | |
1377 | |
1378 void | |
1379 initialize_default_warning_state (void) | |
1380 { | |
1381 initialize_warning_options ("on"); | |
1382 | |
1383 // Most people will want to have the following disabled. | |
1384 | |
1385 disable_warning ("Octave:array-to-scalar"); | |
1386 disable_warning ("Octave:array-to-vector"); | |
1387 disable_warning ("Octave:empty-list-elements"); | |
1388 disable_warning ("Octave:fortran-indexing"); | |
1389 disable_warning ("Octave:imag-to-real"); | |
1390 disable_warning ("Octave:matlab-incompatible"); | |
1391 disable_warning ("Octave:missing-semicolon"); | |
1392 disable_warning ("Octave:neg-dim-as-zero"); | |
1393 disable_warning ("Octave:resize-on-range-error"); | |
1394 disable_warning ("Octave:separator-insert"); | |
1395 disable_warning ("Octave:single-quote-string"); | |
1396 disable_warning ("Octave:str-to-num"); | |
1397 disable_warning ("Octave:string-concat"); | |
1398 disable_warning ("Octave:variable-switch-label"); | |
1399 } | |
1400 | |
6361 | 1401 DEFUN (lasterror, args, , |
1402 "-*- texinfo -*-\n\ | |
1403 @deftypefn {Built-in Function} {@var{err} =} lasterror (@var{err})\n\ | |
1404 @deftypefnx {Built-in Function} {} lasterror ('reset')\n\ | |
1405 Returns or sets the last error message. Called without any arguments\n\ | |
1406 returns a structure containing the last error message, as well as other\n\ | |
1407 information related to this error. The elements of this structure are:\n\ | |
1408 \n\ | |
1409 @table @asis\n\ | |
1410 @item 'message'\n\ | |
1411 The text of the last error message\n\ | |
1412 @item 'identifier'\n\ | |
1413 The message identifier of this error message\n\ | |
1414 @item 'stack'\n\ | |
7001 | 1415 A structure containing information on where the message occurred. This might\n\ |
6361 | 1416 be an empty structure if this in the case where this information can not\n\ |
1417 be obtained. The fields of this structure are:\n\ | |
1418 \n\ | |
1419 @table @asis\n\ | |
1420 @item 'file'\n\ | |
1421 The name of the file where the error occurred\n\ | |
1422 @item 'name'\n\ | |
7001 | 1423 The name of function in which the error occurred\n\ |
6361 | 1424 @item 'line'\n\ |
7001 | 1425 The line number at which the error occurred\n\ |
6361 | 1426 @item 'column'\n\ |
1427 An optional field with the column number at which the error occurred\n\ | |
1428 @end table\n\ | |
1429 @end table\n\ | |
1430 \n\ | |
1431 The @var{err} structure may also be passed to @code{lasterror} to set the\n\ | |
1432 information about the last error. The only constraint on @var{err} in that\n\ | |
1433 case is that it is a scalar structure. Any fields of @var{err} that match\n\ | |
1434 the above are set to the value passed in @var{err}, while other fields are\n\ | |
1435 set to their default values.\n\ | |
1436 \n\ | |
1437 If @code{lasterror} is called with the argument 'reset', all values take\n\ | |
1438 their default values.\n\ | |
1439 @end deftypefn") | |
1440 { | |
1441 octave_value retval; | |
1442 int nargin = args.length(); | |
1443 | |
1444 if (nargin < 2) | |
1445 { | |
1446 Octave_map err; | |
1447 | |
1448 err.assign ("message", Vlast_error_message); | |
1449 err.assign ("identifier", Vlast_error_id); | |
1450 | |
1451 if (! (Vlast_error_file.empty() && Vlast_error_name.empty() && | |
1452 Vlast_error_line < 0 && Vlast_error_column < 0)) | |
1453 { | |
1454 Octave_map err_stack; | |
1455 | |
1456 err_stack.assign ("file", Vlast_error_file); | |
1457 err_stack.assign ("name", Vlast_error_name); | |
1458 err_stack.assign ("line", Vlast_error_line); | |
1459 err_stack.assign ("column", Vlast_error_column); | |
1460 | |
1461 err.assign ("stack", octave_value (err_stack)); | |
1462 } | |
1463 else | |
1464 { | |
1465 string_vector sv(4); | |
1466 sv[0] = "file"; | |
1467 sv[1] = "name"; | |
1468 sv[2] = "line"; | |
1469 sv[3] = "column"; | |
1470 err.assign ("stack", octave_value (Octave_map (dim_vector (0,1), | |
1471 sv))); | |
1472 } | |
1473 | |
1474 if (nargin == 1) | |
1475 { | |
1476 if (args(0).is_string()) | |
1477 { | |
6483 | 1478 if (args(0).string_value () == "reset") |
6361 | 1479 { |
1480 Vlast_error_message = std::string(); | |
1481 Vlast_error_id = std::string(); | |
1482 Vlast_error_file = std::string(); | |
1483 Vlast_error_name = std::string(); | |
1484 Vlast_error_line = -1; | |
1485 Vlast_error_column = -1; | |
1486 } | |
1487 else | |
1488 error("lasterror: unrecognized string argument"); | |
1489 } | |
1490 else if (args(0).is_map ()) | |
1491 { | |
6483 | 1492 Octave_map new_err = args(0).map_value (); |
6361 | 1493 std::string new_error_message; |
1494 std::string new_error_id; | |
1495 std::string new_error_file; | |
1496 std::string new_error_name; | |
1497 int new_error_line = -1; | |
1498 int new_error_column = -1; | |
1499 | |
6483 | 1500 if (! error_state && new_err.contains ("message")) |
6361 | 1501 { |
1502 const std::string tmp = | |
6483 | 1503 new_err.contents("message")(0).string_value (); |
6361 | 1504 new_error_message = tmp; |
1505 } | |
1506 | |
6483 | 1507 if (! error_state && new_err.contains ("identifier")) |
6361 | 1508 { |
1509 const std::string tmp = | |
6483 | 1510 new_err.contents("identifier")(0).string_value (); |
6361 | 1511 new_error_id = tmp; |
1512 } | |
1513 | |
6483 | 1514 if (! error_state && new_err.contains ("stack")) |
6361 | 1515 { |
1516 Octave_map new_err_stack = | |
6483 | 1517 new_err.contents("identifier")(0).map_value (); |
6361 | 1518 |
6483 | 1519 if (! error_state && new_err_stack.contains ("file")) |
6361 | 1520 { |
1521 const std::string tmp = | |
6483 | 1522 new_err_stack.contents("file")(0).string_value (); |
6361 | 1523 new_error_file = tmp; |
1524 } | |
1525 | |
6483 | 1526 if (! error_state && new_err_stack.contains ("name")) |
6361 | 1527 { |
1528 const std::string tmp = | |
6483 | 1529 new_err_stack.contents("name")(0).string_value (); |
6361 | 1530 new_error_name = tmp; |
1531 } | |
1532 | |
6483 | 1533 if (! error_state && new_err_stack.contains ("line")) |
6361 | 1534 { |
1535 const int tmp = | |
6483 | 1536 new_err_stack.contents("line")(0).nint_value (); |
6361 | 1537 new_error_line = tmp; |
1538 } | |
1539 | |
6483 | 1540 if (! error_state && new_err_stack.contains ("column")) |
6361 | 1541 { |
1542 const int tmp = | |
6483 | 1543 new_err_stack.contents("column")(0).nint_value (); |
6361 | 1544 new_error_column = tmp; |
1545 } | |
1546 } | |
1547 | |
1548 if (! error_state) | |
1549 { | |
1550 Vlast_error_message = new_error_message; | |
1551 Vlast_error_id = new_error_id; | |
1552 Vlast_error_file = new_error_file; | |
1553 Vlast_error_name = new_error_name; | |
1554 Vlast_error_line = new_error_line; | |
1555 Vlast_error_column = new_error_column; | |
1556 } | |
1557 } | |
1558 else | |
1559 error ("lasterror: argument must be a structure or a string"); | |
1560 } | |
1561 | |
6483 | 1562 if (! error_state) |
6361 | 1563 retval = err; |
1564 } | |
1565 else | |
1566 print_usage (); | |
1567 | |
1568 return retval; | |
1569 } | |
1570 | |
5567 | 1571 DEFUN (lasterr, args, nargout, |
3935 | 1572 "-*- texinfo -*-\n\ |
5567 | 1573 @deftypefn {Built-in Function} {[@var{msg}, @var{msgid}] =} lasterr (@var{msg}, @var{msgid})\n\ |
3935 | 1574 Without any arguments, return the last error message. With one\n\ |
5567 | 1575 argument, set the last error message to @var{msg}. With two arguments,\n\ |
1576 also set the last message identifier.\n\ | |
3935 | 1577 @end deftypefn") |
1578 { | |
1579 octave_value_list retval; | |
1580 | |
1581 int argc = args.length () + 1; | |
1582 | |
5567 | 1583 if (argc < 4) |
5335 | 1584 { |
1585 string_vector argv = args.make_argv ("lasterr"); | |
3935 | 1586 |
5335 | 1587 if (! error_state) |
1588 { | |
5567 | 1589 std::string prev_error_id = Vlast_error_id; |
1590 std::string prev_error_message = Vlast_error_message; | |
1591 | |
1592 if (argc > 2) | |
1593 Vlast_error_id = argv(2); | |
1594 | |
1595 if (argc > 1) | |
5335 | 1596 Vlast_error_message = argv(1); |
5567 | 1597 |
1598 if (argc == 1 || nargout > 0) | |
1599 { | |
1600 retval(1) = prev_error_id; | |
1601 retval(0) = prev_error_message; | |
1602 } | |
5335 | 1603 } |
5567 | 1604 else |
5582 | 1605 error ("lasterr: expecting arguments to be character strings"); |
5335 | 1606 } |
3935 | 1607 else |
5823 | 1608 print_usage (); |
3935 | 1609 |
1610 return retval; | |
1611 } | |
1612 | |
4699 | 1613 // For backward compatibility. |
1614 DEFALIAS (error_text, lasterr); | |
1615 DEFALIAS (__error_text__, lasterr); | |
1616 | |
5567 | 1617 DEFUN (lastwarn, args, nargout, |
3934 | 1618 "-*- texinfo -*-\n\ |
5567 | 1619 @deftypefn {Built-in Function} {[@var{msg}, @var{msgid}] =} lastwarn (@var{msg}, @var{msgid})\n\ |
3935 | 1620 Without any arguments, return the last warning message. With one\n\ |
5567 | 1621 argument, set the last warning message to @var{msg}. With two arguments,\n\ |
1622 also set the last message identifier.\n\ | |
3934 | 1623 @end deftypefn") |
1624 { | |
1625 octave_value_list retval; | |
1626 | |
1627 int argc = args.length () + 1; | |
1628 | |
5567 | 1629 if (argc < 4) |
1630 { | |
1631 string_vector argv = args.make_argv ("lastwarn"); | |
1632 | |
1633 if (! error_state) | |
1634 { | |
1635 std::string prev_warning_id = Vlast_warning_id; | |
1636 std::string prev_warning_message = Vlast_warning_message; | |
1637 | |
1638 if (argc > 2) | |
1639 Vlast_warning_id = argv(2); | |
3934 | 1640 |
5567 | 1641 if (argc > 1) |
1642 Vlast_warning_message = argv(1); | |
1643 | |
1644 if (argc == 1 || nargout > 0) | |
1645 { | |
5582 | 1646 warning_state = 0; |
5567 | 1647 retval(1) = prev_warning_id; |
1648 retval(0) = prev_warning_message; | |
1649 } | |
1650 } | |
1651 else | |
1652 error ("lastwarn: expecting arguments to be character strings"); | |
1653 } | |
3934 | 1654 else |
5823 | 1655 print_usage (); |
3934 | 1656 |
1657 return retval; | |
897 | 1658 } |
1659 | |
1957 | 1660 DEFUN (usage, args, , |
3373 | 1661 "-*- texinfo -*-\n\ |
1662 @deftypefn {Built-in Function} {} usage (@var{msg})\n\ | |
1663 Print the message @var{msg}, prefixed by the string @samp{usage: }, and\n\ | |
1664 set Octave's internal error state such that control will return to the\n\ | |
1665 top level without evaluating any more commands. This is useful for\n\ | |
1666 aborting from functions.\n\ | |
1667 \n\ | |
1668 After @code{usage} is evaluated, Octave will print a traceback of all\n\ | |
1669 the function calls leading to the usage message.\n\ | |
899 | 1670 \n\ |
3373 | 1671 You should use this function for reporting problems errors that result\n\ |
1672 from an improper call to a function, such as calling a function with an\n\ | |
1673 incorrect number of arguments, or with arguments of the wrong type. For\n\ | |
1674 example, most functions distributed with Octave begin with code like\n\ | |
1675 this\n\ | |
1676 \n\ | |
1677 @example\n\ | |
1678 @group\n\ | |
1679 if (nargin != 2)\n\ | |
1680 usage (\"foo (a, b)\");\n\ | |
1681 endif\n\ | |
1682 @end group\n\ | |
1683 @end example\n\ | |
1684 \n\ | |
1685 @noindent\n\ | |
1686 to check for the proper number of arguments.\n\ | |
1687 @end deftypefn") | |
899 | 1688 { |
3934 | 1689 octave_value_list retval; |
5567 | 1690 handle_message (usage_with_id, "", "unknown", args); |
3934 | 1691 return retval; |
899 | 1692 } |
1693 | |
5794 | 1694 DEFUN (beep_on_error, args, nargout, |
1695 "-*- texinfo -*-\n\ | |
1696 @deftypefn {Built-in Function} {@var{val} =} beep_on_error ()\n\ | |
1697 @deftypefnx {Built-in Function} {@var{old_val} =} beep_on_error (@var{new_val})\n\ | |
1698 Query or set the internal variable that controls whether Octave will try\n\ | |
1699 to ring the terminal bell before printing an error message.\n\ | |
1700 @end deftypefn") | |
3707 | 1701 { |
5794 | 1702 return SET_INTERNAL_VARIABLE (beep_on_error); |
3707 | 1703 } |
1704 | |
5794 | 1705 DEFUN (debug_on_error, args, nargout, |
3373 | 1706 "-*- texinfo -*-\n\ |
5794 | 1707 @deftypefn {Built-in Function} {@var{val} =} debug_on_error ()\n\ |
1708 @deftypefnx {Built-in Function} {@var{old_val} =} debug_on_error (@var{new_val})\n\ | |
1709 Query or set the internal variable that controls whether Octave will try\n\ | |
3707 | 1710 to enter the debugger when an error is encountered. This will also\n\ |
1711 inhibit printing of the normal traceback message (you will only see\n\ | |
5794 | 1712 the top-level error message).\n\ |
1713 @end deftypefn") | |
1714 { | |
1715 return SET_INTERNAL_VARIABLE (debug_on_error); | |
1716 } | |
3707 | 1717 |
5794 | 1718 DEFUN (debug_on_warning, args, nargout, |
3707 | 1719 "-*- texinfo -*-\n\ |
5794 | 1720 @deftypefn {Built-in Function} {@var{val} =} debug_on_warning ()\n\ |
1721 @deftypefnx {Built-in Function} {@var{old_val} =} debug_on_warning (@var{new_val})\n\ | |
1722 Query or set the internal variable that controls whether Octave will try\n\ | |
1723 to enter the debugger when a warning is encountered.\n\ | |
1724 @end deftypefn") | |
1725 { | |
1726 return SET_INTERNAL_VARIABLE (debug_on_warning); | |
2174 | 1727 } |
1728 | |
1 | 1729 /* |
1730 ;;; Local Variables: *** | |
1731 ;;; mode: C++ *** | |
1732 ;;; End: *** | |
1733 */ |