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