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