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