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