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