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