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