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); |
6640
|
730 error_1 (std::cerr, 0, id, fmt, args); |
6361
|
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 { |
6483
|
843 Octave_map err = args(0).map_value (); |
6361
|
844 |
6483
|
845 if (! error_state) |
6361
|
846 { |
6483
|
847 if (err.contains ("message") && err.contains ("identifier")) |
6361
|
848 { |
6483
|
849 std::string msg = err.contents("message")(0).string_value (); |
|
850 std::string id = err.contents("identifier")(0).string_value (); |
6361
|
851 int len = msg.length(); |
|
852 std::string file; |
|
853 std::string nm; |
|
854 int l = -1; |
|
855 int c = -1; |
|
856 |
6483
|
857 if (err.contains ("stack")) |
6361
|
858 { |
6483
|
859 Octave_map err_stack = err.contents("stack")(0).map_value (); |
|
860 |
6640
|
861 if (err_stack.numel () > 0) |
|
862 { |
|
863 if (err_stack.contains ("file")) |
|
864 file = err_stack.contents("file")(0).string_value (); |
6483
|
865 |
6640
|
866 if (err_stack.contains ("name")) |
|
867 nm = err_stack.contents("name")(0).string_value (); |
6483
|
868 |
6640
|
869 if (err_stack.contains ("line")) |
|
870 l = err_stack.contents("line")(0).nint_value (); |
|
871 |
|
872 if (err_stack.contains ("column")) |
|
873 c = err_stack.contents("column")(0).nint_value (); |
|
874 } |
6361
|
875 } |
|
876 |
|
877 // Ugh. |
6483
|
878 char *tmp_msg = strsave (msg.c_str ()); |
6361
|
879 if (tmp_msg[len-1] == '\n') |
|
880 { |
|
881 if (len > 1) |
|
882 { |
|
883 tmp_msg[len - 1] = '\0'; |
6483
|
884 rethrow_error (id.c_str (), "%s\n", tmp_msg); |
6361
|
885 } |
|
886 } |
|
887 else |
6483
|
888 rethrow_error (id.c_str (), "%s", tmp_msg); |
6361
|
889 delete [] tmp_msg; |
|
890 |
|
891 // FIXME: Need to restore the stack as rethrow_error sets it? |
|
892 Vlast_error_file = file; |
|
893 Vlast_error_name = nm; |
|
894 Vlast_error_line = l; |
|
895 Vlast_error_column = c; |
|
896 |
6483
|
897 if (err.contains ("stack")) |
6361
|
898 { |
|
899 if (file.empty ()) |
|
900 { |
|
901 if (nm.empty ()) |
|
902 { |
|
903 if (l > 0) |
6483
|
904 { |
|
905 if (c > 0) |
|
906 pr_where_1 ("error: near line %d, column %d", |
|
907 l, c); |
|
908 else |
|
909 pr_where_1 ("error: near line %d", l); |
|
910 } |
6361
|
911 } |
|
912 else |
|
913 { |
|
914 if (l > 0) |
6483
|
915 { |
|
916 if (c > 0) |
|
917 pr_where_1 ("error: called from `%s' near line %d, column %d", |
|
918 nm.c_str (), l, c); |
|
919 else |
|
920 pr_where_1 ("error: called from `%d' near line %d", nm.c_str (), l); |
|
921 } |
6361
|
922 } |
|
923 } |
|
924 else |
|
925 { |
|
926 if (nm.empty ()) |
|
927 { |
|
928 if (l > 0) |
6483
|
929 { |
|
930 if (c > 0) |
|
931 pr_where_1 ("error: in file %s near line %d, column %d", |
|
932 file.c_str (), l, c); |
|
933 else |
|
934 pr_where_1 ("error: in file %s near line %d", file.c_str (), l); |
|
935 } |
6361
|
936 } |
|
937 else |
|
938 { |
|
939 if (l > 0) |
6483
|
940 { |
|
941 if (c > 0) |
|
942 pr_where_1 ("error: called from `%s' in file %s near line %d, column %d", |
|
943 nm.c_str (), file.c_str (), l, c); |
|
944 else |
|
945 pr_where_1 ("error: called from `%d' in file %s near line %d", nm.c_str (), file.c_str (), l); |
|
946 } |
6361
|
947 } |
|
948 } |
|
949 } |
|
950 } |
|
951 else |
|
952 error ("rethrow: structure must contain the fields 'message and 'identifier'"); |
|
953 } |
|
954 } |
|
955 return retval; |
|
956 } |
|
957 |
1957
|
958 DEFUN (error, args, , |
3373
|
959 "-*- texinfo -*-\n\ |
|
960 @deftypefn {Built-in Function} {} error (@var{template}, @dots{})\n\ |
5781
|
961 Format the optional arguments under the control of the template string\n\ |
|
962 @var{template} using the same rules as the @code{printf} family of\n\ |
|
963 functions (@pxref{Formatted Output}) and print the resulting message\n\ |
|
964 on the @code{stderr} stream. The message is prefixed by the character\n\ |
|
965 string @samp{error: }.\n\ |
3373
|
966 \n\ |
|
967 Calling @code{error} also sets Octave's internal error state such that\n\ |
|
968 control will return to the top level without evaluating any more\n\ |
|
969 commands. This is useful for aborting from functions or scripts.\n\ |
897
|
970 \n\ |
3373
|
971 If the error message does not end with a new line character, Octave will\n\ |
|
972 print a traceback of all the function calls leading to the error. For\n\ |
|
973 example, given the following function definitions:\n\ |
|
974 \n\ |
|
975 @example\n\ |
|
976 @group\n\ |
|
977 function f () g () end\n\ |
|
978 function g () h () end\n\ |
|
979 function h () nargin == 1 || error (\"nargin != 1\"); end\n\ |
|
980 @end group\n\ |
|
981 @end example\n\ |
1489
|
982 \n\ |
3373
|
983 @noindent\n\ |
|
984 calling the function @code{f} will result in a list of messages that\n\ |
|
985 can help you to quickly locate the exact location of the error:\n\ |
1489
|
986 \n\ |
3373
|
987 @example\n\ |
|
988 @group\n\ |
|
989 f ()\n\ |
|
990 error: nargin != 1\n\ |
|
991 error: evaluating index expression near line 1, column 30\n\ |
|
992 error: evaluating binary operator `||' near line 1, column 27\n\ |
|
993 error: called from `h'\n\ |
|
994 error: called from `g'\n\ |
|
995 error: called from `f'\n\ |
|
996 @end group\n\ |
|
997 @end example\n\ |
|
998 \n\ |
|
999 If the error message ends in a new line character, Octave will print the\n\ |
|
1000 message but will not display any traceback messages as it returns\n\ |
|
1001 control to the top level. For example, modifying the error message\n\ |
|
1002 in the previous example to end in a new line causes Octave to only print\n\ |
|
1003 a single message:\n\ |
|
1004 \n\ |
|
1005 @example\n\ |
|
1006 @group\n\ |
|
1007 function h () nargin == 1 || error (\"nargin != 1\\n\"); end\n\ |
|
1008 f ()\n\ |
|
1009 error: nargin != 1\n\ |
|
1010 @end group\n\ |
|
1011 @end example\n\ |
|
1012 @end deftypefn") |
897
|
1013 { |
5775
|
1014 // FIXME -- need to extract and pass message id to |
5567
|
1015 // handle_message. |
|
1016 |
3934
|
1017 octave_value_list retval; |
5567
|
1018 handle_message (error_with_id, "", "unspecified error", args); |
3934
|
1019 return retval; |
1489
|
1020 } |
897
|
1021 |
5567
|
1022 DEFCMD (warning, args, nargout, |
3373
|
1023 "-*- texinfo -*-\n\ |
5781
|
1024 @deftypefn {Built-in Function} {} warning (@var{template}, @dots{})\n\ |
|
1025 @deftypefnx {Built-in Function} {} warning (@var{id}, @var{template})\n\ |
|
1026 Format the optional arguments under the control of the template string\n\ |
|
1027 @var{template} using the same rules as the @code{printf} family of\n\ |
|
1028 functions (@pxref{Formatted Output}) and print the resulting message\n\ |
|
1029 on the @code{stderr} stream. The message is prefixed by the character\n\ |
|
1030 string @samp{warning: }.\n\ |
|
1031 You should use this function when you want to notify the user\n\ |
3600
|
1032 of an unusual condition, but only when it makes sense for your program\n\ |
|
1033 to go on.\n\ |
5781
|
1034 \n\ |
|
1035 The optional message identifier allows users to enable or disable\n\ |
|
1036 warnings tagged by @var{id}. The special identifier @samp{\"all\"} may\n\ |
|
1037 be used to set the state of all warnings.\n\ |
|
1038 \n\ |
|
1039 @deftypefnx {Built-in Function} {} warning (\"on\", @var{id})\n\ |
|
1040 @deftypefnx {Built-in Function} {} warning (\"off\", @var{id})\n\ |
|
1041 @deftypefnx {Built-in Function} {} warning (\"error\", @var{id})\n\ |
|
1042 @deftypefnx {Built-in Function} {} warning (\"query\", @var{id})\n\ |
6653
|
1043 Set or query the state of a particular warning using the identifier\n\ |
5781
|
1044 @var{id}. If the identifier is omitted, a value of @samp{\"all\"} is\n\ |
|
1045 assumed. If you set the state of a warning to @samp{\"error\"}, the\n\ |
|
1046 warning named by @var{id} is handled as if it were an error instead.\n\ |
5783
|
1047 @seealso{warning_ids}\n\ |
3373
|
1048 @end deftypefn") |
1489
|
1049 { |
5567
|
1050 octave_value retval; |
3934
|
1051 |
5567
|
1052 int nargin = args.length (); |
|
1053 int argc = nargin + 1; |
3934
|
1054 |
3935
|
1055 bool done = false; |
3934
|
1056 |
5567
|
1057 if (argc > 1 && args.all_strings_p ()) |
3935
|
1058 { |
|
1059 string_vector argv = args.make_argv ("warning"); |
|
1060 |
|
1061 if (! error_state) |
3934
|
1062 { |
5567
|
1063 std::string arg1 = argv(1); |
|
1064 std::string arg2 = "all"; |
|
1065 |
|
1066 if (argc == 3) |
|
1067 arg2 = argv(2); |
|
1068 |
|
1069 if (arg1 == "on" || arg1 == "off" || arg1 == "error") |
3934
|
1070 { |
5567
|
1071 Octave_map old_warning_options = warning_options; |
|
1072 |
|
1073 if (arg2 == "all") |
|
1074 { |
|
1075 Octave_map tmp; |
3934
|
1076 |
5567
|
1077 tmp.assign ("identifier", arg2); |
|
1078 tmp.assign ("state", arg1); |
|
1079 |
|
1080 warning_options = tmp; |
|
1081 |
3935
|
1082 done = true; |
|
1083 } |
5567
|
1084 else if (arg2 == "backtrace") |
|
1085 { |
|
1086 if (arg1 != "error") |
|
1087 { |
|
1088 Vbacktrace_on_warning = (arg1 == "on"); |
|
1089 done = true; |
|
1090 } |
|
1091 } |
|
1092 else if (arg2 == "debug") |
|
1093 { |
|
1094 if (arg1 != "error") |
|
1095 { |
5794
|
1096 Vdebug_on_warning = (arg1 == "on"); |
5567
|
1097 done = true; |
|
1098 } |
|
1099 } |
|
1100 else if (arg2 == "verbose") |
|
1101 { |
|
1102 if (arg1 != "error") |
|
1103 { |
|
1104 Vverbose_warning = (arg1 == "on"); |
|
1105 done = true; |
|
1106 } |
|
1107 } |
5582
|
1108 else if (arg2 == "quiet") |
|
1109 { |
|
1110 if (arg1 != "error") |
|
1111 { |
|
1112 Vquiet_warning = (arg1 == "on"); |
|
1113 done = true; |
|
1114 } |
|
1115 } |
5567
|
1116 else |
3935
|
1117 { |
5567
|
1118 if (arg2 == "last") |
|
1119 arg2 = Vlast_warning_id; |
|
1120 |
|
1121 if (arg2 == "all") |
5794
|
1122 initialize_warning_options (arg1); |
5567
|
1123 else |
|
1124 { |
|
1125 Cell ident = warning_options.contents ("identifier"); |
|
1126 Cell state = warning_options.contents ("state"); |
|
1127 |
|
1128 octave_idx_type nel = ident.numel (); |
|
1129 |
|
1130 bool found = false; |
|
1131 |
|
1132 for (octave_idx_type i = 0; i < nel; i++) |
|
1133 { |
|
1134 if (ident(i).string_value () == arg2) |
|
1135 { |
5775
|
1136 // FIXME -- if state for "all" is |
5567
|
1137 // same as arg1, we can simply remove the |
|
1138 // item from the list. |
|
1139 |
|
1140 state(i) = arg1; |
|
1141 warning_options.assign ("state", state); |
|
1142 found = true; |
|
1143 break; |
|
1144 } |
|
1145 } |
|
1146 |
|
1147 if (! found) |
|
1148 { |
5775
|
1149 // FIXME -- if state for "all" is |
5567
|
1150 // same as arg1, we don't need to do anything. |
|
1151 |
|
1152 ident.resize (dim_vector (1, nel+1)); |
|
1153 state.resize (dim_vector (1, nel+1)); |
|
1154 |
|
1155 ident(nel) = arg2; |
|
1156 state(nel) = arg1; |
|
1157 |
|
1158 warning_options.clear (); |
|
1159 |
|
1160 warning_options.assign ("identifier", ident); |
|
1161 warning_options.assign ("state", state); |
|
1162 } |
|
1163 } |
|
1164 |
3935
|
1165 done = true; |
|
1166 } |
5567
|
1167 |
|
1168 if (done && nargout > 0) |
6427
|
1169 retval = old_warning_options; |
5567
|
1170 } |
|
1171 else if (arg1 == "query") |
|
1172 { |
|
1173 if (arg2 == "all") |
|
1174 retval = warning_options; |
|
1175 else if (arg2 == "backtrace" || arg2 == "debug" |
5582
|
1176 || arg2 == "verbose" || arg2 == "quiet") |
5567
|
1177 { |
|
1178 Octave_map tmp; |
|
1179 tmp.assign ("identifier", arg2); |
|
1180 if (arg2 == "backtrace") |
|
1181 tmp.assign ("state", Vbacktrace_on_warning ? "on" : "off"); |
|
1182 else if (arg2 == "debug") |
|
1183 tmp.assign ("state", Vdebug_on_warning ? "on" : "off"); |
5582
|
1184 else if (arg2 == "verbose") |
|
1185 tmp.assign ("state", Vverbose_warning ? "on" : "off"); |
5567
|
1186 else |
5582
|
1187 tmp.assign ("state", Vquiet_warning ? "on" : "off"); |
|
1188 |
|
1189 retval = tmp; |
5567
|
1190 } |
|
1191 else |
3935
|
1192 { |
5567
|
1193 if (arg2 == "last") |
|
1194 arg2 = Vlast_warning_id; |
|
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 std::string val; |
|
1204 |
|
1205 for (octave_idx_type i = 0; i < nel; i++) |
|
1206 { |
|
1207 if (ident(i).string_value () == arg2) |
|
1208 { |
|
1209 val = state(i).string_value (); |
|
1210 found = true; |
|
1211 break; |
|
1212 } |
|
1213 } |
|
1214 |
5781
|
1215 if (! found) |
|
1216 { |
|
1217 for (octave_idx_type i = 0; i < nel; i++) |
|
1218 { |
|
1219 if (ident(i).string_value () == "all") |
|
1220 { |
|
1221 val = state(i).string_value (); |
|
1222 found = true; |
|
1223 break; |
|
1224 } |
|
1225 } |
|
1226 } |
|
1227 |
5567
|
1228 if (found) |
|
1229 { |
|
1230 Octave_map tmp; |
|
1231 |
|
1232 tmp.assign ("identifier", arg2); |
|
1233 tmp.assign ("state", val); |
|
1234 |
|
1235 retval = tmp; |
|
1236 } |
|
1237 else |
5781
|
1238 error ("warning: unable to find default warning state!"); |
3935
|
1239 } |
5567
|
1240 |
|
1241 done = true; |
3935
|
1242 } |
3934
|
1243 } |
|
1244 } |
5567
|
1245 else if (argc == 1) |
|
1246 { |
|
1247 retval = warning_options; |
3934
|
1248 |
5567
|
1249 done = true; |
|
1250 } |
|
1251 else if (argc == 2) |
|
1252 { |
|
1253 octave_value arg = args(0); |
|
1254 |
|
1255 Octave_map old_warning_options = warning_options; |
|
1256 |
|
1257 if (arg.is_map ()) |
|
1258 { |
|
1259 Octave_map m = arg.map_value (); |
|
1260 |
|
1261 if (m.contains ("identifier") && m.contains ("state")) |
6427
|
1262 warning_options = m; |
5567
|
1263 else |
|
1264 error ("warning: expecting structure with fields `identifier' and `state'"); |
|
1265 |
|
1266 done = true; |
|
1267 |
|
1268 if (nargout > 0) |
|
1269 retval = old_warning_options; |
|
1270 } |
|
1271 } |
|
1272 |
|
1273 if (! (error_state || done)) |
3935
|
1274 { |
5567
|
1275 octave_value_list nargs = args; |
|
1276 |
|
1277 std::string id; |
|
1278 |
|
1279 if (nargin > 1) |
|
1280 { |
|
1281 std::string arg1 = args(0).string_value (); |
|
1282 |
|
1283 if (! error_state) |
|
1284 { |
|
1285 if (arg1.find ('%') == NPOS) |
|
1286 { |
|
1287 id = arg1; |
|
1288 |
|
1289 nargs.resize (nargin-1); |
|
1290 |
|
1291 for (int i = 1; i < nargin; i++) |
|
1292 nargs(i-1) = args(i); |
|
1293 } |
|
1294 } |
|
1295 else |
|
1296 return retval; |
|
1297 } |
|
1298 |
|
1299 // handle_message. |
|
1300 |
3935
|
1301 std::string prev_msg = Vlast_warning_message; |
|
1302 |
5567
|
1303 std::string curr_msg = handle_message (warning_with_id, id.c_str (), |
|
1304 "unspecified warning", nargs); |
3935
|
1305 |
|
1306 if (nargout > 0) |
5567
|
1307 retval = prev_msg; |
3935
|
1308 } |
|
1309 |
3934
|
1310 return retval; |
|
1311 } |
|
1312 |
5904
|
1313 void |
5794
|
1314 disable_warning (const std::string& id) |
|
1315 { |
|
1316 octave_value_list args; |
|
1317 |
|
1318 args(1) = id; |
|
1319 args(0) = "off"; |
|
1320 |
|
1321 Fwarning (args, 0); |
|
1322 } |
|
1323 |
|
1324 void |
|
1325 initialize_default_warning_state (void) |
|
1326 { |
|
1327 initialize_warning_options ("on"); |
|
1328 |
|
1329 // Most people will want to have the following disabled. |
|
1330 |
|
1331 disable_warning ("Octave:array-to-scalar"); |
|
1332 disable_warning ("Octave:array-to-vector"); |
|
1333 disable_warning ("Octave:empty-list-elements"); |
|
1334 disable_warning ("Octave:fortran-indexing"); |
|
1335 disable_warning ("Octave:imag-to-real"); |
|
1336 disable_warning ("Octave:matlab-incompatible"); |
|
1337 disable_warning ("Octave:missing-semicolon"); |
|
1338 disable_warning ("Octave:neg-dim-as-zero"); |
|
1339 disable_warning ("Octave:resize-on-range-error"); |
|
1340 disable_warning ("Octave:separator-insert"); |
|
1341 disable_warning ("Octave:single-quote-string"); |
|
1342 disable_warning ("Octave:str-to-num"); |
|
1343 disable_warning ("Octave:string-concat"); |
|
1344 disable_warning ("Octave:variable-switch-label"); |
|
1345 } |
|
1346 |
6361
|
1347 DEFUN (lasterror, args, , |
|
1348 "-*- texinfo -*-\n\ |
|
1349 @deftypefn {Built-in Function} {@var{err} =} lasterror (@var{err})\n\ |
|
1350 @deftypefnx {Built-in Function} {} lasterror ('reset')\n\ |
|
1351 Returns or sets the last error message. Called without any arguments\n\ |
|
1352 returns a structure containing the last error message, as well as other\n\ |
|
1353 information related to this error. The elements of this structure are:\n\ |
|
1354 \n\ |
|
1355 @table @asis\n\ |
|
1356 @item 'message'\n\ |
|
1357 The text of the last error message\n\ |
|
1358 @item 'identifier'\n\ |
|
1359 The message identifier of this error message\n\ |
|
1360 @item 'stack'\n\ |
|
1361 A structure containing information on where the message occured. This might\n\ |
|
1362 be an empty structure if this in the case where this information can not\n\ |
|
1363 be obtained. The fields of this structure are:\n\ |
|
1364 \n\ |
|
1365 @table @asis\n\ |
|
1366 @item 'file'\n\ |
|
1367 The name of the file where the error occurred\n\ |
|
1368 @item 'name'\n\ |
|
1369 The name of function in which the error occured\n\ |
|
1370 @item 'line'\n\ |
|
1371 The line number at which the error occured\n\ |
|
1372 @item 'column'\n\ |
|
1373 An optional field with the column number at which the error occurred\n\ |
|
1374 @end table\n\ |
|
1375 @end table\n\ |
|
1376 \n\ |
|
1377 The @var{err} structure may also be passed to @code{lasterror} to set the\n\ |
|
1378 information about the last error. The only constraint on @var{err} in that\n\ |
|
1379 case is that it is a scalar structure. Any fields of @var{err} that match\n\ |
|
1380 the above are set to the value passed in @var{err}, while other fields are\n\ |
|
1381 set to their default values.\n\ |
|
1382 \n\ |
|
1383 If @code{lasterror} is called with the argument 'reset', all values take\n\ |
|
1384 their default values.\n\ |
|
1385 @end deftypefn") |
|
1386 { |
|
1387 octave_value retval; |
|
1388 int nargin = args.length(); |
|
1389 |
|
1390 if (nargin < 2) |
|
1391 { |
|
1392 Octave_map err; |
|
1393 |
|
1394 err.assign ("message", Vlast_error_message); |
|
1395 err.assign ("identifier", Vlast_error_id); |
|
1396 |
|
1397 if (! (Vlast_error_file.empty() && Vlast_error_name.empty() && |
|
1398 Vlast_error_line < 0 && Vlast_error_column < 0)) |
|
1399 { |
|
1400 Octave_map err_stack; |
|
1401 |
|
1402 err_stack.assign ("file", Vlast_error_file); |
|
1403 err_stack.assign ("name", Vlast_error_name); |
|
1404 err_stack.assign ("line", Vlast_error_line); |
|
1405 err_stack.assign ("column", Vlast_error_column); |
|
1406 |
|
1407 err.assign ("stack", octave_value (err_stack)); |
|
1408 } |
|
1409 else |
|
1410 { |
|
1411 string_vector sv(4); |
|
1412 sv[0] = "file"; |
|
1413 sv[1] = "name"; |
|
1414 sv[2] = "line"; |
|
1415 sv[3] = "column"; |
|
1416 err.assign ("stack", octave_value (Octave_map (dim_vector (0,1), |
|
1417 sv))); |
|
1418 } |
|
1419 |
|
1420 if (nargin == 1) |
|
1421 { |
|
1422 if (args(0).is_string()) |
|
1423 { |
6483
|
1424 if (args(0).string_value () == "reset") |
6361
|
1425 { |
|
1426 Vlast_error_message = std::string(); |
|
1427 Vlast_error_id = std::string(); |
|
1428 Vlast_error_file = std::string(); |
|
1429 Vlast_error_name = std::string(); |
|
1430 Vlast_error_line = -1; |
|
1431 Vlast_error_column = -1; |
|
1432 } |
|
1433 else |
|
1434 error("lasterror: unrecognized string argument"); |
|
1435 } |
|
1436 else if (args(0).is_map ()) |
|
1437 { |
6483
|
1438 Octave_map new_err = args(0).map_value (); |
6361
|
1439 std::string new_error_message; |
|
1440 std::string new_error_id; |
|
1441 std::string new_error_file; |
|
1442 std::string new_error_name; |
|
1443 int new_error_line = -1; |
|
1444 int new_error_column = -1; |
|
1445 |
6483
|
1446 if (! error_state && new_err.contains ("message")) |
6361
|
1447 { |
|
1448 const std::string tmp = |
6483
|
1449 new_err.contents("message")(0).string_value (); |
6361
|
1450 new_error_message = tmp; |
|
1451 } |
|
1452 |
6483
|
1453 if (! error_state && new_err.contains ("identifier")) |
6361
|
1454 { |
|
1455 const std::string tmp = |
6483
|
1456 new_err.contents("identifier")(0).string_value (); |
6361
|
1457 new_error_id = tmp; |
|
1458 } |
|
1459 |
6483
|
1460 if (! error_state && new_err.contains ("stack")) |
6361
|
1461 { |
|
1462 Octave_map new_err_stack = |
6483
|
1463 new_err.contents("identifier")(0).map_value (); |
6361
|
1464 |
6483
|
1465 if (! error_state && new_err_stack.contains ("file")) |
6361
|
1466 { |
|
1467 const std::string tmp = |
6483
|
1468 new_err_stack.contents("file")(0).string_value (); |
6361
|
1469 new_error_file = tmp; |
|
1470 } |
|
1471 |
6483
|
1472 if (! error_state && new_err_stack.contains ("name")) |
6361
|
1473 { |
|
1474 const std::string tmp = |
6483
|
1475 new_err_stack.contents("name")(0).string_value (); |
6361
|
1476 new_error_name = tmp; |
|
1477 } |
|
1478 |
6483
|
1479 if (! error_state && new_err_stack.contains ("line")) |
6361
|
1480 { |
|
1481 const int tmp = |
6483
|
1482 new_err_stack.contents("line")(0).nint_value (); |
6361
|
1483 new_error_line = tmp; |
|
1484 } |
|
1485 |
6483
|
1486 if (! error_state && new_err_stack.contains ("column")) |
6361
|
1487 { |
|
1488 const int tmp = |
6483
|
1489 new_err_stack.contents("column")(0).nint_value (); |
6361
|
1490 new_error_column = tmp; |
|
1491 } |
|
1492 } |
|
1493 |
|
1494 if (! error_state) |
|
1495 { |
|
1496 Vlast_error_message = new_error_message; |
|
1497 Vlast_error_id = new_error_id; |
|
1498 Vlast_error_file = new_error_file; |
|
1499 Vlast_error_name = new_error_name; |
|
1500 Vlast_error_line = new_error_line; |
|
1501 Vlast_error_column = new_error_column; |
|
1502 } |
|
1503 } |
|
1504 else |
|
1505 error ("lasterror: argument must be a structure or a string"); |
|
1506 } |
|
1507 |
6483
|
1508 if (! error_state) |
6361
|
1509 retval = err; |
|
1510 } |
|
1511 else |
|
1512 print_usage (); |
|
1513 |
|
1514 return retval; |
|
1515 } |
|
1516 |
5567
|
1517 DEFUN (lasterr, args, nargout, |
3935
|
1518 "-*- texinfo -*-\n\ |
5567
|
1519 @deftypefn {Built-in Function} {[@var{msg}, @var{msgid}] =} lasterr (@var{msg}, @var{msgid})\n\ |
3935
|
1520 Without any arguments, return the last error message. With one\n\ |
5567
|
1521 argument, set the last error message to @var{msg}. With two arguments,\n\ |
|
1522 also set the last message identifier.\n\ |
3935
|
1523 @end deftypefn") |
|
1524 { |
|
1525 octave_value_list retval; |
|
1526 |
|
1527 int argc = args.length () + 1; |
|
1528 |
5567
|
1529 if (argc < 4) |
5335
|
1530 { |
|
1531 string_vector argv = args.make_argv ("lasterr"); |
3935
|
1532 |
5335
|
1533 if (! error_state) |
|
1534 { |
5567
|
1535 std::string prev_error_id = Vlast_error_id; |
|
1536 std::string prev_error_message = Vlast_error_message; |
|
1537 |
|
1538 if (argc > 2) |
|
1539 Vlast_error_id = argv(2); |
|
1540 |
|
1541 if (argc > 1) |
5335
|
1542 Vlast_error_message = argv(1); |
5567
|
1543 |
|
1544 if (argc == 1 || nargout > 0) |
|
1545 { |
|
1546 retval(1) = prev_error_id; |
|
1547 retval(0) = prev_error_message; |
|
1548 } |
5335
|
1549 } |
5567
|
1550 else |
5582
|
1551 error ("lasterr: expecting arguments to be character strings"); |
5335
|
1552 } |
3935
|
1553 else |
5823
|
1554 print_usage (); |
3935
|
1555 |
|
1556 return retval; |
|
1557 } |
|
1558 |
4699
|
1559 // For backward compatibility. |
|
1560 DEFALIAS (error_text, lasterr); |
|
1561 DEFALIAS (__error_text__, lasterr); |
|
1562 |
5567
|
1563 DEFUN (lastwarn, args, nargout, |
3934
|
1564 "-*- texinfo -*-\n\ |
5567
|
1565 @deftypefn {Built-in Function} {[@var{msg}, @var{msgid}] =} lastwarn (@var{msg}, @var{msgid})\n\ |
3935
|
1566 Without any arguments, return the last warning message. With one\n\ |
5567
|
1567 argument, set the last warning message to @var{msg}. With two arguments,\n\ |
|
1568 also set the last message identifier.\n\ |
3934
|
1569 @end deftypefn") |
|
1570 { |
|
1571 octave_value_list retval; |
|
1572 |
|
1573 int argc = args.length () + 1; |
|
1574 |
5567
|
1575 if (argc < 4) |
|
1576 { |
|
1577 string_vector argv = args.make_argv ("lastwarn"); |
|
1578 |
|
1579 if (! error_state) |
|
1580 { |
|
1581 std::string prev_warning_id = Vlast_warning_id; |
|
1582 std::string prev_warning_message = Vlast_warning_message; |
|
1583 |
|
1584 if (argc > 2) |
|
1585 Vlast_warning_id = argv(2); |
3934
|
1586 |
5567
|
1587 if (argc > 1) |
|
1588 Vlast_warning_message = argv(1); |
|
1589 |
|
1590 if (argc == 1 || nargout > 0) |
|
1591 { |
5582
|
1592 warning_state = 0; |
5567
|
1593 retval(1) = prev_warning_id; |
|
1594 retval(0) = prev_warning_message; |
|
1595 } |
|
1596 } |
|
1597 else |
|
1598 error ("lastwarn: expecting arguments to be character strings"); |
|
1599 } |
3934
|
1600 else |
5823
|
1601 print_usage (); |
3934
|
1602 |
|
1603 return retval; |
897
|
1604 } |
|
1605 |
1957
|
1606 DEFUN (usage, args, , |
3373
|
1607 "-*- texinfo -*-\n\ |
|
1608 @deftypefn {Built-in Function} {} usage (@var{msg})\n\ |
|
1609 Print the message @var{msg}, prefixed by the string @samp{usage: }, and\n\ |
|
1610 set Octave's internal error state such that control will return to the\n\ |
|
1611 top level without evaluating any more commands. This is useful for\n\ |
|
1612 aborting from functions.\n\ |
|
1613 \n\ |
|
1614 After @code{usage} is evaluated, Octave will print a traceback of all\n\ |
|
1615 the function calls leading to the usage message.\n\ |
899
|
1616 \n\ |
3373
|
1617 You should use this function for reporting problems errors that result\n\ |
|
1618 from an improper call to a function, such as calling a function with an\n\ |
|
1619 incorrect number of arguments, or with arguments of the wrong type. For\n\ |
|
1620 example, most functions distributed with Octave begin with code like\n\ |
|
1621 this\n\ |
|
1622 \n\ |
|
1623 @example\n\ |
|
1624 @group\n\ |
|
1625 if (nargin != 2)\n\ |
|
1626 usage (\"foo (a, b)\");\n\ |
|
1627 endif\n\ |
|
1628 @end group\n\ |
|
1629 @end example\n\ |
|
1630 \n\ |
|
1631 @noindent\n\ |
|
1632 to check for the proper number of arguments.\n\ |
|
1633 @end deftypefn") |
899
|
1634 { |
3934
|
1635 octave_value_list retval; |
5567
|
1636 handle_message (usage_with_id, "", "unknown", args); |
3934
|
1637 return retval; |
899
|
1638 } |
|
1639 |
5794
|
1640 DEFUN (beep_on_error, args, nargout, |
|
1641 "-*- texinfo -*-\n\ |
|
1642 @deftypefn {Built-in Function} {@var{val} =} beep_on_error ()\n\ |
|
1643 @deftypefnx {Built-in Function} {@var{old_val} =} beep_on_error (@var{new_val})\n\ |
|
1644 Query or set the internal variable that controls whether Octave will try\n\ |
|
1645 to ring the terminal bell before printing an error message.\n\ |
|
1646 @end deftypefn") |
3707
|
1647 { |
5794
|
1648 return SET_INTERNAL_VARIABLE (beep_on_error); |
3707
|
1649 } |
|
1650 |
5794
|
1651 DEFUN (debug_on_error, args, nargout, |
3373
|
1652 "-*- texinfo -*-\n\ |
5794
|
1653 @deftypefn {Built-in Function} {@var{val} =} debug_on_error ()\n\ |
|
1654 @deftypefnx {Built-in Function} {@var{old_val} =} debug_on_error (@var{new_val})\n\ |
|
1655 Query or set the internal variable that controls whether Octave will try\n\ |
3707
|
1656 to enter the debugger when an error is encountered. This will also\n\ |
|
1657 inhibit printing of the normal traceback message (you will only see\n\ |
5794
|
1658 the top-level error message).\n\ |
|
1659 @end deftypefn") |
|
1660 { |
|
1661 return SET_INTERNAL_VARIABLE (debug_on_error); |
|
1662 } |
3707
|
1663 |
5794
|
1664 DEFUN (debug_on_warning, args, nargout, |
3707
|
1665 "-*- texinfo -*-\n\ |
5794
|
1666 @deftypefn {Built-in Function} {@var{val} =} debug_on_warning ()\n\ |
|
1667 @deftypefnx {Built-in Function} {@var{old_val} =} debug_on_warning (@var{new_val})\n\ |
|
1668 Query or set the internal variable that controls whether Octave will try\n\ |
|
1669 to enter the debugger when a warning is encountered.\n\ |
|
1670 @end deftypefn") |
|
1671 { |
|
1672 return SET_INTERNAL_VARIABLE (debug_on_warning); |
2174
|
1673 } |
|
1674 |
1
|
1675 /* |
|
1676 ;;; Local Variables: *** |
|
1677 ;;; mode: C++ *** |
|
1678 ;;; End: *** |
|
1679 */ |