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