1
|
1 // error.cc -*- C++ -*- |
|
2 /* |
|
3 |
436
|
4 Copyright (C) 1992, 1993, 1994 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 |
|
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 { |
600
|
47 if (name) |
|
48 cerr << name << ": "; |
581
|
49 cerr.vform (fmt, args); |
|
50 cerr << endl; |
|
51 |
|
52 ostrstream output_buf; |
1
|
53 |
599
|
54 if (name) |
|
55 output_buf << name << ": "; |
581
|
56 output_buf.vform (fmt, args); |
|
57 output_buf << endl; |
|
58 |
|
59 char *msg = output_buf.str (); |
|
60 |
|
61 maybe_write_to_diary_file (msg); |
|
62 |
|
63 delete [] msg; |
1
|
64 } |
|
65 |
|
66 void |
|
67 message (const char *name, const char *fmt, ...) |
|
68 { |
|
69 va_list args; |
|
70 va_start (args, fmt); |
|
71 verror (name, fmt, args); |
|
72 va_end (args); |
|
73 } |
|
74 |
|
75 void |
|
76 usage (const char *fmt, ...) |
|
77 { |
|
78 va_list args; |
|
79 va_start (args, fmt); |
|
80 verror ("usage", fmt, args); |
|
81 va_end (args); |
|
82 } |
|
83 |
|
84 void |
|
85 warning (const char *fmt, ...) |
|
86 { |
|
87 va_list args; |
|
88 va_start (args, fmt); |
|
89 verror ("warning", fmt, args); |
|
90 va_end (args); |
|
91 } |
|
92 |
|
93 void |
|
94 error (const char *fmt, ...) |
|
95 { |
436
|
96 if (error_state == -2) |
|
97 return; |
|
98 |
143
|
99 if (! error_state) |
|
100 error_state = 1; |
|
101 |
672
|
102 if (suppress_octave_error_messages) |
|
103 return; |
|
104 |
169
|
105 flush_output_to_pager (); |
|
106 |
1
|
107 va_list args; |
|
108 va_start (args, fmt); |
436
|
109 |
|
110 int len; |
439
|
111 if (fmt && *fmt && fmt[(len = strlen (fmt)) - 1] == '\n') |
436
|
112 { |
|
113 error_state = -2; |
|
114 char *tmp_fmt = strsave (fmt); |
|
115 tmp_fmt[len - 1] = '\0'; |
|
116 verror ("error", tmp_fmt, args); |
439
|
117 delete [] tmp_fmt; |
436
|
118 } |
|
119 else |
|
120 verror ("error", fmt, args); |
|
121 |
1
|
122 va_end (args); |
|
123 } |
|
124 |
189
|
125 void |
1
|
126 panic (const char *fmt, ...) |
|
127 { |
169
|
128 flush_output_to_pager (); |
|
129 |
1
|
130 va_list args; |
|
131 va_start (args, fmt); |
|
132 verror ("panic", fmt, args); |
|
133 va_end (args); |
|
134 abort (); |
|
135 } |
|
136 |
712
|
137 DEFUN ("error", Ferror, Serror, 1, 1, |
528
|
138 "error (MESSAGE): print MESSAGE and set the error state.\n\ |
|
139 This should eventually take us up to the top level, possibly\n\ |
|
140 printing traceback messages as we go.\n\ |
|
141 \n\ |
|
142 If MESSAGE ends in a newline character, traceback messages are not\n\ |
|
143 printed.") |
|
144 { |
|
145 Octave_object retval; |
|
146 |
|
147 char *msg = "unspecified_error"; |
|
148 |
|
149 int nargin = args.length (); |
|
150 |
712
|
151 if (nargin == 1 && args(0).is_defined ()) |
528
|
152 { |
712
|
153 if (args(0).is_string ()) |
528
|
154 { |
712
|
155 msg = args(0).string_value (); |
528
|
156 |
|
157 if (! msg || ! *msg) |
|
158 return retval; |
|
159 } |
729
|
160 else if (args(0).is_empty ()) |
528
|
161 return retval; |
|
162 } |
|
163 |
|
164 error (msg); |
|
165 |
|
166 return retval; |
|
167 } |
|
168 |
1
|
169 /* |
|
170 ;;; Local Variables: *** |
|
171 ;;; mode: C++ *** |
|
172 ;;; page-delimiter: "^/\\*" *** |
|
173 ;;; End: *** |
|
174 */ |