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