Mercurial > octave-nkf
annotate src/toplev.cc @ 8669:33783e94fb16
line number fixes and other evaluator tweaks
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 04 Feb 2009 13:28:06 -0500 |
parents | 73c4516fae10 |
children | 236ff50db90f |
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 global_command->accept (*current_evaluator); |
3883 | 560 |
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
|
561 delete global_command; |
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 global_command = 0; |
3883 | 564 |
4171 | 565 OCTAVE_QUIT; |
566 | |
3883 | 567 if (! (interactive || forced_interactive)) |
568 { | |
4207 | 569 bool quit = (tree_return_command::returning |
570 || tree_break_command::breaking); | |
4153 | 571 |
4207 | 572 if (tree_return_command::returning) |
573 tree_return_command::returning = 0; | |
4153 | 574 |
4207 | 575 if (tree_break_command::breaking) |
576 tree_break_command::breaking--; | |
4153 | 577 |
578 if (quit) | |
579 break; | |
580 } | |
581 | |
582 if (error_state) | |
583 { | |
584 if (! (interactive || forced_interactive)) | |
585 { | |
586 // We should exit with a non-zero status. | |
587 retval = 1; | |
588 break; | |
589 } | |
590 } | |
591 else | |
592 { | |
593 if (octave_completion_matches_called) | |
594 octave_completion_matches_called = false; | |
595 else | |
596 command_editor::increment_current_command_number (); | |
3883 | 597 } |
598 } | |
4153 | 599 else if (parser_end_of_input) |
600 break; | |
2620 | 601 } |
4153 | 602 } |
4182 | 603 catch (octave_interrupt_exception) |
4153 | 604 { |
4180 | 605 recover_from_exception (); |
5629 | 606 octave_stdout << "\n"; |
4180 | 607 } |
4181 | 608 catch (std::bad_alloc) |
4180 | 609 { |
610 recover_from_exception (); | |
611 std::cerr | |
6680 | 612 << "error: memory exhausted or requested size too large for range of Octave's index type -- trying to return to prompt" |
613 << std::endl; | |
1907 | 614 } |
615 } | |
616 while (retval == 0); | |
617 | |
618 return retval; | |
619 } | |
620 | |
1683 | 621 // Fix up things before exiting. |
622 | |
623 void | |
3162 | 624 clean_up_and_exit (int retval) |
625 { | |
3216 | 626 do_octave_atexit (); |
1683 | 627 |
5451 | 628 sysdep_cleanup (); |
629 | |
3162 | 630 exit (retval == EOF ? 0 : retval); |
1683 | 631 } |
632 | |
3180 | 633 DEFUN (quit, args, nargout, |
3332 | 634 "-*- texinfo -*-\n\ |
635 @deftypefn {Built-in Function} {} exit (@var{status})\n\ | |
636 @deftypefnx {Built-in Function} {} quit (@var{status})\n\ | |
637 Exit the current Octave session. If the optional integer value\n\ | |
638 @var{status} is supplied, pass that value to the operating system as the\n\ | |
6615 | 639 Octave's exit status. The default value is zero.\n\ |
3333 | 640 @end deftypefn") |
1683 | 641 { |
2086 | 642 octave_value_list retval; |
2068 | 643 |
3180 | 644 if (nargout == 0) |
2068 | 645 { |
3180 | 646 int exit_status = 0; |
647 | |
648 quitting_gracefully = true; | |
1683 | 649 |
3180 | 650 if (args.length () > 0) |
651 { | |
3202 | 652 int tmp = args(0).nint_value (); |
1683 | 653 |
3202 | 654 if (! error_state) |
655 exit_status = tmp; | |
3180 | 656 } |
657 | |
658 clean_up_and_exit (exit_status); | |
2068 | 659 } |
3180 | 660 else |
661 error ("quit: invalid number of output arguments"); | |
2068 | 662 |
1683 | 663 return retval; |
664 } | |
665 | |
666 DEFALIAS (exit, quit); | |
667 | |
1957 | 668 DEFUN (warranty, , , |
3446 | 669 "-*- texinfo -*-\n\ |
670 @deftypefn {Built-in Function} {} warranty ()\n\ | |
671 Describe the conditions for copying and distributing Octave.\n\ | |
672 @end deftypefn") | |
1683 | 673 { |
2086 | 674 octave_value_list retval; |
1683 | 675 |
3922 | 676 octave_stdout << "\n" \ |
677 OCTAVE_NAME_VERSION_AND_COPYRIGHT "\n\ | |
678 \n\ | |
1683 | 679 This program is free software; you can redistribute it and/or modify\n\ |
680 it under the terms of the GNU General Public License as published by\n\ | |
7016 | 681 the Free Software Foundation; either version 3 of the License, or\n\ |
1683 | 682 (at your option) any later version.\n\ |
683 \n\ | |
684 This program is distributed in the hope that it will be useful,\n\ | |
685 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\ | |
686 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\ | |
687 GNU General Public License for more details.\n\ | |
688 \n\ | |
689 You should have received a copy of the GNU General Public License\n\ | |
7016 | 690 along with this program. If not, see <http://www.gnu.org/licenses/>.\n\ |
1683 | 691 \n"; |
692 | |
693 return retval; | |
694 } | |
695 | |
696 // Execute a shell command. | |
697 | |
1965 | 698 static void |
699 cleanup_iprocstream (void *p) | |
700 { | |
3060 | 701 iprocstream *cmd = static_cast<iprocstream *> (p); |
702 | |
703 octave_child_list::remove (cmd->pid ()); | |
704 | |
705 delete cmd; | |
1965 | 706 } |
707 | |
6316 | 708 static int |
709 wait_for_input (int fid) | |
710 { | |
711 int retval = -1; | |
712 | |
713 #if defined (HAVE_SELECT) | |
714 if (fid >= 0) | |
715 { | |
716 fd_set set; | |
717 | |
718 FD_ZERO (&set); | |
719 FD_SET (fid, &set); | |
720 | |
721 retval = select (FD_SETSIZE, &set, 0, 0, 0); | |
722 } | |
723 #else | |
724 retval = 1; | |
725 #endif | |
726 | |
727 return retval; | |
728 } | |
729 | |
2086 | 730 static octave_value_list |
3523 | 731 run_command_and_return_output (const std::string& cmd_str) |
2083 | 732 { |
2086 | 733 octave_value_list retval; |
2083 | 734 |
735 iprocstream *cmd = new iprocstream (cmd_str.c_str ()); | |
736 | |
3060 | 737 if (cmd) |
2083 | 738 { |
3060 | 739 unwind_protect::add (cleanup_iprocstream, cmd); |
740 | |
741 if (*cmd) | |
742 { | |
6316 | 743 int fid = cmd->file_number (); |
2095 | 744 |
6316 | 745 std::ostringstream output_buf; |
4494 | 746 |
3060 | 747 char ch; |
3147 | 748 |
4489 | 749 for (;;) |
3147 | 750 { |
4489 | 751 if (cmd->get (ch)) |
752 output_buf.put (ch); | |
753 else | |
754 { | |
755 if (! cmd->eof () && errno == EAGAIN) | |
756 { | |
757 cmd->clear (); | |
3147 | 758 |
6316 | 759 if (wait_for_input (fid) != 1) |
760 break; | |
4489 | 761 } |
762 else | |
763 break; | |
764 } | |
3147 | 765 } |
766 | |
3252 | 767 int cmd_status = cmd->close (); |
2083 | 768 |
3060 | 769 if (WIFEXITED (cmd_status)) |
770 cmd_status = WEXITSTATUS (cmd_status); | |
771 else | |
772 cmd_status = 127; | |
2083 | 773 |
5659 | 774 retval(0) = (double) cmd_status; |
5765 | 775 retval(1) = output_buf.str (); |
3060 | 776 } |
777 | |
778 unwind_protect::run (); | |
2083 | 779 } |
780 else | |
781 error ("unable to start subprocess for `%s'", cmd_str.c_str ()); | |
782 | |
783 return retval; | |
784 } | |
785 | |
5285 | 786 enum system_exec_type { et_sync, et_async }; |
787 | |
1957 | 788 DEFUN (system, args, nargout, |
3301 | 789 "-*- texinfo -*-\n\ |
790 @deftypefn {Built-in Function} {} system (@var{string}, @var{return_output}, @var{type})\n\ | |
791 Execute a shell command specified by @var{string}. The second\n\ | |
792 argument is optional. If @var{type} is @code{\"async\"}, the process\n\ | |
793 is started in the background and the process id of the child process\n\ | |
794 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
|
795 Octave waits until it exits. If the @var{type} argument is omitted, a\n\ |
3301 | 796 value of @code{\"sync\"} is assumed.\n\ |
2083 | 797 \n\ |
3301 | 798 If two input arguments are given (the actual value of\n\ |
799 @var{return_output} is irrelevant) and the subprocess is started\n\ | |
800 synchronously, or if @var{system} is called with one input argument and\n\ | |
801 one or more output arguments, the output from the command is returned.\n\ | |
5097 | 802 Otherwise, if the subprocess is executed synchronously, its output is\n\ |
2321 | 803 sent to the standard output. To send the output of a command executed\n\ |
3301 | 804 with @var{system} through the pager, use a command like\n\ |
2321 | 805 \n\ |
3301 | 806 @example\n\ |
807 disp (system (cmd, 1));\n\ | |
808 @end example\n\ | |
2321 | 809 \n\ |
3301 | 810 @noindent\n\ |
2321 | 811 or\n\ |
812 \n\ | |
3301 | 813 @example\n\ |
814 printf (\"%s\n\", system (cmd, 1));\n\ | |
815 @end example\n\ | |
816 \n\ | |
5659 | 817 The @code{system} function can return two values. The first is the\n\ |
818 exit status of the command and the second is any output from the\n\ | |
819 command that was written to the standard output stream. For example,\n\ | |
3301 | 820 \n\ |
821 @example\n\ | |
5717 | 822 [status, output] = system (\"echo foo; exit 2\");\n\ |
3301 | 823 @end example\n\ |
824 \n\ | |
825 @noindent\n\ | |
826 will set the variable @code{output} to the string @samp{foo}, and the\n\ | |
827 variable @code{status} to the integer @samp{2}.\n\ | |
828 @end deftypefn") | |
1683 | 829 { |
2086 | 830 octave_value_list retval; |
1683 | 831 |
3834 | 832 unwind_protect::begin_frame ("Fsystem"); |
833 | |
1683 | 834 int nargin = args.length (); |
835 | |
2083 | 836 if (nargin > 0 && nargin < 4) |
1683 | 837 { |
5672 | 838 bool return_output = (nargout > 1 || nargin > 1); |
2083 | 839 |
3523 | 840 std::string cmd_str = args(0).string_value (); |
2083 | 841 |
5285 | 842 system_exec_type type = et_sync; |
2083 | 843 |
844 if (! error_state) | |
845 { | |
846 if (nargin > 2) | |
847 { | |
3523 | 848 std::string type_str = args(2).string_value (); |
1683 | 849 |
2083 | 850 if (! error_state) |
851 { | |
852 if (type_str == "sync") | |
5285 | 853 type = et_sync; |
2083 | 854 else if (type_str == "async") |
5285 | 855 type = et_async; |
2083 | 856 else |
857 error ("system: third arg must be \"sync\" or \"async\""); | |
858 } | |
859 else | |
860 error ("system: third argument must be a string"); | |
861 } | |
862 } | |
863 else | |
3523 | 864 error ("system: expecting std::string as first argument"); |
1683 | 865 |
2083 | 866 if (! error_state) |
867 { | |
7104 | 868 #if defined (__WIN32__) && ! defined (__CYGWIN__) |
869 // Work around weird double-quote handling on Windows systems. | |
870 if (type == et_sync) | |
871 cmd_str = "\"" + cmd_str + "\""; | |
872 #endif | |
873 | |
5285 | 874 if (type == et_async) |
2083 | 875 { |
6222 | 876 // FIXME -- maybe this should go in sysdep.cc? |
4086 | 877 #ifdef HAVE_FORK |
2083 | 878 pid_t pid = fork (); |
879 | |
880 if (pid < 0) | |
881 error ("system: fork failed -- can't create child process"); | |
882 else if (pid == 0) | |
883 { | |
5775 | 884 // FIXME -- should probably replace this |
3273 | 885 // call with something portable. |
886 | |
5510 | 887 execl ("/bin/sh", "sh", "-c", cmd_str.c_str (), |
888 static_cast<void *> (0)); | |
3273 | 889 |
890 panic_impossible (); | |
2083 | 891 } |
892 else | |
4254 | 893 retval(0) = pid; |
6222 | 894 #elif defined (__WIN32__) |
895 STARTUPINFO si; | |
896 PROCESS_INFORMATION pi; | |
897 ZeroMemory (&si, sizeof (si)); | |
898 ZeroMemory (&pi, sizeof (pi)); | |
899 OCTAVE_LOCAL_BUFFER (char, xcmd_str, cmd_str.length()+1); | |
6676 | 900 strcpy (xcmd_str, cmd_str.c_str ()); |
6222 | 901 |
902 if (! CreateProcess (0, xcmd_str, 0, 0, FALSE, 0, 0, 0, &si, &pi)) | |
903 error ("system: CreateProcess failed -- can't create child process"); | |
904 else | |
905 { | |
906 retval(0) = pi.dwProcessId; | |
907 CloseHandle (pi.hProcess); | |
908 CloseHandle (pi.hThread); | |
909 } | |
4086 | 910 #else |
911 error ("asynchronous system calls are not supported"); | |
912 #endif | |
2083 | 913 } |
2321 | 914 else if (return_output) |
915 retval = run_command_and_return_output (cmd_str); | |
2083 | 916 else |
2321 | 917 { |
918 int status = system (cmd_str.c_str ()); | |
919 | |
920 // The value in status is as returned by waitpid. If | |
921 // the process exited normally, extract the actual exit | |
922 // status of the command. Otherwise, return 127 as a | |
923 // failure code. | |
924 | |
2693 | 925 if (WIFEXITED (status)) |
926 status = WEXITSTATUS (status); | |
2321 | 927 |
4233 | 928 retval(0) = status; |
2321 | 929 } |
2083 | 930 } |
1683 | 931 } |
932 else | |
5823 | 933 print_usage (); |
1683 | 934 |
3834 | 935 unwind_protect::run_frame ("Fsystem"); |
936 | |
1683 | 937 return retval; |
938 } | |
939 | |
940 DEFALIAS (shell_cmd, system); | |
941 | |
5775 | 942 // FIXME -- this should really be static, but that causes |
2614 | 943 // problems on some systems. |
6680 | 944 std::list<std::string> octave_atexit_functions; |
2077 | 945 |
946 void | |
947 do_octave_atexit (void) | |
948 { | |
3216 | 949 static bool deja_vu = false; |
950 | |
2077 | 951 while (! octave_atexit_functions.empty ()) |
952 { | |
6680 | 953 std::string fcn = octave_atexit_functions.front (); |
4214 | 954 |
6680 | 955 octave_atexit_functions.pop_front (); |
2077 | 956 |
5237 | 957 reset_error_handler (); |
958 | |
4233 | 959 feval (fcn, octave_value_list (), 0); |
3215 | 960 |
961 flush_octave_stdout (); | |
2077 | 962 } |
3216 | 963 |
964 if (! deja_vu) | |
965 { | |
966 deja_vu = true; | |
967 | |
6068 | 968 // Do this explicitly so that destructors for mex file objects |
969 // are called, so that functions registered with mexAtExit are | |
970 // called. | |
6072 | 971 clear_mex_functions (); |
6068 | 972 |
3216 | 973 command_editor::restore_terminal_state (); |
974 | |
5775 | 975 // FIXME -- is this needed? Can it cause any trouble? |
3216 | 976 raw_mode (0); |
977 | |
5305 | 978 octave_history_write_timestamp (); |
979 | |
3216 | 980 command_history::clean_up_and_save (); |
981 | |
982 close_files (); | |
983 | |
984 cleanup_tmp_files (); | |
985 | |
986 flush_octave_stdout (); | |
987 | |
5305 | 988 if (! quitting_gracefully && (interactive || forced_interactive)) |
5629 | 989 { |
990 octave_stdout << "\n"; | |
991 | |
992 // Yes, we want this to be separate from the call to | |
993 // flush_octave_stdout above. | |
994 | |
995 flush_octave_stdout (); | |
996 } | |
3216 | 997 } |
2077 | 998 } |
999 | |
7409 | 1000 void |
1001 octave_add_atexit_function (const std::string& fname) | |
1002 { | |
1003 octave_atexit_functions.push_front (fname); | |
1004 } | |
1005 | |
1006 bool | |
1007 octave_remove_atexit_function (const std::string& fname) | |
1008 { | |
1009 bool found = false; | |
1010 | |
1011 for (std::list<std::string>::iterator p = octave_atexit_functions.begin (); | |
1012 p != octave_atexit_functions.end (); p++) | |
1013 { | |
1014 if (*p == fname) | |
1015 { | |
1016 octave_atexit_functions.erase (p); | |
1017 found = true; | |
1018 break; | |
1019 } | |
1020 } | |
1021 | |
1022 return found; | |
1023 } | |
1024 | |
1025 | |
6680 | 1026 DEFUN (atexit, args, nargout, |
3332 | 1027 "-*- texinfo -*-\n\ |
1028 @deftypefn {Built-in Function} {} atexit (@var{fcn})\n\ | |
1029 Register a function to be called when Octave exits. For example,\n\ | |
2077 | 1030 \n\ |
3332 | 1031 @example\n\ |
1032 @group\n\ | |
6620 | 1033 function bye_bye ()\n\ |
1034 disp (\"Bye bye\");\n\ | |
3332 | 1035 endfunction\n\ |
6620 | 1036 atexit (\"bye_bye\");\n\ |
3332 | 1037 @end group\n\ |
1038 @end example\n\ | |
1039 \n\ | |
1040 @noindent\n\ | |
6620 | 1041 will print the message \"Bye bye\" when Octave exits.\n\ |
6680 | 1042 \n\ |
1043 @deftypefnx {Built-in Function} {} atexit (@var{fcn}, @var{flag})\n\ | |
1044 Register or unregister a function to be called when Octave exits,\n\ | |
1045 depending on @var{flag}. If @var{flag} is true, the function is\n\ | |
1046 registered, if @var{flag} is false, it is unregistered. For example,\n\ | |
1047 after registering the function @code{bye_bye} as above,\n\ | |
1048 \n\ | |
1049 @example\n\ | |
1050 atexit (\"bye_bye\", false);\n\ | |
1051 @end example\n\ | |
1052 \n\ | |
1053 @noindent\n\ | |
1054 will remove the function from the list and Octave will not call\n\ | |
1055 the function @code{bye_by} when it exits.\n\ | |
1056 \n\ | |
7001 | 1057 Note that @code{atexit} only removes the first occurrence of a function\n\ |
6680 | 1058 from the list, so if a function was placed in the list multiple\n\ |
1059 times with @code{atexit}, it must also be removed from the list\n\ | |
1060 multiple times.\n\ | |
3333 | 1061 @end deftypefn") |
2077 | 1062 { |
2086 | 1063 octave_value_list retval; |
2077 | 1064 |
1065 int nargin = args.length (); | |
1066 | |
6680 | 1067 if (nargin == 1 || nargin == 2) |
2077 | 1068 { |
3523 | 1069 std::string arg = args(0).string_value (); |
2077 | 1070 |
1071 if (! error_state) | |
6680 | 1072 { |
1073 bool add_mode = true; | |
1074 | |
1075 if (nargin == 2) | |
1076 { | |
1077 add_mode = args(1).bool_value (); | |
1078 | |
1079 if (error_state) | |
1080 error ("atexit: second argument must be a logical value"); | |
1081 } | |
1082 | |
1083 if (! error_state) | |
1084 { | |
1085 if (add_mode) | |
7409 | 1086 octave_add_atexit_function (arg); |
6680 | 1087 else |
1088 { | |
7409 | 1089 bool found = octave_remove_atexit_function (arg); |
6680 | 1090 |
1091 if (nargout > 0) | |
1092 retval(0) = found; | |
1093 } | |
1094 } | |
1095 } | |
2077 | 1096 else |
6680 | 1097 error ("atexit: argument must be a string"); |
2077 | 1098 } |
1099 else | |
5823 | 1100 print_usage (); |
2077 | 1101 |
1102 return retval; | |
1103 } | |
1104 | |
2689 | 1105 DEFUN (octave_config_info, args, , |
3301 | 1106 "-*- texinfo -*-\n\ |
1107 @deftypefn {Built-in Function} {} octave_config_info (@var{option})\n\ | |
1108 Return a structure containing configuration and installation\n\ | |
1109 information for Octave.\n\ | |
2689 | 1110 \n\ |
3301 | 1111 if @var{option} is a string, return the configuration information for the\n\ |
2689 | 1112 specified option.\n\ |
1113 \n\ | |
3301 | 1114 @end deftypefn") |
2162 | 1115 { |
2689 | 1116 octave_value retval; |
1117 | |
4128 | 1118 #if defined (ENABLE_DYNAMIC_LINKING) |
2689 | 1119 bool octave_supports_dynamic_linking = true; |
1120 #else | |
1121 bool octave_supports_dynamic_linking = false; | |
1122 #endif | |
1123 | |
4357 | 1124 static bool initialized = false; |
1125 static Octave_map m; | |
2162 | 1126 |
6274 | 1127 struct conf_info_struct |
1128 { | |
1129 bool subst_home; | |
1130 const char *key; | |
1131 const char *val; | |
1132 }; | |
1133 | |
1134 static const conf_info_struct conf_info[] = | |
4357 | 1135 { |
6274 | 1136 { false, "ALL_CFLAGS", OCTAVE_CONF_ALL_CFLAGS }, |
1137 { false, "ALL_CXXFLAGS", OCTAVE_CONF_ALL_CXXFLAGS }, | |
1138 { false, "ALL_FFLAGS", OCTAVE_CONF_ALL_FFLAGS }, | |
1139 { false, "ALL_LDFLAGS", OCTAVE_CONF_ALL_LDFLAGS }, | |
1140 { false, "AR", OCTAVE_CONF_AR }, | |
1141 { false, "ARFLAGS", OCTAVE_CONF_ARFLAGS }, | |
1142 { false, "BLAS_LIBS", OCTAVE_CONF_BLAS_LIBS }, | |
1143 { false, "CC", OCTAVE_CONF_CC }, | |
1144 { false, "CC_VERSION", OCTAVE_CONF_CC_VERSION }, | |
1145 { false, "CFLAGS", OCTAVE_CONF_CFLAGS }, | |
1146 { false, "CPICFLAG", OCTAVE_CONF_CPICFLAG }, | |
1147 { false, "CPPFLAGS", OCTAVE_CONF_CPPFLAGS }, | |
1148 { false, "CURL_LIBS", OCTAVE_CONF_CURL_LIBS }, | |
1149 { false, "CXX", OCTAVE_CONF_CXX }, | |
1150 { false, "CXXCPP", OCTAVE_CONF_CXXCPP }, | |
1151 { false, "CXXFLAGS", OCTAVE_CONF_CXXFLAGS }, | |
1152 { false, "CXXPICFLAG", OCTAVE_CONF_CXXPICFLAG }, | |
1153 { false, "CXX_VERSION", OCTAVE_CONF_CXX_VERSION }, | |
1154 { false, "DEFAULT_PAGER", OCTAVE_DEFAULT_PAGER }, | |
1155 { false, "DEFS", OCTAVE_CONF_DEFS }, | |
1156 { false, "DL_LD", OCTAVE_CONF_DL_LD }, | |
1157 { false, "DL_LDFLAGS", OCTAVE_CONF_DL_LDFLAGS }, | |
1158 { false, "ENABLE_DYNAMIC_LINKING", OCTAVE_CONF_ENABLE_DYNAMIC_LINKING }, | |
1159 { false, "EXEEXT", OCTAVE_CONF_EXEEXT }, | |
1160 { false, "F2C", OCTAVE_CONF_F2C }, | |
1161 { false, "F2CFLAGS", OCTAVE_CONF_F2CFLAGS }, | |
1162 { false, "F77", OCTAVE_CONF_F77 }, | |
1163 { false, "F77_FLOAT_STORE_FLAG", OCTAVE_CONF_F77_FLOAT_STORE_FLAG }, | |
1164 { false, "FC", OCTAVE_CONF_FC }, | |
1165 { false, "FFLAGS", OCTAVE_CONF_FFLAGS }, | |
1166 { false, "FFTW_LIBS", OCTAVE_CONF_FFTW_LIBS }, | |
1167 { false, "FLIBS", OCTAVE_CONF_FLIBS }, | |
1168 { false, "FPICFLAG", OCTAVE_CONF_FPICFLAG }, | |
1169 { false, "GLPK_LIBS", OCTAVE_CONF_GLPK_LIBS }, | |
7361 | 1170 { false, "GNUPLOT", OCTAVE_CONF_GNUPLOT }, |
6274 | 1171 { false, "INCFLAGS", OCTAVE_CONF_INCFLAGS }, |
1172 { false, "LDFLAGS", OCTAVE_CONF_LDFLAGS }, | |
1173 { false, "LD_CXX", OCTAVE_CONF_LD_CXX }, | |
1174 { false, "LD_STATIC_FLAG", OCTAVE_CONF_LD_STATIC_FLAG }, | |
1175 { false, "LEX", OCTAVE_CONF_LEX }, | |
1176 { false, "LEXLIB", OCTAVE_CONF_LEXLIB }, | |
1177 { false, "LFLAGS", OCTAVE_CONF_LFLAGS }, | |
1178 { false, "LIBCRUFT", OCTAVE_CONF_LIBCRUFT }, | |
1179 { false, "LIBEXT", OCTAVE_CONF_LIBEXT }, | |
1180 { false, "LIBFLAGS", OCTAVE_CONF_LIBFLAGS }, | |
1181 { false, "LIBOCTAVE", OCTAVE_CONF_LIBOCTAVE }, | |
1182 { false, "LIBOCTINTERP", OCTAVE_CONF_LIBOCTINTERP }, | |
1183 { false, "LIBREADLINE", OCTAVE_CONF_LIBREADLINE }, | |
1184 { false, "LIBS", OCTAVE_CONF_LIBS }, | |
1185 { 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
|
1186 { 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
|
1187 { false, "MAGICK_LIBS", OCTAVE_CONF_MAGICK_LIBS }, |
6274 | 1188 { false, "MKOCTFILE_DL_LDFLAGS", OCTAVE_CONF_MKOCTFILE_DL_LDFLAGS }, |
1189 { false, "RANLIB", OCTAVE_CONF_RANLIB }, | |
1190 { false, "RDYNAMIC_FLAG", OCTAVE_CONF_RDYNAMIC_FLAG }, | |
1191 { false, "RLD_FLAG", OCTAVE_CONF_RLD_FLAG }, | |
1192 { false, "SED", OCTAVE_CONF_SED }, | |
1193 { false, "SHARED_LIBS", OCTAVE_CONF_SHARED_LIBS }, | |
1194 { false, "SHLEXT", OCTAVE_CONF_SHLEXT }, | |
1195 { false, "SHLEXT_VER", OCTAVE_CONF_SHLEXT_VER }, | |
1196 { false, "SH_LD", OCTAVE_CONF_SH_LD }, | |
1197 { false, "SH_LDFLAGS", OCTAVE_CONF_SH_LDFLAGS }, | |
1198 { false, "SONAME_FLAGS", OCTAVE_CONF_SONAME_FLAGS }, | |
1199 { false, "STATIC_LIBS", OCTAVE_CONF_STATIC_LIBS }, | |
1200 { false, "UGLY_DEFS", OCTAVE_CONF_UGLY_DEFS }, | |
1201 { false, "USE_64_BIT_IDX_T", OCTAVE_CONF_USE_64_BIT_IDX_T }, | |
1202 { false, "XTRA_CFLAGS", OCTAVE_CONF_XTRA_CFLAGS }, | |
1203 { false, "XTRA_CXXFLAGS", OCTAVE_CONF_XTRA_CXXFLAGS }, | |
1204 { false, "YACC", OCTAVE_CONF_YACC }, | |
1205 { false, "YFLAGS", OCTAVE_CONF_YFLAGS }, | |
1206 { false, "api_version", OCTAVE_API_VERSION }, | |
1207 { true, "archlibdir", OCTAVE_ARCHLIBDIR }, | |
1208 { true, "bindir", OCTAVE_BINDIR }, | |
1209 { false, "canonical_host_type", OCTAVE_CANONICAL_HOST_TYPE }, | |
1210 { false, "config_opts", OCTAVE_CONF_config_opts }, | |
1211 { true, "datadir", OCTAVE_DATADIR }, | |
1212 { true, "datarootdir", OCTAVE_DATAROOTDIR }, | |
6276 | 1213 { true, "exec_prefix", OCTAVE_EXEC_PREFIX }, |
6274 | 1214 { true, "fcnfiledir", OCTAVE_FCNFILEDIR }, |
1215 { true, "imagedir", OCTAVE_IMAGEDIR }, | |
1216 { true, "includedir", OCTAVE_INCLUDEDIR }, | |
1217 { true, "infodir", OCTAVE_INFODIR }, | |
6276 | 1218 { true, "infofile", OCTAVE_INFOFILE }, |
6274 | 1219 { true, "libdir", OCTAVE_LIBDIR }, |
1220 { true, "libexecdir", OCTAVE_LIBEXECDIR }, | |
6311 | 1221 { true, "localapiarchlibdir", OCTAVE_LOCALAPIARCHLIBDIR }, |
6274 | 1222 { true, "localapifcnfiledir", OCTAVE_LOCALAPIFCNFILEDIR }, |
1223 { true, "localapioctfiledir", OCTAVE_LOCALAPIOCTFILEDIR }, | |
1224 { true, "localarchlibdir", OCTAVE_LOCALARCHLIBDIR }, | |
1225 { true, "localfcnfiledir", OCTAVE_LOCALFCNFILEDIR }, | |
1226 { true, "localoctfiledir", OCTAVE_LOCALOCTFILEDIR }, | |
1227 { true, "localstartupfiledir", OCTAVE_LOCALSTARTUPFILEDIR }, | |
1228 { true, "localverarchlibdir", OCTAVE_LOCALVERARCHLIBDIR }, | |
1229 { true, "localverfcnfiledir", OCTAVE_LOCALVERFCNFILEDIR }, | |
1230 { true, "localveroctfiledir", OCTAVE_LOCALVEROCTFILEDIR }, | |
1231 { true, "man1dir", OCTAVE_MAN1DIR }, | |
1232 { false, "man1ext", OCTAVE_MAN1EXT }, | |
1233 { true, "mandir", OCTAVE_MANDIR }, | |
1234 { true, "octfiledir", OCTAVE_OCTFILEDIR }, | |
1235 { true, "octincludedir", OCTAVE_OCTINCLUDEDIR }, | |
1236 { true, "octlibdir", OCTAVE_OCTLIBDIR }, | |
6276 | 1237 { true, "prefix", OCTAVE_PREFIX }, |
6274 | 1238 { true, "startupfiledir", OCTAVE_STARTUPFILEDIR }, |
1239 { false, "version", OCTAVE_VERSION }, | |
1240 { false, 0, 0 } | |
4357 | 1241 }; |
1242 | |
1243 if (! initialized) | |
1244 { | |
4675 | 1245 m.assign ("dld", octave_value (octave_supports_dynamic_linking)); |
4357 | 1246 |
4697 | 1247 oct_mach_info::float_format ff = oct_mach_info::native_float_format (); |
1248 m.assign ("float_format", | |
1249 octave_value (oct_mach_info::float_format_as_string (ff))); | |
1250 | |
1251 m.assign ("words_big_endian", | |
1252 octave_value (oct_mach_info::words_big_endian ())); | |
1253 | |
1254 m.assign ("words_little_endian", | |
1255 octave_value (oct_mach_info::words_little_endian ())); | |
1256 | |
4357 | 1257 int i = 0; |
4440 | 1258 |
1259 while (true) | |
4357 | 1260 { |
6274 | 1261 const conf_info_struct& elt = conf_info[i++]; |
1262 | |
1263 const char *key = elt.key; | |
4357 | 1264 |
1265 if (key) | |
6274 | 1266 { |
1267 if (elt.subst_home) | |
1268 m.assign (key, octave_value (subst_octave_home (elt.val))); | |
1269 else | |
1270 m.assign (key, octave_value (elt.val)); | |
1271 } | |
4357 | 1272 else |
1273 break; | |
1274 } | |
1275 | |
4691 | 1276 bool unix_system = true; |
7013 | 1277 bool mac_system = false; |
4691 | 1278 bool windows_system = false; |
1279 | |
1280 #if defined (WIN32) | |
1281 windows_system = true; | |
1282 #if !defined (__CYGWIN__) | |
1283 unix_system = false; | |
1284 #endif | |
1285 #endif | |
1286 | |
8574
83b8c739d626
toplev.cc: check OCTAVE_USE_OS_X_API instead of __APPLE__ && __MACH__
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
1287 #if defined (OCTAVE_USE_OS_X_API) |
7010 | 1288 mac_system = true; |
1289 #endif | |
1290 | |
4691 | 1291 m.assign ("unix", octave_value (unix_system)); |
7010 | 1292 m.assign ("mac", octave_value (mac_system)); |
4691 | 1293 m.assign ("windows", octave_value (windows_system)); |
1294 | |
4357 | 1295 initialized = true; |
1296 } | |
2162 | 1297 |
2689 | 1298 int nargin = args.length (); |
1299 | |
1300 if (nargin == 1) | |
1301 { | |
3523 | 1302 std::string arg = args(0).string_value (); |
2689 | 1303 |
1304 if (! error_state) | |
4675 | 1305 { |
1306 Cell c = m.contents (arg.c_str ()); | |
5199 | 1307 |
1308 if (c.is_empty ()) | |
1309 error ("octave_config_info: no info for `%s'", arg.c_str ()); | |
1310 else | |
1311 retval = c(0); | |
4675 | 1312 } |
2689 | 1313 } |
1314 else if (nargin == 0) | |
4233 | 1315 retval = m; |
2689 | 1316 else |
5823 | 1317 print_usage (); |
2689 | 1318 |
1319 return retval; | |
2162 | 1320 } |
1321 | |
1683 | 1322 #if defined (__GNUG__) && defined (DEBUG_NEW_DELETE) |
2806 | 1323 |
1683 | 1324 int debug_new_delete = 0; |
1325 | |
1326 typedef void (*vfp)(void); | |
1327 extern vfp __new_handler; | |
1328 | |
1329 void * | |
1330 __builtin_new (size_t sz) | |
1331 { | |
1332 void *p; | |
1333 | |
1334 /* malloc (0) is unpredictable; avoid it. */ | |
1335 if (sz == 0) | |
1336 sz = 1; | |
2800 | 1337 p = malloc (sz); |
1683 | 1338 while (p == 0) |
1339 { | |
1340 (*__new_handler) (); | |
2800 | 1341 p = malloc (sz); |
1683 | 1342 } |
1343 | |
1344 if (debug_new_delete) | |
5629 | 1345 std::cerr << "__builtin_new: " << p << std::endl; |
1683 | 1346 |
1347 return p; | |
1348 } | |
1349 | |
1350 void | |
1351 __builtin_delete (void *ptr) | |
1352 { | |
1353 if (debug_new_delete) | |
5629 | 1354 std::cerr << "__builtin_delete: " << ptr << std::endl; |
1683 | 1355 |
1356 if (ptr) | |
1357 free (ptr); | |
1358 } | |
2806 | 1359 |
1683 | 1360 #endif |
1361 | |
1362 /* | |
1363 ;;; Local Variables: *** | |
1364 ;;; mode: C++ *** | |
1365 ;;; End: *** | |
1366 */ |