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