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