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