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