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