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