1
|
1 /* |
|
2 |
1884
|
3 Copyright (C) 1996 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" |
|
37 #include "pt-const.h" |
1352
|
38 #include "oct-obj.h" |
|
39 #include "utils.h" |
1
|
40 |
2174
|
41 // TRUE means that Octave will try to beep obnoxiously before printing |
|
42 // error messages. |
|
43 static bool Vbeep_on_error; |
|
44 |
143
|
45 // Current error state. |
672
|
46 int error_state = 0; |
|
47 |
1489
|
48 // Tell the error handler whether to print messages, or just store |
|
49 // them for later. Used for handling errors in eval() and |
|
50 // the `unwind_protect' statement. |
|
51 int buffer_error_messages; |
|
52 |
|
53 // The message buffer |
|
54 ostrstream *error_message_buffer = 0; |
143
|
55 |
1
|
56 static void |
|
57 verror (const char *name, const char *fmt, va_list args) |
|
58 { |
2095
|
59 flush_octave_stdout (); |
914
|
60 |
2174
|
61 bool to_beep_or_not_to_beep_p = Vbeep_on_error && ! error_state; |
1423
|
62 |
581
|
63 ostrstream output_buf; |
1
|
64 |
2174
|
65 if (to_beep_or_not_to_beep_p) |
1423
|
66 output_buf << "\a"; |
599
|
67 if (name) |
|
68 output_buf << name << ": "; |
581
|
69 output_buf.vform (fmt, args); |
1165
|
70 output_buf << endl << ends; |
581
|
71 |
|
72 char *msg = output_buf.str (); |
|
73 |
1489
|
74 if (buffer_error_messages) |
|
75 { |
|
76 char *ptr = msg; |
|
77 |
|
78 if (! error_message_buffer) |
|
79 { |
|
80 error_message_buffer = new ostrstream; |
|
81 |
|
82 // XXX FIXME XXX -- this is ugly, but it prevents |
|
83 // |
|
84 // eval ("error (\"msg\")", "error (__error_text__)"); |
|
85 // |
|
86 // from printing `error: ' twice. Assumes that the NAME we |
|
87 // have been given doesn't contain `:'. |
|
88 |
|
89 ptr = strchr (msg, ':') + 2; |
|
90 ptr = ptr ? ptr : msg; |
|
91 } |
|
92 |
|
93 *error_message_buffer << ptr; |
|
94 } |
|
95 else |
|
96 { |
2095
|
97 octave_diary << msg; |
1489
|
98 cerr << msg; |
|
99 } |
581
|
100 |
|
101 delete [] msg; |
1
|
102 } |
|
103 |
1266
|
104 // Note that we don't actually print any message if the error string |
|
105 // is just "" or "\n". This allows error ("") and error ("\n") to |
|
106 // just set the error state. |
|
107 |
1005
|
108 static void |
|
109 error_1 (const char *name, const char *fmt, va_list args) |
|
110 { |
|
111 if (error_state != -2) |
|
112 { |
1489
|
113 if (fmt) |
1005
|
114 { |
1489
|
115 if (*fmt) |
1005
|
116 { |
1489
|
117 int len = strlen (fmt); |
|
118 if (fmt[len - 1] == '\n') |
1266
|
119 { |
1489
|
120 if (len > 1) |
1266
|
121 { |
1489
|
122 char *tmp_fmt = strsave (fmt); |
|
123 tmp_fmt[len - 1] = '\0'; |
|
124 verror (name, tmp_fmt, args); |
|
125 delete [] tmp_fmt; |
|
126 } |
1423
|
127 |
1489
|
128 error_state = -2; |
1266
|
129 } |
1489
|
130 else |
|
131 verror (name, fmt, args); |
1005
|
132 } |
|
133 } |
1489
|
134 else |
|
135 panic ("error_1: invalid format"); |
1423
|
136 |
|
137 if (! error_state) |
|
138 error_state = 1; |
1005
|
139 } |
|
140 } |
|
141 |
1
|
142 void |
|
143 message (const char *name, const char *fmt, ...) |
|
144 { |
|
145 va_list args; |
|
146 va_start (args, fmt); |
|
147 verror (name, fmt, args); |
|
148 va_end (args); |
|
149 } |
|
150 |
|
151 void |
|
152 usage (const char *fmt, ...) |
|
153 { |
|
154 va_list args; |
|
155 va_start (args, fmt); |
905
|
156 error_state = -1; |
1
|
157 verror ("usage", fmt, args); |
|
158 va_end (args); |
|
159 } |
|
160 |
|
161 void |
|
162 warning (const char *fmt, ...) |
|
163 { |
|
164 va_list args; |
|
165 va_start (args, fmt); |
|
166 verror ("warning", fmt, args); |
|
167 va_end (args); |
|
168 } |
|
169 |
|
170 void |
|
171 error (const char *fmt, ...) |
|
172 { |
|
173 va_list args; |
|
174 va_start (args, fmt); |
1005
|
175 error_1 ("error", fmt, args); |
|
176 va_end (args); |
|
177 } |
436
|
178 |
1005
|
179 void |
|
180 parse_error (const char *fmt, ...) |
|
181 { |
|
182 va_list args; |
|
183 va_start (args, fmt); |
|
184 error_1 (0, fmt, args); |
1
|
185 va_end (args); |
|
186 } |
|
187 |
189
|
188 void |
1
|
189 panic (const char *fmt, ...) |
|
190 { |
2095
|
191 flush_octave_stdout (); |
169
|
192 |
1
|
193 va_list args; |
|
194 va_start (args, fmt); |
|
195 verror ("panic", fmt, args); |
|
196 va_end (args); |
|
197 abort (); |
|
198 } |
|
199 |
1489
|
200 typedef void (*error_fun)(const char *, ...); |
|
201 |
2086
|
202 extern octave_value_list Fsprintf (const octave_value_list&, int); |
1489
|
203 |
2086
|
204 static octave_value_list |
|
205 handle_message (error_fun f, const char *msg, const octave_value_list& args) |
528
|
206 { |
2086
|
207 octave_value_list retval; |
528
|
208 |
1728
|
209 string tstr; |
|
210 |
528
|
211 int nargin = args.length (); |
|
212 |
2086
|
213 octave_value arg = ((nargin > 1) ? Fsprintf (args, 1) : args) (0); |
1489
|
214 |
|
215 if (arg.is_defined ()) |
528
|
216 { |
1489
|
217 if (arg.is_string ()) |
528
|
218 { |
1728
|
219 tstr = arg.string_value (); |
|
220 msg = tstr.c_str (); |
528
|
221 |
1266
|
222 if (! msg) |
528
|
223 return retval; |
|
224 } |
1489
|
225 else if (arg.is_empty ()) |
528
|
226 return retval; |
|
227 } |
|
228 |
1489
|
229 // Ugh. |
|
230 |
|
231 int len = strlen (msg); |
|
232 if (msg[len - 1] == '\n') |
|
233 { |
|
234 if (len > 1) |
|
235 { |
|
236 char *tmp_msg = strsave (msg); |
|
237 tmp_msg[len - 1] = '\0'; |
|
238 f ("%s\n", tmp_msg); |
|
239 delete [] tmp_msg; |
|
240 } |
|
241 } |
|
242 else |
|
243 f ("%s", msg); |
528
|
244 |
|
245 return retval; |
|
246 } |
|
247 |
1957
|
248 DEFUN (error, args, , |
1489
|
249 "error (FMT, ...): print message according to FMT and set error state.\n\ |
897
|
250 \n\ |
1489
|
251 This should eventually take us up to the top level, possibly\n\ |
|
252 printing traceback messages as we go.\n\ |
|
253 \n\ |
|
254 If MESSAGE ends in a newline character, traceback messages are not\n\ |
|
255 printed.\n\ |
|
256 \n\ |
|
257 See also: printf") |
897
|
258 { |
1489
|
259 return handle_message (error, "unspecified error", args); |
|
260 } |
897
|
261 |
1957
|
262 DEFUN (warning, args, , |
1489
|
263 "warning (FMT, ...): print a warning message according to FMT.\n\ |
|
264 \n\ |
|
265 See also: error, printf") |
|
266 { |
|
267 return handle_message (warning, "unspecified warning", args); |
897
|
268 } |
|
269 |
1957
|
270 DEFUN (usage, args, , |
1489
|
271 "usage (FMT, ...): print a usage message according to FMT.\n\ |
899
|
272 \n\ |
1489
|
273 See also: error, printf") |
899
|
274 { |
1489
|
275 return handle_message (usage, "unknown", args); |
899
|
276 } |
|
277 |
2174
|
278 static int |
|
279 beep_on_error (void) |
|
280 { |
|
281 Vbeep_on_error = check_preference ("beep_on_error"); |
|
282 |
|
283 return 0; |
|
284 } |
|
285 |
|
286 void |
|
287 symbols_of_error (void) |
|
288 { |
|
289 DEFVAR (beep_on_error, 0.0, 0, beep_on_error, |
|
290 "if true, beep before printing error messages"); |
|
291 } |
|
292 |
1
|
293 /* |
|
294 ;;; Local Variables: *** |
|
295 ;;; mode: C++ *** |
|
296 ;;; End: *** |
|
297 */ |