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