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 |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
240
|
23 #ifdef HAVE_CONFIG_H |
1192
|
24 #include <config.h> |
1
|
25 #endif |
|
26 |
1343
|
27 #include <cstdarg> |
1633
|
28 #include <cstring> |
1343
|
29 |
1728
|
30 #include <string> |
|
31 |
581
|
32 #include <strstream.h> |
1
|
33 |
1352
|
34 #include "defun.h" |
1
|
35 #include "error.h" |
1742
|
36 #include "pager.h" |
1352
|
37 #include "oct-obj.h" |
|
38 #include "utils.h" |
2370
|
39 #include "ov.h" |
|
40 #include "variables.h" |
1
|
41 |
2174
|
42 // TRUE means that Octave will try to beep obnoxiously before printing |
|
43 // error messages. |
|
44 static bool Vbeep_on_error; |
|
45 |
143
|
46 // Current error state. |
672
|
47 int error_state = 0; |
|
48 |
1489
|
49 // Tell the error handler whether to print messages, or just store |
|
50 // them for later. Used for handling errors in eval() and |
|
51 // the `unwind_protect' statement. |
3136
|
52 bool buffer_error_messages = false; |
1489
|
53 |
|
54 // The message buffer |
|
55 ostrstream *error_message_buffer = 0; |
143
|
56 |
1
|
57 static void |
|
58 verror (const char *name, const char *fmt, va_list args) |
|
59 { |
2095
|
60 flush_octave_stdout (); |
914
|
61 |
2174
|
62 bool to_beep_or_not_to_beep_p = Vbeep_on_error && ! error_state; |
1423
|
63 |
581
|
64 ostrstream output_buf; |
1
|
65 |
2174
|
66 if (to_beep_or_not_to_beep_p) |
1423
|
67 output_buf << "\a"; |
599
|
68 if (name) |
|
69 output_buf << name << ": "; |
581
|
70 output_buf.vform (fmt, args); |
1165
|
71 output_buf << endl << ends; |
581
|
72 |
|
73 char *msg = output_buf.str (); |
|
74 |
1489
|
75 if (buffer_error_messages) |
|
76 { |
|
77 char *ptr = msg; |
|
78 |
|
79 if (! error_message_buffer) |
|
80 { |
|
81 error_message_buffer = new ostrstream; |
|
82 |
|
83 // XXX FIXME XXX -- this is ugly, but it prevents |
|
84 // |
|
85 // eval ("error (\"msg\")", "error (__error_text__)"); |
|
86 // |
|
87 // from printing `error: ' twice. Assumes that the NAME we |
|
88 // have been given doesn't contain `:'. |
|
89 |
3162
|
90 ptr = strchr (msg, ':'); |
|
91 |
|
92 if (ptr) |
|
93 { |
|
94 if (*++ptr != '\0') |
|
95 ++ptr; |
|
96 } |
|
97 else |
|
98 ptr = msg; |
1489
|
99 } |
|
100 |
|
101 *error_message_buffer << ptr; |
|
102 } |
|
103 else |
|
104 { |
2095
|
105 octave_diary << msg; |
1489
|
106 cerr << msg; |
|
107 } |
581
|
108 |
|
109 delete [] msg; |
1
|
110 } |
|
111 |
1266
|
112 // Note that we don't actually print any message if the error string |
|
113 // is just "" or "\n". This allows error ("") and error ("\n") to |
|
114 // just set the error state. |
|
115 |
1005
|
116 static void |
|
117 error_1 (const char *name, const char *fmt, va_list args) |
|
118 { |
|
119 if (error_state != -2) |
|
120 { |
1489
|
121 if (fmt) |
1005
|
122 { |
1489
|
123 if (*fmt) |
1005
|
124 { |
1489
|
125 int len = strlen (fmt); |
|
126 if (fmt[len - 1] == '\n') |
1266
|
127 { |
1489
|
128 if (len > 1) |
1266
|
129 { |
1489
|
130 char *tmp_fmt = strsave (fmt); |
|
131 tmp_fmt[len - 1] = '\0'; |
|
132 verror (name, tmp_fmt, args); |
|
133 delete [] tmp_fmt; |
|
134 } |
1423
|
135 |
1489
|
136 error_state = -2; |
1266
|
137 } |
1489
|
138 else |
|
139 verror (name, fmt, args); |
1005
|
140 } |
|
141 } |
1489
|
142 else |
|
143 panic ("error_1: invalid format"); |
1423
|
144 |
|
145 if (! error_state) |
|
146 error_state = 1; |
1005
|
147 } |
|
148 } |
|
149 |
1
|
150 void |
|
151 message (const char *name, const char *fmt, ...) |
|
152 { |
|
153 va_list args; |
|
154 va_start (args, fmt); |
|
155 verror (name, fmt, args); |
|
156 va_end (args); |
|
157 } |
|
158 |
|
159 void |
|
160 usage (const char *fmt, ...) |
|
161 { |
|
162 va_list args; |
|
163 va_start (args, fmt); |
905
|
164 error_state = -1; |
1
|
165 verror ("usage", fmt, args); |
|
166 va_end (args); |
|
167 } |
|
168 |
|
169 void |
|
170 warning (const char *fmt, ...) |
|
171 { |
|
172 va_list args; |
|
173 va_start (args, fmt); |
|
174 verror ("warning", fmt, args); |
|
175 va_end (args); |
|
176 } |
|
177 |
|
178 void |
|
179 error (const char *fmt, ...) |
|
180 { |
|
181 va_list args; |
|
182 va_start (args, fmt); |
1005
|
183 error_1 ("error", fmt, args); |
|
184 va_end (args); |
|
185 } |
436
|
186 |
1005
|
187 void |
|
188 parse_error (const char *fmt, ...) |
|
189 { |
|
190 va_list args; |
|
191 va_start (args, fmt); |
|
192 error_1 (0, fmt, args); |
1
|
193 va_end (args); |
|
194 } |
|
195 |
189
|
196 void |
1
|
197 panic (const char *fmt, ...) |
|
198 { |
2095
|
199 flush_octave_stdout (); |
169
|
200 |
1
|
201 va_list args; |
|
202 va_start (args, fmt); |
|
203 verror ("panic", fmt, args); |
|
204 va_end (args); |
|
205 abort (); |
|
206 } |
|
207 |
1489
|
208 typedef void (*error_fun)(const char *, ...); |
|
209 |
2086
|
210 extern octave_value_list Fsprintf (const octave_value_list&, int); |
1489
|
211 |
2086
|
212 static octave_value_list |
|
213 handle_message (error_fun f, const char *msg, const octave_value_list& args) |
528
|
214 { |
2086
|
215 octave_value_list retval; |
528
|
216 |
1728
|
217 string tstr; |
|
218 |
528
|
219 int nargin = args.length (); |
|
220 |
2745
|
221 if (nargin > 0) |
528
|
222 { |
3066
|
223 octave_value arg; |
|
224 |
|
225 if (nargin > 1) |
|
226 { |
|
227 octave_value_list tmp = Fsprintf (args, 1); |
|
228 arg = tmp(0); |
|
229 } |
|
230 else |
|
231 arg = args(0); |
2745
|
232 |
|
233 if (arg.is_defined ()) |
528
|
234 { |
2745
|
235 if (arg.is_string ()) |
|
236 { |
|
237 tstr = arg.string_value (); |
|
238 msg = tstr.c_str (); |
|
239 |
|
240 if (! msg) |
|
241 return retval; |
|
242 } |
|
243 else if (arg.is_empty ()) |
528
|
244 return retval; |
|
245 } |
|
246 } |
|
247 |
1489
|
248 // Ugh. |
|
249 |
|
250 int len = strlen (msg); |
|
251 if (msg[len - 1] == '\n') |
|
252 { |
|
253 if (len > 1) |
|
254 { |
|
255 char *tmp_msg = strsave (msg); |
|
256 tmp_msg[len - 1] = '\0'; |
|
257 f ("%s\n", tmp_msg); |
|
258 delete [] tmp_msg; |
|
259 } |
|
260 } |
|
261 else |
|
262 f ("%s", msg); |
528
|
263 |
|
264 return retval; |
|
265 } |
|
266 |
1957
|
267 DEFUN (error, args, , |
1489
|
268 "error (FMT, ...): print message according to FMT and set error state.\n\ |
897
|
269 \n\ |
1489
|
270 This should eventually take us up to the top level, possibly\n\ |
|
271 printing traceback messages as we go.\n\ |
|
272 \n\ |
2802
|
273 If the resulting error message ends in a newline character, traceback\n\ |
2620
|
274 messages are not printed.\n\ |
1489
|
275 \n\ |
|
276 See also: printf") |
897
|
277 { |
1489
|
278 return handle_message (error, "unspecified error", args); |
|
279 } |
897
|
280 |
1957
|
281 DEFUN (warning, args, , |
1489
|
282 "warning (FMT, ...): print a warning message according to FMT.\n\ |
|
283 \n\ |
|
284 See also: error, printf") |
|
285 { |
|
286 return handle_message (warning, "unspecified warning", args); |
897
|
287 } |
|
288 |
1957
|
289 DEFUN (usage, args, , |
1489
|
290 "usage (FMT, ...): print a usage message according to FMT.\n\ |
899
|
291 \n\ |
1489
|
292 See also: error, printf") |
899
|
293 { |
1489
|
294 return handle_message (usage, "unknown", args); |
899
|
295 } |
|
296 |
3018
|
297 void |
|
298 bind_global_error_variable (void) |
|
299 { |
|
300 *error_message_buffer << ends; |
|
301 |
|
302 char *error_text = error_message_buffer->str (); |
|
303 |
|
304 bind_builtin_variable ("__error_text__", error_text, 1); |
|
305 |
|
306 delete [] error_text; |
|
307 |
|
308 delete error_message_buffer; |
|
309 |
|
310 error_message_buffer = 0; |
|
311 } |
|
312 |
|
313 void |
|
314 clear_global_error_variable (void *) |
|
315 { |
|
316 delete error_message_buffer; |
|
317 error_message_buffer = 0; |
|
318 |
|
319 bind_builtin_variable ("__error_text__", "", 1); |
|
320 } |
|
321 |
2174
|
322 static int |
|
323 beep_on_error (void) |
|
324 { |
|
325 Vbeep_on_error = check_preference ("beep_on_error"); |
|
326 |
|
327 return 0; |
|
328 } |
|
329 |
|
330 void |
|
331 symbols_of_error (void) |
|
332 { |
|
333 DEFVAR (beep_on_error, 0.0, 0, beep_on_error, |
|
334 "if true, beep before printing error messages"); |
3018
|
335 |
3141
|
336 DEFCONST (error_text, "", |
3018
|
337 "the text of error messages that would have been printed in the\n\ |
|
338 body of the most recent unwind_protect statement or the TRY part of\n\ |
|
339 the most recent eval() command. Outside of unwind_protect and\n\ |
|
340 eval(), or if no error has ocurred within them, the value of\n\ |
|
341 __error_text__ is guaranteed to be the empty string."); |
2174
|
342 } |
|
343 |
1
|
344 /* |
|
345 ;;; Local Variables: *** |
|
346 ;;; mode: C++ *** |
|
347 ;;; End: *** |
|
348 */ |