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