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