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