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