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 |
143
|
88 // Current error state. |
3935
|
89 // |
|
90 // Valid values: |
|
91 // |
|
92 // -2: an error has occurred, but don't print any messages. |
|
93 // -1: an error has occurred, we are printing a traceback |
|
94 // 0: no error |
|
95 // 1: an error has occurred |
|
96 // |
672
|
97 int error_state = 0; |
|
98 |
3489
|
99 // Current warning state. |
3935
|
100 // |
|
101 // Valid values: |
|
102 // |
|
103 // 0: no warning |
|
104 // 1: a warning has occurred |
|
105 // |
3489
|
106 int warning_state = 0; |
|
107 |
1489
|
108 // Tell the error handler whether to print messages, or just store |
|
109 // them for later. Used for handling errors in eval() and |
|
110 // the `unwind_protect' statement. |
4699
|
111 int buffer_error_messages = 0; |
1489
|
112 |
3815
|
113 // TRUE means error messages are turned off. |
|
114 bool discard_error_messages = false; |
|
115 |
4452
|
116 // TRUE means warning messages are turned off. |
|
117 bool discard_warning_messages = false; |
|
118 |
3811
|
119 // The message buffer. |
5765
|
120 static std::ostringstream *error_message_buffer = 0; |
143
|
121 |
4318
|
122 void |
|
123 reset_error_handler (void) |
|
124 { |
|
125 error_state = 0; |
|
126 warning_state = 0; |
4699
|
127 buffer_error_messages = 0; |
4318
|
128 discard_error_messages = false; |
|
129 } |
|
130 |
5567
|
131 static void |
5794
|
132 initialize_warning_options (const std::string& state) |
5567
|
133 { |
|
134 warning_options.clear (); |
|
135 |
|
136 warning_options.assign ("identifier", "all"); |
|
137 warning_options.assign ("state", state); |
|
138 } |
|
139 |
3491
|
140 // Warning messages are never buffered. |
|
141 |
|
142 static void |
5567
|
143 vwarning (const char *name, const char *id, const char *fmt, va_list args) |
3491
|
144 { |
4452
|
145 if (discard_warning_messages) |
|
146 return; |
|
147 |
3491
|
148 flush_octave_stdout (); |
|
149 |
5765
|
150 std::ostringstream output_buf; |
3491
|
151 |
|
152 if (name) |
3761
|
153 output_buf << name << ": "; |
|
154 |
|
155 octave_vformat (output_buf, fmt, args); |
|
156 |
5765
|
157 output_buf << std::endl; |
3491
|
158 |
5775
|
159 // FIXME -- we really want to capture the message before it |
3935
|
160 // has all the formatting goop attached to it. We probably also |
|
161 // want just the message, not the traceback information. |
|
162 |
5765
|
163 std::string msg_string = output_buf.str (); |
3934
|
164 |
3935
|
165 if (! warning_state) |
|
166 { |
|
167 // This is the first warning in a possible series. |
5567
|
168 |
|
169 Vlast_warning_id = id; |
3935
|
170 Vlast_warning_message = msg_string; |
|
171 } |
3934
|
172 |
5582
|
173 if (! Vquiet_warning) |
|
174 { |
|
175 octave_diary << msg_string; |
3935
|
176 |
5582
|
177 std::cerr << msg_string; |
|
178 } |
3491
|
179 } |
|
180 |
1
|
181 static void |
4732
|
182 verror (bool save_last_error, std::ostream& os, |
5567
|
183 const char *name, const char *id, const char *fmt, va_list args) |
1
|
184 { |
3815
|
185 if (discard_error_messages) |
|
186 return; |
|
187 |
3585
|
188 if (! buffer_error_messages) |
|
189 flush_octave_stdout (); |
914
|
190 |
2174
|
191 bool to_beep_or_not_to_beep_p = Vbeep_on_error && ! error_state; |
1423
|
192 |
5765
|
193 std::ostringstream output_buf; |
1
|
194 |
2174
|
195 if (to_beep_or_not_to_beep_p) |
1423
|
196 output_buf << "\a"; |
3620
|
197 |
599
|
198 if (name) |
|
199 output_buf << name << ": "; |
3620
|
200 |
|
201 octave_vformat (output_buf, fmt, args); |
|
202 |
5765
|
203 output_buf << std::endl; |
581
|
204 |
5775
|
205 // FIXME -- we really want to capture the message before it |
3935
|
206 // has all the formatting goop attached to it. We probably also |
|
207 // want just the message, not the traceback information. |
|
208 |
5765
|
209 std::string msg_string = output_buf.str (); |
3935
|
210 |
4731
|
211 if (! error_state && save_last_error) |
3935
|
212 { |
|
213 // This is the first error in a possible series. |
5567
|
214 |
|
215 Vlast_error_id = id; |
3935
|
216 Vlast_error_message = msg_string; |
|
217 } |
|
218 |
1489
|
219 if (buffer_error_messages) |
|
220 { |
3941
|
221 std::string tmp = msg_string; |
1489
|
222 |
|
223 if (! error_message_buffer) |
|
224 { |
5765
|
225 error_message_buffer = new std::ostringstream (); |
1489
|
226 |
5775
|
227 // FIXME -- this is ugly, but it prevents |
1489
|
228 // |
4699
|
229 // eval ("error (\"msg\")", "error (lasterr ())"); |
1489
|
230 // |
|
231 // from printing `error: ' twice. Assumes that the NAME we |
|
232 // have been given doesn't contain `:'. |
|
233 |
3935
|
234 size_t pos = msg_string.find (':'); |
3162
|
235 |
3935
|
236 if (pos != NPOS && pos < Vlast_error_message.length () - 2) |
|
237 tmp = msg_string.substr (pos+2); |
1489
|
238 } |
|
239 |
3935
|
240 *error_message_buffer << tmp; |
1489
|
241 } |
|
242 else |
|
243 { |
3935
|
244 octave_diary << msg_string; |
4732
|
245 os << msg_string; |
1489
|
246 } |
1
|
247 } |
|
248 |
1266
|
249 // Note that we don't actually print any message if the error string |
|
250 // is just "" or "\n". This allows error ("") and error ("\n") to |
|
251 // just set the error state. |
|
252 |
1005
|
253 static void |
5567
|
254 error_1 (std::ostream& os, const char *name, const char *id, |
|
255 const char *fmt, va_list args) |
1005
|
256 { |
|
257 if (error_state != -2) |
|
258 { |
1489
|
259 if (fmt) |
1005
|
260 { |
1489
|
261 if (*fmt) |
1005
|
262 { |
1489
|
263 int len = strlen (fmt); |
|
264 if (fmt[len - 1] == '\n') |
1266
|
265 { |
1489
|
266 if (len > 1) |
1266
|
267 { |
1489
|
268 char *tmp_fmt = strsave (fmt); |
|
269 tmp_fmt[len - 1] = '\0'; |
5567
|
270 verror (true, os, name, id, tmp_fmt, args); |
1489
|
271 delete [] tmp_fmt; |
|
272 } |
1423
|
273 |
1489
|
274 error_state = -2; |
1266
|
275 } |
1489
|
276 else |
5567
|
277 verror (true, os, name, id, fmt, args); |
1005
|
278 } |
|
279 } |
1489
|
280 else |
|
281 panic ("error_1: invalid format"); |
1423
|
282 |
|
283 if (! error_state) |
|
284 error_state = 1; |
1005
|
285 } |
|
286 } |
|
287 |
1
|
288 void |
|
289 message (const char *name, const char *fmt, ...) |
|
290 { |
|
291 va_list args; |
|
292 va_start (args, fmt); |
5567
|
293 verror (false, std::cerr, name, "", fmt, args); |
1
|
294 va_end (args); |
|
295 } |
|
296 |
|
297 void |
5567
|
298 message_with_id (const char *name, const char *id, const char *fmt, ...) |
|
299 { |
|
300 va_list args; |
|
301 va_start (args, fmt); |
|
302 verror (false, std::cerr, name, id, fmt, args); |
|
303 va_end (args); |
|
304 } |
|
305 |
|
306 void |
|
307 usage_1 (const char *id, const char *fmt, va_list args) |
|
308 { |
|
309 verror (true, std::cerr, "usage", id, fmt, args); |
|
310 error_state = -1; |
|
311 } |
|
312 |
|
313 void |
1
|
314 usage (const char *fmt, ...) |
|
315 { |
|
316 va_list args; |
|
317 va_start (args, fmt); |
5567
|
318 usage_1 ("", fmt, args); |
|
319 va_end (args); |
|
320 } |
|
321 |
|
322 void |
|
323 usage_with_id (const char *id, const char *fmt, ...) |
|
324 { |
|
325 va_list args; |
|
326 va_start (args, fmt); |
|
327 usage_1 (id, fmt, args); |
1
|
328 va_end (args); |
|
329 } |
|
330 |
3707
|
331 static void |
3719
|
332 pr_where_2 (const char *fmt, va_list args) |
|
333 { |
|
334 if (fmt) |
|
335 { |
|
336 if (*fmt) |
|
337 { |
|
338 int len = strlen (fmt); |
|
339 if (fmt[len - 1] == '\n') |
|
340 { |
|
341 if (len > 1) |
|
342 { |
|
343 char *tmp_fmt = strsave (fmt); |
|
344 tmp_fmt[len - 1] = '\0'; |
5567
|
345 verror (false, std::cerr, 0, "", tmp_fmt, args); |
3719
|
346 delete [] tmp_fmt; |
|
347 } |
|
348 } |
|
349 else |
5567
|
350 verror (false, std::cerr, 0, "", fmt, args); |
3719
|
351 } |
|
352 } |
|
353 else |
|
354 panic ("pr_where_2: invalid format"); |
|
355 } |
|
356 |
|
357 static void |
3707
|
358 pr_where_1 (const char *fmt, ...) |
|
359 { |
|
360 va_list args; |
|
361 va_start (args, fmt); |
3719
|
362 pr_where_2 (fmt, args); |
3707
|
363 va_end (args); |
|
364 } |
|
365 |
|
366 static void |
4719
|
367 pr_where (const char *name, bool print_code = true) |
3707
|
368 { |
|
369 if (curr_statement) |
|
370 { |
4976
|
371 std::string nm; |
3707
|
372 |
4976
|
373 int l = -1; |
|
374 int c = -1; |
|
375 |
5744
|
376 octave_function *fcn |
|
377 = octave_call_stack::caller_user_script_or_function (); |
4976
|
378 |
|
379 if (fcn) |
3708
|
380 { |
5743
|
381 nm = fcn->fcn_file_name (); |
4976
|
382 |
5743
|
383 if (nm.empty ()) |
|
384 nm = fcn->name (); |
3708
|
385 |
5743
|
386 if (curr_statement) |
|
387 { |
|
388 l = curr_statement->line (); |
|
389 c = curr_statement->column (); |
4976
|
390 } |
3708
|
391 } |
3707
|
392 |
4976
|
393 if (nm.empty ()) |
|
394 { |
|
395 if (l > 0 && c > 0) |
|
396 pr_where_1 ("%s: near line %d, column %d:", name, l, c); |
|
397 } |
3708
|
398 else |
4976
|
399 { |
|
400 if (l > 0 && c > 0) |
|
401 pr_where_1 ("%s: in %s near line %d, column %d:", |
|
402 name, nm.c_str (), l, c); |
|
403 else |
|
404 pr_where_1 ("%s: in %s", name, nm.c_str ()); |
|
405 } |
3707
|
406 |
4719
|
407 if (print_code) |
|
408 { |
5775
|
409 // FIXME -- Note that the column number is probably |
4719
|
410 // not going to mean much here since the code is being |
|
411 // reproduced from the parse tree, and we are only showing |
|
412 // one statement even if there were multiple statements on |
|
413 // the original source line. |
3707
|
414 |
5765
|
415 std::ostringstream output_buf; |
3708
|
416 |
4719
|
417 output_buf << std::endl; |
3707
|
418 |
4719
|
419 tree_print_code tpc (output_buf, ">>> "); |
|
420 |
|
421 curr_statement->accept (tpc); |
3707
|
422 |
5765
|
423 output_buf << std::endl; |
3707
|
424 |
5765
|
425 std::string msg = output_buf.str (); |
3707
|
426 |
5765
|
427 pr_where_1 ("%s", msg.c_str ()); |
4719
|
428 } |
3707
|
429 } |
|
430 } |
|
431 |
5567
|
432 static int |
|
433 check_state (const std::string& state) |
|
434 { |
|
435 // -1: not found |
|
436 // 0: found, "off" |
|
437 // 1: found, "on" |
|
438 // 2: found, "error" |
|
439 |
|
440 if (state == "off") |
|
441 return 0; |
|
442 else if (state == "on") |
|
443 return 1; |
|
444 else if (state == "error") |
|
445 return 2; |
|
446 else |
|
447 return -1; |
|
448 } |
|
449 |
|
450 // For given warning ID, return 0 if warnings are disabled, 1 if |
|
451 // enabled, and 2 if this ID should be an error instead of a warning. |
|
452 |
5781
|
453 int |
5567
|
454 warning_enabled (const std::string& id) |
1
|
455 { |
5567
|
456 int retval = 0; |
|
457 |
|
458 int all_state = -1; |
|
459 int id_state = -1; |
|
460 |
|
461 octave_idx_type nel = warning_options.numel (); |
|
462 |
|
463 if (nel > 0) |
|
464 { |
|
465 Cell identifier = warning_options.contents ("identifier"); |
|
466 Cell state = warning_options.contents ("state"); |
|
467 |
|
468 bool all_found = false; |
|
469 bool id_found = false; |
|
470 |
|
471 for (octave_idx_type i = 0; i < nel; i++) |
|
472 { |
|
473 octave_value ov = identifier(i); |
|
474 std::string ovs = ov.string_value (); |
|
475 |
|
476 if (! all_found && ovs == "all") |
|
477 { |
|
478 all_state = check_state (state(i).string_value ()); |
|
479 |
|
480 if (all_state >= 0) |
|
481 all_found = true; |
|
482 } |
|
483 |
|
484 if (! id_found && ovs == id) |
|
485 { |
|
486 id_state = check_state (state(i).string_value ()); |
|
487 |
|
488 if (id_state >= 0) |
|
489 id_found = true; |
|
490 } |
|
491 |
|
492 if (all_found && id_found) |
|
493 break; |
|
494 } |
|
495 } |
|
496 |
|
497 if (all_state == -1) |
|
498 panic_impossible (); |
|
499 |
|
500 if (all_state == 0) |
|
501 { |
|
502 if (id_state >= 0) |
|
503 retval = id_state; |
|
504 } |
|
505 else if (all_state == 1) |
|
506 { |
|
507 if (id_state == 0 || id_state == 2) |
|
508 retval = id_state; |
|
509 else |
|
510 retval = all_state; |
|
511 } |
|
512 else if (all_state == 2) |
|
513 retval = 2; |
|
514 |
|
515 return retval; |
|
516 } |
|
517 |
|
518 static void |
|
519 warning_1 (const char *id, const char *fmt, va_list args) |
|
520 { |
|
521 int warn_opt = warning_enabled (id); |
|
522 |
|
523 if (warn_opt == 2) |
|
524 { |
|
525 // Handle this warning as an error. |
|
526 |
|
527 error (id, fmt, args); |
|
528 } |
|
529 else if (warn_opt == 1) |
3934
|
530 { |
4014
|
531 if (curr_sym_tab != top_level_sym_tab |
5567
|
532 && Vbacktrace_on_warning |
4452
|
533 && ! warning_state |
|
534 && ! discard_warning_messages) |
4719
|
535 pr_where ("warning", false); |
3986
|
536 |
5567
|
537 vwarning ("warning", id, fmt, args); |
3707
|
538 |
3935
|
539 warning_state = 1; |
|
540 |
3934
|
541 if ((interactive || forced_interactive) |
5743
|
542 && Vdebug_on_warning |
5744
|
543 && octave_call_stack::caller_user_script_or_function ()) |
3934
|
544 { |
|
545 unwind_protect_bool (Vdebug_on_warning); |
|
546 Vdebug_on_warning = false; |
3707
|
547 |
3934
|
548 do_keyboard (octave_value_list ()); |
3707
|
549 |
3934
|
550 unwind_protect::run (); |
|
551 } |
3707
|
552 } |
1
|
553 } |
|
554 |
|
555 void |
5567
|
556 warning (const char *fmt, ...) |
|
557 { |
|
558 va_list args; |
|
559 va_start (args, fmt); |
|
560 warning_1 ("", fmt, args); |
|
561 va_end (args); |
|
562 } |
|
563 |
|
564 void |
|
565 warning_with_id (const char *id, const char *fmt, ...) |
|
566 { |
|
567 va_list args; |
|
568 va_start (args, fmt); |
|
569 warning_1 (id, fmt, args); |
|
570 va_end (args); |
|
571 } |
|
572 |
|
573 static void |
|
574 error_2 (const char *id, const char *fmt, va_list args) |
1
|
575 { |
3707
|
576 int init_state = error_state; |
|
577 |
5567
|
578 error_1 (std::cerr, "error", id, fmt, args); |
3707
|
579 |
|
580 if ((interactive || forced_interactive) |
5743
|
581 && Vdebug_on_error && init_state == 0 |
5744
|
582 && octave_call_stack::caller_user_script_or_function ()) |
3707
|
583 { |
|
584 unwind_protect_bool (Vdebug_on_error); |
|
585 Vdebug_on_error = false; |
|
586 |
3708
|
587 pr_where ("error"); |
3707
|
588 |
|
589 error_state = 0; |
|
590 |
|
591 do_keyboard (octave_value_list ()); |
|
592 |
|
593 unwind_protect::run (); |
|
594 } |
1005
|
595 } |
436
|
596 |
1005
|
597 void |
5567
|
598 error (const char *fmt, ...) |
|
599 { |
|
600 va_list args; |
|
601 va_start (args, fmt); |
|
602 error_2 ("", fmt, args); |
|
603 va_end (args); |
|
604 } |
|
605 |
|
606 void |
|
607 error_with_id (const char *id, const char *fmt, ...) |
|
608 { |
|
609 va_list args; |
|
610 va_start (args, fmt); |
|
611 error_2 (id, fmt, args); |
|
612 va_end (args); |
|
613 } |
|
614 |
|
615 void |
1005
|
616 parse_error (const char *fmt, ...) |
|
617 { |
|
618 va_list args; |
|
619 va_start (args, fmt); |
5567
|
620 error_1 (std::cerr, 0, "", fmt, args); |
|
621 va_end (args); |
|
622 } |
|
623 |
|
624 void |
|
625 parse_error_with_id (const char *id, const char *fmt, ...) |
|
626 { |
|
627 va_list args; |
|
628 va_start (args, fmt); |
|
629 error_1 (std::cerr, 0, id, fmt, args); |
1
|
630 va_end (args); |
|
631 } |
|
632 |
189
|
633 void |
1
|
634 panic (const char *fmt, ...) |
|
635 { |
|
636 va_list args; |
|
637 va_start (args, fmt); |
4699
|
638 buffer_error_messages = 0; |
3815
|
639 discard_error_messages = false; |
5567
|
640 verror (false, std::cerr, "panic", "", fmt, args); |
1
|
641 va_end (args); |
|
642 abort (); |
|
643 } |
|
644 |
4732
|
645 static void |
|
646 defun_usage_message_1 (const char *fmt, ...) |
|
647 { |
|
648 va_list args; |
|
649 va_start (args, fmt); |
5567
|
650 error_1 (octave_stdout, 0, "", fmt, args); |
4732
|
651 va_end (args); |
|
652 } |
|
653 |
|
654 void |
|
655 defun_usage_message (const std::string& msg) |
|
656 { |
|
657 defun_usage_message_1 ("%s", msg.c_str ()); |
|
658 } |
|
659 |
5567
|
660 typedef void (*error_fun)(const char *, const char *, ...); |
1489
|
661 |
2086
|
662 extern octave_value_list Fsprintf (const octave_value_list&, int); |
1489
|
663 |
3934
|
664 static std::string |
5567
|
665 handle_message (error_fun f, const char *id, const char *msg, |
|
666 const octave_value_list& args) |
528
|
667 { |
3934
|
668 std::string retval; |
528
|
669 |
3523
|
670 std::string tstr; |
1728
|
671 |
528
|
672 int nargin = args.length (); |
|
673 |
2745
|
674 if (nargin > 0) |
528
|
675 { |
3066
|
676 octave_value arg; |
|
677 |
|
678 if (nargin > 1) |
|
679 { |
|
680 octave_value_list tmp = Fsprintf (args, 1); |
|
681 arg = tmp(0); |
|
682 } |
|
683 else |
|
684 arg = args(0); |
2745
|
685 |
|
686 if (arg.is_defined ()) |
528
|
687 { |
2745
|
688 if (arg.is_string ()) |
|
689 { |
|
690 tstr = arg.string_value (); |
|
691 msg = tstr.c_str (); |
|
692 |
|
693 if (! msg) |
|
694 return retval; |
|
695 } |
|
696 else if (arg.is_empty ()) |
528
|
697 return retval; |
|
698 } |
|
699 } |
|
700 |
1489
|
701 // Ugh. |
|
702 |
|
703 int len = strlen (msg); |
|
704 if (msg[len - 1] == '\n') |
|
705 { |
|
706 if (len > 1) |
|
707 { |
|
708 char *tmp_msg = strsave (msg); |
|
709 tmp_msg[len - 1] = '\0'; |
5567
|
710 f (id, "%s\n", tmp_msg); |
3934
|
711 retval = tmp_msg; |
1489
|
712 delete [] tmp_msg; |
|
713 } |
|
714 } |
|
715 else |
3934
|
716 { |
5567
|
717 f (id, "%s", msg); |
3934
|
718 retval = msg; |
|
719 } |
528
|
720 |
|
721 return retval; |
|
722 } |
|
723 |
1957
|
724 DEFUN (error, args, , |
3373
|
725 "-*- texinfo -*-\n\ |
|
726 @deftypefn {Built-in Function} {} error (@var{template}, @dots{})\n\ |
5781
|
727 Format the optional arguments under the control of the template string\n\ |
|
728 @var{template} using the same rules as the @code{printf} family of\n\ |
|
729 functions (@pxref{Formatted Output}) and print the resulting message\n\ |
|
730 on the @code{stderr} stream. The message is prefixed by the character\n\ |
|
731 string @samp{error: }.\n\ |
3373
|
732 \n\ |
|
733 Calling @code{error} also sets Octave's internal error state such that\n\ |
|
734 control will return to the top level without evaluating any more\n\ |
|
735 commands. This is useful for aborting from functions or scripts.\n\ |
897
|
736 \n\ |
3373
|
737 If the error message does not end with a new line character, Octave will\n\ |
|
738 print a traceback of all the function calls leading to the error. For\n\ |
|
739 example, given the following function definitions:\n\ |
|
740 \n\ |
|
741 @example\n\ |
|
742 @group\n\ |
|
743 function f () g () end\n\ |
|
744 function g () h () end\n\ |
|
745 function h () nargin == 1 || error (\"nargin != 1\"); end\n\ |
|
746 @end group\n\ |
|
747 @end example\n\ |
1489
|
748 \n\ |
3373
|
749 @noindent\n\ |
|
750 calling the function @code{f} will result in a list of messages that\n\ |
|
751 can help you to quickly locate the exact location of the error:\n\ |
1489
|
752 \n\ |
3373
|
753 @example\n\ |
|
754 @group\n\ |
|
755 f ()\n\ |
|
756 error: nargin != 1\n\ |
|
757 error: evaluating index expression near line 1, column 30\n\ |
|
758 error: evaluating binary operator `||' near line 1, column 27\n\ |
|
759 error: called from `h'\n\ |
|
760 error: called from `g'\n\ |
|
761 error: called from `f'\n\ |
|
762 @end group\n\ |
|
763 @end example\n\ |
|
764 \n\ |
|
765 If the error message ends in a new line character, Octave will print the\n\ |
|
766 message but will not display any traceback messages as it returns\n\ |
|
767 control to the top level. For example, modifying the error message\n\ |
|
768 in the previous example to end in a new line causes Octave to only print\n\ |
|
769 a single message:\n\ |
|
770 \n\ |
|
771 @example\n\ |
|
772 @group\n\ |
|
773 function h () nargin == 1 || error (\"nargin != 1\\n\"); end\n\ |
|
774 f ()\n\ |
|
775 error: nargin != 1\n\ |
|
776 @end group\n\ |
|
777 @end example\n\ |
|
778 @end deftypefn") |
897
|
779 { |
5775
|
780 // FIXME -- need to extract and pass message id to |
5567
|
781 // handle_message. |
|
782 |
3934
|
783 octave_value_list retval; |
5567
|
784 handle_message (error_with_id, "", "unspecified error", args); |
3934
|
785 return retval; |
1489
|
786 } |
897
|
787 |
5567
|
788 DEFCMD (warning, args, nargout, |
3373
|
789 "-*- texinfo -*-\n\ |
5781
|
790 @deftypefn {Built-in Function} {} warning (@var{template}, @dots{})\n\ |
|
791 @deftypefnx {Built-in Function} {} warning (@var{id}, @var{template})\n\ |
|
792 Format the optional arguments under the control of the template string\n\ |
|
793 @var{template} using the same rules as the @code{printf} family of\n\ |
|
794 functions (@pxref{Formatted Output}) and print the resulting message\n\ |
|
795 on the @code{stderr} stream. The message is prefixed by the character\n\ |
|
796 string @samp{warning: }.\n\ |
|
797 You should use this function when you want to notify the user\n\ |
3600
|
798 of an unusual condition, but only when it makes sense for your program\n\ |
|
799 to go on.\n\ |
5781
|
800 \n\ |
|
801 The optional message identifier allows users to enable or disable\n\ |
|
802 warnings tagged by @var{id}. The special identifier @samp{\"all\"} may\n\ |
|
803 be used to set the state of all warnings.\n\ |
|
804 \n\ |
|
805 @deftypefnx {Built-in Function} {} warning (\"on\", @var{id})\n\ |
|
806 @deftypefnx {Built-in Function} {} warning (\"off\", @var{id})\n\ |
|
807 @deftypefnx {Built-in Function} {} warning (\"error\", @var{id})\n\ |
|
808 @deftypefnx {Built-in Function} {} warning (\"query\", @var{id})\n\ |
|
809 Set or query the the state of a particular warning using the identifier\n\ |
|
810 @var{id}. If the identifier is omitted, a value of @samp{\"all\"} is\n\ |
|
811 assumed. If you set the state of a warning to @samp{\"error\"}, the\n\ |
|
812 warning named by @var{id} is handled as if it were an error instead.\n\ |
5783
|
813 @seealso{warning_ids}\n\ |
3373
|
814 @end deftypefn") |
1489
|
815 { |
5567
|
816 octave_value retval; |
3934
|
817 |
5567
|
818 int nargin = args.length (); |
|
819 int argc = nargin + 1; |
3934
|
820 |
3935
|
821 bool done = false; |
3934
|
822 |
5567
|
823 if (argc > 1 && args.all_strings_p ()) |
3935
|
824 { |
|
825 string_vector argv = args.make_argv ("warning"); |
|
826 |
|
827 if (! error_state) |
3934
|
828 { |
5567
|
829 std::string arg1 = argv(1); |
|
830 std::string arg2 = "all"; |
|
831 |
|
832 if (argc == 3) |
|
833 arg2 = argv(2); |
|
834 |
|
835 if (arg1 == "on" || arg1 == "off" || arg1 == "error") |
3934
|
836 { |
5567
|
837 Octave_map old_warning_options = warning_options; |
|
838 |
|
839 if (arg2 == "all") |
|
840 { |
|
841 Octave_map tmp; |
3934
|
842 |
5567
|
843 tmp.assign ("identifier", arg2); |
|
844 tmp.assign ("state", arg1); |
|
845 |
|
846 warning_options = tmp; |
|
847 |
3935
|
848 done = true; |
|
849 } |
5567
|
850 else if (arg2 == "backtrace") |
|
851 { |
|
852 if (arg1 != "error") |
|
853 { |
|
854 Vbacktrace_on_warning = (arg1 == "on"); |
|
855 done = true; |
|
856 } |
|
857 } |
|
858 else if (arg2 == "debug") |
|
859 { |
|
860 if (arg1 != "error") |
|
861 { |
5794
|
862 Vdebug_on_warning = (arg1 == "on"); |
5567
|
863 done = true; |
|
864 } |
|
865 } |
|
866 else if (arg2 == "verbose") |
|
867 { |
|
868 if (arg1 != "error") |
|
869 { |
|
870 Vverbose_warning = (arg1 == "on"); |
|
871 done = true; |
|
872 } |
|
873 } |
5582
|
874 else if (arg2 == "quiet") |
|
875 { |
|
876 if (arg1 != "error") |
|
877 { |
|
878 Vquiet_warning = (arg1 == "on"); |
|
879 done = true; |
|
880 } |
|
881 } |
5567
|
882 else |
3935
|
883 { |
5567
|
884 if (arg2 == "last") |
|
885 arg2 = Vlast_warning_id; |
|
886 |
|
887 if (arg2 == "all") |
5794
|
888 initialize_warning_options (arg1); |
5567
|
889 else |
|
890 { |
|
891 Cell ident = warning_options.contents ("identifier"); |
|
892 Cell state = warning_options.contents ("state"); |
|
893 |
|
894 octave_idx_type nel = ident.numel (); |
|
895 |
|
896 bool found = false; |
|
897 |
|
898 for (octave_idx_type i = 0; i < nel; i++) |
|
899 { |
|
900 if (ident(i).string_value () == arg2) |
|
901 { |
5775
|
902 // FIXME -- if state for "all" is |
5567
|
903 // same as arg1, we can simply remove the |
|
904 // item from the list. |
|
905 |
|
906 state(i) = arg1; |
|
907 warning_options.assign ("state", state); |
|
908 found = true; |
|
909 break; |
|
910 } |
|
911 } |
|
912 |
|
913 if (! found) |
|
914 { |
5775
|
915 // FIXME -- if state for "all" is |
5567
|
916 // same as arg1, we don't need to do anything. |
|
917 |
|
918 ident.resize (dim_vector (1, nel+1)); |
|
919 state.resize (dim_vector (1, nel+1)); |
|
920 |
|
921 ident(nel) = arg2; |
|
922 state(nel) = arg1; |
|
923 |
|
924 warning_options.clear (); |
|
925 |
|
926 warning_options.assign ("identifier", ident); |
|
927 warning_options.assign ("state", state); |
|
928 } |
|
929 } |
|
930 |
3935
|
931 done = true; |
|
932 } |
5567
|
933 |
|
934 if (done && nargout > 0) |
|
935 retval = warning_options; |
|
936 } |
|
937 else if (arg1 == "query") |
|
938 { |
|
939 if (arg2 == "all") |
|
940 retval = warning_options; |
|
941 else if (arg2 == "backtrace" || arg2 == "debug" |
5582
|
942 || arg2 == "verbose" || arg2 == "quiet") |
5567
|
943 { |
|
944 Octave_map tmp; |
|
945 tmp.assign ("identifier", arg2); |
|
946 if (arg2 == "backtrace") |
|
947 tmp.assign ("state", Vbacktrace_on_warning ? "on" : "off"); |
|
948 else if (arg2 == "debug") |
|
949 tmp.assign ("state", Vdebug_on_warning ? "on" : "off"); |
5582
|
950 else if (arg2 == "verbose") |
|
951 tmp.assign ("state", Vverbose_warning ? "on" : "off"); |
5567
|
952 else |
5582
|
953 tmp.assign ("state", Vquiet_warning ? "on" : "off"); |
|
954 |
|
955 retval = tmp; |
5567
|
956 } |
|
957 else |
3935
|
958 { |
5567
|
959 if (arg2 == "last") |
|
960 arg2 = Vlast_warning_id; |
|
961 |
|
962 Cell ident = warning_options.contents ("identifier"); |
|
963 Cell state = warning_options.contents ("state"); |
|
964 |
|
965 octave_idx_type nel = ident.numel (); |
|
966 |
|
967 bool found = false; |
|
968 |
|
969 std::string val; |
|
970 |
|
971 for (octave_idx_type i = 0; i < nel; i++) |
|
972 { |
|
973 if (ident(i).string_value () == arg2) |
|
974 { |
|
975 val = state(i).string_value (); |
|
976 found = true; |
|
977 break; |
|
978 } |
|
979 } |
|
980 |
5781
|
981 if (! found) |
|
982 { |
|
983 for (octave_idx_type i = 0; i < nel; i++) |
|
984 { |
|
985 if (ident(i).string_value () == "all") |
|
986 { |
|
987 val = state(i).string_value (); |
|
988 found = true; |
|
989 break; |
|
990 } |
|
991 } |
|
992 } |
|
993 |
5567
|
994 if (found) |
|
995 { |
|
996 Octave_map tmp; |
|
997 |
|
998 tmp.assign ("identifier", arg2); |
|
999 tmp.assign ("state", val); |
|
1000 |
|
1001 retval = tmp; |
|
1002 } |
|
1003 else |
5781
|
1004 error ("warning: unable to find default warning state!"); |
3935
|
1005 } |
5567
|
1006 |
|
1007 done = true; |
3935
|
1008 } |
3934
|
1009 } |
|
1010 } |
5567
|
1011 else if (argc == 1) |
|
1012 { |
|
1013 retval = warning_options; |
3934
|
1014 |
5567
|
1015 done = true; |
|
1016 } |
|
1017 else if (argc == 2) |
|
1018 { |
|
1019 octave_value arg = args(0); |
|
1020 |
|
1021 Octave_map old_warning_options = warning_options; |
|
1022 |
|
1023 if (arg.is_map ()) |
|
1024 { |
|
1025 Octave_map m = arg.map_value (); |
|
1026 |
|
1027 if (m.contains ("identifier") && m.contains ("state")) |
|
1028 warning ("warning: setting state with structure not implemented"); |
|
1029 else |
|
1030 error ("warning: expecting structure with fields `identifier' and `state'"); |
|
1031 |
|
1032 done = true; |
|
1033 |
|
1034 if (nargout > 0) |
|
1035 retval = old_warning_options; |
|
1036 } |
|
1037 } |
|
1038 |
|
1039 if (! (error_state || done)) |
3935
|
1040 { |
5567
|
1041 octave_value_list nargs = args; |
|
1042 |
|
1043 std::string id; |
|
1044 |
|
1045 if (nargin > 1) |
|
1046 { |
|
1047 std::string arg1 = args(0).string_value (); |
|
1048 |
|
1049 if (! error_state) |
|
1050 { |
|
1051 if (arg1.find ('%') == NPOS) |
|
1052 { |
|
1053 id = arg1; |
|
1054 |
|
1055 nargs.resize (nargin-1); |
|
1056 |
|
1057 for (int i = 1; i < nargin; i++) |
|
1058 nargs(i-1) = args(i); |
|
1059 } |
|
1060 } |
|
1061 else |
|
1062 return retval; |
|
1063 } |
|
1064 |
|
1065 // handle_message. |
|
1066 |
3935
|
1067 std::string prev_msg = Vlast_warning_message; |
|
1068 |
5567
|
1069 std::string curr_msg = handle_message (warning_with_id, id.c_str (), |
|
1070 "unspecified warning", nargs); |
3935
|
1071 |
|
1072 if (nargout > 0) |
5567
|
1073 retval = prev_msg; |
3935
|
1074 } |
|
1075 |
3934
|
1076 return retval; |
|
1077 } |
|
1078 |
5794
|
1079 static void |
|
1080 disable_warning (const std::string& id) |
|
1081 { |
|
1082 octave_value_list args; |
|
1083 |
|
1084 args(1) = id; |
|
1085 args(0) = "off"; |
|
1086 |
|
1087 Fwarning (args, 0); |
|
1088 } |
|
1089 |
|
1090 void |
|
1091 initialize_default_warning_state (void) |
|
1092 { |
|
1093 initialize_warning_options ("on"); |
|
1094 |
|
1095 // Most people will want to have the following disabled. |
|
1096 |
|
1097 disable_warning ("Octave:array-to-scalar"); |
|
1098 disable_warning ("Octave:array-to-vector"); |
|
1099 disable_warning ("Octave:empty-list-elements"); |
|
1100 disable_warning ("Octave:fortran-indexing"); |
|
1101 disable_warning ("Octave:imag-to-real"); |
|
1102 disable_warning ("Octave:matlab-incompatible"); |
|
1103 disable_warning ("Octave:missing-semicolon"); |
|
1104 disable_warning ("Octave:neg-dim-as-zero"); |
|
1105 disable_warning ("Octave:resize-on-range-error"); |
|
1106 disable_warning ("Octave:separator-insert"); |
|
1107 disable_warning ("Octave:single-quote-string"); |
|
1108 disable_warning ("Octave:str-to-num"); |
|
1109 disable_warning ("Octave:string-concat"); |
|
1110 disable_warning ("Octave:variable-switch-label"); |
|
1111 } |
|
1112 |
5567
|
1113 DEFUN (lasterr, args, nargout, |
3935
|
1114 "-*- texinfo -*-\n\ |
5567
|
1115 @deftypefn {Built-in Function} {[@var{msg}, @var{msgid}] =} lasterr (@var{msg}, @var{msgid})\n\ |
3935
|
1116 Without any arguments, return the last error message. With one\n\ |
5567
|
1117 argument, set the last error message to @var{msg}. With two arguments,\n\ |
|
1118 also set the last message identifier.\n\ |
3935
|
1119 @end deftypefn") |
|
1120 { |
|
1121 octave_value_list retval; |
|
1122 |
|
1123 int argc = args.length () + 1; |
|
1124 |
5567
|
1125 if (argc < 4) |
5335
|
1126 { |
|
1127 string_vector argv = args.make_argv ("lasterr"); |
3935
|
1128 |
5335
|
1129 if (! error_state) |
|
1130 { |
5567
|
1131 std::string prev_error_id = Vlast_error_id; |
|
1132 std::string prev_error_message = Vlast_error_message; |
|
1133 |
|
1134 if (argc > 2) |
|
1135 Vlast_error_id = argv(2); |
|
1136 |
|
1137 if (argc > 1) |
5335
|
1138 Vlast_error_message = argv(1); |
5567
|
1139 |
|
1140 if (argc == 1 || nargout > 0) |
|
1141 { |
|
1142 retval(1) = prev_error_id; |
|
1143 retval(0) = prev_error_message; |
|
1144 } |
5335
|
1145 } |
5567
|
1146 else |
5582
|
1147 error ("lasterr: expecting arguments to be character strings"); |
5335
|
1148 } |
3935
|
1149 else |
|
1150 print_usage ("lasterr"); |
|
1151 |
|
1152 return retval; |
|
1153 } |
|
1154 |
4699
|
1155 // For backward compatibility. |
|
1156 DEFALIAS (error_text, lasterr); |
|
1157 DEFALIAS (__error_text__, lasterr); |
|
1158 |
5567
|
1159 DEFUN (lastwarn, args, nargout, |
3934
|
1160 "-*- texinfo -*-\n\ |
5567
|
1161 @deftypefn {Built-in Function} {[@var{msg}, @var{msgid}] =} lastwarn (@var{msg}, @var{msgid})\n\ |
3935
|
1162 Without any arguments, return the last warning message. With one\n\ |
5567
|
1163 argument, set the last warning message to @var{msg}. With two arguments,\n\ |
|
1164 also set the last message identifier.\n\ |
3934
|
1165 @end deftypefn") |
|
1166 { |
|
1167 octave_value_list retval; |
|
1168 |
|
1169 int argc = args.length () + 1; |
|
1170 |
5567
|
1171 if (argc < 4) |
|
1172 { |
|
1173 string_vector argv = args.make_argv ("lastwarn"); |
|
1174 |
|
1175 if (! error_state) |
|
1176 { |
|
1177 std::string prev_warning_id = Vlast_warning_id; |
|
1178 std::string prev_warning_message = Vlast_warning_message; |
|
1179 |
|
1180 if (argc > 2) |
|
1181 Vlast_warning_id = argv(2); |
3934
|
1182 |
5567
|
1183 if (argc > 1) |
|
1184 Vlast_warning_message = argv(1); |
|
1185 |
|
1186 if (argc == 1 || nargout > 0) |
|
1187 { |
5582
|
1188 warning_state = 0; |
5567
|
1189 retval(1) = prev_warning_id; |
|
1190 retval(0) = prev_warning_message; |
|
1191 } |
|
1192 } |
|
1193 else |
|
1194 error ("lastwarn: expecting arguments to be character strings"); |
|
1195 } |
3934
|
1196 else |
|
1197 print_usage ("lastwarn"); |
|
1198 |
|
1199 return retval; |
897
|
1200 } |
|
1201 |
1957
|
1202 DEFUN (usage, args, , |
3373
|
1203 "-*- texinfo -*-\n\ |
|
1204 @deftypefn {Built-in Function} {} usage (@var{msg})\n\ |
|
1205 Print the message @var{msg}, prefixed by the string @samp{usage: }, and\n\ |
|
1206 set Octave's internal error state such that control will return to the\n\ |
|
1207 top level without evaluating any more commands. This is useful for\n\ |
|
1208 aborting from functions.\n\ |
|
1209 \n\ |
|
1210 After @code{usage} is evaluated, Octave will print a traceback of all\n\ |
|
1211 the function calls leading to the usage message.\n\ |
899
|
1212 \n\ |
3373
|
1213 You should use this function for reporting problems errors that result\n\ |
|
1214 from an improper call to a function, such as calling a function with an\n\ |
|
1215 incorrect number of arguments, or with arguments of the wrong type. For\n\ |
|
1216 example, most functions distributed with Octave begin with code like\n\ |
|
1217 this\n\ |
|
1218 \n\ |
|
1219 @example\n\ |
|
1220 @group\n\ |
|
1221 if (nargin != 2)\n\ |
|
1222 usage (\"foo (a, b)\");\n\ |
|
1223 endif\n\ |
|
1224 @end group\n\ |
|
1225 @end example\n\ |
|
1226 \n\ |
|
1227 @noindent\n\ |
|
1228 to check for the proper number of arguments.\n\ |
|
1229 @end deftypefn") |
899
|
1230 { |
3934
|
1231 octave_value_list retval; |
5567
|
1232 handle_message (usage_with_id, "", "unknown", args); |
3934
|
1233 return retval; |
899
|
1234 } |
|
1235 |
5794
|
1236 DEFUN (beep_on_error, args, nargout, |
|
1237 "-*- texinfo -*-\n\ |
|
1238 @deftypefn {Built-in Function} {@var{val} =} beep_on_error ()\n\ |
|
1239 @deftypefnx {Built-in Function} {@var{old_val} =} beep_on_error (@var{new_val})\n\ |
|
1240 Query or set the internal variable that controls whether Octave will try\n\ |
|
1241 to ring the terminal bell before printing an error message.\n\ |
|
1242 @end deftypefn") |
3707
|
1243 { |
5794
|
1244 return SET_INTERNAL_VARIABLE (beep_on_error); |
3707
|
1245 } |
|
1246 |
5794
|
1247 DEFUN (debug_on_error, args, nargout, |
3373
|
1248 "-*- texinfo -*-\n\ |
5794
|
1249 @deftypefn {Built-in Function} {@var{val} =} debug_on_error ()\n\ |
|
1250 @deftypefnx {Built-in Function} {@var{old_val} =} debug_on_error (@var{new_val})\n\ |
|
1251 Query or set the internal variable that controls whether Octave will try\n\ |
3707
|
1252 to enter the debugger when an error is encountered. This will also\n\ |
|
1253 inhibit printing of the normal traceback message (you will only see\n\ |
5794
|
1254 the top-level error message).\n\ |
|
1255 @end deftypefn") |
|
1256 { |
|
1257 return SET_INTERNAL_VARIABLE (debug_on_error); |
|
1258 } |
3707
|
1259 |
5794
|
1260 DEFUN (debug_on_warning, args, nargout, |
3707
|
1261 "-*- texinfo -*-\n\ |
5794
|
1262 @deftypefn {Built-in Function} {@var{val} =} debug_on_warning ()\n\ |
|
1263 @deftypefnx {Built-in Function} {@var{old_val} =} debug_on_warning (@var{new_val})\n\ |
|
1264 Query or set the internal variable that controls whether Octave will try\n\ |
|
1265 to enter the debugger when a warning is encountered.\n\ |
|
1266 @end deftypefn") |
|
1267 { |
|
1268 return SET_INTERNAL_VARIABLE (debug_on_warning); |
2174
|
1269 } |
|
1270 |
1
|
1271 /* |
|
1272 ;;; Local Variables: *** |
|
1273 ;;; mode: C++ *** |
|
1274 ;;; End: *** |
|
1275 */ |