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 |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
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 |
1005
|
68 static void |
|
69 error_1 (const char *name, const char *fmt, va_list args) |
|
70 { |
|
71 if (error_state != -2) |
|
72 { |
|
73 if (! error_state) |
|
74 error_state = 1; |
|
75 |
|
76 if (! suppress_octave_error_messages) |
|
77 { |
|
78 int len = 0; |
|
79 if (fmt && *fmt && fmt[(len = strlen (fmt)) - 1] == '\n') |
|
80 { |
|
81 error_state = -2; |
|
82 char *tmp_fmt = strsave (fmt); |
|
83 tmp_fmt[len - 1] = '\0'; |
|
84 verror (name, tmp_fmt, args); |
|
85 delete [] tmp_fmt; |
|
86 } |
|
87 else |
|
88 verror (name, fmt, args); |
|
89 } |
|
90 } |
|
91 } |
|
92 |
1
|
93 void |
|
94 message (const char *name, const char *fmt, ...) |
|
95 { |
|
96 va_list args; |
|
97 va_start (args, fmt); |
|
98 verror (name, fmt, args); |
|
99 va_end (args); |
|
100 } |
|
101 |
|
102 void |
|
103 usage (const char *fmt, ...) |
|
104 { |
|
105 va_list args; |
|
106 va_start (args, fmt); |
905
|
107 error_state = -1; |
1
|
108 verror ("usage", fmt, args); |
|
109 va_end (args); |
|
110 } |
|
111 |
|
112 void |
|
113 warning (const char *fmt, ...) |
|
114 { |
|
115 va_list args; |
|
116 va_start (args, fmt); |
|
117 verror ("warning", fmt, args); |
|
118 va_end (args); |
|
119 } |
|
120 |
|
121 void |
|
122 error (const char *fmt, ...) |
|
123 { |
|
124 va_list args; |
|
125 va_start (args, fmt); |
1005
|
126 error_1 ("error", fmt, args); |
|
127 va_end (args); |
|
128 } |
436
|
129 |
1005
|
130 void |
|
131 parse_error (const char *fmt, ...) |
|
132 { |
|
133 va_list args; |
|
134 va_start (args, fmt); |
|
135 error_1 (0, fmt, args); |
1
|
136 va_end (args); |
|
137 } |
|
138 |
189
|
139 void |
1
|
140 panic (const char *fmt, ...) |
|
141 { |
169
|
142 flush_output_to_pager (); |
|
143 |
1
|
144 va_list args; |
|
145 va_start (args, fmt); |
|
146 verror ("panic", fmt, args); |
|
147 va_end (args); |
|
148 abort (); |
|
149 } |
|
150 |
712
|
151 DEFUN ("error", Ferror, Serror, 1, 1, |
528
|
152 "error (MESSAGE): print MESSAGE and set the error state.\n\ |
|
153 This should eventually take us up to the top level, possibly\n\ |
|
154 printing traceback messages as we go.\n\ |
|
155 \n\ |
|
156 If MESSAGE ends in a newline character, traceback messages are not\n\ |
|
157 printed.") |
|
158 { |
|
159 Octave_object retval; |
|
160 |
899
|
161 char *msg = "unspecified error"; |
528
|
162 |
|
163 int nargin = args.length (); |
|
164 |
712
|
165 if (nargin == 1 && args(0).is_defined ()) |
528
|
166 { |
712
|
167 if (args(0).is_string ()) |
528
|
168 { |
712
|
169 msg = args(0).string_value (); |
528
|
170 |
|
171 if (! msg || ! *msg) |
|
172 return retval; |
|
173 } |
729
|
174 else if (args(0).is_empty ()) |
528
|
175 return retval; |
|
176 } |
|
177 |
|
178 error (msg); |
|
179 |
|
180 return retval; |
|
181 } |
|
182 |
897
|
183 DEFUN ("warning", Fwarning, Swarning, 1, 1, |
|
184 "warning (MESSAGE): print a warning MESSAGE.\n\ |
|
185 \n\ |
|
186 See also: error") |
|
187 { |
|
188 Octave_object retval; |
|
189 |
899
|
190 char *msg = "unspecified warning"; |
897
|
191 |
|
192 int nargin = args.length (); |
|
193 |
|
194 if (nargin == 1 && args(0).is_defined ()) |
|
195 { |
|
196 if (args(0).is_string ()) |
|
197 { |
|
198 msg = args(0).string_value (); |
|
199 |
|
200 if (! msg || ! *msg) |
|
201 return retval; |
|
202 } |
|
203 else if (args(0).is_empty ()) |
|
204 return retval; |
|
205 } |
|
206 |
|
207 warning (msg); |
|
208 |
|
209 return retval; |
|
210 } |
|
211 |
899
|
212 DEFUN ("usage", Fusage, Susage, 1, 1, |
|
213 "usage (MESSAGE): print a usage MESSAGE.\n\ |
|
214 \n\ |
|
215 See also: error") |
|
216 { |
|
217 Octave_object retval; |
|
218 |
|
219 char *msg = "unknown"; |
|
220 |
|
221 int nargin = args.length (); |
|
222 |
|
223 if (nargin == 1 && args(0).is_defined ()) |
|
224 { |
|
225 if (args(0).is_string ()) |
|
226 { |
|
227 msg = args(0).string_value (); |
|
228 |
|
229 if (! msg || ! *msg) |
|
230 return retval; |
|
231 } |
|
232 else if (args(0).is_empty ()) |
|
233 return retval; |
|
234 } |
|
235 |
|
236 usage (msg); |
|
237 |
|
238 return retval; |
|
239 } |
|
240 |
1
|
241 /* |
|
242 ;;; Local Variables: *** |
|
243 ;;; mode: C++ *** |
|
244 ;;; page-delimiter: "^/\\*" *** |
|
245 ;;; End: *** |
|
246 */ |