1
|
1 // variables.cc -*- C++ -*- |
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton |
1
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
1
|
26 #endif |
|
27 |
605
|
28 #if 0 |
|
29 #include <ctype.h> |
|
30 #include <iostream.h> |
|
31 |
|
32 #include "mappers.h" |
|
33 #endif |
|
34 |
1
|
35 #include <sys/types.h> |
|
36 #ifdef HAVE_UNISTD_H |
|
37 #include <unistd.h> |
|
38 #endif |
529
|
39 #include <float.h> |
|
40 #include <string.h> |
279
|
41 #include <strstream.h> |
1
|
42 |
605
|
43 #include "defaults.h" |
|
44 #include "version.h" |
704
|
45 #include "dynamic-ld.h" |
581
|
46 #include "octave-hist.h" |
|
47 #include "unwind-prot.h" |
605
|
48 #include "variables.h" |
581
|
49 #include "user-prefs.h" |
605
|
50 #include "statdefs.h" |
584
|
51 #include "tree-base.h" |
|
52 #include "tree-expr.h" |
1
|
53 #include "tree-const.h" |
605
|
54 #include "dirfns.h" |
581
|
55 #include "oct-obj.h" |
|
56 #include "sysdep.h" |
|
57 #include "symtab.h" |
529
|
58 #include "octave.h" |
605
|
59 #include "pager.h" |
1
|
60 #include "error.h" |
605
|
61 #include "defun.h" |
1
|
62 #include "utils.h" |
605
|
63 #include "parse.h" |
581
|
64 #include "input.h" |
164
|
65 #include "help.h" |
581
|
66 #include "lex.h" |
529
|
67 |
|
68 extern "C" |
|
69 { |
581
|
70 #include <readline/readline.h> |
529
|
71 |
|
72 #include "fnmatch.h" |
|
73 } |
1
|
74 |
|
75 // Symbol table for symbols at the top level. |
581
|
76 symbol_table *top_level_sym_tab = 0; |
1
|
77 |
|
78 // Symbol table for the current scope. |
581
|
79 symbol_table *curr_sym_tab = 0; |
1
|
80 |
|
81 // Symbol table for global symbols. |
581
|
82 symbol_table *global_sym_tab = 0; |
1
|
83 |
593
|
84 // Initialization. |
|
85 |
|
86 // Create the initial symbol tables and set the current scope at the |
|
87 // top level. |
|
88 |
195
|
89 void |
|
90 initialize_symbol_tables (void) |
|
91 { |
581
|
92 if (! global_sym_tab) |
|
93 global_sym_tab = new symbol_table (); |
195
|
94 |
581
|
95 if (! top_level_sym_tab) |
|
96 top_level_sym_tab = new symbol_table (); |
195
|
97 |
|
98 curr_sym_tab = top_level_sym_tab; |
|
99 } |
|
100 |
593
|
101 // Attributes of variables and functions. |
|
102 |
|
103 // Is this variable a builtin? |
|
104 |
|
105 int |
|
106 is_builtin_variable (const char *name) |
|
107 { |
|
108 symbol_record *sr = global_sym_tab->lookup (name, 0, 0); |
|
109 return (sr && sr->is_builtin_variable ()); |
|
110 } |
|
111 |
|
112 // Is this a text-style function? |
|
113 |
|
114 int |
|
115 is_text_function_name (const char *s) |
|
116 { |
|
117 symbol_record *sr = global_sym_tab->lookup (s); |
|
118 return (sr && sr->is_text_function ()); |
|
119 } |
|
120 |
|
121 // Is this function globally in this scope? |
|
122 |
605
|
123 int |
593
|
124 is_globally_visible (const char *name) |
|
125 { |
|
126 symbol_record *sr = curr_sym_tab->lookup (name, 0, 0); |
|
127 return (sr && sr->is_linked_to_global ()); |
|
128 } |
|
129 |
|
130 // Is this tree_constant a valid function? |
|
131 |
|
132 tree_fvc * |
|
133 is_valid_function (const tree_constant& arg, char *warn_for, int warn) |
|
134 { |
|
135 tree_fvc *ans = 0; |
|
136 |
636
|
137 char *fcn_name = arg.string_value (); |
|
138 |
|
139 if (error_state) |
593
|
140 { |
|
141 if (warn) |
|
142 error ("%s: expecting function name as argument", warn_for); |
|
143 return ans; |
|
144 } |
|
145 |
|
146 symbol_record *sr = 0; |
|
147 if (fcn_name) |
|
148 sr = lookup_by_name (fcn_name); |
|
149 |
|
150 if (sr) |
|
151 ans = sr->def (); |
|
152 |
|
153 if (! sr || ! ans || ! sr->is_function ()) |
|
154 { |
|
155 if (warn) |
|
156 error ("%s: the symbol `%s' is not valid as a function", |
|
157 warn_for, fcn_name); |
|
158 ans = 0; |
|
159 } |
|
160 |
|
161 return ans; |
|
162 } |
|
163 |
|
164 // Does this function take the right number of arguments? |
|
165 |
|
166 int |
|
167 takes_correct_nargs (tree_fvc *fcn, int expected_nargin, char *warn_for, |
|
168 int warn) |
|
169 { |
712
|
170 int nargin = fcn->max_expected_args (); |
|
171 int e_nargin = expected_nargin; |
593
|
172 if (nargin != e_nargin) |
|
173 { |
|
174 if (warn) |
722
|
175 error ("%s: expecting function to take %d argument%s", |
593
|
176 warn_for, e_nargin, (e_nargin == 1 ? "" : "s")); |
|
177 return 0; |
|
178 } |
|
179 return 1; |
|
180 } |
|
181 |
712
|
182 DEFUN ("is_global", Fis_global, Sis_global, 1, 1, |
593
|
183 "is_global (X): return 1 if the string X names a global variable\n\ |
|
184 otherwise, return 0.") |
|
185 { |
|
186 Octave_object retval = 0.0; |
|
187 |
712
|
188 int nargin = args.length (); |
|
189 |
|
190 if (nargin != 1) |
593
|
191 { |
|
192 print_usage ("is_global"); |
|
193 return retval; |
|
194 } |
|
195 |
722
|
196 char *name = args(0).string_value (); |
593
|
197 |
636
|
198 if (error_state) |
|
199 { |
|
200 error ("is_global: expecting string argument"); |
|
201 return retval; |
|
202 } |
|
203 |
593
|
204 symbol_record *sr = curr_sym_tab->lookup (name, 0, 0); |
|
205 |
|
206 retval = (double) (sr && sr->is_linked_to_global ()); |
|
207 |
|
208 return retval; |
|
209 } |
|
210 |
712
|
211 DEFUN ("exist", Fexist, Sexist, 1, 1, |
593
|
212 "exist (NAME): check if variable or file exists\n\ |
|
213 \n\ |
|
214 return 0 if NAME is undefined, 1 if it is a variable, or 2 if it is\n\ |
|
215 a function.") |
|
216 { |
|
217 Octave_object retval; |
|
218 |
712
|
219 int nargin = args.length (); |
|
220 |
|
221 if (nargin != 1) |
593
|
222 { |
|
223 print_usage ("exist"); |
|
224 return retval; |
|
225 } |
|
226 |
722
|
227 char *name = args(0).string_value (); |
593
|
228 |
636
|
229 if (error_state) |
|
230 { |
|
231 error ("exist: expecting string argument"); |
|
232 return retval; |
|
233 } |
|
234 |
593
|
235 symbol_record *sr = curr_sym_tab->lookup (name, 0, 0); |
|
236 if (! sr) |
|
237 sr = global_sym_tab->lookup (name, 0, 0); |
|
238 |
|
239 retval = 0.0; |
|
240 |
|
241 if (sr && sr->is_variable () && sr->is_defined ()) |
|
242 retval = 1.0; |
|
243 else if (sr && sr->is_function ()) |
|
244 retval = 2.0; |
|
245 else |
|
246 { |
|
247 char *path = fcn_file_in_path (name); |
|
248 if (path) |
|
249 { |
|
250 delete [] path; |
|
251 retval = 2.0; |
|
252 } |
|
253 else |
|
254 { |
|
255 struct stat buf; |
|
256 if (stat (name, &buf) == 0 && S_ISREG (buf.st_mode)) |
|
257 retval = 2.0; |
|
258 } |
|
259 } |
|
260 |
|
261 return retval; |
|
262 } |
|
263 |
|
264 // XXX FIXME XXX -- should these really be here? |
|
265 |
|
266 static char * |
|
267 octave_home (void) |
|
268 { |
|
269 char *oh = getenv ("OCTAVE_HOME"); |
666
|
270 |
|
271 return (oh ? oh : OCTAVE_PREFIX); |
|
272 } |
|
273 |
|
274 static char * |
|
275 subst_octave_home (char *s) |
|
276 { |
|
277 char *home = octave_home (); |
|
278 char *prefix = OCTAVE_PREFIX; |
|
279 |
|
280 char *retval; |
|
281 |
|
282 if (strcmp (home, prefix) == 0) |
|
283 retval = strsave (s); |
593
|
284 else |
666
|
285 { |
|
286 int len_home = strlen (home); |
|
287 int len_prefix = strlen (prefix); |
|
288 |
|
289 int count = 0; |
|
290 char *ptr = s; |
811
|
291 char *next = 0; |
|
292 while ((next = strstr (ptr, prefix))) |
666
|
293 { |
811
|
294 ptr = next + len_prefix; |
666
|
295 count++; |
|
296 } |
|
297 |
|
298 int grow_size = count * (len_home - len_prefix); |
|
299 |
811
|
300 int len_s = strlen (s); |
|
301 |
|
302 int len_retval = len_s + count * grow_size; |
666
|
303 |
|
304 retval = new char [len_retval+1]; |
|
305 |
|
306 char *p1 = s; |
|
307 char *p2 = p1; |
|
308 char *pdest = retval; |
|
309 |
811
|
310 // Is this really a good way to do this? |
|
311 |
|
312 while (count >= 0) |
666
|
313 { |
|
314 p2 = strstr (p1, prefix); |
|
315 |
|
316 if (! p2) |
770
|
317 { |
811
|
318 memcpy (pdest, p1, strlen (p1)+1); |
|
319 break; |
770
|
320 } |
811
|
321 else if (p1 == p2) |
666
|
322 { |
|
323 memcpy (pdest, home, len_home); |
|
324 pdest += len_home; |
|
325 p1 += len_prefix; |
811
|
326 count--; |
666
|
327 } |
|
328 else |
|
329 { |
|
330 int len = (int) (p2 - p1); |
|
331 memcpy (pdest, p1, len); |
|
332 pdest += len; |
|
333 p1 += len; |
|
334 } |
|
335 |
|
336 } |
|
337 } |
|
338 |
|
339 return retval; |
593
|
340 } |
|
341 |
|
342 static char * |
|
343 octave_info_dir (void) |
|
344 { |
666
|
345 static char *retval = subst_octave_home (OCTAVE_INFODIR); |
|
346 return retval; |
|
347 } |
|
348 |
686
|
349 char * |
666
|
350 octave_arch_lib_dir (void) |
|
351 { |
|
352 static char *retval = subst_octave_home (OCTAVE_ARCHLIBDIR); |
|
353 return retval; |
593
|
354 } |
|
355 |
798
|
356 char * |
|
357 octave_bin_dir (void) |
|
358 { |
|
359 static char *retval = subst_octave_home (OCTAVE_BINDIR); |
|
360 return retval; |
|
361 } |
|
362 |
593
|
363 static char * |
|
364 default_pager (void) |
|
365 { |
|
366 static char *pager_binary = 0; |
|
367 delete [] pager_binary; |
|
368 char *pgr = getenv ("PAGER"); |
|
369 if (pgr) |
|
370 pager_binary = strsave (pgr); |
|
371 else |
|
372 #ifdef DEFAULT_PAGER |
|
373 pager_binary = strsave (DEFAULT_PAGER); |
|
374 #else |
|
375 pager_binary = strsave (""); |
|
376 #endif |
|
377 |
|
378 return pager_binary; |
|
379 } |
|
380 |
|
381 char * |
|
382 octave_lib_dir (void) |
|
383 { |
666
|
384 static char *retval = subst_octave_home (OCTAVE_LIBDIR); |
|
385 return retval; |
593
|
386 } |
|
387 |
|
388 // Handle OCTAVE_PATH from the environment like TeX handles TEXINPUTS. |
|
389 // If the path starts with `:', prepend the standard path. If it ends |
|
390 // with `:' append the standard path. If it begins and ends with |
|
391 // `:', do both (which is useless, but the luser asked for it...). |
|
392 // |
|
393 // This function may eventually be called more than once, so be |
|
394 // careful not to create memory leaks. |
|
395 |
|
396 char * |
|
397 default_path (void) |
|
398 { |
|
399 static char *pathstring = 0; |
|
400 delete [] pathstring; |
|
401 |
666
|
402 static char *std_path = subst_octave_home (OCTAVE_FCNFILEPATH); |
593
|
403 |
|
404 char *oct_path = getenv ("OCTAVE_PATH"); |
|
405 |
|
406 if (oct_path) |
|
407 { |
|
408 pathstring = strsave (oct_path); |
|
409 |
|
410 if (pathstring[0] == ':') |
|
411 { |
|
412 char *tmp = pathstring; |
|
413 pathstring = strconcat (std_path, pathstring); |
|
414 delete [] tmp; |
|
415 } |
|
416 |
|
417 int tmp_len = strlen (pathstring); |
|
418 if (pathstring[tmp_len-1] == ':') |
|
419 { |
|
420 char *tmp = pathstring; |
|
421 pathstring = strconcat (pathstring, std_path); |
|
422 delete [] tmp; |
|
423 } |
|
424 } |
|
425 else |
|
426 pathstring = strsave (std_path); |
|
427 |
|
428 return pathstring; |
|
429 } |
|
430 |
|
431 char * |
|
432 default_info_file (void) |
|
433 { |
|
434 static char *info_file_string = 0; |
|
435 delete [] info_file_string; |
|
436 char *oct_info_file = getenv ("OCTAVE_INFO_FILE"); |
|
437 if (oct_info_file) |
|
438 info_file_string = strsave (oct_info_file); |
|
439 else |
|
440 { |
|
441 char *infodir = octave_info_dir (); |
|
442 info_file_string = strconcat (infodir, "/octave.info"); |
|
443 } |
|
444 return info_file_string; |
|
445 } |
|
446 |
|
447 char * |
|
448 default_editor (void) |
|
449 { |
|
450 static char *editor_string = 0; |
|
451 delete [] editor_string; |
|
452 char *env_editor = getenv ("EDITOR"); |
|
453 if (env_editor && *env_editor) |
|
454 editor_string = strsave (env_editor); |
|
455 else |
|
456 editor_string = strsave ("vi"); |
|
457 return editor_string; |
|
458 } |
|
459 |
|
460 char * |
|
461 get_site_defaults (void) |
|
462 { |
731
|
463 static char *startupdir = subst_octave_home (OCTAVE_STARTUPFILEDIR); |
|
464 static char *sd = strconcat (startupdir, "/octaverc"); |
593
|
465 return sd; |
|
466 } |
|
467 |
|
468 // Functions for looking up variables and functions. |
|
469 |
581
|
470 // Is there a corresponding function file that is newer than the |
|
471 // symbol definition? |
|
472 |
593
|
473 static int |
1
|
474 symbol_out_of_date (symbol_record *sr) |
|
475 { |
195
|
476 int ignore = user_pref.ignore_function_time_stamp; |
|
477 |
|
478 if (ignore == 2) |
|
479 return 0; |
|
480 |
529
|
481 if (sr) |
1
|
482 { |
490
|
483 tree_fvc *ans = sr->def (); |
529
|
484 if (ans) |
1
|
485 { |
339
|
486 char *ff = ans->fcn_file_name (); |
529
|
487 if (ff && ! (ignore && ans->is_system_fcn_file ())) |
1
|
488 { |
|
489 time_t tp = ans->time_parsed (); |
339
|
490 char *fname = fcn_file_in_path (ff); |
195
|
491 int status = is_newer (fname, tp); |
|
492 delete [] fname; |
|
493 if (status > 0) |
|
494 return 1; |
1
|
495 } |
|
496 } |
|
497 } |
195
|
498 return 0; |
1
|
499 } |
|
500 |
991
|
501 static int |
|
502 looks_like_octave_copyright (char *s) |
|
503 { |
|
504 if (s && strncmp (s, " Copyright (C) ", 15) == 0) |
|
505 { |
|
506 s = strchr (s, '\n'); |
|
507 if (s) |
|
508 { |
|
509 s++; |
|
510 s = strchr (s, '\n'); |
|
511 if (s) |
|
512 { |
|
513 s++; |
|
514 if (strncmp (s, " This file is part of Octave.", 29) == 0) |
|
515 return 1; |
|
516 } |
|
517 } |
|
518 } |
|
519 return 0; |
|
520 } |
|
521 |
|
522 static char * |
581
|
523 gobble_leading_white_space (FILE *ffile) |
|
524 { |
991
|
525 ostrstream buf; |
|
526 |
|
527 int first_comments_seen = 0; |
|
528 int have_help_text = 0; |
581
|
529 int in_comment = 0; |
|
530 int c; |
|
531 while ((c = getc (ffile)) != EOF) |
|
532 { |
984
|
533 current_input_column++; |
581
|
534 if (in_comment) |
|
535 { |
991
|
536 if (! have_help_text) |
|
537 { |
|
538 first_comments_seen = 1; |
|
539 buf << (char) c; |
|
540 } |
|
541 |
581
|
542 if (c == '\n') |
984
|
543 { |
|
544 input_line_number++; |
|
545 current_input_column = 0; |
|
546 in_comment = 0; |
|
547 } |
581
|
548 } |
|
549 else |
|
550 { |
984
|
551 switch (c) |
581
|
552 { |
984
|
553 case ' ': |
|
554 case '\t': |
991
|
555 if (first_comments_seen) |
|
556 have_help_text = 1; |
984
|
557 break; |
|
558 |
|
559 case '\n': |
993
|
560 if (first_comments_seen) |
|
561 have_help_text = 1; |
984
|
562 input_line_number++; |
|
563 current_input_column = 0; |
|
564 continue; |
|
565 |
|
566 case '%': |
|
567 case '#': |
|
568 in_comment = 1; |
|
569 break; |
|
570 |
|
571 default: |
|
572 current_input_column--; |
581
|
573 ungetc (c, ffile); |
991
|
574 goto done; |
581
|
575 } |
|
576 } |
|
577 } |
991
|
578 |
|
579 done: |
|
580 |
|
581 buf << ends; |
|
582 char *help_txt = buf.str (); |
|
583 |
|
584 if (! help_txt || ! *help_txt || looks_like_octave_copyright (help_txt)) |
|
585 { |
|
586 delete help_txt; |
|
587 help_txt = 0; |
|
588 } |
|
589 |
|
590 return help_txt; |
581
|
591 } |
|
592 |
|
593 static int |
|
594 is_function_file (FILE *ffile) |
|
595 { |
|
596 int status = 0; |
|
597 |
|
598 long pos = ftell (ffile); |
|
599 |
|
600 char buf [10]; |
|
601 fgets (buf, 10, ffile); |
|
602 int len = strlen (buf); |
|
603 if (len > 8 && strncmp (buf, "function", 8) == 0 |
|
604 && ! (isalnum (buf[8]) || buf[8] == '_')) |
|
605 status = 1; |
|
606 |
|
607 fseek (ffile, pos, SEEK_SET); |
|
608 |
|
609 return status; |
|
610 } |
|
611 |
|
612 static int |
|
613 parse_fcn_file (int exec_script, char *ff) |
|
614 { |
|
615 begin_unwind_frame ("parse_fcn_file"); |
|
616 |
|
617 int script_file_executed = 0; |
|
618 |
|
619 assert (ff); |
|
620 |
|
621 // Open function file and parse. |
|
622 |
|
623 int old_reading_fcn_file_state = reading_fcn_file; |
|
624 |
|
625 unwind_protect_ptr (rl_instream); |
|
626 unwind_protect_ptr (ff_instream); |
|
627 |
|
628 unwind_protect_int (using_readline); |
|
629 unwind_protect_int (input_line_number); |
|
630 unwind_protect_int (current_input_column); |
|
631 unwind_protect_int (reading_fcn_file); |
|
632 |
|
633 using_readline = 0; |
|
634 reading_fcn_file = 1; |
|
635 input_line_number = 0; |
|
636 current_input_column = 1; |
|
637 |
|
638 FILE *ffile = get_input_from_file (ff, 0); |
|
639 |
|
640 if (ffile) |
|
641 { |
|
642 // Check to see if this file defines a function or is just a list of |
|
643 // commands. |
|
644 |
991
|
645 char *tmp_help_txt = gobble_leading_white_space (ffile); |
|
646 |
581
|
647 if (is_function_file (ffile)) |
|
648 { |
|
649 unwind_protect_int (echo_input); |
|
650 unwind_protect_int (saving_history); |
|
651 unwind_protect_int (reading_fcn_file); |
|
652 |
|
653 echo_input = 0; |
|
654 saving_history = 0; |
|
655 reading_fcn_file = 1; |
|
656 |
|
657 YY_BUFFER_STATE old_buf = current_buffer (); |
|
658 YY_BUFFER_STATE new_buf = create_buffer (ffile); |
|
659 |
|
660 add_unwind_protect (restore_input_buffer, (void *) old_buf); |
|
661 add_unwind_protect (delete_input_buffer, (void *) new_buf); |
|
662 |
|
663 switch_to_buffer (new_buf); |
|
664 |
|
665 unwind_protect_ptr (curr_sym_tab); |
|
666 |
|
667 reset_parser (); |
|
668 |
991
|
669 delete [] help_buf; |
|
670 help_buf = tmp_help_txt; |
|
671 |
581
|
672 int status = yyparse (); |
|
673 |
|
674 if (status != 0) |
|
675 { |
|
676 error ("parse error while reading function file %s", ff); |
|
677 global_sym_tab->clear (curr_fcn_file_name); |
|
678 } |
|
679 } |
|
680 else if (exec_script) |
|
681 { |
|
682 // The value of `reading_fcn_file' will be restored to the proper value |
|
683 // when we unwind from this frame. |
|
684 reading_fcn_file = old_reading_fcn_file_state; |
|
685 |
|
686 unwind_protect_int (reading_script_file); |
|
687 reading_script_file = 1; |
|
688 |
|
689 parse_and_execute (ffile, 1); |
|
690 |
|
691 script_file_executed = 1; |
|
692 } |
|
693 fclose (ffile); |
|
694 } |
|
695 |
|
696 run_unwind_frame ("parse_fcn_file"); |
|
697 |
|
698 return script_file_executed; |
|
699 } |
|
700 |
593
|
701 static int |
581
|
702 load_fcn_from_file (symbol_record *sym_rec, int exec_script) |
|
703 { |
|
704 int script_file_executed = 0; |
|
705 |
|
706 char *nm = sym_rec->name (); |
|
707 |
704
|
708 // This is needed by yyparse. |
|
709 |
581
|
710 curr_fcn_file_name = nm; |
|
711 |
704
|
712 #ifdef WITH_DLD |
581
|
713 |
704
|
714 if (load_octave_oct_file (nm)) |
|
715 { |
|
716 force_link_to_function (nm); |
|
717 } |
|
718 else |
581
|
719 |
704
|
720 #endif |
581
|
721 |
|
722 { |
704
|
723 char *ff = fcn_file_in_path (nm); |
581
|
724 |
|
725 if (ff) |
|
726 { |
|
727 script_file_executed = parse_fcn_file (exec_script, ff); |
|
728 delete [] ff; |
|
729 } |
|
730 |
|
731 if (! (error_state || script_file_executed)) |
|
732 force_link_to_function (nm); |
|
733 } |
|
734 |
|
735 return script_file_executed; |
|
736 } |
|
737 |
|
738 int |
|
739 lookup (symbol_record *sym_rec, int exec_script) |
|
740 { |
|
741 int script_file_executed = 0; |
|
742 |
|
743 if (! sym_rec->is_linked_to_global ()) |
|
744 { |
|
745 if (sym_rec->is_defined ()) |
|
746 { |
|
747 if (sym_rec->is_function () && symbol_out_of_date (sym_rec)) |
593
|
748 script_file_executed = load_fcn_from_file (sym_rec, exec_script); |
581
|
749 } |
|
750 else if (! sym_rec->is_formal_parameter ()) |
|
751 { |
|
752 link_to_builtin_or_function (sym_rec); |
|
753 |
|
754 if (! sym_rec->is_defined ()) |
593
|
755 script_file_executed = load_fcn_from_file (sym_rec, exec_script); |
581
|
756 else if (sym_rec->is_function () && symbol_out_of_date (sym_rec)) |
593
|
757 script_file_executed = load_fcn_from_file (sym_rec, exec_script); |
581
|
758 } |
|
759 } |
|
760 |
|
761 return script_file_executed; |
|
762 } |
|
763 |
|
764 // Get the symbol record for the given name that is visible in the |
|
765 // current scope. Reread any function definitions that appear to be |
|
766 // out of date. If a function is available in a file but is not |
|
767 // currently loaded, this will load it and insert the name in the |
|
768 // current symbol table. |
|
769 |
|
770 symbol_record * |
|
771 lookup_by_name (const char *nm, int exec_script) |
|
772 { |
|
773 symbol_record *sym_rec = curr_sym_tab->lookup (nm, 1, 0); |
|
774 |
|
775 lookup (sym_rec, exec_script); |
|
776 |
|
777 return sym_rec; |
|
778 } |
|
779 |
991
|
780 char * |
993
|
781 get_help_from_file (const char *path) |
991
|
782 { |
|
783 if (path && *path) |
|
784 { |
|
785 FILE *fptr = fopen (path, "r"); |
|
786 if (fptr) |
|
787 { |
|
788 char *help_txt = gobble_leading_white_space (fptr); |
|
789 fclose (fptr); |
|
790 return help_txt; |
|
791 } |
|
792 } |
|
793 return 0; |
|
794 } |
|
795 |
593
|
796 // Variable values. |
195
|
797 |
581
|
798 // Look for the given name in the global symbol table. If it refers |
|
799 // to a string, return a new copy. If not, return 0; |
|
800 |
1
|
801 char * |
195
|
802 builtin_string_variable (const char *name) |
1
|
803 { |
195
|
804 symbol_record *sr = global_sym_tab->lookup (name, 0, 0); |
|
805 |
|
806 // It is a prorgramming error to look for builtins that aren't. |
|
807 |
529
|
808 assert (sr); |
195
|
809 |
529
|
810 char *retval = 0; |
1
|
811 |
490
|
812 tree_fvc *defn = sr->def (); |
195
|
813 |
529
|
814 if (defn) |
1
|
815 { |
|
816 tree_constant val = defn->eval (0); |
195
|
817 |
610
|
818 if (! error_state && val.is_string ()) |
1
|
819 { |
|
820 char *s = val.string_value (); |
636
|
821 |
529
|
822 if (s) |
1
|
823 retval = strsave (s); |
|
824 } |
|
825 } |
|
826 |
|
827 return retval; |
|
828 } |
|
829 |
581
|
830 // Look for the given name in the global symbol table. If it refers |
|
831 // to a real scalar, place the value in d and return 0. Otherwise, |
|
832 // return -1. |
|
833 |
1
|
834 int |
195
|
835 builtin_real_scalar_variable (const char *name, double& d) |
1
|
836 { |
|
837 int status = -1; |
195
|
838 symbol_record *sr = global_sym_tab->lookup (name, 0, 0); |
|
839 |
|
840 // It is a prorgramming error to look for builtins that aren't. |
|
841 |
529
|
842 assert (sr); |
1
|
843 |
490
|
844 tree_fvc *defn = sr->def (); |
195
|
845 |
529
|
846 if (defn) |
1
|
847 { |
|
848 tree_constant val = defn->eval (0); |
195
|
849 |
598
|
850 if (! error_state && val.is_scalar_type ()) |
1
|
851 { |
|
852 d = val.double_value (); |
|
853 status = 0; |
|
854 } |
|
855 } |
|
856 |
|
857 return status; |
|
858 } |
|
859 |
593
|
860 // Global stuff and links to builtin variables and functions. |
|
861 |
581
|
862 // Make the definition of the symbol record sr be the same as the |
|
863 // definition of the global variable of the same name, creating it if |
|
864 // it doesn't already exist. |
|
865 |
195
|
866 void |
|
867 link_to_global_variable (symbol_record *sr) |
|
868 { |
|
869 if (sr->is_linked_to_global ()) |
|
870 return; |
|
871 |
|
872 symbol_record *gsr = global_sym_tab->lookup (sr->name (), 1, 0); |
|
873 |
|
874 if (sr->is_formal_parameter ()) |
|
875 { |
|
876 error ("can't make function parameter `%s' global", sr->name ()); |
|
877 return; |
|
878 } |
|
879 |
|
880 // There must be a better way to do this. XXX FIXME XXX |
|
881 |
|
882 if (sr->is_variable ()) |
|
883 { |
|
884 // Would be nice not to have this cast. XXX FIXME XXX |
|
885 tree_constant *tmp = (tree_constant *) sr->def (); |
529
|
886 if (tmp) |
|
887 tmp = new tree_constant (*tmp); |
|
888 else |
207
|
889 tmp = new tree_constant (); |
195
|
890 gsr->define (tmp); |
|
891 } |
|
892 else |
529
|
893 sr->clear (); |
195
|
894 |
|
895 // If the global symbol is currently defined as a function, we need to |
|
896 // hide it with a variable. |
|
897 |
|
898 if (gsr->is_function ()) |
529
|
899 gsr->define ((tree_constant *) 0); |
195
|
900 |
|
901 sr->alias (gsr, 1); |
|
902 sr->mark_as_linked_to_global (); |
|
903 } |
|
904 |
581
|
905 // Make the definition of the symbol record sr be the same as the |
|
906 // definition of the builtin variable of the same name. |
|
907 |
195
|
908 void |
|
909 link_to_builtin_variable (symbol_record *sr) |
|
910 { |
|
911 symbol_record *tmp_sym = global_sym_tab->lookup (sr->name (), 0, 0); |
|
912 |
529
|
913 if (tmp_sym && tmp_sym->is_builtin_variable ()) |
|
914 sr->alias (tmp_sym); |
195
|
915 } |
|
916 |
581
|
917 // Make the definition of the symbol record sr be the same as the |
|
918 // definition of the builtin variable or function, or user function of |
|
919 // the same name, provided that the name has not been used as a formal |
|
920 // parameter. |
|
921 |
195
|
922 void |
|
923 link_to_builtin_or_function (symbol_record *sr) |
|
924 { |
|
925 symbol_record *tmp_sym = global_sym_tab->lookup (sr->name (), 0, 0); |
|
926 |
529
|
927 if (tmp_sym |
|
928 && (tmp_sym->is_builtin_variable () || tmp_sym->is_function ()) |
|
929 && ! tmp_sym->is_formal_parameter ()) |
|
930 sr->alias (tmp_sym); |
195
|
931 } |
|
932 |
581
|
933 // Force a link to a function in the current symbol table. This is |
|
934 // used just after defining a function to avoid different behavior |
|
935 // depending on whether or not the function has been evaluated after |
|
936 // being defined. |
|
937 // |
|
938 // Return without doing anything if there isn't a function with the |
|
939 // given name defined in the global symbol table. |
|
940 |
195
|
941 void |
|
942 force_link_to_function (const char *id_name) |
|
943 { |
|
944 symbol_record *gsr = global_sym_tab->lookup (id_name, 1, 0); |
|
945 if (gsr->is_function ()) |
|
946 { |
|
947 curr_sym_tab->clear (id_name); |
|
948 symbol_record *csr = curr_sym_tab->lookup (id_name, 1, 0); |
|
949 csr->alias (gsr); |
|
950 } |
|
951 } |
|
952 |
605
|
953 // Help stuff. Shouldn't this go in help.cc? |
593
|
954 |
|
955 // It's not likely that this does the right thing now. XXX FIXME XXX |
|
956 |
|
957 char ** |
|
958 make_name_list (void) |
|
959 { |
|
960 int key_len = 0; |
|
961 int glb_len = 0; |
|
962 int top_len = 0; |
|
963 int lcl_len = 0; |
|
964 int ffl_len = 0; |
|
965 |
|
966 char **key = 0; |
|
967 char **glb = 0; |
|
968 char **top = 0; |
|
969 char **lcl = 0; |
|
970 char **ffl = 0; |
|
971 |
|
972 // Each of these functions returns a new vector of pointers to new |
|
973 // strings. |
|
974 |
|
975 key = names (keyword_help (), key_len); |
|
976 glb = global_sym_tab->list (glb_len); |
|
977 top = top_level_sym_tab->list (top_len); |
|
978 if (top_level_sym_tab != curr_sym_tab) |
|
979 lcl = curr_sym_tab->list (lcl_len); |
|
980 ffl = get_fcn_file_names (ffl_len, 1); |
|
981 |
|
982 int total_len = key_len + glb_len + top_len + lcl_len + ffl_len; |
|
983 |
|
984 char **list = new char * [total_len+1]; |
|
985 |
|
986 // Put all the symbols in one big list. Only copy pointers, not the |
|
987 // strings they point to, then only delete the original array of |
|
988 // pointers, and not the strings they point to. |
|
989 |
|
990 int j = 0; |
|
991 int i = 0; |
|
992 for (i = 0; i < key_len; i++) |
|
993 list[j++] = key[i]; |
|
994 |
|
995 for (i = 0; i < glb_len; i++) |
|
996 list[j++] = glb[i]; |
|
997 |
|
998 for (i = 0; i < top_len; i++) |
|
999 list[j++] = top[i]; |
|
1000 |
|
1001 for (i = 0; i < lcl_len; i++) |
|
1002 list[j++] = lcl[i]; |
|
1003 |
|
1004 for (i = 0; i < ffl_len; i++) |
|
1005 list[j++] = ffl[i]; |
|
1006 |
|
1007 list[j] = 0; |
|
1008 |
|
1009 delete [] key; |
|
1010 delete [] glb; |
|
1011 delete [] top; |
|
1012 delete [] lcl; |
|
1013 delete [] ffl; |
|
1014 |
|
1015 return list; |
|
1016 } |
|
1017 |
|
1018 // List variable names. |
|
1019 |
|
1020 static void |
|
1021 print_symbol_info_line (ostrstream& output_buf, const symbol_record_info& s) |
|
1022 { |
|
1023 output_buf << (s.is_read_only () ? " -" : " w"); |
|
1024 output_buf << (s.is_eternal () ? "- " : "d "); |
|
1025 #if 0 |
|
1026 output_buf << (s.hides_fcn () ? "f" : (s.hides_builtin () ? "F" : "-")); |
|
1027 #endif |
|
1028 output_buf.form (" %-16s", s.type_as_string ()); |
|
1029 if (s.is_function ()) |
|
1030 output_buf << " - -"; |
|
1031 else |
|
1032 { |
|
1033 output_buf.form ("%7d", s.rows ()); |
|
1034 output_buf.form ("%7d", s.columns ()); |
|
1035 } |
|
1036 output_buf << " " << s.name () << "\n"; |
|
1037 } |
|
1038 |
|
1039 static void |
|
1040 print_long_listing (ostrstream& output_buf, symbol_record_info *s) |
|
1041 { |
|
1042 if (! s) |
|
1043 return; |
|
1044 |
|
1045 symbol_record_info *ptr = s; |
|
1046 while (ptr->is_defined ()) |
|
1047 { |
|
1048 print_symbol_info_line (output_buf, *ptr); |
|
1049 ptr++; |
|
1050 } |
|
1051 } |
|
1052 |
|
1053 static int |
867
|
1054 maybe_list (const char *header, char **argv, int argc, |
|
1055 ostrstream& output_buf, int show_verbose, symbol_table |
|
1056 *sym_tab, unsigned type, unsigned scope) |
593
|
1057 { |
|
1058 int count; |
|
1059 int status = 0; |
|
1060 if (show_verbose) |
|
1061 { |
|
1062 symbol_record_info *symbols; |
867
|
1063 symbols = sym_tab->long_list (count, argv, argc, 1, type, scope); |
593
|
1064 if (symbols && count > 0) |
|
1065 { |
|
1066 output_buf << "\n" << header << "\n\n" |
|
1067 << "prot type rows cols name\n" |
|
1068 << "==== ==== ==== ==== ====\n"; |
|
1069 |
|
1070 print_long_listing (output_buf, symbols); |
|
1071 status = 1; |
|
1072 } |
|
1073 delete [] symbols; |
|
1074 } |
|
1075 else |
|
1076 { |
867
|
1077 char **symbols = sym_tab->list (count, argv, argc, 1, type, scope); |
593
|
1078 if (symbols && count > 0) |
|
1079 { |
|
1080 output_buf << "\n" << header << "\n\n"; |
|
1081 list_in_columns (output_buf, symbols); |
|
1082 status = 1; |
|
1083 } |
|
1084 delete [] symbols; |
|
1085 } |
|
1086 return status; |
|
1087 } |
|
1088 |
|
1089 DEFUN_TEXT ("document", Fdocument, Sdocument, -1, 1, |
|
1090 "document symbol string ...\n\ |
|
1091 \n\ |
|
1092 Associate a cryptic message with a variable name.") |
|
1093 { |
|
1094 Octave_object retval; |
|
1095 |
|
1096 DEFINE_ARGV("document"); |
|
1097 |
|
1098 if (argc == 3) |
|
1099 { |
|
1100 char *name = argv[1]; |
|
1101 char *help = argv[2]; |
|
1102 |
|
1103 if (is_builtin_variable (name)) |
|
1104 error ("sorry, can't redefine help for builtin variables"); |
|
1105 else |
|
1106 { |
|
1107 symbol_record *sym_rec = curr_sym_tab->lookup (name, 0); |
|
1108 |
|
1109 if (sym_rec) |
|
1110 sym_rec->document (help); |
|
1111 else |
|
1112 error ("document: no such symbol `%s'", name); |
|
1113 } |
|
1114 } |
|
1115 else |
|
1116 print_usage ("document"); |
|
1117 |
|
1118 DELETE_ARGV; |
|
1119 |
|
1120 return retval; |
|
1121 } |
|
1122 |
584
|
1123 // XXX FIXME XXX -- this should take a list of regular expressions |
|
1124 // naming the variables to look for. |
|
1125 |
581
|
1126 static Octave_object |
|
1127 do_who (int argc, char **argv, int nargout) |
529
|
1128 { |
|
1129 Octave_object retval; |
|
1130 |
|
1131 int show_builtins = 0; |
|
1132 int show_functions = (curr_sym_tab == top_level_sym_tab); |
|
1133 int show_variables = 1; |
|
1134 int show_verbose = 0; |
|
1135 |
584
|
1136 char *my_name = argv[0]; |
|
1137 |
529
|
1138 if (argc > 1) |
|
1139 { |
|
1140 show_functions = 0; |
|
1141 show_variables = 0; |
|
1142 } |
|
1143 |
867
|
1144 while (--argc > 0) |
529
|
1145 { |
|
1146 argv++; |
867
|
1147 |
529
|
1148 if (strcmp (*argv, "-all") == 0 || strcmp (*argv, "-a") == 0) |
|
1149 { |
|
1150 show_builtins++; |
|
1151 show_functions++; |
|
1152 show_variables++; |
|
1153 } |
605
|
1154 else if (strcmp (*argv, "-builtins") == 0 || strcmp (*argv, "-b") == 0) |
529
|
1155 show_builtins++; |
605
|
1156 else if (strcmp (*argv, "-functions") == 0 || strcmp (*argv, "-f") == 0) |
529
|
1157 show_functions++; |
605
|
1158 else if (strcmp (*argv, "-long") == 0 || strcmp (*argv, "-l") == 0) |
|
1159 show_verbose++; |
|
1160 else if (strcmp (*argv, "-variables") == 0 || strcmp (*argv, "-v") == 0) |
529
|
1161 show_variables++; |
867
|
1162 else if (*argv[0] == '-') |
|
1163 warning ("%s: unrecognized option `%s'", my_name, *argv); |
529
|
1164 else |
867
|
1165 break; |
529
|
1166 } |
|
1167 |
|
1168 // If the user specified -l and nothing else, show variables. If |
|
1169 // evaluating this at the top level, also show functions. |
|
1170 |
|
1171 if (show_verbose && ! (show_builtins || show_functions || show_variables)) |
|
1172 { |
|
1173 show_functions = (curr_sym_tab == top_level_sym_tab); |
|
1174 show_variables = 1; |
|
1175 } |
|
1176 |
|
1177 ostrstream output_buf; |
|
1178 int pad_after = 0; |
|
1179 |
|
1180 if (show_builtins) |
|
1181 { |
867
|
1182 pad_after += maybe_list ("*** built-in variables:", argv, argc, |
529
|
1183 output_buf, show_verbose, global_sym_tab, |
|
1184 symbol_def::BUILTIN_VARIABLE, |
|
1185 SYMTAB_ALL_SCOPES); |
|
1186 |
867
|
1187 pad_after += maybe_list ("*** built-in functions:", argv, argc, |
529
|
1188 output_buf, show_verbose, global_sym_tab, |
|
1189 symbol_def::BUILTIN_FUNCTION, |
|
1190 SYMTAB_ALL_SCOPES); |
|
1191 } |
|
1192 |
|
1193 if (show_functions) |
|
1194 { |
|
1195 pad_after += maybe_list ("*** currently compiled functions:", |
867
|
1196 argv, argc, output_buf, show_verbose, |
|
1197 global_sym_tab, symbol_def::USER_FUNCTION, |
529
|
1198 SYMTAB_ALL_SCOPES); |
|
1199 } |
|
1200 |
|
1201 if (show_variables) |
|
1202 { |
867
|
1203 pad_after += maybe_list ("*** local user variables:", argv, argc, |
529
|
1204 output_buf, show_verbose, curr_sym_tab, |
|
1205 symbol_def::USER_VARIABLE, |
|
1206 SYMTAB_LOCAL_SCOPE); |
|
1207 |
|
1208 pad_after += maybe_list ("*** globally visible user variables:", |
867
|
1209 argv, argc, output_buf, show_verbose, |
|
1210 curr_sym_tab, symbol_def::USER_VARIABLE, |
529
|
1211 SYMTAB_GLOBAL_SCOPE); |
|
1212 } |
|
1213 |
|
1214 if (pad_after) |
|
1215 output_buf << "\n"; |
|
1216 |
|
1217 output_buf << ends; |
|
1218 maybe_page_output (output_buf); |
|
1219 |
581
|
1220 return retval; |
|
1221 } |
|
1222 |
|
1223 DEFUN_TEXT ("who", Fwho, Swho, -1, 1, |
|
1224 "who [-all] [-builtins] [-functions] [-long] [-variables]\n\ |
|
1225 \n\ |
|
1226 List currently defined symbol(s). Options may be shortened to one\n\ |
|
1227 character, but may not be combined.") |
|
1228 { |
|
1229 Octave_object retval; |
|
1230 |
|
1231 DEFINE_ARGV("who"); |
|
1232 |
|
1233 retval = do_who (argc, argv, nargout); |
|
1234 |
529
|
1235 DELETE_ARGV; |
|
1236 |
|
1237 return retval; |
|
1238 } |
|
1239 |
581
|
1240 DEFUN_TEXT ("whos", Fwhos, Swhos, -1, 1, |
|
1241 "whos [-all] [-builtins] [-functions] [-long] [-variables]\n\ |
|
1242 \n\ |
|
1243 List currently defined symbol(s). Options may be shortened to one\n\ |
|
1244 character, but may not be combined.") |
|
1245 { |
|
1246 Octave_object retval; |
|
1247 |
712
|
1248 int nargin = args.length (); |
|
1249 |
742
|
1250 Octave_object tmp_args; |
|
1251 for (int i = nargin; i > 0; i--) |
|
1252 tmp_args(i) = args(i-1); |
|
1253 tmp_args(0) = "-long"; |
581
|
1254 |
742
|
1255 int argc = tmp_args.length () + 1; |
581
|
1256 char **argv = make_argv (tmp_args, "whos"); |
|
1257 |
|
1258 if (error_state) |
|
1259 return retval; |
|
1260 |
|
1261 retval = do_who (argc, argv, nargout); |
|
1262 |
|
1263 while (--argc >= 0) |
|
1264 delete [] argv[argc]; |
|
1265 delete [] argv; |
|
1266 |
|
1267 return retval; |
|
1268 } |
|
1269 |
593
|
1270 // Install variables and functions in the symbol tables. |
529
|
1271 |
593
|
1272 void |
|
1273 install_builtin_mapper (builtin_mapper_function *mf) |
529
|
1274 { |
593
|
1275 symbol_record *sym_rec = global_sym_tab->lookup (mf->name, 1); |
|
1276 sym_rec->unprotect (); |
|
1277 |
|
1278 Mapper_fcn mfcn; |
628
|
1279 mfcn.name = strsave (mf->name); |
593
|
1280 mfcn.can_return_complex_for_real_arg = mf->can_return_complex_for_real_arg; |
|
1281 mfcn.lower_limit = mf->lower_limit; |
|
1282 mfcn.upper_limit = mf->upper_limit; |
|
1283 mfcn.d_d_mapper = mf->d_d_mapper; |
|
1284 mfcn.d_c_mapper = mf->d_c_mapper; |
|
1285 mfcn.c_c_mapper = mf->c_c_mapper; |
|
1286 |
722
|
1287 tree_builtin *def = new tree_builtin (1, 1, mfcn, mf->name); |
593
|
1288 |
|
1289 sym_rec->define (def); |
|
1290 |
|
1291 sym_rec->document (mf->help_string); |
|
1292 sym_rec->make_eternal (); |
|
1293 sym_rec->protect (); |
|
1294 } |
|
1295 |
|
1296 void |
|
1297 install_builtin_function (builtin_function *f) |
|
1298 { |
|
1299 symbol_record *sym_rec = global_sym_tab->lookup (f->name, 1); |
|
1300 sym_rec->unprotect (); |
|
1301 |
|
1302 tree_builtin *def = new tree_builtin (f->nargin_max, f->nargout_max, |
|
1303 f->fcn, f->name); |
|
1304 |
|
1305 sym_rec->define (def, f->is_text_fcn); |
|
1306 |
|
1307 sym_rec->document (f->help_string); |
|
1308 sym_rec->make_eternal (); |
|
1309 sym_rec->protect (); |
|
1310 } |
|
1311 |
|
1312 void |
|
1313 install_builtin_variable (builtin_variable *v) |
|
1314 { |
|
1315 if (v->install_as_function) |
|
1316 install_builtin_variable_as_function (v->name, v->value, v->protect, |
|
1317 v->eternal, v->help_string); |
529
|
1318 else |
593
|
1319 bind_builtin_variable (v->name, v->value, v->protect, v->eternal, |
|
1320 v->sv_function, v->help_string); |
529
|
1321 } |
|
1322 |
593
|
1323 void |
|
1324 install_builtin_variable_as_function (const char *name, tree_constant *val, |
|
1325 int protect, int eternal, |
|
1326 const char *help) |
529
|
1327 { |
593
|
1328 symbol_record *sym_rec = global_sym_tab->lookup (name, 1); |
|
1329 sym_rec->unprotect (); |
|
1330 |
|
1331 const char *tmp_help = help; |
|
1332 if (! help) |
|
1333 tmp_help = sym_rec->help (); |
|
1334 |
|
1335 sym_rec->define_as_fcn (val); |
|
1336 |
|
1337 sym_rec->document (tmp_help); |
|
1338 |
|
1339 if (protect) |
|
1340 sym_rec->protect (); |
|
1341 |
|
1342 if (eternal) |
|
1343 sym_rec->make_eternal (); |
|
1344 } |
|
1345 |
|
1346 void |
|
1347 alias_builtin (const char *alias, const char *name) |
|
1348 { |
|
1349 symbol_record *sr_name = global_sym_tab->lookup (name, 0, 0); |
|
1350 if (! sr_name) |
|
1351 panic ("can't alias to undefined name!"); |
|
1352 |
|
1353 symbol_record *sr_alias = global_sym_tab->lookup (alias, 1, 0); |
|
1354 |
|
1355 if (sr_alias) |
|
1356 sr_alias->alias (sr_name); |
|
1357 else |
770
|
1358 panic ("can't find symbol record for builtin function `%s'", alias); |
529
|
1359 } |
|
1360 |
593
|
1361 // Defining variables. |
|
1362 |
|
1363 void |
|
1364 bind_nargin_and_nargout (symbol_table *sym_tab, int nargin, int nargout) |
529
|
1365 { |
593
|
1366 tree_constant *tmp; |
|
1367 symbol_record *sr; |
|
1368 |
|
1369 sr = sym_tab->lookup ("nargin", 1, 0); |
|
1370 sr->unprotect (); |
712
|
1371 tmp = new tree_constant (nargin); |
593
|
1372 sr->define (tmp); |
|
1373 |
|
1374 sr = sym_tab->lookup ("nargout", 1, 0); |
|
1375 sr->unprotect (); |
|
1376 tmp = new tree_constant (nargout); |
|
1377 sr->define (tmp); |
|
1378 } |
|
1379 |
|
1380 // Give a global variable a definition. This will insert the symbol |
|
1381 // in the global table if necessary. |
|
1382 |
|
1383 // How is this different than install_builtin_variable? Are both |
|
1384 // functions needed? |
|
1385 |
|
1386 void |
|
1387 bind_builtin_variable (const char *varname, tree_constant *val, |
|
1388 int protect, int eternal, sv_Function sv_fcn, |
|
1389 const char *help) |
|
1390 { |
|
1391 symbol_record *sr = global_sym_tab->lookup (varname, 1, 0); |
|
1392 |
|
1393 // It is a programming error for a builtin symbol to be missing. |
|
1394 // Besides, we just inserted it, so it must be there. |
|
1395 |
|
1396 assert (sr); |
|
1397 |
|
1398 sr->unprotect (); |
|
1399 |
|
1400 // Must do this before define, since define will call the special |
|
1401 // variable function only if it knows about it, and it needs to, so |
|
1402 // that user prefs can be properly initialized. |
|
1403 |
|
1404 if (sv_fcn) |
|
1405 sr->set_sv_function (sv_fcn); |
|
1406 |
|
1407 sr->define_builtin_var (val); |
|
1408 |
|
1409 if (protect) |
|
1410 sr->protect (); |
|
1411 |
|
1412 if (eternal) |
|
1413 sr->make_eternal (); |
|
1414 |
|
1415 if (help) |
|
1416 sr->document (help); |
529
|
1417 } |
|
1418 |
593
|
1419 void |
|
1420 install_builtin_variables (void) |
529
|
1421 { |
593
|
1422 // XXX FIXME XX -- these should probably be moved to where they |
|
1423 // logically belong instead of being all grouped here. |
|
1424 |
|
1425 DEFVAR ("EDITOR", SBV_EDITOR, editor, 0, 0, 1, sv_editor, |
|
1426 "name of the editor to be invoked by the edit_history command"); |
|
1427 |
|
1428 DEFVAR ("I", SBV_I, Complex (0.0, 1.0), 0, 1, 1, 0, |
|
1429 "sqrt (-1)"); |
|
1430 |
|
1431 DEFVAR ("Inf", SBV_Inf, octave_Inf, 0, 1, 1, 0, |
|
1432 "infinity"); |
|
1433 |
|
1434 DEFVAR ("INFO_FILE", SBV_INFO_FILE, info_file, 0, 0, 1, sv_info_file, |
|
1435 "name of the Octave info file"); |
|
1436 |
|
1437 DEFVAR ("J", SBV_J, Complex (0.0, 1.0), 0, 1, 1, 0, |
|
1438 "sqrt (-1)"); |
529
|
1439 |
593
|
1440 #if defined (HAVE_ISNAN) |
|
1441 DEFVAR ("NaN", SBV_NaN, octave_NaN, 0, 1, 1, 0, |
|
1442 "not a number"); |
|
1443 #endif |
|
1444 |
|
1445 DEFVAR ("LOADPATH", SBV_LOADPATH, load_path, 0, 0, 1, sv_loadpath, |
|
1446 "colon separated list of directories to search for scripts"); |
|
1447 |
686
|
1448 DEFVAR ("IMAGEPATH", SBV_IMAGEPATH, OCTAVE_IMAGEPATH, 0, 0, 1, |
|
1449 sv_imagepath, |
|
1450 "colon separated list of directories to search for image files"); |
|
1451 |
664
|
1452 DEFVAR ("OCTAVE_VERSION", SBV_version, version_string, 0, 1, 1, 0, |
|
1453 "Octave version"); |
|
1454 |
593
|
1455 DEFVAR ("PAGER", SBV_PAGER, default_pager (), 0, 0, 1, sv_pager_binary, |
|
1456 "path to pager binary"); |
|
1457 |
|
1458 DEFVAR ("PS1", SBV_PS1, "\\s:\\#> ", 0, 0, 1, sv_ps1, |
|
1459 "primary prompt string"); |
|
1460 |
|
1461 DEFVAR ("PS2", SBV_PS2, "> ", 0, 0, 1, sv_ps2, |
|
1462 "secondary prompt string"); |
|
1463 |
1044
|
1464 DEFVAR ("PS4", SBV_PS4, "+ ", 0, 0, 1, sv_ps4, |
|
1465 "string printed before echoed input (enabled by --echo-input)"); |
|
1466 |
593
|
1467 DEFVAR ("PWD", SBV_PWD, get_working_directory ("initialize_globals"), |
|
1468 0, 1, 1, sv_pwd, |
|
1469 "current working directory"); |
|
1470 |
|
1471 DEFVAR ("SEEK_SET", SBV_SEEK_SET, 0.0, 0, 1, 1, 0, |
|
1472 "used with fseek to position file relative to the beginning"); |
529
|
1473 |
593
|
1474 DEFVAR ("SEEK_CUR", SBV_SEEK_CUR, 1.0, 0, 1, 1, 0, |
|
1475 "used with fseek to position file relative to the current position"); |
|
1476 |
|
1477 DEFVAR ("SEEK_END", SBV_SEEK_END, 2.0, 0, 1, 1, 0, |
|
1478 "used with fseek to position file relative to the end"); |
|
1479 |
|
1480 DEFVAR ("ans", SBV_ans, , 0, 0, 1, 0, |
|
1481 ""); |
|
1482 |
661
|
1483 DEFVAR ("automatic_replot", SBV_automatic_replot, "false", |
|
1484 0, 0, 1, automatic_replot, |
|
1485 "if true, auto-insert a replot command when a plot changes"); |
|
1486 |
984
|
1487 DEFVAR ("whitespace_in_literal_matrix", SBV_whitespace_in_literal_matrix, "", |
|
1488 0, 0, 1, whitespace_in_literal_matrix, |
|
1489 "control auto-insertion of commas and semicolons in literal matrices"); |
593
|
1490 |
605
|
1491 DEFVAR ("default_save_format", SBV_default_save_format, "ascii", |
|
1492 0, 0, 1, sv_default_save_format, |
997
|
1493 "default format for files created with save, may be one of\n\ |
|
1494 \"binary\", \"text\", or \"mat-binary\""); |
605
|
1495 |
593
|
1496 DEFVAR ("do_fortran_indexing", SBV_do_fortran_indexing, "false", 0, 0, |
|
1497 1, do_fortran_indexing, |
|
1498 "allow single indices for matrices"); |
|
1499 |
|
1500 DEFVAR ("empty_list_elements_ok", SBV_empty_list_elements_ok, "warn", |
|
1501 0, 0, 1, empty_list_elements_ok, |
|
1502 "ignore the empty element in expressions like `a = [[], 1]'"); |
529
|
1503 |
593
|
1504 DEFVAR ("eps", SBV_eps, DBL_EPSILON, 0, 1, 1, 0, |
|
1505 "machine precision"); |
|
1506 |
|
1507 DEFVAR ("gnuplot_binary", SBV_gnuplot_binary, "gnuplot", 0, 0, 1, |
|
1508 sv_gnuplot_binary, |
|
1509 "path to gnuplot binary"); |
|
1510 |
|
1511 DEFVAR ("i", SBV_i, Complex (0.0, 1.0), 1, 1, 1, 0, |
|
1512 "sqrt (-1)"); |
529
|
1513 |
593
|
1514 DEFVAR ("ignore_function_time_stamp", SBV_ignore_function_time_stamp, |
|
1515 "system", 0, 0, 1, |
|
1516 ignore_function_time_stamp, |
|
1517 "don't check to see if function files have changed since they were\n\ |
|
1518 last compiled. Possible values are \"system\" and \"all\""); |
|
1519 |
|
1520 DEFVAR ("implicit_str_to_num_ok", SBV_implicit_str_to_num_ok, "false", |
|
1521 0, 0, 1, implicit_str_to_num_ok, |
|
1522 "allow implicit string to number conversion"); |
|
1523 |
|
1524 DEFVAR ("inf", SBV_inf, octave_Inf, 0, 1, 1, 0, |
|
1525 "infinity"); |
|
1526 |
|
1527 DEFVAR ("j", SBV_j, Complex (0.0, 1.0), 1, 1, 1, 0, |
|
1528 "sqrt (-1)"); |
529
|
1529 |
593
|
1530 #if defined (HAVE_ISNAN) |
|
1531 DEFVAR ("nan", SBV_nan, octave_NaN, 0, 1, 1, 0, |
|
1532 "not a number"); |
|
1533 #endif |
|
1534 |
|
1535 DEFVAR ("ok_to_lose_imaginary_part", SBV_ok_to_lose_imaginary_part, |
|
1536 "warn", 0, 0, 1, ok_to_lose_imaginary_part, |
|
1537 "silently convert from complex to real by dropping imaginary part"); |
|
1538 |
|
1539 DEFVAR ("output_max_field_width", SBV_output_max_field_width, 10.0, 0, |
|
1540 0, 1, set_output_max_field_width, |
|
1541 "maximum width of an output field for numeric output"); |
|
1542 |
|
1543 DEFVAR ("output_precision", SBV_output_precision, 5.0, 0, 0, 1, |
|
1544 set_output_precision, |
|
1545 "number of significant figures to display for numeric output"); |
|
1546 |
|
1547 DEFVAR ("page_screen_output", SBV_page_screen_output, "true", 0, 0, 1, |
|
1548 page_screen_output, |
|
1549 "if possible, send output intended for the screen through the pager"); |
529
|
1550 |
593
|
1551 DEFVAR ("pi", SBV_pi, 4.0 * atan (1.0), 0, 1, 1, 0, |
|
1552 "ratio of the circumference of a circle to its diameter"); |
|
1553 |
|
1554 DEFVAR ("prefer_column_vectors", SBV_prefer_column_vectors, "true", 0, |
|
1555 0, 1, prefer_column_vectors, |
|
1556 "prefer column/row vectors"); |
|
1557 |
|
1558 DEFVAR ("prefer_zero_one_indexing", SBV_prefer_zero_one_indexing, |
|
1559 "false", 0, 0, 1, prefer_zero_one_indexing, |
|
1560 "when there is a conflict, prefer zero-one style indexing"); |
|
1561 |
|
1562 DEFVAR ("print_answer_id_name", SBV_print_answer_id_name, "true", 0, |
|
1563 0, 1, print_answer_id_name, |
|
1564 "set output style to print `var_name = ...'"); |
|
1565 |
|
1566 DEFVAR ("print_empty_dimensions", SBV_print_empty_dimensions, "true", |
|
1567 0, 0, 1, print_empty_dimensions, |
|
1568 "also print dimensions of empty matrices"); |
|
1569 |
|
1570 DEFVAR ("propagate_empty_matrices", SBV_propagate_empty_matrices, |
|
1571 "true", 0, 0, 1, propagate_empty_matrices, |
|
1572 "operations on empty matrices return an empty matrix, not an error"); |
529
|
1573 |
868
|
1574 DEFVAR ("realmax", SBV_realmax, DBL_MAX, 1, 1, 1, 0, |
|
1575 "realmax (): return largest representable floating point number"); |
|
1576 |
|
1577 DEFVAR ("realmin", SBV_realmin, DBL_MIN, 1, 1, 1, 0, |
|
1578 "realmin (): return smallest representable floating point number"); |
|
1579 |
593
|
1580 DEFVAR ("resize_on_range_error", SBV_resize_on_range_error, "true", 0, |
|
1581 0, 1, resize_on_range_error, |
|
1582 "enlarge matrices on assignment"); |
|
1583 |
|
1584 DEFVAR ("return_last_computed_value", SBV_return_last_computed_value, |
|
1585 "false", 0, 0, 1, |
|
1586 return_last_computed_value, |
|
1587 "if a function does not return any values explicitly, return the\n\ |
|
1588 last computed value"); |
|
1589 |
|
1590 DEFVAR ("save_precision", SBV_save_precision, 17.0, 0, 0, 1, |
|
1591 set_save_precision, |
|
1592 "number of significant figures kept by the ASCII save command"); |
|
1593 |
|
1594 DEFVAR ("silent_functions", SBV_silent_functions, "false", 0, 0, 1, |
|
1595 silent_functions, |
|
1596 "suppress printing results in called functions"); |
|
1597 |
|
1598 DEFVAR ("split_long_rows", SBV_split_long_rows, "true", 0, 0, 1, |
|
1599 split_long_rows, |
|
1600 "split long matrix rows instead of wrapping"); |
529
|
1601 |
593
|
1602 DEFVAR ("stdin", SBV_stdin, 0.0, 0, 1, 1, 0, |
|
1603 "file number of the standard input stream"); |
|
1604 |
|
1605 DEFVAR ("stdout", SBV_stdout, 1.0, 0, 1, 1, 0, |
|
1606 "file number of the standard output stream"); |
|
1607 |
|
1608 DEFVAR ("stderr", SBV_stderr, 2.0, 0, 1, 1, 0, |
|
1609 "file number of the standard error stream"); |
|
1610 |
|
1611 DEFVAR ("treat_neg_dim_as_zero", SBV_treat_neg_dim_as_zero, "false", |
|
1612 0, 0, 1, treat_neg_dim_as_zero, |
|
1613 "convert negative dimensions to zero"); |
|
1614 |
|
1615 DEFVAR ("warn_assign_as_truth_value", SBV_warn_assign_as_truth_value, |
|
1616 "true", 0, 0, 1, |
|
1617 warn_assign_as_truth_value, |
|
1618 "produce warning for assignments used as truth values"); |
|
1619 |
|
1620 DEFVAR ("warn_comma_in_global_decl", SBV_warn_comma_in_global_decl, |
|
1621 "true", 0, 0, 1, warn_comma_in_global_decl, |
|
1622 "produce warning for commas in global declarations"); |
|
1623 |
|
1624 DEFVAR ("warn_divide_by_zero", SBV_warn_divide_by_zero, "true", 0, 0, |
|
1625 1, warn_divide_by_zero, |
|
1626 "on IEEE machines, allow divide by zero errors to be suppressed"); |
1037
|
1627 |
|
1628 DEFVAR ("warn_function_name_clash", SBV_warn_function_name_clash, |
|
1629 "true", 0, 0, 1, warn_function_name_clash, |
|
1630 "produce warning if function name conflicts with file name"); |
529
|
1631 } |
|
1632 |
593
|
1633 // Deleting names from the symbol tables. |
|
1634 |
|
1635 DEFUN_TEXT ("clear", Fclear, Sclear, -1, 1, |
668
|
1636 "clear [-x] [name ...]\n\ |
|
1637 \n\ |
|
1638 Clear symbol(s) matching a list of globbing patterns.\n\ |
593
|
1639 \n\ |
668
|
1640 If no arguments are given, clear all user-defined variables and |
|
1641 functions.\n\ |
|
1642 \n\ |
|
1643 With -x, exclude the named variables") |
529
|
1644 { |
593
|
1645 Octave_object retval; |
|
1646 |
|
1647 DEFINE_ARGV("clear"); |
|
1648 |
668
|
1649 argc--; |
|
1650 argv++; |
|
1651 |
593
|
1652 // Always clear the local table, but don't clear currently compiled |
|
1653 // functions unless we are at the top level. (Allowing that to happen |
|
1654 // inside functions would result in pretty odd behavior...) |
|
1655 |
|
1656 int clear_user_functions = (curr_sym_tab == top_level_sym_tab); |
|
1657 |
668
|
1658 if (argc == 0) |
593
|
1659 { |
|
1660 curr_sym_tab->clear (); |
|
1661 global_sym_tab->clear (clear_user_functions); |
|
1662 } |
529
|
1663 else |
|
1664 { |
668
|
1665 int exclusive = 0; |
|
1666 |
|
1667 if (argc > 0) |
|
1668 { |
|
1669 if (strcmp (*argv, "-x") == 0) |
|
1670 { |
|
1671 exclusive = 1; |
|
1672 argv++; |
|
1673 argc--; |
|
1674 } |
|
1675 } |
|
1676 |
|
1677 int lcount = 0; |
|
1678 int gcount = 0; |
|
1679 int fcount = 0; |
529
|
1680 |
668
|
1681 char **lvars = 0; |
|
1682 char **gvars = 0; |
|
1683 char **fcns = 0; |
|
1684 |
|
1685 if (argc > 0) |
593
|
1686 { |
893
|
1687 lvars = curr_sym_tab->list (lcount, 0, 0, 0, |
|
1688 symbol_def::USER_VARIABLE, |
668
|
1689 SYMTAB_LOCAL_SCOPE); |
|
1690 |
893
|
1691 gvars = curr_sym_tab->list (gcount, 0, 0, 0, |
|
1692 symbol_def::USER_VARIABLE, |
668
|
1693 SYMTAB_GLOBAL_SCOPE); |
|
1694 |
893
|
1695 fcns = global_sym_tab->list (fcount, 0, 0, 0, |
|
1696 symbol_def::USER_FUNCTION, |
864
|
1697 SYMTAB_ALL_SCOPES); |
668
|
1698 } |
|
1699 |
|
1700 while (argc > 0) |
|
1701 { |
|
1702 char *pat = *argv; |
|
1703 |
|
1704 if (pat) |
593
|
1705 { |
|
1706 int i; |
|
1707 for (i = 0; i < lcount; i++) |
|
1708 { |
668
|
1709 char *nm = lvars[i]; |
|
1710 int match = (fnmatch (pat, nm, __FNM_FLAGS) == 0); |
|
1711 if ((exclusive && ! match) || (! exclusive && match)) |
|
1712 curr_sym_tab->clear (nm); |
593
|
1713 } |
529
|
1714 |
593
|
1715 int count; |
|
1716 for (i = 0; i < gcount; i++) |
|
1717 { |
668
|
1718 char *nm = gvars[i]; |
|
1719 int match = (fnmatch (pat, nm, __FNM_FLAGS) == 0); |
|
1720 if ((exclusive && ! match) || (! exclusive && match)) |
593
|
1721 { |
668
|
1722 count = curr_sym_tab->clear (nm); |
593
|
1723 if (count > 0) |
668
|
1724 global_sym_tab->clear (nm, clear_user_functions); |
593
|
1725 } |
|
1726 } |
529
|
1727 |
593
|
1728 for (i = 0; i < fcount; i++) |
|
1729 { |
668
|
1730 char *nm = fcns[i]; |
|
1731 int match = (fnmatch (pat, nm, __FNM_FLAGS) == 0); |
|
1732 if ((exclusive && ! match) || (! exclusive && match)) |
593
|
1733 { |
668
|
1734 count = curr_sym_tab->clear (nm); |
864
|
1735 global_sym_tab->clear (nm, clear_user_functions); |
593
|
1736 } |
|
1737 } |
|
1738 } |
668
|
1739 |
|
1740 argc--; |
|
1741 argv++; |
593
|
1742 } |
529
|
1743 |
593
|
1744 delete [] lvars; |
|
1745 delete [] gvars; |
|
1746 delete [] fcns; |
|
1747 |
|
1748 } |
|
1749 |
|
1750 DELETE_ARGV; |
|
1751 |
|
1752 return retval; |
529
|
1753 } |
|
1754 |
1
|
1755 /* |
|
1756 ;;; Local Variables: *** |
|
1757 ;;; mode: C++ *** |
|
1758 ;;; page-delimiter: "^/\\*" *** |
|
1759 ;;; End: *** |
|
1760 */ |