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