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