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