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