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