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. |
|
52 int buffer_error_messages; |
|
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 |
|
90 ptr = strchr (msg, ':') + 2; |
|
91 ptr = ptr ? ptr : msg; |
|
92 } |
|
93 |
|
94 *error_message_buffer << ptr; |
|
95 } |
|
96 else |
|
97 { |
2095
|
98 octave_diary << msg; |
1489
|
99 cerr << msg; |
|
100 } |
581
|
101 |
|
102 delete [] msg; |
1
|
103 } |
|
104 |
1266
|
105 // Note that we don't actually print any message if the error string |
|
106 // is just "" or "\n". This allows error ("") and error ("\n") to |
|
107 // just set the error state. |
|
108 |
1005
|
109 static void |
|
110 error_1 (const char *name, const char *fmt, va_list args) |
|
111 { |
|
112 if (error_state != -2) |
|
113 { |
1489
|
114 if (fmt) |
1005
|
115 { |
1489
|
116 if (*fmt) |
1005
|
117 { |
1489
|
118 int len = strlen (fmt); |
|
119 if (fmt[len - 1] == '\n') |
1266
|
120 { |
1489
|
121 if (len > 1) |
1266
|
122 { |
1489
|
123 char *tmp_fmt = strsave (fmt); |
|
124 tmp_fmt[len - 1] = '\0'; |
|
125 verror (name, tmp_fmt, args); |
|
126 delete [] tmp_fmt; |
|
127 } |
1423
|
128 |
1489
|
129 error_state = -2; |
1266
|
130 } |
1489
|
131 else |
|
132 verror (name, fmt, args); |
1005
|
133 } |
|
134 } |
1489
|
135 else |
|
136 panic ("error_1: invalid format"); |
1423
|
137 |
|
138 if (! error_state) |
|
139 error_state = 1; |
1005
|
140 } |
|
141 } |
|
142 |
1
|
143 void |
|
144 message (const char *name, const char *fmt, ...) |
|
145 { |
|
146 va_list args; |
|
147 va_start (args, fmt); |
|
148 verror (name, fmt, args); |
|
149 va_end (args); |
|
150 } |
|
151 |
|
152 void |
|
153 usage (const char *fmt, ...) |
|
154 { |
|
155 va_list args; |
|
156 va_start (args, fmt); |
905
|
157 error_state = -1; |
1
|
158 verror ("usage", fmt, args); |
|
159 va_end (args); |
|
160 } |
|
161 |
|
162 void |
|
163 warning (const char *fmt, ...) |
|
164 { |
|
165 va_list args; |
|
166 va_start (args, fmt); |
|
167 verror ("warning", fmt, args); |
|
168 va_end (args); |
|
169 } |
|
170 |
|
171 void |
|
172 error (const char *fmt, ...) |
|
173 { |
|
174 va_list args; |
|
175 va_start (args, fmt); |
1005
|
176 error_1 ("error", fmt, args); |
|
177 va_end (args); |
|
178 } |
436
|
179 |
1005
|
180 void |
|
181 parse_error (const char *fmt, ...) |
|
182 { |
|
183 va_list args; |
|
184 va_start (args, fmt); |
|
185 error_1 (0, fmt, args); |
1
|
186 va_end (args); |
|
187 } |
|
188 |
189
|
189 void |
1
|
190 panic (const char *fmt, ...) |
|
191 { |
2095
|
192 flush_octave_stdout (); |
169
|
193 |
1
|
194 va_list args; |
|
195 va_start (args, fmt); |
|
196 verror ("panic", fmt, args); |
|
197 va_end (args); |
|
198 abort (); |
|
199 } |
|
200 |
1489
|
201 typedef void (*error_fun)(const char *, ...); |
|
202 |
2086
|
203 extern octave_value_list Fsprintf (const octave_value_list&, int); |
1489
|
204 |
2086
|
205 static octave_value_list |
|
206 handle_message (error_fun f, const char *msg, const octave_value_list& args) |
528
|
207 { |
2086
|
208 octave_value_list retval; |
528
|
209 |
1728
|
210 string tstr; |
|
211 |
528
|
212 int nargin = args.length (); |
|
213 |
2745
|
214 if (nargin > 0) |
528
|
215 { |
2745
|
216 octave_value arg = ((nargin > 1) ? Fsprintf (args, 1) : args) (0); |
|
217 |
|
218 if (arg.is_defined ()) |
528
|
219 { |
2745
|
220 if (arg.is_string ()) |
|
221 { |
|
222 tstr = arg.string_value (); |
|
223 msg = tstr.c_str (); |
|
224 |
|
225 if (! msg) |
|
226 return retval; |
|
227 } |
|
228 else if (arg.is_empty ()) |
528
|
229 return retval; |
|
230 } |
|
231 } |
|
232 |
1489
|
233 // Ugh. |
|
234 |
|
235 int len = strlen (msg); |
|
236 if (msg[len - 1] == '\n') |
|
237 { |
|
238 if (len > 1) |
|
239 { |
|
240 char *tmp_msg = strsave (msg); |
|
241 tmp_msg[len - 1] = '\0'; |
|
242 f ("%s\n", tmp_msg); |
|
243 delete [] tmp_msg; |
|
244 } |
|
245 } |
|
246 else |
|
247 f ("%s", msg); |
528
|
248 |
|
249 return retval; |
|
250 } |
|
251 |
1957
|
252 DEFUN (error, args, , |
1489
|
253 "error (FMT, ...): print message according to FMT and set error state.\n\ |
897
|
254 \n\ |
1489
|
255 This should eventually take us up to the top level, possibly\n\ |
|
256 printing traceback messages as we go.\n\ |
|
257 \n\ |
2802
|
258 If the resulting error message ends in a newline character, traceback\n\ |
2620
|
259 messages are not printed.\n\ |
1489
|
260 \n\ |
|
261 See also: printf") |
897
|
262 { |
1489
|
263 return handle_message (error, "unspecified error", args); |
|
264 } |
897
|
265 |
1957
|
266 DEFUN (warning, args, , |
1489
|
267 "warning (FMT, ...): print a warning message according to FMT.\n\ |
|
268 \n\ |
|
269 See also: error, printf") |
|
270 { |
|
271 return handle_message (warning, "unspecified warning", args); |
897
|
272 } |
|
273 |
1957
|
274 DEFUN (usage, args, , |
1489
|
275 "usage (FMT, ...): print a usage message according to FMT.\n\ |
899
|
276 \n\ |
1489
|
277 See also: error, printf") |
899
|
278 { |
1489
|
279 return handle_message (usage, "unknown", args); |
899
|
280 } |
|
281 |
2174
|
282 static int |
|
283 beep_on_error (void) |
|
284 { |
|
285 Vbeep_on_error = check_preference ("beep_on_error"); |
|
286 |
|
287 return 0; |
|
288 } |
|
289 |
|
290 void |
|
291 symbols_of_error (void) |
|
292 { |
|
293 DEFVAR (beep_on_error, 0.0, 0, beep_on_error, |
|
294 "if true, beep before printing error messages"); |
|
295 } |
|
296 |
1
|
297 /* |
|
298 ;;; Local Variables: *** |
|
299 ;;; mode: C++ *** |
|
300 ;;; End: *** |
|
301 */ |