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