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