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