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 |
|
43 // XXX FIXME XXX |
|
44 int suppress_octave_error_messages = 0; |
143
|
45 |
1
|
46 static void |
|
47 verror (const char *name, const char *fmt, va_list args) |
|
48 { |
914
|
49 flush_output_to_pager (); |
|
50 |
1423
|
51 int to_beep_or_not_to_beep = user_pref.beep_on_error && ! error_state; |
|
52 |
|
53 if (to_beep_or_not_to_beep) |
|
54 cerr << "\a"; |
600
|
55 if (name) |
|
56 cerr << name << ": "; |
581
|
57 cerr.vform (fmt, args); |
|
58 cerr << endl; |
|
59 |
|
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 |
|
71 maybe_write_to_diary_file (msg); |
|
72 |
|
73 delete [] msg; |
1
|
74 } |
|
75 |
1266
|
76 // Note that we don't actually print any message if the error string |
|
77 // is just "" or "\n". This allows error ("") and error ("\n") to |
|
78 // just set the error state. |
|
79 |
1005
|
80 static void |
|
81 error_1 (const char *name, const char *fmt, va_list args) |
|
82 { |
|
83 if (error_state != -2) |
|
84 { |
|
85 if (! suppress_octave_error_messages) |
|
86 { |
1266
|
87 if (fmt) |
1005
|
88 { |
1266
|
89 if (*fmt) |
|
90 { |
|
91 int len = strlen (fmt); |
|
92 if (fmt[len - 1] == '\n') |
|
93 { |
|
94 if (len > 1) |
|
95 { |
|
96 char *tmp_fmt = strsave (fmt); |
|
97 tmp_fmt[len - 1] = '\0'; |
|
98 verror (name, tmp_fmt, args); |
|
99 delete [] tmp_fmt; |
|
100 } |
1423
|
101 |
|
102 error_state = -2; |
1266
|
103 } |
|
104 else |
|
105 verror (name, fmt, args); |
|
106 } |
1005
|
107 } |
|
108 else |
1266
|
109 panic ("error_1: invalid format"); |
1005
|
110 } |
1423
|
111 |
|
112 if (! error_state) |
|
113 error_state = 1; |
1005
|
114 } |
|
115 } |
|
116 |
1
|
117 void |
|
118 message (const char *name, const char *fmt, ...) |
|
119 { |
|
120 va_list args; |
|
121 va_start (args, fmt); |
|
122 verror (name, fmt, args); |
|
123 va_end (args); |
|
124 } |
|
125 |
|
126 void |
|
127 usage (const char *fmt, ...) |
|
128 { |
|
129 va_list args; |
|
130 va_start (args, fmt); |
905
|
131 error_state = -1; |
1
|
132 verror ("usage", fmt, args); |
|
133 va_end (args); |
|
134 } |
|
135 |
|
136 void |
|
137 warning (const char *fmt, ...) |
|
138 { |
|
139 va_list args; |
|
140 va_start (args, fmt); |
|
141 verror ("warning", fmt, args); |
|
142 va_end (args); |
|
143 } |
|
144 |
|
145 void |
|
146 error (const char *fmt, ...) |
|
147 { |
|
148 va_list args; |
|
149 va_start (args, fmt); |
1005
|
150 error_1 ("error", fmt, args); |
|
151 va_end (args); |
|
152 } |
436
|
153 |
1005
|
154 void |
|
155 parse_error (const char *fmt, ...) |
|
156 { |
|
157 va_list args; |
|
158 va_start (args, fmt); |
|
159 error_1 (0, fmt, args); |
1
|
160 va_end (args); |
|
161 } |
|
162 |
189
|
163 void |
1
|
164 panic (const char *fmt, ...) |
|
165 { |
169
|
166 flush_output_to_pager (); |
|
167 |
1
|
168 va_list args; |
|
169 va_start (args, fmt); |
|
170 verror ("panic", fmt, args); |
|
171 va_end (args); |
|
172 abort (); |
|
173 } |
|
174 |
1488
|
175 DEFUN ("error", Ferror, Serror, 10, |
528
|
176 "error (MESSAGE): print MESSAGE and set the error state.\n\ |
|
177 This should eventually take us up to the top level, possibly\n\ |
|
178 printing traceback messages as we go.\n\ |
|
179 \n\ |
|
180 If MESSAGE ends in a newline character, traceback messages are not\n\ |
|
181 printed.") |
|
182 { |
|
183 Octave_object retval; |
|
184 |
1363
|
185 const char *msg = "unspecified error"; |
528
|
186 |
|
187 int nargin = args.length (); |
|
188 |
712
|
189 if (nargin == 1 && args(0).is_defined ()) |
528
|
190 { |
712
|
191 if (args(0).is_string ()) |
528
|
192 { |
712
|
193 msg = args(0).string_value (); |
528
|
194 |
1266
|
195 if (! msg) |
528
|
196 return retval; |
|
197 } |
729
|
198 else if (args(0).is_empty ()) |
528
|
199 return retval; |
|
200 } |
|
201 |
|
202 error (msg); |
|
203 |
|
204 return retval; |
|
205 } |
|
206 |
1488
|
207 DEFUN ("warning", Fwarning, Swarning, 10, |
897
|
208 "warning (MESSAGE): print a warning MESSAGE.\n\ |
|
209 \n\ |
|
210 See also: error") |
|
211 { |
|
212 Octave_object retval; |
|
213 |
1363
|
214 const char *msg = "unspecified warning"; |
897
|
215 |
|
216 int nargin = args.length (); |
|
217 |
|
218 if (nargin == 1 && args(0).is_defined ()) |
|
219 { |
|
220 if (args(0).is_string ()) |
|
221 { |
|
222 msg = args(0).string_value (); |
|
223 |
|
224 if (! msg || ! *msg) |
|
225 return retval; |
|
226 } |
|
227 else if (args(0).is_empty ()) |
|
228 return retval; |
|
229 } |
|
230 |
|
231 warning (msg); |
|
232 |
|
233 return retval; |
|
234 } |
|
235 |
1488
|
236 DEFUN ("usage", Fusage, Susage, 10, |
899
|
237 "usage (MESSAGE): print a usage MESSAGE.\n\ |
|
238 \n\ |
|
239 See also: error") |
|
240 { |
|
241 Octave_object retval; |
|
242 |
1363
|
243 const char *msg = "unknown"; |
899
|
244 |
|
245 int nargin = args.length (); |
|
246 |
|
247 if (nargin == 1 && args(0).is_defined ()) |
|
248 { |
|
249 if (args(0).is_string ()) |
|
250 { |
|
251 msg = args(0).string_value (); |
|
252 |
|
253 if (! msg || ! *msg) |
|
254 return retval; |
|
255 } |
|
256 else if (args(0).is_empty ()) |
|
257 return retval; |
|
258 } |
|
259 |
|
260 usage (msg); |
|
261 |
|
262 return retval; |
|
263 } |
|
264 |
1
|
265 /* |
|
266 ;;; Local Variables: *** |
|
267 ;;; mode: C++ *** |
|
268 ;;; page-delimiter: "^/\\*" *** |
|
269 ;;; End: *** |
|
270 */ |