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