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