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