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