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