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