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 |
1343
|
27 #include <cfloat> |
2144
|
28 #include <cmath> |
1468
|
29 #include <cstdio> |
1343
|
30 #include <cstring> |
605
|
31 |
1728
|
32 #include <string> |
|
33 |
2095
|
34 #include <iostream.h> |
1350
|
35 #include <strstream.h> |
|
36 |
|
37 #ifdef HAVE_UNISTD_H |
2442
|
38 #ifdef HAVE_SYS_TYPES_H |
1
|
39 #include <sys/types.h> |
2442
|
40 #endif |
1
|
41 #include <unistd.h> |
|
42 #endif |
|
43 |
2926
|
44 #include "cmd-edit.h" |
|
45 #include "cmd-hist.h" |
1792
|
46 #include "file-ops.h" |
2926
|
47 #include "file-stat.h" |
2892
|
48 #include "lo-mappers.h" |
2926
|
49 #include "oct-env.h" |
|
50 #include "glob-match.h" |
1755
|
51 #include "str-vec.h" |
|
52 |
2492
|
53 #include <defaults.h> |
1352
|
54 #include "defun.h" |
|
55 #include "dirfns.h" |
704
|
56 #include "dynamic-ld.h" |
1352
|
57 #include "error.h" |
2233
|
58 #include "fn-cache.h" |
2205
|
59 #include "gripes.h" |
1352
|
60 #include "help.h" |
|
61 #include "input.h" |
|
62 #include "lex.h" |
2926
|
63 #include "sysdep.h" |
1742
|
64 #include "oct-hist.h" |
2926
|
65 #include "oct-map.h" |
2975
|
66 #include "ov-mapper.h" |
2926
|
67 #include "oct-obj.h" |
|
68 #include "ov.h" |
605
|
69 #include "pager.h" |
1352
|
70 #include "parse.h" |
2892
|
71 #include "pt-id.h" |
|
72 #include "pt-indir.h" |
2926
|
73 #include "symtab.h" |
2205
|
74 #include "toplev.h" |
1352
|
75 #include "unwind-prot.h" |
1
|
76 #include "utils.h" |
1352
|
77 #include "variables.h" |
2492
|
78 #include <version.h> |
529
|
79 |
2205
|
80 // Echo commands as they are executed? |
|
81 // |
|
82 // 1 ==> echo commands read from script files |
|
83 // 2 ==> echo commands from functions |
|
84 // 4 ==> echo commands read from command line |
|
85 // |
|
86 // more than one state can be active at once. |
|
87 int Vecho_executing_commands; |
|
88 |
|
89 // Should Octave always check to see if function files have changed |
|
90 // since they were last compiled? |
2806
|
91 static int Vignore_function_time_stamp; |
2205
|
92 |
1
|
93 // Symbol table for symbols at the top level. |
581
|
94 symbol_table *top_level_sym_tab = 0; |
1
|
95 |
|
96 // Symbol table for the current scope. |
581
|
97 symbol_table *curr_sym_tab = 0; |
1
|
98 |
|
99 // Symbol table for global symbols. |
581
|
100 symbol_table *global_sym_tab = 0; |
1
|
101 |
593
|
102 // Initialization. |
|
103 |
|
104 // Create the initial symbol tables and set the current scope at the |
|
105 // top level. |
|
106 |
195
|
107 void |
|
108 initialize_symbol_tables (void) |
|
109 { |
581
|
110 if (! global_sym_tab) |
3005
|
111 global_sym_tab = new symbol_table (2048); |
195
|
112 |
581
|
113 if (! top_level_sym_tab) |
3005
|
114 top_level_sym_tab = new symbol_table (4096); |
195
|
115 |
|
116 curr_sym_tab = top_level_sym_tab; |
|
117 } |
|
118 |
593
|
119 // Attributes of variables and functions. |
|
120 |
|
121 // Is this variable a builtin? |
|
122 |
1827
|
123 bool |
1755
|
124 is_builtin_variable (const string& name) |
593
|
125 { |
2856
|
126 symbol_record *sr = global_sym_tab->lookup (name); |
593
|
127 return (sr && sr->is_builtin_variable ()); |
|
128 } |
|
129 |
|
130 // Is this a text-style function? |
|
131 |
1827
|
132 bool |
1755
|
133 is_text_function_name (const string& s) |
593
|
134 { |
|
135 symbol_record *sr = global_sym_tab->lookup (s); |
|
136 return (sr && sr->is_text_function ()); |
|
137 } |
|
138 |
2294
|
139 // Is this a mapper function? |
|
140 |
|
141 bool |
|
142 is_builtin_function_name (const string& s) |
|
143 { |
|
144 symbol_record *sr = global_sym_tab->lookup (s); |
|
145 return (sr && sr->is_builtin_function ()); |
|
146 } |
|
147 |
|
148 // Is this a mapper function? |
|
149 |
|
150 bool |
|
151 is_mapper_function_name (const string& s) |
|
152 { |
|
153 symbol_record *sr = global_sym_tab->lookup (s); |
|
154 return (sr && sr->is_mapper_function ()); |
|
155 } |
|
156 |
593
|
157 // Is this function globally in this scope? |
|
158 |
1827
|
159 bool |
1755
|
160 is_globally_visible (const string& name) |
593
|
161 { |
2856
|
162 symbol_record *sr = curr_sym_tab->lookup (name); |
593
|
163 return (sr && sr->is_linked_to_global ()); |
|
164 } |
|
165 |
2086
|
166 // Is this octave_value a valid function? |
593
|
167 |
2975
|
168 octave_function * |
2856
|
169 is_valid_function (const octave_value& arg, const string& warn_for, bool warn) |
593
|
170 { |
2975
|
171 octave_function *ans = 0; |
593
|
172 |
1755
|
173 string fcn_name; |
1728
|
174 |
1517
|
175 if (arg.is_string ()) |
1755
|
176 fcn_name = arg.string_value (); |
1517
|
177 |
1755
|
178 if (fcn_name.empty () || error_state) |
593
|
179 { |
|
180 if (warn) |
1755
|
181 error ("%s: expecting function name as argument", |
|
182 warn_for.c_str ()); |
593
|
183 return ans; |
|
184 } |
|
185 |
|
186 symbol_record *sr = 0; |
1755
|
187 |
|
188 if (! fcn_name.empty ()) |
593
|
189 sr = lookup_by_name (fcn_name); |
|
190 |
|
191 if (sr) |
2975
|
192 { |
|
193 octave_value tmp = sr->def (); |
|
194 ans = tmp.function_value (true); |
|
195 } |
593
|
196 |
|
197 if (! sr || ! ans || ! sr->is_function ()) |
|
198 { |
|
199 if (warn) |
|
200 error ("%s: the symbol `%s' is not valid as a function", |
1755
|
201 warn_for.c_str (), fcn_name.c_str ()); |
593
|
202 ans = 0; |
|
203 } |
|
204 |
|
205 return ans; |
|
206 } |
|
207 |
2975
|
208 octave_function * |
2796
|
209 extract_function (const octave_value& arg, const string& warn_for, |
|
210 const string& fname, const string& header, |
|
211 const string& trailer) |
|
212 { |
2975
|
213 octave_function *retval = 0; |
2796
|
214 |
|
215 retval = is_valid_function (arg, warn_for, 0); |
|
216 |
|
217 if (! retval) |
|
218 { |
|
219 string s = arg.string_value (); |
|
220 |
|
221 string cmd = header; |
|
222 cmd.append (s); |
|
223 cmd.append (trailer); |
|
224 |
|
225 if (! error_state) |
|
226 { |
|
227 int parse_status; |
|
228 |
2898
|
229 eval_string (cmd, true, parse_status); |
2796
|
230 |
|
231 if (parse_status == 0) |
|
232 { |
|
233 retval = is_valid_function (fname, warn_for, 0); |
|
234 |
|
235 if (! retval) |
|
236 { |
|
237 error ("%s: `%s' is not valid as a function", |
|
238 warn_for.c_str (), fname.c_str ()); |
|
239 return retval; |
|
240 } |
|
241 } |
|
242 else |
|
243 error ("%s: `%s' is not valid as a function", |
|
244 warn_for.c_str (), fname.c_str ()); |
|
245 } |
|
246 else |
|
247 error ("%s: expecting first argument to be a string", |
|
248 warn_for.c_str ()); |
|
249 } |
|
250 |
|
251 return retval; |
|
252 } |
|
253 |
2921
|
254 string_vector |
|
255 get_struct_elts (const string& text) |
|
256 { |
|
257 int n = 1; |
|
258 |
|
259 size_t pos = 0; |
|
260 |
|
261 size_t len = text.length (); |
|
262 |
|
263 while ((pos = text.find ('.', pos)) != NPOS) |
|
264 { |
|
265 if (++pos == len) |
|
266 break; |
|
267 |
|
268 n++; |
|
269 } |
|
270 |
|
271 string_vector retval (n); |
|
272 |
|
273 pos = 0; |
|
274 |
|
275 for (int i = 0; i < n; i++) |
|
276 { |
|
277 size_t len = text.find ('.', pos); |
|
278 |
|
279 if (len != NPOS) |
|
280 len -= pos; |
|
281 |
|
282 retval[i] = text.substr (pos, len); |
|
283 |
|
284 if (len != NPOS) |
|
285 pos += len + 1; |
|
286 } |
|
287 |
|
288 return retval; |
|
289 } |
|
290 |
|
291 string_vector |
|
292 generate_struct_completions (const string& text, string& prefix, string& hint) |
|
293 { |
|
294 string_vector names; |
|
295 |
|
296 size_t pos = text.rfind ('.'); |
|
297 |
|
298 string id; |
|
299 string_vector elts; |
|
300 |
|
301 if (pos == NPOS) |
|
302 { |
|
303 hint = text; |
|
304 prefix = text; |
|
305 elts.resize (1, text); |
|
306 } |
|
307 else |
|
308 { |
|
309 if (pos == text.length ()) |
|
310 hint = ""; |
|
311 else |
|
312 hint = text.substr (pos+1); |
|
313 |
|
314 prefix = text.substr (0, pos); |
|
315 |
|
316 elts = get_struct_elts (prefix); |
|
317 } |
|
318 |
|
319 id = elts[0]; |
|
320 |
|
321 symbol_record *sr = curr_sym_tab->lookup (id); |
|
322 |
|
323 if (! sr) |
|
324 sr = global_sym_tab->lookup (id); |
|
325 |
|
326 if (sr && sr->is_defined ()) |
|
327 { |
2975
|
328 octave_value tmp = sr->def (); |
2921
|
329 |
2963
|
330 // XXX FIXME XXX -- make this work for all types that can do |
|
331 // structure reference operations. |
2975
|
332 if (tmp.is_map ()) |
2921
|
333 { |
|
334 for (int i = 1; i < elts.length (); i++) |
|
335 { |
2975
|
336 tmp = tmp.do_struct_elt_index_op (elts[i], true); |
2921
|
337 |
2975
|
338 if (! tmp.is_map ()) |
2921
|
339 break; |
|
340 } |
|
341 |
2975
|
342 if (tmp.is_map ()) |
2921
|
343 { |
2975
|
344 Octave_map m = tmp.map_value (); |
2921
|
345 |
|
346 names = m.make_name_list (); |
|
347 } |
|
348 } |
|
349 } |
|
350 |
|
351 return names; |
|
352 } |
|
353 |
|
354 bool |
|
355 looks_like_struct (const string& text) |
|
356 { |
|
357 bool retval = true; |
|
358 |
|
359 string_vector elts = get_struct_elts (text); |
|
360 |
|
361 string id = elts[0]; |
|
362 |
|
363 symbol_record *sr = curr_sym_tab->lookup (id); |
|
364 |
|
365 if (! sr) |
|
366 sr = global_sym_tab->lookup (id); |
|
367 |
|
368 if (sr && sr->is_defined ()) |
|
369 { |
2975
|
370 octave_value tmp = sr->def (); |
2921
|
371 |
2963
|
372 // XXX FIXME XXX -- should this work for all types that can do |
|
373 // structure reference operations? |
|
374 |
2975
|
375 if (tmp.is_map ()) |
2921
|
376 { |
|
377 for (int i = 1; i < elts.length (); i++) |
|
378 { |
2975
|
379 tmp = tmp.do_struct_elt_index_op (elts[i], true); |
2921
|
380 |
2975
|
381 if (! tmp.is_map ()) |
2921
|
382 { |
|
383 retval = false; |
|
384 break; |
|
385 } |
|
386 } |
|
387 } |
|
388 else |
|
389 retval = false; |
|
390 } |
|
391 else |
|
392 retval = false; |
|
393 |
|
394 return retval; |
|
395 } |
2796
|
396 |
1957
|
397 DEFUN (is_global, args, , |
593
|
398 "is_global (X): return 1 if the string X names a global variable\n\ |
|
399 otherwise, return 0.") |
|
400 { |
2086
|
401 octave_value_list retval = 0.0; |
593
|
402 |
712
|
403 int nargin = args.length (); |
|
404 |
|
405 if (nargin != 1) |
593
|
406 { |
|
407 print_usage ("is_global"); |
|
408 return retval; |
|
409 } |
|
410 |
1755
|
411 string name = args(0).string_value (); |
593
|
412 |
636
|
413 if (error_state) |
|
414 { |
|
415 error ("is_global: expecting string argument"); |
|
416 return retval; |
|
417 } |
|
418 |
2856
|
419 symbol_record *sr = curr_sym_tab->lookup (name); |
593
|
420 |
2800
|
421 retval = static_cast<double> (sr && sr->is_linked_to_global ()); |
593
|
422 |
|
423 return retval; |
|
424 } |
|
425 |
1957
|
426 DEFUN (exist, args, , |
593
|
427 "exist (NAME): check if variable or file exists\n\ |
|
428 \n\ |
1564
|
429 returns:\n\ |
|
430 \n\ |
|
431 0 : NAME is undefined\n\ |
|
432 1 : NAME is a variable\n\ |
|
433 2 : NAME is a function\n\ |
|
434 3 : NAME is a .oct file in the current LOADPATH\n\ |
|
435 5 : NAME is a built-in function") |
593
|
436 { |
2086
|
437 octave_value_list retval; |
593
|
438 |
712
|
439 int nargin = args.length (); |
|
440 |
|
441 if (nargin != 1) |
593
|
442 { |
|
443 print_usage ("exist"); |
|
444 return retval; |
|
445 } |
|
446 |
1755
|
447 string name = args(0).string_value (); |
593
|
448 |
636
|
449 if (error_state) |
|
450 { |
|
451 error ("exist: expecting string argument"); |
|
452 return retval; |
|
453 } |
|
454 |
1755
|
455 string struct_elts; |
2790
|
456 string symbol_name = name; |
1755
|
457 |
|
458 size_t pos = name.find ('.'); |
|
459 |
2790
|
460 if (pos != NPOS && pos > 0) |
1277
|
461 { |
1755
|
462 struct_elts = name.substr (pos+1); |
2790
|
463 symbol_name = name.substr (0, pos); |
1277
|
464 } |
|
465 |
2856
|
466 symbol_record *sr = curr_sym_tab->lookup (symbol_name); |
593
|
467 if (! sr) |
2856
|
468 sr = global_sym_tab->lookup (symbol_name); |
593
|
469 |
|
470 retval = 0.0; |
|
471 |
|
472 if (sr && sr->is_variable () && sr->is_defined ()) |
1277
|
473 { |
2390
|
474 if (struct_elts.empty () || sr->is_map_element (struct_elts)) |
|
475 retval = 1.0; |
1277
|
476 } |
1421
|
477 else if (sr && sr->is_builtin_function ()) |
|
478 { |
|
479 retval = 5.0; |
|
480 } |
|
481 else if (sr && sr->is_user_function ()) |
|
482 { |
|
483 retval = 2.0; |
|
484 } |
593
|
485 else |
|
486 { |
1755
|
487 string path = fcn_file_in_path (name); |
|
488 |
|
489 if (path.length () > 0) |
593
|
490 { |
|
491 retval = 2.0; |
|
492 } |
|
493 else |
|
494 { |
1421
|
495 path = oct_file_in_path (name); |
1755
|
496 |
|
497 if (path.length () > 0) |
1421
|
498 { |
|
499 retval = 3.0; |
|
500 } |
|
501 else |
|
502 { |
1766
|
503 file_stat fs (name); |
|
504 |
|
505 if (fs && fs.is_reg ()) |
1421
|
506 retval = 2.0; |
|
507 } |
593
|
508 } |
|
509 } |
|
510 |
|
511 return retval; |
|
512 } |
|
513 |
581
|
514 // Is there a corresponding function file that is newer than the |
|
515 // symbol definition? |
|
516 |
2926
|
517 static bool |
1
|
518 symbol_out_of_date (symbol_record *sr) |
|
519 { |
2926
|
520 bool retval = false; |
195
|
521 |
2926
|
522 if (Vignore_function_time_stamp != 2 && sr) |
1
|
523 { |
2975
|
524 octave_value ans = sr->def (); |
|
525 |
|
526 if (! Vignore_function_time_stamp && ans.is_defined ()) |
1
|
527 { |
2975
|
528 octave_function *tmp = ans.function_value (true); |
|
529 |
|
530 if (tmp && tmp->is_system_fcn_file ()) |
1
|
531 { |
2975
|
532 string ff = tmp->fcn_file_name (); |
1755
|
533 |
2975
|
534 if (! ff.empty ()) |
|
535 { |
|
536 time_t tp = tmp->time_parsed (); |
1755
|
537 |
2975
|
538 string fname = fcn_file_in_path (ff); |
|
539 |
|
540 int status = file_stat::is_newer (fname, tp); |
1755
|
541 |
2975
|
542 if (status > 0) |
|
543 retval = true; |
|
544 } |
1
|
545 } |
|
546 } |
|
547 } |
2926
|
548 |
|
549 return retval; |
1
|
550 } |
|
551 |
2926
|
552 static bool |
1755
|
553 looks_like_octave_copyright (const string& s) |
991
|
554 { |
2926
|
555 bool retval = false; |
|
556 |
1755
|
557 string t = s.substr (0, 15); |
|
558 |
|
559 if (t == " Copyright (C) ") |
991
|
560 { |
1755
|
561 size_t pos = s.find ('\n'); |
|
562 |
|
563 if (pos != NPOS) |
991
|
564 { |
1755
|
565 pos = s.find ('\n', pos + 1); |
|
566 |
|
567 if (pos != NPOS) |
991
|
568 { |
1755
|
569 pos++; |
|
570 |
|
571 t = s.substr (pos, 29); |
|
572 |
|
573 if (t == " This file is part of Octave." |
1983
|
574 || t == " This program is free softwar") |
2926
|
575 retval = true; |
991
|
576 } |
|
577 } |
|
578 } |
2926
|
579 |
|
580 return retval; |
991
|
581 } |
|
582 |
1419
|
583 // Eat whitespace and comments from FFILE, returning the text of the |
|
584 // comments read if it doesn't look like a copyright notice. If |
|
585 // IN_PARTS, consider each block of comments separately; otherwise, |
2487
|
586 // grab them all at once. If UPDATE_POS is TRUE, line and column |
|
587 // number information is updated. |
1418
|
588 |
2300
|
589 // XXX FIXME XXX -- grab_help_text() in lex.l duplicates some of this |
|
590 // code! |
|
591 |
1755
|
592 static string |
2487
|
593 gobble_leading_white_space (FILE *ffile, bool in_parts, bool update_pos) |
581
|
594 { |
1755
|
595 string help_txt; |
991
|
596 |
2300
|
597 bool first_comments_seen = false; |
|
598 bool begin_comment = false; |
|
599 bool have_help_text = false; |
|
600 bool in_comment = false; |
581
|
601 int c; |
1755
|
602 |
581
|
603 while ((c = getc (ffile)) != EOF) |
|
604 { |
2487
|
605 if (update_pos) |
|
606 current_input_column++; |
1755
|
607 |
2300
|
608 if (begin_comment) |
|
609 { |
|
610 if (c == '%' || c == '#') |
|
611 continue; |
|
612 else |
|
613 begin_comment = false; |
|
614 } |
|
615 |
581
|
616 if (in_comment) |
|
617 { |
991
|
618 if (! have_help_text) |
|
619 { |
2300
|
620 first_comments_seen = true; |
1755
|
621 help_txt += (char) c; |
991
|
622 } |
|
623 |
581
|
624 if (c == '\n') |
984
|
625 { |
2487
|
626 if (update_pos) |
|
627 { |
|
628 input_line_number++; |
|
629 current_input_column = 0; |
|
630 } |
2300
|
631 in_comment = false; |
1755
|
632 |
1419
|
633 if (in_parts) |
1418
|
634 { |
1419
|
635 if ((c = getc (ffile)) != EOF) |
|
636 { |
2487
|
637 if (update_pos) |
|
638 current_input_column--; |
1419
|
639 ungetc (c, ffile); |
|
640 if (c == '\n') |
|
641 break; |
|
642 } |
|
643 else |
1418
|
644 break; |
|
645 } |
984
|
646 } |
581
|
647 } |
|
648 else |
|
649 { |
984
|
650 switch (c) |
581
|
651 { |
984
|
652 case ' ': |
|
653 case '\t': |
991
|
654 if (first_comments_seen) |
2300
|
655 have_help_text = true; |
984
|
656 break; |
|
657 |
|
658 case '\n': |
993
|
659 if (first_comments_seen) |
2300
|
660 have_help_text = true; |
2487
|
661 if (update_pos) |
|
662 { |
|
663 input_line_number++; |
|
664 current_input_column = 0; |
|
665 } |
984
|
666 continue; |
|
667 |
|
668 case '%': |
|
669 case '#': |
2300
|
670 begin_comment = true; |
|
671 in_comment = true; |
984
|
672 break; |
|
673 |
|
674 default: |
2487
|
675 if (update_pos) |
|
676 current_input_column--; |
581
|
677 ungetc (c, ffile); |
991
|
678 goto done; |
581
|
679 } |
|
680 } |
|
681 } |
991
|
682 |
|
683 done: |
|
684 |
1755
|
685 if (! help_txt.empty ()) |
991
|
686 { |
1419
|
687 if (looks_like_octave_copyright (help_txt)) |
1755
|
688 help_txt.resize (0); |
1419
|
689 |
1755
|
690 if (in_parts && help_txt.empty ()) |
2487
|
691 help_txt = gobble_leading_white_space (ffile, in_parts, update_pos); |
1418
|
692 } |
|
693 |
991
|
694 return help_txt; |
581
|
695 } |
|
696 |
|
697 static int |
|
698 is_function_file (FILE *ffile) |
|
699 { |
|
700 int status = 0; |
|
701 |
|
702 long pos = ftell (ffile); |
|
703 |
2487
|
704 gobble_leading_white_space (ffile, false, false); |
2474
|
705 |
581
|
706 char buf [10]; |
|
707 fgets (buf, 10, ffile); |
|
708 int len = strlen (buf); |
|
709 if (len > 8 && strncmp (buf, "function", 8) == 0 |
|
710 && ! (isalnum (buf[8]) || buf[8] == '_')) |
|
711 status = 1; |
|
712 |
|
713 fseek (ffile, pos, SEEK_SET); |
|
714 |
|
715 return status; |
|
716 } |
|
717 |
1907
|
718 static void |
|
719 restore_command_history (void *) |
|
720 { |
2926
|
721 command_history::ignore_entries (! Vsaving_history); |
1907
|
722 } |
|
723 |
2517
|
724 static void |
|
725 safe_fclose (void *f) |
|
726 { |
|
727 if (f) |
2800
|
728 fclose (static_cast<FILE *> (f)); |
2517
|
729 } |
|
730 |
2926
|
731 static void |
|
732 restore_input_stream (void *f) |
|
733 { |
|
734 command_editor::set_input_stream (static_cast<FILE *> (f)); |
|
735 } |
|
736 |
581
|
737 static int |
2856
|
738 parse_fcn_file (bool exec_script, const string& ff) |
581
|
739 { |
2985
|
740 unwind_protect::begin_frame ("parse_fcn_file"); |
581
|
741 |
|
742 int script_file_executed = 0; |
|
743 |
1358
|
744 // Open function file and parse. |
581
|
745 |
|
746 int old_reading_fcn_file_state = reading_fcn_file; |
|
747 |
2926
|
748 FILE *in_stream = command_editor::get_input_stream (); |
|
749 |
2985
|
750 unwind_protect::add (restore_input_stream, in_stream); |
2926
|
751 |
581
|
752 unwind_protect_ptr (ff_instream); |
|
753 |
2926
|
754 unwind_protect_int (line_editing); |
581
|
755 unwind_protect_int (input_line_number); |
|
756 unwind_protect_int (current_input_column); |
|
757 unwind_protect_int (reading_fcn_file); |
|
758 |
2926
|
759 line_editing = 0; |
581
|
760 reading_fcn_file = 1; |
|
761 input_line_number = 0; |
|
762 current_input_column = 1; |
|
763 |
|
764 FILE *ffile = get_input_from_file (ff, 0); |
|
765 |
2985
|
766 unwind_protect::add (safe_fclose, ffile); |
2517
|
767 |
581
|
768 if (ffile) |
|
769 { |
1271
|
770 // Check to see if this file defines a function or is just a |
|
771 // list of commands. |
581
|
772 |
|
773 if (is_function_file (ffile)) |
|
774 { |
1907
|
775 // XXX FIXME XXX -- we shouldn't need both the |
2926
|
776 // command_history object and the |
2205
|
777 // Vsaving_history variable... |
2926
|
778 command_history::ignore_entries (); |
1907
|
779 |
2985
|
780 unwind_protect::add (restore_command_history, 0); |
1907
|
781 |
2205
|
782 unwind_protect_int (Vecho_executing_commands); |
|
783 unwind_protect_int (Vsaving_history); |
581
|
784 unwind_protect_int (reading_fcn_file); |
1516
|
785 unwind_protect_int (input_from_command_line_file); |
581
|
786 |
2205
|
787 Vecho_executing_commands = ECHO_OFF; |
|
788 Vsaving_history = 0; |
581
|
789 reading_fcn_file = 1; |
1516
|
790 input_from_command_line_file = 0; |
581
|
791 |
|
792 YY_BUFFER_STATE old_buf = current_buffer (); |
|
793 YY_BUFFER_STATE new_buf = create_buffer (ffile); |
|
794 |
2985
|
795 unwind_protect::add (restore_input_buffer, (void *) old_buf); |
|
796 unwind_protect::add (delete_input_buffer, (void *) new_buf); |
581
|
797 |
|
798 switch_to_buffer (new_buf); |
|
799 |
|
800 unwind_protect_ptr (curr_sym_tab); |
|
801 |
|
802 reset_parser (); |
|
803 |
2487
|
804 help_buf = gobble_leading_white_space (ffile, true, true); |
2474
|
805 |
|
806 // XXX FIXME XXX -- this should not be necessary. |
2487
|
807 gobble_leading_white_space (ffile, false, true); |
991
|
808 |
581
|
809 int status = yyparse (); |
|
810 |
|
811 if (status != 0) |
|
812 { |
1755
|
813 error ("parse error while reading function file %s", |
|
814 ff.c_str ()); |
581
|
815 global_sym_tab->clear (curr_fcn_file_name); |
|
816 } |
|
817 } |
|
818 else if (exec_script) |
|
819 { |
1271
|
820 // The value of `reading_fcn_file' will be restored to the |
|
821 // proper value when we unwind from this frame. |
581
|
822 reading_fcn_file = old_reading_fcn_file_state; |
|
823 |
1952
|
824 // XXX FIXME XXX -- we shouldn't need both the |
2926
|
825 // command_history object and the |
2205
|
826 // Vsaving_history variable... |
2926
|
827 command_history::ignore_entries (); |
1952
|
828 |
2985
|
829 unwind_protect::add (restore_command_history, 0); |
1952
|
830 |
2205
|
831 unwind_protect_int (Vsaving_history); |
581
|
832 unwind_protect_int (reading_script_file); |
1952
|
833 |
2205
|
834 Vsaving_history = 0; |
581
|
835 reading_script_file = 1; |
|
836 |
2898
|
837 parse_and_execute (ffile); |
581
|
838 |
|
839 script_file_executed = 1; |
|
840 } |
|
841 } |
|
842 |
2985
|
843 unwind_protect::run_frame ("parse_fcn_file"); |
581
|
844 |
|
845 return script_file_executed; |
|
846 } |
|
847 |
1827
|
848 static bool |
2856
|
849 load_fcn_from_file (symbol_record *sym_rec, bool exec_script) |
581
|
850 { |
1827
|
851 bool script_file_executed = false; |
581
|
852 |
1755
|
853 string nm = sym_rec->name (); |
581
|
854 |
2894
|
855 if (octave_dynamic_loader::load_fcn_from_dot_oct_file (nm)) |
704
|
856 { |
|
857 force_link_to_function (nm); |
|
858 } |
|
859 else |
581
|
860 { |
1755
|
861 string ff = fcn_file_in_path (nm); |
581
|
862 |
1606
|
863 // These are needed by yyparse. |
|
864 |
2985
|
865 unwind_protect::begin_frame ("load_fcn_from_file"); |
1755
|
866 |
|
867 unwind_protect_str (curr_fcn_file_name); |
|
868 unwind_protect_str (curr_fcn_file_full_name); |
|
869 |
1606
|
870 curr_fcn_file_name = nm; |
|
871 curr_fcn_file_full_name = ff; |
|
872 |
1755
|
873 if (ff.length () > 0) |
|
874 script_file_executed = parse_fcn_file (exec_script, ff); |
581
|
875 |
|
876 if (! (error_state || script_file_executed)) |
|
877 force_link_to_function (nm); |
1755
|
878 |
2985
|
879 unwind_protect::run_frame ("load_fcn_from_file"); |
581
|
880 } |
|
881 |
|
882 return script_file_executed; |
|
883 } |
|
884 |
1827
|
885 bool |
2856
|
886 lookup (symbol_record *sym_rec, bool exec_script) |
581
|
887 { |
1827
|
888 bool script_executed = false; |
581
|
889 |
|
890 if (! sym_rec->is_linked_to_global ()) |
|
891 { |
|
892 if (sym_rec->is_defined ()) |
|
893 { |
|
894 if (sym_rec->is_function () && symbol_out_of_date (sym_rec)) |
1271
|
895 script_executed = load_fcn_from_file (sym_rec, exec_script); |
581
|
896 } |
|
897 else if (! sym_rec->is_formal_parameter ()) |
|
898 { |
|
899 link_to_builtin_or_function (sym_rec); |
1271
|
900 |
581
|
901 if (! sym_rec->is_defined ()) |
1271
|
902 script_executed = load_fcn_from_file (sym_rec, exec_script); |
581
|
903 else if (sym_rec->is_function () && symbol_out_of_date (sym_rec)) |
1271
|
904 script_executed = load_fcn_from_file (sym_rec, exec_script); |
581
|
905 } |
|
906 } |
|
907 |
1271
|
908 return script_executed; |
581
|
909 } |
|
910 |
|
911 // Get the symbol record for the given name that is visible in the |
|
912 // current scope. Reread any function definitions that appear to be |
|
913 // out of date. If a function is available in a file but is not |
|
914 // currently loaded, this will load it and insert the name in the |
|
915 // current symbol table. |
|
916 |
|
917 symbol_record * |
2856
|
918 lookup_by_name (const string& nm, bool exec_script) |
581
|
919 { |
2856
|
920 symbol_record *sym_rec = curr_sym_tab->lookup (nm, true); |
581
|
921 |
|
922 lookup (sym_rec, exec_script); |
|
923 |
|
924 return sym_rec; |
|
925 } |
|
926 |
2849
|
927 octave_value |
|
928 get_global_value (const string& nm) |
|
929 { |
|
930 octave_value retval; |
|
931 |
|
932 symbol_record *sr = global_sym_tab->lookup (nm); |
|
933 |
|
934 if (sr) |
|
935 { |
2975
|
936 octave_value sr_def = sr->def (); |
2849
|
937 |
2975
|
938 if (sr_def.is_undefined ()) |
2849
|
939 error ("get_global_by_name: undefined symbol `%s'", nm.c_str ()); |
|
940 } |
|
941 else |
|
942 error ("get_global_by_name: unknown symbol `%s'", nm.c_str ()); |
|
943 |
|
944 return retval; |
|
945 } |
|
946 |
|
947 void |
|
948 set_global_value (const string& nm, const octave_value& val) |
|
949 { |
2856
|
950 symbol_record *sr = global_sym_tab->lookup (nm, true); |
2849
|
951 |
|
952 if (sr) |
|
953 sr->define (val); |
|
954 else |
|
955 panic_impossible (); |
|
956 } |
|
957 |
1755
|
958 string |
|
959 get_help_from_file (const string& path) |
991
|
960 { |
1755
|
961 string retval; |
|
962 |
|
963 if (! path.empty ()) |
991
|
964 { |
1755
|
965 FILE *fptr = fopen (path.c_str (), "r"); |
|
966 |
991
|
967 if (fptr) |
|
968 { |
2985
|
969 unwind_protect::add (safe_fclose, (void *) fptr); |
2517
|
970 |
2487
|
971 retval = gobble_leading_white_space (fptr, true, true); |
2517
|
972 |
2985
|
973 unwind_protect::run (); |
991
|
974 } |
|
975 } |
1755
|
976 |
|
977 return retval; |
991
|
978 } |
|
979 |
593
|
980 // Variable values. |
195
|
981 |
581
|
982 // Look for the given name in the global symbol table. If it refers |
|
983 // to a string, return a new copy. If not, return 0; |
|
984 |
1755
|
985 string |
|
986 builtin_string_variable (const string& name) |
1
|
987 { |
2856
|
988 symbol_record *sr = global_sym_tab->lookup (name); |
195
|
989 |
1271
|
990 // It is a prorgramming error to look for builtins that aren't. |
195
|
991 |
529
|
992 assert (sr); |
195
|
993 |
1755
|
994 string retval; |
1
|
995 |
2975
|
996 octave_value val = sr->def (); |
195
|
997 |
2975
|
998 if (! error_state && val.is_string ()) |
|
999 retval = val.string_value (); |
1
|
1000 |
|
1001 return retval; |
|
1002 } |
|
1003 |
581
|
1004 // Look for the given name in the global symbol table. If it refers |
1504
|
1005 // to a real scalar, place the value in d and return 1. Otherwise, |
|
1006 // return 0. |
581
|
1007 |
1
|
1008 int |
1755
|
1009 builtin_real_scalar_variable (const string& name, double& d) |
1
|
1010 { |
1504
|
1011 int status = 0; |
2856
|
1012 symbol_record *sr = global_sym_tab->lookup (name); |
195
|
1013 |
1271
|
1014 // It is a prorgramming error to look for builtins that aren't. |
195
|
1015 |
529
|
1016 assert (sr); |
1
|
1017 |
2975
|
1018 octave_value val = sr->def (); |
195
|
1019 |
2975
|
1020 if (! error_state && val.is_scalar_type ()) |
|
1021 { |
|
1022 d = val.double_value (); |
|
1023 status = 1; |
1
|
1024 } |
|
1025 |
|
1026 return status; |
|
1027 } |
|
1028 |
1093
|
1029 // Look for the given name in the global symbol table. |
|
1030 |
2086
|
1031 octave_value |
1755
|
1032 builtin_any_variable (const string& name) |
1093
|
1033 { |
2856
|
1034 symbol_record *sr = global_sym_tab->lookup (name); |
1093
|
1035 |
1271
|
1036 // It is a prorgramming error to look for builtins that aren't. |
1093
|
1037 |
|
1038 assert (sr); |
|
1039 |
2975
|
1040 return sr->def (); |
1093
|
1041 } |
|
1042 |
593
|
1043 // Global stuff and links to builtin variables and functions. |
|
1044 |
581
|
1045 // Make the definition of the symbol record sr be the same as the |
|
1046 // definition of the global variable of the same name, creating it if |
1418
|
1047 // it doesn't already exist. |
581
|
1048 |
195
|
1049 void |
|
1050 link_to_global_variable (symbol_record *sr) |
|
1051 { |
2975
|
1052 if (! sr->is_linked_to_global ()) |
195
|
1053 { |
2975
|
1054 sr->mark_as_linked_to_global (); |
195
|
1055 |
2975
|
1056 if (! error_state) |
|
1057 { |
|
1058 string nm = sr->name (); |
2846
|
1059 |
2975
|
1060 symbol_record *gsr = global_sym_tab->lookup (nm, true); |
|
1061 |
|
1062 // There must be a better way to do this. XXX FIXME XXX |
195
|
1063 |
2975
|
1064 if (sr->is_variable ()) |
|
1065 gsr->define (sr->def ()); |
|
1066 else |
|
1067 sr->clear (); |
1271
|
1068 |
2975
|
1069 // Make sure this symbol is a variable. |
2900
|
1070 |
2975
|
1071 if (! gsr->is_variable ()) |
|
1072 gsr->define (octave_value ()); |
2900
|
1073 |
2975
|
1074 sr->alias (gsr, 1); |
|
1075 } |
195
|
1076 } |
|
1077 } |
|
1078 |
581
|
1079 // Make the definition of the symbol record sr be the same as the |
|
1080 // definition of the builtin variable of the same name. |
|
1081 |
195
|
1082 void |
|
1083 link_to_builtin_variable (symbol_record *sr) |
|
1084 { |
2856
|
1085 symbol_record *tmp_sym = global_sym_tab->lookup (sr->name ()); |
195
|
1086 |
529
|
1087 if (tmp_sym && tmp_sym->is_builtin_variable ()) |
|
1088 sr->alias (tmp_sym); |
195
|
1089 } |
|
1090 |
581
|
1091 // Make the definition of the symbol record sr be the same as the |
|
1092 // definition of the builtin variable or function, or user function of |
|
1093 // the same name, provided that the name has not been used as a formal |
|
1094 // parameter. |
|
1095 |
195
|
1096 void |
|
1097 link_to_builtin_or_function (symbol_record *sr) |
|
1098 { |
2856
|
1099 symbol_record *tmp_sym = global_sym_tab->lookup (sr->name ()); |
195
|
1100 |
529
|
1101 if (tmp_sym |
|
1102 && (tmp_sym->is_builtin_variable () || tmp_sym->is_function ()) |
|
1103 && ! tmp_sym->is_formal_parameter ()) |
|
1104 sr->alias (tmp_sym); |
195
|
1105 } |
|
1106 |
581
|
1107 // Force a link to a function in the current symbol table. This is |
|
1108 // used just after defining a function to avoid different behavior |
|
1109 // depending on whether or not the function has been evaluated after |
|
1110 // being defined. |
|
1111 // |
|
1112 // Return without doing anything if there isn't a function with the |
|
1113 // given name defined in the global symbol table. |
|
1114 |
195
|
1115 void |
1755
|
1116 force_link_to_function (const string& id_name) |
195
|
1117 { |
2856
|
1118 symbol_record *gsr = global_sym_tab->lookup (id_name, true); |
195
|
1119 if (gsr->is_function ()) |
|
1120 { |
|
1121 curr_sym_tab->clear (id_name); |
2856
|
1122 symbol_record *csr = curr_sym_tab->lookup (id_name, true); |
195
|
1123 csr->alias (gsr); |
|
1124 } |
|
1125 } |
|
1126 |
2294
|
1127 DEFUN (document, args, , |
|
1128 "document (NAME, STRING)\n\ |
593
|
1129 \n\ |
|
1130 Associate a cryptic message with a variable name.") |
|
1131 { |
2294
|
1132 octave_value retval; |
1755
|
1133 |
2294
|
1134 int nargin = args.length (); |
1755
|
1135 |
2294
|
1136 if (nargin == 2) |
593
|
1137 { |
2294
|
1138 string name = args(0).string_value (); |
593
|
1139 |
2294
|
1140 if (! error_state) |
593
|
1141 { |
2294
|
1142 string help = args(1).string_value (); |
593
|
1143 |
2294
|
1144 if (! error_state) |
|
1145 { |
|
1146 if (is_builtin_variable (name) |
|
1147 || is_text_function_name (name) |
|
1148 || is_mapper_function_name (name) |
|
1149 || is_builtin_function_name (name)) |
|
1150 error ("document: can't redefine help for built-in variables and functions"); |
|
1151 else |
|
1152 { |
2856
|
1153 symbol_record *sym_rec = curr_sym_tab->lookup (name); |
2294
|
1154 |
|
1155 if (sym_rec) |
|
1156 sym_rec->document (help); |
|
1157 else |
|
1158 error ("document: no such symbol `%s'", name.c_str ()); |
|
1159 } |
|
1160 } |
593
|
1161 } |
|
1162 } |
|
1163 else |
|
1164 print_usage ("document"); |
|
1165 |
|
1166 return retval; |
|
1167 } |
|
1168 |
584
|
1169 // XXX FIXME XXX -- this should take a list of regular expressions |
|
1170 // naming the variables to look for. |
|
1171 |
2086
|
1172 static octave_value_list |
1755
|
1173 do_who (int argc, const string_vector& argv) |
529
|
1174 { |
2086
|
1175 octave_value_list retval; |
529
|
1176 |
2856
|
1177 bool show_builtins = false; |
|
1178 bool show_functions = (curr_sym_tab == top_level_sym_tab); |
|
1179 bool show_variables = true; |
|
1180 bool show_verbose = false; |
529
|
1181 |
1755
|
1182 string my_name = argv[0]; |
584
|
1183 |
529
|
1184 if (argc > 1) |
|
1185 { |
2856
|
1186 show_functions = false; |
|
1187 show_variables = false; |
529
|
1188 } |
|
1189 |
1857
|
1190 int i; |
|
1191 for (i = 1; i < argc; i++) |
529
|
1192 { |
1755
|
1193 if (argv[i] == "-all" || argv[i] == "-a") |
529
|
1194 { |
2856
|
1195 show_builtins = true; |
|
1196 show_functions = true; |
|
1197 show_variables = true; |
529
|
1198 } |
1755
|
1199 else if (argv[i] == "-builtins" || argv[i] == "-b") |
2856
|
1200 show_builtins = true; |
1755
|
1201 else if (argv[i] == "-functions" || argv[i] == "-f") |
2856
|
1202 show_functions = true; |
1755
|
1203 else if (argv[i] == "-long" || argv[i] == "-l") |
2856
|
1204 show_verbose = true; |
1755
|
1205 else if (argv[i] == "-variables" || argv[i] == "-v") |
2856
|
1206 show_variables = true; |
1755
|
1207 else if (argv[i][0] == '-') |
|
1208 warning ("%s: unrecognized option `%s'", my_name.c_str (), |
|
1209 argv[i].c_str ()); |
529
|
1210 else |
867
|
1211 break; |
529
|
1212 } |
|
1213 |
1857
|
1214 int npats = argc - i; |
|
1215 string_vector pats (npats); |
|
1216 for (int j = 0; j < npats; j++) |
|
1217 pats[j] = argv[i+j]; |
|
1218 |
1271
|
1219 // If the user specified -l and nothing else, show variables. If |
|
1220 // evaluating this at the top level, also show functions. |
529
|
1221 |
|
1222 if (show_verbose && ! (show_builtins || show_functions || show_variables)) |
|
1223 { |
|
1224 show_functions = (curr_sym_tab == top_level_sym_tab); |
|
1225 show_variables = 1; |
|
1226 } |
|
1227 |
|
1228 int pad_after = 0; |
|
1229 |
|
1230 if (show_builtins) |
|
1231 { |
3011
|
1232 pad_after += global_sym_tab->maybe_list |
3013
|
1233 ("*** built-in variables:", pats, octave_stdout, |
3011
|
1234 show_verbose, symbol_record::BUILTIN_VARIABLE, SYMTAB_ALL_SCOPES); |
529
|
1235 |
3011
|
1236 pad_after += global_sym_tab->maybe_list |
3013
|
1237 ("*** built-in functions:", pats, octave_stdout, |
3011
|
1238 show_verbose, symbol_record::BUILTIN_FUNCTION, SYMTAB_ALL_SCOPES); |
529
|
1239 } |
|
1240 |
|
1241 if (show_functions) |
|
1242 { |
3011
|
1243 pad_after += global_sym_tab->maybe_list |
3013
|
1244 ("*** currently compiled functions:", pats, |
3011
|
1245 octave_stdout, show_verbose, symbol_record::USER_FUNCTION, |
|
1246 SYMTAB_ALL_SCOPES); |
529
|
1247 } |
|
1248 |
|
1249 if (show_variables) |
|
1250 { |
3011
|
1251 pad_after += curr_sym_tab->maybe_list |
3013
|
1252 ("*** local user variables:", pats, octave_stdout, |
3011
|
1253 show_verbose, symbol_record::USER_VARIABLE, SYMTAB_LOCAL_SCOPE); |
529
|
1254 |
3011
|
1255 pad_after += curr_sym_tab->maybe_list |
3013
|
1256 ("*** globally visible user variables:", pats, |
3011
|
1257 octave_stdout, show_verbose, symbol_record::USER_VARIABLE, |
|
1258 SYMTAB_GLOBAL_SCOPE); |
529
|
1259 } |
|
1260 |
|
1261 if (pad_after) |
2095
|
1262 octave_stdout << "\n"; |
529
|
1263 |
581
|
1264 return retval; |
|
1265 } |
|
1266 |
1957
|
1267 DEFUN_TEXT (who, args, , |
581
|
1268 "who [-all] [-builtins] [-functions] [-long] [-variables]\n\ |
|
1269 \n\ |
|
1270 List currently defined symbol(s). Options may be shortened to one\n\ |
|
1271 character, but may not be combined.") |
|
1272 { |
2086
|
1273 octave_value_list retval; |
581
|
1274 |
1755
|
1275 int argc = args.length () + 1; |
|
1276 |
1968
|
1277 string_vector argv = args.make_argv ("who"); |
1755
|
1278 |
|
1279 if (error_state) |
|
1280 return retval; |
581
|
1281 |
1488
|
1282 retval = do_who (argc, argv); |
581
|
1283 |
529
|
1284 return retval; |
|
1285 } |
|
1286 |
1957
|
1287 DEFUN_TEXT (whos, args, , |
581
|
1288 "whos [-all] [-builtins] [-functions] [-long] [-variables]\n\ |
|
1289 \n\ |
|
1290 List currently defined symbol(s). Options may be shortened to one\n\ |
|
1291 character, but may not be combined.") |
|
1292 { |
2086
|
1293 octave_value_list retval; |
581
|
1294 |
712
|
1295 int nargin = args.length (); |
|
1296 |
2086
|
1297 octave_value_list tmp_args; |
742
|
1298 for (int i = nargin; i > 0; i--) |
|
1299 tmp_args(i) = args(i-1); |
|
1300 tmp_args(0) = "-long"; |
581
|
1301 |
742
|
1302 int argc = tmp_args.length () + 1; |
1755
|
1303 |
1980
|
1304 string_vector argv = tmp_args.make_argv ("whos"); |
581
|
1305 |
|
1306 if (error_state) |
|
1307 return retval; |
|
1308 |
1488
|
1309 retval = do_who (argc, argv); |
581
|
1310 |
|
1311 return retval; |
|
1312 } |
|
1313 |
593
|
1314 // Defining variables. |
|
1315 |
1162
|
1316 void |
2856
|
1317 bind_ans (const octave_value& val, bool print) |
1162
|
1318 { |
2856
|
1319 static symbol_record *sr = global_sym_tab->lookup ("ans", true); |
1162
|
1320 |
2978
|
1321 if (val.is_defined ()) |
|
1322 { |
|
1323 sr->define (val); |
1162
|
1324 |
2978
|
1325 if (print) |
|
1326 val.print_with_name (octave_stdout, "ans"); |
|
1327 } |
1162
|
1328 } |
|
1329 |
1489
|
1330 void |
|
1331 bind_global_error_variable (void) |
|
1332 { |
|
1333 *error_message_buffer << ends; |
|
1334 |
|
1335 char *error_text = error_message_buffer->str (); |
|
1336 |
|
1337 bind_builtin_variable ("__error_text__", error_text, 1); |
|
1338 |
|
1339 delete [] error_text; |
|
1340 |
|
1341 delete error_message_buffer; |
|
1342 |
|
1343 error_message_buffer = 0; |
|
1344 } |
|
1345 |
|
1346 void |
|
1347 clear_global_error_variable (void *) |
|
1348 { |
|
1349 delete error_message_buffer; |
|
1350 error_message_buffer = 0; |
|
1351 |
|
1352 bind_builtin_variable ("__error_text__", "", 1); |
|
1353 } |
|
1354 |
593
|
1355 // Give a global variable a definition. This will insert the symbol |
|
1356 // in the global table if necessary. |
|
1357 |
|
1358 // How is this different than install_builtin_variable? Are both |
|
1359 // functions needed? |
|
1360 |
|
1361 void |
2390
|
1362 bind_builtin_variable (const string& varname, const octave_value& val, |
2953
|
1363 bool protect, bool eternal, |
3005
|
1364 symbol_record::change_function chg_fcn, |
1755
|
1365 const string& help) |
593
|
1366 { |
2856
|
1367 symbol_record *sr = global_sym_tab->lookup (varname, true); |
593
|
1368 |
1271
|
1369 // It is a programming error for a builtin symbol to be missing. |
|
1370 // Besides, we just inserted it, so it must be there. |
593
|
1371 |
|
1372 assert (sr); |
|
1373 |
|
1374 sr->unprotect (); |
|
1375 |
1271
|
1376 // Must do this before define, since define will call the special |
|
1377 // variable function only if it knows about it, and it needs to, so |
|
1378 // that user prefs can be properly initialized. |
593
|
1379 |
3005
|
1380 if (chg_fcn) |
|
1381 sr->set_change_function (chg_fcn); |
593
|
1382 |
|
1383 sr->define_builtin_var (val); |
|
1384 |
|
1385 if (protect) |
|
1386 sr->protect (); |
|
1387 |
|
1388 if (eternal) |
|
1389 sr->make_eternal (); |
|
1390 |
1755
|
1391 sr->document (help); |
529
|
1392 } |
|
1393 |
593
|
1394 // Deleting names from the symbol tables. |
|
1395 |
1957
|
1396 DEFUN_TEXT (clear, args, , |
668
|
1397 "clear [-x] [name ...]\n\ |
|
1398 \n\ |
|
1399 Clear symbol(s) matching a list of globbing patterns.\n\ |
593
|
1400 \n\ |
2802
|
1401 If no arguments are given, clear all user-defined variables and\n\ |
668
|
1402 functions.\n\ |
|
1403 \n\ |
|
1404 With -x, exclude the named variables") |
529
|
1405 { |
2086
|
1406 octave_value_list retval; |
593
|
1407 |
1755
|
1408 int argc = args.length () + 1; |
593
|
1409 |
1968
|
1410 string_vector argv = args.make_argv ("clear"); |
1755
|
1411 |
|
1412 if (error_state) |
|
1413 return retval; |
668
|
1414 |
1271
|
1415 // Always clear the local table, but don't clear currently compiled |
|
1416 // functions unless we are at the top level. (Allowing that to |
|
1417 // happen inside functions would result in pretty odd behavior...) |
593
|
1418 |
2856
|
1419 bool clear_user_functions = (curr_sym_tab == top_level_sym_tab); |
593
|
1420 |
1755
|
1421 if (argc == 1) |
593
|
1422 { |
|
1423 curr_sym_tab->clear (); |
|
1424 global_sym_tab->clear (clear_user_functions); |
|
1425 } |
529
|
1426 else |
|
1427 { |
668
|
1428 int exclusive = 0; |
|
1429 |
1755
|
1430 int idx = 1; |
|
1431 |
|
1432 if (argc > 1) |
668
|
1433 { |
1755
|
1434 if (argv[idx] == "-x") |
|
1435 exclusive = 1; |
668
|
1436 } |
|
1437 |
|
1438 int lcount = 0; |
|
1439 int gcount = 0; |
|
1440 int fcount = 0; |
529
|
1441 |
1755
|
1442 string_vector lvars; |
|
1443 string_vector gvars; |
|
1444 string_vector fcns; |
668
|
1445 |
|
1446 if (argc > 0) |
593
|
1447 { |
3013
|
1448 string_vector tmp; |
|
1449 |
|
1450 lvars = curr_sym_tab->name_list (lcount, tmp, false, |
|
1451 SYMTAB_VARIABLES, |
|
1452 SYMTAB_LOCAL_SCOPE); |
668
|
1453 |
3013
|
1454 gvars = curr_sym_tab->name_list (gcount, tmp, false, |
|
1455 SYMTAB_VARIABLES, |
|
1456 SYMTAB_GLOBAL_SCOPE); |
668
|
1457 |
3013
|
1458 fcns = global_sym_tab->name_list (fcount, tmp, false, |
|
1459 symbol_record::USER_FUNCTION, |
|
1460 SYMTAB_ALL_SCOPES); |
668
|
1461 } |
|
1462 |
2438
|
1463 // XXX FIXME XXX -- this needs to be optimized to avoid the |
|
1464 // pattern matching code if the string doesn't contain any |
|
1465 // globbing patterns. |
|
1466 |
1970
|
1467 for (int k = idx; k < argc; k++) |
668
|
1468 { |
1755
|
1469 string patstr = argv[k]; |
668
|
1470 |
1755
|
1471 if (! patstr.empty ()) |
593
|
1472 { |
1792
|
1473 glob_match pattern (patstr); |
1755
|
1474 |
593
|
1475 int i; |
|
1476 for (i = 0; i < lcount; i++) |
|
1477 { |
1755
|
1478 string nm = lvars[i]; |
1792
|
1479 int match = pattern.match (nm); |
668
|
1480 if ((exclusive && ! match) || (! exclusive && match)) |
|
1481 curr_sym_tab->clear (nm); |
593
|
1482 } |
529
|
1483 |
593
|
1484 int count; |
|
1485 for (i = 0; i < gcount; i++) |
|
1486 { |
1755
|
1487 string nm = gvars[i]; |
1792
|
1488 int match = pattern.match (nm); |
668
|
1489 if ((exclusive && ! match) || (! exclusive && match)) |
593
|
1490 { |
668
|
1491 count = curr_sym_tab->clear (nm); |
593
|
1492 if (count > 0) |
668
|
1493 global_sym_tab->clear (nm, clear_user_functions); |
593
|
1494 } |
|
1495 } |
529
|
1496 |
593
|
1497 for (i = 0; i < fcount; i++) |
|
1498 { |
1755
|
1499 string nm = fcns[i]; |
1792
|
1500 int match = pattern.match (nm); |
668
|
1501 if ((exclusive && ! match) || (! exclusive && match)) |
593
|
1502 { |
668
|
1503 count = curr_sym_tab->clear (nm); |
864
|
1504 global_sym_tab->clear (nm, clear_user_functions); |
593
|
1505 } |
|
1506 } |
|
1507 } |
|
1508 } |
|
1509 } |
|
1510 |
|
1511 return retval; |
529
|
1512 } |
|
1513 |
3005
|
1514 DEFUN (__dump_symtab_info__, args, , |
|
1515 "__dump_symtab_info__ (): print raw symbol table statistices") |
|
1516 { |
|
1517 octave_value_list retval; |
|
1518 |
|
1519 int nargin = args.length (); |
|
1520 |
|
1521 if (nargin == 1) |
|
1522 { |
|
1523 string arg = args(0).string_value (); |
|
1524 |
|
1525 if (arg == "global") |
|
1526 global_sym_tab->print_stats (); |
|
1527 else |
|
1528 print_usage ("__dump_symtab_info__"); |
|
1529 } |
|
1530 else if (nargin == 0) |
|
1531 curr_sym_tab->print_stats (); |
|
1532 else |
|
1533 print_usage ("__dump_symtab_info__"); |
|
1534 |
|
1535 return retval; |
|
1536 } |
|
1537 |
3016
|
1538 // XXX FIXME XXX -- some of these should do their own checking to be |
|
1539 // able to provide more meaningful warning or error messages. |
|
1540 |
|
1541 static int |
|
1542 echo_executing_commands (void) |
|
1543 { |
|
1544 Vecho_executing_commands = check_preference ("echo_executing_commands"); |
|
1545 |
|
1546 return 0; |
|
1547 } |
|
1548 |
|
1549 static int |
|
1550 ignore_function_time_stamp (void) |
|
1551 { |
|
1552 int pref = 0; |
|
1553 |
|
1554 string val = builtin_string_variable ("ignore_function_time_stamp"); |
|
1555 |
|
1556 if (! val.empty ()) |
|
1557 { |
|
1558 if (val.compare ("all", 0, 3) == 0) |
|
1559 pref = 2; |
|
1560 if (val.compare ("system", 0, 6) == 0) |
|
1561 pref = 1; |
|
1562 } |
|
1563 |
|
1564 Vignore_function_time_stamp = pref; |
|
1565 |
|
1566 return 0; |
|
1567 } |
|
1568 |
|
1569 // XXX FIXME XXX -- there still may be better places for some of these |
|
1570 // to be defined. |
|
1571 |
|
1572 void |
|
1573 symbols_of_variables (void) |
|
1574 { |
|
1575 DEFVAR (ans, , 0, 0, |
|
1576 ""); |
|
1577 |
|
1578 DEFCONST (argv, , 0, 0, |
|
1579 "the command line arguments this program was invoked with"); |
|
1580 |
|
1581 DEFVAR (echo_executing_commands, static_cast<double> (ECHO_OFF), 0, |
|
1582 echo_executing_commands, |
|
1583 "echo commands as they are executed"); |
|
1584 |
|
1585 DEFCONST (error_text, "", 0, 0, |
|
1586 "the text of error messages that would have been printed in the\n\ |
|
1587 body of the most recent unwind_protect statement or the TRY part of\n\ |
|
1588 the most recent eval() command. Outside of unwind_protect and\n\ |
|
1589 eval(), or if no error has ocurred within them, the value of\n\ |
|
1590 __error_text__ is guaranteed to be the empty string."); |
|
1591 |
|
1592 DEFVAR (ignore_function_time_stamp, "system", 0, ignore_function_time_stamp, |
|
1593 "don't check to see if function files have changed since they were\n\ |
|
1594 last compiled. Possible values are \"system\" and \"all\""); |
|
1595 |
|
1596 DEFCONST (program_invocation_name, |
|
1597 octave_env::get_program_invocation_name (), 0, 0, |
|
1598 "the full name of the current program or script, including the\n\ |
|
1599 directory specification"); |
|
1600 |
|
1601 DEFCONST (program_name, octave_env::get_program_name (), 0, 0, |
|
1602 "the name of the current program or script"); |
|
1603 } |
|
1604 |
1
|
1605 /* |
|
1606 ;;; Local Variables: *** |
|
1607 ;;; mode: C++ *** |
|
1608 ;;; End: *** |
|
1609 */ |