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