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 { |
4016
|
606 std::string file_name = fcn_file_in_path (name); |
1755
|
607 |
4016
|
608 if ((type == "any" || type == "file") && ! file_name.empty ()) |
|
609 { |
|
610 retval = 2; |
|
611 } |
|
612 } |
|
613 |
|
614 if (! retval) |
|
615 { |
|
616 std::string file_name = oct_file_in_path (name); |
|
617 |
|
618 if ((type == "any" || type == "file") && ! file_name.empty ()) |
593
|
619 { |
4016
|
620 retval = 3; |
593
|
621 } |
4016
|
622 } |
|
623 |
|
624 if (! retval) |
|
625 { |
|
626 std::string file_name = file_in_path (name, ""); |
|
627 |
|
628 if (file_name.empty ()) |
|
629 file_name = name; |
|
630 |
|
631 file_stat fs (file_name); |
|
632 |
|
633 if (fs) |
593
|
634 { |
4016
|
635 if ((type == "any" || type == "file") |
|
636 && fs.is_reg ()) |
1421
|
637 { |
4016
|
638 retval = 2; |
1421
|
639 } |
4016
|
640 else if ((type == "any" || type == "dir") |
|
641 && fs.is_dir ()) |
1421
|
642 { |
4016
|
643 retval = 7; |
1421
|
644 } |
593
|
645 } |
|
646 } |
|
647 |
|
648 return retval; |
|
649 } |
|
650 |
4016
|
651 DEFUN (exist, args, , |
|
652 "-*- texinfo -*-\n\ |
|
653 @deftypefn {Built-in Function} {} exist (@var{name}, @var{type})\n\ |
|
654 Return 1 if the name exists as a variable, 2 if the name (after\n\ |
|
655 appending @samp{.m}) is a function file in Octave's LOADPATH, 3 if the\n\ |
|
656 name is a @samp{.oct} file in Octave's LOADPATH, 5 if the name is a\n\ |
|
657 built-in function, 7 if the name is a directory, 101 if the name is\n\ |
|
658 a built-in variable, 102 if the name is a built-in constant, or 103\n\ |
|
659 if the name is a function not associated with a file (entered on\n\ |
|
660 the command line).\n\ |
|
661 \n\ |
|
662 Otherwise, return 0.\n\ |
|
663 \n\ |
|
664 This function also returns 2 if a regular file called @var{name}\n\ |
|
665 exists in Octave's @code{LOADPATH}. If you want information about\n\ |
|
666 other types of files, you should use some combination of the functions\n\ |
|
667 @code{file_in_path} and @code{stat} instead.\n\ |
|
668 \n\ |
|
669 If the optional argument @var{type} is supplied, check only for\n\ |
|
670 symbols of the specified type. Valid types are\n\ |
|
671 \n\ |
|
672 @table @samp\n\ |
|
673 @item \"var\"\n\ |
|
674 Check only for variables.\n\ |
|
675 @item \"builtin\"\n\ |
|
676 Check only for built-in functions.\n\ |
|
677 @item \"file\"\n\ |
|
678 Check only for files.\n\ |
|
679 @item \"dir\"\n\ |
|
680 Check only for directories.\n\ |
|
681 @end table\n\ |
|
682 @end deftypefn") |
|
683 { |
4233
|
684 octave_value retval = false; |
4016
|
685 |
|
686 int nargin = args.length (); |
|
687 |
|
688 if (nargin == 1 || nargin == 2) |
|
689 { |
|
690 std::string name = args(0).string_value (); |
|
691 |
|
692 if (! error_state) |
|
693 { |
|
694 std::string type |
|
695 = (nargin == 2) ? args(1).string_value () : std::string ("any"); |
|
696 |
|
697 if (! error_state) |
4233
|
698 retval = symbol_exist (name, type); |
4016
|
699 else |
|
700 error ("exist: expecting second argument to be a string"); |
|
701 } |
|
702 else |
|
703 error ("exist: expecting first argument to be a string"); |
|
704 } |
|
705 else |
|
706 print_usage ("exist"); |
|
707 |
|
708 return retval; |
|
709 } |
|
710 |
581
|
711 // Is there a corresponding function file that is newer than the |
|
712 // symbol definition? |
|
713 |
2926
|
714 static bool |
1
|
715 symbol_out_of_date (symbol_record *sr) |
|
716 { |
2926
|
717 bool retval = false; |
195
|
718 |
2926
|
719 if (Vignore_function_time_stamp != 2 && sr) |
1
|
720 { |
2975
|
721 octave_value ans = sr->def (); |
|
722 |
3022
|
723 octave_function *tmp = ans.function_value (true); |
2975
|
724 |
3022
|
725 if (tmp) |
|
726 { |
3523
|
727 std::string ff = tmp->fcn_file_name (); |
1755
|
728 |
3022
|
729 if (! (ff.empty () |
|
730 || (Vignore_function_time_stamp |
|
731 && tmp->is_system_fcn_file ()))) |
|
732 { |
3165
|
733 if (tmp->time_checked () < Vlast_prompt_time) |
|
734 { |
|
735 time_t tp = tmp->time_parsed (); |
2975
|
736 |
3523
|
737 std::string fname; |
3325
|
738 |
|
739 if (tmp->is_dld_function ()) |
|
740 fname = ff; |
|
741 else |
|
742 fname = fcn_file_in_path (ff); |
1755
|
743 |
3255
|
744 tmp->mark_fcn_file_up_to_date (octave_time ()); |
3165
|
745 |
|
746 file_stat fs (fname); |
3022
|
747 |
3165
|
748 if (fs && fs.is_newer (tp)) |
|
749 retval = true; |
|
750 } |
1
|
751 } |
|
752 } |
|
753 } |
2926
|
754 |
|
755 return retval; |
1
|
756 } |
|
757 |
1827
|
758 bool |
2856
|
759 lookup (symbol_record *sym_rec, bool exec_script) |
581
|
760 { |
1827
|
761 bool script_executed = false; |
581
|
762 |
|
763 if (! sym_rec->is_linked_to_global ()) |
|
764 { |
|
765 if (sym_rec->is_defined ()) |
|
766 { |
|
767 if (sym_rec->is_function () && symbol_out_of_date (sym_rec)) |
1271
|
768 script_executed = load_fcn_from_file (sym_rec, exec_script); |
581
|
769 } |
|
770 else if (! sym_rec->is_formal_parameter ()) |
|
771 { |
|
772 link_to_builtin_or_function (sym_rec); |
1271
|
773 |
581
|
774 if (! sym_rec->is_defined ()) |
1271
|
775 script_executed = load_fcn_from_file (sym_rec, exec_script); |
581
|
776 else if (sym_rec->is_function () && symbol_out_of_date (sym_rec)) |
1271
|
777 script_executed = load_fcn_from_file (sym_rec, exec_script); |
581
|
778 } |
|
779 } |
|
780 |
1271
|
781 return script_executed; |
581
|
782 } |
|
783 |
|
784 // Get the symbol record for the given name that is visible in the |
|
785 // current scope. Reread any function definitions that appear to be |
|
786 // out of date. If a function is available in a file but is not |
|
787 // currently loaded, this will load it and insert the name in the |
|
788 // current symbol table. |
|
789 |
|
790 symbol_record * |
3523
|
791 lookup_by_name (const std::string& nm, bool exec_script) |
581
|
792 { |
2856
|
793 symbol_record *sym_rec = curr_sym_tab->lookup (nm, true); |
581
|
794 |
|
795 lookup (sym_rec, exec_script); |
|
796 |
|
797 return sym_rec; |
|
798 } |
|
799 |
4342
|
800 octave_function * |
|
801 lookup_function (const std::string& nm) |
|
802 { |
|
803 octave_function *retval = 0; |
|
804 |
|
805 symbol_record *sr = 0; |
|
806 |
|
807 if (curr_parent_function) |
|
808 { |
|
809 std::string parent = curr_parent_function->function_name (); |
|
810 |
|
811 sr = fbi_sym_tab->lookup (parent + ":" + nm); |
|
812 } |
|
813 |
|
814 if (! sr || ! sr->is_function ()) |
|
815 { |
|
816 sr = fbi_sym_tab->lookup (nm, true); |
|
817 |
|
818 if (sr && ! sr->is_function ()) |
|
819 load_fcn_from_file (sr, false); |
|
820 } |
|
821 |
|
822 if (sr) |
|
823 { |
|
824 octave_value v = sr->def (); |
|
825 |
|
826 if (v.is_function ()) |
|
827 retval = v.function_value (); |
|
828 } |
|
829 |
|
830 return retval; |
|
831 } |
|
832 |
2849
|
833 octave_value |
3523
|
834 get_global_value (const std::string& nm) |
2849
|
835 { |
|
836 octave_value retval; |
|
837 |
|
838 symbol_record *sr = global_sym_tab->lookup (nm); |
|
839 |
|
840 if (sr) |
|
841 { |
2975
|
842 octave_value sr_def = sr->def (); |
2849
|
843 |
2975
|
844 if (sr_def.is_undefined ()) |
2849
|
845 error ("get_global_by_name: undefined symbol `%s'", nm.c_str ()); |
3088
|
846 else |
|
847 retval = sr_def; |
2849
|
848 } |
|
849 else |
|
850 error ("get_global_by_name: unknown symbol `%s'", nm.c_str ()); |
|
851 |
|
852 return retval; |
|
853 } |
|
854 |
|
855 void |
3523
|
856 set_global_value (const std::string& nm, const octave_value& val) |
2849
|
857 { |
2856
|
858 symbol_record *sr = global_sym_tab->lookup (nm, true); |
2849
|
859 |
|
860 if (sr) |
|
861 sr->define (val); |
|
862 else |
|
863 panic_impossible (); |
|
864 } |
|
865 |
593
|
866 // Variable values. |
195
|
867 |
581
|
868 // Look for the given name in the global symbol table. If it refers |
|
869 // to a string, return a new copy. If not, return 0; |
|
870 |
3523
|
871 std::string |
|
872 builtin_string_variable (const std::string& name) |
1
|
873 { |
4009
|
874 symbol_record *sr = fbi_sym_tab->lookup (name); |
195
|
875 |
4329
|
876 // It is a programming error to look for builtins that aren't. |
195
|
877 |
4280
|
878 // Use != here to avoid possible conversion to int of smaller type |
|
879 // than the sr pointer. |
|
880 |
|
881 assert (sr != 0); |
195
|
882 |
3523
|
883 std::string retval; |
1
|
884 |
2975
|
885 octave_value val = sr->def (); |
195
|
886 |
2975
|
887 if (! error_state && val.is_string ()) |
|
888 retval = val.string_value (); |
1
|
889 |
|
890 return retval; |
|
891 } |
|
892 |
581
|
893 // Look for the given name in the global symbol table. If it refers |
1504
|
894 // to a real scalar, place the value in d and return 1. Otherwise, |
|
895 // return 0. |
581
|
896 |
1
|
897 int |
3523
|
898 builtin_real_scalar_variable (const std::string& name, double& d) |
1
|
899 { |
1504
|
900 int status = 0; |
4009
|
901 symbol_record *sr = fbi_sym_tab->lookup (name); |
195
|
902 |
4329
|
903 // It is a programming error to look for builtins that aren't. |
195
|
904 |
4280
|
905 // Use != here to avoid possible conversion to int of smaller type |
|
906 // than the sr pointer. |
|
907 |
|
908 assert (sr != 0); |
1
|
909 |
2975
|
910 octave_value val = sr->def (); |
195
|
911 |
2975
|
912 if (! error_state && val.is_scalar_type ()) |
|
913 { |
|
914 d = val.double_value (); |
|
915 status = 1; |
1
|
916 } |
|
917 |
|
918 return status; |
|
919 } |
|
920 |
1093
|
921 // Look for the given name in the global symbol table. |
|
922 |
2086
|
923 octave_value |
3523
|
924 builtin_any_variable (const std::string& name) |
1093
|
925 { |
4009
|
926 symbol_record *sr = fbi_sym_tab->lookup (name); |
1093
|
927 |
4329
|
928 // It is a programming error to look for builtins that aren't. |
1093
|
929 |
4280
|
930 // Use != here to avoid possible conversion to int of smaller type |
|
931 // than the sr pointer. |
|
932 |
|
933 assert (sr != 0); |
1093
|
934 |
2975
|
935 return sr->def (); |
1093
|
936 } |
|
937 |
593
|
938 // Global stuff and links to builtin variables and functions. |
|
939 |
581
|
940 // Make the definition of the symbol record sr be the same as the |
|
941 // definition of the global variable of the same name, creating it if |
1418
|
942 // it doesn't already exist. |
581
|
943 |
195
|
944 void |
|
945 link_to_global_variable (symbol_record *sr) |
|
946 { |
2975
|
947 if (! sr->is_linked_to_global ()) |
195
|
948 { |
2975
|
949 sr->mark_as_linked_to_global (); |
195
|
950 |
2975
|
951 if (! error_state) |
|
952 { |
3523
|
953 std::string nm = sr->name (); |
2846
|
954 |
2975
|
955 symbol_record *gsr = global_sym_tab->lookup (nm, true); |
|
956 |
|
957 // Make sure this symbol is a variable. |
2900
|
958 |
4009
|
959 if (! gsr->is_user_variable ()) |
2975
|
960 gsr->define (octave_value ()); |
2900
|
961 |
4009
|
962 sr->alias (gsr); |
2975
|
963 } |
195
|
964 } |
|
965 } |
|
966 |
581
|
967 // Make the definition of the symbol record sr be the same as the |
|
968 // definition of the builtin variable of the same name. |
|
969 |
|
970 // Make the definition of the symbol record sr be the same as the |
3258
|
971 // definition of the builtin variable, constant, or function, or user |
|
972 // function of the same name, provided that the name has not been used |
|
973 // as a formal parameter. |
581
|
974 |
195
|
975 void |
|
976 link_to_builtin_or_function (symbol_record *sr) |
|
977 { |
4238
|
978 std::string nm = sr->name (); |
|
979 |
|
980 symbol_record *tmp_sym = 0; |
|
981 |
|
982 if (curr_parent_function) |
|
983 { |
|
984 std::string parent = curr_parent_function->function_name (); |
|
985 |
|
986 tmp_sym = fbi_sym_tab->lookup (parent + ":" + nm); |
|
987 } |
|
988 |
|
989 if (! tmp_sym) |
|
990 tmp_sym = fbi_sym_tab->lookup (nm); |
195
|
991 |
529
|
992 if (tmp_sym |
3258
|
993 && (tmp_sym->is_builtin_variable () |
|
994 || tmp_sym->is_builtin_constant () |
|
995 || tmp_sym->is_function ()) |
529
|
996 && ! tmp_sym->is_formal_parameter ()) |
|
997 sr->alias (tmp_sym); |
195
|
998 } |
|
999 |
581
|
1000 // Force a link to a function in the current symbol table. This is |
|
1001 // used just after defining a function to avoid different behavior |
|
1002 // depending on whether or not the function has been evaluated after |
|
1003 // being defined. |
|
1004 // |
|
1005 // Return without doing anything if there isn't a function with the |
|
1006 // given name defined in the global symbol table. |
|
1007 |
195
|
1008 void |
3523
|
1009 force_link_to_function (const std::string& id_name) |
195
|
1010 { |
4009
|
1011 symbol_record *fsr = fbi_sym_tab->lookup (id_name, true); |
|
1012 if (fsr->is_function ()) |
195
|
1013 { |
|
1014 curr_sym_tab->clear (id_name); |
2856
|
1015 symbol_record *csr = curr_sym_tab->lookup (id_name, true); |
4009
|
1016 csr->alias (fsr); |
195
|
1017 } |
|
1018 } |
|
1019 |
2294
|
1020 DEFUN (document, args, , |
3361
|
1021 "-*- texinfo -*-\n\ |
|
1022 @deftypefn {Built-in Function} {} document (@var{symbol}, @var{text})\n\ |
|
1023 Set the documentation string for @var{symbol} to @var{text}.\n\ |
|
1024 @end deftypefn") |
593
|
1025 { |
2294
|
1026 octave_value retval; |
1755
|
1027 |
2294
|
1028 int nargin = args.length (); |
1755
|
1029 |
2294
|
1030 if (nargin == 2) |
593
|
1031 { |
3523
|
1032 std::string name = args(0).string_value (); |
593
|
1033 |
2294
|
1034 if (! error_state) |
593
|
1035 { |
3523
|
1036 std::string help = args(1).string_value (); |
593
|
1037 |
2294
|
1038 if (! error_state) |
|
1039 { |
|
1040 if (is_builtin_variable (name) |
4208
|
1041 || is_command_name (name) |
2294
|
1042 || is_mapper_function_name (name) |
|
1043 || is_builtin_function_name (name)) |
|
1044 error ("document: can't redefine help for built-in variables and functions"); |
|
1045 else |
|
1046 { |
2856
|
1047 symbol_record *sym_rec = curr_sym_tab->lookup (name); |
2294
|
1048 |
|
1049 if (sym_rec) |
|
1050 sym_rec->document (help); |
|
1051 else |
|
1052 error ("document: no such symbol `%s'", name.c_str ()); |
|
1053 } |
|
1054 } |
593
|
1055 } |
|
1056 } |
|
1057 else |
|
1058 print_usage ("document"); |
|
1059 |
|
1060 return retval; |
|
1061 } |
|
1062 |
4435
|
1063 static octave_value |
|
1064 do_who (int argc, const string_vector& argv, int return_list) |
529
|
1065 { |
4435
|
1066 octave_value retval; |
529
|
1067 |
2856
|
1068 bool show_builtins = false; |
3248
|
1069 bool show_functions = false; |
|
1070 bool show_variables = false; |
2856
|
1071 bool show_verbose = false; |
529
|
1072 |
3523
|
1073 std::string my_name = argv[0]; |
584
|
1074 |
1857
|
1075 int i; |
|
1076 for (i = 1; i < argc; i++) |
529
|
1077 { |
1755
|
1078 if (argv[i] == "-all" || argv[i] == "-a") |
529
|
1079 { |
2856
|
1080 show_builtins = true; |
|
1081 show_functions = true; |
|
1082 show_variables = true; |
529
|
1083 } |
1755
|
1084 else if (argv[i] == "-builtins" || argv[i] == "-b") |
2856
|
1085 show_builtins = true; |
1755
|
1086 else if (argv[i] == "-functions" || argv[i] == "-f") |
2856
|
1087 show_functions = true; |
1755
|
1088 else if (argv[i] == "-long" || argv[i] == "-l") |
2856
|
1089 show_verbose = true; |
1755
|
1090 else if (argv[i] == "-variables" || argv[i] == "-v") |
2856
|
1091 show_variables = true; |
1755
|
1092 else if (argv[i][0] == '-') |
|
1093 warning ("%s: unrecognized option `%s'", my_name.c_str (), |
|
1094 argv[i].c_str ()); |
529
|
1095 else |
867
|
1096 break; |
529
|
1097 } |
|
1098 |
3248
|
1099 // If no options were specified to select the type of symbol to |
|
1100 // display, then set defaults. |
|
1101 |
|
1102 if (! (show_builtins || show_functions || show_variables)) |
|
1103 { |
4208
|
1104 show_functions = at_top_level (); |
3248
|
1105 show_variables = true; |
|
1106 } |
|
1107 |
1857
|
1108 int npats = argc - i; |
|
1109 string_vector pats (npats); |
|
1110 for (int j = 0; j < npats; j++) |
|
1111 pats[j] = argv[i+j]; |
|
1112 |
1271
|
1113 // If the user specified -l and nothing else, show variables. If |
|
1114 // evaluating this at the top level, also show functions. |
529
|
1115 |
|
1116 if (show_verbose && ! (show_builtins || show_functions || show_variables)) |
|
1117 { |
4208
|
1118 show_functions = at_top_level (); |
529
|
1119 show_variables = 1; |
|
1120 } |
|
1121 |
4435
|
1122 if (return_list) |
529
|
1123 { |
4435
|
1124 string_vector names; |
|
1125 |
|
1126 if (show_builtins) |
|
1127 { |
|
1128 names.append (fbi_sym_tab->name_list |
|
1129 (pats, true, symbol_record::BUILTIN_CONSTANT, |
|
1130 SYMTAB_ALL_SCOPES)); |
|
1131 |
|
1132 names.append (fbi_sym_tab->name_list |
|
1133 (pats, true, symbol_record::BUILTIN_VARIABLE, |
|
1134 SYMTAB_ALL_SCOPES)); |
|
1135 |
|
1136 names.append (fbi_sym_tab->name_list |
|
1137 (pats, true, symbol_record::BUILTIN_FUNCTION, |
|
1138 SYMTAB_ALL_SCOPES)); |
|
1139 } |
|
1140 |
|
1141 if (show_functions) |
|
1142 { |
|
1143 names.append (fbi_sym_tab->name_list |
|
1144 (pats, true, symbol_record::DLD_FUNCTION, |
|
1145 SYMTAB_ALL_SCOPES)); |
4108
|
1146 |
4435
|
1147 names.append (fbi_sym_tab->name_list |
|
1148 (pats, true, symbol_record::USER_FUNCTION, |
|
1149 SYMTAB_ALL_SCOPES)); |
|
1150 } |
|
1151 |
|
1152 if (show_variables) |
|
1153 { |
|
1154 names.append (curr_sym_tab->name_list |
|
1155 (pats, true, symbol_record::USER_VARIABLE, |
|
1156 SYMTAB_LOCAL_SCOPE)); |
|
1157 |
|
1158 names.append (curr_sym_tab->name_list |
|
1159 (pats, true, symbol_record::USER_VARIABLE, |
|
1160 SYMTAB_GLOBAL_SCOPE)); |
|
1161 } |
|
1162 |
|
1163 if (show_verbose) |
|
1164 { |
|
1165 int len = names.length (); |
|
1166 |
|
1167 octave_value_list ovl (len, octave_value ()); |
|
1168 |
|
1169 for (int i = 0; i < len; i++) |
|
1170 ovl(i) = names(i); |
|
1171 |
|
1172 retval = Octave_map ("name", ovl); |
|
1173 } |
|
1174 else |
|
1175 retval = Cell (names); |
529
|
1176 } |
4435
|
1177 else |
529
|
1178 { |
4435
|
1179 int pad_after = 0; |
|
1180 |
|
1181 if (show_builtins) |
|
1182 { |
|
1183 pad_after += fbi_sym_tab->maybe_list |
|
1184 ("*** built-in constants:", pats, octave_stdout, |
|
1185 show_verbose, symbol_record::BUILTIN_CONSTANT, SYMTAB_ALL_SCOPES); |
|
1186 |
|
1187 pad_after += fbi_sym_tab->maybe_list |
|
1188 ("*** built-in variables:", pats, octave_stdout, |
|
1189 show_verbose, symbol_record::BUILTIN_VARIABLE, SYMTAB_ALL_SCOPES); |
|
1190 |
|
1191 pad_after += fbi_sym_tab->maybe_list |
|
1192 ("*** built-in functions:", pats, octave_stdout, |
|
1193 show_verbose, symbol_record::BUILTIN_FUNCTION, SYMTAB_ALL_SCOPES); |
|
1194 } |
529
|
1195 |
4435
|
1196 if (show_functions) |
|
1197 { |
|
1198 pad_after += fbi_sym_tab->maybe_list |
|
1199 ("*** dynamically linked functions:", pats, |
|
1200 octave_stdout, show_verbose, symbol_record::DLD_FUNCTION, |
|
1201 SYMTAB_ALL_SCOPES); |
|
1202 |
|
1203 pad_after += fbi_sym_tab->maybe_list |
|
1204 ("*** currently compiled functions:", pats, |
|
1205 octave_stdout, show_verbose, symbol_record::USER_FUNCTION, |
|
1206 SYMTAB_ALL_SCOPES); |
|
1207 } |
|
1208 |
|
1209 if (show_variables) |
|
1210 { |
|
1211 pad_after += curr_sym_tab->maybe_list |
|
1212 ("*** local user variables:", pats, octave_stdout, |
|
1213 show_verbose, symbol_record::USER_VARIABLE, SYMTAB_LOCAL_SCOPE); |
|
1214 |
|
1215 pad_after += curr_sym_tab->maybe_list |
|
1216 ("*** globally visible user variables:", pats, |
|
1217 octave_stdout, show_verbose, symbol_record::USER_VARIABLE, |
|
1218 SYMTAB_GLOBAL_SCOPE); |
|
1219 } |
|
1220 |
|
1221 if (pad_after) |
|
1222 octave_stdout << "\n"; |
529
|
1223 } |
|
1224 |
581
|
1225 return retval; |
|
1226 } |
|
1227 |
4435
|
1228 DEFCMD (who, args, nargout, |
3361
|
1229 "-*- texinfo -*-\n\ |
|
1230 @deffn {Command} who options pattern @dots{}\n\ |
|
1231 @deffnx {Command} whos options pattern @dots{}\n\ |
|
1232 List currently defined symbols matching the given patterns. The\n\ |
|
1233 following are valid options. They may be shortened to one character but\n\ |
|
1234 may not be combined.\n\ |
|
1235 \n\ |
|
1236 @table @code\n\ |
|
1237 @item -all\n\ |
|
1238 List all currently defined symbols.\n\ |
|
1239 \n\ |
|
1240 @item -builtins\n\ |
|
1241 List built-in variables and functions. This includes all currently\n\ |
|
1242 compiled function files, but does not include all function files that\n\ |
|
1243 are in the @code{LOADPATH}.\n\ |
581
|
1244 \n\ |
3361
|
1245 @item -functions\n\ |
|
1246 List user-defined functions.\n\ |
|
1247 \n\ |
|
1248 @item -long\n\ |
|
1249 Print a long listing including the type and dimensions of any symbols.\n\ |
|
1250 The symbols in the first column of output indicate whether it is\n\ |
|
1251 possible to redefine the symbol, and whether it is possible for it to be\n\ |
|
1252 cleared.\n\ |
|
1253 \n\ |
|
1254 @item -variables\n\ |
|
1255 List user-defined variables.\n\ |
|
1256 @end table\n\ |
|
1257 \n\ |
|
1258 Valid patterns are the same as described for the @code{clear} command\n\ |
|
1259 above. If no patterns are supplied, all symbols from the given category\n\ |
|
1260 are listed. By default, only user defined functions and variables\n\ |
|
1261 visible in the local scope are displayed.\n\ |
|
1262 \n\ |
|
1263 The command @kbd{whos} is equivalent to @kbd{who -long}.\n\ |
|
1264 @end deffn") |
581
|
1265 { |
4435
|
1266 octave_value retval; |
581
|
1267 |
4435
|
1268 if (nargout < 2) |
|
1269 { |
|
1270 int argc = args.length () + 1; |
|
1271 |
|
1272 string_vector argv = args.make_argv ("who"); |
1755
|
1273 |
4435
|
1274 if (error_state) |
|
1275 return retval; |
1755
|
1276 |
4435
|
1277 retval = do_who (argc, argv, nargout == 1); |
|
1278 } |
|
1279 else |
|
1280 print_usage ("who"); |
581
|
1281 |
529
|
1282 return retval; |
|
1283 } |
|
1284 |
4435
|
1285 DEFCMD (whos, args, nargout, |
3458
|
1286 "-*- texinfo -*-\n\ |
|
1287 @deffn {Command} whos options pattern @dots{}\n\ |
|
1288 See who.\n\ |
|
1289 @end deffn") |
581
|
1290 { |
4435
|
1291 octave_value retval; |
712
|
1292 |
4435
|
1293 if (nargout < 2) |
|
1294 { |
|
1295 int nargin = args.length (); |
|
1296 |
|
1297 octave_value_list tmp_args; |
|
1298 |
|
1299 for (int i = nargin; i > 0; i--) |
|
1300 tmp_args(i) = args(i-1); |
581
|
1301 |
4435
|
1302 tmp_args(0) = "-long"; |
1755
|
1303 |
4435
|
1304 int argc = tmp_args.length () + 1; |
|
1305 |
|
1306 string_vector argv = tmp_args.make_argv ("whos"); |
581
|
1307 |
4435
|
1308 if (error_state) |
|
1309 return retval; |
581
|
1310 |
4435
|
1311 retval = do_who (argc, argv, nargout == 1); |
|
1312 } |
|
1313 else |
|
1314 print_usage ("whos"); |
581
|
1315 |
|
1316 return retval; |
|
1317 } |
|
1318 |
593
|
1319 // Defining variables. |
|
1320 |
1162
|
1321 void |
2856
|
1322 bind_ans (const octave_value& val, bool print) |
1162
|
1323 { |
4009
|
1324 static symbol_record *sr = fbi_sym_tab->lookup ("ans", true); |
1162
|
1325 |
2978
|
1326 if (val.is_defined ()) |
|
1327 { |
|
1328 sr->define (val); |
1162
|
1329 |
2978
|
1330 if (print) |
|
1331 val.print_with_name (octave_stdout, "ans"); |
|
1332 } |
1162
|
1333 } |
|
1334 |
3259
|
1335 // Give a global constant a definition. This will insert the symbol |
|
1336 // in the global table if necessary. |
|
1337 |
|
1338 // How is this different than install_builtin_constant? Are both |
|
1339 // functions needed? |
|
1340 |
|
1341 void |
3523
|
1342 bind_builtin_constant (const std::string& name, const octave_value& val, |
|
1343 bool protect, bool eternal, const std::string& help) |
3259
|
1344 { |
4009
|
1345 symbol_record *sym_rec = fbi_sym_tab->lookup (name, true); |
3259
|
1346 sym_rec->unprotect (); |
|
1347 |
3523
|
1348 std::string tmp_help = help.empty () ? sym_rec->help () : help; |
3259
|
1349 |
|
1350 sym_rec->define_builtin_const (val); |
|
1351 |
|
1352 sym_rec->document (tmp_help); |
|
1353 |
|
1354 if (protect) |
|
1355 sym_rec->protect (); |
|
1356 |
|
1357 if (eternal) |
|
1358 sym_rec->make_eternal (); |
|
1359 } |
|
1360 |
593
|
1361 // Give a global variable a definition. This will insert the symbol |
|
1362 // in the global table if necessary. |
|
1363 |
|
1364 // How is this different than install_builtin_variable? Are both |
|
1365 // functions needed? |
|
1366 |
|
1367 void |
3523
|
1368 bind_builtin_variable (const std::string& varname, const octave_value& val, |
2953
|
1369 bool protect, bool eternal, |
3005
|
1370 symbol_record::change_function chg_fcn, |
3523
|
1371 const std::string& help) |
593
|
1372 { |
4009
|
1373 symbol_record *sr = fbi_sym_tab->lookup (varname, true); |
593
|
1374 |
1271
|
1375 // It is a programming error for a builtin symbol to be missing. |
|
1376 // Besides, we just inserted it, so it must be there. |
593
|
1377 |
4280
|
1378 // Use != here to avoid possible conversion to int of smaller type |
|
1379 // than the sr pointer. |
|
1380 |
|
1381 assert (sr != 0); |
593
|
1382 |
|
1383 sr->unprotect (); |
|
1384 |
1271
|
1385 // Must do this before define, since define will call the special |
|
1386 // variable function only if it knows about it, and it needs to, so |
|
1387 // that user prefs can be properly initialized. |
593
|
1388 |
3005
|
1389 if (chg_fcn) |
|
1390 sr->set_change_function (chg_fcn); |
593
|
1391 |
|
1392 sr->define_builtin_var (val); |
|
1393 |
|
1394 if (protect) |
|
1395 sr->protect (); |
|
1396 |
|
1397 if (eternal) |
|
1398 sr->make_eternal (); |
|
1399 |
1755
|
1400 sr->document (help); |
529
|
1401 } |
|
1402 |
4319
|
1403 void |
|
1404 mlock (const std::string& nm) |
|
1405 { |
|
1406 symbol_record *sr = fbi_sym_tab->lookup (nm, true); |
|
1407 |
|
1408 if (sr) |
|
1409 sr->mark_as_static (); |
|
1410 } |
|
1411 |
|
1412 void |
|
1413 munlock (const std::string& nm) |
|
1414 { |
|
1415 symbol_record *sr = fbi_sym_tab->lookup (nm); |
|
1416 |
|
1417 if (sr && sr->is_static ()) |
|
1418 sr->unmark_static (); |
|
1419 else |
|
1420 error ("munlock: %s is not locked", nm.c_str ()); |
|
1421 } |
|
1422 |
|
1423 bool |
|
1424 mislocked (const std::string& nm) |
|
1425 { |
|
1426 symbol_record *sr = fbi_sym_tab->lookup (nm); |
|
1427 |
|
1428 return (sr && sr->is_static ()); |
|
1429 } |
|
1430 |
|
1431 DEFCMD (mlock, args, , |
|
1432 "-*- texinfo -*-\n\ |
|
1433 @deftypefnx {Built-in Function} {} mlock (@var{name})\n\ |
|
1434 Lock the named function into memory. If no function is named\n\ |
|
1435 then lock in the current function.\n\ |
|
1436 @end deftypefn\n\ |
|
1437 @seealso{munlock, mislocked, and persistent}") |
|
1438 { |
|
1439 octave_value_list retval; |
|
1440 |
|
1441 if (args.length () == 1) |
|
1442 { |
|
1443 std::string name = args(0).string_value (); |
|
1444 |
|
1445 if (! error_state) |
|
1446 mlock (name); |
|
1447 else |
|
1448 error ("mlock: expecting argument to be a function name"); |
|
1449 } |
|
1450 else if (args.length () == 0) |
|
1451 { |
|
1452 if (curr_function) |
|
1453 mlock (curr_function->function_name ()); |
|
1454 else |
|
1455 error ("mlock: invalid use outside a function"); |
|
1456 } |
|
1457 else |
|
1458 print_usage ("mlock"); |
|
1459 |
|
1460 return retval; |
|
1461 } |
|
1462 |
|
1463 DEFCMD (munlock, args, , |
|
1464 "-*- texinfo -*-\n\ |
|
1465 @deftypefn {Built-in Function} {} munlock (@var{fcn})\n\ |
|
1466 Unlock the named function. If no function is named\n\ |
|
1467 then unlock the current function.\n\ |
|
1468 @end deftypefn\n\ |
|
1469 @seealso{mlock, mislocked, and persistent}") |
|
1470 { |
|
1471 octave_value_list retval; |
|
1472 |
|
1473 if (args.length() == 1) |
|
1474 { |
|
1475 std::string name = args(0).string_value (); |
|
1476 |
|
1477 if (! error_state) |
|
1478 munlock (name); |
|
1479 else |
|
1480 error ("munlock: expecting argument to be a function name"); |
|
1481 } |
|
1482 else if (args.length () == 0) |
|
1483 { |
|
1484 if (curr_function) |
|
1485 mlock (curr_function->function_name ()); |
|
1486 else |
|
1487 error ("munlock: invalid use outside a function"); |
|
1488 } |
|
1489 else |
|
1490 print_usage ("munlock"); |
|
1491 |
|
1492 return retval; |
|
1493 } |
|
1494 |
|
1495 |
|
1496 DEFCMD (mislocked, args, , |
|
1497 "-*- texinfo -*-\n\ |
|
1498 @deftypefn {Built-in Function} {} mislocked (@var{fcn})\n\ |
|
1499 Return true if the named function is locked. If no function is named\n\ |
|
1500 then return true if the current function is locked.\n\ |
|
1501 @end deftypefn\n\ |
|
1502 @seealso{mlock, munlock, and persistent}") |
|
1503 { |
|
1504 octave_value retval; |
|
1505 |
|
1506 if (args.length() == 1) |
|
1507 { |
|
1508 std::string name = args(0).string_value (); |
|
1509 |
|
1510 if (! error_state) |
|
1511 retval = mislocked (name); |
|
1512 else |
|
1513 error ("mislocked: expecting argument to be a function name"); |
|
1514 } |
|
1515 else if (args.length () == 0) |
|
1516 { |
|
1517 if (curr_function) |
|
1518 retval = mislocked (curr_function->function_name ()); |
|
1519 else |
|
1520 error ("mislocked: invalid use outside a function"); |
|
1521 } |
|
1522 else |
|
1523 print_usage ("mislocked"); |
|
1524 |
|
1525 return retval; |
|
1526 } |
|
1527 |
593
|
1528 // Deleting names from the symbol tables. |
|
1529 |
3681
|
1530 static inline bool |
4009
|
1531 name_matches_any_pattern (const std::string& nm, |
|
1532 const string_vector& argv, int argc, int idx) |
3681
|
1533 { |
|
1534 bool retval = false; |
|
1535 |
|
1536 for (int k = idx; k < argc; k++) |
|
1537 { |
|
1538 std::string patstr = argv[k]; |
|
1539 |
|
1540 if (! patstr.empty ()) |
|
1541 { |
|
1542 glob_match pattern (patstr); |
|
1543 |
|
1544 if (pattern.match (nm)) |
|
1545 { |
|
1546 retval = true; |
|
1547 break; |
|
1548 } |
|
1549 } |
|
1550 } |
|
1551 |
|
1552 return retval; |
|
1553 } |
|
1554 |
4009
|
1555 static inline bool |
|
1556 is_local_variable (const std::string& nm) |
|
1557 { |
|
1558 symbol_record *sr = curr_sym_tab->lookup (nm); |
|
1559 |
|
1560 return (sr && sr->is_variable ()); |
|
1561 } |
|
1562 |
|
1563 static inline void |
|
1564 maybe_warn_exclusive (bool exclusive) |
|
1565 { |
|
1566 if (exclusive) |
|
1567 warning ("clear: ignoring --exclusive option"); |
|
1568 } |
|
1569 |
|
1570 static inline void |
|
1571 do_clear_all (void) |
|
1572 { |
|
1573 curr_sym_tab->clear (); |
|
1574 fbi_sym_tab->clear_functions (); |
|
1575 global_sym_tab->clear (); |
|
1576 } |
|
1577 |
|
1578 static inline void |
|
1579 do_clear_functions (void) |
|
1580 { |
|
1581 curr_sym_tab->clear_functions (); |
|
1582 fbi_sym_tab->clear_functions (); |
|
1583 } |
|
1584 |
|
1585 static inline void |
|
1586 do_clear_globals (void) |
|
1587 { |
|
1588 curr_sym_tab->clear_globals (); |
|
1589 global_sym_tab->clear (); |
|
1590 } |
|
1591 |
|
1592 static inline void |
|
1593 do_clear_variables (void) |
|
1594 { |
|
1595 curr_sym_tab->clear (); |
|
1596 } |
|
1597 |
|
1598 static inline bool |
|
1599 do_clear_function (const std::string& nm) |
|
1600 { |
|
1601 bool b1 = curr_sym_tab->clear_function (nm); |
|
1602 |
|
1603 bool b2 = fbi_sym_tab->clear_function (nm); |
|
1604 |
|
1605 return b1 || b2; |
|
1606 } |
|
1607 |
|
1608 static inline bool |
|
1609 do_clear_global (const std::string& nm) |
|
1610 { |
|
1611 bool b1 = curr_sym_tab->clear_global (nm); |
|
1612 |
|
1613 bool b2 = global_sym_tab->clear_variable (nm); |
|
1614 |
|
1615 return b1 || b2; |
|
1616 } |
|
1617 |
|
1618 static inline bool |
|
1619 do_clear_variable (const std::string& nm) |
|
1620 { |
|
1621 return curr_sym_tab->clear_variable (nm); |
|
1622 } |
|
1623 |
|
1624 static inline bool |
|
1625 do_clear_symbol (const std::string& nm) |
|
1626 { |
|
1627 bool cleared = curr_sym_tab->clear_variable (nm); |
|
1628 |
|
1629 if (! cleared) |
|
1630 cleared = do_clear_function (nm); |
|
1631 |
|
1632 return cleared; |
|
1633 } |
|
1634 |
|
1635 static inline bool |
|
1636 do_clear_function_pattern (const std::string& pat) |
|
1637 { |
|
1638 bool b1 = curr_sym_tab->clear_function_pattern (pat); |
|
1639 |
|
1640 bool b2 = fbi_sym_tab->clear_function_pattern (pat); |
|
1641 |
|
1642 return b1 || b2; |
|
1643 } |
|
1644 |
|
1645 static inline bool |
|
1646 do_clear_global_pattern (const std::string& pat) |
|
1647 { |
|
1648 bool b1 = curr_sym_tab->clear_global_pattern (pat); |
|
1649 |
|
1650 bool b2 = global_sym_tab->clear_variable_pattern (pat); |
|
1651 |
|
1652 return b1 || b2; |
|
1653 } |
|
1654 |
|
1655 static inline bool |
|
1656 do_clear_variable_pattern (const std::string& pat) |
|
1657 { |
|
1658 return curr_sym_tab->clear_variable_pattern (pat); |
|
1659 } |
|
1660 |
|
1661 static inline bool |
|
1662 do_clear_symbol_pattern (const std::string& pat) |
|
1663 { |
|
1664 // XXX FIXME XXX -- if we have a variable v1 and a function v2 and |
|
1665 // someone says clear v*, we will clear the variable but not the |
|
1666 // function. Is that really what should happen? (I think it is |
|
1667 // what Matlab does.) |
|
1668 |
|
1669 bool cleared = curr_sym_tab->clear_variable_pattern (pat); |
|
1670 |
|
1671 if (! cleared) |
|
1672 cleared = do_clear_function_pattern (pat); |
|
1673 |
|
1674 return cleared; |
|
1675 } |
|
1676 |
|
1677 static inline void |
|
1678 do_clear_functions (const string_vector& argv, int argc, int idx, |
|
1679 bool exclusive = false) |
|
1680 { |
|
1681 if (idx == argc) |
|
1682 do_clear_functions (); |
|
1683 else |
|
1684 { |
|
1685 if (exclusive) |
|
1686 { |
|
1687 string_vector lfcns = curr_sym_tab->user_function_name_list (); |
|
1688 |
|
1689 int lcount = lfcns.length (); |
|
1690 |
|
1691 for (int i = 0; i < lcount; i++) |
|
1692 { |
|
1693 std::string nm = lfcns[i]; |
|
1694 |
|
1695 if (! name_matches_any_pattern (nm, argv, argc, idx)) |
|
1696 do_clear_function (nm); |
|
1697 } |
|
1698 |
|
1699 string_vector fcns = fbi_sym_tab->user_function_name_list (); |
|
1700 |
|
1701 int fcount = fcns.length (); |
|
1702 |
|
1703 for (int i = 0; i < fcount; i++) |
|
1704 { |
|
1705 std::string nm = fcns[i]; |
|
1706 |
|
1707 if (! name_matches_any_pattern (nm, argv, argc, idx)) |
|
1708 do_clear_function (nm); |
|
1709 } |
|
1710 } |
|
1711 else |
|
1712 { |
|
1713 while (idx < argc) |
|
1714 do_clear_function_pattern (argv[idx++]); |
|
1715 } |
|
1716 } |
|
1717 } |
|
1718 |
|
1719 static inline void |
|
1720 do_clear_globals (const string_vector& argv, int argc, int idx, |
|
1721 bool exclusive = false) |
|
1722 { |
|
1723 if (idx == argc) |
|
1724 do_clear_globals (); |
|
1725 else |
|
1726 { |
|
1727 if (exclusive) |
|
1728 { |
|
1729 string_vector lvars = curr_sym_tab->global_variable_name_list (); |
|
1730 |
|
1731 int lcount = lvars.length (); |
|
1732 |
|
1733 for (int i = 0; i < lcount; i++) |
|
1734 { |
|
1735 std::string nm = lvars[i]; |
|
1736 |
|
1737 if (! name_matches_any_pattern (nm, argv, argc, idx)) |
|
1738 do_clear_global (nm); |
|
1739 } |
|
1740 |
|
1741 string_vector gvars = global_sym_tab->global_variable_name_list (); |
|
1742 |
|
1743 int gcount = gvars.length (); |
|
1744 |
|
1745 for (int i = 0; i < gcount; i++) |
|
1746 { |
|
1747 std::string nm = gvars[i]; |
|
1748 |
|
1749 if (! name_matches_any_pattern (nm, argv, argc, idx)) |
|
1750 do_clear_global (nm); |
|
1751 } |
|
1752 } |
|
1753 else |
|
1754 { |
|
1755 while (idx < argc) |
|
1756 do_clear_global_pattern (argv[idx++]); |
|
1757 } |
|
1758 } |
|
1759 } |
|
1760 |
|
1761 static inline void |
|
1762 do_clear_variables (const string_vector& argv, int argc, int idx, |
|
1763 bool exclusive = false) |
|
1764 { |
|
1765 if (idx == argc) |
|
1766 do_clear_variables (); |
|
1767 else |
|
1768 { |
|
1769 if (exclusive) |
|
1770 { |
|
1771 string_vector lvars = curr_sym_tab->variable_name_list (); |
|
1772 |
|
1773 int lcount = lvars.length (); |
|
1774 |
|
1775 for (int i = 0; i < lcount; i++) |
|
1776 { |
|
1777 std::string nm = lvars[i]; |
|
1778 |
|
1779 if (! name_matches_any_pattern (nm, argv, argc, idx)) |
|
1780 do_clear_variable (nm); |
|
1781 } |
|
1782 } |
|
1783 else |
|
1784 { |
|
1785 while (idx < argc) |
|
1786 do_clear_variable_pattern (argv[idx++]); |
|
1787 } |
|
1788 } |
|
1789 } |
|
1790 |
|
1791 static inline void |
|
1792 do_clear_symbols (const string_vector& argv, int argc, int idx, |
|
1793 bool exclusive = false) |
|
1794 { |
|
1795 if (idx == argc) |
|
1796 do_clear_variables (); |
|
1797 else |
|
1798 { |
|
1799 if (exclusive) |
|
1800 { |
|
1801 // XXX FIXME XXX -- is this really what we want, or do we |
|
1802 // somehow want to only clear the functions that are not |
|
1803 // shadowed by local variables? It seems that would be a |
|
1804 // bit harder to do. |
|
1805 |
|
1806 do_clear_variables (argv, argc, idx, exclusive); |
|
1807 do_clear_functions (argv, argc, idx, exclusive); |
|
1808 } |
|
1809 else |
|
1810 { |
|
1811 while (idx < argc) |
|
1812 do_clear_symbol_pattern (argv[idx++]); |
|
1813 } |
|
1814 } |
|
1815 } |
|
1816 |
|
1817 static void |
|
1818 do_matlab_compatible_clear (const string_vector& argv, int argc, int idx) |
|
1819 { |
|
1820 // This is supposed to be mostly Matlab compatible. |
|
1821 |
|
1822 for (; idx < argc; idx++) |
|
1823 { |
|
1824 if (argv[idx] == "all" && ! is_local_variable ("all")) |
|
1825 { |
|
1826 do_clear_all (); |
|
1827 } |
|
1828 else if (argv[idx] == "functions" && ! is_local_variable ("functions")) |
|
1829 { |
|
1830 do_clear_functions (argv, argc, ++idx); |
|
1831 } |
|
1832 else if (argv[idx] == "global" && ! is_local_variable ("global")) |
|
1833 { |
|
1834 do_clear_globals (argv, argc, ++idx); |
|
1835 } |
|
1836 else if (argv[idx] == "variables" && ! is_local_variable ("variables")) |
|
1837 { |
|
1838 do_clear_variables (); |
|
1839 } |
|
1840 else |
|
1841 { |
|
1842 do_clear_symbol_pattern (argv[idx]); |
|
1843 } |
|
1844 } |
|
1845 } |
|
1846 |
|
1847 #define CLEAR_OPTION_ERROR(cond) \ |
|
1848 do \ |
|
1849 { \ |
|
1850 if (cond) \ |
|
1851 { \ |
|
1852 print_usage ("clear"); \ |
|
1853 return retval; \ |
|
1854 } \ |
|
1855 } \ |
|
1856 while (0) |
|
1857 |
4208
|
1858 DEFCMD (clear, args, , |
3361
|
1859 "-*- texinfo -*-\n\ |
|
1860 @deffn {Command} clear [-x] pattern @dots{}\n\ |
|
1861 Delete the names matching the given patterns from the symbol table. The\n\ |
|
1862 pattern may contain the following special characters:\n\ |
4016
|
1863 \n\ |
3361
|
1864 @table @code\n\ |
|
1865 @item ?\n\ |
|
1866 Match any single character.\n\ |
668
|
1867 \n\ |
3361
|
1868 @item *\n\ |
|
1869 Match zero or more characters.\n\ |
|
1870 \n\ |
|
1871 @item [ @var{list} ]\n\ |
|
1872 Match the list of characters specified by @var{list}. If the first\n\ |
|
1873 character is @code{!} or @code{^}, match all characters except those\n\ |
|
1874 specified by @var{list}. For example, the pattern @samp{[a-zA-Z]} will\n\ |
|
1875 match all lower and upper case alphabetic characters.\n\ |
|
1876 @end table\n\ |
|
1877 \n\ |
|
1878 For example, the command\n\ |
593
|
1879 \n\ |
3361
|
1880 @example\n\ |
|
1881 clear foo b*r\n\ |
|
1882 @end example\n\ |
|
1883 \n\ |
|
1884 @noindent\n\ |
|
1885 clears the name @code{foo} and all names that begin with the letter\n\ |
|
1886 @code{b} and end with the letter @code{r}.\n\ |
668
|
1887 \n\ |
3361
|
1888 If @code{clear} is called without any arguments, all user-defined\n\ |
|
1889 variables (local and global) are cleared from the symbol table. If\n\ |
|
1890 @code{clear} is called with at least one argument, only the visible\n\ |
|
1891 names matching the arguments are cleared. For example, suppose you have\n\ |
|
1892 defined a function @code{foo}, and then hidden it by performing the\n\ |
|
1893 assignment @code{foo = 2}. Executing the command @kbd{clear foo} once\n\ |
|
1894 will clear the variable definition and restore the definition of\n\ |
|
1895 @code{foo} as a function. Executing @kbd{clear foo} a second time will\n\ |
|
1896 clear the function definition.\n\ |
|
1897 \n\ |
|
1898 With -x, clear the variables that don't match the patterns.\n\ |
|
1899 \n\ |
|
1900 This command may not be used within a function body.\n\ |
|
1901 @end deffn") |
529
|
1902 { |
2086
|
1903 octave_value_list retval; |
593
|
1904 |
1755
|
1905 int argc = args.length () + 1; |
593
|
1906 |
1968
|
1907 string_vector argv = args.make_argv ("clear"); |
1755
|
1908 |
4009
|
1909 if (! error_state) |
529
|
1910 { |
4009
|
1911 if (argc == 1) |
593
|
1912 { |
4009
|
1913 do_clear_variables (); |
3681
|
1914 } |
|
1915 else |
|
1916 { |
4009
|
1917 int idx = 0; |
|
1918 |
|
1919 bool clear_all = false; |
|
1920 bool clear_functions = false; |
|
1921 bool clear_globals = false; |
|
1922 bool clear_variables = false; |
|
1923 bool exclusive = false; |
|
1924 bool have_dash_option = false; |
3681
|
1925 |
4009
|
1926 while (++idx < argc) |
|
1927 { |
4010
|
1928 if (argv[idx] == "-all" || argv[idx] == "-a") |
593
|
1929 { |
4009
|
1930 CLEAR_OPTION_ERROR (have_dash_option && ! exclusive); |
3681
|
1931 |
4009
|
1932 have_dash_option = true; |
|
1933 clear_all = true; |
|
1934 } |
4010
|
1935 else if (argv[idx] == "-exclusive" || argv[idx] == "-x") |
4009
|
1936 { |
|
1937 have_dash_option = true; |
|
1938 exclusive = true; |
|
1939 } |
4010
|
1940 else if (argv[idx] == "-functions" || argv[idx] == "-f") |
4009
|
1941 { |
|
1942 CLEAR_OPTION_ERROR (have_dash_option && ! exclusive); |
3681
|
1943 |
4009
|
1944 have_dash_option = true; |
|
1945 clear_functions = true; |
|
1946 } |
4010
|
1947 else if (argv[idx] == "-global" || argv[idx] == "-g") |
4009
|
1948 { |
|
1949 CLEAR_OPTION_ERROR (have_dash_option && ! exclusive); |
|
1950 |
|
1951 have_dash_option = true; |
|
1952 clear_globals = true; |
|
1953 } |
4010
|
1954 else if (argv[idx] == "-variables" || argv[idx] == "-v") |
4009
|
1955 { |
|
1956 CLEAR_OPTION_ERROR (have_dash_option && ! exclusive); |
3681
|
1957 |
4009
|
1958 have_dash_option = true; |
|
1959 clear_variables = true; |
|
1960 } |
|
1961 else |
|
1962 break; |
|
1963 } |
3681
|
1964 |
4224
|
1965 if (idx <= argc) |
4009
|
1966 { |
|
1967 if (! have_dash_option) |
|
1968 { |
|
1969 do_matlab_compatible_clear (argv, argc, idx); |
|
1970 } |
|
1971 else |
|
1972 { |
|
1973 if (clear_all) |
3681
|
1974 { |
4009
|
1975 maybe_warn_exclusive (exclusive); |
3681
|
1976 |
4009
|
1977 if (++idx < argc) |
|
1978 warning |
4010
|
1979 ("clear: ignoring extra arguments after -all"); |
3681
|
1980 |
4009
|
1981 curr_sym_tab->clear (); |
|
1982 fbi_sym_tab->clear_functions (); |
|
1983 global_sym_tab->clear (); |
|
1984 } |
|
1985 else if (clear_functions) |
|
1986 { |
|
1987 do_clear_functions (argv, argc, idx, exclusive); |
|
1988 } |
|
1989 else if (clear_globals) |
593
|
1990 { |
4009
|
1991 do_clear_globals (argv, argc, idx, exclusive); |
|
1992 } |
|
1993 else if (clear_variables) |
|
1994 { |
|
1995 do_clear_variables (argv, argc, idx, exclusive); |
|
1996 } |
|
1997 else |
|
1998 { |
|
1999 do_clear_symbols (argv, argc, idx, exclusive); |
593
|
2000 } |
|
2001 } |
|
2002 } |
|
2003 } |
|
2004 } |
|
2005 |
|
2006 return retval; |
529
|
2007 } |
|
2008 |
3933
|
2009 DEFUN (__print_symtab_info__, args, , |
3446
|
2010 "-*- texinfo -*-\n\ |
3933
|
2011 @deftypefn {Built-in Function} {} __print_symtab_info__ ()\n\ |
3446
|
2012 Print raw symbol table statistices.\n\ |
|
2013 @end deftypefn") |
3005
|
2014 { |
|
2015 octave_value_list retval; |
|
2016 |
|
2017 int nargin = args.length (); |
|
2018 |
|
2019 if (nargin == 1) |
|
2020 { |
3523
|
2021 std::string arg = args(0).string_value (); |
3005
|
2022 |
4009
|
2023 if (arg == "fbi") |
|
2024 fbi_sym_tab->print_info (octave_stdout); |
|
2025 else if (arg == "global") |
3933
|
2026 global_sym_tab->print_info (octave_stdout); |
|
2027 else if (arg == "top-level") |
|
2028 top_level_sym_tab->print_info (octave_stdout); |
3005
|
2029 else |
3933
|
2030 { |
4009
|
2031 symbol_record *fsr = fbi_sym_tab->lookup (arg, true); |
3933
|
2032 |
4009
|
2033 if (fsr && fsr->is_user_function ()) |
3933
|
2034 { |
4009
|
2035 octave_value tmp = fsr->def (); |
3933
|
2036 const octave_value& rep = tmp.get_rep (); |
|
2037 |
|
2038 const octave_user_function& fcn |
|
2039 = static_cast<const octave_user_function&> (rep); |
|
2040 |
|
2041 fcn.print_symtab_info (octave_stdout); |
|
2042 } |
|
2043 else |
|
2044 error ("no user-defined function named %s", arg.c_str ()); |
|
2045 } |
3005
|
2046 } |
|
2047 else if (nargin == 0) |
3933
|
2048 curr_sym_tab->print_info (octave_stdout); |
3005
|
2049 else |
3933
|
2050 print_usage ("__print_symtab_info__"); |
3005
|
2051 |
|
2052 return retval; |
|
2053 } |
|
2054 |
3933
|
2055 DEFUN (__print_symbol_info__, args, , |
3446
|
2056 "-*- texinfo -*-\n\ |
|
2057 @deftypefn {Built-in Function} {} __dump_symbol_info__ (@var{name})\n\ |
3548
|
2058 Print symbol table information for the symbol @var{name}.\n\ |
3446
|
2059 @end deftypefn") |
3239
|
2060 { |
|
2061 octave_value_list retval; |
|
2062 |
|
2063 int nargin = args.length (); |
|
2064 |
|
2065 if (nargin == 1) |
|
2066 { |
3523
|
2067 std::string symbol_name = args(0).string_value (); |
3239
|
2068 |
|
2069 if (! error_state) |
|
2070 { |
|
2071 symbol_record *sr = curr_sym_tab->lookup (symbol_name); |
|
2072 |
|
2073 if (sr) |
3933
|
2074 sr->print_info (octave_stdout); |
3239
|
2075 else |
3933
|
2076 error ("__print_symbol_info__: symbol %s not found", |
3239
|
2077 symbol_name.c_str ()); |
|
2078 } |
|
2079 else |
3933
|
2080 print_usage ("__print_symbol_info__"); |
3239
|
2081 } |
|
2082 else |
3933
|
2083 print_usage ("__print_symbol_info__"); |
3239
|
2084 |
|
2085 return retval; |
|
2086 } |
|
2087 |
3016
|
2088 // XXX FIXME XXX -- some of these should do their own checking to be |
|
2089 // able to provide more meaningful warning or error messages. |
|
2090 |
|
2091 static int |
|
2092 ignore_function_time_stamp (void) |
|
2093 { |
|
2094 int pref = 0; |
|
2095 |
3523
|
2096 std::string val = builtin_string_variable ("ignore_function_time_stamp"); |
3016
|
2097 |
|
2098 if (! val.empty ()) |
|
2099 { |
3565
|
2100 if (val == "all") |
3016
|
2101 pref = 2; |
3565
|
2102 else if (val == "system") |
3016
|
2103 pref = 1; |
|
2104 } |
|
2105 |
|
2106 Vignore_function_time_stamp = pref; |
|
2107 |
|
2108 return 0; |
|
2109 } |
|
2110 |
|
2111 // XXX FIXME XXX -- there still may be better places for some of these |
|
2112 // to be defined. |
|
2113 |
|
2114 void |
|
2115 symbols_of_variables (void) |
|
2116 { |
3258
|
2117 DEFVAR (ans, , 0, |
3372
|
2118 "-*- texinfo -*-\n\ |
|
2119 @defvr {Built-in Variable} ans\n\ |
|
2120 This variable holds the most recently computed result that was not\n\ |
|
2121 explicitly assigned to a variable. For example, after the expression\n\ |
|
2122 \n\ |
|
2123 @example\n\ |
|
2124 3^2 + 4^2\n\ |
|
2125 @end example\n\ |
|
2126 \n\ |
|
2127 @noindent\n\ |
|
2128 is evaluated, the value of @code{ans} is 25.\n\ |
|
2129 @end defvr"); |
3016
|
2130 |
3258
|
2131 DEFVAR (ignore_function_time_stamp, "system", ignore_function_time_stamp, |
3371
|
2132 "-*- texinfo -*-\n\ |
|
2133 @defvr {Built-in Variable} ignore_function_time_stamp\n\ |
|
2134 This variable can be used to prevent Octave from making the system call\n\ |
|
2135 @code{stat} each time it looks up functions defined in function files.\n\ |
|
2136 If @code{ignore_function_time_stamp} to @code{\"system\"}, Octave will not\n\ |
|
2137 automatically recompile function files in subdirectories of\n\ |
|
2138 @file{@var{octave-home}/lib/@var{version}} if they have changed since\n\ |
|
2139 they were last compiled, but will recompile other function files in the\n\ |
|
2140 @code{LOADPATH} if they change. If set to @code{\"all\"}, Octave will not\n\ |
|
2141 recompile any function files unless their definitions are removed with\n\ |
|
2142 @code{clear}. For any other value of @code{ignore_function_time_stamp},\n\ |
|
2143 Octave will always check to see if functions defined in function files\n\ |
|
2144 need to recompiled. The default value of @code{ignore_function_time_stamp} is\n\ |
|
2145 @code{\"system\"}.\n\ |
|
2146 @end defvr"); |
3016
|
2147 } |
|
2148 |
1
|
2149 /* |
|
2150 ;;; Local Variables: *** |
|
2151 ;;; mode: C++ *** |
|
2152 ;;; End: *** |
|
2153 */ |