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