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