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