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