Mercurial > hg > octave-avbm
annotate src/toplev.cc @ 8658:73c4516fae10
New evaluator and debugger derived from tree-walker class
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 04 Feb 2009 00:47:53 -0500 |
parents | 6adcafc70c32 |
children | 33783e94fb16 |
rev | line source |
---|---|
1683 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, |
4 2004, 2005, 2006, 2007 John W. Eaton | |
1683 | 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 | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
1683 | 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 | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
1683 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
28 #include <cassert> | |
4489 | 29 #include <cerrno> |
1683 | 30 #include <cstdlib> |
31 #include <cstring> | |
4221 | 32 #include <new> |
1683 | 33 |
3503 | 34 #include <fstream> |
35 #include <iostream> | |
5765 | 36 #include <sstream> |
1728 | 37 #include <string> |
38 | |
1683 | 39 #ifdef HAVE_UNISTD_H |
2442 | 40 #ifdef HAVE_SYS_TYPES_H |
1683 | 41 #include <sys/types.h> |
2442 | 42 #endif |
1683 | 43 #include <unistd.h> |
44 #endif | |
45 | |
2926 | 46 #include "cmd-edit.h" |
47 #include "file-ops.h" | |
1683 | 48 #include "lo-error.h" |
2370 | 49 #include "lo-mappers.h" |
3020 | 50 #include "oct-env.h" |
4153 | 51 #include "quit.h" |
1755 | 52 #include "str-vec.h" |
8377
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
8347
diff
changeset
|
53 #include "oct-locbuf.h" |
1683 | 54 |
2492 | 55 #include <defaults.h> |
1683 | 56 #include "defun.h" |
57 #include "error.h" | |
58 #include "file-io.h" | |
59 #include "input.h" | |
60 #include "lex.h" | |
2492 | 61 #include <oct-conf.h> |
1742 | 62 #include "oct-hist.h" |
2162 | 63 #include "oct-map.h" |
2862 | 64 #include "oct-obj.h" |
1683 | 65 #include "pager.h" |
66 #include "parse.h" | |
67 #include "pathsearch.h" | |
68 #include "procstream.h" | |
2370 | 69 #include "ov.h" |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8581
diff
changeset
|
70 #include "pt-eval.h" |
2985 | 71 #include "pt-jump.h" |
2982 | 72 #include "pt-stmt.h" |
1683 | 73 #include "sighandlers.h" |
74 #include "sysdep.h" | |
2693 | 75 #include "syswait.h" |
1750 | 76 #include "toplev.h" |
1683 | 77 #include "unwind-prot.h" |
78 #include "utils.h" | |
79 #include "variables.h" | |
2492 | 80 #include <version.h> |
1683 | 81 |
3020 | 82 // TRUE means we are exiting via the builtin exit or quit functions. |
83 static bool quitting_gracefully = false; | |
1683 | 84 |
4217 | 85 // TRUE means we are ready to interpret commands, but not everything |
86 // is ready for interactive use. | |
87 bool octave_interpreter_ready = false; | |
88 | |
4172 | 89 // TRUE means we've processed all the init code and we are good to go. |
90 bool octave_initialized = false; | |
91 | |
1683 | 92 // Current command to execute. |
93 tree_statement_list *global_command = 0; | |
94 | |
5743 | 95 octave_call_stack *octave_call_stack::instance = 0; |
96 | |
7734
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7733
diff
changeset
|
97 int |
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7733
diff
changeset
|
98 octave_call_stack::do_current_line (void) const |
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7733
diff
changeset
|
99 { |
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7733
diff
changeset
|
100 tree_statement *stmt = do_top_statement (); |
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7733
diff
changeset
|
101 |
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7733
diff
changeset
|
102 return stmt ? stmt->line () : -1; |
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7733
diff
changeset
|
103 } |
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7733
diff
changeset
|
104 |
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7733
diff
changeset
|
105 int |
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7733
diff
changeset
|
106 octave_call_stack::do_current_column (void) const |
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7733
diff
changeset
|
107 { |
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7733
diff
changeset
|
108 tree_statement *stmt = do_top_statement (); |
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7733
diff
changeset
|
109 |
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7733
diff
changeset
|
110 return stmt ? stmt->column () : -1; |
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7733
diff
changeset
|
111 } |
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7733
diff
changeset
|
112 |
7877 | 113 int |
7923
c3d21b9b94b6
eliminate octave_call_stack member functions caller_user_script and caller_user_function, and unused difference_type args
John W. Eaton <jwe@octave.org>
parents:
7913
diff
changeset
|
114 octave_call_stack::do_caller_user_code_line (void) const |
7877 | 115 { |
116 int retval = -1; | |
117 | |
7890 | 118 const_iterator p = cs.end (); |
119 | |
120 while (p != cs.begin ()) | |
7877 | 121 { |
7890 | 122 const call_stack_elt& elt = *(--p); |
7877 | 123 |
124 octave_function *f = elt.fcn; | |
125 | |
126 if (f && f->is_user_code ()) | |
127 { | |
128 tree_statement *stmt = elt.stmt; | |
129 | |
130 if (stmt) | |
131 { | |
132 retval = stmt->line (); | |
133 break; | |
134 } | |
135 } | |
136 } | |
137 | |
138 return retval; | |
139 } | |
140 | |
141 int | |
7923
c3d21b9b94b6
eliminate octave_call_stack member functions caller_user_script and caller_user_function, and unused difference_type args
John W. Eaton <jwe@octave.org>
parents:
7913
diff
changeset
|
142 octave_call_stack::do_caller_user_code_column (void) const |
7877 | 143 { |
144 int retval = -1; | |
145 | |
7890 | 146 const_iterator p = cs.end (); |
147 | |
148 while (p != cs.begin ()) | |
7877 | 149 { |
7890 | 150 const call_stack_elt& elt = *(--p); |
7877 | 151 |
152 octave_function *f = elt.fcn; | |
153 | |
154 if (f && f->is_user_code ()) | |
155 { | |
156 tree_statement *stmt = elt.stmt; | |
157 | |
158 if (stmt) | |
159 { | |
160 retval = stmt->column (); | |
161 break; | |
162 } | |
163 } | |
164 } | |
165 | |
166 return retval; | |
167 } | |
168 | |
7901 | 169 size_t |
170 octave_call_stack::do_num_user_code_frames (octave_idx_type& curr_user_frame) const | |
171 { | |
172 size_t retval = 0; | |
173 | |
174 curr_user_frame = 0; | |
175 | |
176 // Look for the caller of dbstack. | |
177 size_t frame = cs[curr_frame].prev; | |
178 | |
179 bool found = false; | |
180 | |
181 size_t k = cs.size (); | |
182 | |
183 for (const_reverse_iterator p = cs.rbegin (); p != cs.rend (); p++) | |
184 { | |
185 octave_function *f = (*p).fcn; | |
186 | |
187 if (--k == frame) | |
188 found = true; | |
189 | |
190 if (f && f->is_user_code ()) | |
191 { | |
192 if (! found) | |
193 curr_user_frame++; | |
194 | |
195 retval++; | |
196 } | |
197 } | |
198 | |
199 // We counted how many user frames were not the one, in reverse. | |
200 // Now set curr_user_frame to be the index in the other direction. | |
201 curr_user_frame = retval - curr_user_frame - 1; | |
202 | |
203 return retval; | |
204 } | |
205 | |
7719
87eda1f8faaa
octave_user_code: new base class for octave_user_script and octave_user_function
John W. Eaton <jwe@octave.org>
parents:
7552
diff
changeset
|
206 octave_user_code * |
7923
c3d21b9b94b6
eliminate octave_call_stack member functions caller_user_script and caller_user_function, and unused difference_type args
John W. Eaton <jwe@octave.org>
parents:
7913
diff
changeset
|
207 octave_call_stack::do_caller_user_code (size_t nskip) const |
5744 | 208 { |
7719
87eda1f8faaa
octave_user_code: new base class for octave_user_script and octave_user_function
John W. Eaton <jwe@octave.org>
parents:
7552
diff
changeset
|
209 octave_user_code *retval = 0; |
5744 | 210 |
7890 | 211 const_iterator p = cs.end (); |
212 | |
213 while (p != cs.begin ()) | |
5744 | 214 { |
7890 | 215 const call_stack_elt& elt = *(--p); |
7734
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7733
diff
changeset
|
216 |
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7733
diff
changeset
|
217 octave_function *f = elt.fcn; |
5744 | 218 |
7719
87eda1f8faaa
octave_user_code: new base class for octave_user_script and octave_user_function
John W. Eaton <jwe@octave.org>
parents:
7552
diff
changeset
|
219 if (f && f->is_user_code ()) |
5744 | 220 { |
7923
c3d21b9b94b6
eliminate octave_call_stack member functions caller_user_script and caller_user_function, and unused difference_type args
John W. Eaton <jwe@octave.org>
parents:
7913
diff
changeset
|
221 if (nskip > 0) |
c3d21b9b94b6
eliminate octave_call_stack member functions caller_user_script and caller_user_function, and unused difference_type args
John W. Eaton <jwe@octave.org>
parents:
7913
diff
changeset
|
222 nskip--; |
c3d21b9b94b6
eliminate octave_call_stack member functions caller_user_script and caller_user_function, and unused difference_type args
John W. Eaton <jwe@octave.org>
parents:
7913
diff
changeset
|
223 else |
c3d21b9b94b6
eliminate octave_call_stack member functions caller_user_script and caller_user_function, and unused difference_type args
John W. Eaton <jwe@octave.org>
parents:
7913
diff
changeset
|
224 { |
c3d21b9b94b6
eliminate octave_call_stack member functions caller_user_script and caller_user_function, and unused difference_type args
John W. Eaton <jwe@octave.org>
parents:
7913
diff
changeset
|
225 retval = dynamic_cast<octave_user_code *> (f); |
c3d21b9b94b6
eliminate octave_call_stack member functions caller_user_script and caller_user_function, and unused difference_type args
John W. Eaton <jwe@octave.org>
parents:
7913
diff
changeset
|
226 break; |
c3d21b9b94b6
eliminate octave_call_stack member functions caller_user_script and caller_user_function, and unused difference_type args
John W. Eaton <jwe@octave.org>
parents:
7913
diff
changeset
|
227 } |
5744 | 228 } |
229 } | |
230 | |
231 return retval; | |
232 } | |
233 | |
7734
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7733
diff
changeset
|
234 Octave_map |
7901 | 235 octave_call_stack::do_backtrace (size_t nskip, |
236 octave_idx_type& curr_user_frame) const | |
7734
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7733
diff
changeset
|
237 { |
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7733
diff
changeset
|
238 Octave_map retval; |
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7733
diff
changeset
|
239 |
7901 | 240 size_t user_code_frames = do_num_user_code_frames (curr_user_frame); |
241 | |
242 size_t nframes = nskip <= user_code_frames ? user_code_frames - nskip : 0; | |
7734
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7733
diff
changeset
|
243 |
7901 | 244 // Our list is reversed. |
245 curr_user_frame = nframes - curr_user_frame - 1; | |
246 | |
247 Cell keys (6, 1); | |
7734
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7733
diff
changeset
|
248 |
7901 | 249 keys(0) = "file"; |
250 keys(1) = "name"; | |
251 keys(2) = "line"; | |
252 keys(3) = "column"; | |
253 keys(4) = "scope"; | |
254 keys(5) = "context"; | |
7890 | 255 |
7901 | 256 Cell file (nframes, 1); |
257 Cell name (nframes, 1); | |
258 Cell line (nframes, 1); | |
259 Cell column (nframes, 1); | |
260 Cell scope (nframes, 1); | |
261 Cell context (nframes, 1); | |
7734
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7733
diff
changeset
|
262 |
7901 | 263 if (nframes > 0) |
264 { | |
265 int k = 0; | |
266 | |
267 for (const_reverse_iterator p = cs.rbegin (); p != cs.rend (); p++) | |
7734
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7733
diff
changeset
|
268 { |
7901 | 269 const call_stack_elt& elt = *p; |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
270 |
7736 | 271 octave_function *f = elt.fcn; |
7734
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7733
diff
changeset
|
272 |
7901 | 273 if (f && f->is_user_code ()) |
7736 | 274 { |
7901 | 275 if (nskip > 0) |
276 nskip--; | |
7736 | 277 else |
278 { | |
7901 | 279 scope(k) = elt.scope; |
280 context(k) = elt.context; | |
281 | |
282 file(k) = f->fcn_file_name (); | |
283 std::string parent_fcn_name = f->parent_fcn_name (); | |
284 if (parent_fcn_name == std::string ()) | |
285 name(k) = f->name (); | |
286 else | |
287 name(k) = f->parent_fcn_name () + Vfilemarker + f->name (); | |
288 | |
289 tree_statement *stmt = elt.stmt; | |
290 | |
291 if (stmt) | |
292 { | |
293 line(k) = stmt->line (); | |
294 column(k) = stmt->column (); | |
295 } | |
296 else | |
297 { | |
298 line(k) = -1; | |
299 column(k) = -1; | |
300 } | |
301 | |
302 k++; | |
7736 | 303 } |
7734
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7733
diff
changeset
|
304 } |
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7733
diff
changeset
|
305 } |
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7733
diff
changeset
|
306 |
7736 | 307 retval.assign ("file", file); |
308 retval.assign ("name", name); | |
309 retval.assign ("line", line); | |
310 retval.assign ("column", column); | |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
311 retval.assign ("scope", scope); |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
312 retval.assign ("context", context); |
7734
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7733
diff
changeset
|
313 } |
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7733
diff
changeset
|
314 |
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7733
diff
changeset
|
315 return retval; |
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7733
diff
changeset
|
316 } |
2dee19385d32
eliminate tree_statement_stack; handle current statement info in octave_call_stack
John W. Eaton <jwe@octave.org>
parents:
7733
diff
changeset
|
317 |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
318 bool |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
319 octave_call_stack::do_goto_frame (size_t n, bool verbose) |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
320 { |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
321 bool retval = false; |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
322 |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
323 if (n < cs.size ()) |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
324 { |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
325 retval = true; |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
326 |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
327 curr_frame = n; |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
328 |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
329 const call_stack_elt& elt = cs[n]; |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
330 |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
331 symbol_table::set_scope_and_context (elt.scope, elt.context); |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
332 |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
333 if (verbose) |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
334 { |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
335 octave_function *f = elt.fcn; |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
336 std::string nm = f ? f->name () : std::string ("<unknown>"); |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
337 |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
338 tree_statement *s = elt.stmt; |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
339 int l = -1; |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
340 int c = -1; |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
341 if (s) |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
342 { |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
343 l = s->line (); |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
344 c = s->column (); |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
345 } |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
346 |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
347 octave_stdout << "stopped in " << nm |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
348 << " at line " << l << " column " << c |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
349 << " (" << elt.scope << "[" << elt.context << "])" |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
350 << std::endl; |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
351 } |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
352 } |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
353 |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
354 return retval; |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
355 } |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
356 |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
357 bool |
7901 | 358 octave_call_stack::do_goto_frame_relative (int nskip, bool verbose) |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
359 { |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
360 bool retval = false; |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
361 |
7901 | 362 if (nskip == 0) |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
363 retval = true; |
7890 | 364 else |
365 { | |
7901 | 366 int incr = nskip < 0 ? -1 : 1; |
367 | |
368 // Start looking with the caller of dbup/dbdown. | |
369 size_t frame = cs[curr_frame].prev; | |
370 | |
371 while (true) | |
372 { | |
373 if ((incr < 0 && frame == 0) || (incr > 0 && frame == cs.size () - 1)) | |
374 break; | |
375 | |
376 frame += incr; | |
377 | |
378 const call_stack_elt& elt = cs[frame]; | |
379 | |
380 octave_function *f = elt.fcn; | |
381 | |
382 if (f && f->is_user_code ()) | |
383 { | |
384 if (nskip > 0) | |
385 nskip--; | |
386 else if (nskip < 0) | |
387 nskip++; | |
388 | |
389 if (nskip == 0) | |
390 { | |
391 curr_frame = frame; | |
392 cs[cs.size () - 1].prev = curr_frame; | |
7890 | 393 |
7901 | 394 if (verbose) |
395 { | |
396 tree_statement *s = elt.stmt; | |
397 int l = -1; | |
398 int c = -1; | |
399 if (s) | |
400 { | |
401 l = s->line (); | |
402 c = s->column (); | |
403 } | |
404 | |
405 std::ostringstream buf; | |
406 buf << f->name () << ": " << " line " << l | |
407 << ", column " << c << std::endl; | |
408 | |
409 octave_stdout << buf.str (); | |
410 } | |
411 | |
412 retval = true; | |
413 break; | |
414 } | |
415 } | |
416 } | |
417 | |
418 // There is no need to set scope and context here. That will | |
419 // happen when the dbup/dbdown frame is popped and we jump to | |
420 // the new "prev" frame set above. | |
7890 | 421 } |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
422 |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
423 return retval; |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
424 } |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
425 |
5744 | 426 void |
7901 | 427 octave_call_stack::do_goto_caller_frame (void) |
428 { | |
429 size_t frame = curr_frame; | |
430 | |
431 bool skipped = false; | |
432 | |
433 while (frame != 0) | |
434 { | |
435 frame = cs[frame].prev; | |
436 | |
437 const call_stack_elt& elt = cs[frame]; | |
438 | |
439 octave_function *f = elt.fcn; | |
440 | |
8581
6adcafc70c32
toplev.cc (octave_call_stack::do_goto_caller_frame): allow caller frame to be base frame
John W. Eaton <jwe@octave.org>
parents:
8574
diff
changeset
|
441 if (frame == 0 || (f && f->is_user_code ())) |
7901 | 442 { |
443 if (! skipped) | |
444 // We found the current user code frame, so skip it. | |
445 skipped = true; | |
446 else | |
447 { | |
448 // We found the caller user code frame. | |
449 call_stack_elt tmp (elt); | |
450 tmp.prev = curr_frame; | |
451 | |
452 curr_frame = cs.size (); | |
453 | |
454 cs.push_back (tmp); | |
455 | |
456 symbol_table::set_scope_and_context (tmp.scope, tmp.context); | |
457 | |
458 break; | |
459 } | |
460 } | |
461 } | |
462 } | |
463 | |
464 void | |
465 octave_call_stack::do_goto_base_frame (void) | |
466 { | |
467 call_stack_elt tmp (cs[0]); | |
468 tmp.prev = curr_frame; | |
469 | |
470 curr_frame = cs.size (); | |
471 | |
472 cs.push_back (tmp); | |
473 | |
474 symbol_table::set_scope_and_context (tmp.scope, tmp.context); | |
475 } | |
476 | |
477 void | |
8013
b3e667f1ab4c
call backtrace_error_message in eval functions, not when popping stack
John W. Eaton <jwe@octave.org>
parents:
8011
diff
changeset
|
478 octave_call_stack::do_backtrace_error_message (void) const |
8011
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
479 { |
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
480 if (error_state > 0) |
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
481 { |
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
482 error_state = -1; |
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
483 |
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
484 error ("called from:"); |
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
485 } |
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
486 |
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
487 if (! cs.empty ()) |
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
488 { |
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
489 const call_stack_elt& elt = cs.back (); |
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
490 |
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
491 octave_function *fcn = elt.fcn; |
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
492 tree_statement *stmt = elt.stmt; |
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
493 |
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
494 std::string fcn_name = "?unknown?"; |
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
495 |
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
496 if (fcn) |
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
497 { |
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
498 fcn_name = fcn->fcn_file_name (); |
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
499 |
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
500 if (fcn_name.empty ()) |
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
501 fcn_name = fcn->name (); |
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
502 } |
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
503 |
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
504 int line = stmt ? stmt->line () : -1; |
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
505 int column = stmt ? stmt->column () : -1; |
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
506 |
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
507 error (" %s at line %d, column %d", |
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
508 fcn_name.c_str (), line, column); |
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
509 } |
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
510 } |
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
511 |
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
512 void |
4180 | 513 recover_from_exception (void) |
514 { | |
515 can_interrupt = true; | |
4182 | 516 octave_interrupt_immediately = 0; |
4180 | 517 octave_interrupt_state = 0; |
5142 | 518 octave_signal_caught = 0; |
7481
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7409
diff
changeset
|
519 octave_exception_state = octave_no_exception; |
4180 | 520 octave_restore_signal_mask (); |
521 octave_catch_interrupts (); | |
522 } | |
523 | |
1907 | 524 int |
5189 | 525 main_loop (void) |
1907 | 526 { |
2016 | 527 octave_save_signal_mask (); |
528 | |
3020 | 529 can_interrupt = true; |
1907 | 530 |
5142 | 531 octave_signal_hook = octave_signal_handler; |
4429 | 532 octave_interrupt_hook = unwind_protect::run_all; |
533 octave_bad_alloc_hook = unwind_protect::run_all; | |
534 | |
2554 | 535 octave_catch_interrupts (); |
1907 | 536 |
4172 | 537 octave_initialized = true; |
538 | |
1907 | 539 // The big loop. |
540 | |
4153 | 541 int retval = 0; |
1907 | 542 do |
543 { | |
4180 | 544 try |
1907 | 545 { |
7336 | 546 symbol_table::reset_scope (); |
2620 | 547 |
4318 | 548 reset_error_handler (); |
549 | |
4153 | 550 reset_parser (); |
2620 | 551 |
4753 | 552 // This is the same as yyparse in parse.y. |
553 retval = octave_parse (); | |
2620 | 554 |
4153 | 555 if (retval == 0) |
556 { | |
7913
f46e73bcb85b
toplev.cc (main_loop): undo previous change; input.cc (get_debug_input): don't delete global_command here
John W. Eaton <jwe@octave.org>
parents:
7903
diff
changeset
|
557 if (global_command) |
3804 | 558 { |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8581
diff
changeset
|
559 current_evaluator->reset (); |
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8581
diff
changeset
|
560 |
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8581
diff
changeset
|
561 global_command->accept (*current_evaluator); |
3883 | 562 |
7913
f46e73bcb85b
toplev.cc (main_loop): undo previous change; input.cc (get_debug_input): don't delete global_command here
John W. Eaton <jwe@octave.org>
parents:
7903
diff
changeset
|
563 delete global_command; |
3883 | 564 |
7913
f46e73bcb85b
toplev.cc (main_loop): undo previous change; input.cc (get_debug_input): don't delete global_command here
John W. Eaton <jwe@octave.org>
parents:
7903
diff
changeset
|
565 global_command = 0; |
3883 | 566 |
4171 | 567 OCTAVE_QUIT; |
568 | |
3883 | 569 if (! (interactive || forced_interactive)) |
570 { | |
4207 | 571 bool quit = (tree_return_command::returning |
572 || tree_break_command::breaking); | |
4153 | 573 |
4207 | 574 if (tree_return_command::returning) |
575 tree_return_command::returning = 0; | |
4153 | 576 |
4207 | 577 if (tree_break_command::breaking) |
578 tree_break_command::breaking--; | |
4153 | 579 |
580 if (quit) | |
581 break; | |
582 } | |
583 | |
584 if (error_state) | |
585 { | |
586 if (! (interactive || forced_interactive)) | |
587 { | |
588 // We should exit with a non-zero status. | |
589 retval = 1; | |
590 break; | |
591 } | |
592 } | |
593 else | |
594 { | |
595 if (octave_completion_matches_called) | |
596 octave_completion_matches_called = false; | |
597 else | |
598 command_editor::increment_current_command_number (); | |
3883 | 599 } |
600 } | |
4153 | 601 else if (parser_end_of_input) |
602 break; | |
2620 | 603 } |
4153 | 604 } |
4182 | 605 catch (octave_interrupt_exception) |
4153 | 606 { |
4180 | 607 recover_from_exception (); |
5629 | 608 octave_stdout << "\n"; |
4180 | 609 } |
4181 | 610 catch (std::bad_alloc) |
4180 | 611 { |
612 recover_from_exception (); | |
613 std::cerr | |
6680 | 614 << "error: memory exhausted or requested size too large for range of Octave's index type -- trying to return to prompt" |
615 << std::endl; | |
1907 | 616 } |
617 } | |
618 while (retval == 0); | |
619 | |
620 return retval; | |
621 } | |
622 | |
1683 | 623 // Fix up things before exiting. |
624 | |
625 void | |
3162 | 626 clean_up_and_exit (int retval) |
627 { | |
3216 | 628 do_octave_atexit (); |
1683 | 629 |
5451 | 630 sysdep_cleanup (); |
631 | |
3162 | 632 exit (retval == EOF ? 0 : retval); |
1683 | 633 } |
634 | |
3180 | 635 DEFUN (quit, args, nargout, |
3332 | 636 "-*- texinfo -*-\n\ |
637 @deftypefn {Built-in Function} {} exit (@var{status})\n\ | |
638 @deftypefnx {Built-in Function} {} quit (@var{status})\n\ | |
639 Exit the current Octave session. If the optional integer value\n\ | |
640 @var{status} is supplied, pass that value to the operating system as the\n\ | |
6615 | 641 Octave's exit status. The default value is zero.\n\ |
3333 | 642 @end deftypefn") |
1683 | 643 { |
2086 | 644 octave_value_list retval; |
2068 | 645 |
3180 | 646 if (nargout == 0) |
2068 | 647 { |
3180 | 648 int exit_status = 0; |
649 | |
650 quitting_gracefully = true; | |
1683 | 651 |
3180 | 652 if (args.length () > 0) |
653 { | |
3202 | 654 int tmp = args(0).nint_value (); |
1683 | 655 |
3202 | 656 if (! error_state) |
657 exit_status = tmp; | |
3180 | 658 } |
659 | |
660 clean_up_and_exit (exit_status); | |
2068 | 661 } |
3180 | 662 else |
663 error ("quit: invalid number of output arguments"); | |
2068 | 664 |
1683 | 665 return retval; |
666 } | |
667 | |
668 DEFALIAS (exit, quit); | |
669 | |
1957 | 670 DEFUN (warranty, , , |
3446 | 671 "-*- texinfo -*-\n\ |
672 @deftypefn {Built-in Function} {} warranty ()\n\ | |
673 Describe the conditions for copying and distributing Octave.\n\ | |
674 @end deftypefn") | |
1683 | 675 { |
2086 | 676 octave_value_list retval; |
1683 | 677 |
3922 | 678 octave_stdout << "\n" \ |
679 OCTAVE_NAME_VERSION_AND_COPYRIGHT "\n\ | |
680 \n\ | |
1683 | 681 This program is free software; you can redistribute it and/or modify\n\ |
682 it under the terms of the GNU General Public License as published by\n\ | |
7016 | 683 the Free Software Foundation; either version 3 of the License, or\n\ |
1683 | 684 (at your option) any later version.\n\ |
685 \n\ | |
686 This program is distributed in the hope that it will be useful,\n\ | |
687 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\ | |
688 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\ | |
689 GNU General Public License for more details.\n\ | |
690 \n\ | |
691 You should have received a copy of the GNU General Public License\n\ | |
7016 | 692 along with this program. If not, see <http://www.gnu.org/licenses/>.\n\ |
1683 | 693 \n"; |
694 | |
695 return retval; | |
696 } | |
697 | |
698 // Execute a shell command. | |
699 | |
1965 | 700 static void |
701 cleanup_iprocstream (void *p) | |
702 { | |
3060 | 703 iprocstream *cmd = static_cast<iprocstream *> (p); |
704 | |
705 octave_child_list::remove (cmd->pid ()); | |
706 | |
707 delete cmd; | |
1965 | 708 } |
709 | |
6316 | 710 static int |
711 wait_for_input (int fid) | |
712 { | |
713 int retval = -1; | |
714 | |
715 #if defined (HAVE_SELECT) | |
716 if (fid >= 0) | |
717 { | |
718 fd_set set; | |
719 | |
720 FD_ZERO (&set); | |
721 FD_SET (fid, &set); | |
722 | |
723 retval = select (FD_SETSIZE, &set, 0, 0, 0); | |
724 } | |
725 #else | |
726 retval = 1; | |
727 #endif | |
728 | |
729 return retval; | |
730 } | |
731 | |
2086 | 732 static octave_value_list |
3523 | 733 run_command_and_return_output (const std::string& cmd_str) |
2083 | 734 { |
2086 | 735 octave_value_list retval; |
2083 | 736 |
737 iprocstream *cmd = new iprocstream (cmd_str.c_str ()); | |
738 | |
3060 | 739 if (cmd) |
2083 | 740 { |
3060 | 741 unwind_protect::add (cleanup_iprocstream, cmd); |
742 | |
743 if (*cmd) | |
744 { | |
6316 | 745 int fid = cmd->file_number (); |
2095 | 746 |
6316 | 747 std::ostringstream output_buf; |
4494 | 748 |
3060 | 749 char ch; |
3147 | 750 |
4489 | 751 for (;;) |
3147 | 752 { |
4489 | 753 if (cmd->get (ch)) |
754 output_buf.put (ch); | |
755 else | |
756 { | |
757 if (! cmd->eof () && errno == EAGAIN) | |
758 { | |
759 cmd->clear (); | |
3147 | 760 |
6316 | 761 if (wait_for_input (fid) != 1) |
762 break; | |
4489 | 763 } |
764 else | |
765 break; | |
766 } | |
3147 | 767 } |
768 | |
3252 | 769 int cmd_status = cmd->close (); |
2083 | 770 |
3060 | 771 if (WIFEXITED (cmd_status)) |
772 cmd_status = WEXITSTATUS (cmd_status); | |
773 else | |
774 cmd_status = 127; | |
2083 | 775 |
5659 | 776 retval(0) = (double) cmd_status; |
5765 | 777 retval(1) = output_buf.str (); |
3060 | 778 } |
779 | |
780 unwind_protect::run (); | |
2083 | 781 } |
782 else | |
783 error ("unable to start subprocess for `%s'", cmd_str.c_str ()); | |
784 | |
785 return retval; | |
786 } | |
787 | |
5285 | 788 enum system_exec_type { et_sync, et_async }; |
789 | |
1957 | 790 DEFUN (system, args, nargout, |
3301 | 791 "-*- texinfo -*-\n\ |
792 @deftypefn {Built-in Function} {} system (@var{string}, @var{return_output}, @var{type})\n\ | |
793 Execute a shell command specified by @var{string}. The second\n\ | |
794 argument is optional. If @var{type} is @code{\"async\"}, the process\n\ | |
795 is started in the background and the process id of the child process\n\ | |
796 is returned immediately. Otherwise, the process is started, and\n\ | |
8347
fa78cb8d8a5c
corrections for typos
Brian Gough<bjg@network-theory.co.uk>
parents:
8013
diff
changeset
|
797 Octave waits until it exits. If the @var{type} argument is omitted, a\n\ |
3301 | 798 value of @code{\"sync\"} is assumed.\n\ |
2083 | 799 \n\ |
3301 | 800 If two input arguments are given (the actual value of\n\ |
801 @var{return_output} is irrelevant) and the subprocess is started\n\ | |
802 synchronously, or if @var{system} is called with one input argument and\n\ | |
803 one or more output arguments, the output from the command is returned.\n\ | |
5097 | 804 Otherwise, if the subprocess is executed synchronously, its output is\n\ |
2321 | 805 sent to the standard output. To send the output of a command executed\n\ |
3301 | 806 with @var{system} through the pager, use a command like\n\ |
2321 | 807 \n\ |
3301 | 808 @example\n\ |
809 disp (system (cmd, 1));\n\ | |
810 @end example\n\ | |
2321 | 811 \n\ |
3301 | 812 @noindent\n\ |
2321 | 813 or\n\ |
814 \n\ | |
3301 | 815 @example\n\ |
816 printf (\"%s\n\", system (cmd, 1));\n\ | |
817 @end example\n\ | |
818 \n\ | |
5659 | 819 The @code{system} function can return two values. The first is the\n\ |
820 exit status of the command and the second is any output from the\n\ | |
821 command that was written to the standard output stream. For example,\n\ | |
3301 | 822 \n\ |
823 @example\n\ | |
5717 | 824 [status, output] = system (\"echo foo; exit 2\");\n\ |
3301 | 825 @end example\n\ |
826 \n\ | |
827 @noindent\n\ | |
828 will set the variable @code{output} to the string @samp{foo}, and the\n\ | |
829 variable @code{status} to the integer @samp{2}.\n\ | |
830 @end deftypefn") | |
1683 | 831 { |
2086 | 832 octave_value_list retval; |
1683 | 833 |
3834 | 834 unwind_protect::begin_frame ("Fsystem"); |
835 | |
1683 | 836 int nargin = args.length (); |
837 | |
2083 | 838 if (nargin > 0 && nargin < 4) |
1683 | 839 { |
5672 | 840 bool return_output = (nargout > 1 || nargin > 1); |
2083 | 841 |
3523 | 842 std::string cmd_str = args(0).string_value (); |
2083 | 843 |
5285 | 844 system_exec_type type = et_sync; |
2083 | 845 |
846 if (! error_state) | |
847 { | |
848 if (nargin > 2) | |
849 { | |
3523 | 850 std::string type_str = args(2).string_value (); |
1683 | 851 |
2083 | 852 if (! error_state) |
853 { | |
854 if (type_str == "sync") | |
5285 | 855 type = et_sync; |
2083 | 856 else if (type_str == "async") |
5285 | 857 type = et_async; |
2083 | 858 else |
859 error ("system: third arg must be \"sync\" or \"async\""); | |
860 } | |
861 else | |
862 error ("system: third argument must be a string"); | |
863 } | |
864 } | |
865 else | |
3523 | 866 error ("system: expecting std::string as first argument"); |
1683 | 867 |
2083 | 868 if (! error_state) |
869 { | |
7104 | 870 #if defined (__WIN32__) && ! defined (__CYGWIN__) |
871 // Work around weird double-quote handling on Windows systems. | |
872 if (type == et_sync) | |
873 cmd_str = "\"" + cmd_str + "\""; | |
874 #endif | |
875 | |
5285 | 876 if (type == et_async) |
2083 | 877 { |
6222 | 878 // FIXME -- maybe this should go in sysdep.cc? |
4086 | 879 #ifdef HAVE_FORK |
2083 | 880 pid_t pid = fork (); |
881 | |
882 if (pid < 0) | |
883 error ("system: fork failed -- can't create child process"); | |
884 else if (pid == 0) | |
885 { | |
5775 | 886 // FIXME -- should probably replace this |
3273 | 887 // call with something portable. |
888 | |
5510 | 889 execl ("/bin/sh", "sh", "-c", cmd_str.c_str (), |
890 static_cast<void *> (0)); | |
3273 | 891 |
892 panic_impossible (); | |
2083 | 893 } |
894 else | |
4254 | 895 retval(0) = pid; |
6222 | 896 #elif defined (__WIN32__) |
897 STARTUPINFO si; | |
898 PROCESS_INFORMATION pi; | |
899 ZeroMemory (&si, sizeof (si)); | |
900 ZeroMemory (&pi, sizeof (pi)); | |
901 OCTAVE_LOCAL_BUFFER (char, xcmd_str, cmd_str.length()+1); | |
6676 | 902 strcpy (xcmd_str, cmd_str.c_str ()); |
6222 | 903 |
904 if (! CreateProcess (0, xcmd_str, 0, 0, FALSE, 0, 0, 0, &si, &pi)) | |
905 error ("system: CreateProcess failed -- can't create child process"); | |
906 else | |
907 { | |
908 retval(0) = pi.dwProcessId; | |
909 CloseHandle (pi.hProcess); | |
910 CloseHandle (pi.hThread); | |
911 } | |
4086 | 912 #else |
913 error ("asynchronous system calls are not supported"); | |
914 #endif | |
2083 | 915 } |
2321 | 916 else if (return_output) |
917 retval = run_command_and_return_output (cmd_str); | |
2083 | 918 else |
2321 | 919 { |
920 int status = system (cmd_str.c_str ()); | |
921 | |
922 // The value in status is as returned by waitpid. If | |
923 // the process exited normally, extract the actual exit | |
924 // status of the command. Otherwise, return 127 as a | |
925 // failure code. | |
926 | |
2693 | 927 if (WIFEXITED (status)) |
928 status = WEXITSTATUS (status); | |
2321 | 929 |
4233 | 930 retval(0) = status; |
2321 | 931 } |
2083 | 932 } |
1683 | 933 } |
934 else | |
5823 | 935 print_usage (); |
1683 | 936 |
3834 | 937 unwind_protect::run_frame ("Fsystem"); |
938 | |
1683 | 939 return retval; |
940 } | |
941 | |
942 DEFALIAS (shell_cmd, system); | |
943 | |
5775 | 944 // FIXME -- this should really be static, but that causes |
2614 | 945 // problems on some systems. |
6680 | 946 std::list<std::string> octave_atexit_functions; |
2077 | 947 |
948 void | |
949 do_octave_atexit (void) | |
950 { | |
3216 | 951 static bool deja_vu = false; |
952 | |
2077 | 953 while (! octave_atexit_functions.empty ()) |
954 { | |
6680 | 955 std::string fcn = octave_atexit_functions.front (); |
4214 | 956 |
6680 | 957 octave_atexit_functions.pop_front (); |
2077 | 958 |
5237 | 959 reset_error_handler (); |
960 | |
4233 | 961 feval (fcn, octave_value_list (), 0); |
3215 | 962 |
963 flush_octave_stdout (); | |
2077 | 964 } |
3216 | 965 |
966 if (! deja_vu) | |
967 { | |
968 deja_vu = true; | |
969 | |
6068 | 970 // Do this explicitly so that destructors for mex file objects |
971 // are called, so that functions registered with mexAtExit are | |
972 // called. | |
6072 | 973 clear_mex_functions (); |
6068 | 974 |
3216 | 975 command_editor::restore_terminal_state (); |
976 | |
5775 | 977 // FIXME -- is this needed? Can it cause any trouble? |
3216 | 978 raw_mode (0); |
979 | |
5305 | 980 octave_history_write_timestamp (); |
981 | |
3216 | 982 command_history::clean_up_and_save (); |
983 | |
984 close_files (); | |
985 | |
986 cleanup_tmp_files (); | |
987 | |
988 flush_octave_stdout (); | |
989 | |
5305 | 990 if (! quitting_gracefully && (interactive || forced_interactive)) |
5629 | 991 { |
992 octave_stdout << "\n"; | |
993 | |
994 // Yes, we want this to be separate from the call to | |
995 // flush_octave_stdout above. | |
996 | |
997 flush_octave_stdout (); | |
998 } | |
3216 | 999 } |
2077 | 1000 } |
1001 | |
7409 | 1002 void |
1003 octave_add_atexit_function (const std::string& fname) | |
1004 { | |
1005 octave_atexit_functions.push_front (fname); | |
1006 } | |
1007 | |
1008 bool | |
1009 octave_remove_atexit_function (const std::string& fname) | |
1010 { | |
1011 bool found = false; | |
1012 | |
1013 for (std::list<std::string>::iterator p = octave_atexit_functions.begin (); | |
1014 p != octave_atexit_functions.end (); p++) | |
1015 { | |
1016 if (*p == fname) | |
1017 { | |
1018 octave_atexit_functions.erase (p); | |
1019 found = true; | |
1020 break; | |
1021 } | |
1022 } | |
1023 | |
1024 return found; | |
1025 } | |
1026 | |
1027 | |
6680 | 1028 DEFUN (atexit, args, nargout, |
3332 | 1029 "-*- texinfo -*-\n\ |
1030 @deftypefn {Built-in Function} {} atexit (@var{fcn})\n\ | |
1031 Register a function to be called when Octave exits. For example,\n\ | |
2077 | 1032 \n\ |
3332 | 1033 @example\n\ |
1034 @group\n\ | |
6620 | 1035 function bye_bye ()\n\ |
1036 disp (\"Bye bye\");\n\ | |
3332 | 1037 endfunction\n\ |
6620 | 1038 atexit (\"bye_bye\");\n\ |
3332 | 1039 @end group\n\ |
1040 @end example\n\ | |
1041 \n\ | |
1042 @noindent\n\ | |
6620 | 1043 will print the message \"Bye bye\" when Octave exits.\n\ |
6680 | 1044 \n\ |
1045 @deftypefnx {Built-in Function} {} atexit (@var{fcn}, @var{flag})\n\ | |
1046 Register or unregister a function to be called when Octave exits,\n\ | |
1047 depending on @var{flag}. If @var{flag} is true, the function is\n\ | |
1048 registered, if @var{flag} is false, it is unregistered. For example,\n\ | |
1049 after registering the function @code{bye_bye} as above,\n\ | |
1050 \n\ | |
1051 @example\n\ | |
1052 atexit (\"bye_bye\", false);\n\ | |
1053 @end example\n\ | |
1054 \n\ | |
1055 @noindent\n\ | |
1056 will remove the function from the list and Octave will not call\n\ | |
1057 the function @code{bye_by} when it exits.\n\ | |
1058 \n\ | |
7001 | 1059 Note that @code{atexit} only removes the first occurrence of a function\n\ |
6680 | 1060 from the list, so if a function was placed in the list multiple\n\ |
1061 times with @code{atexit}, it must also be removed from the list\n\ | |
1062 multiple times.\n\ | |
3333 | 1063 @end deftypefn") |
2077 | 1064 { |
2086 | 1065 octave_value_list retval; |
2077 | 1066 |
1067 int nargin = args.length (); | |
1068 | |
6680 | 1069 if (nargin == 1 || nargin == 2) |
2077 | 1070 { |
3523 | 1071 std::string arg = args(0).string_value (); |
2077 | 1072 |
1073 if (! error_state) | |
6680 | 1074 { |
1075 bool add_mode = true; | |
1076 | |
1077 if (nargin == 2) | |
1078 { | |
1079 add_mode = args(1).bool_value (); | |
1080 | |
1081 if (error_state) | |
1082 error ("atexit: second argument must be a logical value"); | |
1083 } | |
1084 | |
1085 if (! error_state) | |
1086 { | |
1087 if (add_mode) | |
7409 | 1088 octave_add_atexit_function (arg); |
6680 | 1089 else |
1090 { | |
7409 | 1091 bool found = octave_remove_atexit_function (arg); |
6680 | 1092 |
1093 if (nargout > 0) | |
1094 retval(0) = found; | |
1095 } | |
1096 } | |
1097 } | |
2077 | 1098 else |
6680 | 1099 error ("atexit: argument must be a string"); |
2077 | 1100 } |
1101 else | |
5823 | 1102 print_usage (); |
2077 | 1103 |
1104 return retval; | |
1105 } | |
1106 | |
2689 | 1107 DEFUN (octave_config_info, args, , |
3301 | 1108 "-*- texinfo -*-\n\ |
1109 @deftypefn {Built-in Function} {} octave_config_info (@var{option})\n\ | |
1110 Return a structure containing configuration and installation\n\ | |
1111 information for Octave.\n\ | |
2689 | 1112 \n\ |
3301 | 1113 if @var{option} is a string, return the configuration information for the\n\ |
2689 | 1114 specified option.\n\ |
1115 \n\ | |
3301 | 1116 @end deftypefn") |
2162 | 1117 { |
2689 | 1118 octave_value retval; |
1119 | |
4128 | 1120 #if defined (ENABLE_DYNAMIC_LINKING) |
2689 | 1121 bool octave_supports_dynamic_linking = true; |
1122 #else | |
1123 bool octave_supports_dynamic_linking = false; | |
1124 #endif | |
1125 | |
4357 | 1126 static bool initialized = false; |
1127 static Octave_map m; | |
2162 | 1128 |
6274 | 1129 struct conf_info_struct |
1130 { | |
1131 bool subst_home; | |
1132 const char *key; | |
1133 const char *val; | |
1134 }; | |
1135 | |
1136 static const conf_info_struct conf_info[] = | |
4357 | 1137 { |
6274 | 1138 { false, "ALL_CFLAGS", OCTAVE_CONF_ALL_CFLAGS }, |
1139 { false, "ALL_CXXFLAGS", OCTAVE_CONF_ALL_CXXFLAGS }, | |
1140 { false, "ALL_FFLAGS", OCTAVE_CONF_ALL_FFLAGS }, | |
1141 { false, "ALL_LDFLAGS", OCTAVE_CONF_ALL_LDFLAGS }, | |
1142 { false, "AR", OCTAVE_CONF_AR }, | |
1143 { false, "ARFLAGS", OCTAVE_CONF_ARFLAGS }, | |
1144 { false, "BLAS_LIBS", OCTAVE_CONF_BLAS_LIBS }, | |
1145 { false, "CC", OCTAVE_CONF_CC }, | |
1146 { false, "CC_VERSION", OCTAVE_CONF_CC_VERSION }, | |
1147 { false, "CFLAGS", OCTAVE_CONF_CFLAGS }, | |
1148 { false, "CPICFLAG", OCTAVE_CONF_CPICFLAG }, | |
1149 { false, "CPPFLAGS", OCTAVE_CONF_CPPFLAGS }, | |
1150 { false, "CURL_LIBS", OCTAVE_CONF_CURL_LIBS }, | |
1151 { false, "CXX", OCTAVE_CONF_CXX }, | |
1152 { false, "CXXCPP", OCTAVE_CONF_CXXCPP }, | |
1153 { false, "CXXFLAGS", OCTAVE_CONF_CXXFLAGS }, | |
1154 { false, "CXXPICFLAG", OCTAVE_CONF_CXXPICFLAG }, | |
1155 { false, "CXX_VERSION", OCTAVE_CONF_CXX_VERSION }, | |
1156 { false, "DEFAULT_PAGER", OCTAVE_DEFAULT_PAGER }, | |
1157 { false, "DEFS", OCTAVE_CONF_DEFS }, | |
1158 { false, "DL_LD", OCTAVE_CONF_DL_LD }, | |
1159 { false, "DL_LDFLAGS", OCTAVE_CONF_DL_LDFLAGS }, | |
1160 { false, "ENABLE_DYNAMIC_LINKING", OCTAVE_CONF_ENABLE_DYNAMIC_LINKING }, | |
1161 { false, "EXEEXT", OCTAVE_CONF_EXEEXT }, | |
1162 { false, "F2C", OCTAVE_CONF_F2C }, | |
1163 { false, "F2CFLAGS", OCTAVE_CONF_F2CFLAGS }, | |
1164 { false, "F77", OCTAVE_CONF_F77 }, | |
1165 { false, "F77_FLOAT_STORE_FLAG", OCTAVE_CONF_F77_FLOAT_STORE_FLAG }, | |
1166 { false, "FC", OCTAVE_CONF_FC }, | |
1167 { false, "FFLAGS", OCTAVE_CONF_FFLAGS }, | |
1168 { false, "FFTW_LIBS", OCTAVE_CONF_FFTW_LIBS }, | |
1169 { false, "FLIBS", OCTAVE_CONF_FLIBS }, | |
1170 { false, "FPICFLAG", OCTAVE_CONF_FPICFLAG }, | |
1171 { false, "GLPK_LIBS", OCTAVE_CONF_GLPK_LIBS }, | |
7361 | 1172 { false, "GNUPLOT", OCTAVE_CONF_GNUPLOT }, |
6274 | 1173 { false, "INCFLAGS", OCTAVE_CONF_INCFLAGS }, |
1174 { false, "LDFLAGS", OCTAVE_CONF_LDFLAGS }, | |
1175 { false, "LD_CXX", OCTAVE_CONF_LD_CXX }, | |
1176 { false, "LD_STATIC_FLAG", OCTAVE_CONF_LD_STATIC_FLAG }, | |
1177 { false, "LEX", OCTAVE_CONF_LEX }, | |
1178 { false, "LEXLIB", OCTAVE_CONF_LEXLIB }, | |
1179 { false, "LFLAGS", OCTAVE_CONF_LFLAGS }, | |
1180 { false, "LIBCRUFT", OCTAVE_CONF_LIBCRUFT }, | |
1181 { false, "LIBEXT", OCTAVE_CONF_LIBEXT }, | |
1182 { false, "LIBFLAGS", OCTAVE_CONF_LIBFLAGS }, | |
1183 { false, "LIBOCTAVE", OCTAVE_CONF_LIBOCTAVE }, | |
1184 { false, "LIBOCTINTERP", OCTAVE_CONF_LIBOCTINTERP }, | |
1185 { false, "LIBREADLINE", OCTAVE_CONF_LIBREADLINE }, | |
1186 { false, "LIBS", OCTAVE_CONF_LIBS }, | |
1187 { false, "LN_S", OCTAVE_CONF_LN_S }, | |
7926
d74f996e005d
__magick_read__.cc: configuration and style fixes
John W. Eaton <jwe@octave.org>
parents:
7923
diff
changeset
|
1188 { false, "MAGICK_INCFLAGS", OCTAVE_CONF_MAGICK_INCFLAGS }, |
d74f996e005d
__magick_read__.cc: configuration and style fixes
John W. Eaton <jwe@octave.org>
parents:
7923
diff
changeset
|
1189 { false, "MAGICK_LIBS", OCTAVE_CONF_MAGICK_LIBS }, |
6274 | 1190 { false, "MKOCTFILE_DL_LDFLAGS", OCTAVE_CONF_MKOCTFILE_DL_LDFLAGS }, |
1191 { false, "RANLIB", OCTAVE_CONF_RANLIB }, | |
1192 { false, "RDYNAMIC_FLAG", OCTAVE_CONF_RDYNAMIC_FLAG }, | |
1193 { false, "RLD_FLAG", OCTAVE_CONF_RLD_FLAG }, | |
1194 { false, "SED", OCTAVE_CONF_SED }, | |
1195 { false, "SHARED_LIBS", OCTAVE_CONF_SHARED_LIBS }, | |
1196 { false, "SHLEXT", OCTAVE_CONF_SHLEXT }, | |
1197 { false, "SHLEXT_VER", OCTAVE_CONF_SHLEXT_VER }, | |
1198 { false, "SH_LD", OCTAVE_CONF_SH_LD }, | |
1199 { false, "SH_LDFLAGS", OCTAVE_CONF_SH_LDFLAGS }, | |
1200 { false, "SONAME_FLAGS", OCTAVE_CONF_SONAME_FLAGS }, | |
1201 { false, "STATIC_LIBS", OCTAVE_CONF_STATIC_LIBS }, | |
1202 { false, "UGLY_DEFS", OCTAVE_CONF_UGLY_DEFS }, | |
1203 { false, "USE_64_BIT_IDX_T", OCTAVE_CONF_USE_64_BIT_IDX_T }, | |
1204 { false, "XTRA_CFLAGS", OCTAVE_CONF_XTRA_CFLAGS }, | |
1205 { false, "XTRA_CXXFLAGS", OCTAVE_CONF_XTRA_CXXFLAGS }, | |
1206 { false, "YACC", OCTAVE_CONF_YACC }, | |
1207 { false, "YFLAGS", OCTAVE_CONF_YFLAGS }, | |
1208 { false, "api_version", OCTAVE_API_VERSION }, | |
1209 { true, "archlibdir", OCTAVE_ARCHLIBDIR }, | |
1210 { true, "bindir", OCTAVE_BINDIR }, | |
1211 { false, "canonical_host_type", OCTAVE_CANONICAL_HOST_TYPE }, | |
1212 { false, "config_opts", OCTAVE_CONF_config_opts }, | |
1213 { true, "datadir", OCTAVE_DATADIR }, | |
1214 { true, "datarootdir", OCTAVE_DATAROOTDIR }, | |
6276 | 1215 { true, "exec_prefix", OCTAVE_EXEC_PREFIX }, |
6274 | 1216 { true, "fcnfiledir", OCTAVE_FCNFILEDIR }, |
1217 { true, "imagedir", OCTAVE_IMAGEDIR }, | |
1218 { true, "includedir", OCTAVE_INCLUDEDIR }, | |
1219 { true, "infodir", OCTAVE_INFODIR }, | |
6276 | 1220 { true, "infofile", OCTAVE_INFOFILE }, |
6274 | 1221 { true, "libdir", OCTAVE_LIBDIR }, |
1222 { true, "libexecdir", OCTAVE_LIBEXECDIR }, | |
6311 | 1223 { true, "localapiarchlibdir", OCTAVE_LOCALAPIARCHLIBDIR }, |
6274 | 1224 { true, "localapifcnfiledir", OCTAVE_LOCALAPIFCNFILEDIR }, |
1225 { true, "localapioctfiledir", OCTAVE_LOCALAPIOCTFILEDIR }, | |
1226 { true, "localarchlibdir", OCTAVE_LOCALARCHLIBDIR }, | |
1227 { true, "localfcnfiledir", OCTAVE_LOCALFCNFILEDIR }, | |
1228 { true, "localoctfiledir", OCTAVE_LOCALOCTFILEDIR }, | |
1229 { true, "localstartupfiledir", OCTAVE_LOCALSTARTUPFILEDIR }, | |
1230 { true, "localverarchlibdir", OCTAVE_LOCALVERARCHLIBDIR }, | |
1231 { true, "localverfcnfiledir", OCTAVE_LOCALVERFCNFILEDIR }, | |
1232 { true, "localveroctfiledir", OCTAVE_LOCALVEROCTFILEDIR }, | |
1233 { true, "man1dir", OCTAVE_MAN1DIR }, | |
1234 { false, "man1ext", OCTAVE_MAN1EXT }, | |
1235 { true, "mandir", OCTAVE_MANDIR }, | |
1236 { true, "octfiledir", OCTAVE_OCTFILEDIR }, | |
1237 { true, "octincludedir", OCTAVE_OCTINCLUDEDIR }, | |
1238 { true, "octlibdir", OCTAVE_OCTLIBDIR }, | |
6276 | 1239 { true, "prefix", OCTAVE_PREFIX }, |
6274 | 1240 { true, "startupfiledir", OCTAVE_STARTUPFILEDIR }, |
1241 { false, "version", OCTAVE_VERSION }, | |
1242 { false, 0, 0 } | |
4357 | 1243 }; |
1244 | |
1245 if (! initialized) | |
1246 { | |
4675 | 1247 m.assign ("dld", octave_value (octave_supports_dynamic_linking)); |
4357 | 1248 |
4697 | 1249 oct_mach_info::float_format ff = oct_mach_info::native_float_format (); |
1250 m.assign ("float_format", | |
1251 octave_value (oct_mach_info::float_format_as_string (ff))); | |
1252 | |
1253 m.assign ("words_big_endian", | |
1254 octave_value (oct_mach_info::words_big_endian ())); | |
1255 | |
1256 m.assign ("words_little_endian", | |
1257 octave_value (oct_mach_info::words_little_endian ())); | |
1258 | |
4357 | 1259 int i = 0; |
4440 | 1260 |
1261 while (true) | |
4357 | 1262 { |
6274 | 1263 const conf_info_struct& elt = conf_info[i++]; |
1264 | |
1265 const char *key = elt.key; | |
4357 | 1266 |
1267 if (key) | |
6274 | 1268 { |
1269 if (elt.subst_home) | |
1270 m.assign (key, octave_value (subst_octave_home (elt.val))); | |
1271 else | |
1272 m.assign (key, octave_value (elt.val)); | |
1273 } | |
4357 | 1274 else |
1275 break; | |
1276 } | |
1277 | |
4691 | 1278 bool unix_system = true; |
7013 | 1279 bool mac_system = false; |
4691 | 1280 bool windows_system = false; |
1281 | |
1282 #if defined (WIN32) | |
1283 windows_system = true; | |
1284 #if !defined (__CYGWIN__) | |
1285 unix_system = false; | |
1286 #endif | |
1287 #endif | |
1288 | |
8574
83b8c739d626
toplev.cc: check OCTAVE_USE_OS_X_API instead of __APPLE__ && __MACH__
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
1289 #if defined (OCTAVE_USE_OS_X_API) |
7010 | 1290 mac_system = true; |
1291 #endif | |
1292 | |
4691 | 1293 m.assign ("unix", octave_value (unix_system)); |
7010 | 1294 m.assign ("mac", octave_value (mac_system)); |
4691 | 1295 m.assign ("windows", octave_value (windows_system)); |
1296 | |
4357 | 1297 initialized = true; |
1298 } | |
2162 | 1299 |
2689 | 1300 int nargin = args.length (); |
1301 | |
1302 if (nargin == 1) | |
1303 { | |
3523 | 1304 std::string arg = args(0).string_value (); |
2689 | 1305 |
1306 if (! error_state) | |
4675 | 1307 { |
1308 Cell c = m.contents (arg.c_str ()); | |
5199 | 1309 |
1310 if (c.is_empty ()) | |
1311 error ("octave_config_info: no info for `%s'", arg.c_str ()); | |
1312 else | |
1313 retval = c(0); | |
4675 | 1314 } |
2689 | 1315 } |
1316 else if (nargin == 0) | |
4233 | 1317 retval = m; |
2689 | 1318 else |
5823 | 1319 print_usage (); |
2689 | 1320 |
1321 return retval; | |
2162 | 1322 } |
1323 | |
1683 | 1324 #if defined (__GNUG__) && defined (DEBUG_NEW_DELETE) |
2806 | 1325 |
1683 | 1326 int debug_new_delete = 0; |
1327 | |
1328 typedef void (*vfp)(void); | |
1329 extern vfp __new_handler; | |
1330 | |
1331 void * | |
1332 __builtin_new (size_t sz) | |
1333 { | |
1334 void *p; | |
1335 | |
1336 /* malloc (0) is unpredictable; avoid it. */ | |
1337 if (sz == 0) | |
1338 sz = 1; | |
2800 | 1339 p = malloc (sz); |
1683 | 1340 while (p == 0) |
1341 { | |
1342 (*__new_handler) (); | |
2800 | 1343 p = malloc (sz); |
1683 | 1344 } |
1345 | |
1346 if (debug_new_delete) | |
5629 | 1347 std::cerr << "__builtin_new: " << p << std::endl; |
1683 | 1348 |
1349 return p; | |
1350 } | |
1351 | |
1352 void | |
1353 __builtin_delete (void *ptr) | |
1354 { | |
1355 if (debug_new_delete) | |
5629 | 1356 std::cerr << "__builtin_delete: " << ptr << std::endl; |
1683 | 1357 |
1358 if (ptr) | |
1359 free (ptr); | |
1360 } | |
2806 | 1361 |
1683 | 1362 #endif |
1363 | |
1364 /* | |
1365 ;;; Local Variables: *** | |
1366 ;;; mode: C++ *** | |
1367 ;;; End: *** | |
1368 */ |