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