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