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