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