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