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