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