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