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