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