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> |
3162
|
29 #include <ctime> |
605
|
30 |
1728
|
31 #include <string> |
|
32 |
2926
|
33 #include "file-stat.h" |
|
34 #include "oct-env.h" |
|
35 #include "glob-match.h" |
1755
|
36 #include "str-vec.h" |
|
37 |
2492
|
38 #include <defaults.h> |
1352
|
39 #include "defun.h" |
|
40 #include "dirfns.h" |
|
41 #include "error.h" |
2205
|
42 #include "gripes.h" |
1352
|
43 #include "help.h" |
3165
|
44 #include "input.h" |
1352
|
45 #include "lex.h" |
2926
|
46 #include "oct-map.h" |
|
47 #include "oct-obj.h" |
|
48 #include "ov.h" |
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 |
593
|
70 // Initialization. |
|
71 |
|
72 // Create the initial symbol tables and set the current scope at the |
|
73 // top level. |
|
74 |
195
|
75 void |
|
76 initialize_symbol_tables (void) |
|
77 { |
581
|
78 if (! global_sym_tab) |
3005
|
79 global_sym_tab = new symbol_table (2048); |
195
|
80 |
581
|
81 if (! top_level_sym_tab) |
3005
|
82 top_level_sym_tab = new symbol_table (4096); |
195
|
83 |
|
84 curr_sym_tab = top_level_sym_tab; |
|
85 } |
|
86 |
593
|
87 // Attributes of variables and functions. |
|
88 |
|
89 // Is this variable a builtin? |
|
90 |
1827
|
91 bool |
1755
|
92 is_builtin_variable (const string& name) |
593
|
93 { |
2856
|
94 symbol_record *sr = global_sym_tab->lookup (name); |
593
|
95 return (sr && sr->is_builtin_variable ()); |
|
96 } |
|
97 |
|
98 // Is this a text-style function? |
|
99 |
1827
|
100 bool |
1755
|
101 is_text_function_name (const string& s) |
593
|
102 { |
|
103 symbol_record *sr = global_sym_tab->lookup (s); |
|
104 return (sr && sr->is_text_function ()); |
|
105 } |
|
106 |
2294
|
107 // Is this a mapper function? |
|
108 |
|
109 bool |
|
110 is_builtin_function_name (const string& s) |
|
111 { |
|
112 symbol_record *sr = global_sym_tab->lookup (s); |
|
113 return (sr && sr->is_builtin_function ()); |
|
114 } |
|
115 |
|
116 // Is this a mapper function? |
|
117 |
|
118 bool |
|
119 is_mapper_function_name (const string& s) |
|
120 { |
|
121 symbol_record *sr = global_sym_tab->lookup (s); |
|
122 return (sr && sr->is_mapper_function ()); |
|
123 } |
|
124 |
593
|
125 // Is this function globally in this scope? |
|
126 |
1827
|
127 bool |
1755
|
128 is_globally_visible (const string& name) |
593
|
129 { |
2856
|
130 symbol_record *sr = curr_sym_tab->lookup (name); |
593
|
131 return (sr && sr->is_linked_to_global ()); |
|
132 } |
|
133 |
2086
|
134 // Is this octave_value a valid function? |
593
|
135 |
2975
|
136 octave_function * |
2856
|
137 is_valid_function (const octave_value& arg, const string& warn_for, bool warn) |
593
|
138 { |
2975
|
139 octave_function *ans = 0; |
593
|
140 |
1755
|
141 string fcn_name; |
1728
|
142 |
1517
|
143 if (arg.is_string ()) |
1755
|
144 fcn_name = arg.string_value (); |
1517
|
145 |
1755
|
146 if (fcn_name.empty () || error_state) |
593
|
147 { |
|
148 if (warn) |
1755
|
149 error ("%s: expecting function name as argument", |
|
150 warn_for.c_str ()); |
593
|
151 return ans; |
|
152 } |
|
153 |
|
154 symbol_record *sr = 0; |
1755
|
155 |
|
156 if (! fcn_name.empty ()) |
593
|
157 sr = lookup_by_name (fcn_name); |
|
158 |
|
159 if (sr) |
2975
|
160 { |
|
161 octave_value tmp = sr->def (); |
|
162 ans = tmp.function_value (true); |
|
163 } |
593
|
164 |
|
165 if (! sr || ! ans || ! sr->is_function ()) |
|
166 { |
|
167 if (warn) |
|
168 error ("%s: the symbol `%s' is not valid as a function", |
1755
|
169 warn_for.c_str (), fcn_name.c_str ()); |
593
|
170 ans = 0; |
|
171 } |
|
172 |
|
173 return ans; |
|
174 } |
|
175 |
2975
|
176 octave_function * |
2796
|
177 extract_function (const octave_value& arg, const string& warn_for, |
|
178 const string& fname, const string& header, |
|
179 const string& trailer) |
|
180 { |
2975
|
181 octave_function *retval = 0; |
2796
|
182 |
|
183 retval = is_valid_function (arg, warn_for, 0); |
|
184 |
|
185 if (! retval) |
|
186 { |
|
187 string s = arg.string_value (); |
|
188 |
|
189 string cmd = header; |
|
190 cmd.append (s); |
|
191 cmd.append (trailer); |
|
192 |
|
193 if (! error_state) |
|
194 { |
|
195 int parse_status; |
|
196 |
2898
|
197 eval_string (cmd, true, parse_status); |
2796
|
198 |
|
199 if (parse_status == 0) |
|
200 { |
|
201 retval = is_valid_function (fname, warn_for, 0); |
|
202 |
|
203 if (! retval) |
|
204 { |
|
205 error ("%s: `%s' is not valid as a function", |
|
206 warn_for.c_str (), fname.c_str ()); |
|
207 return retval; |
|
208 } |
|
209 } |
|
210 else |
|
211 error ("%s: `%s' is not valid as a function", |
|
212 warn_for.c_str (), fname.c_str ()); |
|
213 } |
|
214 else |
|
215 error ("%s: expecting first argument to be a string", |
|
216 warn_for.c_str ()); |
|
217 } |
|
218 |
|
219 return retval; |
|
220 } |
|
221 |
2921
|
222 string_vector |
|
223 get_struct_elts (const string& text) |
|
224 { |
|
225 int n = 1; |
|
226 |
|
227 size_t pos = 0; |
|
228 |
|
229 size_t len = text.length (); |
|
230 |
|
231 while ((pos = text.find ('.', pos)) != NPOS) |
|
232 { |
|
233 if (++pos == len) |
|
234 break; |
|
235 |
|
236 n++; |
|
237 } |
|
238 |
|
239 string_vector retval (n); |
|
240 |
|
241 pos = 0; |
|
242 |
|
243 for (int i = 0; i < n; i++) |
|
244 { |
|
245 size_t len = text.find ('.', pos); |
|
246 |
|
247 if (len != NPOS) |
|
248 len -= pos; |
|
249 |
|
250 retval[i] = text.substr (pos, len); |
|
251 |
|
252 if (len != NPOS) |
|
253 pos += len + 1; |
|
254 } |
|
255 |
|
256 return retval; |
|
257 } |
|
258 |
|
259 string_vector |
|
260 generate_struct_completions (const string& text, string& prefix, string& hint) |
|
261 { |
|
262 string_vector names; |
|
263 |
|
264 size_t pos = text.rfind ('.'); |
|
265 |
|
266 string id; |
|
267 string_vector elts; |
|
268 |
|
269 if (pos == NPOS) |
|
270 { |
|
271 hint = text; |
|
272 prefix = text; |
|
273 elts.resize (1, text); |
|
274 } |
|
275 else |
|
276 { |
|
277 if (pos == text.length ()) |
|
278 hint = ""; |
|
279 else |
|
280 hint = text.substr (pos+1); |
|
281 |
|
282 prefix = text.substr (0, pos); |
|
283 |
|
284 elts = get_struct_elts (prefix); |
|
285 } |
|
286 |
|
287 id = elts[0]; |
|
288 |
|
289 symbol_record *sr = curr_sym_tab->lookup (id); |
|
290 |
|
291 if (! sr) |
|
292 sr = global_sym_tab->lookup (id); |
|
293 |
|
294 if (sr && sr->is_defined ()) |
|
295 { |
2975
|
296 octave_value tmp = sr->def (); |
2921
|
297 |
2963
|
298 // XXX FIXME XXX -- make this work for all types that can do |
|
299 // structure reference operations. |
2975
|
300 if (tmp.is_map ()) |
2921
|
301 { |
|
302 for (int i = 1; i < elts.length (); i++) |
|
303 { |
2975
|
304 tmp = tmp.do_struct_elt_index_op (elts[i], true); |
2921
|
305 |
2975
|
306 if (! tmp.is_map ()) |
2921
|
307 break; |
|
308 } |
|
309 |
2975
|
310 if (tmp.is_map ()) |
2921
|
311 { |
2975
|
312 Octave_map m = tmp.map_value (); |
2921
|
313 |
|
314 names = m.make_name_list (); |
|
315 } |
|
316 } |
|
317 } |
|
318 |
|
319 return names; |
|
320 } |
|
321 |
|
322 bool |
|
323 looks_like_struct (const string& text) |
|
324 { |
|
325 bool retval = true; |
|
326 |
|
327 string_vector elts = get_struct_elts (text); |
|
328 |
|
329 string id = elts[0]; |
|
330 |
|
331 symbol_record *sr = curr_sym_tab->lookup (id); |
|
332 |
|
333 if (! sr) |
|
334 sr = global_sym_tab->lookup (id); |
|
335 |
|
336 if (sr && sr->is_defined ()) |
|
337 { |
2975
|
338 octave_value tmp = sr->def (); |
2921
|
339 |
2963
|
340 // XXX FIXME XXX -- should this work for all types that can do |
|
341 // structure reference operations? |
|
342 |
2975
|
343 if (tmp.is_map ()) |
2921
|
344 { |
|
345 for (int i = 1; i < elts.length (); i++) |
|
346 { |
2975
|
347 tmp = tmp.do_struct_elt_index_op (elts[i], true); |
2921
|
348 |
2975
|
349 if (! tmp.is_map ()) |
2921
|
350 { |
|
351 retval = false; |
|
352 break; |
|
353 } |
|
354 } |
|
355 } |
|
356 else |
|
357 retval = false; |
|
358 } |
|
359 else |
|
360 retval = false; |
|
361 |
|
362 return retval; |
|
363 } |
2796
|
364 |
1957
|
365 DEFUN (is_global, args, , |
593
|
366 "is_global (X): return 1 if the string X names a global variable\n\ |
|
367 otherwise, return 0.") |
|
368 { |
2086
|
369 octave_value_list retval = 0.0; |
593
|
370 |
712
|
371 int nargin = args.length (); |
|
372 |
|
373 if (nargin != 1) |
593
|
374 { |
|
375 print_usage ("is_global"); |
|
376 return retval; |
|
377 } |
|
378 |
1755
|
379 string name = args(0).string_value (); |
593
|
380 |
636
|
381 if (error_state) |
|
382 { |
|
383 error ("is_global: expecting string argument"); |
|
384 return retval; |
|
385 } |
|
386 |
2856
|
387 symbol_record *sr = curr_sym_tab->lookup (name); |
593
|
388 |
2800
|
389 retval = static_cast<double> (sr && sr->is_linked_to_global ()); |
593
|
390 |
|
391 return retval; |
|
392 } |
|
393 |
1957
|
394 DEFUN (exist, args, , |
593
|
395 "exist (NAME): check if variable or file exists\n\ |
|
396 \n\ |
1564
|
397 returns:\n\ |
|
398 \n\ |
|
399 0 : NAME is undefined\n\ |
|
400 1 : NAME is a variable\n\ |
|
401 2 : NAME is a function\n\ |
|
402 3 : NAME is a .oct file in the current LOADPATH\n\ |
3096
|
403 5 : NAME is a built-in function\n\ |
|
404 \n\ |
|
405 This function also returns 2 if a regular file called NAME exists in\n\ |
|
406 Octave's LOADPATH. If you want information about other types of\n\ |
|
407 files, you should use some combination of the functions file_in_path\n\ |
|
408 and stat instead.") |
593
|
409 { |
2086
|
410 octave_value_list retval; |
593
|
411 |
712
|
412 int nargin = args.length (); |
|
413 |
|
414 if (nargin != 1) |
593
|
415 { |
|
416 print_usage ("exist"); |
|
417 return retval; |
|
418 } |
|
419 |
1755
|
420 string name = args(0).string_value (); |
593
|
421 |
636
|
422 if (error_state) |
|
423 { |
|
424 error ("exist: expecting string argument"); |
|
425 return retval; |
|
426 } |
|
427 |
1755
|
428 string struct_elts; |
2790
|
429 string symbol_name = name; |
1755
|
430 |
|
431 size_t pos = name.find ('.'); |
|
432 |
2790
|
433 if (pos != NPOS && pos > 0) |
1277
|
434 { |
1755
|
435 struct_elts = name.substr (pos+1); |
2790
|
436 symbol_name = name.substr (0, pos); |
1277
|
437 } |
|
438 |
2856
|
439 symbol_record *sr = curr_sym_tab->lookup (symbol_name); |
3130
|
440 if (! (sr && sr->is_defined ())) |
2856
|
441 sr = global_sym_tab->lookup (symbol_name); |
593
|
442 |
|
443 retval = 0.0; |
|
444 |
|
445 if (sr && sr->is_variable () && sr->is_defined ()) |
1277
|
446 { |
2390
|
447 if (struct_elts.empty () || sr->is_map_element (struct_elts)) |
|
448 retval = 1.0; |
1277
|
449 } |
1421
|
450 else if (sr && sr->is_builtin_function ()) |
|
451 { |
|
452 retval = 5.0; |
|
453 } |
|
454 else if (sr && sr->is_user_function ()) |
|
455 { |
|
456 retval = 2.0; |
|
457 } |
593
|
458 else |
|
459 { |
1755
|
460 string path = fcn_file_in_path (name); |
|
461 |
|
462 if (path.length () > 0) |
593
|
463 { |
|
464 retval = 2.0; |
|
465 } |
|
466 else |
|
467 { |
1421
|
468 path = oct_file_in_path (name); |
1755
|
469 |
|
470 if (path.length () > 0) |
1421
|
471 { |
|
472 retval = 3.0; |
|
473 } |
|
474 else |
|
475 { |
3096
|
476 string file_name = file_in_path (name, ""); |
1766
|
477 |
3096
|
478 if (! file_name.empty ()) |
|
479 { |
|
480 file_stat fs (file_name); |
|
481 |
|
482 if (fs && fs.is_reg ()) |
|
483 retval = 2.0; |
|
484 } |
1421
|
485 } |
593
|
486 } |
|
487 } |
|
488 |
|
489 return retval; |
|
490 } |
|
491 |
581
|
492 // Is there a corresponding function file that is newer than the |
|
493 // symbol definition? |
|
494 |
2926
|
495 static bool |
1
|
496 symbol_out_of_date (symbol_record *sr) |
|
497 { |
2926
|
498 bool retval = false; |
195
|
499 |
2926
|
500 if (Vignore_function_time_stamp != 2 && sr) |
1
|
501 { |
2975
|
502 octave_value ans = sr->def (); |
|
503 |
3022
|
504 octave_function *tmp = ans.function_value (true); |
2975
|
505 |
3022
|
506 if (tmp) |
|
507 { |
|
508 string ff = tmp->fcn_file_name (); |
1755
|
509 |
3022
|
510 if (! (ff.empty () |
|
511 || (Vignore_function_time_stamp |
|
512 && tmp->is_system_fcn_file ()))) |
|
513 { |
3165
|
514 if (tmp->time_checked () < Vlast_prompt_time) |
|
515 { |
|
516 time_t tp = tmp->time_parsed (); |
2975
|
517 |
3165
|
518 string fname = fcn_file_in_path (ff); |
1755
|
519 |
3165
|
520 tmp->mark_fcn_file_up_to_date (time (0)); |
|
521 |
|
522 file_stat fs (fname); |
3022
|
523 |
3165
|
524 if (fs && fs.is_newer (tp)) |
|
525 retval = true; |
|
526 } |
1
|
527 } |
|
528 } |
|
529 } |
2926
|
530 |
|
531 return retval; |
1
|
532 } |
|
533 |
1827
|
534 bool |
2856
|
535 lookup (symbol_record *sym_rec, bool exec_script) |
581
|
536 { |
1827
|
537 bool script_executed = false; |
581
|
538 |
|
539 if (! sym_rec->is_linked_to_global ()) |
|
540 { |
|
541 if (sym_rec->is_defined ()) |
|
542 { |
|
543 if (sym_rec->is_function () && symbol_out_of_date (sym_rec)) |
1271
|
544 script_executed = load_fcn_from_file (sym_rec, exec_script); |
581
|
545 } |
|
546 else if (! sym_rec->is_formal_parameter ()) |
|
547 { |
|
548 link_to_builtin_or_function (sym_rec); |
1271
|
549 |
581
|
550 if (! sym_rec->is_defined ()) |
1271
|
551 script_executed = load_fcn_from_file (sym_rec, exec_script); |
581
|
552 else if (sym_rec->is_function () && symbol_out_of_date (sym_rec)) |
1271
|
553 script_executed = load_fcn_from_file (sym_rec, exec_script); |
581
|
554 } |
|
555 } |
|
556 |
1271
|
557 return script_executed; |
581
|
558 } |
|
559 |
|
560 // Get the symbol record for the given name that is visible in the |
|
561 // current scope. Reread any function definitions that appear to be |
|
562 // out of date. If a function is available in a file but is not |
|
563 // currently loaded, this will load it and insert the name in the |
|
564 // current symbol table. |
|
565 |
|
566 symbol_record * |
2856
|
567 lookup_by_name (const string& nm, bool exec_script) |
581
|
568 { |
2856
|
569 symbol_record *sym_rec = curr_sym_tab->lookup (nm, true); |
581
|
570 |
|
571 lookup (sym_rec, exec_script); |
|
572 |
|
573 return sym_rec; |
|
574 } |
|
575 |
2849
|
576 octave_value |
|
577 get_global_value (const string& nm) |
|
578 { |
|
579 octave_value retval; |
|
580 |
|
581 symbol_record *sr = global_sym_tab->lookup (nm); |
|
582 |
|
583 if (sr) |
|
584 { |
2975
|
585 octave_value sr_def = sr->def (); |
2849
|
586 |
2975
|
587 if (sr_def.is_undefined ()) |
2849
|
588 error ("get_global_by_name: undefined symbol `%s'", nm.c_str ()); |
3088
|
589 else |
|
590 retval = sr_def; |
2849
|
591 } |
|
592 else |
|
593 error ("get_global_by_name: unknown symbol `%s'", nm.c_str ()); |
|
594 |
|
595 return retval; |
|
596 } |
|
597 |
|
598 void |
|
599 set_global_value (const string& nm, const octave_value& val) |
|
600 { |
2856
|
601 symbol_record *sr = global_sym_tab->lookup (nm, true); |
2849
|
602 |
|
603 if (sr) |
|
604 sr->define (val); |
|
605 else |
|
606 panic_impossible (); |
|
607 } |
|
608 |
593
|
609 // Variable values. |
195
|
610 |
581
|
611 // Look for the given name in the global symbol table. If it refers |
|
612 // to a string, return a new copy. If not, return 0; |
|
613 |
1755
|
614 string |
|
615 builtin_string_variable (const string& name) |
1
|
616 { |
2856
|
617 symbol_record *sr = global_sym_tab->lookup (name); |
195
|
618 |
1271
|
619 // It is a prorgramming error to look for builtins that aren't. |
195
|
620 |
529
|
621 assert (sr); |
195
|
622 |
1755
|
623 string retval; |
1
|
624 |
2975
|
625 octave_value val = sr->def (); |
195
|
626 |
2975
|
627 if (! error_state && val.is_string ()) |
|
628 retval = val.string_value (); |
1
|
629 |
|
630 return retval; |
|
631 } |
|
632 |
581
|
633 // Look for the given name in the global symbol table. If it refers |
1504
|
634 // to a real scalar, place the value in d and return 1. Otherwise, |
|
635 // return 0. |
581
|
636 |
1
|
637 int |
1755
|
638 builtin_real_scalar_variable (const string& name, double& d) |
1
|
639 { |
1504
|
640 int status = 0; |
2856
|
641 symbol_record *sr = global_sym_tab->lookup (name); |
195
|
642 |
1271
|
643 // It is a prorgramming error to look for builtins that aren't. |
195
|
644 |
529
|
645 assert (sr); |
1
|
646 |
2975
|
647 octave_value val = sr->def (); |
195
|
648 |
2975
|
649 if (! error_state && val.is_scalar_type ()) |
|
650 { |
|
651 d = val.double_value (); |
|
652 status = 1; |
1
|
653 } |
|
654 |
|
655 return status; |
|
656 } |
|
657 |
1093
|
658 // Look for the given name in the global symbol table. |
|
659 |
2086
|
660 octave_value |
1755
|
661 builtin_any_variable (const string& name) |
1093
|
662 { |
2856
|
663 symbol_record *sr = global_sym_tab->lookup (name); |
1093
|
664 |
1271
|
665 // It is a prorgramming error to look for builtins that aren't. |
1093
|
666 |
|
667 assert (sr); |
|
668 |
2975
|
669 return sr->def (); |
1093
|
670 } |
|
671 |
593
|
672 // Global stuff and links to builtin variables and functions. |
|
673 |
581
|
674 // Make the definition of the symbol record sr be the same as the |
|
675 // definition of the global variable of the same name, creating it if |
1418
|
676 // it doesn't already exist. |
581
|
677 |
195
|
678 void |
|
679 link_to_global_variable (symbol_record *sr) |
|
680 { |
2975
|
681 if (! sr->is_linked_to_global ()) |
195
|
682 { |
2975
|
683 sr->mark_as_linked_to_global (); |
195
|
684 |
2975
|
685 if (! error_state) |
|
686 { |
|
687 string nm = sr->name (); |
2846
|
688 |
2975
|
689 symbol_record *gsr = global_sym_tab->lookup (nm, true); |
|
690 |
|
691 // There must be a better way to do this. XXX FIXME XXX |
195
|
692 |
2975
|
693 if (sr->is_variable ()) |
|
694 gsr->define (sr->def ()); |
|
695 else |
|
696 sr->clear (); |
1271
|
697 |
2975
|
698 // Make sure this symbol is a variable. |
2900
|
699 |
2975
|
700 if (! gsr->is_variable ()) |
|
701 gsr->define (octave_value ()); |
2900
|
702 |
2975
|
703 sr->alias (gsr, 1); |
|
704 } |
195
|
705 } |
|
706 } |
|
707 |
581
|
708 // Make the definition of the symbol record sr be the same as the |
|
709 // definition of the builtin variable of the same name. |
|
710 |
195
|
711 void |
|
712 link_to_builtin_variable (symbol_record *sr) |
|
713 { |
2856
|
714 symbol_record *tmp_sym = global_sym_tab->lookup (sr->name ()); |
195
|
715 |
529
|
716 if (tmp_sym && tmp_sym->is_builtin_variable ()) |
|
717 sr->alias (tmp_sym); |
195
|
718 } |
|
719 |
581
|
720 // Make the definition of the symbol record sr be the same as the |
|
721 // definition of the builtin variable or function, or user function of |
|
722 // the same name, provided that the name has not been used as a formal |
|
723 // parameter. |
|
724 |
195
|
725 void |
|
726 link_to_builtin_or_function (symbol_record *sr) |
|
727 { |
2856
|
728 symbol_record *tmp_sym = global_sym_tab->lookup (sr->name ()); |
195
|
729 |
529
|
730 if (tmp_sym |
|
731 && (tmp_sym->is_builtin_variable () || tmp_sym->is_function ()) |
|
732 && ! tmp_sym->is_formal_parameter ()) |
|
733 sr->alias (tmp_sym); |
195
|
734 } |
|
735 |
581
|
736 // Force a link to a function in the current symbol table. This is |
|
737 // used just after defining a function to avoid different behavior |
|
738 // depending on whether or not the function has been evaluated after |
|
739 // being defined. |
|
740 // |
|
741 // Return without doing anything if there isn't a function with the |
|
742 // given name defined in the global symbol table. |
|
743 |
195
|
744 void |
1755
|
745 force_link_to_function (const string& id_name) |
195
|
746 { |
2856
|
747 symbol_record *gsr = global_sym_tab->lookup (id_name, true); |
195
|
748 if (gsr->is_function ()) |
|
749 { |
|
750 curr_sym_tab->clear (id_name); |
2856
|
751 symbol_record *csr = curr_sym_tab->lookup (id_name, true); |
195
|
752 csr->alias (gsr); |
|
753 } |
|
754 } |
|
755 |
2294
|
756 DEFUN (document, args, , |
|
757 "document (NAME, STRING)\n\ |
593
|
758 \n\ |
|
759 Associate a cryptic message with a variable name.") |
|
760 { |
2294
|
761 octave_value retval; |
1755
|
762 |
2294
|
763 int nargin = args.length (); |
1755
|
764 |
2294
|
765 if (nargin == 2) |
593
|
766 { |
2294
|
767 string name = args(0).string_value (); |
593
|
768 |
2294
|
769 if (! error_state) |
593
|
770 { |
2294
|
771 string help = args(1).string_value (); |
593
|
772 |
2294
|
773 if (! error_state) |
|
774 { |
|
775 if (is_builtin_variable (name) |
|
776 || is_text_function_name (name) |
|
777 || is_mapper_function_name (name) |
|
778 || is_builtin_function_name (name)) |
|
779 error ("document: can't redefine help for built-in variables and functions"); |
|
780 else |
|
781 { |
2856
|
782 symbol_record *sym_rec = curr_sym_tab->lookup (name); |
2294
|
783 |
|
784 if (sym_rec) |
|
785 sym_rec->document (help); |
|
786 else |
|
787 error ("document: no such symbol `%s'", name.c_str ()); |
|
788 } |
|
789 } |
593
|
790 } |
|
791 } |
|
792 else |
|
793 print_usage ("document"); |
|
794 |
|
795 return retval; |
|
796 } |
|
797 |
584
|
798 // XXX FIXME XXX -- this should take a list of regular expressions |
|
799 // naming the variables to look for. |
|
800 |
2086
|
801 static octave_value_list |
1755
|
802 do_who (int argc, const string_vector& argv) |
529
|
803 { |
2086
|
804 octave_value_list retval; |
529
|
805 |
2856
|
806 bool show_builtins = false; |
|
807 bool show_functions = (curr_sym_tab == top_level_sym_tab); |
|
808 bool show_variables = true; |
|
809 bool show_verbose = false; |
529
|
810 |
1755
|
811 string my_name = argv[0]; |
584
|
812 |
529
|
813 if (argc > 1) |
|
814 { |
2856
|
815 show_functions = false; |
|
816 show_variables = false; |
529
|
817 } |
|
818 |
1857
|
819 int i; |
|
820 for (i = 1; i < argc; i++) |
529
|
821 { |
1755
|
822 if (argv[i] == "-all" || argv[i] == "-a") |
529
|
823 { |
2856
|
824 show_builtins = true; |
|
825 show_functions = true; |
|
826 show_variables = true; |
529
|
827 } |
1755
|
828 else if (argv[i] == "-builtins" || argv[i] == "-b") |
2856
|
829 show_builtins = true; |
1755
|
830 else if (argv[i] == "-functions" || argv[i] == "-f") |
2856
|
831 show_functions = true; |
1755
|
832 else if (argv[i] == "-long" || argv[i] == "-l") |
2856
|
833 show_verbose = true; |
1755
|
834 else if (argv[i] == "-variables" || argv[i] == "-v") |
2856
|
835 show_variables = true; |
1755
|
836 else if (argv[i][0] == '-') |
|
837 warning ("%s: unrecognized option `%s'", my_name.c_str (), |
|
838 argv[i].c_str ()); |
529
|
839 else |
867
|
840 break; |
529
|
841 } |
|
842 |
1857
|
843 int npats = argc - i; |
|
844 string_vector pats (npats); |
|
845 for (int j = 0; j < npats; j++) |
|
846 pats[j] = argv[i+j]; |
|
847 |
1271
|
848 // If the user specified -l and nothing else, show variables. If |
|
849 // evaluating this at the top level, also show functions. |
529
|
850 |
|
851 if (show_verbose && ! (show_builtins || show_functions || show_variables)) |
|
852 { |
|
853 show_functions = (curr_sym_tab == top_level_sym_tab); |
|
854 show_variables = 1; |
|
855 } |
|
856 |
|
857 int pad_after = 0; |
|
858 |
|
859 if (show_builtins) |
|
860 { |
3011
|
861 pad_after += global_sym_tab->maybe_list |
3013
|
862 ("*** built-in variables:", pats, octave_stdout, |
3011
|
863 show_verbose, symbol_record::BUILTIN_VARIABLE, SYMTAB_ALL_SCOPES); |
529
|
864 |
3011
|
865 pad_after += global_sym_tab->maybe_list |
3013
|
866 ("*** built-in functions:", pats, octave_stdout, |
3011
|
867 show_verbose, symbol_record::BUILTIN_FUNCTION, SYMTAB_ALL_SCOPES); |
529
|
868 } |
|
869 |
|
870 if (show_functions) |
|
871 { |
3011
|
872 pad_after += global_sym_tab->maybe_list |
3013
|
873 ("*** currently compiled functions:", pats, |
3011
|
874 octave_stdout, show_verbose, symbol_record::USER_FUNCTION, |
|
875 SYMTAB_ALL_SCOPES); |
529
|
876 } |
|
877 |
|
878 if (show_variables) |
|
879 { |
3011
|
880 pad_after += curr_sym_tab->maybe_list |
3013
|
881 ("*** local user variables:", pats, octave_stdout, |
3011
|
882 show_verbose, symbol_record::USER_VARIABLE, SYMTAB_LOCAL_SCOPE); |
529
|
883 |
3011
|
884 pad_after += curr_sym_tab->maybe_list |
3013
|
885 ("*** globally visible user variables:", pats, |
3011
|
886 octave_stdout, show_verbose, symbol_record::USER_VARIABLE, |
|
887 SYMTAB_GLOBAL_SCOPE); |
529
|
888 } |
|
889 |
|
890 if (pad_after) |
2095
|
891 octave_stdout << "\n"; |
529
|
892 |
581
|
893 return retval; |
|
894 } |
|
895 |
1957
|
896 DEFUN_TEXT (who, args, , |
581
|
897 "who [-all] [-builtins] [-functions] [-long] [-variables]\n\ |
|
898 \n\ |
|
899 List currently defined symbol(s). Options may be shortened to one\n\ |
|
900 character, but may not be combined.") |
|
901 { |
2086
|
902 octave_value_list retval; |
581
|
903 |
1755
|
904 int argc = args.length () + 1; |
|
905 |
1968
|
906 string_vector argv = args.make_argv ("who"); |
1755
|
907 |
|
908 if (error_state) |
|
909 return retval; |
581
|
910 |
1488
|
911 retval = do_who (argc, argv); |
581
|
912 |
529
|
913 return retval; |
|
914 } |
|
915 |
1957
|
916 DEFUN_TEXT (whos, args, , |
581
|
917 "whos [-all] [-builtins] [-functions] [-long] [-variables]\n\ |
|
918 \n\ |
|
919 List currently defined symbol(s). Options may be shortened to one\n\ |
|
920 character, but may not be combined.") |
|
921 { |
2086
|
922 octave_value_list retval; |
581
|
923 |
712
|
924 int nargin = args.length (); |
|
925 |
2086
|
926 octave_value_list tmp_args; |
742
|
927 for (int i = nargin; i > 0; i--) |
|
928 tmp_args(i) = args(i-1); |
|
929 tmp_args(0) = "-long"; |
581
|
930 |
742
|
931 int argc = tmp_args.length () + 1; |
1755
|
932 |
1980
|
933 string_vector argv = tmp_args.make_argv ("whos"); |
581
|
934 |
|
935 if (error_state) |
|
936 return retval; |
|
937 |
1488
|
938 retval = do_who (argc, argv); |
581
|
939 |
|
940 return retval; |
|
941 } |
|
942 |
593
|
943 // Defining variables. |
|
944 |
1162
|
945 void |
2856
|
946 bind_ans (const octave_value& val, bool print) |
1162
|
947 { |
2856
|
948 static symbol_record *sr = global_sym_tab->lookup ("ans", true); |
1162
|
949 |
2978
|
950 if (val.is_defined ()) |
|
951 { |
|
952 sr->define (val); |
1162
|
953 |
2978
|
954 if (print) |
|
955 val.print_with_name (octave_stdout, "ans"); |
|
956 } |
1162
|
957 } |
|
958 |
593
|
959 // Give a global variable a definition. This will insert the symbol |
|
960 // in the global table if necessary. |
|
961 |
|
962 // How is this different than install_builtin_variable? Are both |
|
963 // functions needed? |
|
964 |
|
965 void |
2390
|
966 bind_builtin_variable (const string& varname, const octave_value& val, |
2953
|
967 bool protect, bool eternal, |
3005
|
968 symbol_record::change_function chg_fcn, |
1755
|
969 const string& help) |
593
|
970 { |
2856
|
971 symbol_record *sr = global_sym_tab->lookup (varname, true); |
593
|
972 |
1271
|
973 // It is a programming error for a builtin symbol to be missing. |
|
974 // Besides, we just inserted it, so it must be there. |
593
|
975 |
|
976 assert (sr); |
|
977 |
|
978 sr->unprotect (); |
|
979 |
1271
|
980 // Must do this before define, since define will call the special |
|
981 // variable function only if it knows about it, and it needs to, so |
|
982 // that user prefs can be properly initialized. |
593
|
983 |
3005
|
984 if (chg_fcn) |
|
985 sr->set_change_function (chg_fcn); |
593
|
986 |
|
987 sr->define_builtin_var (val); |
|
988 |
|
989 if (protect) |
|
990 sr->protect (); |
|
991 |
|
992 if (eternal) |
|
993 sr->make_eternal (); |
|
994 |
1755
|
995 sr->document (help); |
529
|
996 } |
|
997 |
593
|
998 // Deleting names from the symbol tables. |
|
999 |
1957
|
1000 DEFUN_TEXT (clear, args, , |
668
|
1001 "clear [-x] [name ...]\n\ |
|
1002 \n\ |
|
1003 Clear symbol(s) matching a list of globbing patterns.\n\ |
593
|
1004 \n\ |
2802
|
1005 If no arguments are given, clear all user-defined variables and\n\ |
668
|
1006 functions.\n\ |
|
1007 \n\ |
|
1008 With -x, exclude the named variables") |
529
|
1009 { |
2086
|
1010 octave_value_list retval; |
593
|
1011 |
1755
|
1012 int argc = args.length () + 1; |
593
|
1013 |
1968
|
1014 string_vector argv = args.make_argv ("clear"); |
1755
|
1015 |
|
1016 if (error_state) |
|
1017 return retval; |
668
|
1018 |
1271
|
1019 // Always clear the local table, but don't clear currently compiled |
|
1020 // functions unless we are at the top level. (Allowing that to |
|
1021 // happen inside functions would result in pretty odd behavior...) |
593
|
1022 |
2856
|
1023 bool clear_user_functions = (curr_sym_tab == top_level_sym_tab); |
593
|
1024 |
1755
|
1025 if (argc == 1) |
593
|
1026 { |
|
1027 curr_sym_tab->clear (); |
|
1028 global_sym_tab->clear (clear_user_functions); |
|
1029 } |
529
|
1030 else |
|
1031 { |
668
|
1032 int exclusive = 0; |
|
1033 |
1755
|
1034 int idx = 1; |
|
1035 |
|
1036 if (argc > 1) |
668
|
1037 { |
1755
|
1038 if (argv[idx] == "-x") |
3125
|
1039 { |
|
1040 idx++; |
|
1041 exclusive = 1; |
|
1042 } |
668
|
1043 } |
|
1044 |
|
1045 int lcount = 0; |
|
1046 int gcount = 0; |
|
1047 int fcount = 0; |
529
|
1048 |
1755
|
1049 string_vector lvars; |
|
1050 string_vector gvars; |
|
1051 string_vector fcns; |
668
|
1052 |
|
1053 if (argc > 0) |
593
|
1054 { |
3013
|
1055 string_vector tmp; |
|
1056 |
|
1057 lvars = curr_sym_tab->name_list (lcount, tmp, false, |
|
1058 SYMTAB_VARIABLES, |
|
1059 SYMTAB_LOCAL_SCOPE); |
668
|
1060 |
3013
|
1061 gvars = curr_sym_tab->name_list (gcount, tmp, false, |
|
1062 SYMTAB_VARIABLES, |
|
1063 SYMTAB_GLOBAL_SCOPE); |
668
|
1064 |
3013
|
1065 fcns = global_sym_tab->name_list (fcount, tmp, false, |
|
1066 symbol_record::USER_FUNCTION, |
|
1067 SYMTAB_ALL_SCOPES); |
668
|
1068 } |
|
1069 |
2438
|
1070 // XXX FIXME XXX -- this needs to be optimized to avoid the |
|
1071 // pattern matching code if the string doesn't contain any |
|
1072 // globbing patterns. |
|
1073 |
1970
|
1074 for (int k = idx; k < argc; k++) |
668
|
1075 { |
1755
|
1076 string patstr = argv[k]; |
668
|
1077 |
1755
|
1078 if (! patstr.empty ()) |
593
|
1079 { |
1792
|
1080 glob_match pattern (patstr); |
1755
|
1081 |
593
|
1082 int i; |
|
1083 for (i = 0; i < lcount; i++) |
|
1084 { |
1755
|
1085 string nm = lvars[i]; |
1792
|
1086 int match = pattern.match (nm); |
668
|
1087 if ((exclusive && ! match) || (! exclusive && match)) |
|
1088 curr_sym_tab->clear (nm); |
593
|
1089 } |
529
|
1090 |
593
|
1091 int count; |
|
1092 for (i = 0; i < gcount; i++) |
|
1093 { |
1755
|
1094 string nm = gvars[i]; |
1792
|
1095 int match = pattern.match (nm); |
668
|
1096 if ((exclusive && ! match) || (! exclusive && match)) |
593
|
1097 { |
668
|
1098 count = curr_sym_tab->clear (nm); |
593
|
1099 if (count > 0) |
668
|
1100 global_sym_tab->clear (nm, clear_user_functions); |
593
|
1101 } |
|
1102 } |
529
|
1103 |
593
|
1104 for (i = 0; i < fcount; i++) |
|
1105 { |
1755
|
1106 string nm = fcns[i]; |
1792
|
1107 int match = pattern.match (nm); |
668
|
1108 if ((exclusive && ! match) || (! exclusive && match)) |
593
|
1109 { |
668
|
1110 count = curr_sym_tab->clear (nm); |
864
|
1111 global_sym_tab->clear (nm, clear_user_functions); |
593
|
1112 } |
|
1113 } |
|
1114 } |
|
1115 } |
|
1116 } |
|
1117 |
|
1118 return retval; |
529
|
1119 } |
|
1120 |
3005
|
1121 DEFUN (__dump_symtab_info__, args, , |
|
1122 "__dump_symtab_info__ (): print raw symbol table statistices") |
|
1123 { |
|
1124 octave_value_list retval; |
|
1125 |
|
1126 int nargin = args.length (); |
|
1127 |
|
1128 if (nargin == 1) |
|
1129 { |
|
1130 string arg = args(0).string_value (); |
|
1131 |
|
1132 if (arg == "global") |
|
1133 global_sym_tab->print_stats (); |
|
1134 else |
|
1135 print_usage ("__dump_symtab_info__"); |
|
1136 } |
|
1137 else if (nargin == 0) |
|
1138 curr_sym_tab->print_stats (); |
|
1139 else |
|
1140 print_usage ("__dump_symtab_info__"); |
|
1141 |
|
1142 return retval; |
|
1143 } |
|
1144 |
3016
|
1145 // XXX FIXME XXX -- some of these should do their own checking to be |
|
1146 // able to provide more meaningful warning or error messages. |
|
1147 |
|
1148 static int |
|
1149 ignore_function_time_stamp (void) |
|
1150 { |
|
1151 int pref = 0; |
|
1152 |
|
1153 string val = builtin_string_variable ("ignore_function_time_stamp"); |
|
1154 |
|
1155 if (! val.empty ()) |
|
1156 { |
|
1157 if (val.compare ("all", 0, 3) == 0) |
|
1158 pref = 2; |
|
1159 if (val.compare ("system", 0, 6) == 0) |
|
1160 pref = 1; |
|
1161 } |
|
1162 |
|
1163 Vignore_function_time_stamp = pref; |
|
1164 |
|
1165 return 0; |
|
1166 } |
|
1167 |
|
1168 // XXX FIXME XXX -- there still may be better places for some of these |
|
1169 // to be defined. |
|
1170 |
|
1171 void |
|
1172 symbols_of_variables (void) |
|
1173 { |
|
1174 DEFVAR (ans, , 0, 0, |
|
1175 ""); |
|
1176 |
|
1177 DEFVAR (ignore_function_time_stamp, "system", 0, ignore_function_time_stamp, |
|
1178 "don't check to see if function files have changed since they were\n\ |
3020
|
1179 last compiled. Possible values are \"system\" and \"all\""); |
3016
|
1180 } |
|
1181 |
1
|
1182 /* |
|
1183 ;;; Local Variables: *** |
|
1184 ;;; mode: C++ *** |
|
1185 ;;; End: *** |
|
1186 */ |