1
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
1
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
1
|
26 #endif |
|
27 |
1343
|
28 #include <cstdarg> |
1633
|
29 #include <cstring> |
1343
|
30 |
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). |
5794
|
56 static bool Vdebug_on_error = false; |
3707
|
57 |
|
58 // TRUE means that Octave will try to enter the debugger when a warning |
|
59 // is encountered. |
5794
|
60 static 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 |
|
235 if (curr_statement) |
|
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(); |
|
244 Vlast_error_line = curr_statement->line (); |
|
245 Vlast_error_column = curr_statement->column (); |
|
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 { |
|
425 if (curr_statement) |
|
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 |
5743
|
442 if (curr_statement) |
|
443 { |
|
444 l = curr_statement->line (); |
|
445 c = curr_statement->column (); |
4976
|
446 } |
3708
|
447 } |
3707
|
448 |
4976
|
449 if (nm.empty ()) |
|
450 { |
|
451 if (l > 0 && c > 0) |
|
452 pr_where_1 ("%s: near line %d, column %d:", name, l, c); |
|
453 } |
3708
|
454 else |
4976
|
455 { |
|
456 if (l > 0 && c > 0) |
|
457 pr_where_1 ("%s: in %s near line %d, column %d:", |
|
458 name, nm.c_str (), l, c); |
|
459 else |
|
460 pr_where_1 ("%s: in %s", name, nm.c_str ()); |
|
461 } |
3707
|
462 |
4719
|
463 if (print_code) |
|
464 { |
5775
|
465 // FIXME -- Note that the column number is probably |
4719
|
466 // not going to mean much here since the code is being |
|
467 // reproduced from the parse tree, and we are only showing |
|
468 // one statement even if there were multiple statements on |
|
469 // the original source line. |
3707
|
470 |
5765
|
471 std::ostringstream output_buf; |
3708
|
472 |
4719
|
473 output_buf << std::endl; |
3707
|
474 |
4719
|
475 tree_print_code tpc (output_buf, ">>> "); |
|
476 |
|
477 curr_statement->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) |
|
623 retval = 2; |
|
624 |
|
625 return retval; |
|
626 } |
|
627 |
|
628 static void |
|
629 warning_1 (const char *id, const char *fmt, va_list args) |
|
630 { |
|
631 int warn_opt = warning_enabled (id); |
|
632 |
|
633 if (warn_opt == 2) |
|
634 { |
|
635 // Handle this warning as an error. |
|
636 |
5998
|
637 error_2 (id, fmt, args); |
5567
|
638 } |
|
639 else if (warn_opt == 1) |
3934
|
640 { |
4014
|
641 if (curr_sym_tab != top_level_sym_tab |
5567
|
642 && Vbacktrace_on_warning |
4452
|
643 && ! warning_state |
|
644 && ! discard_warning_messages) |
4719
|
645 pr_where ("warning", false); |
3986
|
646 |
5567
|
647 vwarning ("warning", id, fmt, args); |
3707
|
648 |
3935
|
649 warning_state = 1; |
|
650 |
3934
|
651 if ((interactive || forced_interactive) |
5743
|
652 && Vdebug_on_warning |
5744
|
653 && octave_call_stack::caller_user_script_or_function ()) |
3934
|
654 { |
|
655 unwind_protect_bool (Vdebug_on_warning); |
|
656 Vdebug_on_warning = false; |
3707
|
657 |
3934
|
658 do_keyboard (octave_value_list ()); |
3707
|
659 |
3934
|
660 unwind_protect::run (); |
|
661 } |
3707
|
662 } |
1
|
663 } |
|
664 |
|
665 void |
6338
|
666 vwarning (const char *fmt, va_list args) |
|
667 { |
|
668 warning_1 ("", fmt, args); |
|
669 } |
|
670 |
|
671 void |
5567
|
672 warning (const char *fmt, ...) |
|
673 { |
|
674 va_list args; |
|
675 va_start (args, fmt); |
6338
|
676 vwarning (fmt, args); |
5567
|
677 va_end (args); |
|
678 } |
|
679 |
|
680 void |
6338
|
681 vwarning_with_id (const char *id, const char *fmt, va_list args) |
|
682 { |
|
683 warning_1 (id, fmt, args); |
|
684 } |
|
685 |
|
686 void |
5567
|
687 warning_with_id (const char *id, const char *fmt, ...) |
|
688 { |
|
689 va_list args; |
|
690 va_start (args, fmt); |
6338
|
691 vwarning_with_id (id, fmt, args); |
5567
|
692 va_end (args); |
|
693 } |
|
694 |
|
695 void |
6338
|
696 vparse_error (const char *fmt, va_list args) |
|
697 { |
|
698 error_1 (std::cerr, 0, "", fmt, args); |
|
699 } |
|
700 |
|
701 void |
1005
|
702 parse_error (const char *fmt, ...) |
|
703 { |
|
704 va_list args; |
|
705 va_start (args, fmt); |
6338
|
706 vparse_error (fmt, args); |
5567
|
707 va_end (args); |
|
708 } |
|
709 |
|
710 void |
6338
|
711 vparse_error_with_id (const char *id, const char *fmt, va_list args) |
|
712 { |
|
713 error_1 (std::cerr, 0, id, fmt, args); |
|
714 } |
|
715 |
|
716 void |
5567
|
717 parse_error_with_id (const char *id, const char *fmt, ...) |
|
718 { |
|
719 va_list args; |
|
720 va_start (args, fmt); |
6338
|
721 vparse_error_with_id (id, fmt, args); |
1
|
722 va_end (args); |
|
723 } |
|
724 |
189
|
725 void |
6361
|
726 rethrow_error (const char *id, const char *fmt, ...) |
|
727 { |
|
728 va_list args; |
|
729 va_start (args, fmt); |
|
730 error_1 (std::cerr, "error", id, fmt, args); |
|
731 va_end (args); |
|
732 } |
|
733 |
|
734 void |
1
|
735 panic (const char *fmt, ...) |
|
736 { |
|
737 va_list args; |
|
738 va_start (args, fmt); |
4699
|
739 buffer_error_messages = 0; |
3815
|
740 discard_error_messages = false; |
5567
|
741 verror (false, std::cerr, "panic", "", fmt, args); |
1
|
742 va_end (args); |
|
743 abort (); |
|
744 } |
|
745 |
4732
|
746 static void |
|
747 defun_usage_message_1 (const char *fmt, ...) |
|
748 { |
|
749 va_list args; |
|
750 va_start (args, fmt); |
5567
|
751 error_1 (octave_stdout, 0, "", fmt, args); |
4732
|
752 va_end (args); |
|
753 } |
|
754 |
|
755 void |
|
756 defun_usage_message (const std::string& msg) |
|
757 { |
|
758 defun_usage_message_1 ("%s", msg.c_str ()); |
|
759 } |
|
760 |
5567
|
761 typedef void (*error_fun)(const char *, const char *, ...); |
1489
|
762 |
2086
|
763 extern octave_value_list Fsprintf (const octave_value_list&, int); |
1489
|
764 |
3934
|
765 static std::string |
5567
|
766 handle_message (error_fun f, const char *id, const char *msg, |
|
767 const octave_value_list& args) |
528
|
768 { |
3934
|
769 std::string retval; |
528
|
770 |
3523
|
771 std::string tstr; |
1728
|
772 |
528
|
773 int nargin = args.length (); |
|
774 |
2745
|
775 if (nargin > 0) |
528
|
776 { |
3066
|
777 octave_value arg; |
|
778 |
|
779 if (nargin > 1) |
|
780 { |
|
781 octave_value_list tmp = Fsprintf (args, 1); |
|
782 arg = tmp(0); |
|
783 } |
|
784 else |
|
785 arg = args(0); |
2745
|
786 |
|
787 if (arg.is_defined ()) |
528
|
788 { |
2745
|
789 if (arg.is_string ()) |
|
790 { |
|
791 tstr = arg.string_value (); |
|
792 msg = tstr.c_str (); |
|
793 |
|
794 if (! msg) |
|
795 return retval; |
|
796 } |
|
797 else if (arg.is_empty ()) |
528
|
798 return retval; |
|
799 } |
|
800 } |
|
801 |
1489
|
802 // Ugh. |
|
803 |
|
804 int len = strlen (msg); |
|
805 if (msg[len - 1] == '\n') |
|
806 { |
|
807 if (len > 1) |
|
808 { |
|
809 char *tmp_msg = strsave (msg); |
|
810 tmp_msg[len - 1] = '\0'; |
5567
|
811 f (id, "%s\n", tmp_msg); |
3934
|
812 retval = tmp_msg; |
1489
|
813 delete [] tmp_msg; |
|
814 } |
|
815 } |
|
816 else |
3934
|
817 { |
5567
|
818 f (id, "%s", msg); |
3934
|
819 retval = msg; |
|
820 } |
528
|
821 |
|
822 return retval; |
|
823 } |
|
824 |
6361
|
825 DEFUN (rethrow, args, , |
|
826 "-*- texinfo -*-\n\ |
|
827 @deftypefn {Built-in Function} {} rethrow (@var{err})\n\ |
|
828 Reissues a previous error as defined by @var{err}. @var{err} is a structure\n\ |
|
829 that must contain at least the 'message' and 'identifier' fields. @var{err}\n\ |
|
830 can also contain a field 'stack' that gives information on the assumed\n\ |
|
831 location of the error. Typically @var{err} is returned from\n\ |
|
832 @code{lasterror}.\n\ |
|
833 @seealso{lasterror, lasterr, error}\n\ |
|
834 @end deftypefn") |
|
835 { |
|
836 octave_value retval; |
|
837 int nargin = args.length(); |
|
838 |
|
839 if (nargin != 1) |
|
840 print_usage(); |
|
841 else |
|
842 { |
|
843 Octave_map err = args(0).map_value(); |
|
844 |
|
845 if (!error_state) |
|
846 { |
|
847 if (err.contains("message") && err.contains("identifier")) |
|
848 { |
|
849 std::string msg = err.contents("message")(0).string_value(); |
|
850 std::string id = err.contents("identifier")(0).string_value(); |
|
851 int len = msg.length(); |
|
852 std::string file; |
|
853 std::string nm; |
|
854 int l = -1; |
|
855 int c = -1; |
|
856 |
|
857 if (err.contains("stack")) |
|
858 { |
|
859 Octave_map err_stack = err.contents("stack")(0).map_value(); |
|
860 |
|
861 if (err_stack.contains("file")) |
|
862 file = err_stack.contents("file")(0).string_value(); |
|
863 if (err_stack.contains("name")) |
|
864 nm = err_stack.contents("name")(0).string_value(); |
|
865 if (err_stack.contains("line")) |
|
866 l = err_stack.contents("line")(0).nint_value(); |
|
867 if (err_stack.contains("column")) |
|
868 c = err_stack.contents("column")(0).nint_value(); |
|
869 } |
|
870 |
|
871 // Ugh. |
|
872 char *tmp_msg = strsave (msg.c_str()); |
|
873 if (tmp_msg[len-1] == '\n') |
|
874 { |
|
875 if (len > 1) |
|
876 { |
|
877 tmp_msg[len - 1] = '\0'; |
|
878 rethrow_error (id.c_str(), "%s\n", tmp_msg); |
|
879 } |
|
880 } |
|
881 else |
|
882 rethrow_error (id.c_str(), "%s", tmp_msg); |
|
883 delete [] tmp_msg; |
|
884 |
|
885 // FIXME: Need to restore the stack as rethrow_error sets it? |
|
886 Vlast_error_file = file; |
|
887 Vlast_error_name = nm; |
|
888 Vlast_error_line = l; |
|
889 Vlast_error_column = c; |
|
890 |
|
891 if (err.contains("stack")) |
|
892 { |
|
893 if (file.empty ()) |
|
894 { |
|
895 if (nm.empty ()) |
|
896 { |
|
897 if (l > 0) |
|
898 if (c > 0) |
|
899 pr_where_1 ("error: near line %d, column %d", |
|
900 l, c); |
|
901 else |
|
902 pr_where_1 ("error: near line %d", l ); |
|
903 } |
|
904 else |
|
905 { |
|
906 if (l > 0) |
|
907 if (c > 0) |
|
908 pr_where_1 ("error: called from `%s' near line %d, column %d", |
|
909 nm.c_str(), l, c); |
|
910 else |
|
911 pr_where_1 ("error: called from `%d' near line %d", nm.c_str(), l ); |
|
912 } |
|
913 } |
|
914 else |
|
915 { |
|
916 if (nm.empty ()) |
|
917 { |
|
918 if (l > 0) |
|
919 if (c > 0) |
|
920 pr_where_1 ("error: in file %s near line %d, column %d", |
|
921 file.c_str(), l, c); |
|
922 else |
|
923 pr_where_1 ("error: in file %s near line %d", file.c_str(), l ); |
|
924 } |
|
925 else |
|
926 { |
|
927 if (l > 0) |
|
928 if (c > 0) |
|
929 pr_where_1 ("error: called from `%s' in file %s near line %d, column %d", |
|
930 nm.c_str(), file.c_str(), l, c); |
|
931 else |
|
932 pr_where_1 ("error: called from `%d' in file %s near line %d", nm.c_str(), file.c_str(), l ); |
|
933 } |
|
934 } |
|
935 } |
|
936 } |
|
937 else |
|
938 error ("rethrow: structure must contain the fields 'message and 'identifier'"); |
|
939 } |
|
940 } |
|
941 return retval; |
|
942 } |
|
943 |
1957
|
944 DEFUN (error, args, , |
3373
|
945 "-*- texinfo -*-\n\ |
|
946 @deftypefn {Built-in Function} {} error (@var{template}, @dots{})\n\ |
5781
|
947 Format the optional arguments under the control of the template string\n\ |
|
948 @var{template} using the same rules as the @code{printf} family of\n\ |
|
949 functions (@pxref{Formatted Output}) and print the resulting message\n\ |
|
950 on the @code{stderr} stream. The message is prefixed by the character\n\ |
|
951 string @samp{error: }.\n\ |
3373
|
952 \n\ |
|
953 Calling @code{error} also sets Octave's internal error state such that\n\ |
|
954 control will return to the top level without evaluating any more\n\ |
|
955 commands. This is useful for aborting from functions or scripts.\n\ |
897
|
956 \n\ |
3373
|
957 If the error message does not end with a new line character, Octave will\n\ |
|
958 print a traceback of all the function calls leading to the error. For\n\ |
|
959 example, given the following function definitions:\n\ |
|
960 \n\ |
|
961 @example\n\ |
|
962 @group\n\ |
|
963 function f () g () end\n\ |
|
964 function g () h () end\n\ |
|
965 function h () nargin == 1 || error (\"nargin != 1\"); end\n\ |
|
966 @end group\n\ |
|
967 @end example\n\ |
1489
|
968 \n\ |
3373
|
969 @noindent\n\ |
|
970 calling the function @code{f} will result in a list of messages that\n\ |
|
971 can help you to quickly locate the exact location of the error:\n\ |
1489
|
972 \n\ |
3373
|
973 @example\n\ |
|
974 @group\n\ |
|
975 f ()\n\ |
|
976 error: nargin != 1\n\ |
|
977 error: evaluating index expression near line 1, column 30\n\ |
|
978 error: evaluating binary operator `||' near line 1, column 27\n\ |
|
979 error: called from `h'\n\ |
|
980 error: called from `g'\n\ |
|
981 error: called from `f'\n\ |
|
982 @end group\n\ |
|
983 @end example\n\ |
|
984 \n\ |
|
985 If the error message ends in a new line character, Octave will print the\n\ |
|
986 message but will not display any traceback messages as it returns\n\ |
|
987 control to the top level. For example, modifying the error message\n\ |
|
988 in the previous example to end in a new line causes Octave to only print\n\ |
|
989 a single message:\n\ |
|
990 \n\ |
|
991 @example\n\ |
|
992 @group\n\ |
|
993 function h () nargin == 1 || error (\"nargin != 1\\n\"); end\n\ |
|
994 f ()\n\ |
|
995 error: nargin != 1\n\ |
|
996 @end group\n\ |
|
997 @end example\n\ |
|
998 @end deftypefn") |
897
|
999 { |
5775
|
1000 // FIXME -- need to extract and pass message id to |
5567
|
1001 // handle_message. |
|
1002 |
3934
|
1003 octave_value_list retval; |
5567
|
1004 handle_message (error_with_id, "", "unspecified error", args); |
3934
|
1005 return retval; |
1489
|
1006 } |
897
|
1007 |
5567
|
1008 DEFCMD (warning, args, nargout, |
3373
|
1009 "-*- texinfo -*-\n\ |
5781
|
1010 @deftypefn {Built-in Function} {} warning (@var{template}, @dots{})\n\ |
|
1011 @deftypefnx {Built-in Function} {} warning (@var{id}, @var{template})\n\ |
|
1012 Format the optional arguments under the control of the template string\n\ |
|
1013 @var{template} using the same rules as the @code{printf} family of\n\ |
|
1014 functions (@pxref{Formatted Output}) and print the resulting message\n\ |
|
1015 on the @code{stderr} stream. The message is prefixed by the character\n\ |
|
1016 string @samp{warning: }.\n\ |
|
1017 You should use this function when you want to notify the user\n\ |
3600
|
1018 of an unusual condition, but only when it makes sense for your program\n\ |
|
1019 to go on.\n\ |
5781
|
1020 \n\ |
|
1021 The optional message identifier allows users to enable or disable\n\ |
|
1022 warnings tagged by @var{id}. The special identifier @samp{\"all\"} may\n\ |
|
1023 be used to set the state of all warnings.\n\ |
|
1024 \n\ |
|
1025 @deftypefnx {Built-in Function} {} warning (\"on\", @var{id})\n\ |
|
1026 @deftypefnx {Built-in Function} {} warning (\"off\", @var{id})\n\ |
|
1027 @deftypefnx {Built-in Function} {} warning (\"error\", @var{id})\n\ |
|
1028 @deftypefnx {Built-in Function} {} warning (\"query\", @var{id})\n\ |
|
1029 Set or query the the state of a particular warning using the identifier\n\ |
|
1030 @var{id}. If the identifier is omitted, a value of @samp{\"all\"} is\n\ |
|
1031 assumed. If you set the state of a warning to @samp{\"error\"}, the\n\ |
|
1032 warning named by @var{id} is handled as if it were an error instead.\n\ |
5783
|
1033 @seealso{warning_ids}\n\ |
3373
|
1034 @end deftypefn") |
1489
|
1035 { |
5567
|
1036 octave_value retval; |
3934
|
1037 |
5567
|
1038 int nargin = args.length (); |
|
1039 int argc = nargin + 1; |
3934
|
1040 |
3935
|
1041 bool done = false; |
3934
|
1042 |
5567
|
1043 if (argc > 1 && args.all_strings_p ()) |
3935
|
1044 { |
|
1045 string_vector argv = args.make_argv ("warning"); |
|
1046 |
|
1047 if (! error_state) |
3934
|
1048 { |
5567
|
1049 std::string arg1 = argv(1); |
|
1050 std::string arg2 = "all"; |
|
1051 |
|
1052 if (argc == 3) |
|
1053 arg2 = argv(2); |
|
1054 |
|
1055 if (arg1 == "on" || arg1 == "off" || arg1 == "error") |
3934
|
1056 { |
5567
|
1057 Octave_map old_warning_options = warning_options; |
|
1058 |
|
1059 if (arg2 == "all") |
|
1060 { |
|
1061 Octave_map tmp; |
3934
|
1062 |
5567
|
1063 tmp.assign ("identifier", arg2); |
|
1064 tmp.assign ("state", arg1); |
|
1065 |
|
1066 warning_options = tmp; |
|
1067 |
3935
|
1068 done = true; |
|
1069 } |
5567
|
1070 else if (arg2 == "backtrace") |
|
1071 { |
|
1072 if (arg1 != "error") |
|
1073 { |
|
1074 Vbacktrace_on_warning = (arg1 == "on"); |
|
1075 done = true; |
|
1076 } |
|
1077 } |
|
1078 else if (arg2 == "debug") |
|
1079 { |
|
1080 if (arg1 != "error") |
|
1081 { |
5794
|
1082 Vdebug_on_warning = (arg1 == "on"); |
5567
|
1083 done = true; |
|
1084 } |
|
1085 } |
|
1086 else if (arg2 == "verbose") |
|
1087 { |
|
1088 if (arg1 != "error") |
|
1089 { |
|
1090 Vverbose_warning = (arg1 == "on"); |
|
1091 done = true; |
|
1092 } |
|
1093 } |
5582
|
1094 else if (arg2 == "quiet") |
|
1095 { |
|
1096 if (arg1 != "error") |
|
1097 { |
|
1098 Vquiet_warning = (arg1 == "on"); |
|
1099 done = true; |
|
1100 } |
|
1101 } |
5567
|
1102 else |
3935
|
1103 { |
5567
|
1104 if (arg2 == "last") |
|
1105 arg2 = Vlast_warning_id; |
|
1106 |
|
1107 if (arg2 == "all") |
5794
|
1108 initialize_warning_options (arg1); |
5567
|
1109 else |
|
1110 { |
|
1111 Cell ident = warning_options.contents ("identifier"); |
|
1112 Cell state = warning_options.contents ("state"); |
|
1113 |
|
1114 octave_idx_type nel = ident.numel (); |
|
1115 |
|
1116 bool found = false; |
|
1117 |
|
1118 for (octave_idx_type i = 0; i < nel; i++) |
|
1119 { |
|
1120 if (ident(i).string_value () == arg2) |
|
1121 { |
5775
|
1122 // FIXME -- if state for "all" is |
5567
|
1123 // same as arg1, we can simply remove the |
|
1124 // item from the list. |
|
1125 |
|
1126 state(i) = arg1; |
|
1127 warning_options.assign ("state", state); |
|
1128 found = true; |
|
1129 break; |
|
1130 } |
|
1131 } |
|
1132 |
|
1133 if (! found) |
|
1134 { |
5775
|
1135 // FIXME -- if state for "all" is |
5567
|
1136 // same as arg1, we don't need to do anything. |
|
1137 |
|
1138 ident.resize (dim_vector (1, nel+1)); |
|
1139 state.resize (dim_vector (1, nel+1)); |
|
1140 |
|
1141 ident(nel) = arg2; |
|
1142 state(nel) = arg1; |
|
1143 |
|
1144 warning_options.clear (); |
|
1145 |
|
1146 warning_options.assign ("identifier", ident); |
|
1147 warning_options.assign ("state", state); |
|
1148 } |
|
1149 } |
|
1150 |
3935
|
1151 done = true; |
|
1152 } |
5567
|
1153 |
|
1154 if (done && nargout > 0) |
6427
|
1155 retval = old_warning_options; |
5567
|
1156 } |
|
1157 else if (arg1 == "query") |
|
1158 { |
|
1159 if (arg2 == "all") |
|
1160 retval = warning_options; |
|
1161 else if (arg2 == "backtrace" || arg2 == "debug" |
5582
|
1162 || arg2 == "verbose" || arg2 == "quiet") |
5567
|
1163 { |
|
1164 Octave_map tmp; |
|
1165 tmp.assign ("identifier", arg2); |
|
1166 if (arg2 == "backtrace") |
|
1167 tmp.assign ("state", Vbacktrace_on_warning ? "on" : "off"); |
|
1168 else if (arg2 == "debug") |
|
1169 tmp.assign ("state", Vdebug_on_warning ? "on" : "off"); |
5582
|
1170 else if (arg2 == "verbose") |
|
1171 tmp.assign ("state", Vverbose_warning ? "on" : "off"); |
5567
|
1172 else |
5582
|
1173 tmp.assign ("state", Vquiet_warning ? "on" : "off"); |
|
1174 |
|
1175 retval = tmp; |
5567
|
1176 } |
|
1177 else |
3935
|
1178 { |
5567
|
1179 if (arg2 == "last") |
|
1180 arg2 = Vlast_warning_id; |
|
1181 |
|
1182 Cell ident = warning_options.contents ("identifier"); |
|
1183 Cell state = warning_options.contents ("state"); |
|
1184 |
|
1185 octave_idx_type nel = ident.numel (); |
|
1186 |
|
1187 bool found = false; |
|
1188 |
|
1189 std::string val; |
|
1190 |
|
1191 for (octave_idx_type i = 0; i < nel; i++) |
|
1192 { |
|
1193 if (ident(i).string_value () == arg2) |
|
1194 { |
|
1195 val = state(i).string_value (); |
|
1196 found = true; |
|
1197 break; |
|
1198 } |
|
1199 } |
|
1200 |
5781
|
1201 if (! found) |
|
1202 { |
|
1203 for (octave_idx_type i = 0; i < nel; i++) |
|
1204 { |
|
1205 if (ident(i).string_value () == "all") |
|
1206 { |
|
1207 val = state(i).string_value (); |
|
1208 found = true; |
|
1209 break; |
|
1210 } |
|
1211 } |
|
1212 } |
|
1213 |
5567
|
1214 if (found) |
|
1215 { |
|
1216 Octave_map tmp; |
|
1217 |
|
1218 tmp.assign ("identifier", arg2); |
|
1219 tmp.assign ("state", val); |
|
1220 |
|
1221 retval = tmp; |
|
1222 } |
|
1223 else |
5781
|
1224 error ("warning: unable to find default warning state!"); |
3935
|
1225 } |
5567
|
1226 |
|
1227 done = true; |
3935
|
1228 } |
3934
|
1229 } |
|
1230 } |
5567
|
1231 else if (argc == 1) |
|
1232 { |
|
1233 retval = warning_options; |
3934
|
1234 |
5567
|
1235 done = true; |
|
1236 } |
|
1237 else if (argc == 2) |
|
1238 { |
|
1239 octave_value arg = args(0); |
|
1240 |
|
1241 Octave_map old_warning_options = warning_options; |
|
1242 |
|
1243 if (arg.is_map ()) |
|
1244 { |
|
1245 Octave_map m = arg.map_value (); |
|
1246 |
|
1247 if (m.contains ("identifier") && m.contains ("state")) |
6427
|
1248 warning_options = m; |
5567
|
1249 else |
|
1250 error ("warning: expecting structure with fields `identifier' and `state'"); |
|
1251 |
|
1252 done = true; |
|
1253 |
|
1254 if (nargout > 0) |
|
1255 retval = old_warning_options; |
|
1256 } |
|
1257 } |
|
1258 |
|
1259 if (! (error_state || done)) |
3935
|
1260 { |
5567
|
1261 octave_value_list nargs = args; |
|
1262 |
|
1263 std::string id; |
|
1264 |
|
1265 if (nargin > 1) |
|
1266 { |
|
1267 std::string arg1 = args(0).string_value (); |
|
1268 |
|
1269 if (! error_state) |
|
1270 { |
|
1271 if (arg1.find ('%') == NPOS) |
|
1272 { |
|
1273 id = arg1; |
|
1274 |
|
1275 nargs.resize (nargin-1); |
|
1276 |
|
1277 for (int i = 1; i < nargin; i++) |
|
1278 nargs(i-1) = args(i); |
|
1279 } |
|
1280 } |
|
1281 else |
|
1282 return retval; |
|
1283 } |
|
1284 |
|
1285 // handle_message. |
|
1286 |
3935
|
1287 std::string prev_msg = Vlast_warning_message; |
|
1288 |
5567
|
1289 std::string curr_msg = handle_message (warning_with_id, id.c_str (), |
|
1290 "unspecified warning", nargs); |
3935
|
1291 |
|
1292 if (nargout > 0) |
5567
|
1293 retval = prev_msg; |
3935
|
1294 } |
|
1295 |
3934
|
1296 return retval; |
|
1297 } |
|
1298 |
5904
|
1299 void |
5794
|
1300 disable_warning (const std::string& id) |
|
1301 { |
|
1302 octave_value_list args; |
|
1303 |
|
1304 args(1) = id; |
|
1305 args(0) = "off"; |
|
1306 |
|
1307 Fwarning (args, 0); |
|
1308 } |
|
1309 |
|
1310 void |
|
1311 initialize_default_warning_state (void) |
|
1312 { |
|
1313 initialize_warning_options ("on"); |
|
1314 |
|
1315 // Most people will want to have the following disabled. |
|
1316 |
|
1317 disable_warning ("Octave:array-to-scalar"); |
|
1318 disable_warning ("Octave:array-to-vector"); |
|
1319 disable_warning ("Octave:empty-list-elements"); |
|
1320 disable_warning ("Octave:fortran-indexing"); |
|
1321 disable_warning ("Octave:imag-to-real"); |
|
1322 disable_warning ("Octave:matlab-incompatible"); |
|
1323 disable_warning ("Octave:missing-semicolon"); |
|
1324 disable_warning ("Octave:neg-dim-as-zero"); |
|
1325 disable_warning ("Octave:resize-on-range-error"); |
|
1326 disable_warning ("Octave:separator-insert"); |
|
1327 disable_warning ("Octave:single-quote-string"); |
|
1328 disable_warning ("Octave:str-to-num"); |
|
1329 disable_warning ("Octave:string-concat"); |
|
1330 disable_warning ("Octave:variable-switch-label"); |
|
1331 } |
|
1332 |
6361
|
1333 DEFUN (lasterror, args, , |
|
1334 "-*- texinfo -*-\n\ |
|
1335 @deftypefn {Built-in Function} {@var{err} =} lasterror (@var{err})\n\ |
|
1336 @deftypefnx {Built-in Function} {} lasterror ('reset')\n\ |
|
1337 Returns or sets the last error message. Called without any arguments\n\ |
|
1338 returns a structure containing the last error message, as well as other\n\ |
|
1339 information related to this error. The elements of this structure are:\n\ |
|
1340 \n\ |
|
1341 @table @asis\n\ |
|
1342 @item 'message'\n\ |
|
1343 The text of the last error message\n\ |
|
1344 @item 'identifier'\n\ |
|
1345 The message identifier of this error message\n\ |
|
1346 @item 'stack'\n\ |
|
1347 A structure containing information on where the message occured. This might\n\ |
|
1348 be an empty structure if this in the case where this information can not\n\ |
|
1349 be obtained. The fields of this structure are:\n\ |
|
1350 \n\ |
|
1351 @table @asis\n\ |
|
1352 @item 'file'\n\ |
|
1353 The name of the file where the error occurred\n\ |
|
1354 @item 'name'\n\ |
|
1355 The name of function in which the error occured\n\ |
|
1356 @item 'line'\n\ |
|
1357 The line number at which the error occured\n\ |
|
1358 @item 'column'\n\ |
|
1359 An optional field with the column number at which the error occurred\n\ |
|
1360 @end table\n\ |
|
1361 @end table\n\ |
|
1362 \n\ |
|
1363 The @var{err} structure may also be passed to @code{lasterror} to set the\n\ |
|
1364 information about the last error. The only constraint on @var{err} in that\n\ |
|
1365 case is that it is a scalar structure. Any fields of @var{err} that match\n\ |
|
1366 the above are set to the value passed in @var{err}, while other fields are\n\ |
|
1367 set to their default values.\n\ |
|
1368 \n\ |
|
1369 If @code{lasterror} is called with the argument 'reset', all values take\n\ |
|
1370 their default values.\n\ |
|
1371 @end deftypefn") |
|
1372 { |
|
1373 octave_value retval; |
|
1374 int nargin = args.length(); |
|
1375 |
|
1376 if (nargin < 2) |
|
1377 { |
|
1378 Octave_map err; |
|
1379 |
|
1380 err.assign ("message", Vlast_error_message); |
|
1381 err.assign ("identifier", Vlast_error_id); |
|
1382 |
|
1383 if (! (Vlast_error_file.empty() && Vlast_error_name.empty() && |
|
1384 Vlast_error_line < 0 && Vlast_error_column < 0)) |
|
1385 { |
|
1386 Octave_map err_stack; |
|
1387 |
|
1388 err_stack.assign ("file", Vlast_error_file); |
|
1389 err_stack.assign ("name", Vlast_error_name); |
|
1390 err_stack.assign ("line", Vlast_error_line); |
|
1391 err_stack.assign ("column", Vlast_error_column); |
|
1392 |
|
1393 err.assign ("stack", octave_value (err_stack)); |
|
1394 } |
|
1395 else |
|
1396 { |
|
1397 string_vector sv(4); |
|
1398 sv[0] = "file"; |
|
1399 sv[1] = "name"; |
|
1400 sv[2] = "line"; |
|
1401 sv[3] = "column"; |
|
1402 err.assign ("stack", octave_value (Octave_map (dim_vector (0,1), |
|
1403 sv))); |
|
1404 } |
|
1405 |
|
1406 if (nargin == 1) |
|
1407 { |
|
1408 if (args(0).is_string()) |
|
1409 { |
|
1410 if (args(0).string_value() == "reset") |
|
1411 { |
|
1412 Vlast_error_message = std::string(); |
|
1413 Vlast_error_id = std::string(); |
|
1414 Vlast_error_file = std::string(); |
|
1415 Vlast_error_name = std::string(); |
|
1416 Vlast_error_line = -1; |
|
1417 Vlast_error_column = -1; |
|
1418 } |
|
1419 else |
|
1420 error("lasterror: unrecognized string argument"); |
|
1421 } |
|
1422 else if (args(0).is_map ()) |
|
1423 { |
|
1424 Octave_map new_err = args(0).map_value(); |
|
1425 std::string new_error_message; |
|
1426 std::string new_error_id; |
|
1427 std::string new_error_file; |
|
1428 std::string new_error_name; |
|
1429 int new_error_line = -1; |
|
1430 int new_error_column = -1; |
|
1431 |
|
1432 if (!error_state && new_err.contains("message")) |
|
1433 { |
|
1434 const std::string tmp = |
|
1435 new_err.contents("message")(0).string_value(); |
|
1436 new_error_message = tmp; |
|
1437 } |
|
1438 |
|
1439 if (!error_state && new_err.contains("identifier")) |
|
1440 { |
|
1441 const std::string tmp = |
|
1442 new_err.contents("identifier")(0).string_value(); |
|
1443 new_error_id = tmp; |
|
1444 } |
|
1445 |
|
1446 if (!error_state && new_err.contains("stack")) |
|
1447 { |
|
1448 Octave_map new_err_stack = |
|
1449 new_err.contents("identifier")(0).map_value(); |
|
1450 |
|
1451 if (!error_state && new_err_stack.contains("file")) |
|
1452 { |
|
1453 const std::string tmp = |
|
1454 new_err_stack.contents("file")(0).string_value(); |
|
1455 new_error_file = tmp; |
|
1456 } |
|
1457 |
|
1458 if (!error_state && new_err_stack.contains("name")) |
|
1459 { |
|
1460 const std::string tmp = |
|
1461 new_err_stack.contents("name")(0).string_value(); |
|
1462 new_error_name = tmp; |
|
1463 } |
|
1464 |
|
1465 if (!error_state && new_err_stack.contains("line")) |
|
1466 { |
|
1467 const int tmp = |
|
1468 new_err_stack.contents("line")(0).nint_value(); |
|
1469 new_error_line = tmp; |
|
1470 } |
|
1471 |
|
1472 if (!error_state && new_err_stack.contains("column")) |
|
1473 { |
|
1474 const int tmp = |
|
1475 new_err_stack.contents("column")(0).nint_value(); |
|
1476 new_error_column = tmp; |
|
1477 } |
|
1478 } |
|
1479 |
|
1480 if (! error_state) |
|
1481 { |
|
1482 Vlast_error_message = new_error_message; |
|
1483 Vlast_error_id = new_error_id; |
|
1484 Vlast_error_file = new_error_file; |
|
1485 Vlast_error_name = new_error_name; |
|
1486 Vlast_error_line = new_error_line; |
|
1487 Vlast_error_column = new_error_column; |
|
1488 } |
|
1489 } |
|
1490 else |
|
1491 error ("lasterror: argument must be a structure or a string"); |
|
1492 } |
|
1493 |
|
1494 if (!error_state) |
|
1495 retval = err; |
|
1496 } |
|
1497 else |
|
1498 print_usage (); |
|
1499 |
|
1500 return retval; |
|
1501 } |
|
1502 |
5567
|
1503 DEFUN (lasterr, args, nargout, |
3935
|
1504 "-*- texinfo -*-\n\ |
5567
|
1505 @deftypefn {Built-in Function} {[@var{msg}, @var{msgid}] =} lasterr (@var{msg}, @var{msgid})\n\ |
3935
|
1506 Without any arguments, return the last error message. With one\n\ |
5567
|
1507 argument, set the last error message to @var{msg}. With two arguments,\n\ |
|
1508 also set the last message identifier.\n\ |
3935
|
1509 @end deftypefn") |
|
1510 { |
|
1511 octave_value_list retval; |
|
1512 |
|
1513 int argc = args.length () + 1; |
|
1514 |
5567
|
1515 if (argc < 4) |
5335
|
1516 { |
|
1517 string_vector argv = args.make_argv ("lasterr"); |
3935
|
1518 |
5335
|
1519 if (! error_state) |
|
1520 { |
5567
|
1521 std::string prev_error_id = Vlast_error_id; |
|
1522 std::string prev_error_message = Vlast_error_message; |
|
1523 |
|
1524 if (argc > 2) |
|
1525 Vlast_error_id = argv(2); |
|
1526 |
|
1527 if (argc > 1) |
5335
|
1528 Vlast_error_message = argv(1); |
5567
|
1529 |
|
1530 if (argc == 1 || nargout > 0) |
|
1531 { |
|
1532 retval(1) = prev_error_id; |
|
1533 retval(0) = prev_error_message; |
|
1534 } |
5335
|
1535 } |
5567
|
1536 else |
5582
|
1537 error ("lasterr: expecting arguments to be character strings"); |
5335
|
1538 } |
3935
|
1539 else |
5823
|
1540 print_usage (); |
3935
|
1541 |
|
1542 return retval; |
|
1543 } |
|
1544 |
4699
|
1545 // For backward compatibility. |
|
1546 DEFALIAS (error_text, lasterr); |
|
1547 DEFALIAS (__error_text__, lasterr); |
|
1548 |
5567
|
1549 DEFUN (lastwarn, args, nargout, |
3934
|
1550 "-*- texinfo -*-\n\ |
5567
|
1551 @deftypefn {Built-in Function} {[@var{msg}, @var{msgid}] =} lastwarn (@var{msg}, @var{msgid})\n\ |
3935
|
1552 Without any arguments, return the last warning message. With one\n\ |
5567
|
1553 argument, set the last warning message to @var{msg}. With two arguments,\n\ |
|
1554 also set the last message identifier.\n\ |
3934
|
1555 @end deftypefn") |
|
1556 { |
|
1557 octave_value_list retval; |
|
1558 |
|
1559 int argc = args.length () + 1; |
|
1560 |
5567
|
1561 if (argc < 4) |
|
1562 { |
|
1563 string_vector argv = args.make_argv ("lastwarn"); |
|
1564 |
|
1565 if (! error_state) |
|
1566 { |
|
1567 std::string prev_warning_id = Vlast_warning_id; |
|
1568 std::string prev_warning_message = Vlast_warning_message; |
|
1569 |
|
1570 if (argc > 2) |
|
1571 Vlast_warning_id = argv(2); |
3934
|
1572 |
5567
|
1573 if (argc > 1) |
|
1574 Vlast_warning_message = argv(1); |
|
1575 |
|
1576 if (argc == 1 || nargout > 0) |
|
1577 { |
5582
|
1578 warning_state = 0; |
5567
|
1579 retval(1) = prev_warning_id; |
|
1580 retval(0) = prev_warning_message; |
|
1581 } |
|
1582 } |
|
1583 else |
|
1584 error ("lastwarn: expecting arguments to be character strings"); |
|
1585 } |
3934
|
1586 else |
5823
|
1587 print_usage (); |
3934
|
1588 |
|
1589 return retval; |
897
|
1590 } |
|
1591 |
1957
|
1592 DEFUN (usage, args, , |
3373
|
1593 "-*- texinfo -*-\n\ |
|
1594 @deftypefn {Built-in Function} {} usage (@var{msg})\n\ |
|
1595 Print the message @var{msg}, prefixed by the string @samp{usage: }, and\n\ |
|
1596 set Octave's internal error state such that control will return to the\n\ |
|
1597 top level without evaluating any more commands. This is useful for\n\ |
|
1598 aborting from functions.\n\ |
|
1599 \n\ |
|
1600 After @code{usage} is evaluated, Octave will print a traceback of all\n\ |
|
1601 the function calls leading to the usage message.\n\ |
899
|
1602 \n\ |
3373
|
1603 You should use this function for reporting problems errors that result\n\ |
|
1604 from an improper call to a function, such as calling a function with an\n\ |
|
1605 incorrect number of arguments, or with arguments of the wrong type. For\n\ |
|
1606 example, most functions distributed with Octave begin with code like\n\ |
|
1607 this\n\ |
|
1608 \n\ |
|
1609 @example\n\ |
|
1610 @group\n\ |
|
1611 if (nargin != 2)\n\ |
|
1612 usage (\"foo (a, b)\");\n\ |
|
1613 endif\n\ |
|
1614 @end group\n\ |
|
1615 @end example\n\ |
|
1616 \n\ |
|
1617 @noindent\n\ |
|
1618 to check for the proper number of arguments.\n\ |
|
1619 @end deftypefn") |
899
|
1620 { |
3934
|
1621 octave_value_list retval; |
5567
|
1622 handle_message (usage_with_id, "", "unknown", args); |
3934
|
1623 return retval; |
899
|
1624 } |
|
1625 |
5794
|
1626 DEFUN (beep_on_error, args, nargout, |
|
1627 "-*- texinfo -*-\n\ |
|
1628 @deftypefn {Built-in Function} {@var{val} =} beep_on_error ()\n\ |
|
1629 @deftypefnx {Built-in Function} {@var{old_val} =} beep_on_error (@var{new_val})\n\ |
|
1630 Query or set the internal variable that controls whether Octave will try\n\ |
|
1631 to ring the terminal bell before printing an error message.\n\ |
|
1632 @end deftypefn") |
3707
|
1633 { |
5794
|
1634 return SET_INTERNAL_VARIABLE (beep_on_error); |
3707
|
1635 } |
|
1636 |
5794
|
1637 DEFUN (debug_on_error, args, nargout, |
3373
|
1638 "-*- texinfo -*-\n\ |
5794
|
1639 @deftypefn {Built-in Function} {@var{val} =} debug_on_error ()\n\ |
|
1640 @deftypefnx {Built-in Function} {@var{old_val} =} debug_on_error (@var{new_val})\n\ |
|
1641 Query or set the internal variable that controls whether Octave will try\n\ |
3707
|
1642 to enter the debugger when an error is encountered. This will also\n\ |
|
1643 inhibit printing of the normal traceback message (you will only see\n\ |
5794
|
1644 the top-level error message).\n\ |
|
1645 @end deftypefn") |
|
1646 { |
|
1647 return SET_INTERNAL_VARIABLE (debug_on_error); |
|
1648 } |
3707
|
1649 |
5794
|
1650 DEFUN (debug_on_warning, args, nargout, |
3707
|
1651 "-*- texinfo -*-\n\ |
5794
|
1652 @deftypefn {Built-in Function} {@var{val} =} debug_on_warning ()\n\ |
|
1653 @deftypefnx {Built-in Function} {@var{old_val} =} debug_on_warning (@var{new_val})\n\ |
|
1654 Query or set the internal variable that controls whether Octave will try\n\ |
|
1655 to enter the debugger when a warning is encountered.\n\ |
|
1656 @end deftypefn") |
|
1657 { |
|
1658 return SET_INTERNAL_VARIABLE (debug_on_warning); |
2174
|
1659 } |
|
1660 |
1
|
1661 /* |
|
1662 ;;; Local Variables: *** |
|
1663 ;;; mode: C++ *** |
|
1664 ;;; End: *** |
|
1665 */ |