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 |
2846
|
959 if (sr->is_static ()) |
|
960 { |
|
961 error ("can't make static variable `%s' global", nm.c_str ()); |
|
962 return; |
|
963 } |
|
964 |
1271
|
965 // There must be a better way to do this. XXX FIXME XXX |
195
|
966 |
|
967 if (sr->is_variable ()) |
|
968 { |
1271
|
969 // Would be nice not to have this cast. XXX FIXME XXX |
|
970 |
2390
|
971 tree_constant *tmp = (tree_constant *) sr->def (); |
529
|
972 if (tmp) |
2390
|
973 tmp = new tree_constant (*tmp); |
529
|
974 else |
2390
|
975 tmp = new tree_constant (); |
195
|
976 gsr->define (tmp); |
|
977 } |
|
978 else |
529
|
979 sr->clear (); |
195
|
980 |
1271
|
981 // If the global symbol is currently defined as a function, we need |
|
982 // to hide it with a variable. |
195
|
983 |
|
984 if (gsr->is_function ()) |
2390
|
985 gsr->define ((tree_constant *) 0); |
195
|
986 |
|
987 sr->alias (gsr, 1); |
|
988 sr->mark_as_linked_to_global (); |
|
989 } |
|
990 |
581
|
991 // Make the definition of the symbol record sr be the same as the |
|
992 // definition of the builtin variable of the same name. |
|
993 |
195
|
994 void |
|
995 link_to_builtin_variable (symbol_record *sr) |
|
996 { |
|
997 symbol_record *tmp_sym = global_sym_tab->lookup (sr->name (), 0, 0); |
|
998 |
529
|
999 if (tmp_sym && tmp_sym->is_builtin_variable ()) |
|
1000 sr->alias (tmp_sym); |
195
|
1001 } |
|
1002 |
581
|
1003 // Make the definition of the symbol record sr be the same as the |
|
1004 // definition of the builtin variable or function, or user function of |
|
1005 // the same name, provided that the name has not been used as a formal |
|
1006 // parameter. |
|
1007 |
195
|
1008 void |
|
1009 link_to_builtin_or_function (symbol_record *sr) |
|
1010 { |
|
1011 symbol_record *tmp_sym = global_sym_tab->lookup (sr->name (), 0, 0); |
|
1012 |
529
|
1013 if (tmp_sym |
|
1014 && (tmp_sym->is_builtin_variable () || tmp_sym->is_function ()) |
|
1015 && ! tmp_sym->is_formal_parameter ()) |
|
1016 sr->alias (tmp_sym); |
195
|
1017 } |
|
1018 |
581
|
1019 // Force a link to a function in the current symbol table. This is |
|
1020 // used just after defining a function to avoid different behavior |
|
1021 // depending on whether or not the function has been evaluated after |
|
1022 // being defined. |
|
1023 // |
|
1024 // Return without doing anything if there isn't a function with the |
|
1025 // given name defined in the global symbol table. |
|
1026 |
195
|
1027 void |
1755
|
1028 force_link_to_function (const string& id_name) |
195
|
1029 { |
|
1030 symbol_record *gsr = global_sym_tab->lookup (id_name, 1, 0); |
|
1031 if (gsr->is_function ()) |
|
1032 { |
|
1033 curr_sym_tab->clear (id_name); |
|
1034 symbol_record *csr = curr_sym_tab->lookup (id_name, 1, 0); |
|
1035 csr->alias (gsr); |
|
1036 } |
|
1037 } |
|
1038 |
605
|
1039 // Help stuff. Shouldn't this go in help.cc? |
593
|
1040 |
|
1041 // It's not likely that this does the right thing now. XXX FIXME XXX |
|
1042 |
1755
|
1043 string_vector |
593
|
1044 make_name_list (void) |
|
1045 { |
|
1046 int key_len = 0; |
|
1047 int glb_len = 0; |
|
1048 int top_len = 0; |
|
1049 int lcl_len = 0; |
|
1050 |
1755
|
1051 string_vector key; |
|
1052 string_vector glb; |
|
1053 string_vector top; |
|
1054 string_vector lcl; |
|
1055 string_vector ffl; |
593
|
1056 |
1271
|
1057 // Each of these functions returns a new vector of pointers to new |
|
1058 // strings. |
593
|
1059 |
|
1060 key = names (keyword_help (), key_len); |
1795
|
1061 |
593
|
1062 glb = global_sym_tab->list (glb_len); |
1795
|
1063 |
593
|
1064 top = top_level_sym_tab->list (top_len); |
1795
|
1065 |
593
|
1066 if (top_level_sym_tab != curr_sym_tab) |
|
1067 lcl = curr_sym_tab->list (lcl_len); |
1795
|
1068 |
2233
|
1069 ffl = octave_fcn_file_name_cache::list_no_suffix (); |
1795
|
1070 int ffl_len = ffl.length (); |
593
|
1071 |
|
1072 int total_len = key_len + glb_len + top_len + lcl_len + ffl_len; |
|
1073 |
1755
|
1074 string_vector list (total_len); |
1418
|
1075 |
1271
|
1076 // Put all the symbols in one big list. Only copy pointers, not the |
|
1077 // strings they point to, then only delete the original array of |
|
1078 // pointers, and not the strings they point to. |
593
|
1079 |
|
1080 int j = 0; |
|
1081 int i = 0; |
|
1082 for (i = 0; i < key_len; i++) |
|
1083 list[j++] = key[i]; |
|
1084 |
|
1085 for (i = 0; i < glb_len; i++) |
|
1086 list[j++] = glb[i]; |
|
1087 |
|
1088 for (i = 0; i < top_len; i++) |
|
1089 list[j++] = top[i]; |
|
1090 |
|
1091 for (i = 0; i < lcl_len; i++) |
|
1092 list[j++] = lcl[i]; |
|
1093 |
|
1094 for (i = 0; i < ffl_len; i++) |
|
1095 list[j++] = ffl[i]; |
|
1096 |
|
1097 return list; |
|
1098 } |
|
1099 |
|
1100 // List variable names. |
|
1101 |
|
1102 static void |
2095
|
1103 print_symbol_info_line (ostream& os, const symbol_record_info& s) |
593
|
1104 { |
2095
|
1105 os << (s.is_read_only () ? " -" : " w"); |
|
1106 os << (s.is_eternal () ? "- " : "d "); |
593
|
1107 #if 0 |
2095
|
1108 os << (s.hides_fcn () ? "f" : (s.hides_builtin () ? "F" : "-")); |
593
|
1109 #endif |
2390
|
1110 os.form (" %-16s", s.type_name ().c_str ()); |
2404
|
1111 |
|
1112 int nr = s.rows (); |
|
1113 int nc = s.columns (); |
|
1114 |
|
1115 if (nr < 0) |
|
1116 os << " -"; |
593
|
1117 else |
2404
|
1118 os.form ("%7d", nr); |
|
1119 |
|
1120 if (nc < 0) |
|
1121 os << " -"; |
|
1122 else |
|
1123 os.form ("%7d", nc); |
|
1124 |
2095
|
1125 os << " " << s.name () << "\n"; |
593
|
1126 } |
|
1127 |
|
1128 static void |
2095
|
1129 print_long_listing (ostream& os, symbol_record_info *s) |
593
|
1130 { |
|
1131 if (! s) |
|
1132 return; |
|
1133 |
|
1134 symbol_record_info *ptr = s; |
|
1135 while (ptr->is_defined ()) |
|
1136 { |
2095
|
1137 print_symbol_info_line (os, *ptr); |
593
|
1138 ptr++; |
|
1139 } |
|
1140 } |
|
1141 |
|
1142 static int |
1755
|
1143 maybe_list (const char *header, const string_vector& argv, int argc, |
2095
|
1144 ostream& os, int show_verbose, symbol_table |
867
|
1145 *sym_tab, unsigned type, unsigned scope) |
593
|
1146 { |
|
1147 int count; |
|
1148 int status = 0; |
|
1149 if (show_verbose) |
|
1150 { |
|
1151 symbol_record_info *symbols; |
867
|
1152 symbols = sym_tab->long_list (count, argv, argc, 1, type, scope); |
593
|
1153 if (symbols && count > 0) |
|
1154 { |
2095
|
1155 os << "\n" << header << "\n\n" |
593
|
1156 << "prot type rows cols name\n" |
|
1157 << "==== ==== ==== ==== ====\n"; |
|
1158 |
2095
|
1159 print_long_listing (os, symbols); |
593
|
1160 status = 1; |
|
1161 } |
|
1162 delete [] symbols; |
|
1163 } |
|
1164 else |
|
1165 { |
1755
|
1166 string_vector symbols = sym_tab->list (count, argv, argc, 1, |
|
1167 type, scope); |
|
1168 if (symbols.length () > 0 && count > 0) |
593
|
1169 { |
2095
|
1170 os << "\n" << header << "\n\n"; |
|
1171 symbols.list_in_columns (os); |
593
|
1172 status = 1; |
|
1173 } |
|
1174 } |
|
1175 return status; |
|
1176 } |
|
1177 |
2294
|
1178 DEFUN (document, args, , |
|
1179 "document (NAME, STRING)\n\ |
593
|
1180 \n\ |
|
1181 Associate a cryptic message with a variable name.") |
|
1182 { |
2294
|
1183 octave_value retval; |
1755
|
1184 |
2294
|
1185 int nargin = args.length (); |
1755
|
1186 |
2294
|
1187 if (nargin == 2) |
593
|
1188 { |
2294
|
1189 string name = args(0).string_value (); |
593
|
1190 |
2294
|
1191 if (! error_state) |
593
|
1192 { |
2294
|
1193 string help = args(1).string_value (); |
593
|
1194 |
2294
|
1195 if (! error_state) |
|
1196 { |
|
1197 if (is_builtin_variable (name) |
|
1198 || is_text_function_name (name) |
|
1199 || is_mapper_function_name (name) |
|
1200 || is_builtin_function_name (name)) |
|
1201 error ("document: can't redefine help for built-in variables and functions"); |
|
1202 else |
|
1203 { |
|
1204 symbol_record *sym_rec = curr_sym_tab->lookup (name, 0); |
|
1205 |
|
1206 if (sym_rec) |
|
1207 sym_rec->document (help); |
|
1208 else |
|
1209 error ("document: no such symbol `%s'", name.c_str ()); |
|
1210 } |
|
1211 } |
593
|
1212 } |
|
1213 } |
|
1214 else |
|
1215 print_usage ("document"); |
|
1216 |
|
1217 return retval; |
|
1218 } |
|
1219 |
584
|
1220 // XXX FIXME XXX -- this should take a list of regular expressions |
|
1221 // naming the variables to look for. |
|
1222 |
2086
|
1223 static octave_value_list |
1755
|
1224 do_who (int argc, const string_vector& argv) |
529
|
1225 { |
2086
|
1226 octave_value_list retval; |
529
|
1227 |
|
1228 int show_builtins = 0; |
|
1229 int show_functions = (curr_sym_tab == top_level_sym_tab); |
|
1230 int show_variables = 1; |
|
1231 int show_verbose = 0; |
|
1232 |
1755
|
1233 string my_name = argv[0]; |
584
|
1234 |
529
|
1235 if (argc > 1) |
|
1236 { |
|
1237 show_functions = 0; |
|
1238 show_variables = 0; |
|
1239 } |
|
1240 |
1857
|
1241 int i; |
|
1242 for (i = 1; i < argc; i++) |
529
|
1243 { |
1755
|
1244 if (argv[i] == "-all" || argv[i] == "-a") |
529
|
1245 { |
|
1246 show_builtins++; |
|
1247 show_functions++; |
1418
|
1248 show_variables++; |
529
|
1249 } |
1755
|
1250 else if (argv[i] == "-builtins" || argv[i] == "-b") |
529
|
1251 show_builtins++; |
1755
|
1252 else if (argv[i] == "-functions" || argv[i] == "-f") |
529
|
1253 show_functions++; |
1755
|
1254 else if (argv[i] == "-long" || argv[i] == "-l") |
605
|
1255 show_verbose++; |
1755
|
1256 else if (argv[i] == "-variables" || argv[i] == "-v") |
529
|
1257 show_variables++; |
1755
|
1258 else if (argv[i][0] == '-') |
|
1259 warning ("%s: unrecognized option `%s'", my_name.c_str (), |
|
1260 argv[i].c_str ()); |
529
|
1261 else |
867
|
1262 break; |
529
|
1263 } |
|
1264 |
1857
|
1265 int npats = argc - i; |
|
1266 string_vector pats (npats); |
|
1267 for (int j = 0; j < npats; j++) |
|
1268 pats[j] = argv[i+j]; |
|
1269 |
1271
|
1270 // If the user specified -l and nothing else, show variables. If |
|
1271 // evaluating this at the top level, also show functions. |
529
|
1272 |
|
1273 if (show_verbose && ! (show_builtins || show_functions || show_variables)) |
|
1274 { |
|
1275 show_functions = (curr_sym_tab == top_level_sym_tab); |
|
1276 show_variables = 1; |
|
1277 } |
|
1278 |
|
1279 int pad_after = 0; |
|
1280 |
|
1281 if (show_builtins) |
|
1282 { |
1857
|
1283 pad_after += maybe_list ("*** built-in variables:", pats, npats, |
2095
|
1284 octave_stdout, show_verbose, global_sym_tab, |
529
|
1285 symbol_def::BUILTIN_VARIABLE, |
|
1286 SYMTAB_ALL_SCOPES); |
|
1287 |
1857
|
1288 pad_after += maybe_list ("*** built-in functions:", pats, npats, |
2095
|
1289 octave_stdout, show_verbose, global_sym_tab, |
529
|
1290 symbol_def::BUILTIN_FUNCTION, |
|
1291 SYMTAB_ALL_SCOPES); |
|
1292 } |
|
1293 |
|
1294 if (show_functions) |
|
1295 { |
|
1296 pad_after += maybe_list ("*** currently compiled functions:", |
2095
|
1297 pats, npats, octave_stdout, show_verbose, |
867
|
1298 global_sym_tab, symbol_def::USER_FUNCTION, |
529
|
1299 SYMTAB_ALL_SCOPES); |
|
1300 } |
|
1301 |
|
1302 if (show_variables) |
|
1303 { |
1857
|
1304 pad_after += maybe_list ("*** local user variables:", pats, npats, |
2095
|
1305 octave_stdout, show_verbose, curr_sym_tab, |
529
|
1306 symbol_def::USER_VARIABLE, |
1418
|
1307 SYMTAB_LOCAL_SCOPE); |
529
|
1308 |
|
1309 pad_after += maybe_list ("*** globally visible user variables:", |
2095
|
1310 pats, npats, octave_stdout, show_verbose, |
867
|
1311 curr_sym_tab, symbol_def::USER_VARIABLE, |
529
|
1312 SYMTAB_GLOBAL_SCOPE); |
|
1313 } |
|
1314 |
|
1315 if (pad_after) |
2095
|
1316 octave_stdout << "\n"; |
529
|
1317 |
581
|
1318 return retval; |
|
1319 } |
|
1320 |
1957
|
1321 DEFUN_TEXT (who, args, , |
581
|
1322 "who [-all] [-builtins] [-functions] [-long] [-variables]\n\ |
|
1323 \n\ |
|
1324 List currently defined symbol(s). Options may be shortened to one\n\ |
|
1325 character, but may not be combined.") |
|
1326 { |
2086
|
1327 octave_value_list retval; |
581
|
1328 |
1755
|
1329 int argc = args.length () + 1; |
|
1330 |
1968
|
1331 string_vector argv = args.make_argv ("who"); |
1755
|
1332 |
|
1333 if (error_state) |
|
1334 return retval; |
581
|
1335 |
1488
|
1336 retval = do_who (argc, argv); |
581
|
1337 |
529
|
1338 return retval; |
|
1339 } |
|
1340 |
1957
|
1341 DEFUN_TEXT (whos, args, , |
581
|
1342 "whos [-all] [-builtins] [-functions] [-long] [-variables]\n\ |
|
1343 \n\ |
|
1344 List currently defined symbol(s). Options may be shortened to one\n\ |
|
1345 character, but may not be combined.") |
|
1346 { |
2086
|
1347 octave_value_list retval; |
581
|
1348 |
712
|
1349 int nargin = args.length (); |
|
1350 |
2086
|
1351 octave_value_list tmp_args; |
742
|
1352 for (int i = nargin; i > 0; i--) |
|
1353 tmp_args(i) = args(i-1); |
|
1354 tmp_args(0) = "-long"; |
581
|
1355 |
742
|
1356 int argc = tmp_args.length () + 1; |
1755
|
1357 |
1980
|
1358 string_vector argv = tmp_args.make_argv ("whos"); |
581
|
1359 |
|
1360 if (error_state) |
|
1361 return retval; |
|
1362 |
1488
|
1363 retval = do_who (argc, argv); |
581
|
1364 |
|
1365 return retval; |
|
1366 } |
|
1367 |
593
|
1368 // Install variables and functions in the symbol tables. |
529
|
1369 |
593
|
1370 void |
1755
|
1371 install_builtin_mapper (const builtin_mapper_function& mf) |
529
|
1372 { |
1755
|
1373 symbol_record *sym_rec = global_sym_tab->lookup (mf.name, 1); |
593
|
1374 sym_rec->unprotect (); |
|
1375 |
2089
|
1376 tree_builtin *def = new tree_builtin (mf, mf.name); |
593
|
1377 |
|
1378 sym_rec->define (def); |
|
1379 |
1755
|
1380 sym_rec->document (mf.help_string); |
593
|
1381 sym_rec->make_eternal (); |
|
1382 sym_rec->protect (); |
|
1383 } |
|
1384 |
|
1385 void |
1755
|
1386 install_builtin_function (const builtin_function& f) |
593
|
1387 { |
1755
|
1388 symbol_record *sym_rec = global_sym_tab->lookup (f.name, 1); |
593
|
1389 sym_rec->unprotect (); |
|
1390 |
1755
|
1391 tree_builtin *def = new tree_builtin (f.fcn, f.name); |
593
|
1392 |
1755
|
1393 sym_rec->define (def, f.is_text_fcn); |
593
|
1394 |
1755
|
1395 sym_rec->document (f.help_string); |
593
|
1396 sym_rec->make_eternal (); |
|
1397 sym_rec->protect (); |
|
1398 } |
|
1399 |
|
1400 void |
1755
|
1401 install_builtin_variable (const builtin_variable& v) |
593
|
1402 { |
1755
|
1403 if (v.install_as_function) |
|
1404 install_builtin_variable_as_function (v.name, v.value, v.protect, |
|
1405 v.eternal, v.help_string); |
529
|
1406 else |
1755
|
1407 bind_builtin_variable (v.name, v.value, v.protect, v.eternal, |
|
1408 v.sv_function, v.help_string); |
529
|
1409 } |
|
1410 |
593
|
1411 void |
2390
|
1412 install_builtin_variable_as_function (const string& name, |
|
1413 const octave_value& val, |
593
|
1414 int protect, int eternal, |
1755
|
1415 const string& help) |
529
|
1416 { |
593
|
1417 symbol_record *sym_rec = global_sym_tab->lookup (name, 1); |
|
1418 sym_rec->unprotect (); |
|
1419 |
1755
|
1420 string tmp_help = help.empty () ? sym_rec->help () : help; |
593
|
1421 |
|
1422 sym_rec->define_as_fcn (val); |
|
1423 |
|
1424 sym_rec->document (tmp_help); |
|
1425 |
|
1426 if (protect) |
|
1427 sym_rec->protect (); |
|
1428 |
|
1429 if (eternal) |
|
1430 sym_rec->make_eternal (); |
|
1431 } |
|
1432 |
|
1433 void |
1755
|
1434 alias_builtin (const string& alias, const string& name) |
593
|
1435 { |
|
1436 symbol_record *sr_name = global_sym_tab->lookup (name, 0, 0); |
1755
|
1437 |
593
|
1438 if (! sr_name) |
|
1439 panic ("can't alias to undefined name!"); |
|
1440 |
|
1441 symbol_record *sr_alias = global_sym_tab->lookup (alias, 1, 0); |
|
1442 |
|
1443 if (sr_alias) |
|
1444 sr_alias->alias (sr_name); |
|
1445 else |
1755
|
1446 panic ("can't find symbol record for builtin function `%s'", |
|
1447 alias.c_str ()); |
529
|
1448 } |
|
1449 |
593
|
1450 // Defining variables. |
|
1451 |
1162
|
1452 void |
2086
|
1453 bind_ans (const octave_value& val, int print) |
1162
|
1454 { |
|
1455 static symbol_record *sr = global_sym_tab->lookup ("ans", 1, 0); |
|
1456 |
1281
|
1457 tree_identifier *ans_id = new tree_identifier (sr); |
2390
|
1458 tree_constant *tmp = new tree_constant (val); |
1162
|
1459 |
1281
|
1460 // XXX FIXME XXX -- making ans_id static, passing its address to |
|
1461 // tree_simple_assignment_expression along with a flag to not delete |
|
1462 // it seems to create a memory leak. Hmm. |
|
1463 |
1827
|
1464 tree_simple_assignment_expression tmp_ass (ans_id, tmp, false, true); |
1162
|
1465 |
|
1466 tmp_ass.eval (print); |
|
1467 } |
|
1468 |
1489
|
1469 void |
|
1470 bind_global_error_variable (void) |
|
1471 { |
|
1472 *error_message_buffer << ends; |
|
1473 |
|
1474 char *error_text = error_message_buffer->str (); |
|
1475 |
|
1476 bind_builtin_variable ("__error_text__", error_text, 1); |
|
1477 |
|
1478 delete [] error_text; |
|
1479 |
|
1480 delete error_message_buffer; |
|
1481 |
|
1482 error_message_buffer = 0; |
|
1483 } |
|
1484 |
|
1485 void |
|
1486 clear_global_error_variable (void *) |
|
1487 { |
|
1488 delete error_message_buffer; |
|
1489 error_message_buffer = 0; |
|
1490 |
|
1491 bind_builtin_variable ("__error_text__", "", 1); |
|
1492 } |
|
1493 |
593
|
1494 // Give a global variable a definition. This will insert the symbol |
|
1495 // in the global table if necessary. |
|
1496 |
|
1497 // How is this different than install_builtin_variable? Are both |
|
1498 // functions needed? |
|
1499 |
|
1500 void |
2390
|
1501 bind_builtin_variable (const string& varname, const octave_value& val, |
593
|
1502 int protect, int eternal, sv_Function sv_fcn, |
1755
|
1503 const string& help) |
593
|
1504 { |
|
1505 symbol_record *sr = global_sym_tab->lookup (varname, 1, 0); |
|
1506 |
1271
|
1507 // It is a programming error for a builtin symbol to be missing. |
|
1508 // Besides, we just inserted it, so it must be there. |
593
|
1509 |
|
1510 assert (sr); |
|
1511 |
|
1512 sr->unprotect (); |
|
1513 |
1271
|
1514 // Must do this before define, since define will call the special |
|
1515 // variable function only if it knows about it, and it needs to, so |
|
1516 // that user prefs can be properly initialized. |
593
|
1517 |
|
1518 if (sv_fcn) |
|
1519 sr->set_sv_function (sv_fcn); |
|
1520 |
|
1521 sr->define_builtin_var (val); |
|
1522 |
|
1523 if (protect) |
|
1524 sr->protect (); |
|
1525 |
|
1526 if (eternal) |
|
1527 sr->make_eternal (); |
|
1528 |
1755
|
1529 sr->document (help); |
529
|
1530 } |
|
1531 |
2205
|
1532 // XXX FIXME XXX -- some of these should do their own checking to be |
|
1533 // able to provide more meaningful warning or error messages. |
|
1534 |
|
1535 static int |
|
1536 echo_executing_commands (void) |
|
1537 { |
|
1538 Vecho_executing_commands = check_preference ("echo_executing_commands"); |
|
1539 |
|
1540 return 0; |
|
1541 } |
|
1542 |
|
1543 static int |
|
1544 history_size (void) |
|
1545 { |
|
1546 double val; |
|
1547 if (builtin_real_scalar_variable ("history_size", val) |
|
1548 && ! xisnan (val)) |
|
1549 { |
|
1550 int ival = NINT (val); |
2800
|
1551 if (ival >= 0 && ival == val) |
2205
|
1552 { |
|
1553 Vhistory_size = ival; |
|
1554 octave_command_history.set_size (ival); |
|
1555 return 0; |
|
1556 } |
|
1557 } |
|
1558 gripe_invalid_value_specified ("history_size"); |
|
1559 return -1; |
|
1560 } |
|
1561 |
|
1562 static int |
|
1563 history_file (void) |
|
1564 { |
|
1565 int status = 0; |
|
1566 |
|
1567 string s = builtin_string_variable ("history_file"); |
2065
|
1568 |
2205
|
1569 if (s.empty ()) |
|
1570 { |
|
1571 gripe_invalid_value_specified ("history_file"); |
|
1572 status = -1; |
|
1573 } |
|
1574 else |
|
1575 { |
|
1576 Vhistory_file = s; |
|
1577 octave_command_history.set_file (oct_tilde_expand (s)); |
|
1578 } |
|
1579 |
|
1580 return status; |
|
1581 } |
|
1582 |
|
1583 static int |
|
1584 ignore_function_time_stamp (void) |
|
1585 { |
|
1586 int pref = 0; |
|
1587 |
|
1588 string val = builtin_string_variable ("ignore_function_time_stamp"); |
|
1589 |
|
1590 if (! val.empty ()) |
|
1591 { |
|
1592 if (val.compare ("all", 0, 3) == 0) |
|
1593 pref = 2; |
|
1594 if (val.compare ("system", 0, 6) == 0) |
|
1595 pref = 1; |
|
1596 } |
|
1597 |
|
1598 Vignore_function_time_stamp = pref; |
|
1599 |
|
1600 return 0; |
|
1601 } |
|
1602 |
|
1603 static int |
|
1604 saving_history (void) |
|
1605 { |
|
1606 Vsaving_history = check_preference ("saving_history"); |
|
1607 |
|
1608 octave_command_history.ignore_entries (! Vsaving_history); |
|
1609 |
|
1610 return 0; |
|
1611 } |
|
1612 |
|
1613 // XXX FIXME XXX -- there still may be better places for some of these |
|
1614 // to be defined. |
2065
|
1615 |
|
1616 static void |
2205
|
1617 symbols_of_variables (void) |
529
|
1618 { |
1957
|
1619 DEFVAR (ans, , 0, 0, |
593
|
1620 ""); |
|
1621 |
1957
|
1622 DEFCONST (argv, , 0, 0, |
1343
|
1623 "the command line arguments this program was invoked with"); |
|
1624 |
2800
|
1625 DEFVAR (echo_executing_commands, static_cast<double> (ECHO_OFF), 0, |
2205
|
1626 echo_executing_commands, |
|
1627 "echo commands as they are executed"); |
|
1628 |
1957
|
1629 DEFCONST (error_text, "", 0, 0, |
2802
|
1630 "the text of error messages that would have been printed in the\n\ |
1489
|
1631 body of the most recent unwind_protect statement or the TRY part of\n\ |
|
1632 the most recent eval() command. Outside of unwind_protect and\n\ |
|
1633 eval(), or if no error has ocurred within them, the value of\n\ |
|
1634 __error_text__ is guaranteed to be the empty string."); |
|
1635 |
2205
|
1636 DEFVAR (history_file, default_history_file (), 0, history_file, |
1646
|
1637 "name of command history file"); |
|
1638 |
2825
|
1639 double tmp_hist_size = default_history_size (); |
|
1640 |
|
1641 DEFVAR (history_size, tmp_hist_size, 0, history_size, |
1646
|
1642 "number of commands to save in the history list"); |
|
1643 |
1957
|
1644 DEFVAR (ignore_function_time_stamp, "system", 0, ignore_function_time_stamp, |
593
|
1645 "don't check to see if function files have changed since they were\n\ |
|
1646 last compiled. Possible values are \"system\" and \"all\""); |
|
1647 |
2205
|
1648 DEFCONST (program_invocation_name, Vprogram_invocation_name, 0, 0, |
1343
|
1649 "the full name of the current program or script, including the\n\ |
|
1650 directory specification"); |
|
1651 |
2205
|
1652 DEFCONST (program_name, Vprogram_name, 0, 0, |
1343
|
1653 "the name of the current program or script"); |
|
1654 |
1957
|
1655 DEFVAR (saving_history, 1.0, 0, saving_history, |
1643
|
1656 "save command history"); |
529
|
1657 } |
|
1658 |
2065
|
1659 void |
|
1660 install_builtin_variables (void) |
|
1661 { |
2205
|
1662 symbols_of_data (); |
|
1663 symbols_of_defaults (); |
|
1664 symbols_of_dirfns (); |
2181
|
1665 symbols_of_error (); |
2205
|
1666 symbols_of_file_io (); |
|
1667 symbols_of_help (); |
2181
|
1668 symbols_of_input (); |
|
1669 symbols_of_lex (); |
2205
|
1670 symbols_of_load_save (); |
2097
|
1671 symbols_of_pager (); |
2181
|
1672 symbols_of_parse (); |
|
1673 symbols_of_pr_output (); |
|
1674 symbols_of_pt_fcn (); |
|
1675 symbols_of_pt_mat (); |
|
1676 symbols_of_pt_plot (); |
2076
|
1677 symbols_of_syscalls (); |
2806
|
1678 symbols_of_toplev (); |
2390
|
1679 symbols_of_value (); |
2205
|
1680 symbols_of_variables (); |
2065
|
1681 } |
|
1682 |
593
|
1683 // Deleting names from the symbol tables. |
|
1684 |
1957
|
1685 DEFUN_TEXT (clear, args, , |
668
|
1686 "clear [-x] [name ...]\n\ |
|
1687 \n\ |
|
1688 Clear symbol(s) matching a list of globbing patterns.\n\ |
593
|
1689 \n\ |
2802
|
1690 If no arguments are given, clear all user-defined variables and\n\ |
668
|
1691 functions.\n\ |
|
1692 \n\ |
|
1693 With -x, exclude the named variables") |
529
|
1694 { |
2086
|
1695 octave_value_list retval; |
593
|
1696 |
1755
|
1697 int argc = args.length () + 1; |
593
|
1698 |
1968
|
1699 string_vector argv = args.make_argv ("clear"); |
1755
|
1700 |
|
1701 if (error_state) |
|
1702 return retval; |
668
|
1703 |
1271
|
1704 // Always clear the local table, but don't clear currently compiled |
|
1705 // functions unless we are at the top level. (Allowing that to |
|
1706 // happen inside functions would result in pretty odd behavior...) |
593
|
1707 |
|
1708 int clear_user_functions = (curr_sym_tab == top_level_sym_tab); |
|
1709 |
1755
|
1710 if (argc == 1) |
593
|
1711 { |
|
1712 curr_sym_tab->clear (); |
|
1713 global_sym_tab->clear (clear_user_functions); |
|
1714 } |
529
|
1715 else |
|
1716 { |
668
|
1717 int exclusive = 0; |
|
1718 |
1755
|
1719 int idx = 1; |
|
1720 |
|
1721 if (argc > 1) |
668
|
1722 { |
1755
|
1723 if (argv[idx] == "-x") |
|
1724 exclusive = 1; |
668
|
1725 } |
|
1726 |
|
1727 int lcount = 0; |
|
1728 int gcount = 0; |
|
1729 int fcount = 0; |
529
|
1730 |
1755
|
1731 string_vector lvars; |
|
1732 string_vector gvars; |
|
1733 string_vector fcns; |
668
|
1734 |
|
1735 if (argc > 0) |
593
|
1736 { |
893
|
1737 lvars = curr_sym_tab->list (lcount, 0, 0, 0, |
1418
|
1738 SYMTAB_VARIABLES, |
668
|
1739 SYMTAB_LOCAL_SCOPE); |
|
1740 |
893
|
1741 gvars = curr_sym_tab->list (gcount, 0, 0, 0, |
1418
|
1742 SYMTAB_VARIABLES, |
668
|
1743 SYMTAB_GLOBAL_SCOPE); |
|
1744 |
893
|
1745 fcns = global_sym_tab->list (fcount, 0, 0, 0, |
|
1746 symbol_def::USER_FUNCTION, |
864
|
1747 SYMTAB_ALL_SCOPES); |
668
|
1748 } |
|
1749 |
2438
|
1750 // XXX FIXME XXX -- this needs to be optimized to avoid the |
|
1751 // pattern matching code if the string doesn't contain any |
|
1752 // globbing patterns. |
|
1753 |
1970
|
1754 for (int k = idx; k < argc; k++) |
668
|
1755 { |
1755
|
1756 string patstr = argv[k]; |
668
|
1757 |
1755
|
1758 if (! patstr.empty ()) |
593
|
1759 { |
1792
|
1760 glob_match pattern (patstr); |
1755
|
1761 |
593
|
1762 int i; |
|
1763 for (i = 0; i < lcount; i++) |
|
1764 { |
1755
|
1765 string nm = lvars[i]; |
1792
|
1766 int match = pattern.match (nm); |
668
|
1767 if ((exclusive && ! match) || (! exclusive && match)) |
|
1768 curr_sym_tab->clear (nm); |
593
|
1769 } |
529
|
1770 |
593
|
1771 int count; |
|
1772 for (i = 0; i < gcount; i++) |
|
1773 { |
1755
|
1774 string nm = gvars[i]; |
1792
|
1775 int match = pattern.match (nm); |
668
|
1776 if ((exclusive && ! match) || (! exclusive && match)) |
593
|
1777 { |
668
|
1778 count = curr_sym_tab->clear (nm); |
593
|
1779 if (count > 0) |
668
|
1780 global_sym_tab->clear (nm, clear_user_functions); |
593
|
1781 } |
|
1782 } |
529
|
1783 |
593
|
1784 for (i = 0; i < fcount; i++) |
|
1785 { |
1755
|
1786 string nm = fcns[i]; |
1792
|
1787 int match = pattern.match (nm); |
668
|
1788 if ((exclusive && ! match) || (! exclusive && match)) |
593
|
1789 { |
668
|
1790 count = curr_sym_tab->clear (nm); |
864
|
1791 global_sym_tab->clear (nm, clear_user_functions); |
593
|
1792 } |
|
1793 } |
|
1794 } |
|
1795 } |
|
1796 } |
|
1797 |
|
1798 return retval; |
529
|
1799 } |
|
1800 |
1
|
1801 /* |
|
1802 ;;; Local Variables: *** |
|
1803 ;;; mode: C++ *** |
|
1804 ;;; End: *** |
|
1805 */ |