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