1
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
240
|
23 #ifdef HAVE_CONFIG_H |
1192
|
24 #include <config.h> |
1
|
25 #endif |
|
26 |
1468
|
27 #include <cstdio> |
1343
|
28 #include <cstring> |
605
|
29 |
4207
|
30 #include <set> |
1728
|
31 #include <string> |
|
32 |
2926
|
33 #include "file-stat.h" |
|
34 #include "oct-env.h" |
|
35 #include "glob-match.h" |
1755
|
36 #include "str-vec.h" |
|
37 |
2492
|
38 #include <defaults.h> |
4435
|
39 #include "Cell.h" |
1352
|
40 #include "defun.h" |
|
41 #include "dirfns.h" |
|
42 #include "error.h" |
2205
|
43 #include "gripes.h" |
1352
|
44 #include "help.h" |
3165
|
45 #include "input.h" |
1352
|
46 #include "lex.h" |
2926
|
47 #include "oct-map.h" |
|
48 #include "oct-obj.h" |
|
49 #include "ov.h" |
3933
|
50 #include "ov-usr-fcn.h" |
605
|
51 #include "pager.h" |
1352
|
52 #include "parse.h" |
2926
|
53 #include "symtab.h" |
2205
|
54 #include "toplev.h" |
1352
|
55 #include "unwind-prot.h" |
1
|
56 #include "utils.h" |
1352
|
57 #include "variables.h" |
2205
|
58 |
|
59 // Should Octave always check to see if function files have changed |
|
60 // since they were last compiled? |
2806
|
61 static int Vignore_function_time_stamp; |
2205
|
62 |
1
|
63 // Symbol table for symbols at the top level. |
581
|
64 symbol_table *top_level_sym_tab = 0; |
1
|
65 |
|
66 // Symbol table for the current scope. |
581
|
67 symbol_table *curr_sym_tab = 0; |
1
|
68 |
4245
|
69 // Symbol table for the current caller scope. |
|
70 symbol_table *curr_caller_sym_tab = 0; |
|
71 |
1
|
72 // Symbol table for global symbols. |
581
|
73 symbol_table *global_sym_tab = 0; |
1
|
74 |
4009
|
75 // Symbol table for functions and built-in symbols. |
|
76 symbol_table *fbi_sym_tab = 0; |
|
77 |
4208
|
78 static inline bool |
|
79 at_top_level (void) |
|
80 { |
|
81 return (curr_sym_tab == top_level_sym_tab); |
|
82 } |
|
83 |
593
|
84 // Initialization. |
|
85 |
|
86 // Create the initial symbol tables and set the current scope at the |
|
87 // top level. |
|
88 |
195
|
89 void |
|
90 initialize_symbol_tables (void) |
|
91 { |
4009
|
92 if (! fbi_sym_tab) |
4238
|
93 fbi_sym_tab = new symbol_table (2048, "FBI"); |
4009
|
94 |
581
|
95 if (! global_sym_tab) |
4238
|
96 global_sym_tab = new symbol_table (2048, "GLOBAL"); |
195
|
97 |
581
|
98 if (! top_level_sym_tab) |
4238
|
99 top_level_sym_tab = new symbol_table (4096, "TOP"); |
195
|
100 |
4245
|
101 curr_caller_sym_tab = curr_sym_tab = top_level_sym_tab; |
195
|
102 } |
|
103 |
593
|
104 // Attributes of variables and functions. |
|
105 |
|
106 // Is this variable a builtin? |
|
107 |
1827
|
108 bool |
3523
|
109 is_builtin_variable (const std::string& name) |
593
|
110 { |
4009
|
111 symbol_record *sr = fbi_sym_tab->lookup (name); |
593
|
112 return (sr && sr->is_builtin_variable ()); |
|
113 } |
|
114 |
4208
|
115 // Is this a command-style function? |
593
|
116 |
4208
|
117 static std::set <std::string> command_set; |
4207
|
118 |
|
119 static inline bool |
4208
|
120 is_marked_as_command (const std::string& s) |
4207
|
121 { |
4208
|
122 return command_set.find (s) != command_set.end (); |
4207
|
123 } |
|
124 |
|
125 static inline void |
4208
|
126 mark_as_command (const std::string& s) |
4207
|
127 { |
4208
|
128 command_set.insert (s); |
4207
|
129 } |
|
130 |
|
131 static inline void |
4208
|
132 unmark_command (const std::string& s) |
4207
|
133 { |
4208
|
134 command_set.erase (s); |
4207
|
135 |
|
136 symbol_record *sr = fbi_sym_tab->lookup (s); |
|
137 |
|
138 if (sr) |
4208
|
139 sr->unmark_command (); |
4207
|
140 } |
|
141 |
4208
|
142 DEFCMD (mark_as_command, args, , |
4207
|
143 "-*- texinfo -*-\n\ |
4208
|
144 @deftypefn {Built-in Function} {} mark_as_command (@var{name})\n\ |
|
145 Enter @var{name} into the list of commands.\n\ |
4207
|
146 @end deftypefn") |
|
147 { |
|
148 octave_value_list retval; |
|
149 |
4208
|
150 if (at_top_level ()) |
|
151 { |
|
152 int nargin = args.length (); |
4207
|
153 |
4208
|
154 if (nargin > 0) |
|
155 { |
|
156 int argc = nargin + 1; |
4207
|
157 |
4208
|
158 string_vector argv = args.make_argv ("mark_as_command"); |
4207
|
159 |
4208
|
160 if (! error_state) |
|
161 { |
|
162 for (int i = 1; i < argc; i++) |
|
163 mark_as_command (argv[i]); |
|
164 } |
4207
|
165 } |
4208
|
166 else |
|
167 print_usage ("mark_as_command"); |
4207
|
168 } |
|
169 else |
4208
|
170 warning ("mark_as_command: invalid use inside function body"); |
4207
|
171 |
|
172 return retval; |
|
173 } |
|
174 |
4208
|
175 DEFCMD (unmark_command, args, , |
4207
|
176 "-*- texinfo -*-\n\ |
4208
|
177 @deftypefn {Built-in Function} {} mark_as_command (@var{name})\n\ |
|
178 Remove @var{name} from the list of commands.\n\ |
4207
|
179 @end deftypefn") |
|
180 { |
|
181 octave_value_list retval; |
|
182 |
4208
|
183 if (at_top_level ()) |
|
184 { |
|
185 int nargin = args.length (); |
4207
|
186 |
4208
|
187 if (nargin > 0) |
|
188 { |
|
189 int argc = nargin + 1; |
4207
|
190 |
4208
|
191 string_vector argv = args.make_argv ("unmark_command"); |
4207
|
192 |
4208
|
193 if (! error_state) |
|
194 { |
|
195 for (int i = 1; i < argc; i++) |
|
196 unmark_command (argv[i]); |
|
197 } |
4207
|
198 } |
4208
|
199 else |
|
200 print_usage ("unmark_command"); |
4207
|
201 } |
|
202 else |
4208
|
203 warning ("mark_as_command: invalid use inside function body"); |
4207
|
204 |
|
205 return retval; |
|
206 } |
|
207 |
1827
|
208 bool |
4208
|
209 is_command_name (const std::string& s) |
593
|
210 { |
4207
|
211 bool retval = false; |
|
212 |
4009
|
213 symbol_record *sr = fbi_sym_tab->lookup (s); |
4207
|
214 |
|
215 if (sr) |
|
216 { |
4208
|
217 if (sr->is_command ()) |
4207
|
218 retval = true; |
4208
|
219 else if (is_marked_as_command (s)) |
4207
|
220 { |
4208
|
221 sr->mark_as_command (); |
4207
|
222 retval = true; |
|
223 } |
|
224 } |
|
225 else |
4208
|
226 retval = is_marked_as_command (s); |
4207
|
227 |
|
228 return retval; |
593
|
229 } |
|
230 |
3355
|
231 // Is this a built-in function? |
2294
|
232 |
|
233 bool |
3523
|
234 is_builtin_function_name (const std::string& s) |
2294
|
235 { |
4009
|
236 symbol_record *sr = fbi_sym_tab->lookup (s); |
2294
|
237 return (sr && sr->is_builtin_function ()); |
|
238 } |
|
239 |
|
240 // Is this a mapper function? |
|
241 |
|
242 bool |
3523
|
243 is_mapper_function_name (const std::string& s) |
2294
|
244 { |
4009
|
245 symbol_record *sr = fbi_sym_tab->lookup (s); |
2294
|
246 return (sr && sr->is_mapper_function ()); |
|
247 } |
|
248 |
593
|
249 // Is this function globally in this scope? |
|
250 |
1827
|
251 bool |
3523
|
252 is_globally_visible (const std::string& name) |
593
|
253 { |
2856
|
254 symbol_record *sr = curr_sym_tab->lookup (name); |
593
|
255 return (sr && sr->is_linked_to_global ()); |
|
256 } |
|
257 |
2086
|
258 // Is this octave_value a valid function? |
593
|
259 |
2975
|
260 octave_function * |
4345
|
261 is_valid_function (const std::string& fcn_name, |
|
262 const std::string& warn_for, bool warn) |
593
|
263 { |
2975
|
264 octave_function *ans = 0; |
593
|
265 |
|
266 symbol_record *sr = 0; |
1755
|
267 |
|
268 if (! fcn_name.empty ()) |
3618
|
269 { |
4009
|
270 sr = fbi_sym_tab->lookup (fcn_name, true); |
3618
|
271 |
|
272 lookup (sr, false); |
|
273 } |
593
|
274 |
|
275 if (sr) |
2975
|
276 { |
|
277 octave_value tmp = sr->def (); |
|
278 ans = tmp.function_value (true); |
|
279 } |
593
|
280 |
|
281 if (! sr || ! ans || ! sr->is_function ()) |
|
282 { |
|
283 if (warn) |
|
284 error ("%s: the symbol `%s' is not valid as a function", |
1755
|
285 warn_for.c_str (), fcn_name.c_str ()); |
593
|
286 ans = 0; |
|
287 } |
|
288 |
|
289 return ans; |
|
290 } |
|
291 |
2975
|
292 octave_function * |
4345
|
293 is_valid_function (const octave_value& arg, |
|
294 const std::string& warn_for, bool warn) |
3178
|
295 { |
|
296 octave_function *ans = 0; |
|
297 |
3523
|
298 std::string fcn_name; |
3178
|
299 |
|
300 if (arg.is_string ()) |
|
301 fcn_name = arg.string_value (); |
|
302 |
|
303 if (! error_state) |
|
304 ans = is_valid_function (fcn_name, warn_for, warn); |
|
305 else if (warn) |
|
306 error ("%s: expecting function name as argument", warn_for.c_str ()); |
|
307 |
|
308 return ans; |
|
309 } |
|
310 |
|
311 octave_function * |
3523
|
312 extract_function (const octave_value& arg, const std::string& warn_for, |
|
313 const std::string& fname, const std::string& header, |
|
314 const std::string& trailer) |
2796
|
315 { |
2975
|
316 octave_function *retval = 0; |
2796
|
317 |
|
318 retval = is_valid_function (arg, warn_for, 0); |
|
319 |
|
320 if (! retval) |
|
321 { |
3523
|
322 std::string s = arg.string_value (); |
2796
|
323 |
3523
|
324 std::string cmd = header; |
2796
|
325 cmd.append (s); |
|
326 cmd.append (trailer); |
|
327 |
|
328 if (! error_state) |
|
329 { |
|
330 int parse_status; |
|
331 |
2898
|
332 eval_string (cmd, true, parse_status); |
2796
|
333 |
|
334 if (parse_status == 0) |
|
335 { |
|
336 retval = is_valid_function (fname, warn_for, 0); |
|
337 |
|
338 if (! retval) |
|
339 { |
|
340 error ("%s: `%s' is not valid as a function", |
|
341 warn_for.c_str (), fname.c_str ()); |
|
342 return retval; |
|
343 } |
|
344 } |
|
345 else |
|
346 error ("%s: `%s' is not valid as a function", |
|
347 warn_for.c_str (), fname.c_str ()); |
|
348 } |
|
349 else |
|
350 error ("%s: expecting first argument to be a string", |
|
351 warn_for.c_str ()); |
|
352 } |
|
353 |
|
354 return retval; |
|
355 } |
|
356 |
2921
|
357 string_vector |
3523
|
358 get_struct_elts (const std::string& text) |
2921
|
359 { |
|
360 int n = 1; |
|
361 |
|
362 size_t pos = 0; |
|
363 |
|
364 size_t len = text.length (); |
|
365 |
|
366 while ((pos = text.find ('.', pos)) != NPOS) |
|
367 { |
|
368 if (++pos == len) |
|
369 break; |
|
370 |
|
371 n++; |
|
372 } |
|
373 |
|
374 string_vector retval (n); |
|
375 |
|
376 pos = 0; |
|
377 |
|
378 for (int i = 0; i < n; i++) |
|
379 { |
|
380 size_t len = text.find ('.', pos); |
|
381 |
|
382 if (len != NPOS) |
|
383 len -= pos; |
|
384 |
|
385 retval[i] = text.substr (pos, len); |
|
386 |
|
387 if (len != NPOS) |
|
388 pos += len + 1; |
|
389 } |
|
390 |
|
391 return retval; |
|
392 } |
|
393 |
4179
|
394 static inline bool |
|
395 is_variable (const std::string& name) |
|
396 { |
|
397 bool retval = false; |
|
398 |
|
399 if (! name.empty ()) |
|
400 { |
|
401 symbol_record *sr = curr_sym_tab->lookup (name); |
|
402 |
|
403 if (! sr) |
|
404 sr = fbi_sym_tab->lookup (name); |
|
405 |
|
406 retval = (sr && sr->is_variable ()); |
|
407 } |
|
408 |
|
409 return retval; |
|
410 } |
|
411 |
2921
|
412 string_vector |
3933
|
413 generate_struct_completions (const std::string& text, |
|
414 std::string& prefix, std::string& hint) |
2921
|
415 { |
|
416 string_vector names; |
|
417 |
|
418 size_t pos = text.rfind ('.'); |
|
419 |
3933
|
420 if (pos != NPOS) |
2921
|
421 { |
|
422 if (pos == text.length ()) |
|
423 hint = ""; |
|
424 else |
|
425 hint = text.substr (pos+1); |
|
426 |
|
427 prefix = text.substr (0, pos); |
|
428 |
4179
|
429 std::string base_name = prefix; |
|
430 |
|
431 pos = base_name.find_first_of ("{(."); |
2921
|
432 |
4179
|
433 if (pos != NPOS) |
|
434 base_name = base_name.substr (0, pos); |
4143
|
435 |
4179
|
436 if (is_variable (base_name)) |
|
437 { |
|
438 int parse_status; |
|
439 |
|
440 unwind_protect::begin_frame ("generate_struct_completions"); |
3935
|
441 |
4179
|
442 unwind_protect_str (Vwarning_option); |
|
443 unwind_protect_bool (discard_error_messages); |
|
444 unwind_protect_int (error_state); |
3935
|
445 |
4179
|
446 Vwarning_option = "off"; |
|
447 discard_error_messages = true; |
2921
|
448 |
4179
|
449 octave_value tmp = eval_string (prefix, true, parse_status); |
|
450 |
|
451 unwind_protect::run_frame ("generate_struct_completions"); |
3935
|
452 |
4179
|
453 if (tmp.is_defined () && tmp.is_map ()) |
|
454 names = tmp.map_keys (); |
|
455 } |
|
456 } |
2921
|
457 |
|
458 return names; |
|
459 } |
|
460 |
4179
|
461 // XXX FIXME XXX -- this will have to be much smarter to work |
|
462 // "correctly". |
|
463 |
2921
|
464 bool |
3523
|
465 looks_like_struct (const std::string& text) |
2921
|
466 { |
3968
|
467 bool retval = false; |
|
468 |
4179
|
469 #if 0 |
3968
|
470 symbol_record *sr = curr_sym_tab->lookup (text); |
2963
|
471 |
3968
|
472 if (sr && ! sr->is_function ()) |
|
473 { |
|
474 int parse_status; |
2921
|
475 |
4143
|
476 unwind_protect::begin_frame ("looks_like_struct"); |
|
477 |
|
478 unwind_protect_str (Vwarning_option); |
|
479 unwind_protect_bool (discard_error_messages); |
|
480 unwind_protect_int (error_state); |
|
481 |
|
482 Vwarning_option = "off"; |
|
483 discard_error_messages = true; |
|
484 |
3968
|
485 octave_value tmp = eval_string (text, true, parse_status); |
|
486 |
4143
|
487 unwind_protect::run_frame ("looks_like_struct"); |
|
488 |
3968
|
489 retval = (tmp.is_defined () && tmp.is_map ()); |
|
490 } |
4179
|
491 #endif |
3968
|
492 |
|
493 return retval; |
2921
|
494 } |
2796
|
495 |
4028
|
496 DEFUN (isglobal, args, , |
3361
|
497 "-*- texinfo -*-\n\ |
4028
|
498 @deftypefn {Built-in Function} {} isglobal (@var{name})\n\ |
3361
|
499 Return 1 if @var{name} is globally visible. Otherwise, return 0. For\n\ |
|
500 example,\n\ |
|
501 \n\ |
|
502 @example\n\ |
|
503 @group\n\ |
|
504 global x\n\ |
4028
|
505 isglobal (\"x\")\n\ |
3361
|
506 @result{} 1\n\ |
|
507 @end group\n\ |
|
508 @end example\n\ |
|
509 @end deftypefn") |
593
|
510 { |
4233
|
511 octave_value retval = false; |
593
|
512 |
712
|
513 int nargin = args.length (); |
|
514 |
|
515 if (nargin != 1) |
593
|
516 { |
4028
|
517 print_usage ("isglobal"); |
593
|
518 return retval; |
|
519 } |
|
520 |
3523
|
521 std::string name = args(0).string_value (); |
593
|
522 |
636
|
523 if (error_state) |
|
524 { |
4028
|
525 error ("isglobal: expecting std::string argument"); |
636
|
526 return retval; |
|
527 } |
|
528 |
2856
|
529 symbol_record *sr = curr_sym_tab->lookup (name); |
593
|
530 |
4233
|
531 retval = (sr && sr->is_linked_to_global ()); |
593
|
532 |
|
533 return retval; |
|
534 } |
|
535 |
4016
|
536 int |
|
537 symbol_exist (const std::string& name, const std::string& type) |
593
|
538 { |
4016
|
539 int retval = 0; |
636
|
540 |
3523
|
541 std::string struct_elts; |
|
542 std::string symbol_name = name; |
1755
|
543 |
|
544 size_t pos = name.find ('.'); |
|
545 |
2790
|
546 if (pos != NPOS && pos > 0) |
1277
|
547 { |
1755
|
548 struct_elts = name.substr (pos+1); |
2790
|
549 symbol_name = name.substr (0, pos); |
1277
|
550 } |
|
551 |
4009
|
552 // We shouldn't need to look in the global symbol table, since any |
|
553 // name that is visible in the current scope will be in the local |
|
554 // symbol table. |
|
555 |
2856
|
556 symbol_record *sr = curr_sym_tab->lookup (symbol_name); |
593
|
557 |
4009
|
558 if (! (sr && sr->is_defined ())) |
|
559 sr = fbi_sym_tab->lookup (symbol_name); |
593
|
560 |
4009
|
561 if (sr && sr->is_defined ()) |
1277
|
562 { |
4357
|
563 bool not_a_struct = struct_elts.empty (); |
|
564 bool var_ok = not_a_struct || sr->is_map_element (struct_elts); |
|
565 |
4016
|
566 if (! retval |
4357
|
567 && var_ok |
4016
|
568 && (type == "any" || type == "var") |
4357
|
569 && sr->is_user_variable ()) |
4009
|
570 { |
4016
|
571 retval = 1; |
4009
|
572 } |
4016
|
573 |
|
574 if (! retval |
|
575 && (type == "any" || type == "builtin")) |
4009
|
576 { |
4357
|
577 if (not_a_struct && sr->is_builtin_function ()) |
4016
|
578 { |
|
579 retval = 5; |
|
580 } |
4357
|
581 else if (var_ok && sr->is_builtin_variable ()) |
4016
|
582 { |
|
583 retval = 101; |
|
584 } |
4357
|
585 else if (var_ok && sr->is_builtin_constant ()) |
4016
|
586 { |
|
587 retval = 102; |
|
588 } |
4009
|
589 } |
4016
|
590 |
|
591 if (! retval |
4357
|
592 && not_a_struct |
4016
|
593 && (type == "any" || type == "file") |
|
594 && (sr->is_user_function () || sr->is_dld_function ())) |
4009
|
595 { |
4261
|
596 octave_value t = sr->def (); |
4016
|
597 octave_function *f = t.function_value (true); |
|
598 std::string s = f ? f->fcn_file_name () : std::string (); |
|
599 |
|
600 retval = s.empty () ? 103 : 2; |
4009
|
601 } |
1421
|
602 } |
4016
|
603 |
|
604 if (! retval) |
593
|
605 { |
4437
|
606 string_vector names (2); |
1755
|
607 |
4437
|
608 names(0) = name + ".oct"; |
|
609 names(1) = name + ".m"; |
|
610 |
|
611 std::string file_name = Vload_path_dir_path.find_first_of (names); |
|
612 |
|
613 size_t len = file_name.length (); |
4016
|
614 |
4437
|
615 if (! file_name.empty ()) |
593
|
616 { |
4437
|
617 if (type == "any" || type == "file") |
|
618 { |
|
619 if (file_name.substr (len-4) == ".oct") |
|
620 retval = 3; |
|
621 else |
|
622 retval = 2; |
|
623 } |
593
|
624 } |
4016
|
625 } |
|
626 |
|
627 if (! retval) |
|
628 { |
|
629 std::string file_name = file_in_path (name, ""); |
|
630 |
|
631 if (file_name.empty ()) |
|
632 file_name = name; |
|
633 |
|
634 file_stat fs (file_name); |
|
635 |
|
636 if (fs) |
593
|
637 { |
4016
|
638 if ((type == "any" || type == "file") |
|
639 && fs.is_reg ()) |
1421
|
640 { |
4016
|
641 retval = 2; |
1421
|
642 } |
4016
|
643 else if ((type == "any" || type == "dir") |
|
644 && fs.is_dir ()) |
1421
|
645 { |
4016
|
646 retval = 7; |
1421
|
647 } |
593
|
648 } |
|
649 } |
|
650 |
|
651 return retval; |
|
652 } |
|
653 |
4016
|
654 DEFUN (exist, args, , |
|
655 "-*- texinfo -*-\n\ |
|
656 @deftypefn {Built-in Function} {} exist (@var{name}, @var{type})\n\ |
|
657 Return 1 if the name exists as a variable, 2 if the name (after\n\ |
|
658 appending @samp{.m}) is a function file in Octave's LOADPATH, 3 if the\n\ |
|
659 name is a @samp{.oct} file in Octave's LOADPATH, 5 if the name is a\n\ |
|
660 built-in function, 7 if the name is a directory, 101 if the name is\n\ |
|
661 a built-in variable, 102 if the name is a built-in constant, or 103\n\ |
|
662 if the name is a function not associated with a file (entered on\n\ |
|
663 the command line).\n\ |
|
664 \n\ |
|
665 Otherwise, return 0.\n\ |
|
666 \n\ |
|
667 This function also returns 2 if a regular file called @var{name}\n\ |
|
668 exists in Octave's @code{LOADPATH}. If you want information about\n\ |
|
669 other types of files, you should use some combination of the functions\n\ |
|
670 @code{file_in_path} and @code{stat} instead.\n\ |
|
671 \n\ |
|
672 If the optional argument @var{type} is supplied, check only for\n\ |
|
673 symbols of the specified type. Valid types are\n\ |
|
674 \n\ |
|
675 @table @samp\n\ |
|
676 @item \"var\"\n\ |
|
677 Check only for variables.\n\ |
|
678 @item \"builtin\"\n\ |
|
679 Check only for built-in functions.\n\ |
|
680 @item \"file\"\n\ |
|
681 Check only for files.\n\ |
|
682 @item \"dir\"\n\ |
|
683 Check only for directories.\n\ |
|
684 @end table\n\ |
|
685 @end deftypefn") |
|
686 { |
4233
|
687 octave_value retval = false; |
4016
|
688 |
|
689 int nargin = args.length (); |
|
690 |
|
691 if (nargin == 1 || nargin == 2) |
|
692 { |
|
693 std::string name = args(0).string_value (); |
|
694 |
|
695 if (! error_state) |
|
696 { |
|
697 std::string type |
|
698 = (nargin == 2) ? args(1).string_value () : std::string ("any"); |
|
699 |
|
700 if (! error_state) |
4233
|
701 retval = symbol_exist (name, type); |
4016
|
702 else |
|
703 error ("exist: expecting second argument to be a string"); |
|
704 } |
|
705 else |
|
706 error ("exist: expecting first argument to be a string"); |
|
707 } |
|
708 else |
|
709 print_usage ("exist"); |
|
710 |
|
711 return retval; |
|
712 } |
|
713 |
581
|
714 // Is there a corresponding function file that is newer than the |
|
715 // symbol definition? |
|
716 |
2926
|
717 static bool |
1
|
718 symbol_out_of_date (symbol_record *sr) |
|
719 { |
2926
|
720 bool retval = false; |
195
|
721 |
2926
|
722 if (Vignore_function_time_stamp != 2 && sr) |
1
|
723 { |
2975
|
724 octave_value ans = sr->def (); |
|
725 |
3022
|
726 octave_function *tmp = ans.function_value (true); |
2975
|
727 |
3022
|
728 if (tmp) |
|
729 { |
3523
|
730 std::string ff = tmp->fcn_file_name (); |
1755
|
731 |
3022
|
732 if (! (ff.empty () |
|
733 || (Vignore_function_time_stamp |
|
734 && tmp->is_system_fcn_file ()))) |
|
735 { |
3165
|
736 if (tmp->time_checked () < Vlast_prompt_time) |
|
737 { |
|
738 time_t tp = tmp->time_parsed (); |
2975
|
739 |
3523
|
740 std::string fname; |
3325
|
741 |
|
742 if (tmp->is_dld_function ()) |
|
743 fname = ff; |
|
744 else |
|
745 fname = fcn_file_in_path (ff); |
1755
|
746 |
3255
|
747 tmp->mark_fcn_file_up_to_date (octave_time ()); |
3165
|
748 |
|
749 file_stat fs (fname); |
3022
|
750 |
3165
|
751 if (fs && fs.is_newer (tp)) |
|
752 retval = true; |
|
753 } |
1
|
754 } |
|
755 } |
|
756 } |
2926
|
757 |
|
758 return retval; |
1
|
759 } |
|
760 |
1827
|
761 bool |
2856
|
762 lookup (symbol_record *sym_rec, bool exec_script) |
581
|
763 { |
1827
|
764 bool script_executed = false; |
581
|
765 |
|
766 if (! sym_rec->is_linked_to_global ()) |
|
767 { |
|
768 if (sym_rec->is_defined ()) |
|
769 { |
|
770 if (sym_rec->is_function () && symbol_out_of_date (sym_rec)) |
1271
|
771 script_executed = load_fcn_from_file (sym_rec, exec_script); |
581
|
772 } |
|
773 else if (! sym_rec->is_formal_parameter ()) |
|
774 { |
|
775 link_to_builtin_or_function (sym_rec); |
1271
|
776 |
581
|
777 if (! sym_rec->is_defined ()) |
1271
|
778 script_executed = load_fcn_from_file (sym_rec, exec_script); |
581
|
779 else if (sym_rec->is_function () && symbol_out_of_date (sym_rec)) |
1271
|
780 script_executed = load_fcn_from_file (sym_rec, exec_script); |
581
|
781 } |
|
782 } |
|
783 |
1271
|
784 return script_executed; |
581
|
785 } |
|
786 |
|
787 // Get the symbol record for the given name that is visible in the |
|
788 // current scope. Reread any function definitions that appear to be |
|
789 // out of date. If a function is available in a file but is not |
|
790 // currently loaded, this will load it and insert the name in the |
|
791 // current symbol table. |
|
792 |
|
793 symbol_record * |
3523
|
794 lookup_by_name (const std::string& nm, bool exec_script) |
581
|
795 { |
2856
|
796 symbol_record *sym_rec = curr_sym_tab->lookup (nm, true); |
581
|
797 |
|
798 lookup (sym_rec, exec_script); |
|
799 |
|
800 return sym_rec; |
|
801 } |
|
802 |
4342
|
803 octave_function * |
|
804 lookup_function (const std::string& nm) |
|
805 { |
|
806 octave_function *retval = 0; |
|
807 |
|
808 symbol_record *sr = 0; |
|
809 |
|
810 if (curr_parent_function) |
|
811 { |
|
812 std::string parent = curr_parent_function->function_name (); |
|
813 |
|
814 sr = fbi_sym_tab->lookup (parent + ":" + nm); |
|
815 } |
|
816 |
|
817 if (! sr || ! sr->is_function ()) |
|
818 { |
|
819 sr = fbi_sym_tab->lookup (nm, true); |
|
820 |
|
821 if (sr && ! sr->is_function ()) |
|
822 load_fcn_from_file (sr, false); |
|
823 } |
|
824 |
|
825 if (sr) |
|
826 { |
|
827 octave_value v = sr->def (); |
|
828 |
|
829 if (v.is_function ()) |
|
830 retval = v.function_value (); |
|
831 } |
|
832 |
|
833 return retval; |
|
834 } |
|
835 |
2849
|
836 octave_value |
3523
|
837 get_global_value (const std::string& nm) |
2849
|
838 { |
|
839 octave_value retval; |
|
840 |
|
841 symbol_record *sr = global_sym_tab->lookup (nm); |
|
842 |
|
843 if (sr) |
|
844 { |
2975
|
845 octave_value sr_def = sr->def (); |
2849
|
846 |
2975
|
847 if (sr_def.is_undefined ()) |
2849
|
848 error ("get_global_by_name: undefined symbol `%s'", nm.c_str ()); |
3088
|
849 else |
|
850 retval = sr_def; |
2849
|
851 } |
|
852 else |
|
853 error ("get_global_by_name: unknown symbol `%s'", nm.c_str ()); |
|
854 |
|
855 return retval; |
|
856 } |
|
857 |
|
858 void |
3523
|
859 set_global_value (const std::string& nm, const octave_value& val) |
2849
|
860 { |
2856
|
861 symbol_record *sr = global_sym_tab->lookup (nm, true); |
2849
|
862 |
|
863 if (sr) |
|
864 sr->define (val); |
|
865 else |
|
866 panic_impossible (); |
|
867 } |
|
868 |
593
|
869 // Variable values. |
195
|
870 |
581
|
871 // Look for the given name in the global symbol table. If it refers |
|
872 // to a string, return a new copy. If not, return 0; |
|
873 |
3523
|
874 std::string |
|
875 builtin_string_variable (const std::string& name) |
1
|
876 { |
4009
|
877 symbol_record *sr = fbi_sym_tab->lookup (name); |
195
|
878 |
4329
|
879 // It is a programming error to look for builtins that aren't. |
195
|
880 |
4280
|
881 // Use != here to avoid possible conversion to int of smaller type |
|
882 // than the sr pointer. |
|
883 |
|
884 assert (sr != 0); |
195
|
885 |
3523
|
886 std::string retval; |
1
|
887 |
2975
|
888 octave_value val = sr->def (); |
195
|
889 |
2975
|
890 if (! error_state && val.is_string ()) |
|
891 retval = val.string_value (); |
1
|
892 |
|
893 return retval; |
|
894 } |
|
895 |
581
|
896 // Look for the given name in the global symbol table. If it refers |
1504
|
897 // to a real scalar, place the value in d and return 1. Otherwise, |
|
898 // return 0. |
581
|
899 |
1
|
900 int |
3523
|
901 builtin_real_scalar_variable (const std::string& name, double& d) |
1
|
902 { |
1504
|
903 int status = 0; |
4009
|
904 symbol_record *sr = fbi_sym_tab->lookup (name); |
195
|
905 |
4329
|
906 // It is a programming error to look for builtins that aren't. |
195
|
907 |
4280
|
908 // Use != here to avoid possible conversion to int of smaller type |
|
909 // than the sr pointer. |
|
910 |
|
911 assert (sr != 0); |
1
|
912 |
2975
|
913 octave_value val = sr->def (); |
195
|
914 |
2975
|
915 if (! error_state && val.is_scalar_type ()) |
|
916 { |
|
917 d = val.double_value (); |
|
918 status = 1; |
1
|
919 } |
|
920 |
|
921 return status; |
|
922 } |
|
923 |
1093
|
924 // Look for the given name in the global symbol table. |
|
925 |
2086
|
926 octave_value |
3523
|
927 builtin_any_variable (const std::string& name) |
1093
|
928 { |
4009
|
929 symbol_record *sr = fbi_sym_tab->lookup (name); |
1093
|
930 |
4329
|
931 // It is a programming error to look for builtins that aren't. |
1093
|
932 |
4280
|
933 // Use != here to avoid possible conversion to int of smaller type |
|
934 // than the sr pointer. |
|
935 |
|
936 assert (sr != 0); |
1093
|
937 |
2975
|
938 return sr->def (); |
1093
|
939 } |
|
940 |
593
|
941 // Global stuff and links to builtin variables and functions. |
|
942 |
581
|
943 // Make the definition of the symbol record sr be the same as the |
|
944 // definition of the global variable of the same name, creating it if |
1418
|
945 // it doesn't already exist. |
581
|
946 |
195
|
947 void |
|
948 link_to_global_variable (symbol_record *sr) |
|
949 { |
2975
|
950 if (! sr->is_linked_to_global ()) |
195
|
951 { |
2975
|
952 sr->mark_as_linked_to_global (); |
195
|
953 |
2975
|
954 if (! error_state) |
|
955 { |
3523
|
956 std::string nm = sr->name (); |
2846
|
957 |
2975
|
958 symbol_record *gsr = global_sym_tab->lookup (nm, true); |
|
959 |
|
960 // Make sure this symbol is a variable. |
2900
|
961 |
4009
|
962 if (! gsr->is_user_variable ()) |
2975
|
963 gsr->define (octave_value ()); |
2900
|
964 |
4009
|
965 sr->alias (gsr); |
2975
|
966 } |
195
|
967 } |
|
968 } |
|
969 |
581
|
970 // Make the definition of the symbol record sr be the same as the |
|
971 // definition of the builtin variable of the same name. |
|
972 |
|
973 // Make the definition of the symbol record sr be the same as the |
3258
|
974 // definition of the builtin variable, constant, or function, or user |
|
975 // function of the same name, provided that the name has not been used |
|
976 // as a formal parameter. |
581
|
977 |
195
|
978 void |
|
979 link_to_builtin_or_function (symbol_record *sr) |
|
980 { |
4238
|
981 std::string nm = sr->name (); |
|
982 |
|
983 symbol_record *tmp_sym = 0; |
|
984 |
|
985 if (curr_parent_function) |
|
986 { |
|
987 std::string parent = curr_parent_function->function_name (); |
|
988 |
|
989 tmp_sym = fbi_sym_tab->lookup (parent + ":" + nm); |
|
990 } |
|
991 |
|
992 if (! tmp_sym) |
|
993 tmp_sym = fbi_sym_tab->lookup (nm); |
195
|
994 |
529
|
995 if (tmp_sym |
3258
|
996 && (tmp_sym->is_builtin_variable () |
|
997 || tmp_sym->is_builtin_constant () |
|
998 || tmp_sym->is_function ()) |
529
|
999 && ! tmp_sym->is_formal_parameter ()) |
|
1000 sr->alias (tmp_sym); |
195
|
1001 } |
|
1002 |
581
|
1003 // Force a link to a function in the current symbol table. This is |
|
1004 // used just after defining a function to avoid different behavior |
|
1005 // depending on whether or not the function has been evaluated after |
|
1006 // being defined. |
|
1007 // |
|
1008 // Return without doing anything if there isn't a function with the |
|
1009 // given name defined in the global symbol table. |
|
1010 |
195
|
1011 void |
3523
|
1012 force_link_to_function (const std::string& id_name) |
195
|
1013 { |
4009
|
1014 symbol_record *fsr = fbi_sym_tab->lookup (id_name, true); |
|
1015 if (fsr->is_function ()) |
195
|
1016 { |
|
1017 curr_sym_tab->clear (id_name); |
2856
|
1018 symbol_record *csr = curr_sym_tab->lookup (id_name, true); |
4009
|
1019 csr->alias (fsr); |
195
|
1020 } |
|
1021 } |
|
1022 |
2294
|
1023 DEFUN (document, args, , |
3361
|
1024 "-*- texinfo -*-\n\ |
|
1025 @deftypefn {Built-in Function} {} document (@var{symbol}, @var{text})\n\ |
|
1026 Set the documentation string for @var{symbol} to @var{text}.\n\ |
|
1027 @end deftypefn") |
593
|
1028 { |
2294
|
1029 octave_value retval; |
1755
|
1030 |
2294
|
1031 int nargin = args.length (); |
1755
|
1032 |
2294
|
1033 if (nargin == 2) |
593
|
1034 { |
3523
|
1035 std::string name = args(0).string_value (); |
593
|
1036 |
2294
|
1037 if (! error_state) |
593
|
1038 { |
3523
|
1039 std::string help = args(1).string_value (); |
593
|
1040 |
2294
|
1041 if (! error_state) |
|
1042 { |
|
1043 if (is_builtin_variable (name) |
4208
|
1044 || is_command_name (name) |
2294
|
1045 || is_mapper_function_name (name) |
|
1046 || is_builtin_function_name (name)) |
|
1047 error ("document: can't redefine help for built-in variables and functions"); |
|
1048 else |
|
1049 { |
2856
|
1050 symbol_record *sym_rec = curr_sym_tab->lookup (name); |
2294
|
1051 |
|
1052 if (sym_rec) |
|
1053 sym_rec->document (help); |
|
1054 else |
|
1055 error ("document: no such symbol `%s'", name.c_str ()); |
|
1056 } |
|
1057 } |
593
|
1058 } |
|
1059 } |
|
1060 else |
|
1061 print_usage ("document"); |
|
1062 |
|
1063 return retval; |
|
1064 } |
|
1065 |
4435
|
1066 static octave_value |
|
1067 do_who (int argc, const string_vector& argv, int return_list) |
529
|
1068 { |
4435
|
1069 octave_value retval; |
529
|
1070 |
2856
|
1071 bool show_builtins = false; |
3248
|
1072 bool show_functions = false; |
|
1073 bool show_variables = false; |
2856
|
1074 bool show_verbose = false; |
529
|
1075 |
3523
|
1076 std::string my_name = argv[0]; |
584
|
1077 |
1857
|
1078 int i; |
|
1079 for (i = 1; i < argc; i++) |
529
|
1080 { |
1755
|
1081 if (argv[i] == "-all" || argv[i] == "-a") |
529
|
1082 { |
2856
|
1083 show_builtins = true; |
|
1084 show_functions = true; |
|
1085 show_variables = true; |
529
|
1086 } |
1755
|
1087 else if (argv[i] == "-builtins" || argv[i] == "-b") |
2856
|
1088 show_builtins = true; |
1755
|
1089 else if (argv[i] == "-functions" || argv[i] == "-f") |
2856
|
1090 show_functions = true; |
1755
|
1091 else if (argv[i] == "-long" || argv[i] == "-l") |
2856
|
1092 show_verbose = true; |
1755
|
1093 else if (argv[i] == "-variables" || argv[i] == "-v") |
2856
|
1094 show_variables = true; |
1755
|
1095 else if (argv[i][0] == '-') |
|
1096 warning ("%s: unrecognized option `%s'", my_name.c_str (), |
|
1097 argv[i].c_str ()); |
529
|
1098 else |
867
|
1099 break; |
529
|
1100 } |
|
1101 |
3248
|
1102 // If no options were specified to select the type of symbol to |
|
1103 // display, then set defaults. |
|
1104 |
|
1105 if (! (show_builtins || show_functions || show_variables)) |
|
1106 { |
4208
|
1107 show_functions = at_top_level (); |
3248
|
1108 show_variables = true; |
|
1109 } |
|
1110 |
1857
|
1111 int npats = argc - i; |
|
1112 string_vector pats (npats); |
|
1113 for (int j = 0; j < npats; j++) |
|
1114 pats[j] = argv[i+j]; |
|
1115 |
1271
|
1116 // If the user specified -l and nothing else, show variables. If |
|
1117 // evaluating this at the top level, also show functions. |
529
|
1118 |
|
1119 if (show_verbose && ! (show_builtins || show_functions || show_variables)) |
|
1120 { |
4208
|
1121 show_functions = at_top_level (); |
529
|
1122 show_variables = 1; |
|
1123 } |
|
1124 |
4435
|
1125 if (return_list) |
529
|
1126 { |
4435
|
1127 string_vector names; |
|
1128 |
|
1129 if (show_builtins) |
|
1130 { |
|
1131 names.append (fbi_sym_tab->name_list |
|
1132 (pats, true, symbol_record::BUILTIN_CONSTANT, |
|
1133 SYMTAB_ALL_SCOPES)); |
|
1134 |
|
1135 names.append (fbi_sym_tab->name_list |
|
1136 (pats, true, symbol_record::BUILTIN_VARIABLE, |
|
1137 SYMTAB_ALL_SCOPES)); |
|
1138 |
|
1139 names.append (fbi_sym_tab->name_list |
|
1140 (pats, true, symbol_record::BUILTIN_FUNCTION, |
|
1141 SYMTAB_ALL_SCOPES)); |
|
1142 } |
|
1143 |
|
1144 if (show_functions) |
|
1145 { |
|
1146 names.append (fbi_sym_tab->name_list |
|
1147 (pats, true, symbol_record::DLD_FUNCTION, |
|
1148 SYMTAB_ALL_SCOPES)); |
4108
|
1149 |
4435
|
1150 names.append (fbi_sym_tab->name_list |
|
1151 (pats, true, symbol_record::USER_FUNCTION, |
|
1152 SYMTAB_ALL_SCOPES)); |
|
1153 } |
|
1154 |
|
1155 if (show_variables) |
|
1156 { |
|
1157 names.append (curr_sym_tab->name_list |
|
1158 (pats, true, symbol_record::USER_VARIABLE, |
|
1159 SYMTAB_LOCAL_SCOPE)); |
|
1160 |
|
1161 names.append (curr_sym_tab->name_list |
|
1162 (pats, true, symbol_record::USER_VARIABLE, |
|
1163 SYMTAB_GLOBAL_SCOPE)); |
|
1164 } |
|
1165 |
|
1166 if (show_verbose) |
|
1167 { |
|
1168 int len = names.length (); |
|
1169 |
|
1170 octave_value_list ovl (len, octave_value ()); |
|
1171 |
|
1172 for (int i = 0; i < len; i++) |
|
1173 ovl(i) = names(i); |
|
1174 |
|
1175 retval = Octave_map ("name", ovl); |
|
1176 } |
|
1177 else |
|
1178 retval = Cell (names); |
529
|
1179 } |
4435
|
1180 else |
529
|
1181 { |
4435
|
1182 int pad_after = 0; |
|
1183 |
|
1184 if (show_builtins) |
|
1185 { |
|
1186 pad_after += fbi_sym_tab->maybe_list |
|
1187 ("*** built-in constants:", pats, octave_stdout, |
|
1188 show_verbose, symbol_record::BUILTIN_CONSTANT, SYMTAB_ALL_SCOPES); |
|
1189 |
|
1190 pad_after += fbi_sym_tab->maybe_list |
|
1191 ("*** built-in variables:", pats, octave_stdout, |
|
1192 show_verbose, symbol_record::BUILTIN_VARIABLE, SYMTAB_ALL_SCOPES); |
|
1193 |
|
1194 pad_after += fbi_sym_tab->maybe_list |
|
1195 ("*** built-in functions:", pats, octave_stdout, |
|
1196 show_verbose, symbol_record::BUILTIN_FUNCTION, SYMTAB_ALL_SCOPES); |
|
1197 } |
529
|
1198 |
4435
|
1199 if (show_functions) |
|
1200 { |
|
1201 pad_after += fbi_sym_tab->maybe_list |
|
1202 ("*** dynamically linked functions:", pats, |
|
1203 octave_stdout, show_verbose, symbol_record::DLD_FUNCTION, |
|
1204 SYMTAB_ALL_SCOPES); |
|
1205 |
|
1206 pad_after += fbi_sym_tab->maybe_list |
|
1207 ("*** currently compiled functions:", pats, |
|
1208 octave_stdout, show_verbose, symbol_record::USER_FUNCTION, |
|
1209 SYMTAB_ALL_SCOPES); |
|
1210 } |
|
1211 |
|
1212 if (show_variables) |
|
1213 { |
|
1214 pad_after += curr_sym_tab->maybe_list |
|
1215 ("*** local user variables:", pats, octave_stdout, |
|
1216 show_verbose, symbol_record::USER_VARIABLE, SYMTAB_LOCAL_SCOPE); |
|
1217 |
|
1218 pad_after += curr_sym_tab->maybe_list |
|
1219 ("*** globally visible user variables:", pats, |
|
1220 octave_stdout, show_verbose, symbol_record::USER_VARIABLE, |
|
1221 SYMTAB_GLOBAL_SCOPE); |
|
1222 } |
|
1223 |
|
1224 if (pad_after) |
|
1225 octave_stdout << "\n"; |
529
|
1226 } |
|
1227 |
581
|
1228 return retval; |
|
1229 } |
|
1230 |
4435
|
1231 DEFCMD (who, args, nargout, |
3361
|
1232 "-*- texinfo -*-\n\ |
|
1233 @deffn {Command} who options pattern @dots{}\n\ |
|
1234 @deffnx {Command} whos options pattern @dots{}\n\ |
|
1235 List currently defined symbols matching the given patterns. The\n\ |
|
1236 following are valid options. They may be shortened to one character but\n\ |
|
1237 may not be combined.\n\ |
|
1238 \n\ |
|
1239 @table @code\n\ |
|
1240 @item -all\n\ |
|
1241 List all currently defined symbols.\n\ |
|
1242 \n\ |
|
1243 @item -builtins\n\ |
|
1244 List built-in variables and functions. This includes all currently\n\ |
|
1245 compiled function files, but does not include all function files that\n\ |
|
1246 are in the @code{LOADPATH}.\n\ |
581
|
1247 \n\ |
3361
|
1248 @item -functions\n\ |
|
1249 List user-defined functions.\n\ |
|
1250 \n\ |
|
1251 @item -long\n\ |
|
1252 Print a long listing including the type and dimensions of any symbols.\n\ |
|
1253 The symbols in the first column of output indicate whether it is\n\ |
|
1254 possible to redefine the symbol, and whether it is possible for it to be\n\ |
|
1255 cleared.\n\ |
|
1256 \n\ |
|
1257 @item -variables\n\ |
|
1258 List user-defined variables.\n\ |
|
1259 @end table\n\ |
|
1260 \n\ |
|
1261 Valid patterns are the same as described for the @code{clear} command\n\ |
|
1262 above. If no patterns are supplied, all symbols from the given category\n\ |
|
1263 are listed. By default, only user defined functions and variables\n\ |
|
1264 visible in the local scope are displayed.\n\ |
|
1265 \n\ |
|
1266 The command @kbd{whos} is equivalent to @kbd{who -long}.\n\ |
|
1267 @end deffn") |
581
|
1268 { |
4435
|
1269 octave_value retval; |
581
|
1270 |
4435
|
1271 if (nargout < 2) |
|
1272 { |
|
1273 int argc = args.length () + 1; |
|
1274 |
|
1275 string_vector argv = args.make_argv ("who"); |
1755
|
1276 |
4435
|
1277 if (error_state) |
|
1278 return retval; |
1755
|
1279 |
4435
|
1280 retval = do_who (argc, argv, nargout == 1); |
|
1281 } |
|
1282 else |
|
1283 print_usage ("who"); |
581
|
1284 |
529
|
1285 return retval; |
|
1286 } |
|
1287 |
4435
|
1288 DEFCMD (whos, args, nargout, |
3458
|
1289 "-*- texinfo -*-\n\ |
|
1290 @deffn {Command} whos options pattern @dots{}\n\ |
|
1291 See who.\n\ |
|
1292 @end deffn") |
581
|
1293 { |
4435
|
1294 octave_value retval; |
712
|
1295 |
4435
|
1296 if (nargout < 2) |
|
1297 { |
|
1298 int nargin = args.length (); |
|
1299 |
|
1300 octave_value_list tmp_args; |
|
1301 |
|
1302 for (int i = nargin; i > 0; i--) |
|
1303 tmp_args(i) = args(i-1); |
581
|
1304 |
4435
|
1305 tmp_args(0) = "-long"; |
1755
|
1306 |
4435
|
1307 int argc = tmp_args.length () + 1; |
|
1308 |
|
1309 string_vector argv = tmp_args.make_argv ("whos"); |
581
|
1310 |
4435
|
1311 if (error_state) |
|
1312 return retval; |
581
|
1313 |
4435
|
1314 retval = do_who (argc, argv, nargout == 1); |
|
1315 } |
|
1316 else |
|
1317 print_usage ("whos"); |
581
|
1318 |
|
1319 return retval; |
|
1320 } |
|
1321 |
593
|
1322 // Defining variables. |
|
1323 |
1162
|
1324 void |
2856
|
1325 bind_ans (const octave_value& val, bool print) |
1162
|
1326 { |
4009
|
1327 static symbol_record *sr = fbi_sym_tab->lookup ("ans", true); |
1162
|
1328 |
2978
|
1329 if (val.is_defined ()) |
|
1330 { |
|
1331 sr->define (val); |
1162
|
1332 |
2978
|
1333 if (print) |
|
1334 val.print_with_name (octave_stdout, "ans"); |
|
1335 } |
1162
|
1336 } |
|
1337 |
3259
|
1338 // Give a global constant a definition. This will insert the symbol |
|
1339 // in the global table if necessary. |
|
1340 |
|
1341 // How is this different than install_builtin_constant? Are both |
|
1342 // functions needed? |
|
1343 |
|
1344 void |
3523
|
1345 bind_builtin_constant (const std::string& name, const octave_value& val, |
|
1346 bool protect, bool eternal, const std::string& help) |
3259
|
1347 { |
4009
|
1348 symbol_record *sym_rec = fbi_sym_tab->lookup (name, true); |
3259
|
1349 sym_rec->unprotect (); |
|
1350 |
3523
|
1351 std::string tmp_help = help.empty () ? sym_rec->help () : help; |
3259
|
1352 |
|
1353 sym_rec->define_builtin_const (val); |
|
1354 |
|
1355 sym_rec->document (tmp_help); |
|
1356 |
|
1357 if (protect) |
|
1358 sym_rec->protect (); |
|
1359 |
|
1360 if (eternal) |
|
1361 sym_rec->make_eternal (); |
|
1362 } |
|
1363 |
593
|
1364 // Give a global variable a definition. This will insert the symbol |
|
1365 // in the global table if necessary. |
|
1366 |
|
1367 // How is this different than install_builtin_variable? Are both |
|
1368 // functions needed? |
|
1369 |
|
1370 void |
3523
|
1371 bind_builtin_variable (const std::string& varname, const octave_value& val, |
2953
|
1372 bool protect, bool eternal, |
3005
|
1373 symbol_record::change_function chg_fcn, |
3523
|
1374 const std::string& help) |
593
|
1375 { |
4009
|
1376 symbol_record *sr = fbi_sym_tab->lookup (varname, true); |
593
|
1377 |
1271
|
1378 // It is a programming error for a builtin symbol to be missing. |
|
1379 // Besides, we just inserted it, so it must be there. |
593
|
1380 |
4280
|
1381 // Use != here to avoid possible conversion to int of smaller type |
|
1382 // than the sr pointer. |
|
1383 |
|
1384 assert (sr != 0); |
593
|
1385 |
|
1386 sr->unprotect (); |
|
1387 |
1271
|
1388 // Must do this before define, since define will call the special |
|
1389 // variable function only if it knows about it, and it needs to, so |
|
1390 // that user prefs can be properly initialized. |
593
|
1391 |
3005
|
1392 if (chg_fcn) |
|
1393 sr->set_change_function (chg_fcn); |
593
|
1394 |
|
1395 sr->define_builtin_var (val); |
|
1396 |
|
1397 if (protect) |
|
1398 sr->protect (); |
|
1399 |
|
1400 if (eternal) |
|
1401 sr->make_eternal (); |
|
1402 |
1755
|
1403 sr->document (help); |
529
|
1404 } |
|
1405 |
4319
|
1406 void |
|
1407 mlock (const std::string& nm) |
|
1408 { |
|
1409 symbol_record *sr = fbi_sym_tab->lookup (nm, true); |
|
1410 |
|
1411 if (sr) |
|
1412 sr->mark_as_static (); |
|
1413 } |
|
1414 |
|
1415 void |
|
1416 munlock (const std::string& nm) |
|
1417 { |
|
1418 symbol_record *sr = fbi_sym_tab->lookup (nm); |
|
1419 |
|
1420 if (sr && sr->is_static ()) |
|
1421 sr->unmark_static (); |
|
1422 else |
|
1423 error ("munlock: %s is not locked", nm.c_str ()); |
|
1424 } |
|
1425 |
|
1426 bool |
|
1427 mislocked (const std::string& nm) |
|
1428 { |
|
1429 symbol_record *sr = fbi_sym_tab->lookup (nm); |
|
1430 |
|
1431 return (sr && sr->is_static ()); |
|
1432 } |
|
1433 |
|
1434 DEFCMD (mlock, args, , |
|
1435 "-*- texinfo -*-\n\ |
|
1436 @deftypefnx {Built-in Function} {} mlock (@var{name})\n\ |
|
1437 Lock the named function into memory. If no function is named\n\ |
|
1438 then lock in the current function.\n\ |
|
1439 @end deftypefn\n\ |
|
1440 @seealso{munlock, mislocked, and persistent}") |
|
1441 { |
|
1442 octave_value_list retval; |
|
1443 |
|
1444 if (args.length () == 1) |
|
1445 { |
|
1446 std::string name = args(0).string_value (); |
|
1447 |
|
1448 if (! error_state) |
|
1449 mlock (name); |
|
1450 else |
|
1451 error ("mlock: expecting argument to be a function name"); |
|
1452 } |
|
1453 else if (args.length () == 0) |
|
1454 { |
|
1455 if (curr_function) |
|
1456 mlock (curr_function->function_name ()); |
|
1457 else |
|
1458 error ("mlock: invalid use outside a function"); |
|
1459 } |
|
1460 else |
|
1461 print_usage ("mlock"); |
|
1462 |
|
1463 return retval; |
|
1464 } |
|
1465 |
|
1466 DEFCMD (munlock, args, , |
|
1467 "-*- texinfo -*-\n\ |
|
1468 @deftypefn {Built-in Function} {} munlock (@var{fcn})\n\ |
|
1469 Unlock the named function. If no function is named\n\ |
|
1470 then unlock the current function.\n\ |
|
1471 @end deftypefn\n\ |
|
1472 @seealso{mlock, mislocked, and persistent}") |
|
1473 { |
|
1474 octave_value_list retval; |
|
1475 |
|
1476 if (args.length() == 1) |
|
1477 { |
|
1478 std::string name = args(0).string_value (); |
|
1479 |
|
1480 if (! error_state) |
|
1481 munlock (name); |
|
1482 else |
|
1483 error ("munlock: expecting argument to be a function name"); |
|
1484 } |
|
1485 else if (args.length () == 0) |
|
1486 { |
|
1487 if (curr_function) |
|
1488 mlock (curr_function->function_name ()); |
|
1489 else |
|
1490 error ("munlock: invalid use outside a function"); |
|
1491 } |
|
1492 else |
|
1493 print_usage ("munlock"); |
|
1494 |
|
1495 return retval; |
|
1496 } |
|
1497 |
|
1498 |
|
1499 DEFCMD (mislocked, args, , |
|
1500 "-*- texinfo -*-\n\ |
|
1501 @deftypefn {Built-in Function} {} mislocked (@var{fcn})\n\ |
|
1502 Return true if the named function is locked. If no function is named\n\ |
|
1503 then return true if the current function is locked.\n\ |
|
1504 @end deftypefn\n\ |
|
1505 @seealso{mlock, munlock, and persistent}") |
|
1506 { |
|
1507 octave_value retval; |
|
1508 |
|
1509 if (args.length() == 1) |
|
1510 { |
|
1511 std::string name = args(0).string_value (); |
|
1512 |
|
1513 if (! error_state) |
|
1514 retval = mislocked (name); |
|
1515 else |
|
1516 error ("mislocked: expecting argument to be a function name"); |
|
1517 } |
|
1518 else if (args.length () == 0) |
|
1519 { |
|
1520 if (curr_function) |
|
1521 retval = mislocked (curr_function->function_name ()); |
|
1522 else |
|
1523 error ("mislocked: invalid use outside a function"); |
|
1524 } |
|
1525 else |
|
1526 print_usage ("mislocked"); |
|
1527 |
|
1528 return retval; |
|
1529 } |
|
1530 |
593
|
1531 // Deleting names from the symbol tables. |
|
1532 |
3681
|
1533 static inline bool |
4009
|
1534 name_matches_any_pattern (const std::string& nm, |
|
1535 const string_vector& argv, int argc, int idx) |
3681
|
1536 { |
|
1537 bool retval = false; |
|
1538 |
|
1539 for (int k = idx; k < argc; k++) |
|
1540 { |
|
1541 std::string patstr = argv[k]; |
|
1542 |
|
1543 if (! patstr.empty ()) |
|
1544 { |
|
1545 glob_match pattern (patstr); |
|
1546 |
|
1547 if (pattern.match (nm)) |
|
1548 { |
|
1549 retval = true; |
|
1550 break; |
|
1551 } |
|
1552 } |
|
1553 } |
|
1554 |
|
1555 return retval; |
|
1556 } |
|
1557 |
4009
|
1558 static inline bool |
|
1559 is_local_variable (const std::string& nm) |
|
1560 { |
|
1561 symbol_record *sr = curr_sym_tab->lookup (nm); |
|
1562 |
|
1563 return (sr && sr->is_variable ()); |
|
1564 } |
|
1565 |
|
1566 static inline void |
|
1567 maybe_warn_exclusive (bool exclusive) |
|
1568 { |
|
1569 if (exclusive) |
|
1570 warning ("clear: ignoring --exclusive option"); |
|
1571 } |
|
1572 |
|
1573 static inline void |
|
1574 do_clear_all (void) |
|
1575 { |
|
1576 curr_sym_tab->clear (); |
|
1577 fbi_sym_tab->clear_functions (); |
|
1578 global_sym_tab->clear (); |
|
1579 } |
|
1580 |
|
1581 static inline void |
|
1582 do_clear_functions (void) |
|
1583 { |
|
1584 curr_sym_tab->clear_functions (); |
|
1585 fbi_sym_tab->clear_functions (); |
|
1586 } |
|
1587 |
|
1588 static inline void |
|
1589 do_clear_globals (void) |
|
1590 { |
|
1591 curr_sym_tab->clear_globals (); |
|
1592 global_sym_tab->clear (); |
|
1593 } |
|
1594 |
|
1595 static inline void |
|
1596 do_clear_variables (void) |
|
1597 { |
|
1598 curr_sym_tab->clear (); |
|
1599 } |
|
1600 |
|
1601 static inline bool |
|
1602 do_clear_function (const std::string& nm) |
|
1603 { |
|
1604 bool b1 = curr_sym_tab->clear_function (nm); |
|
1605 |
|
1606 bool b2 = fbi_sym_tab->clear_function (nm); |
|
1607 |
|
1608 return b1 || b2; |
|
1609 } |
|
1610 |
|
1611 static inline bool |
|
1612 do_clear_global (const std::string& nm) |
|
1613 { |
|
1614 bool b1 = curr_sym_tab->clear_global (nm); |
|
1615 |
|
1616 bool b2 = global_sym_tab->clear_variable (nm); |
|
1617 |
|
1618 return b1 || b2; |
|
1619 } |
|
1620 |
|
1621 static inline bool |
|
1622 do_clear_variable (const std::string& nm) |
|
1623 { |
|
1624 return curr_sym_tab->clear_variable (nm); |
|
1625 } |
|
1626 |
|
1627 static inline bool |
|
1628 do_clear_symbol (const std::string& nm) |
|
1629 { |
|
1630 bool cleared = curr_sym_tab->clear_variable (nm); |
|
1631 |
|
1632 if (! cleared) |
|
1633 cleared = do_clear_function (nm); |
|
1634 |
|
1635 return cleared; |
|
1636 } |
|
1637 |
|
1638 static inline bool |
|
1639 do_clear_function_pattern (const std::string& pat) |
|
1640 { |
|
1641 bool b1 = curr_sym_tab->clear_function_pattern (pat); |
|
1642 |
|
1643 bool b2 = fbi_sym_tab->clear_function_pattern (pat); |
|
1644 |
|
1645 return b1 || b2; |
|
1646 } |
|
1647 |
|
1648 static inline bool |
|
1649 do_clear_global_pattern (const std::string& pat) |
|
1650 { |
|
1651 bool b1 = curr_sym_tab->clear_global_pattern (pat); |
|
1652 |
|
1653 bool b2 = global_sym_tab->clear_variable_pattern (pat); |
|
1654 |
|
1655 return b1 || b2; |
|
1656 } |
|
1657 |
|
1658 static inline bool |
|
1659 do_clear_variable_pattern (const std::string& pat) |
|
1660 { |
|
1661 return curr_sym_tab->clear_variable_pattern (pat); |
|
1662 } |
|
1663 |
|
1664 static inline bool |
|
1665 do_clear_symbol_pattern (const std::string& pat) |
|
1666 { |
|
1667 // XXX FIXME XXX -- if we have a variable v1 and a function v2 and |
|
1668 // someone says clear v*, we will clear the variable but not the |
|
1669 // function. Is that really what should happen? (I think it is |
|
1670 // what Matlab does.) |
|
1671 |
|
1672 bool cleared = curr_sym_tab->clear_variable_pattern (pat); |
|
1673 |
|
1674 if (! cleared) |
|
1675 cleared = do_clear_function_pattern (pat); |
|
1676 |
|
1677 return cleared; |
|
1678 } |
|
1679 |
|
1680 static inline void |
|
1681 do_clear_functions (const string_vector& argv, int argc, int idx, |
|
1682 bool exclusive = false) |
|
1683 { |
|
1684 if (idx == argc) |
|
1685 do_clear_functions (); |
|
1686 else |
|
1687 { |
|
1688 if (exclusive) |
|
1689 { |
|
1690 string_vector lfcns = curr_sym_tab->user_function_name_list (); |
|
1691 |
|
1692 int lcount = lfcns.length (); |
|
1693 |
|
1694 for (int i = 0; i < lcount; i++) |
|
1695 { |
|
1696 std::string nm = lfcns[i]; |
|
1697 |
|
1698 if (! name_matches_any_pattern (nm, argv, argc, idx)) |
|
1699 do_clear_function (nm); |
|
1700 } |
|
1701 |
|
1702 string_vector fcns = fbi_sym_tab->user_function_name_list (); |
|
1703 |
|
1704 int fcount = fcns.length (); |
|
1705 |
|
1706 for (int i = 0; i < fcount; i++) |
|
1707 { |
|
1708 std::string nm = fcns[i]; |
|
1709 |
|
1710 if (! name_matches_any_pattern (nm, argv, argc, idx)) |
|
1711 do_clear_function (nm); |
|
1712 } |
|
1713 } |
|
1714 else |
|
1715 { |
|
1716 while (idx < argc) |
|
1717 do_clear_function_pattern (argv[idx++]); |
|
1718 } |
|
1719 } |
|
1720 } |
|
1721 |
|
1722 static inline void |
|
1723 do_clear_globals (const string_vector& argv, int argc, int idx, |
|
1724 bool exclusive = false) |
|
1725 { |
|
1726 if (idx == argc) |
|
1727 do_clear_globals (); |
|
1728 else |
|
1729 { |
|
1730 if (exclusive) |
|
1731 { |
|
1732 string_vector lvars = curr_sym_tab->global_variable_name_list (); |
|
1733 |
|
1734 int lcount = lvars.length (); |
|
1735 |
|
1736 for (int i = 0; i < lcount; i++) |
|
1737 { |
|
1738 std::string nm = lvars[i]; |
|
1739 |
|
1740 if (! name_matches_any_pattern (nm, argv, argc, idx)) |
|
1741 do_clear_global (nm); |
|
1742 } |
|
1743 |
|
1744 string_vector gvars = global_sym_tab->global_variable_name_list (); |
|
1745 |
|
1746 int gcount = gvars.length (); |
|
1747 |
|
1748 for (int i = 0; i < gcount; i++) |
|
1749 { |
|
1750 std::string nm = gvars[i]; |
|
1751 |
|
1752 if (! name_matches_any_pattern (nm, argv, argc, idx)) |
|
1753 do_clear_global (nm); |
|
1754 } |
|
1755 } |
|
1756 else |
|
1757 { |
|
1758 while (idx < argc) |
|
1759 do_clear_global_pattern (argv[idx++]); |
|
1760 } |
|
1761 } |
|
1762 } |
|
1763 |
|
1764 static inline void |
|
1765 do_clear_variables (const string_vector& argv, int argc, int idx, |
|
1766 bool exclusive = false) |
|
1767 { |
|
1768 if (idx == argc) |
|
1769 do_clear_variables (); |
|
1770 else |
|
1771 { |
|
1772 if (exclusive) |
|
1773 { |
|
1774 string_vector lvars = curr_sym_tab->variable_name_list (); |
|
1775 |
|
1776 int lcount = lvars.length (); |
|
1777 |
|
1778 for (int i = 0; i < lcount; i++) |
|
1779 { |
|
1780 std::string nm = lvars[i]; |
|
1781 |
|
1782 if (! name_matches_any_pattern (nm, argv, argc, idx)) |
|
1783 do_clear_variable (nm); |
|
1784 } |
|
1785 } |
|
1786 else |
|
1787 { |
|
1788 while (idx < argc) |
|
1789 do_clear_variable_pattern (argv[idx++]); |
|
1790 } |
|
1791 } |
|
1792 } |
|
1793 |
|
1794 static inline void |
|
1795 do_clear_symbols (const string_vector& argv, int argc, int idx, |
|
1796 bool exclusive = false) |
|
1797 { |
|
1798 if (idx == argc) |
|
1799 do_clear_variables (); |
|
1800 else |
|
1801 { |
|
1802 if (exclusive) |
|
1803 { |
|
1804 // XXX FIXME XXX -- is this really what we want, or do we |
|
1805 // somehow want to only clear the functions that are not |
|
1806 // shadowed by local variables? It seems that would be a |
|
1807 // bit harder to do. |
|
1808 |
|
1809 do_clear_variables (argv, argc, idx, exclusive); |
|
1810 do_clear_functions (argv, argc, idx, exclusive); |
|
1811 } |
|
1812 else |
|
1813 { |
|
1814 while (idx < argc) |
|
1815 do_clear_symbol_pattern (argv[idx++]); |
|
1816 } |
|
1817 } |
|
1818 } |
|
1819 |
|
1820 static void |
|
1821 do_matlab_compatible_clear (const string_vector& argv, int argc, int idx) |
|
1822 { |
|
1823 // This is supposed to be mostly Matlab compatible. |
|
1824 |
|
1825 for (; idx < argc; idx++) |
|
1826 { |
|
1827 if (argv[idx] == "all" && ! is_local_variable ("all")) |
|
1828 { |
|
1829 do_clear_all (); |
|
1830 } |
|
1831 else if (argv[idx] == "functions" && ! is_local_variable ("functions")) |
|
1832 { |
|
1833 do_clear_functions (argv, argc, ++idx); |
|
1834 } |
|
1835 else if (argv[idx] == "global" && ! is_local_variable ("global")) |
|
1836 { |
|
1837 do_clear_globals (argv, argc, ++idx); |
|
1838 } |
|
1839 else if (argv[idx] == "variables" && ! is_local_variable ("variables")) |
|
1840 { |
|
1841 do_clear_variables (); |
|
1842 } |
|
1843 else |
|
1844 { |
|
1845 do_clear_symbol_pattern (argv[idx]); |
|
1846 } |
|
1847 } |
|
1848 } |
|
1849 |
|
1850 #define CLEAR_OPTION_ERROR(cond) \ |
|
1851 do \ |
|
1852 { \ |
|
1853 if (cond) \ |
|
1854 { \ |
|
1855 print_usage ("clear"); \ |
|
1856 return retval; \ |
|
1857 } \ |
|
1858 } \ |
|
1859 while (0) |
|
1860 |
4208
|
1861 DEFCMD (clear, args, , |
3361
|
1862 "-*- texinfo -*-\n\ |
|
1863 @deffn {Command} clear [-x] pattern @dots{}\n\ |
|
1864 Delete the names matching the given patterns from the symbol table. The\n\ |
|
1865 pattern may contain the following special characters:\n\ |
4016
|
1866 \n\ |
3361
|
1867 @table @code\n\ |
|
1868 @item ?\n\ |
|
1869 Match any single character.\n\ |
668
|
1870 \n\ |
3361
|
1871 @item *\n\ |
|
1872 Match zero or more characters.\n\ |
|
1873 \n\ |
|
1874 @item [ @var{list} ]\n\ |
|
1875 Match the list of characters specified by @var{list}. If the first\n\ |
|
1876 character is @code{!} or @code{^}, match all characters except those\n\ |
|
1877 specified by @var{list}. For example, the pattern @samp{[a-zA-Z]} will\n\ |
|
1878 match all lower and upper case alphabetic characters.\n\ |
|
1879 @end table\n\ |
|
1880 \n\ |
|
1881 For example, the command\n\ |
593
|
1882 \n\ |
3361
|
1883 @example\n\ |
|
1884 clear foo b*r\n\ |
|
1885 @end example\n\ |
|
1886 \n\ |
|
1887 @noindent\n\ |
|
1888 clears the name @code{foo} and all names that begin with the letter\n\ |
|
1889 @code{b} and end with the letter @code{r}.\n\ |
668
|
1890 \n\ |
3361
|
1891 If @code{clear} is called without any arguments, all user-defined\n\ |
|
1892 variables (local and global) are cleared from the symbol table. If\n\ |
|
1893 @code{clear} is called with at least one argument, only the visible\n\ |
|
1894 names matching the arguments are cleared. For example, suppose you have\n\ |
|
1895 defined a function @code{foo}, and then hidden it by performing the\n\ |
|
1896 assignment @code{foo = 2}. Executing the command @kbd{clear foo} once\n\ |
|
1897 will clear the variable definition and restore the definition of\n\ |
|
1898 @code{foo} as a function. Executing @kbd{clear foo} a second time will\n\ |
|
1899 clear the function definition.\n\ |
|
1900 \n\ |
|
1901 With -x, clear the variables that don't match the patterns.\n\ |
|
1902 \n\ |
|
1903 This command may not be used within a function body.\n\ |
|
1904 @end deffn") |
529
|
1905 { |
2086
|
1906 octave_value_list retval; |
593
|
1907 |
1755
|
1908 int argc = args.length () + 1; |
593
|
1909 |
1968
|
1910 string_vector argv = args.make_argv ("clear"); |
1755
|
1911 |
4009
|
1912 if (! error_state) |
529
|
1913 { |
4009
|
1914 if (argc == 1) |
593
|
1915 { |
4009
|
1916 do_clear_variables (); |
3681
|
1917 } |
|
1918 else |
|
1919 { |
4009
|
1920 int idx = 0; |
|
1921 |
|
1922 bool clear_all = false; |
|
1923 bool clear_functions = false; |
|
1924 bool clear_globals = false; |
|
1925 bool clear_variables = false; |
|
1926 bool exclusive = false; |
|
1927 bool have_dash_option = false; |
3681
|
1928 |
4009
|
1929 while (++idx < argc) |
|
1930 { |
4010
|
1931 if (argv[idx] == "-all" || argv[idx] == "-a") |
593
|
1932 { |
4009
|
1933 CLEAR_OPTION_ERROR (have_dash_option && ! exclusive); |
3681
|
1934 |
4009
|
1935 have_dash_option = true; |
|
1936 clear_all = true; |
|
1937 } |
4010
|
1938 else if (argv[idx] == "-exclusive" || argv[idx] == "-x") |
4009
|
1939 { |
|
1940 have_dash_option = true; |
|
1941 exclusive = true; |
|
1942 } |
4010
|
1943 else if (argv[idx] == "-functions" || argv[idx] == "-f") |
4009
|
1944 { |
|
1945 CLEAR_OPTION_ERROR (have_dash_option && ! exclusive); |
3681
|
1946 |
4009
|
1947 have_dash_option = true; |
|
1948 clear_functions = true; |
|
1949 } |
4010
|
1950 else if (argv[idx] == "-global" || argv[idx] == "-g") |
4009
|
1951 { |
|
1952 CLEAR_OPTION_ERROR (have_dash_option && ! exclusive); |
|
1953 |
|
1954 have_dash_option = true; |
|
1955 clear_globals = true; |
|
1956 } |
4010
|
1957 else if (argv[idx] == "-variables" || argv[idx] == "-v") |
4009
|
1958 { |
|
1959 CLEAR_OPTION_ERROR (have_dash_option && ! exclusive); |
3681
|
1960 |
4009
|
1961 have_dash_option = true; |
|
1962 clear_variables = true; |
|
1963 } |
|
1964 else |
|
1965 break; |
|
1966 } |
3681
|
1967 |
4224
|
1968 if (idx <= argc) |
4009
|
1969 { |
|
1970 if (! have_dash_option) |
|
1971 { |
|
1972 do_matlab_compatible_clear (argv, argc, idx); |
|
1973 } |
|
1974 else |
|
1975 { |
|
1976 if (clear_all) |
3681
|
1977 { |
4009
|
1978 maybe_warn_exclusive (exclusive); |
3681
|
1979 |
4009
|
1980 if (++idx < argc) |
|
1981 warning |
4010
|
1982 ("clear: ignoring extra arguments after -all"); |
3681
|
1983 |
4009
|
1984 curr_sym_tab->clear (); |
|
1985 fbi_sym_tab->clear_functions (); |
|
1986 global_sym_tab->clear (); |
|
1987 } |
|
1988 else if (clear_functions) |
|
1989 { |
|
1990 do_clear_functions (argv, argc, idx, exclusive); |
|
1991 } |
|
1992 else if (clear_globals) |
593
|
1993 { |
4009
|
1994 do_clear_globals (argv, argc, idx, exclusive); |
|
1995 } |
|
1996 else if (clear_variables) |
|
1997 { |
|
1998 do_clear_variables (argv, argc, idx, exclusive); |
|
1999 } |
|
2000 else |
|
2001 { |
|
2002 do_clear_symbols (argv, argc, idx, exclusive); |
593
|
2003 } |
|
2004 } |
|
2005 } |
|
2006 } |
|
2007 } |
|
2008 |
|
2009 return retval; |
529
|
2010 } |
|
2011 |
3933
|
2012 DEFUN (__print_symtab_info__, args, , |
3446
|
2013 "-*- texinfo -*-\n\ |
3933
|
2014 @deftypefn {Built-in Function} {} __print_symtab_info__ ()\n\ |
3446
|
2015 Print raw symbol table statistices.\n\ |
|
2016 @end deftypefn") |
3005
|
2017 { |
|
2018 octave_value_list retval; |
|
2019 |
|
2020 int nargin = args.length (); |
|
2021 |
|
2022 if (nargin == 1) |
|
2023 { |
3523
|
2024 std::string arg = args(0).string_value (); |
3005
|
2025 |
4009
|
2026 if (arg == "fbi") |
|
2027 fbi_sym_tab->print_info (octave_stdout); |
|
2028 else if (arg == "global") |
3933
|
2029 global_sym_tab->print_info (octave_stdout); |
|
2030 else if (arg == "top-level") |
|
2031 top_level_sym_tab->print_info (octave_stdout); |
3005
|
2032 else |
3933
|
2033 { |
4009
|
2034 symbol_record *fsr = fbi_sym_tab->lookup (arg, true); |
3933
|
2035 |
4009
|
2036 if (fsr && fsr->is_user_function ()) |
3933
|
2037 { |
4009
|
2038 octave_value tmp = fsr->def (); |
3933
|
2039 const octave_value& rep = tmp.get_rep (); |
|
2040 |
|
2041 const octave_user_function& fcn |
|
2042 = static_cast<const octave_user_function&> (rep); |
|
2043 |
|
2044 fcn.print_symtab_info (octave_stdout); |
|
2045 } |
|
2046 else |
|
2047 error ("no user-defined function named %s", arg.c_str ()); |
|
2048 } |
3005
|
2049 } |
|
2050 else if (nargin == 0) |
3933
|
2051 curr_sym_tab->print_info (octave_stdout); |
3005
|
2052 else |
3933
|
2053 print_usage ("__print_symtab_info__"); |
3005
|
2054 |
|
2055 return retval; |
|
2056 } |
|
2057 |
3933
|
2058 DEFUN (__print_symbol_info__, args, , |
3446
|
2059 "-*- texinfo -*-\n\ |
|
2060 @deftypefn {Built-in Function} {} __dump_symbol_info__ (@var{name})\n\ |
3548
|
2061 Print symbol table information for the symbol @var{name}.\n\ |
3446
|
2062 @end deftypefn") |
3239
|
2063 { |
|
2064 octave_value_list retval; |
|
2065 |
|
2066 int nargin = args.length (); |
|
2067 |
|
2068 if (nargin == 1) |
|
2069 { |
3523
|
2070 std::string symbol_name = args(0).string_value (); |
3239
|
2071 |
|
2072 if (! error_state) |
|
2073 { |
|
2074 symbol_record *sr = curr_sym_tab->lookup (symbol_name); |
|
2075 |
|
2076 if (sr) |
3933
|
2077 sr->print_info (octave_stdout); |
3239
|
2078 else |
3933
|
2079 error ("__print_symbol_info__: symbol %s not found", |
3239
|
2080 symbol_name.c_str ()); |
|
2081 } |
|
2082 else |
3933
|
2083 print_usage ("__print_symbol_info__"); |
3239
|
2084 } |
|
2085 else |
3933
|
2086 print_usage ("__print_symbol_info__"); |
3239
|
2087 |
|
2088 return retval; |
|
2089 } |
|
2090 |
3016
|
2091 // XXX FIXME XXX -- some of these should do their own checking to be |
|
2092 // able to provide more meaningful warning or error messages. |
|
2093 |
|
2094 static int |
|
2095 ignore_function_time_stamp (void) |
|
2096 { |
|
2097 int pref = 0; |
|
2098 |
3523
|
2099 std::string val = builtin_string_variable ("ignore_function_time_stamp"); |
3016
|
2100 |
|
2101 if (! val.empty ()) |
|
2102 { |
3565
|
2103 if (val == "all") |
3016
|
2104 pref = 2; |
3565
|
2105 else if (val == "system") |
3016
|
2106 pref = 1; |
|
2107 } |
|
2108 |
|
2109 Vignore_function_time_stamp = pref; |
|
2110 |
|
2111 return 0; |
|
2112 } |
|
2113 |
|
2114 // XXX FIXME XXX -- there still may be better places for some of these |
|
2115 // to be defined. |
|
2116 |
|
2117 void |
|
2118 symbols_of_variables (void) |
|
2119 { |
3258
|
2120 DEFVAR (ans, , 0, |
3372
|
2121 "-*- texinfo -*-\n\ |
|
2122 @defvr {Built-in Variable} ans\n\ |
|
2123 This variable holds the most recently computed result that was not\n\ |
|
2124 explicitly assigned to a variable. For example, after the expression\n\ |
|
2125 \n\ |
|
2126 @example\n\ |
|
2127 3^2 + 4^2\n\ |
|
2128 @end example\n\ |
|
2129 \n\ |
|
2130 @noindent\n\ |
|
2131 is evaluated, the value of @code{ans} is 25.\n\ |
|
2132 @end defvr"); |
3016
|
2133 |
3258
|
2134 DEFVAR (ignore_function_time_stamp, "system", ignore_function_time_stamp, |
3371
|
2135 "-*- texinfo -*-\n\ |
|
2136 @defvr {Built-in Variable} ignore_function_time_stamp\n\ |
|
2137 This variable can be used to prevent Octave from making the system call\n\ |
|
2138 @code{stat} each time it looks up functions defined in function files.\n\ |
|
2139 If @code{ignore_function_time_stamp} to @code{\"system\"}, Octave will not\n\ |
|
2140 automatically recompile function files in subdirectories of\n\ |
|
2141 @file{@var{octave-home}/lib/@var{version}} if they have changed since\n\ |
|
2142 they were last compiled, but will recompile other function files in the\n\ |
|
2143 @code{LOADPATH} if they change. If set to @code{\"all\"}, Octave will not\n\ |
|
2144 recompile any function files unless their definitions are removed with\n\ |
|
2145 @code{clear}. For any other value of @code{ignore_function_time_stamp},\n\ |
|
2146 Octave will always check to see if functions defined in function files\n\ |
|
2147 need to recompiled. The default value of @code{ignore_function_time_stamp} is\n\ |
|
2148 @code{\"system\"}.\n\ |
|
2149 @end defvr"); |
3016
|
2150 } |
|
2151 |
1
|
2152 /* |
|
2153 ;;; Local Variables: *** |
|
2154 ;;; mode: C++ *** |
|
2155 ;;; End: *** |
|
2156 */ |